Merge remote-tracking branch 'origin/master' into Mario/lms_updates
[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 .float savenextthink;
162 void thrown_wep_think()
163 {
164         self.owner = world;
165         float timeleft = self.savenextthink - time;
166         if(timeleft > 1)
167                 SUB_SetFade(self, self.savenextthink - 1, 1);
168         else if(timeleft > 0)
169                 SUB_SetFade(self, time, timeleft);
170         else
171                 SUB_VanishOrRemove(self);
172 }
173
174 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
175 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
176 {
177         entity oldself, wep;
178         float wa, thisammo, i, j;
179         string s;
180         var .float ammofield;
181
182         wep = spawn();
183
184         setorigin(wep, org);
185         wep.classname = "droppedweapon";
186         wep.velocity = velo;
187         wep.owner = wep.enemy = own;
188         wep.flags |= FL_TOSSED;
189         wep.colormap = own.colormap;
190
191         if(WEPSET_CONTAINS_AW(WEPBIT_SUPERWEAPONS, wpn))
192         {
193                 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
194                 {
195                         wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
196                 }
197                 else
198                 {
199                         float superweapons = 1;
200                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
201                                 if(WEPSET_CONTAINS_AW(WEPBIT_SUPERWEAPONS, i))
202                                         if(WEPSET_CONTAINS_EW(own, i))
203                                                 ++superweapons;
204                         if(superweapons <= 1)
205                         {
206                                 wep.superweapons_finished = own.superweapons_finished;
207                                 own.superweapons_finished = 0;
208                         }
209                         else
210                         {
211                                 float timeleft = own.superweapons_finished - time;
212                                 float weptimeleft = timeleft / superweapons;
213                                 wep.superweapons_finished = time + weptimeleft;
214                                 own.superweapons_finished -= weptimeleft;
215                         }
216                 }
217         }
218
219         wa = W_AmmoItemCode(wpn);
220         if(wa == 0)
221         {
222                 oldself = self;
223                 self = wep;
224                 weapon_defaultspawnfunc(wpn);
225                 self = oldself;
226                 if(startitem_failed)
227                         return string_null;
228                 wep.glowmod = own.weaponentity_glowmod;
229                 wep.think = thrown_wep_think;
230                 wep.savenextthink = wep.nextthink;
231                 wep.nextthink = min(wep.nextthink, time + 0.5);
232                 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
233                 return "";
234         }
235         else
236         {
237                 s = "";
238                 oldself = self;
239                 self = wep;
240                 weapon_defaultspawnfunc(wpn);
241                 self = oldself;
242                 if(startitem_failed)
243                         return string_null;
244                 if(doreduce && g_weapon_stay == 2)
245                 {
246                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
247                         {
248                                 if(wa & j)
249                                 {
250                                         ammofield = Item_CounterField(j);
251
252                                         // if our weapon is loaded, give its load back to the player
253                                         if(self.(weapon_load[self.weapon]) > 0)
254                                         {
255                                                 own.ammofield += self.(weapon_load[self.weapon]);
256                                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
257                                         }
258
259                                         wep.ammofield = 0;
260                                 }
261                         }
262                 }
263                 else if(doreduce)
264                 {
265                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
266                         {
267                                 if(wa & j)
268                                 {
269                                         ammofield = Item_CounterField(j);
270
271                                         // if our weapon is loaded, give its load back to the player
272                                         if(self.(weapon_load[self.weapon]) > 0)
273                                         {
274                                                 own.ammofield += self.(weapon_load[self.weapon]);
275                                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
276                                         }
277
278                                         thisammo = min(own.ammofield, wep.ammofield);
279                                         wep.ammofield = thisammo;
280                                         own.ammofield -= thisammo;
281                                         s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
282                                 }
283                         }
284                         s = substring(s, 5, -1);
285                 }
286                 wep.glowmod = own.weaponentity_glowmod;
287                 wep.think = thrown_wep_think;
288                 wep.savenextthink = wep.nextthink;
289                 wep.nextthink = min(wep.nextthink, time + 0.5);
290                 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
291
292                 return s;
293         }
294 }
295
296 float W_IsWeaponThrowable(float w)
297 {
298         float wa;
299
300         if (!autocvar_g_pickup_items)
301                 return 0;
302         if (g_weaponarena)
303                 return 0;
304         if (g_cts)
305                 return 0;
306         if (g_nexball && w == WEP_GRENADE_LAUNCHER)
307                 return 0;
308     if(w == 0)
309         return 0;
310         
311         wa = W_AmmoItemCode(w);
312         if(WEPSET_CONTAINS_AW(start_weapons, w))
313         {
314                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
315                 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
316                         return 0;
317                 if(wa == 0)
318                         return 0;
319         }
320
321         return 1;
322 }
323
324 // toss current weapon
325 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
326 {
327         float w;
328         string a;
329
330         w = self.weapon;
331         if (w == 0)
332                 return; // just in case
333         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
334                 return;
335         if(!autocvar_g_weapon_throwable)
336                 return;
337         if(self.weaponentity.state != WS_READY)
338                 return;
339         if(!W_IsWeaponThrowable(w))
340                 return;
341
342         if(!WEPSET_CONTAINS_EW(self, w))
343                 return;
344         WEPSET_ANDNOT_EW(self, w);
345
346         W_SwitchWeapon_Force(self, w_getbestweapon(self));
347         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
348         
349         if not(a) return;
350         Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_WEAPON_DROP, a, w);
351 }
352
353 float forbidWeaponUse()
354 {
355         if(time < game_starttime && !autocvar_sv_ready_restart_after_countdown)
356                 return 1;
357         if(round_handler_IsActive() && !round_handler_IsRoundStarted())
358                 return 1;
359         if(self.player_blocked)
360                 return 1;
361         if(self.freezetag_frozen)
362                 return 1;
363         return 0;
364 }
365
366 void W_WeaponFrame()
367 {
368         vector fo, ri, up;
369
370         if (frametime)
371                 self.weapon_frametime = frametime;
372
373         if (!self.weaponentity || self.health < 1)
374                 return; // Dead player can't use weapons and injure impulse commands
375
376         if(forbidWeaponUse())
377         if(self.weaponentity.state != WS_CLEAR)
378         {
379                 w_ready();
380                 return;
381         }
382
383         if(!self.switchweapon)
384         {
385                 self.weapon = 0;
386                 self.switchingweapon = 0;
387                 self.weaponentity.state = WS_CLEAR;
388                 self.weaponname = "";
389                 self.items &~= IT_AMMO;
390                 return;
391         }
392
393         makevectors(self.v_angle);
394         fo = v_forward; // save them in case the weapon think functions change it
395         ri = v_right;
396         up = v_up;
397
398         // Change weapon
399         if (self.weapon != self.switchweapon)
400         {
401                 if (self.weaponentity.state == WS_CLEAR)
402                 {
403                         // end switching!
404                         self.switchingweapon = self.switchweapon;
405
406                         //setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
407                         self.weaponentity.state = WS_RAISE;
408                         weapon_action(self.switchweapon, WR_SETUP);
409
410                         // set our clip load to the load of the weapon we switched to, if it's reloadable
411                         entity e;
412                         e = get_weaponinfo(self.switchweapon);
413                         if(e.spawnflags & WEP_FLAG_RELOADABLE && cvar(strcat("g_balance_", e.netname, "_reload_ammo"))) // prevent accessing undefined cvars
414                         {
415                                 self.clip_load = self.(weapon_load[self.switchweapon]);
416                                 self.clip_size = cvar(strcat("g_balance_", e.netname, "_reload_ammo"));
417                         }
418                         else
419                                 self.clip_load = self.clip_size = 0;
420
421                         // VorteX: add player model weapon select frame here
422                         // setcustomframe(PlayerWeaponRaise);
423                         weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_weaponswitchdelay, w_ready);
424                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
425                 }
426                 else if (self.weaponentity.state == WS_DROP)
427                 {
428                         // in dropping phase we can switch at any time
429                         self.switchingweapon = self.switchweapon;
430                 }
431                 else if (self.weaponentity.state == WS_READY)
432                 {
433                         // start switching!
434                         self.switchingweapon = self.switchweapon;
435
436 #ifndef INDEPENDENT_ATTACK_FINISHED
437                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
438                         {
439 #endif
440                         sound (self, CH_WEAPON_SINGLE, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
441                         self.weaponentity.state = WS_DROP;
442                         // set up weapon switch think in the future, and start drop anim
443                         weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_weaponswitchdelay, w_clear);
444                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
445 #ifndef INDEPENDENT_ATTACK_FINISHED
446                         }
447 #endif
448                 }
449         }
450
451         // LordHavoc: network timing test code
452         //if (self.button0)
453         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
454
455         float w;
456         w = self.weapon;
457
458         // call the think code which may fire the weapon
459         // and do so multiple times to resolve framerate dependency issues if the
460         // server framerate is very low and the weapon fire rate very high
461         float c;
462         c = 0;
463         while (c < W_TICSPERFRAME)
464         {
465                 c = c + 1;
466                 if(w && !WEPSET_CONTAINS_EW(self, w))
467                 {
468                         if(self.weapon == self.switchweapon)
469                                 W_SwitchWeapon_Force(self, w_getbestweapon(self));
470                         w = 0;
471                 }
472
473                 v_forward = fo;
474                 v_right = ri;
475                 v_up = up;
476
477                 if(w)
478                         weapon_action(self.weapon, WR_THINK);
479                 else
480                         weapon_action(self.weapon, WR_GONETHINK);
481
482                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
483                 {
484                         if(self.weapon_think)
485                         {
486                                 v_forward = fo;
487                                 v_right = ri;
488                                 v_up = up;
489                                 self.weapon_think();
490                         }
491                         else
492                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
493                 }
494         }
495
496         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
497         //if (ATTACK_FINISHED(self) < time)
498         //      ATTACK_FINISHED(self) = time;
499
500         //if (self.weapon_nextthink < time)
501         //      self.weapon_nextthink = time;
502
503         // update currentammo incase it has changed
504 #if 0
505         if (self.items & IT_CELLS)
506                 self.currentammo = self.ammo_cells;
507         else if (self.items & IT_ROCKETS)
508                 self.currentammo = self.ammo_rockets;
509         else if (self.items & IT_NAILS)
510                 self.currentammo = self.ammo_nails;
511         else if (self.items & IT_SHELLS)
512                 self.currentammo = self.ammo_shells;
513         else
514                 self.currentammo = 1;
515 #endif
516 }
517
518 string W_Apply_Weaponreplace(string in)
519 {
520         float n = tokenize_console(in);
521         string out = "";
522         float i;
523         for(i = 0; i < n; ++i)
524         {
525                 string s = argv(i);
526                 string r = cvar_string(strcat("g_weaponreplace_", s));
527                 if(r == "")
528                         out = strcat(out, " ", s);
529                 else if(r != "0")
530                         out = strcat(out, " ", r);
531         }
532         return substring(out, 1, -1);
533 }