]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weapons.qc
weapon priority lists; non-critical impulses reordered (cleaned up)
[divverent/nexuiz.git] / data / qcsrc / server / cl_weapons.qc
1 // switch between weapons
2 void W_SwitchWeapon(float imp)
3 {
4         if (self.weapon != imp)
5         if (client_hasweapon(self, imp, TRUE, TRUE))
6         {
7                 self.cnt = self.weapon ? self.weapon : self.switchweapon;
8                 self.switchweapon = imp;
9         }
10 };
11
12 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float complain)
13 {
14         float n, i, weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
15         n = tokenize(weaponorder);
16         switchtonext = switchtolast = 0;
17         first_valid = prev_valid = 0;
18
19         if(dir == 0)
20                 switchtonext = 1;
21
22         for(i = 0; i < n; ++i)
23         {
24                 weaponwant = stof(argv(i));
25                 if(client_hasweapon(pl, weaponwant, TRUE, FALSE))
26                 {
27                         if(switchtonext)
28                                 return weaponwant;
29                         if(!first_valid)
30                                 first_valid = weaponwant;
31                         prev_valid = weaponwant;
32                         if(weaponwant == pl.switchweapon)
33                         {
34                                 if(dir >= 0)
35                                         switchtonext = 1;
36                                 else if(prev_valid)
37                                         return prev_valid;
38                                 else
39                                 {
40                                         switchtolast = 1;
41                                         break;
42                                 }
43                         }
44                 }
45         }
46         if(first_valid)
47         {
48                 if(switchtolast)
49                         return prev_valid;
50                 else
51                         return first_valid;
52         }
53         // complain
54         if(complain)
55         {
56                 for(i = 0; i < n; ++i)
57                 {
58                         weaponwant = stof(argv(i));
59                         client_hasweapon(pl, weaponwant, TRUE, TRUE);
60                 }
61         }
62         return 0;
63 }
64
65 void W_CycleWeapon(string weaponorder, float dir)
66 {
67         float w;
68         w = W_GetCycleWeapon(self, weaponorder, dir, 1);
69         if(w > 0)
70                 W_SwitchWeapon(w);
71 }
72
73 // next weapon
74 void W_NextWeapon()
75 {
76         W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
77 }
78
79 // prev weapon
80 void W_PreviousWeapon()
81 {
82         W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
83 }
84
85 string W_FixWeaponOrder(string order, float complete)
86 {
87         string neworder;
88         float i, n, w;
89
90         n = tokenize(order);
91         for(i = 0; i < n; ++i)
92         {
93                 w = stof(argv(i));
94                 if(w >= WEP_FIRST && w <= WEP_LAST && w == floor(w))
95                         neworder = strcat(neworder, ftos(w), " ");
96         }
97
98         if(complete)
99         {
100                 n = tokenize(neworder);
101                 for(w = WEP_FIRST; w <= WEP_LAST; ++w)
102                 {
103                         for(i = 0; i < n; ++i)
104                                 if(stof(argv(i)) == w)
105                                         break;
106                         if(i == n) // not found
107                                 neworder = strcat(neworder, ftos(w), " ");
108                 }
109         }
110         
111         return substring(neworder, 0, strlen(neworder) - 1);
112 }
113
114 string W_FixWeaponOrder_AllowIncomplete(string order)
115 {
116         return W_FixWeaponOrder(order, 0);
117 }
118
119 string W_FixWeaponOrder_ForceComplete(string order)
120 {
121         return W_FixWeaponOrder(order, 1);
122 }
123
124 float w_getbestweapon(entity e)
125
126         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, 0);
127 };
128
129 // generic weapons table
130 // add new weapons here
131 float weapon_action(float wpn, float wrequest)
132 {
133         if (wpn == WEP_LASER)
134                 return w_laser(wrequest);
135         else if (wpn == WEP_SHOTGUN)
136                 return w_shotgun(wrequest);
137         else if (wpn == WEP_UZI)
138                 return w_uzi(wrequest);
139         else if (wpn == WEP_GRENADE_LAUNCHER)
140                 return w_glauncher(wrequest);
141         else if (wpn == WEP_ELECTRO)
142                 return w_electro(wrequest);
143         else if (wpn == WEP_CRYLINK)
144                 return w_crylink(wrequest);
145         else if (wpn == WEP_NEX)
146                 return w_nex(wrequest);
147         else if (wpn == WEP_HAGAR)
148                 return w_hagar(wrequest);
149         else if (wpn == WEP_ROCKET_LAUNCHER)
150                 return w_rlauncher(wrequest);
151         return FALSE;
152 };
153
154 string W_Name(float weaponid)
155 {
156         if(weaponid == WEP_LASER)             return "Laser";
157         if(weaponid == WEP_UZI)               return "Machine Gun";
158         if(weaponid == WEP_SHOTGUN)           return "Shotgun";
159         if(weaponid == WEP_GRENADE_LAUNCHER)  return "Mortar";
160         if(weaponid == WEP_ELECTRO)           return "Electro";
161         if(weaponid == WEP_NEX)               return "Nex";
162         if(weaponid == WEP_HAGAR)             return "Hagar";
163         if(weaponid == WEP_ROCKET_LAUNCHER)   return "Rocket Launcher";
164         if(weaponid == WEP_CRYLINK)           return "Crylink";
165         return "@!#%'n Tuba";
166 }
167
168 float W_ItemCode(float wpn)
169 {
170         switch(wpn)
171         {
172                 case WEP_LASER:            return IT_LASER;
173                 case WEP_SHOTGUN:          return IT_SHOTGUN;
174                 case WEP_UZI:              return IT_UZI;
175                 case WEP_GRENADE_LAUNCHER: return IT_GRENADE_LAUNCHER;
176                 case WEP_ELECTRO:          return IT_ELECTRO;
177                 case WEP_CRYLINK:          return IT_CRYLINK;
178                 case WEP_NEX:              return IT_NEX;
179                 case WEP_HAGAR:            return IT_HAGAR;
180                 case WEP_ROCKET_LAUNCHER:  return IT_ROCKET_LAUNCHER;
181                 default:                   return 0;
182         }
183 }
184
185 float W_AmmoItemCode(float wpn)
186 {
187         switch(wpn)
188         {
189                 case WEP_SHOTGUN:          return IT_SHELLS;
190                 case WEP_UZI:              return IT_NAILS;
191                 case WEP_GRENADE_LAUNCHER: return IT_ROCKETS;
192                 case WEP_ELECTRO:          return IT_CELLS;
193                 case WEP_CRYLINK:          return IT_CELLS;
194                 case WEP_NEX:              return IT_CELLS;
195                 case WEP_HAGAR:            return IT_ROCKETS;
196                 case WEP_ROCKET_LAUNCHER:  return IT_ROCKETS;
197                 default:                   return 0;
198         }
199 }
200
201 // think function for tossed weapons
202 void thrown_wep_think()
203 {
204         self.solid = SOLID_TRIGGER;
205         self.owner = world;
206         SUB_SetFade(self, time + 20, 1);
207         setorigin(self, self.origin);
208 };
209
210 // toss current weapon
211 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
212 {
213         local float w, ammo;
214         local entity wep, e;
215         local .float ammofield;
216
217         w = self.weapon;
218         if (w == 0)
219                 return; // just in case
220         if (w == WEP_LASER)
221                 return;
222         if (g_instagib)
223                 return;
224         if (g_rocketarena)
225                 return;
226         if (g_lms)
227                 return;
228         if (g_nixnex)
229                 return;
230         if (!cvar("g_pickup_items"))
231                 return;
232
233         e = self;
234         wep = spawn();
235         self = wep;
236
237         setorigin(wep, e.origin + delta);
238         makevectors(e.angles);
239         wep.classname = "droppedweapon";
240         wep.velocity = velo; // e.velocity * 0.5 + v_forward * 750;
241         SUB_SetFade(wep, time + 20, 1);
242
243         ammofield = Item_CounterField(W_AmmoItemCode(w));
244         w = W_ItemCode(w);
245         if(!(e.items & w))
246         {
247                 remove(wep);
248                 goto leave;
249         }
250         Item_SpawnByItemCode(w);
251         if(startitem_failed)
252                 goto leave;
253         if(doreduce)
254         {
255                 ammo = min(e.ammofield, wep.ammofield);
256                 wep.ammofield = ammo;
257                 e.ammofield -= ammo;
258         }
259
260         if(e.items & w)
261                 if(e.health >= 1)
262                         sprint(e, strcat("You dropped the ^2", wep.netname, " with ", ftos(wep.ammofield), " ammo", "\n"));
263
264         wep.owner = e;
265         setorigin(wep, wep.origin);
266         wep.nextthink = time + 0.5;
267         wep.think = thrown_wep_think;
268         wep.classname = "droppedweapon";
269         wep.flags = wep.flags | FL_TOSSED;
270         e.items = e.items - (e.items & w);
271         e.switchweapon = w_getbestweapon(e);
272         wep.colormap = e.colormap;
273         if (e.switchweapon != e.weapon)
274                 e.cnt = e.weapon;
275
276 :leave
277         self = e;
278 };
279
280 // Bringed back weapon frame
281 void W_WeaponFrame()
282 {
283         if((arena_roundbased && time < warmup) || ((time < restart_countdown) && !cvar("sv_ready_restart_after_countdown")))
284                 return;
285
286         if (!self.weaponentity || self.health < 1)
287                 return; // Dead player can't use weapons and injure impulse commands
288
289         if(!self.switchweapon)
290         {
291                 self.weapon = 0;
292                 self.weaponentity.state = WS_CLEAR;
293                 return;
294         }
295
296         makevectors(self.v_angle);
297
298         // Change weapon
299         if (self.weapon != self.switchweapon)
300         {
301                 if (self.weaponentity.state == WS_CLEAR)
302                 {
303                         player_setanim(self.anim_draw, FALSE, TRUE, TRUE);
304                         self.weaponentity.state = WS_RAISE;
305                         weapon_action(self.switchweapon, WR_SETUP);
306                         // VorteX: add player model weapon select frame here
307                         // setcustomframe(PlayerWeaponRaise);
308                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready);
309                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
310                 }
311                 else if (self.weaponentity.state == WS_READY)
312                 {
313 #ifndef INDEPENDENT_ATTACK_FINISHED
314                         if(ATTACK_FINISHED(self) <= time + frametime * 0.5)
315                         {
316 #endif
317                         sound (self, CHAN_WEAPON, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
318                         self.weaponentity.state = WS_DROP;
319                         // set up weapon switch think in the future, and start drop anim
320                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_clear);
321                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
322 #ifndef INDEPENDENT_ATTACK_FINISHED
323                         }
324 #endif
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 (time + frametime * 0.5 >= self.weapon_nextthink)
338                         self.weapon_think();
339         }
340
341         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
342         //if (ATTACK_FINISHED(self) < time)
343         //      ATTACK_FINISHED(self) = time;
344
345         //if (self.weapon_nextthink < time)
346         //      self.weapon_nextthink = 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(g_nixnex_with_laser)
374                 numberof = numberof - 1;
375         id = WEP_FIRST + ceil(random() * numberof) - 1;
376
377         if(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 void Nixnex_GiveCurrentWeapon()
398 {
399         float dt;
400         if(g_nixnex)
401         {
402                 if(!nixnex_nextweapon)
403                         Nixnex_ChooseNextWeapon();
404
405                 dt = ceil(nixnex_nextchange - time);
406
407                 if(dt <= 0)
408                 {
409                         nixnex_weapon = nixnex_nextweapon;
410                         nixnex_nextweapon = 0;
411                         nixnex_nextchange = time + cvar("g_balance_nixnex_roundtime");
412                         //weapon_action(nixnex_weapon, WR_PRECACHE); // forget it, too slow
413                 }
414
415                 if(nixnex_nextchange != self.nixnex_lastchange_id) // this shall only be called once per round!
416                 {
417                         self.nixnex_lastchange_id = nixnex_nextchange;
418                         if (cvar("g_use_ammunition"))
419                         {
420                                 self.ammo_shells = cvar("g_balance_nixnex_ammo_shells");
421                                 self.ammo_nails = cvar("g_balance_nixnex_ammo_nails");
422                                 self.ammo_rockets = cvar("g_balance_nixnex_ammo_rockets");
423                                 self.ammo_cells = cvar("g_balance_nixnex_ammo_cells");
424                         }
425                         else
426                         {
427                                 self.ammo_shells = cvar("g_pickup_shells_max");
428                                 self.ammo_nails = cvar("g_pickup_nails_max");
429                                 self.ammo_rockets = cvar("g_pickup_rockets_max");
430                                 self.ammo_cells = cvar("g_pickup_cells_max");
431                         }
432                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
433                         if(dt >= 1 && dt <= 5)
434                                 self.nixnex_lastinfotime = -42;
435                         else
436                                 centerprint(self, strcat("\n\n^2Active weapon: ^3", W_Name(nixnex_weapon), "\n"));
437                 }
438                 if(self.nixnex_lastinfotime != dt)
439                 {
440                         self.nixnex_lastinfotime = dt; // initial value 0 should count as "not seen"
441                         if(dt >= 1 && dt <= 5)
442                                 centerprint(self, strcat("^3", ftos(dt), "^2 seconds until weapon change...\n\nNext weapon: ^3", W_Name(nixnex_nextweapon), "\n"));
443                 }
444
445                 if(cvar("g_use_ammunition") && time > self.nixnex_nextincr)
446                 {
447                         self.ammo_shells = self.ammo_shells + cvar("g_balance_nixnex_ammoincr_shells");
448                         self.ammo_nails = self.ammo_nails + cvar("g_balance_nixnex_ammoincr_nails");
449                         self.ammo_rockets = self.ammo_rockets + cvar("g_balance_nixnex_ammoincr_rockets");
450                         self.ammo_cells = self.ammo_cells + cvar("g_balance_nixnex_ammoincr_cells");
451                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
452                 }
453
454                 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));
455                 if(g_nixnex_with_laser)
456                         self.items = self.items | IT_LASER;
457                 self.items = self.items | W_ItemCode(nixnex_weapon);
458
459                 if(self.switchweapon != nixnex_weapon)
460                         if(!client_hasweapon(self, self.switchweapon, TRUE, FALSE))
461                                 if(client_hasweapon(self, nixnex_weapon, TRUE, FALSE))
462                                         W_SwitchWeapon(nixnex_weapon);
463         }
464 }
465