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