]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_weapons.qc
use a timer for superweapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_weapons.qc
1 void W_TriggerReload()
2 {
3     weapon_action(self.weapon, WR_RELOAD);
4 }
5
6 // switch between weapons
7 void W_SwitchWeapon(float imp)
8 {
9         if (self.switchweapon != imp)
10         {
11                 if (client_hasweapon(self, imp, TRUE, TRUE))
12                         W_SwitchWeapon_Force(self, imp);
13                 else
14                         self.selectweapon = imp; // update selectweapon ANYWAY
15         }
16         else
17         {
18                 W_TriggerReload();
19         }
20 }
21
22 .float weaponcomplainindex;
23 float W_GetCycleWeapon(entity pl, string weaponorder, float dir, float imp, float complain, float skipmissing)
24 {
25         // We cannot tokenize in this function, as GiveItems calls this
26         // function. Thus we must use car/cdr.
27         float weaponwant, first_valid, prev_valid, switchtonext, switchtolast, c;
28         string rest;
29         switchtonext = switchtolast = 0;
30         first_valid = prev_valid = 0;
31         float weaponcur;
32
33         if(skipmissing || pl.selectweapon == 0)
34                 weaponcur = pl.switchweapon;
35         else
36                 weaponcur = pl.selectweapon;
37
38         if(dir == 0)
39                 switchtonext = 1;
40
41         c = 0;
42
43         rest = weaponorder;
44         while(rest != "")
45         {
46                 weaponwant = stof(car(rest)); rest = cdr(rest);
47                 if(imp >= 0)
48                         if((get_weaponinfo(weaponwant)).impulse != imp)
49                                 continue;
50
51                 ++c;
52
53                 if(!skipmissing || client_hasweapon(pl, weaponwant, TRUE, FALSE))
54                 {
55                         if(switchtonext)
56                                 return weaponwant;
57                         if(!first_valid)
58                                 first_valid = weaponwant;
59                         if(weaponwant == weaponcur)
60                         {
61                                 if(dir >= 0)
62                                         switchtonext = 1;
63                                 else if(prev_valid)
64                                         return prev_valid;
65                                 else
66                                         switchtolast = 1;
67                         }
68                         prev_valid = weaponwant;
69                 }
70         }
71         if(first_valid)
72         {
73                 if(switchtolast)
74                         return prev_valid;
75                 else
76                         return first_valid;
77         }
78         // complain (but only for one weapon on the button that has been pressed)
79         if(complain)
80         {
81                 self.weaponcomplainindex += 1;
82                 c = mod(self.weaponcomplainindex, c) + 1;
83                 rest = weaponorder;
84                 while(rest != "")
85                 {
86                         weaponwant = stof(car(rest)); rest = cdr(rest);
87                         if(imp >= 0)
88                                 if((get_weaponinfo(weaponwant)).impulse != imp)
89                                         continue;
90
91                         --c;
92                         if(c == 0)
93                         {
94                                 client_hasweapon(pl, weaponwant, TRUE, TRUE);
95                                 break;
96                         }
97                 }
98         }
99         return 0;
100 }
101
102 void W_CycleWeapon(string weaponorder, float dir)
103 {
104         float w;
105         w = W_GetCycleWeapon(self, weaponorder, dir, -1, 1, TRUE);
106         if(w > 0)
107                 W_SwitchWeapon(w);
108 }
109
110 void W_NextWeaponOnImpulse(float imp)
111 {
112         float w;
113         w = W_GetCycleWeapon(self, self.cvar_cl_weaponpriority, +1, imp, 1, (self.cvar_cl_weaponimpulsemode == 0));
114         if(w > 0)
115                 W_SwitchWeapon(w);
116 }
117
118 // next weapon
119 void W_NextWeapon(float list)
120 {
121         if(list == 0)
122                 W_CycleWeapon(weaponorder_byid, -1);
123         else if(list == 1)
124                 W_CycleWeapon(self.weaponorder_byimpulse, -1);
125         else if(list == 2)
126                 W_CycleWeapon(self.cvar_cl_weaponpriority, -1);
127 }
128
129 // prev weapon
130 void W_PreviousWeapon(float list)
131 {
132         if(list == 0)
133                 W_CycleWeapon(weaponorder_byid, +1);
134         else if(list == 1)
135                 W_CycleWeapon(self.weaponorder_byimpulse, +1);
136         else if(list == 2)
137                 W_CycleWeapon(self.cvar_cl_weaponpriority, +1);
138 }
139
140 // previously used if exists and has ammo, (second) best otherwise
141 void W_LastWeapon()
142 {
143         if(client_hasweapon(self, self.cnt, TRUE, FALSE))
144                 W_SwitchWeapon(self.cnt);
145         else
146                 W_SwitchToOtherWeapon(self);
147 }
148
149 float w_getbestweapon(entity e)
150 {
151         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, FALSE, TRUE);
152 }
153
154 // generic weapons table
155 // TODO should they be macros instead?
156 float weapon_action(float wpn, float wrequest)
157 {
158         return (get_weaponinfo(wpn)).weapon_func(wrequest);
159 }
160
161 string W_Name(float weaponid)
162 {
163         return (get_weaponinfo(weaponid)).message;
164 }
165
166 float W_WeaponBit(float wpn)
167 {
168         return (get_weaponinfo(wpn)).weapons;
169 }
170
171 float W_AmmoItemCode(float wpn)
172 {
173         float f = (get_weaponinfo(wpn)).items;
174         f &~= IT_SUPERWEAPON;
175         return f;
176 }
177
178 void thrown_wep_think()
179 {
180         self.solid = SOLID_TRIGGER;
181         self.owner = world;
182         SUB_SetFade(self, time + 20, 1);
183 }
184
185 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
186 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
187 {
188         entity oldself, wep;
189         float wa, thisammo, i, j;
190         string s;
191         var .float ammofield;
192
193         wep = spawn();
194
195         setorigin(wep, org);
196         wep.classname = "droppedweapon";
197         wep.velocity = velo;
198         wep.owner = wep.enemy = own;
199         wep.flags |= FL_TOSSED;
200         wep.colormap = own.colormap;
201
202         wa = W_AmmoItemCode(wpn);
203         if(wa == 0)
204         {
205                 oldself = self;
206                 self = wep;
207                 weapon_defaultspawnfunc(wpn);
208                 self = oldself;
209                 if(startitem_failed)
210                         return string_null;
211                 wep.glowmod = own.weaponentity_glowmod;
212                 wep.think = thrown_wep_think;
213                 wep.nextthink = time + 0.5;
214                 return "";
215         }
216         else
217         {
218                 s = "";
219                 oldself = self;
220                 self = wep;
221                 weapon_defaultspawnfunc(wpn);
222                 self = oldself;
223                 if(startitem_failed)
224                         return string_null;
225                 if(doreduce && g_weapon_stay == 2)
226                 {
227                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
228                         {
229                                 if(wa & j)
230                                 {
231                                         ammofield = Item_CounterField(j);
232                                         wep.ammofield = 0;
233
234                                         // if our weapon is loaded, give its load back to the player
235                                         if(self.(weapon_load[self.weapon]) > 0)
236                                         {
237                                                 own.ammofield += self.(weapon_load[self.weapon]);
238                                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
239                                         }
240                                 }
241                         }
242                 }
243                 else if(doreduce)
244                 {
245                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
246                         {
247                                 if(wa & j)
248                                 {
249                                         ammofield = Item_CounterField(j);
250                                         thisammo = min(own.ammofield, wep.ammofield);
251                                         wep.ammofield = thisammo;
252                                         own.ammofield -= thisammo;
253                                         s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
254
255                                         // if our weapon is loaded, give its load back to the player
256                                         if(self.(weapon_load[self.weapon]) > 0)
257                                         {
258                                                 own.ammofield += self.(weapon_load[self.weapon]);
259                                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
260                                         }
261                                 }
262                         }
263                         s = substring(s, 5, -1);
264                 }
265                 wep.glowmod = own.weaponentity_glowmod;
266                 wep.think = thrown_wep_think;
267                 wep.nextthink = time + 0.5;
268                 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
269                 return s;
270         }
271 }
272
273 float W_IsWeaponThrowable(float w)
274 {
275         float wb, wa;
276
277         if (!autocvar_g_pickup_items)
278                 return 0;
279         if (g_weaponarena)
280                 return 0;
281         if (g_lms)
282                 return 0;
283         if (g_ca)
284                 return 0;
285         if (g_cts)
286                 return 0;
287         if (g_nexball && w == WEP_GRENADE_LAUNCHER)
288                 return 0;
289
290         wb = W_WeaponBit(w);
291         if(!wb)
292                 return 0;
293         if(wb & WEPBIT_SUPERWEAPONS) // can't throw a superweapon, they don't work
294                 return 0;
295         wa = W_AmmoItemCode(w);
296         if(start_weapons & wb)
297         {
298                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
299                 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
300                         return 0;
301                 if(wa == 0)
302                         return 0;
303         }
304
305         return 1;
306 }
307
308 // toss current weapon
309 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
310 {
311         float w, wb;
312         string a;
313
314         w = self.weapon;
315         if (w == 0)
316                 return; // just in case
317         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
318                 return;
319         if(!autocvar_g_weapon_throwable)
320                 return;
321         if(self.weaponentity.state != WS_READY)
322                 return;
323         if(!W_IsWeaponThrowable(w))
324                 return;
325
326         wb = W_WeaponBit(w);
327         if(self.weapons & wb != wb)
328                 return;
329
330         self.weapons &~= wb;
331         W_SwitchWeapon_Force(self, w_getbestweapon(self));
332         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
333         if not(a)
334                 return;
335         if(a == "")
336                 sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
337         else
338                 sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n"));
339 }
340
341 // Bringed back weapon frame
342 void W_WeaponFrame()
343 {
344         vector fo, ri, up;
345
346         if (frametime)
347                 self.weapon_frametime = frametime;
348
349         if(((arena_roundbased || g_ca || g_freezetag) && time < warmup) || ((time < game_starttime) && !autocvar_sv_ready_restart_after_countdown))
350                 return;
351
352         if(self.freezetag_frozen == 1)
353                 return;
354
355         if (!self.weaponentity || self.health < 1)
356                 return; // Dead player can't use weapons and injure impulse commands
357
358         if(!self.switchweapon)
359         {
360                 self.weapon = 0;
361                 self.switchingweapon = 0;
362                 self.weaponentity.state = WS_CLEAR;
363                 self.weaponname = "";
364                 self.items &~= IT_AMMO;
365                 return;
366         }
367
368         makevectors(self.v_angle);
369         fo = v_forward; // save them in case the weapon think functions change it
370         ri = v_right;
371         up = v_up;
372
373         // Change weapon
374         if (self.weapon != self.switchweapon)
375         {
376                 if (self.weaponentity.state == WS_CLEAR)
377                 {
378                         // end switching!
379                         self.switchingweapon = self.switchweapon;
380
381                         //setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
382                         self.weaponentity.state = WS_RAISE;
383                         weapon_action(self.switchweapon, WR_SETUP);
384
385                         // set our clip load to the load of the weapon we switched to, if it's reloadable
386                         entity e;
387                         e = get_weaponinfo(self.switchweapon);
388                         if(e.spawnflags & WEP_FLAG_RELOADABLE && cvar(strcat("g_balance_", e.netname, "_reload_ammo"))) // prevent accessing undefined cvars
389                         {
390                                 self.clip_load = self.(weapon_load[self.switchweapon]);
391                                 self.clip_size = cvar(strcat("g_balance_", e.netname, "_reload_ammo"));
392                         }
393                         else
394                                 self.clip_load = self.clip_size = 0;
395
396                         // VorteX: add player model weapon select frame here
397                         // setcustomframe(PlayerWeaponRaise);
398                         weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_weaponswitchdelay, w_ready);
399                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
400                 }
401                 else if (self.weaponentity.state == WS_DROP)
402                 {
403                         // in dropping phase we can switch at any time
404                         self.switchingweapon = self.switchweapon;
405                 }
406                 else if (self.weaponentity.state == WS_READY)
407                 {
408                         // start switching!
409                         self.switchingweapon = self.switchweapon;
410
411 #ifndef INDEPENDENT_ATTACK_FINISHED
412                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
413                         {
414 #endif
415                         sound (self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
416                         self.weaponentity.state = WS_DROP;
417                         // set up weapon switch think in the future, and start drop anim
418                         weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_weaponswitchdelay, w_clear);
419                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
420 #ifndef INDEPENDENT_ATTACK_FINISHED
421                         }
422 #endif
423                 }
424         }
425
426         // LordHavoc: network timing test code
427         //if (self.button0)
428         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
429
430         float wb;
431         wb = W_WeaponBit(self.weapon);
432
433         // call the think code which may fire the weapon
434         // and do so multiple times to resolve framerate dependency issues if the
435         // server framerate is very low and the weapon fire rate very high
436         float c;
437         c = 0;
438         while (c < W_TICSPERFRAME)
439         {
440                 c = c + 1;
441                 if(wb && ((self.weapons & wb) == 0))
442                 {
443                         if(self.weapon == self.switchweapon)
444                                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
445                         wb = 0;
446                 }
447
448                 v_forward = fo;
449                 v_right = ri;
450                 v_up = up;
451
452                 if(wb)
453                         weapon_action(self.weapon, WR_THINK);
454                 else
455                         weapon_action(self.weapon, WR_GONETHINK);
456
457                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
458                 {
459                         if(self.weapon_think)
460                         {
461                                 v_forward = fo;
462                                 v_right = ri;
463                                 v_up = up;
464                                 self.weapon_think();
465                         }
466                         else
467                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
468                 }
469         }
470
471         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
472         //if (ATTACK_FINISHED(self) < time)
473         //      ATTACK_FINISHED(self) = time;
474
475         //if (self.weapon_nextthink < time)
476         //      self.weapon_nextthink = time;
477
478         // update currentammo incase it has changed
479 #if 0
480         if (self.items & IT_CELLS)
481                 self.currentammo = self.ammo_cells;
482         else if (self.items & IT_ROCKETS)
483                 self.currentammo = self.ammo_rockets;
484         else if (self.items & IT_NAILS)
485                 self.currentammo = self.ammo_nails;
486         else if (self.items & IT_SHELLS)
487                 self.currentammo = self.ammo_shells;
488         else
489                 self.currentammo = 1;
490 #endif
491 }