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