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