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