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