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