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