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