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