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