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