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