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