]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_weapons.qc
change extend/reducematchtime from an rpn alias into QC logic
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_weapons.qc
1 void W_Reload()
2 {
3     self.wish_reload = 1;
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_Reload();
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 float w_getbestweapon(entity e)
141 {
142         return W_GetCycleWeapon(e, e.cvar_cl_weaponpriority, 0, -1, FALSE, TRUE);
143 };
144
145 // generic weapons table
146 // TODO should they be macros instead?
147 float weapon_action(float wpn, float wrequest)
148 {
149         return (get_weaponinfo(wpn)).weapon_func(wrequest);
150 };
151
152 string W_Name(float weaponid)
153 {
154         return (get_weaponinfo(weaponid)).message;
155 }
156
157 float W_WeaponBit(float wpn)
158 {
159         return (get_weaponinfo(wpn)).weapons;
160 }
161
162 float W_AmmoItemCode(float wpn)
163 {
164         return (get_weaponinfo(wpn)).items;
165 }
166
167 void thrown_wep_think()
168 {
169         self.solid = SOLID_TRIGGER;
170         self.owner = world;
171         SUB_SetFade(self, time + 20, 1);
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         wa = W_AmmoItemCode(wpn);
192         if(wa == IT_SUPERWEAPON || wa == 0)
193         {
194                 oldself = self;
195                 self = wep;
196                 weapon_defaultspawnfunc(wpn);
197                 self = oldself;
198                 if(startitem_failed)
199                         return string_null;
200                 wep.think = thrown_wep_think;
201                 wep.nextthink = time + 0.5;
202                 return "";
203         }
204         else
205         {
206                 s = "";
207                 oldself = self;
208                 self = wep;
209                 weapon_defaultspawnfunc(wpn);
210                 self = oldself;
211                 if(startitem_failed)
212                         return string_null;
213                 if(doreduce)
214                 {
215                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
216                         {
217                                 if(wa & j)
218                                 {
219                                         ammofield = Item_CounterField(j);
220                                         thisammo = min(own.ammofield, wep.ammofield);
221                                         wep.ammofield = thisammo;
222                                         own.ammofield -= thisammo;
223                                         s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
224                                 }
225                         }
226                         s = substring(s, 5, -1);
227                 }
228                 wep.think = thrown_wep_think;
229                 wep.nextthink = time + 0.5;
230                 return s;
231         }
232 }
233
234 float W_IsWeaponThrowable(float w)
235 {
236         float wb, wa;
237
238         if (!autocvar_g_pickup_items)
239                 return 0;
240         if (g_weaponarena)
241                 return 0;
242         if (g_lms)
243                 return 0;
244         if (g_ca)
245                 return 0;
246         if (g_cts)
247                 return 0;
248         if (g_nexball && w == WEP_GRENADE_LAUNCHER)
249                 return 0;
250
251         wb = W_WeaponBit(w);
252         if(!wb)
253                 return 0;
254         wa = W_AmmoItemCode(w);
255         if(start_weapons & wb)
256         {
257                 if(wa == IT_SUPERWEAPON && start_items & IT_UNLIMITED_SUPERWEAPONS)
258                         return 0;
259                 if(wa != IT_SUPERWEAPON && start_items & IT_UNLIMITED_WEAPON_AMMO)
260                         return 0;
261                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
262                 if(wa == 0)
263                         return 0;
264         }
265
266         return 1;
267 }
268
269 // toss current weapon
270 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
271 {
272         local float w, wb;
273         string a;
274
275         w = self.weapon;
276         if (w == 0)
277                 return; // just in case
278         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
279                 return;
280         if(!autocvar_g_weapon_throwable)
281                 return;
282         if(autocvar_g_weapon_stay == 1)
283                 return;
284         if(self.weaponentity.state != WS_READY)
285                 return;
286         if(!W_IsWeaponThrowable(w))
287                 return;
288
289         wb = W_WeaponBit(w);
290         if(self.weapons & wb != wb)
291                 return;
292
293         self.weapons &~= wb;
294         W_SwitchWeapon_Force(self, w_getbestweapon(self));
295         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
296         if not(a)
297                 return;
298         if(a == "")
299                 sprint(self, strcat("You dropped the ^2", W_Name(w), "\n"));
300         else
301                 sprint(self, strcat("You dropped the ^2", W_Name(w), " with ", a, "\n"));
302 };
303
304 // Bringed back weapon frame
305 void W_WeaponFrame()
306 {
307         vector fo, ri, up;
308
309         if (frametime)
310                 self.weapon_frametime = frametime;
311
312         if(((arena_roundbased || g_ca || g_freezetag) && time < warmup) || ((time < game_starttime) && !autocvar_sv_ready_restart_after_countdown))
313                 return;
314
315         if(g_freezetag && self.freezetag_frozen == 1)
316                 return;
317
318         if (!self.weaponentity || self.health < 1)
319                 return; // Dead player can't use weapons and injure impulse commands
320
321         if(!self.switchweapon)
322         {
323                 self.weapon = 0;
324                 self.weaponentity.state = WS_CLEAR;
325                 self.weaponname = "";
326                 self.items &~= IT_AMMO;
327                 return;
328         }
329
330         makevectors(self.v_angle);
331         fo = v_forward; // save them in case the weapon think functions change it
332         ri = v_right;
333         up = v_up;
334
335         // Change weapon
336         if (self.weapon != self.switchweapon)
337         {
338                 if (self.weaponentity.state == WS_CLEAR)
339                 {
340                         setanim(self, self.anim_draw, FALSE, TRUE, TRUE);
341                         self.weaponentity.state = WS_RAISE;
342                         weapon_action(self.switchweapon, WR_SETUP);
343                         // VorteX: add player model weapon select frame here
344                         // setcustomframe(PlayerWeaponRaise);
345                         weapon_thinkf(WFRAME_IDLE, autocvar_g_balance_weaponswitchdelay, w_ready);
346                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, '0 0 0');
347                 }
348                 else if (self.weaponentity.state == WS_READY)
349                 {
350 #ifndef INDEPENDENT_ATTACK_FINISHED
351                         if(ATTACK_FINISHED(self) <= time + self.weapon_frametime * 0.5)
352                         {
353 #endif
354                         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
355                         sound (self, CHAN_WEAPON2, "weapons/weapon_switch.wav", VOL_BASE, ATTN_NORM);
356                         self.weaponentity.state = WS_DROP;
357                         // set up weapon switch think in the future, and start drop anim
358                         weapon_thinkf(WFRAME_DONTCHANGE, autocvar_g_balance_weaponswitchdelay, w_clear);
359                         weapon_boblayer1(PLAYER_WEAPONSELECTION_SPEED, PLAYER_WEAPONSELECTION_RANGE);
360 #ifndef INDEPENDENT_ATTACK_FINISHED
361                         }
362 #endif
363                 }
364         }
365
366         // LordHavoc: network timing test code
367         //if (self.button0)
368         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(self)), " >= ", ftos(self.weapon_nextthink), "\n");
369
370         float wb;
371         wb = W_WeaponBit(self.weapon);
372
373         // call the think code which may fire the weapon
374         // and do so multiple times to resolve framerate dependency issues if the
375         // server framerate is very low and the weapon fire rate very high
376         local float c;
377         c = 0;
378         while (c < W_TICSPERFRAME)
379         {
380                 c = c + 1;
381                 if(wb && ((self.weapons & wb) == 0))
382                 {
383                         W_SwitchWeapon_Force(self, w_getbestweapon(self));
384                         wb = 0;
385                 }
386                 if(wb)
387                 {
388                         v_forward = fo;
389                         v_right = ri;
390                         v_up = up;
391                         self.weaponentity_glowmod = '0 0 0'; // reset glowmod, weapon think function only *might* set it
392                         weapon_action(self.weapon, WR_THINK);
393                 }
394                 if (time + self.weapon_frametime * 0.5 >= self.weapon_nextthink)
395                 {
396                         if(self.weapon_think)
397                         {
398                                 v_forward = fo;
399                                 v_right = ri;
400                                 v_up = up;
401                                 self.weapon_think();
402                         }
403                         else
404                                 bprint("\{1}^1ERROR: undefined weapon think function for ", self.netname, "\n");
405                 }
406         }
407
408         // don't let attack_finished fall behind when not firing (must be after weapon_setup calls!)
409         //if (ATTACK_FINISHED(self) < time)
410         //      ATTACK_FINISHED(self) = time;
411
412         //if (self.weapon_nextthink < time)
413         //      self.weapon_nextthink = time;
414
415         // update currentammo incase it has changed
416 #if 0
417         if (self.items & IT_CELLS)
418                 self.currentammo = self.ammo_cells;
419         else if (self.items & IT_ROCKETS)
420                 self.currentammo = self.ammo_rockets;
421         else if (self.items & IT_NAILS)
422                 self.currentammo = self.ammo_nails;
423         else if (self.items & IT_SHELLS)
424                 self.currentammo = self.ammo_shells;
425         else
426                 self.currentammo = 1;
427 #endif
428 };