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