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