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