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