]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/weaponsystem.qc
Merge branch 'master' into z411/bai-server
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / weaponsystem.qc
1 #include "weaponsystem.qh"
2
3 #include <common/animdecide.qh>
4 #include <common/constants.qh>
5 #include <common/items/_mod.qh>
6 #include <common/mapobjects/platforms.qh>
7 #include <common/monsters/_mod.qh>
8 #include <common/mutators/mutator/status_effects/_mod.qh>
9 #include <common/ent_cs.qh>
10 #include <common/net_linked.qh>
11 #include <common/notifications/all.qh>
12 #include <common/resources/sv_resources.qh>
13 #include <common/state.qh>
14 #include <common/util.qh>
15 #include <common/vehicles/all.qh>
16 #include <common/weapons/_all.qh>
17 #include <common/wepent.qh>
18 #include <lib/csqcmodel/sv_model.qh>
19 #include <server/cheats.qh>
20 #include <server/client.qh>
21 #include <server/command/common.qh>
22 #include <server/damage.qh>
23 #include <server/items/items.qh>
24 #include <server/hook.qh>
25 #include <server/mutators/_mod.qh>
26 #include <server/round_handler.qh>
27 #include <server/weapons/selection.qh>
28 #include <server/world.qh>
29
30 .int state;
31
32 .float weapon_frametime;
33
34 float W_WeaponRateFactor(entity this)
35 {
36         float t = 1;
37         if(autocvar_g_weaponratefactor > 0)
38                 t = 1.0 / autocvar_g_weaponratefactor;
39
40         MUTATOR_CALLHOOK(WeaponRateFactor, t, this);
41         t = M_ARGV(0, float);
42
43         return t;
44 }
45
46 float W_WeaponSpeedFactor(entity this)
47 {
48         float t = 1.0 * autocvar_g_weaponspeedfactor;
49
50         MUTATOR_CALLHOOK(WeaponSpeedFactor, t, this);
51         t = M_ARGV(0, float);
52
53         return t;
54 }
55
56
57 bool CL_Weaponentity_CustomizeEntityForClient(entity this, entity client)
58 {
59         this.viewmodelforclient = this.owner;
60         if (IS_SPEC(client) && client.enemy == this.owner) this.viewmodelforclient = client;
61         return false;
62 }
63
64 vector CL_Weapon_GetShotOrg(int wpn)
65 {
66         entity wi = REGISTRY_GET(Weapons, wpn);
67         entity e = spawn();
68         CL_WeaponEntity_SetModel(e, wi.mdl, false);
69         vector ret = e.movedir;
70         CL_WeaponEntity_SetModel(e, "", false);
71         delete(e);
72         return ret;
73 }
74
75 .float m_alpha;
76 .string w_weaponname;
77 .int w_dmg;
78 .int w_deadflag;
79
80 void CL_Weaponentity_Think(entity this)
81 {
82         this.nextthink = time;
83         if (game_stopped) this.frame = this.anim_idle.x;
84         .entity weaponentity = this.weaponentity_fld;
85         if (this.owner.(weaponentity) != this)
86         {
87                 // owner has new gun; remove old one
88                 if (this.weaponchild) delete(this.weaponchild);
89                 if (this.hook) delete(this.hook);
90                 delete(this);
91                 return;
92         }
93         if (IS_DEAD(this.owner))
94         {
95                 // owner died; disappear
96                 this.model = "";
97                 if (this.weaponchild) this.weaponchild.model = "";
98                 return;
99         }
100         if (this.w_weaponname != this.weaponname || this.w_dmg != this.modelindex || this.w_deadflag != this.deadflag)
101         {
102                 // owner changed weapons; update appearance
103                 this.w_weaponname = this.weaponname;
104                 this.w_dmg = this.modelindex;
105                 this.w_deadflag = this.deadflag;
106
107                 CL_WeaponEntity_SetModel(this, this.weaponname, true);
108         }
109
110         this.alpha = -1;  // TODO: don't render this entity at all
111
112         if (this.owner.alpha == default_player_alpha) this.m_alpha = default_weapon_alpha;
113         else if (this.owner.alpha != 0) this.m_alpha = this.owner.alpha;
114         else this.m_alpha  = 1;
115
116         if (this.weaponchild)
117         {
118                 this.weaponchild.alpha = this.alpha;
119                 this.weaponchild.effects = this.effects;
120         }
121 }
122
123 void CL_ExteriorWeaponentity_Think(entity this)
124 {
125         this.nextthink = time;
126         .entity weaponentity = this.weaponentity_fld;
127         entity w_ent = this.owner.(weaponentity);
128         if (this.owner.exteriorweaponentity != this)
129         {
130                 delete(this);
131                 return;
132         }
133         if (IS_DEAD(this.owner))
134         {
135                 this.model = "";
136                 return;
137         }
138         if (this.weaponname != w_ent.weaponname || this.dmg != w_ent.modelindex || this.deadflag != w_ent.deadflag)
139         {
140                 this.weaponname = w_ent.weaponname;
141                 this.dmg = w_ent.modelindex;
142                 this.deadflag = w_ent.deadflag;
143                 if (w_ent.weaponname != "")
144                 {
145                         _setmodel(this, W_Model(strcat("v_", w_ent.weaponname, ".md3")));
146                         setsize(this, '0 0 0', '0 0 0');
147                 }
148                 else this.model = "";
149
150                 int tag_found;
151                 if ((tag_found = gettagindex(this.owner, "tag_weapon")))
152                 {
153                         this.tag_index = tag_found;
154                         this.tag_entity = this.owner;
155                 }
156                 else
157                 {
158                         setattachment(this, this.owner, "bip01 r hand");
159                 }
160         }
161         this.effects = this.owner.effects;
162         this.effects |= EF_LOWPRECISION;
163         this.effects = this.effects & EFMASK_CHEAP;  // eat performance
164         if (this.owner.alpha == default_player_alpha) this.alpha = default_weapon_alpha;
165         else if (this.owner.alpha != 0) this.alpha = this.owner.alpha;
166         else this.alpha = 1;
167
168         Weapon wep = this.owner.(weaponentity).m_weapon;
169         if (wep) this.glowmod = weaponentity_glowmod(wep, this.owner.clientcolors, this.owner.(weaponentity));
170         this.colormap = this.owner.colormap;
171         this.skin = w_ent.skin;
172
173         CSQCMODEL_AUTOUPDATE(this);
174 }
175
176 // spawning weaponentity for client
177 void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
178 {
179         entity w_ent = actor.(weaponentity) = new(weaponentity);
180         w_ent.solid = SOLID_NOT;
181         w_ent.owner = actor;
182         setmodel(w_ent, MDL_Null);  // precision set when changed
183         setorigin(w_ent, '0 0 0');
184         w_ent.weaponentity_fld = weaponentity;
185         setthink(w_ent, CL_Weaponentity_Think);
186         w_ent.nextthink = time;
187         w_ent.viewmodelforclient = actor;
188         w_ent.draggable = drag_undraggable;
189         setcefc(w_ent, CL_Weaponentity_CustomizeEntityForClient);
190
191         wepent_link(w_ent);
192
193         if (weaponentity == weaponentities[0]) // only one exterior model, thank you very much
194         {
195                 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
196                 exterior.solid = SOLID_NOT;
197                 exterior.owner = actor;
198                 exterior.draggable = drag_undraggable;
199                 exterior.weaponentity_fld = weaponentity;
200                 setorigin(exterior, '0 0 0');
201                 setthink(exterior, CL_ExteriorWeaponentity_Think);
202                 exterior.nextthink = time;
203
204                 CSQCMODEL_AUTOINIT(exterior);
205         }
206 }
207
208 // Weapon subs
209 void w_clear(Weapon thiswep, entity actor, .entity weaponentity, int fire)
210 {
211         entity w_ent = actor.(weaponentity);
212         if (w_ent)
213         {
214                 w_ent.m_weapon = WEP_Null;
215                 w_ent.m_switchingweapon = WEP_Null;
216                 w_ent.state = WS_CLEAR;
217                 w_ent.effects = 0;
218         }
219 }
220
221 void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
222 {
223         entity w_ent = actor.(weaponentity);
224         if (w_ent) w_ent.state = WS_READY;
225         weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
226 }
227
228 .float prevdryfire;
229 .float prevwarntime;
230 bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary, .entity weaponentity)
231 {
232         if ((actor.items & IT_UNLIMITED_AMMO)) return true;
233         bool ammo = false;
234         if (secondary) ammo = thiswep.wr_checkammo2(thiswep, actor, weaponentity);
235         else ammo = thiswep.wr_checkammo1(thiswep, actor, weaponentity);
236         if (ammo) return true;
237         // always keep the Mine Layer if we placed mines, so that we can detonate them
238         if (thiswep == WEP_MINE_LAYER)
239         {
240                 IL_EACH(g_mines, it.owner == actor && it.weaponentity_fld == weaponentity,
241                 {
242                         return false;
243                 });
244         }
245
246         if (thiswep == WEP_SHOTGUN)
247                 if (!secondary && WEP_CVAR(shotgun, secondary) == 1) return false;           // no clicking, just allow
248
249         if (thiswep == actor.(weaponentity).m_switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
250         {
251                 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
252                 actor.prevdryfire = time;
253         }
254
255         // check if the other firing mode has enough ammo
256         bool ammo_other = false;
257         if (secondary) ammo_other = thiswep.wr_checkammo1(thiswep, actor, weaponentity);
258         else ammo_other = thiswep.wr_checkammo2(thiswep, actor, weaponentity);
259         if (ammo_other)
260         {
261                 if (time - actor.prevwarntime > 1)
262                         Send_Notification(NOTIF_ONE, actor, MSG_MULTI, ITEM_WEAPON_PRIMORSEC, thiswep.m_id, secondary, (1 - secondary));
263                 actor.prevwarntime = time;
264         }
265         else  // this weapon is totally unable to fire, switch to another one
266         {
267                 W_SwitchToOtherWeapon(actor, weaponentity);
268         }
269
270         return false;
271 }
272
273 .float race_penalty;
274 bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
275 {
276         if (actor.weaponentity == NULL) return true;
277         if (!weapon_prepareattack_checkammo(thiswep, actor, secondary, weaponentity)) return false;
278
279         // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
280         // if all players readied up and the countdown is running
281         if (time < game_starttime || time < actor.race_penalty) return false;
282
283         if (timeout_status == TIMEOUT_ACTIVE)  // don't allow the player to shoot while game is paused
284                 return false;
285
286         // do not even think about shooting if switching
287         if (actor.(weaponentity).m_switchweapon != actor.(weaponentity).m_weapon) return false;
288
289         if (attacktime >= 0)
290         {
291                 // don't fire if previous attack is not finished
292                 if (ATTACK_FINISHED(actor, weaponentity) > time + actor.(weaponentity).weapon_frametime * 0.5) return false;
293                 entity this = actor.(weaponentity);
294                 // don't fire while changing weapon
295                 if (this.state != WS_READY) return false;
296         }
297         return true;
298 }
299
300 void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
301 {
302         entity this = actor.(weaponentity);
303         if (this == NULL) return;
304         this.state = WS_INUSE;
305
306         if(StatusEffects_active(STATUSEFFECT_SpawnShield, actor)) // given this is performed often, perform a lighter check first
307                 StatusEffects_remove(STATUSEFFECT_SpawnShield, actor, STATUSEFFECT_REMOVE_CLEAR); // kill spawn shield when you fire
308
309         // if the weapon hasn't been firing continuously, reset the timer
310         if (attacktime >= 0)
311         {
312                 if (ATTACK_FINISHED(actor, weaponentity) < time - this.weapon_frametime * 1.5)
313                 {
314                         ATTACK_FINISHED(actor, weaponentity) = time;
315                         // dprint("resetting attack finished to ", ftos(time), "\n");
316                 }
317                 float arate = W_WeaponRateFactor(actor);
318                 ATTACK_FINISHED(actor, weaponentity) = ATTACK_FINISHED(actor, weaponentity) + attacktime * arate;
319
320                 if(autocvar_g_weaponswitch_debug_alternate && W_DualWielding(actor))
321                 {
322                         int slot = weaponslot(weaponentity);
323                         for(int wepslot = 0; wepslot < MAX_WEAPONSLOTS; ++wepslot)
324                         {
325                                 if(slot == wepslot)
326                                         continue;
327                                 .entity wepent = weaponentities[wepslot];
328                                 if(actor.(wepent) && actor.(wepent).m_weapon != WEP_Null)
329                                 {
330                                         if(ATTACK_FINISHED(actor, wepent) > time + actor.(wepent).weapon_frametime * 0.5)
331                                                 continue; // still cooling down!
332                                         if (ATTACK_FINISHED(actor, wepent) < time - actor.(wepent).weapon_frametime * 1.5)
333                                                 ATTACK_FINISHED(actor, wepent) = time;
334                                         ATTACK_FINISHED(actor, wepent) = ATTACK_FINISHED(actor, wepent) + (attacktime * arate) / MAX_WEAPONSLOTS;
335                                 }
336                         }
337                 }
338         }
339         this.bulletcounter += 1;
340         // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, weaponentity)), "\n");
341 }
342
343 bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
344 {
345         if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
346         {
347                 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
348                 return true;
349         }
350         return false;
351 }
352
353 /**
354  * @param t defer thinking until time + t
355  * @param func next think function
356  */
357 void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,
358         .entity weaponentity, int fire) func)
359 {
360         entity this = actor.(weaponentity);
361         if (this == NULL) return;
362         bool restartanim;
363         if (fr == WFRAME_DONTCHANGE)
364         {
365                 // this can happen when the weapon entity is newly spawned, since it has a clear state and no previous weapon frame
366                 if (this.wframe == WFRAME_DONTCHANGE)
367                         this.wframe = WFRAME_IDLE;
368                 fr = this.wframe;
369                 restartanim = false;
370         }
371         else
372         {
373                 restartanim = fr != WFRAME_IDLE;
374         }
375
376         this.wframe = fr;
377
378         if (this.weapon_think == w_ready && func != w_ready && this.state == WS_RAISE)
379                 backtrace("Tried to override initial weapon think function - should this really happen?");
380
381         t *= W_WeaponRateFactor(actor);
382
383         // VorteX: haste can be added here
384         if (this.weapon_think == w_ready)
385         {
386                 this.weapon_nextthink = time;
387                 // dprint("started firing at ", ftos(time), "\n");
388         }
389         float w_frametime_limit = this.weapon_frametime * 1.5;
390         if (this.weapon_nextthink < time - w_frametime_limit || this.weapon_nextthink > time + w_frametime_limit)
391         {
392                 this.weapon_nextthink = time;
393                 // dprint("reset weapon animation timer at ", ftos(time), "\n");
394         }
395         this.weapon_nextthink += t;
396         this.weapon_think = func;
397         // dprint("next ", ftos(this.weapon_nextthink), "\n");
398
399         if (this)
400         {
401                 FOREACH_CLIENT(true, {
402                         if(it == actor || (IS_SPEC(it) && it.enemy == actor))
403                                 wframe_send(it, this, fr, autocvar_g_weaponratefactor, restartanim);
404                 });
405         }
406
407         if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
408         {
409                 bool primary_melee = boolean(fr == WFRAME_FIRE1 && (this.m_weapon.spawnflags & WEP_TYPE_MELEE_PRI));
410                 bool secondary_melee = boolean(fr == WFRAME_FIRE2 && (this.m_weapon.spawnflags & WEP_TYPE_MELEE_SEC));
411                 int act = (primary_melee || secondary_melee) ? ANIMACTION_MELEE : ANIMACTION_SHOOT;
412                 animdecide_setaction(actor, act, restartanim);
413         }
414         else if (actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
415         {
416                 actor.anim_upper_action = 0;
417         }
418 }
419
420 bool weaponUseForbidden(entity player)
421 {
422         if (round_handler_IsActive() && !round_handler_IsRoundStarted()) return true;
423         if (MUTATOR_CALLHOOK(ForbidWeaponUse, player)) return true;
424         return false;
425 }
426
427 bool weaponLocked(entity player)
428 {
429         if (time < game_starttime && !sv_ready_restart_after_countdown) return true;
430         if (player.player_blocked) return true;
431         if (game_stopped || game_timeout) return true;
432         if (STAT(FROZEN, player)) return true;
433         if (MUTATOR_CALLHOOK(LockWeapon, player)) return true;
434         return false;
435 }
436
437 void W_ResetGunAlign(entity player, int preferred_alignment)
438 {
439         if(W_DualWielding(player))
440                 preferred_alignment = 3; // right align, the second gun will default to left
441
442         // clear current weapon slots' alignments so we can redo the calculations!
443         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
444         {
445                 .entity weaponentity = weaponentities[slot];
446                 if (player.(weaponentity))
447                         player.(weaponentity).m_gunalign = 0;
448         }
449
450         // now set the new values
451         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
452         {
453                 .entity weaponentity = weaponentities[slot];
454                 if (player.(weaponentity))
455                         player.(weaponentity).m_gunalign = W_GunAlign(player.(weaponentity), preferred_alignment);
456         }
457 }
458
459 .bool hook_switchweapon;
460
461 void W_WeaponFrame(Player actor, .entity weaponentity)
462 {
463         TC(Player, actor);
464         TC(PlayerState, PS(actor));
465         entity this = actor.(weaponentity);
466         if (frametime) this.weapon_frametime = frametime;
467
468         if (!this || GetResource(actor, RES_HEALTH) < 1) return;  // Dead player can't use weapons and injure impulse commands
469
470         int button_atck = PHYS_INPUT_BUTTON_ATCK(actor);
471         int button_atck2 = PHYS_INPUT_BUTTON_ATCK2(actor);
472
473         if (weaponUseForbidden(actor))
474                 button_atck = button_atck2 = 0; // forbid primary and secondary fire, switching is allowed
475
476         if (weaponLocked(actor))
477         {
478                 if (this.state != WS_CLEAR)
479                 {
480                         Weapon wpn = this.m_weapon;
481                         w_ready(wpn, actor, weaponentity, button_atck | (button_atck2 << 1));
482                         return;
483                 }
484         }
485
486         if(autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0)
487         {
488                 .entity wepe1 = weaponentities[0];
489                 entity wep1 = actor.(wepe1);
490                 this.m_switchweapon = wep1.m_switchweapon;
491                 entity store = IS_PLAYER(actor) ? PS(actor) : actor;
492                 if(!(this.m_switchweapon.spawnflags & WEP_FLAG_DUALWIELD) && !(store.dual_weapons & wep1.m_switchweapon.m_wepset))
493                 {
494                         this.m_weapon = WEP_Null;
495                         this.m_switchingweapon = WEP_Null;
496                         this.m_switchweapon = WEP_Null;
497                         this.state = WS_CLEAR;
498                         this.weaponname = "";
499                         this.clip_load = this.clip_size = this.old_clip_load = 0;
500                         return;
501                 }
502         }
503
504         if (this.m_switchweapon == WEP_Null)
505         {
506                 if (this.state != WS_CLEAR)
507                         w_ready(this.m_weapon, actor, weaponentity, button_atck | (button_atck2 << 1));
508                 this.m_weapon = WEP_Null;
509                 this.m_switchingweapon = WEP_Null;
510                 this.state = WS_CLEAR;
511                 this.weaponname = "";
512                 this.clip_load = this.clip_size = this.old_clip_load = 0;
513                 return;
514         }
515
516         vector fo, ri, up;
517         MAKE_VECTORS(actor.v_angle, fo, ri, up);
518
519         // Change weapon
520         if (this.m_weapon != this.m_switchweapon)
521         {
522                 switch (this.state)
523                 {
524                         default:
525                                 LOG_WARNF("unhandled weaponentity (%i) state for player (%i): %d", this, actor, this.state);
526                                 break;
527                         case WS_INUSE:
528                         case WS_RAISE:
529                                 break;
530                         case WS_CLEAR:
531                         {
532                                 // end switching!
533                                 Weapon newwep = this.m_switchweapon;
534                                 this.m_switchingweapon = newwep;
535
536                                 // the two weapon entities will notice this has changed and update their models
537                                 this.m_weapon = newwep;
538                                 this.weaponname = newwep.mdl;
539                                 this.bulletcounter = 0;
540                                 actor.activewepid = newwep.m_id; // z411
541                                 newwep.wr_setup(newwep, actor, weaponentity);
542                                 this.state = WS_RAISE;
543
544                                 // set our clip load to the load of the weapon we switched to, if it's reloadable
545                                 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo)  // prevent accessing undefined cvars
546                                 {
547                                         this.clip_load = this.(weapon_load[this.m_switchweapon.m_id]);
548                                         this.clip_size = newwep.reloading_ammo;
549                                 }
550                                 else
551                                 {
552                                         this.clip_load = this.clip_size = 0;
553                                 }
554
555                                 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, newwep.switchdelay_raise, w_ready);
556                                 break;
557                         }
558                         case WS_DROP:
559                         {
560                                 // in dropping phase we can switch at any time
561                                 this.m_switchingweapon = this.m_switchweapon;
562                                 break;
563                         }
564                         case WS_READY:
565                         {
566                                 // start switching!
567                                 this.m_switchingweapon = this.m_switchweapon;
568                                 entity oldwep = this.m_weapon;
569
570                                 // set up weapon switch think in the future, and start drop anim
571                                 if (INDEPENDENT_ATTACK_FINISHED || ATTACK_FINISHED(actor, weaponentity) <= time + this.weapon_frametime * 0.5)
572                                 {
573                                         sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
574                                         this.state = WS_DROP;
575                                         weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_clear);
576                                 }
577                                 break;
578                         }
579                 }
580         }
581
582         // LordHavoc: network timing test code
583         // if (actor.button0)
584         //      print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, weaponentity)), " >= ", ftos(this.weapon_nextthink), "\n");
585
586         Weapon w = this.m_weapon;
587
588         // call the think code which may fire the weapon
589         // and do so multiple times to resolve framerate dependency issues if the
590         // server framerate is very low and the weapon fire rate very high
591         for (int c = 0; c < W_TICSPERFRAME; ++c)
592         {
593                 if (w != WEP_Null && !(STAT(WEAPONS, actor) & WepSet_FromWeapon(w)))
594                 {
595                         if (this.m_weapon == this.m_switchweapon)
596                                 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
597                         w = WEP_Null;
598                 }
599
600                 v_forward = fo;
601                 v_right = ri;
602                 v_up = up;
603
604                 bool block_weapon = false;
605                 {
606                         bool key_pressed = PHYS_INPUT_BUTTON_HOOK(actor) && !actor.vehicle;
607                         if (weaponUseForbidden(actor))
608                                 key_pressed = false;
609
610                         Weapon off = actor.offhand;
611                         if (off && (!(STAT(WEAPONS, actor) & WEPSET(HOOK)) || off != OFFHAND_HOOK))
612                         {
613                                 if (off.offhand_think) off.offhand_think(off, actor, key_pressed);
614                         }
615                         else
616                         {
617                                 if (key_pressed && this.m_switchweapon != WEP_HOOK && !actor.hook_switchweapon)
618                                         W_SwitchWeapon(actor, WEP_HOOK, weaponentity);
619                                 actor.hook_switchweapon = key_pressed;
620                                 Weapon h = WEP_HOOK;
621                                 block_weapon = (this.m_weapon == h && (button_atck || key_pressed));
622                                 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
623                         }
624                 }
625
626                 v_forward = fo;
627                 v_right = ri;
628                 v_up = up;
629
630                 if (!block_weapon)
631                 {
632                         Weapon e = this.m_weapon;
633                         TC(Weapon, e);
634                         if (w != WEP_Null)
635                         {
636                                 e.wr_think(e, actor, weaponentity, button_atck | (button_atck2 << 1));
637                         }
638                         else if (e)
639                         {
640                                 e.wr_gonethink(e, actor, weaponentity);
641                         }
642                 }
643
644                 if (time + this.weapon_frametime * 0.5 >= this.weapon_nextthink)
645                 {
646                         if (this.weapon_think)
647                         {
648                                 v_forward = fo;
649                                 v_right = ri;
650                                 v_up = up;
651                                 Weapon wpn = this.m_weapon;
652                                 this.weapon_think(wpn, actor, weaponentity, button_atck | (button_atck2 << 1));
653                         }
654                         else
655                         {
656                                 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
657                         }
658                 }
659         }
660 }
661
662 void W_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
663 {
664         flash.owner = actor;
665         flash.angles_z = random() * 360;
666
667         entity w_ent = actor.(weaponentity);
668         entity exterior = actor.exteriorweaponentity;
669
670         if (gettagindex(w_ent, "shot")) setattachment(flash, w_ent, "shot");
671         else setattachment(flash, w_ent, "tag_shot");
672         setorigin(flash, offset);
673
674         entity xflash = spawn();
675         copyentity(flash, xflash);
676
677         flash.viewmodelforclient = actor;
678
679         if (w_ent.oldorigin.x > 0)
680         {
681                 setattachment(xflash, exterior, "");
682                 setorigin(xflash, w_ent.oldorigin + offset);
683         }
684         else
685         {
686                 if (gettagindex(exterior, "shot")) setattachment(xflash, exterior, "shot");
687                 else setattachment(xflash, exterior, "tag_shot");
688                 setorigin(xflash, offset);
689         }
690 }
691
692 void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use, .entity weaponentity)
693 {
694         if (MUTATOR_CALLHOOK(W_DecreaseAmmo, actor, actor.(weaponentity), ammo_use)) return;
695         if ((actor.items & IT_UNLIMITED_AMMO) && !wep.reloading_ammo) return;
696
697         ammo_use = M_ARGV(2, float);
698
699         entity w_ent = actor.(weaponentity);
700
701         // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
702         if (wep.reloading_ammo)
703         {
704                 w_ent.clip_load -= ammo_use;
705                 w_ent.(weapon_load[w_ent.m_weapon.m_id]) = w_ent.clip_load;
706         }
707         else if (wep.ammo_type != RES_NONE)
708         {
709                 float ammo = GetResource(actor, wep.ammo_type);
710                 if (ammo < ammo_use)
711                 {
712                         backtrace(sprintf(
713                                 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
714                                 "Please notify the developers immediately with a copy of this backtrace!\n",
715                                 ammo_use, wep.netname, GetAmmoPicture(wep.ammo_type), actor.netname, ammo));
716                 }
717                 SetResource(actor, wep.ammo_type, ammo - ammo_use);
718         }
719 }
720
721 // weapon reloading code
722
723 .float reload_ammo_amount, reload_ammo_min, reload_time;
724 .float reload_complain;
725 .string reload_sound;
726
727 void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
728 {
729         // finish the reloading process, and do the ammo transfer
730
731         entity w_ent = actor.(weaponentity);
732         Weapon wpn = w_ent.m_weapon;
733
734         w_ent.clip_load = w_ent.old_clip_load;  // restore the ammo counter, in case we still had ammo in the weapon before reloading
735
736         // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
737         if (!w_ent.reload_ammo_min || (actor.items & IT_UNLIMITED_AMMO) || wpn.ammo_type == RES_NONE)
738         {
739                 w_ent.clip_load = w_ent.reload_ammo_amount;
740         }
741         else
742         {
743                 // make sure we don't add more ammo than we have
744                 float ammo = GetResource(actor, wpn.ammo_type);
745                 float load = min(w_ent.reload_ammo_amount - w_ent.clip_load, ammo);
746                 w_ent.clip_load += load;
747                 SetResource(actor, wpn.ammo_type, ammo - load);
748         }
749         w_ent.(weapon_load[w_ent.m_weapon.m_id]) = w_ent.clip_load;
750
751         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
752         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
753         // so your weapon is disabled for a few seconds without reason
754
755         // ATTACK_FINISHED(actor, weaponentity) -= w_ent.reload_time - 1;
756
757         w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
758 }
759
760 void W_Reload(entity actor, .entity weaponentity, float sent_ammo_min, Sound sent_sound)
761 {
762         TC(Sound, sent_sound);
763         // set global values to work with
764         entity this = actor.(weaponentity);
765         Weapon e = this.m_weapon;
766
767         if (MUTATOR_CALLHOOK(W_Reload, actor)) return;
768
769         this.reload_ammo_min = sent_ammo_min;
770         this.reload_ammo_amount = e.reloading_ammo;
771         this.reload_time = e.reloading_time;
772         strcpy(actor.reload_sound, Sound_fixpath(sent_sound));
773
774         // don't reload weapons that don't have the RELOADABLE flag
775         if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
776         {
777                 LOG_TRACE(
778                         "Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
779                 return;
780         }
781
782         // return if reloading is disabled for this weapon
783         if (!this.reload_ammo_amount) return;
784
785         // our weapon is fully loaded, no need to reload
786         if (this.clip_load >= this.reload_ammo_amount) return;
787
788         // no ammo, so nothing to load
789         if (e.ammo_type != RES_NONE)
790         {
791                 if (!GetResource(actor, e.ammo_type) && this.reload_ammo_min)
792                 {
793                         if (!(actor.items & IT_UNLIMITED_AMMO))
794                         {
795                                 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0)
796                                         return; // in this case the primary weapon will do the switching when it runs out of ammo (TODO: do this same check but for other slots)
797                                 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
798                                 {
799                                         play2(actor, SND(UNAVAILABLE));
800                                         sprint(actor, strcat("You don't have enough ammo to reload the ^2", this.m_weapon.m_name, "\n"));
801                                         actor.reload_complain = time + 1;
802                                 }
803                                 // switch away if the amount of ammo is not enough to keep using this weapon
804                                 if (!(e.wr_checkammo1(e, actor, weaponentity) + e.wr_checkammo2(e, actor, weaponentity)))
805                                 {
806                                         this.clip_load = -1;  // reload later
807                                         W_SwitchToOtherWeapon(actor, weaponentity);
808                                 }
809                                 return;
810                         }
811                 }
812         }
813         if (this)
814         {
815                 if (this.wframe == WFRAME_RELOAD) return;
816
817                 // allow switching away while reloading, but this will cause a new reload!
818                 this.state = WS_READY;
819         }
820
821         // now begin the reloading process
822
823         _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
824
825         // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
826         // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
827         // so your weapon is disabled for a few seconds without reason
828
829         // ATTACK_FINISHED(actor, weaponentity) = max(time, ATTACK_FINISHED(actor, weaponentity)) + this.reload_time + 1;
830
831         weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, this.reload_time, W_ReloadedAndReady);
832
833         if (this.clip_load < 0) this.clip_load = 0;
834         this.old_clip_load = this.clip_load;
835         this.clip_load = this.(weapon_load[this.m_weapon.m_id]) = -1;
836 }
837
838 void W_DropEvent(.void(Weapon, entity actor, .entity) event, entity player, float weapon_type, entity weapon_item, .entity weaponentity)
839 {
840         Weapon w = REGISTRY_GET(Weapons, weapon_type);
841         weapon_dropevent_item = weapon_item;
842         w.event(w, player, weaponentity);
843 }