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