]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_weapons.qc
new weapon arena menu controls, enjoy! Original code by MirceaKitsune
[divverent/nexuiz.git] / data / qcsrc / server / cl_weapons.qc
1 // switch between weapons
2 void W_SwitchWeapon(float imp)
3 {
4         if (self.switchweapon != imp)
5         if (client_hasweapon(self, imp, TRUE, TRUE))
6                 W_SwitchWeapon_Force(self, imp);
7 };
8
9 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain)
10 {
11         float n, i, weaponwant, first_valid, prev_valid, switchtonext, switchtolast;
12         n = tokenize_sane(weaponorder);
13         switchtonext = switchtolast = 0;
14         first_valid = prev_valid = 0;
15
16         if(dir == 0)
17                 switchtonext = 1;
18
19         for(i = 0; i < n; ++i)
20         {
21                 weaponwant = stof(argv(i));
22
23                 if(imp >= 0)
24                         if((get_weaponinfo(weaponwant)).impulse != imp)
25                                 continue;
26
27                 if(client_hasweapon(pl, weaponwant, TRUE, FALSE))
28                 {
29                         if(switchtonext)
30                                 return weaponwant;
31                         if(!first_valid)
32                                 first_valid = weaponwant;
33                         if(weaponwant == pl.switchweapon)
34                         {
35                                 if(dir >= 0)
36                                         switchtonext = 1;
37                                 else if(prev_valid)
38                                         return prev_valid;
39                                 else
40                                         switchtolast = 1;
41                         }
42                         prev_valid = weaponwant;
43                 }
44         }
45         if(first_valid)
46         {
47                 if(switchtolast)
48                         return prev_valid;
49                 else
50                         return first_valid;
51         }
52         // complain
53         if(complain)
54         {
55                 for(i = 0; i < n; ++i)
56                 {
57                         weaponwant = stof(argv(i));
58                         if(imp >= 0)
59                                 if((get_weaponinfo(weaponwant)).impulse != imp)
60                                         continue;
61                         client_hasweapon(pl, weaponwant, TRUE, TRUE);
62                 }
63         }
64         return 0;
65 }
66
67 void W_CycleWeapon(string weaponorder, float dir)
68 {
69         float w;
70         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1);
71         if(w > 0)
72                 W_SwitchWeapon(w);
73 }
74
75 void W_NextWeaponOnImpulse(float imp)
76 {
77         float w;
78         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1);
79         if(w > 0)
80                 W_SwitchWeapon(w);
81 }
82
83 // next weapon
84 void W_NextWeapon(float list)
85 {
86         if(list == 0)
87                 W_CycleWeapon(weaponpriority_hudselector_0, -1);
88         else if(list == 1)
89                 W_CycleWeapon(weaponpriority_hudselector_1, -1);
90         else if(list == 2)
91                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
92 }
93
94 // prev weapon
95 void W_PreviousWeapon(float list)
96 {
97         if(list == 0)
98                 W_CycleWeapon(weaponpriority_hudselector_0, +1);
99         else if(list == 1)
100                 W_CycleWeapon(weaponpriority_hudselector_1, +1);
101         else if(list == 2)
102                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
103 }
104
105 string W_FixWeaponOrder_AllowIncomplete(string order)
106 {
107         return W_FixWeaponOrder(order, 0);
108 }
109
110 string W_FixWeaponOrder_ForceComplete(string order)
111 {
112         if(order == "")
113                 order = cvar_string("cl_weaponpriority");
114         return W_FixWeaponOrder(order, 1);
115 }
116
117 float w_getbestweapon(entity e)
118 {
119         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, 0);
120 };
121
122 // generic weapons table
123 // TODO should they be macros instead?
124 float weapon_action(float wpn, float wrequest)
125 {
126         return (get_weaponinfo(wpn)).weapon_func(wrequest);
127 };
128
129 string W_Name(float weaponid)
130 {
131         return (get_weaponinfo(weaponid)).message;
132 }
133
134 float W_WeaponBit(float wpn)
135 {
136         return (get_weaponinfo(wpn)).weapons;
137 }
138
139 float W_AmmoItemCode(float wpn)
140 {
141         return (get_weaponinfo(wpn)).items;
142 }
143
144 void thrown_wep_think()
145 {
146         self.solid = SOLID_TRIGGER;
147         self.owner = world;
148         SUB_SetFade(self, time + 20, 1);
149 }
150
151 // returns amount of ammo used, or -1 for failure, or 0 for no ammo count
152 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
153 {
154         entity oldself, wep;
155         float wa, thisammo;
156         var .float ammofield;
157
158         wep = spawn();
159
160         setorigin(wep, org);
161         wep.classname = "droppedweapon";
162         wep.velocity = velo;
163         wep.owner = wep.enemy = own;
164         wep.classname = "droppedweapon";
165         wep.flags = wep.flags | FL_TOSSED;
166         wep.colormap = own.colormap;
167
168         wa = W_AmmoItemCode(wpn);
169         if(wa == IT_SUPERWEAPON || wa == 0)
170         {
171                 oldself = self;
172                 self = wep;
173                 weapon_defaultspawnfunc(wpn);
174                 self = oldself;
175                 if(startitem_failed)
176                         return -1;
177                 wep.think = thrown_wep_think;
178                 wep.nextthink = time + 0.5;
179                 return 0;
180         }
181         else
182         {
183                 ammofield = Item_CounterField(wa);
184                 oldself = self;
185                 self = wep;
186                 weapon_defaultspawnfunc(wpn);
187                 self = oldself;
188                 if(startitem_failed)
189                         return -1;
190                 if(doreduce)
191                 {
192                         thisammo = min(own.ammofield, wep.ammofield);
193                         wep.ammofield = thisammo;
194                         own.ammofield -= thisammo;
195                 }
196                 wep.think = thrown_wep_think;
197                 wep.nextthink = time + 0.5;
198                 return wep.ammofield;
199         }
200 }
201
202 // toss current weapon
203 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
204 {
205         local float w, a, wb, wa;
206
207         w = self.weapon;
208         if (w == 0)
209                 return; // just in case
210         if (w == WEP_LASER)
211                 return; // just in case
212         if (g_weaponarena)
213                 return;
214         if (g_lms)
215                 return;
216         if (g_nixnex)
217                 return;
218         if (!cvar("g_pickup_items"))
219                 return;
220
221         wb = W_WeaponBit(w);
222         wa = W_AmmoItemCode(w);
223         if(!wb)
224                 return;
225         if(self.weapons & wb != wb)
226                 return;
227         if(start_weapons & wb)
228         {
229                 if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS)
230                         return;
231                 if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO)
232                         return;
233                 if(wa == 0)
234                         return;
235         }
236
237         self.weapons = self.weapons - wb;
238         W_SwitchWeapon_Force(self, w_getbestweapon(self));
239         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
240         if(a < 0)
241                 return;
242         if(self.health >= 1)
243         {
244                 if(a == 0)
245                         sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
246                 else
247                         sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", ftos(a), " ", Item_CounterFieldName(W_AmmoItemCode(w)), "\n"));
248         }
249 };
250
251 // Bringed back weapon frame
252 void W_WeaponFrame()
253 {
254         if((arena_roundbased && time < warmup) || ((time < game_starttime) && !cvar("sv_ready_restart_after_countdown")))
255                 return;
256
257         if (!self.weaponentity || self.health < 1)
258                 return; // Dead player can't use weapons and injure impulse commands
259
260         if(!self.switchweapon)
261         {
262                 self.weapon = 0;
263                 self.weaponentity.state = WS_CLEAR;
264                 return;
265         }
266
267         makevectors(self.v_angle);
268
269         // Change weapon
270         if (self.weapon != self.switchweapon)
271         {
272                 if (self.weaponentity.state == WS_CLEAR)
273                 {
274                         player_setanim(self.anim_draw, FALSE, TRUE, TRUE);
275                         self.weaponentity.state = WS_RAISE;
276                         weapon_action(self.switchweapon, WR_SETUP);
277                         // VorteX: add player model weapon select frame here
278                         // setcustomframe(PlayerWeaponRaise);
279                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_ready);
280                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
281                 }
282                 else if (self.weaponentity.state == WS_READY)
283                 {
284 #ifndef INDEPENDENT_ATTACK_FINISHED
285                         if(ATTACK_FINISHED(self) <= time + frametime * 0.5)
286                         {
287 #endif
288                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
289                         sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
290                         self.weaponentity.state = WS_DROP;
291                         // set up weapon switch think in the future, and start drop anim
292                         weapon_thinkf(WFRAME_IDLE, cvar("g_balance_weaponswitchdelay"), w_clear);
293                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
294 #ifndef INDEPENDENT_ATTACK_FINISHED
295                         }
296 #endif
297                 }
298         }
299
300         float wb;
301         wb = W_WeaponBit(self.weapon);
302
303         // call the think code which may fire the weapon
304         // and do so multiple times to resolve framerate dependency issues if the
305         // server framerate is very low and the weapon fire rate very high
306         local float c;
307         c = 0;
308         while (c < 5)
309         {
310                 c = c + 1;
311                 if(wb && self.weapons & wb == 0)
312                 {
313                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
314                         wb = 0;
315                 }
316                 if(wb)
317                         weapon_action(self.weapon, WR_THINK);
318                 if (time + frametime * 0.5 >= self.weapon_nextthink)
319                         self.weapon_think();
320         }
321
322         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
323         //if (ATTACK_FINISHED(self) < time)
324         //      ATTACK_FINISHED(self) = time;
325
326         //if (self.weapon_nextthink < time)
327         //      self.weapon_nextthink = time;
328
329         // update currentammo incase it has changed
330         if (self.items & IT_CELLS)
331                 self.currentammo = self.ammo_cells;
332         else if (self.items & IT_ROCKETS)
333                 self.currentammo = self.ammo_rockets;
334         else if (self.items & IT_NAILS)
335                 self.currentammo = self.ammo_nails;
336         else if (self.items & IT_SHELLS)
337                 self.currentammo = self.ammo_shells;
338         else
339                 self.currentammo = 1;
340
341 };
342
343 float nixnex_weapon;
344 float nixnex_nextchange;
345 float nixnex_nextweapon;
346 .float nixnex_lastchange_id;
347 .float nixnex_lastinfotime;
348 .float nixnex_nextincr;
349
350 void Nixnex_ChooseNextWeapon()
351 {
352         float numberof, id;
353         numberof = WEP_LAST - WEP_FIRST; // all but the current one
354         if(g_nixnex_with_laser)
355                 numberof = numberof - 1;
356         id = WEP_FIRST + floor(random() * numberof);
357
358         if(g_nixnex_with_laser) // skip the laser if needed
359                 id = id + 1;
360
361         if(id >= nixnex_weapon) // skip the current weapon
362                 id = id + 1;
363
364         if(id < WEP_FIRST) // can't happen, but to be sure...
365         {
366                 dprint("Won't happen (id < WEP_FIRST)\n");
367                 id = WEP_FIRST;
368         }
369         if(id > WEP_LAST) // either
370         {
371                 dprint("Won't happen (id > WEP_LAST)\n");
372                 id = WEP_LAST;
373         }
374
375         nixnex_nextweapon = id;
376 }
377
378 void Nixnex_GiveCurrentWeapon()
379 {
380         float dt;
381         if(g_nixnex)
382         {
383                 if(!nixnex_nextweapon)
384                         Nixnex_ChooseNextWeapon();
385
386                 dt = ceil(nixnex_nextchange - time);
387
388                 if(dt <= 0)
389                 {
390                         nixnex_weapon = nixnex_nextweapon;
391                         nixnex_nextweapon = 0;
392                         nixnex_nextchange = time + cvar("g_balance_nixnex_roundtime");
393                         //weapon_action(nixnex_weapon, WR_PRECACHE); // forget it, too slow
394                 }
395
396                 if(nixnex_nextchange != self.nixnex_lastchange_id) // this shall only be called once per round!
397                 {
398                         self.nixnex_lastchange_id = nixnex_nextchange;
399                         if (self.items & IT_UNLIMITED_WEAPON_AMMO)
400                         {
401                                 self.ammo_shells = cvar("g_pickup_shells_max");
402                                 self.ammo_nails = cvar("g_pickup_nails_max");
403                                 self.ammo_rockets = cvar("g_pickup_rockets_max");
404                                 self.ammo_cells = cvar("g_pickup_cells_max");
405                         }
406                         else
407                         {
408                                 self.ammo_shells = cvar("g_balance_nixnex_ammo_shells");
409                                 self.ammo_nails = cvar("g_balance_nixnex_ammo_nails");
410                                 self.ammo_rockets = cvar("g_balance_nixnex_ammo_rockets");
411                                 self.ammo_cells = cvar("g_balance_nixnex_ammo_cells");
412                         }
413                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
414                         if(dt >= 1 && dt <= 5)
415                                 self.nixnex_lastinfotime = -42;
416                         else
417                                 centerprint(self, strcat("\n\n^2Active weapon: ^3", W_Name(nixnex_weapon), "\n"));
418                 }
419                 if(self.nixnex_lastinfotime != dt)
420                 {
421                         self.nixnex_lastinfotime = dt; // initial value 0 should count as "not seen"
422                         if(dt >= 1 && dt <= 5)
423                                 centerprint(self, strcat("^3", ftos(dt), "^2 seconds until weapon change...\n\nNext weapon: ^3", W_Name(nixnex_nextweapon), "\n"));
424                 }
425
426                 if(!(self.items & IT_UNLIMITED_WEAPON_AMMO) && time > self.nixnex_nextincr)
427                 {
428                         self.ammo_shells = self.ammo_shells + cvar("g_balance_nixnex_ammoincr_shells");
429                         self.ammo_nails = self.ammo_nails + cvar("g_balance_nixnex_ammoincr_nails");
430                         self.ammo_rockets = self.ammo_rockets + cvar("g_balance_nixnex_ammoincr_rockets");
431                         self.ammo_cells = self.ammo_cells + cvar("g_balance_nixnex_ammoincr_cells");
432                         self.nixnex_nextincr = time + cvar("g_balance_nixnex_incrtime");
433                 }
434
435                 self.weapons = 0;
436                 if(g_nixnex_with_laser)
437                         self.weapons = self.weapons | WEPBIT_LASER;
438                 self.weapons = self.weapons | W_WeaponBit(nixnex_weapon);
439
440                 if(self.switchweapon != nixnex_weapon)
441                         if(!client_hasweapon(self, self.switchweapon, TRUE, FALSE))
442                                 if(client_hasweapon(self, nixnex_weapon, TRUE, FALSE))
443                                         W_SwitchWeapon(nixnex_weapon);
444         }
445 }