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