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