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