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