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