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