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