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