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