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