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