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