]> icculus.org git repositories - divverent/nexuiz.git/blob - qcsrc/server/gamec/cl_weapons.c
new gamemode: arena
[divverent/nexuiz.git] / qcsrc / server / gamec / cl_weapons.c
1 // generic weapons table
2 // add new weapons here
3 void(float wpn, float wrequest) weapon_action =
4 {
5         if (wpn == WEP_LASER)
6                 w_laser(wrequest);
7         else if (wpn == WEP_SHOTGUN)
8                 w_shotgun(wrequest);
9         else if (wpn == WEP_UZI)
10                 w_uzi(wrequest);
11         else if (wpn == WEP_GRENADE_LAUNCHER)
12                 w_glauncher(wrequest);
13         else if (wpn == WEP_ELECTRO)
14                 w_electro(wrequest);
15         else if (wpn == WEP_CRYLINK)
16                 w_crylink(wrequest);
17         else if (wpn == WEP_NEX)
18                 w_nex(wrequest);
19         else if (wpn == WEP_HAGAR)
20                 w_hagar(wrequest);
21         else if (wpn == WEP_ROCKET_LAUNCHER)
22                 w_rlauncher(wrequest);
23 };
24
25 // think function for tossed weapons
26 void() thrown_wep_think
27 {
28         self.solid = SOLID_TRIGGER;
29         self.owner = world;
30         SUB_SetFade(self, time + 20, 1);
31         setorigin(self, self.origin);
32 };
33
34 // toss current weapon
35 void() W_ThrowWeapon
36 {
37         local float w, ammo;
38         local entity wep, e;
39
40         e = self;
41         wep = spawn();
42         self = wep;
43         w = e.weapon;
44         setorigin(wep, e.origin);
45         makevectors(e.angles);
46         wep.classname = "droppedweapon";
47         wep.velocity = e.velocity * 0.5 + v_forward * 750;
48         SUB_SetFade(wep, time + 20, 1);
49
50         if(w == WEP_SHOTGUN)
51         {
52                 w = IT_SHOTGUN;
53                 if(!(e.items & w))
54                 {
55                         remove(wep);
56                         return;
57                 }
58                 weapon_shotgun();
59                 ammo = min(e.ammo_shells, wep.ammo_shells);
60                 wep.ammo_shells = ammo;
61                 e.ammo_shells -= ammo;
62         }
63         else if(w == WEP_UZI)
64         {
65                 w = IT_UZI;
66                 if(!(e.items & w))
67                 {
68                         remove(wep);
69                         return;
70                 }
71                 weapon_uzi();
72                 ammo = min(e.ammo_nails, wep.ammo_nails);
73                 wep.ammo_nails = ammo;
74                 e.ammo_nails -= ammo;
75         }
76         else if(w == WEP_GRENADE_LAUNCHER)
77         {
78                 w = IT_GRENADE_LAUNCHER;
79                 if(!(e.items & w))
80                 {
81                         remove(wep);
82                         return;
83                 }
84                 weapon_grenadelauncher();
85                 ammo = min(e.ammo_rockets, wep.ammo_rockets);
86                 wep.ammo_rockets = ammo;
87                 e.ammo_rockets -= ammo;
88         }
89         else if(w == WEP_ELECTRO)
90         {
91                 w = IT_ELECTRO;
92                 if(!(e.items & w))
93                 {
94                         remove(wep);
95                         return;
96                 }
97                 weapon_electro();
98                 ammo = min(e.ammo_cells, wep.ammo_cells);
99                 wep.ammo_cells = ammo;
100                 e.ammo_cells -= ammo;
101         }
102         else if(w == WEP_CRYLINK)
103         {
104                 w = IT_CRYLINK;
105                 if(!(e.items & w))
106                 {
107                         remove(wep);
108                         return;
109                 }
110                 weapon_crylink();
111                 ammo = min(e.ammo_cells, wep.ammo_cells);
112                 wep.ammo_cells = ammo;
113                 e.ammo_cells -= ammo;
114         }
115         else if(w == WEP_NEX)
116         {
117                 w = IT_NEX;
118                 if(!(e.items & w))
119                 {
120                         remove(wep);
121                         return;
122                 }
123                 weapon_nex();
124                 ammo = min(e.ammo_cells, wep.ammo_cells);
125                 wep.ammo_cells = ammo;
126                 e.ammo_cells -= ammo;
127         }
128         else if(w == WEP_HAGAR)
129         {
130                 w = IT_HAGAR;
131                 if(!(e.items & w))
132                 {
133                         remove(wep);
134                         return;
135                 }
136                 weapon_hagar();
137                 ammo = min(e.ammo_rockets, wep.ammo_rockets);
138                 wep.ammo_rockets = ammo;
139                 e.ammo_rockets -= ammo;
140         }
141         else if(w == WEP_ROCKET_LAUNCHER)
142         {
143                 w = IT_ROCKET_LAUNCHER;
144                 if(!(e.items & w))
145                 {
146                         remove(wep);
147                         return;
148                 }
149                 weapon_rocketlauncher();
150                 ammo = min(e.ammo_rockets, wep.ammo_rockets);
151                 wep.ammo_rockets = ammo;
152                 e.ammo_rockets -= ammo;
153         }
154
155         if(e.items & w)
156                 sprint(e, strcat("You dropped the ^2", wep.netname, "\n"));
157         wep.owner = e;
158         setorigin(wep, wep.origin);
159         wep.nextthink = time + 0.5;
160         wep.think = thrown_wep_think;
161         wep.classname = "droppedweapon";
162         e.items = e.items - (e.items & w);
163         e.switchweapon = w_getbestweapon(e);
164         wep.colormap = e.colormap;
165         if (e.switchweapon != e.weapon)
166                 e.cnt = e.weapon;
167         self = e;
168 };
169
170 // switch between weapons
171 void(float imp) W_SwitchWeapon
172 {
173         weapon_hasammo = TRUE;
174         if (!client_hasweapon(self, imp, TRUE))
175         {
176                 if (!weapon_hasammo)
177                         sprint(self, "You don't have any ammo for that weapon\n");
178                 else
179                         sprint(self, "You don't own that weapon\n");
180         }
181         else
182         {
183                 self.cnt = self.weapon;
184                 self.switchweapon = imp;
185         }
186 };
187
188 // next weapon
189 void() W_NextWeapon =
190 {
191         local float weaponwant, maxtries;
192
193         maxtries = WEP_LAST;
194
195         weaponwant = self.switchweapon + 1;
196         if (weaponwant < WEP_FIRST)
197                 weaponwant = WEP_LAST;
198         if (weaponwant > WEP_LAST)
199                 weaponwant = WEP_FIRST;
200         weapon_hasammo = TRUE;
201         while(!client_hasweapon(self, weaponwant, TRUE))
202         {
203                 if(!maxtries)
204                         return;
205                 
206                 maxtries -= 1;
207                 weaponwant = weaponwant + 1;
208                 if (weaponwant < WEP_FIRST)
209                         weaponwant = WEP_LAST;
210                 if (weaponwant > WEP_LAST)
211                         weaponwant = WEP_FIRST;
212         }
213         self.cnt = self.weapon;
214         self.switchweapon = weaponwant;
215 };
216
217 // prev weapon
218 void() W_PreviousWeapon =
219 {
220         local float weaponwant, maxtries;
221
222         maxtries = WEP_LAST;
223         
224         weaponwant = self.switchweapon - 1;
225         if (weaponwant < WEP_FIRST)
226                 weaponwant = WEP_LAST;
227         if (weaponwant > WEP_LAST)
228                 weaponwant = WEP_FIRST;
229         weapon_hasammo = TRUE;
230         while(!client_hasweapon(self, weaponwant, TRUE))
231         {
232                 if(!maxtries)
233                         return;
234                         
235                 maxtries -= 1;
236                 weaponwant = weaponwant - 1;
237                 if (weaponwant < WEP_FIRST)
238                         weaponwant = WEP_LAST;
239                 if (weaponwant > WEP_LAST)
240                         weaponwant = WEP_FIRST;
241         }
242         self.cnt = self.weapon;
243         self.switchweapon = weaponwant;
244 };
245
246 // Bringed back weapon frame
247 void() W_WeaponFrame =
248 {
249         if(arena_roundbased)
250         if(time < self.arena_warmup_end)
251                 return;
252         
253         if (!self.weaponentity || self.health < 1)
254                 return; // Dead player can't use weapons and injure impulse commands
255
256         if(!self.switchweapon)
257         {
258                 self.weapon = 0;
259                 self.weaponentity.state = 0;
260                 return;
261         }
262                 
263         makevectors(self.v_angle);
264
265         // Change weapon
266         if (self.weapon != self.switchweapon)
267         {
268                 if (self.weaponentity.state == WS_CLEAR)
269                 {
270                         self.weaponentity.state = WS_RAISE;
271                         weapon_action(self.switchweapon, WR_SETUP);
272                         // VorteX: add player model weapon select frame here
273                         // setcustomframe(PlayerWeaponRaise);
274                         weapon_action(self.weapon, WR_UPDATECOUNTS);
275                         weapon_action(self.weapon, WR_RAISE);
276                 }
277                 else if (self.weaponentity.state == WS_READY)
278                 {
279                         sound (self, CHAN_WEAPON, "weapons/weapon_switch.ogg", 1, ATTN_NORM);
280                         self.weaponentity.state = WS_DROP;
281                         // VorteX: add player model weapon deselect frame here
282                         // setcustomframe(PlayerWeaponDrop);
283                         weapon_action(self.weapon, WR_DROP);
284                 }
285         }
286
287         if (self.button0)
288                 weapon_action(self.weapon, WR_FIRE1);
289         if (self.button3)
290                 weapon_action(self.weapon, WR_FIRE2);
291
292         // do weapon think
293         if (time >= self.weapon_nextthink)
294                 if (self.weapon_nextthink > 0)
295                         self.weapon_think();
296
297         // weapon bobbing and script actions
298         local float bobintensity, q1pitching, framespeed, diff;
299         local vector vel, realorg, layer1, boblayer;
300
301         bobintensity = cvar("g_viewweapon_bobintensity"); // weapon bob intensity
302         q1pitching = fabs(cvar("g_viewweapon_q1pitching")); // q1 style of "bob" when looking up and down
303
304         realorg = self.weaponentity.origin + self.weaponentity.view_ofs;
305         realorg = realorg - self.weaponentity.finaldest; // finaldest is last bob position
306
307         // VorteX: actually this is needed for weapon screen offset
308         if (q1pitching)
309         {
310                 self.weaponentity.view_ofs_x = q1pitching*bound(-5.5, self.v_angle_x/45, 5.5);
311                 self.weaponentity.view_ofs_z = q1pitching*bound(-1.5, self.v_angle_x/60, 1.5);
312         }
313
314         // weapon origin interpolation, layer 1
315         if (realorg != self.weaponentity.pos1)
316         {
317                 framespeed = frametime*self.weaponentity.lip*10; // lip is speed of origin changing (of layer1)
318                 diff = vlen(realorg - self.weaponentity.pos1);
319                 // VorteX: add speed modifier (haste)?
320                 layer1 = frametime*10*self.weaponentity.lip*normalize(self.weaponentity.pos1 - realorg);
321                 if (diff <= vlen(layer1))
322                         layer1 = normalize(self.weaponentity.pos1 - realorg)*diff;
323         }
324
325         // weapon bobbing (q3-style)
326         if (self.flags & FL_ONGROUND && self.waterlevel < 2)
327         {
328                 // VorteX: only xy velocity matters
329                 vel_x = self.velocity_x;
330                 vel_y = self.velocity_y;
331                 framespeed = vlen(vel);
332                 // Y axis
333                 diff = bobintensity*framespeed/300;
334                 self.weaponentity.destvec_y = self.weaponentity.destvec_y + frametime*10;
335                 boblayer_y = diff*cos(self.weaponentity.destvec_y + 90);
336                 // Z axis
337                 diff = bobintensity*framespeed/540;
338                 self.weaponentity.destvec_z = self.weaponentity.destvec_z + frametime*20;
339                 boblayer_z = diff*cos(self.weaponentity.destvec_z);
340                 self.weaponentity.finaldest = boblayer;
341         }
342         else if (self.waterlevel > 0)
343         {// swim, all velocity matters
344                 // X axis
345                 framespeed = vlen(self.velocity);
346                 diff = bobintensity*framespeed/100;
347                 self.weaponentity.destvec_x = self.weaponentity.destvec_x + frametime*6;
348                 boblayer_x = diff*cos(self.weaponentity.destvec_x);
349                 self.weaponentity.finaldest = boblayer;
350         }
351         else
352                 self.weaponentity.finaldest = '0 0 0';
353         self.weaponentity.origin = realorg + boblayer + layer1 - self.weaponentity.view_ofs;
354 };
355
356 float nixnex_weapon;
357 float nixnex_nextchange;
358 float nixnex_nextweapon;
359 .float nixnex_lastchange_id;
360 .float nixnex_lastinfotime;
361 .float nixnex_nextincr;
362
363 void Nixnex_ChooseNextWeapon()
364 {
365         float numberof, id;
366         numberof = WEP_LAST - WEP_FIRST; // all but the current one
367         if(cvar("g_nixnex_with_laser"))
368                 numberof = numberof - 1;
369         id = WEP_FIRST + ceil(random() * numberof) - 1;
370
371         if(cvar("g_nixnex_with_laser")) // skip the laser if needed
372                 id = id + 1;
373         
374         if(id >= nixnex_weapon) // skip the current weapon
375                 id = id + 1;
376         
377         if(id < WEP_FIRST) // can't happen, but to be sure...
378         {
379                 dprint("Won't happen (id < WEP_FIRST)\n");
380                 id = WEP_FIRST;
381         }
382         if(id > WEP_LAST) // either
383         {
384                 dprint("Won't happen (id > WEP_LAST)\n");
385                 id = WEP_LAST;
386         }
387
388         nixnex_nextweapon = id;
389 }
390
391 string W_Name(float weaponid)
392 {
393         if(weaponid == WEP_LASER)             return "Laser";
394         if(weaponid == WEP_UZI)               return "Machine Gun";
395         if(weaponid == WEP_SHOTGUN)           return "Shotgun";
396         if(weaponid == WEP_GRENADE_LAUNCHER)  return "Mortar";
397         if(weaponid == WEP_ELECTRO)           return "Electro";
398         if(weaponid == WEP_NEX)               return "Nex";
399         if(weaponid == WEP_HAGAR)             return "Hagar";
400         if(weaponid == WEP_ROCKET_LAUNCHER)   return "Rocket Launcher";
401         if(weaponid == WEP_CRYLINK)           return "Crylink";
402         return "@!#%'n Tuba";
403 }
404
405 void Nixnex_GiveCurrentWeapon()
406 {
407         float dt;
408         if(cvar("g_nixnex"))
409         {
410                 if(!nixnex_nextweapon)
411                         Nixnex_ChooseNextWeapon();
412
413                 dt = ceil(nixnex_nextchange - time);
414
415                 if(dt <= 0)
416                 {
417                         nixnex_weapon = nixnex_nextweapon;
418                         nixnex_nextweapon = 0;
419                         nixnex_nextchange = time + cvar("g_balance_nixnex_roundtime");
420                 }
421
422                 if(nixnex_nextchange != self.nixnex_lastchange_id) // this shall only be called once per round!
423                 {
424                         self.nixnex_lastchange_id = nixnex_nextchange;
425                         if (cvar("g_use_ammunition"))
426                         {
427                                 self.ammo_shells = cvar("g_balance_nixnex_ammo_shells");
428                                 self.ammo_nails = cvar("g_balance_nixnex_ammo_nails");
429                                 self.ammo_rockets = cvar("g_balance_nixnex_ammo_rockets");
430                                 self.ammo_cells = cvar("g_balance_nixnex_ammo_cells");
431                         }
432                         else
433                         {
434                                 self.ammo_shells = 999;
435                                 self.ammo_nails = 999;
436                                 self.ammo_rockets = 999;
437                                 self.ammo_cells = 999;
438                         }
439                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
440                         if(dt >= 1 && dt <= 5)
441                                 self.nixnex_lastinfotime = -42;
442                         else
443                                 centerprint(self, strcat("\n\n^2Active weapon: ^3", W_Name(nixnex_weapon), "\n"));
444                 }
445                 if(self.nixnex_lastinfotime != dt)
446                 {
447                         self.nixnex_lastinfotime = dt; // initial value 0 should count as "not seen"
448                         if(dt >= 1 && dt <= 5)
449                                 centerprint(self, strcat("^3", ftos(dt), "^2 seconds until weapon change...\n\nNext weapon: ^3", W_Name(nixnex_nextweapon), "\n"));
450                 }
451
452                 if(cvar("g_use_ammunition") && time > self.nixnex_nextincr)
453                 {
454                         self.ammo_shells = self.ammo_shells + cvar("g_balance_nixnex_ammoincr_shells");
455                         self.ammo_nails = self.ammo_nails + cvar("g_balance_nixnex_ammoincr_nails");
456                         self.ammo_rockets = self.ammo_rockets + cvar("g_balance_nixnex_ammoincr_rockets");
457                         self.ammo_cells = self.ammo_cells + cvar("g_balance_nixnex_ammoincr_cells");
458                         weapon_action(self.weapon, WR_UPDATECOUNTS);
459                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
460                 }
461
462                 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));
463                 if(cvar("g_nixnex_with_laser"))
464                         self.items = self.items + IT_LASER;
465                 self.items = self.items - (self.items & weapon_translateindextoflag(nixnex_weapon)) + weapon_translateindextoflag(nixnex_weapon);
466
467                 if(self.switchweapon != nixnex_weapon)
468                         if(!client_hasweapon(self, self.switchweapon, TRUE))
469                                 if(client_hasweapon(self, nixnex_weapon, TRUE))
470                                         W_SwitchWeapon(nixnex_weapon);
471         }
472 }
473