]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weapons.qc
checking for == 0 just in case
[divverent/nexuiz.git] / data / qcsrc / server / cl_weapons.qc
1
2 // generic weapons table
3 // add new weapons here
4 float(float wpn, float wrequest) weapon_action =
5 {
6         if (wpn == WEP_LASER)
7                 return w_laser(wrequest);
8         else if (wpn == WEP_SHOTGUN)
9                 return w_shotgun(wrequest);
10         else if (wpn == WEP_UZI)
11                 return w_uzi(wrequest);
12         else if (wpn == WEP_GRENADE_LAUNCHER)
13                 return w_glauncher(wrequest);
14         else if (wpn == WEP_ELECTRO)
15                 return w_electro(wrequest);
16         else if (wpn == WEP_CRYLINK)
17                 return w_crylink(wrequest);
18         else if (wpn == WEP_NEX)
19                 return w_nex(wrequest);
20         else if (wpn == WEP_HAGAR)
21                 return w_hagar(wrequest);
22         else if (wpn == WEP_ROCKET_LAUNCHER)
23                 return w_rlauncher(wrequest);
24         return FALSE;
25 };
26
27 // think function for tossed weapons
28 void() thrown_wep_think
29 {
30         self.solid = SOLID_TRIGGER;
31         self.owner = world;
32         SUB_SetFade(self, time + 20, 1);
33         setorigin(self, self.origin);
34 };
35
36 // toss current weapon
37 void(vector velo, vector delta, float doreduce) W_ThrowWeapon
38 {
39         local float w, ammo;
40         local entity wep, e;
41
42         w = self.weapon;
43         if(w == WEP_LASER)
44                 return;
45         if(w == 0)
46                 return; // just in case
47
48         e = self;
49         wep = spawn();
50         self = wep;
51
52         setorigin(wep, e.origin + delta);
53         makevectors(e.angles);
54         wep.classname = "droppedweapon";
55         wep.velocity = velo; // e.velocity * 0.5 + v_forward * 750;
56         SUB_SetFade(wep, time + 20, 1);
57
58         if(w == WEP_SHOTGUN)
59         {
60                 w = IT_SHOTGUN;
61                 if(!(e.items & w))
62                 {
63                         remove(wep);
64                         return;
65                 }
66                 weapon_shotgun();
67                 if(startitem_failed)
68                         return;
69                 if(doreduce)
70                 {
71                         ammo = min(e.ammo_shells, wep.ammo_shells);
72                         wep.ammo_shells = ammo;
73                         e.ammo_shells -= ammo;
74                 }
75         }
76         else if(w == WEP_UZI)
77         {
78                 w = IT_UZI;
79                 if(!(e.items & w))
80                 {
81                         remove(wep);
82                         return;
83                 }
84                 weapon_uzi();
85                 if(startitem_failed)
86                         return;
87                 if(doreduce)
88                 {
89                         ammo = min(e.ammo_nails, wep.ammo_nails);
90                         wep.ammo_nails = ammo;
91                         e.ammo_nails -= ammo;
92                 }
93         }
94         else if(w == WEP_GRENADE_LAUNCHER)
95         {
96                 w = IT_GRENADE_LAUNCHER;
97                 if(!(e.items & w))
98                 {
99                         remove(wep);
100                         return;
101                 }
102                 weapon_grenadelauncher();
103                 if(startitem_failed)
104                         return;
105                 if(doreduce)
106                 {
107                         ammo = min(e.ammo_rockets, wep.ammo_rockets);
108                         wep.ammo_rockets = ammo;
109                         e.ammo_rockets -= ammo;
110                 }
111         }
112         else if(w == WEP_ELECTRO)
113         {
114                 w = IT_ELECTRO;
115                 if(!(e.items & w))
116                 {
117                         remove(wep);
118                         return;
119                 }
120                 weapon_electro();
121                 if(startitem_failed)
122                         return;
123                 if(doreduce)
124                 {
125                         ammo = min(e.ammo_cells, wep.ammo_cells);
126                         wep.ammo_cells = ammo;
127                         e.ammo_cells -= ammo;
128                 }
129         }
130         else if(w == WEP_CRYLINK)
131         {
132                 w = IT_CRYLINK;
133                 if(!(e.items & w))
134                 {
135                         remove(wep);
136                         return;
137                 }
138                 weapon_crylink();
139                 if(startitem_failed)
140                         return;
141                 if(doreduce)
142                 {
143                         ammo = min(e.ammo_cells, wep.ammo_cells);
144                         wep.ammo_cells = ammo;
145                         e.ammo_cells -= ammo;
146                 }
147         }
148         else if(w == WEP_NEX)
149         {
150                 w = IT_NEX;
151                 if(!(e.items & w))
152                 {
153                         remove(wep);
154                         return;
155                 }
156                 weapon_nex();
157                 if(startitem_failed)
158                         return;
159                 if(doreduce)
160                 {
161                         ammo = min(e.ammo_cells, wep.ammo_cells);
162                         wep.ammo_cells = ammo;
163                         e.ammo_cells -= ammo;
164                 }
165         }
166         else if(w == WEP_HAGAR)
167         {
168                 w = IT_HAGAR;
169                 if(!(e.items & w))
170                 {
171                         remove(wep);
172                         return;
173                 }
174                 weapon_hagar();
175                 if(startitem_failed)
176                         return;
177                 if(doreduce)
178                 {
179                         ammo = min(e.ammo_rockets, wep.ammo_rockets);
180                         wep.ammo_rockets = ammo;
181                         e.ammo_rockets -= ammo;
182                 }
183         }
184         else if(w == WEP_ROCKET_LAUNCHER)
185         {
186                 w = IT_ROCKET_LAUNCHER;
187                 if(!(e.items & w))
188                 {
189                         remove(wep);
190                         return;
191                 }
192                 weapon_rocketlauncher();
193                 if(startitem_failed)
194                         return;
195                 if(doreduce)
196                 {
197                         ammo = min(e.ammo_rockets, wep.ammo_rockets);
198                         wep.ammo_rockets = ammo;
199                         e.ammo_rockets -= ammo;
200                 }
201         }
202
203         if(e.items & w)
204                 if(e.health >= 1)
205                         sprint(e, strcat("You dropped the ^2", wep.netname, "\n"));
206         wep.owner = e;
207         setorigin(wep, wep.origin);
208         wep.nextthink = time + 0.5;
209         wep.think = thrown_wep_think;
210         wep.classname = "droppedweapon";
211         wep.flags = wep.flags | FL_TOSSED;
212         e.items = e.items - (e.items & w);
213         e.switchweapon = w_getbestweapon(e);
214         wep.colormap = e.colormap;
215         if (e.switchweapon != e.weapon)
216                 e.cnt = e.weapon;
217         self = e;
218 };
219
220 // switch between weapons
221 void(float imp) W_SwitchWeapon
222 {
223         if (self.weapon != imp)
224         if (client_hasweapon(self, imp, TRUE, TRUE))
225         {
226                 self.cnt = self.weapon;
227                 self.switchweapon = imp;
228         }
229 };
230
231 // next weapon
232 void() W_NextWeapon =
233 {
234         local float weaponwant, maxtries;
235
236         maxtries = WEP_LAST;
237
238         weaponwant = self.switchweapon + 1;
239         if (weaponwant < WEP_FIRST)
240                 weaponwant = WEP_LAST;
241         if (weaponwant > WEP_LAST)
242                 weaponwant = WEP_FIRST;
243         while(!client_hasweapon(self, weaponwant, TRUE, FALSE))
244         {
245                 if(!maxtries)
246                         return;
247
248                 maxtries -= 1;
249                 weaponwant = weaponwant + 1;
250                 if (weaponwant < WEP_FIRST)
251                         weaponwant = WEP_LAST;
252                 if (weaponwant > WEP_LAST)
253                         weaponwant = WEP_FIRST;
254         }
255         self.cnt = self.weapon;
256         self.switchweapon = weaponwant;
257 };
258
259 // prev weapon
260 void() W_PreviousWeapon =
261 {
262         local float weaponwant, maxtries;
263
264         maxtries = WEP_LAST;
265
266         weaponwant = self.switchweapon - 1;
267         if (weaponwant < WEP_FIRST)
268                 weaponwant = WEP_LAST;
269         if (weaponwant > WEP_LAST)
270                 weaponwant = WEP_FIRST;
271         while(!client_hasweapon(self, weaponwant, TRUE, FALSE))
272         {
273                 if(!maxtries)
274                         return;
275
276                 maxtries -= 1;
277                 weaponwant = weaponwant - 1;
278                 if (weaponwant < WEP_FIRST)
279                         weaponwant = WEP_LAST;
280                 if (weaponwant > WEP_LAST)
281                         weaponwant = WEP_FIRST;
282         }
283         self.cnt = self.weapon;
284         self.switchweapon = weaponwant;
285 };
286
287 // Bringed back weapon frame
288 void() W_WeaponFrame =
289 {
290         if(arena_roundbased)
291         if(time < warmup)
292                 return;
293
294         if (!self.weaponentity || self.health < 1)
295                 return; // Dead player can't use weapons and injure impulse commands
296
297         if(!self.switchweapon)
298         {
299                 self.weapon = 0;
300                 self.weaponentity.state = WS_CLEAR;
301                 return;
302         }
303
304         makevectors(self.v_angle);
305
306         // Change weapon
307         if (self.weapon != self.switchweapon)
308         {
309                 if (self.weaponentity.state == WS_CLEAR)
310                 {
311                         self.weaponentity.state = WS_RAISE;
312                         weapon_action(self.switchweapon, WR_SETUP);
313                         // VorteX: add player model weapon select frame here
314                         // setcustomframe(PlayerWeaponRaise);
315                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready);
316                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
317                 }
318                 else if (self.weaponentity.state == WS_READY)
319                 {
320                         sound (self, CHAN_WEAPON, "weapons/weapon_switch.ogg", 1, ATTN_NORM);
321                         self.weaponentity.state = WS_DROP;
322                         // set up weapon switch think in the future, and start drop anim
323                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_clear);
324                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
325                 }
326         }
327
328         // call the think code which may fire the weapon
329         // and do so multiple times to resolve framerate dependency issues if the
330         // server framerate is very low and the weapon fire rate very high
331         local float c;
332         c = 0;
333         while (c < 5)
334         {
335                 c = c + 1;
336                 weapon_action(self.weapon, WR_THINK);
337                 if (self.weapon_nextthink > 0 && time >= self.weapon_nextthink)
338                 {
339                         self.weapon_nextthink_lastframe = self.weapon_nextthink;
340                         self.weapon_think();
341                 }
342         }
343
344         // don't let attack_finished fall behind when not firing
345         if (self.attack_finished < time)
346                 self.attack_finished = time;
347
348         // update currentammo incase it has changed
349         if (self.items & IT_CELLS)
350                 self.currentammo = self.ammo_cells;
351         else if (self.items & IT_ROCKETS)
352                 self.currentammo = self.ammo_rockets;
353         else if (self.items & IT_NAILS)
354                 self.currentammo = self.ammo_nails;
355         else if (self.items & IT_SHELLS)
356                 self.currentammo = self.ammo_shells;
357         else
358                 self.currentammo = 1;
359
360 };
361
362 float nixnex_weapon;
363 float nixnex_nextchange;
364 float nixnex_nextweapon;
365 .float nixnex_lastchange_id;
366 .float nixnex_lastinfotime;
367 .float nixnex_nextincr;
368
369 void Nixnex_ChooseNextWeapon()
370 {
371         float numberof, id;
372         numberof = WEP_LAST - WEP_FIRST; // all but the current one
373         if(cvar("g_nixnex_with_laser"))
374                 numberof = numberof - 1;
375         id = WEP_FIRST + ceil(random() * numberof) - 1;
376
377         if(cvar("g_nixnex_with_laser")) // skip the laser if needed
378                 id = id + 1;
379
380         if(id >= nixnex_weapon) // skip the current weapon
381                 id = id + 1;
382
383         if(id < WEP_FIRST) // can't happen, but to be sure...
384         {
385                 dprint("Won't happen (id < WEP_FIRST)\n");
386                 id = WEP_FIRST;
387         }
388         if(id > WEP_LAST) // either
389         {
390                 dprint("Won't happen (id > WEP_LAST)\n");
391                 id = WEP_LAST;
392         }
393
394         nixnex_nextweapon = id;
395 }
396
397 string W_Name(float weaponid)
398 {
399         if(weaponid == WEP_LASER)             return "Laser";
400         if(weaponid == WEP_UZI)               return "Machine Gun";
401         if(weaponid == WEP_SHOTGUN)           return "Shotgun";
402         if(weaponid == WEP_GRENADE_LAUNCHER)  return "Mortar";
403         if(weaponid == WEP_ELECTRO)           return "Electro";
404         if(weaponid == WEP_NEX)               return "Nex";
405         if(weaponid == WEP_HAGAR)             return "Hagar";
406         if(weaponid == WEP_ROCKET_LAUNCHER)   return "Rocket Launcher";
407         if(weaponid == WEP_CRYLINK)           return "Crylink";
408         return "@!#%'n Tuba";
409 }
410
411 void Nixnex_GiveCurrentWeapon()
412 {
413         float dt;
414         if(cvar("g_nixnex"))
415         {
416                 if(!nixnex_nextweapon)
417                         Nixnex_ChooseNextWeapon();
418
419                 dt = ceil(nixnex_nextchange - time);
420
421                 if(dt <= 0)
422                 {
423                         nixnex_weapon = nixnex_nextweapon;
424                         nixnex_nextweapon = 0;
425                         nixnex_nextchange = time + cvar("g_balance_nixnex_roundtime");
426                 }
427
428                 if(nixnex_nextchange != self.nixnex_lastchange_id) // this shall only be called once per round!
429                 {
430                         self.nixnex_lastchange_id = nixnex_nextchange;
431                         if (cvar("g_use_ammunition"))
432                         {
433                                 self.ammo_shells = cvar("g_balance_nixnex_ammo_shells");
434                                 self.ammo_nails = cvar("g_balance_nixnex_ammo_nails");
435                                 self.ammo_rockets = cvar("g_balance_nixnex_ammo_rockets");
436                                 self.ammo_cells = cvar("g_balance_nixnex_ammo_cells");
437                         }
438                         else
439                         {
440                                 self.ammo_shells = 999;
441                                 self.ammo_nails = 999;
442                                 self.ammo_rockets = 999;
443                                 self.ammo_cells = 999;
444                         }
445                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
446                         if(dt >= 1 && dt <= 5)
447                                 self.nixnex_lastinfotime = -42;
448                         else
449                         {
450                                 centermsg_free(CENTERMSG_NIXNEXCOUNT);
451                                 centermsg_set(CENTERMSG_NIXNEX, strcat("^2Active weapon: ^3", W_Name(nixnex_weapon)));
452                         }
453                 }
454                 if(self.nixnex_lastinfotime != dt)
455                 {
456                         self.nixnex_lastinfotime = dt; // initial value 0 should count as "not seen"
457                         if(dt >= 1 && dt <= 5)
458                         {
459                                 centermsg_set(CENTERMSG_NIXNEXCOUNT, strcat("^3", ftos(dt), "^2 seconds until weapon change..."));
460                                 centermsg_set(CENTERMSG_NIXNEX, strcat("Next weapon: ^3", W_Name(nixnex_nextweapon)));
461                         }
462                 }
463
464                 if(cvar("g_use_ammunition") && time > self.nixnex_nextincr)
465                 {
466                         self.ammo_shells = self.ammo_shells + cvar("g_balance_nixnex_ammoincr_shells");
467                         self.ammo_nails = self.ammo_nails + cvar("g_balance_nixnex_ammoincr_nails");
468                         self.ammo_rockets = self.ammo_rockets + cvar("g_balance_nixnex_ammoincr_rockets");
469                         self.ammo_cells = self.ammo_cells + cvar("g_balance_nixnex_ammoincr_cells");
470                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
471                 }
472
473                 self.items = self.items - (self.items & (IT_LASER | IT_SHOTGUN | IT_UZI | IT_GRENADE_LAUNCHER | IT_ELECTRO | IT_CRYLINK | IT_NEX | IT_HAGAR | IT_ROCKET_LAUNCHER));
474                 if(cvar("g_nixnex_with_laser"))
475                         self.items = self.items + IT_LASER;
476                 self.items = self.items - (self.items & weapon_translateindextoflag(nixnex_weapon)) + weapon_translateindextoflag(nixnex_weapon);
477
478                 if(self.switchweapon != nixnex_weapon)
479                         if(!client_hasweapon(self, self.switchweapon, TRUE, FALSE))
480                                 if(client_hasweapon(self, nixnex_weapon, TRUE, FALSE))
481                                         W_SwitchWeapon(nixnex_weapon);
482         }
483 }
484