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