Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
weaponsystem.qc
Go to the documentation of this file.
1#include "weaponsystem.qh"
2
4#include <common/constants.qh>
12#include <common/state.qh>
13#include <common/util.qh>
16#include <common/wepent.qh>
18#include <server/cheats.qh>
19#include <server/client.qh>
21#include <server/damage.qh>
22#include <server/items/items.qh>
23#include <server/hook.qh>
27#include <server/world.qh>
28
29.int state;
30
32
34{
35 float t = (autocvar_g_weaponratefactor > 0)
37 : 1;
38
39 MUTATOR_CALLHOOK(WeaponRateFactor, t, this);
40 t = M_ARGV(0, float);
41
42 return t;
43}
44
46{
48
49 MUTATOR_CALLHOOK(WeaponSpeedFactor, t, this);
50 t = M_ARGV(0, float);
51
52 return t;
53}
54
55
57{
58 this.viewmodelforclient = this.owner;
59 if (IS_SPEC(client) && client.enemy == this.owner)
60 this.viewmodelforclient = client;
61 return false;
62}
63
65{
66 entity wi = REGISTRY_GET(Weapons, wpn);
67 entity e = spawn();
68 CL_WeaponEntity_SetModel(e, wi.mdl, false);
69 vector ret = e.movedir;
70 CL_WeaponEntity_SetModel(e, "", false);
71 delete(e);
72 return ret;
73}
74
75.float m_alpha;
77.int w_dmg;
79
81{
82 this.nextthink = time;
83 if (game_stopped)
84 this.frame = this.anim_idle.x;
85 .entity weaponentity = this.weaponentity_fld;
86 if (this.owner.(weaponentity) != this)
87 {
88 // owner has new gun; remove old one
89 if (this.weaponchild)
90 delete(this.weaponchild);
91 if (this.hook)
92 delete(this.hook);
93 delete(this);
94 return;
95 }
96 if (IS_DEAD(this.owner))
97 {
98 // owner died; disappear
99 this.model = "";
100 if (this.weaponchild)
101 this.weaponchild.model = "";
102 return;
103 }
104 if (this.w_weaponname != this.weaponname || this.w_dmg != this.modelindex || this.w_deadflag != this.deadflag)
105 {
106 // owner changed weapons; update appearance
107 this.w_weaponname = this.weaponname;
108 this.w_dmg = this.modelindex;
109 this.w_deadflag = this.deadflag;
110
111 CL_WeaponEntity_SetModel(this, this.weaponname, true);
112 }
113
114 this.alpha = -1; // TODO: don't render this entity at all
115
116 if (this.owner.alpha == default_player_alpha)
118 else if (this.owner.alpha != 0)
119 this.m_alpha = this.owner.alpha;
120 else
121 this.m_alpha = 1;
122
123 if (this.weaponchild)
124 {
125 this.weaponchild.alpha = this.alpha;
126 this.weaponchild.effects = this.effects;
127 }
128}
129
131{
132 this.nextthink = time;
133 .entity weaponentity = this.weaponentity_fld;
134 entity w_ent = this.owner.(weaponentity);
135 if (this.owner.exteriorweaponentity != this)
136 {
137 delete(this);
138 return;
139 }
140 if (IS_DEAD(this.owner))
141 {
142 this.model = "";
143 return;
144 }
145 if (this.weaponname != w_ent.weaponname || this.dmg != w_ent.modelindex || this.deadflag != w_ent.deadflag)
146 {
147 this.weaponname = w_ent.weaponname;
148 this.dmg = w_ent.modelindex;
149 this.deadflag = w_ent.deadflag;
150 if (w_ent.weaponname != "")
151 {
152 _setmodel(this, W_Model(strcat("v_", w_ent.weaponname, ".md3")));
153 setsize(this, '0 0 0', '0 0 0');
154 }
155 else
156 this.model = "";
157
158 int tag_found = gettagindex(this.owner, "tag_weapon");
159 if (tag_found)
160 {
161 this.tag_index = tag_found;
162 this.tag_entity = this.owner;
163 }
164 else
165 setattachment(this, this.owner, "bip01 r hand");
166 }
167 this.effects = this.owner.effects;
168 this.effects |= EF_LOWPRECISION;
169 this.effects &= EFMASK_CHEAP; // eat performance
170 if (this.owner.alpha == default_player_alpha)
172 else if (this.owner.alpha != 0)
173 this.alpha = this.owner.alpha;
174 else
175 this.alpha = 1;
176
177 Weapon wep = this.owner.(weaponentity).m_weapon;
178 if (wep)
179 this.glowmod = weaponentity_glowmod(wep, this.owner.clientcolors, this.owner.(weaponentity));
180 this.colormap = this.owner.colormap;
181 this.skin = w_ent.skin;
182
184}
185
187void CL_SpawnWeaponentity(entity actor, .entity weaponentity)
188{
189 entity w_ent = actor.(weaponentity) = new(weaponentity);
190 w_ent.solid = SOLID_NOT;
191 w_ent.owner = actor;
192 setmodel(w_ent, MDL_Null); // precision set when changed
193 setorigin(w_ent, '0 0 0');
194 w_ent.weaponentity_fld = weaponentity;
196 w_ent.nextthink = time;
197 w_ent.viewmodelforclient = actor;
198 w_ent.draggable = drag_undraggable;
200
201 wepent_link(w_ent);
202
203 if (weaponentity == weaponentities[0]) // only one exterior model, thank you very much
204 {
205 entity exterior = actor.exteriorweaponentity = new(exteriorweaponentity);
206 exterior.solid = SOLID_NOT;
207 exterior.owner = actor;
208 exterior.draggable = drag_undraggable;
209 exterior.weaponentity_fld = weaponentity;
210 setorigin(exterior, '0 0 0');
212 exterior.nextthink = time;
213
214 CSQCMODEL_AUTOINIT(exterior);
215 }
216}
217
218// Weapon subs
219void w_enddrop(Weapon thiswep, entity actor, .entity weaponentity, int fire)
220{
221 entity w_ent = actor.(weaponentity);
222 if (w_ent)
223 {
224 w_ent.m_weapon = WEP_Null;
225 //w_ent.m_switchingweapon = WEP_Null; // let this field be set the next weapon frame
226 w_ent.state = WS_CLEAR;
227 w_ent.effects = 0;
228 }
229}
230
231void w_ready(Weapon thiswep, entity actor, .entity weaponentity, int fire)
232{
233 entity w_ent = actor.(weaponentity);
234 if (w_ent)
235 w_ent.state = WS_READY;
236 weapon_thinkf(actor, weaponentity, WFRAME_IDLE, 1000000, w_ready);
237}
238
241bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary, .entity weaponentity)
242{
243 if (actor.items & IT_UNLIMITED_AMMO)
244 return true;
245 bool ammo = (secondary)
246 ? thiswep.wr_checkammo2(thiswep, actor, weaponentity)
247 : thiswep.wr_checkammo1(thiswep, actor, weaponentity);
248 if (ammo)
249 return true;
250 // always keep the Mine Layer if we placed mines, so that we can detonate them
251 if (thiswep == WEP_MINE_LAYER)
252 IL_EACH(g_mines, it.owner == actor && it.weaponentity_fld == weaponentity, return false);
253
254 if (thiswep == WEP_SHOTGUN
255 && !secondary && WEP_CVAR(WEP_SHOTGUN, secondary) == 1)
256 return false; // no clicking, just allow
257
258 if (thiswep == actor.(weaponentity).m_switchweapon && time - actor.prevdryfire > 1) // only play once BEFORE starting to switch weapons
259 {
260 sound(actor, CH_WEAPON_A, SND_DRYFIRE, VOL_BASE, ATTEN_NORM);
261 actor.prevdryfire = time;
262 }
263
264 // check if the other firing mode has enough ammo
265 bool ammo_other = (secondary)
266 ? thiswep.wr_checkammo1(thiswep, actor, weaponentity)
267 : thiswep.wr_checkammo2(thiswep, actor, weaponentity);
268 if (ammo_other)
269 {
270 if (time - actor.prevwarntime > 1)
271 Send_Notification(NOTIF_ONE, actor, MSG_MULTI, ITEM_WEAPON_PRIMORSEC, thiswep.m_id, secondary, (1 - secondary));
272 actor.prevwarntime = time;
273 }
274 else // this weapon is totally unable to fire, switch to another one
275 W_SwitchToOtherWeapon(actor, weaponentity);
276
277 return false;
278}
279
281bool weapon_prepareattack_check(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
282{
283 if (actor.weaponentity == NULL)
284 return true;
285 if (!weapon_prepareattack_checkammo(thiswep, actor, secondary, weaponentity))
286 return false;
287
288 // if sv_ready_restart_after_countdown is set, don't allow the player to shoot
289 // if all players readied up and the countdown is running
290 if (time < game_starttime || time < actor.race_penalty)
291 return false;
292
293 if (timeout_status == TIMEOUT_ACTIVE) // don't allow the player to shoot while game is paused
294 return false;
295
296 // do not even think about shooting if switching
297 if (actor.(weaponentity).m_switchweapon != actor.(weaponentity).m_weapon)
298 return false;
299
300 if (attacktime >= 0)
301 {
302 // don't fire if previous attack is not finished
303 if (ATTACK_FINISHED(actor, weaponentity) > time + actor.(weaponentity).weapon_frametime * 0.5)
304 return false;
305 entity this = actor.(weaponentity);
306 // don't fire while changing weapon
307 if (!actor.vehicle && this.state != WS_READY)
308 return false;
309 }
310 return true;
311}
312
313void weapon_prepareattack_do(entity actor, .entity weaponentity, bool secondary, float attacktime)
314{
315 entity this = actor.(weaponentity);
316 if (this == NULL)
317 return;
318 this.state = WS_INUSE;
319
320 if (StatusEffects_active(STATUSEFFECT_SpawnShield, actor)) // given this is performed often, perform a lighter check first
321 StatusEffects_remove(STATUSEFFECT_SpawnShield, actor, STATUSEFFECT_REMOVE_CLEAR); // kill spawn shield when you fire
322
323 // if the weapon hasn't been firing continuously, reset the timer
324 if (attacktime >= 0)
325 {
326 if (ATTACK_FINISHED(actor, weaponentity) < time - this.weapon_frametime * 1.5)
327 {
328 ATTACK_FINISHED(actor, weaponentity) = time;
329 // dprint("resetting attack finished to ", ftos(time), "\n");
330 }
331 float arate = W_WeaponRateFactor(actor);
332 ATTACK_FINISHED(actor, weaponentity) = ATTACK_FINISHED(actor, weaponentity) + attacktime * arate;
333
335 {
336 int slot = weaponslot(weaponentity);
337 for (int wepslot = 0; wepslot < MAX_WEAPONSLOTS; ++wepslot)
338 {
339 if (slot == wepslot)
340 continue;
341 .entity wepent = weaponentities[wepslot];
342 if (actor.(wepent) && actor.(wepent).m_weapon != WEP_Null)
343 {
344 if (ATTACK_FINISHED(actor, wepent) > time + actor.(wepent).weapon_frametime * 0.5)
345 continue; // still cooling down!
346 if (ATTACK_FINISHED(actor, wepent) < time - actor.(wepent).weapon_frametime * 1.5)
347 ATTACK_FINISHED(actor, wepent) = time;
348 ATTACK_FINISHED(actor, wepent) = ATTACK_FINISHED(actor, wepent) + (attacktime * arate) / MAX_WEAPONSLOTS;
349 }
350 }
351 }
352 }
353 ++this.bulletcounter;
354 // dprint("attack finished ", ftos(ATTACK_FINISHED(actor, weaponentity)), "\n");
355}
356
357bool weapon_prepareattack(Weapon thiswep, entity actor, .entity weaponentity, bool secondary, float attacktime)
358{
359 if (weapon_prepareattack_check(thiswep, actor, weaponentity, secondary, attacktime))
360 {
361 weapon_prepareattack_do(actor, weaponentity, secondary, attacktime);
362 return true;
363 }
364 return false;
365}
366
371void weapon_thinkf(entity actor, .entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,
372 .entity weaponentity, int fire) func)
373{
374 entity this = actor.(weaponentity);
375 if (this == NULL)
376 return;
377 bool restartanim;
378 if (fr == WFRAME_DONTCHANGE)
379 {
380 // this can happen when the weapon entity is newly spawned, since it has a clear state and no previous weapon frame
381 if (this.wframe == WFRAME_DONTCHANGE)
382 this.wframe = WFRAME_IDLE;
383 fr = this.wframe;
384 restartanim = false;
385 }
386 else
387 restartanim = fr != WFRAME_IDLE;
388
389 this.wframe = fr;
390
391 if (this.weapon_think == w_ready && func != w_ready && this.state == WS_RAISE)
392 backtrace("Tried to override initial weapon think function - should this really happen?");
393
394 t *= W_WeaponRateFactor(actor);
395
396 // VorteX: haste can be added here
397 if (this.weapon_think == w_ready)
398 {
399 this.weapon_nextthink = time;
400 // dprint("started firing at ", ftos(time), "\n");
401 }
402 float w_frametime_limit = this.weapon_frametime * 1.5;
404 {
405 this.weapon_nextthink = time;
406 // dprint("reset weapon animation timer at ", ftos(time), "\n");
407 }
408 this.weapon_nextthink += t;
409 this.weapon_think = func;
410 // dprint("next ", ftos(this.weapon_nextthink), "\n");
411
412 if (this)
413 FOREACH_CLIENT(it == actor || (IS_SPEC(it) && it.enemy == actor), wframe_send(it, this, fr, autocvar_g_weaponratefactor, restartanim));
414
415 if ((fr == WFRAME_FIRE1 || fr == WFRAME_FIRE2) && t)
416 {
417 bool primary_melee = boolean(fr == WFRAME_FIRE1 && (this.m_weapon.spawnflags & WEP_TYPE_MELEE_PRI));
418 bool secondary_melee = boolean(fr == WFRAME_FIRE2 && (this.m_weapon.spawnflags & WEP_TYPE_MELEE_SEC));
419 int act = (primary_melee || secondary_melee) ? ANIMACTION_MELEE : ANIMACTION_SHOOT;
420 animdecide_setaction(actor, act, restartanim);
421 }
422 else if (actor.anim_upper_action == ANIMACTION_SHOOT || actor.anim_upper_action == ANIMACTION_MELEE)
423 actor.anim_upper_action = 0;
424}
425
427{
429 return true;
430 if (MUTATOR_CALLHOOK(ForbidWeaponUse, player))
431 return true;
432 return false;
433}
434
436{
438 return true;
439 if (player.player_blocked || StatusEffects_active(STATUSEFFECT_Frozen, player))
440 return true;
441 if (MUTATOR_CALLHOOK(LockWeapon, player))
442 return true;
443 return false;
444}
445
446void W_ResetGunAlign(entity player, int preferred_alignment)
447{
448 if (W_DualWielding(player))
449 preferred_alignment = 3; // right align, the second gun will default to left
450
451 // clear current weapon slots' alignments so we can redo the calculations!
452 for (int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
453 {
454 .entity weaponentity = weaponentities[slot];
455 if (player.(weaponentity))
456 player.(weaponentity).m_gunalign = 0;
457 }
458
459 // now set the new values
460 for (int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
461 {
462 .entity weaponentity = weaponentities[slot];
463 if (player.(weaponentity))
464 player.(weaponentity).m_gunalign = W_GunAlign(player.(weaponentity), preferred_alignment);
465 }
466}
467
469
470void W_WeaponFrame(Player actor, .entity weaponentity)
471{
472 TC(Player, actor);
473 TC(PlayerState, PS(actor));
474 entity this = actor.(weaponentity);
475 if (frametime)
477
478 if (!this || GetResource(actor, RES_HEALTH) < 1)
479 return; // Dead player can't use weapons and injure impulse commands
480
481 int button_atck = PHYS_INPUT_BUTTON_ATCK(actor);
482 int button_atck2 = PHYS_INPUT_BUTTON_ATCK2(actor);
483
484 if (weaponUseForbidden(actor))
485 button_atck = button_atck2 = 0; // forbid primary and secondary fire, switching is allowed
486
487 if (weaponLocked(actor)
488 && this.state != WS_CLEAR)
489 {
490 Weapon wpn = this.m_weapon;
491 w_ready(wpn, actor, weaponentity, button_atck | (button_atck2 << 1));
492 return;
493 }
494
495 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0)
496 {
497 .entity wepe1 = weaponentities[0];
498 entity wep1 = actor.(wepe1);
499 this.m_switchweapon = wep1.m_switchweapon;
500 entity store = IS_PLAYER(actor) ? PS(actor) : actor;
501 if (!(this.m_switchweapon.spawnflags & WEP_FLAG_DUALWIELD) && !(store.dual_weapons & wep1.m_switchweapon.m_wepset))
502 {
503 this.m_weapon = WEP_Null;
504 this.m_switchingweapon = WEP_Null;
505 this.m_switchweapon = WEP_Null;
506 this.state = WS_CLEAR;
507 this.weaponname = "";
508 this.clip_load = this.clip_size = this.old_clip_load = 0;
509 return;
510 }
511 }
512
513 if (this.m_switchweapon == WEP_Null)
514 {
515 if (this.state != WS_CLEAR)
516 w_ready(this.m_weapon, actor, weaponentity, button_atck | (button_atck2 << 1));
517 this.m_weapon = WEP_Null;
518 this.m_switchingweapon = WEP_Null;
519 this.state = WS_CLEAR;
520 this.weaponname = "";
521 this.clip_load = this.clip_size = this.old_clip_load = 0;
522 return;
523 }
524
525 vector fo, ri, up;
526 MAKE_VECTORS(actor.v_angle, fo, ri, up);
527
528 // Change weapon
529 if (this.m_weapon != this.m_switchweapon)
530 {
531 switch (this.state)
532 {
533 default:
534 LOG_WARNF("unhandled weaponentity (%i) state for player (%i): %d", this, actor, this.state);
535 break;
536 case WS_INUSE:
537 case WS_RAISE:
538 break;
539 case WS_CLEAR:
540 {
541 // end switching!
542 Weapon newwep = this.m_switchweapon;
543 this.m_switchingweapon = newwep;
544
545 // the two weapon entities will notice this has changed and update their models
546 this.m_weapon = newwep;
547 this.weaponname = newwep.mdl;
548 this.bulletcounter = 0;
549 newwep.wr_setup(newwep, actor, weaponentity);
550 this.state = WS_RAISE;
551
552 // set our clip load to the load of the weapon we switched to, if it's reloadable
553 if ((newwep.spawnflags & WEP_FLAG_RELOADABLE) && newwep.reloading_ammo) // prevent accessing undefined cvars
554 {
555 this.clip_load = this.(weapon_load[this.m_switchweapon.m_id]);
556 this.clip_size = newwep.reloading_ammo;
557 }
558 else
559 this.clip_load = this.clip_size = 0;
560
561 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, newwep.switchdelay_raise, w_ready);
562 break;
563 }
564 case WS_DROP:
565 // in dropping phase we can switch at any time
567 break;
568 case WS_READY:
569 {
570 // start switching!
572 entity oldwep = this.m_weapon;
573
574 // set up weapon switch think in the future, and start drop anim
575 if (INDEPENDENT_ATTACK_FINISHED || ATTACK_FINISHED(actor, weaponentity) <= time + this.weapon_frametime * 0.5)
576 {
577 sound(actor, CH_WEAPON_SINGLE, SND_WEAPON_SWITCH, VOL_BASE, ATTN_NORM);
578 this.state = WS_DROP;
579 weapon_thinkf(actor, weaponentity, WFRAME_DONTCHANGE, oldwep.switchdelay_drop, w_enddrop);
580 this.weapon_nextthink -= this.weapon_frametime; // because it will use one extra frame before the raise begins
581 }
582 break;
583 }
584 }
585 }
586
587 // LordHavoc: network timing test code
588 // if (actor.button0)
589 // print(ftos(frametime), " ", ftos(time), " >= ", ftos(ATTACK_FINISHED(actor, weaponentity)), " >= ", ftos(this.weapon_nextthink), "\n");
590
591 Weapon w = this.m_weapon;
592
593 // call the think code which may fire the weapon
594 // and do so multiple times to resolve framerate dependency issues if the
595 // server framerate is very low and the weapon fire rate very high
596 for (int c = 0; c < W_TICSPERFRAME; ++c)
597 {
598 if (w != WEP_Null && !(STAT(WEAPONS, actor) & WepSet_FromWeapon(w)))
599 {
600 if (this.m_weapon == this.m_switchweapon)
601 W_SwitchWeapon_Force(actor, w_getbestweapon(actor, weaponentity), weaponentity);
602 w = WEP_Null;
603 }
604
605 v_forward = fo;
606 v_right = ri;
607 v_up = up;
608
609 bool block_weapon = false;
610 {
611 bool key_pressed = (PHYS_INPUT_BUTTON_HOOK(actor) && !actor.vehicle);
612 if (weaponUseForbidden(actor))
613 key_pressed = false;
614
615 Weapon off = actor.offhand;
616 if (off && (!(STAT(WEAPONS, actor) & WEPSET(HOOK)) || off != OFFHAND_HOOK))
617 {
618 if (off.offhand_think)
619 off.offhand_think(off, actor, key_pressed);
620 }
621 else
622 {
623 if (key_pressed && this.m_switchweapon != WEP_HOOK && !actor.hook_switchweapon)
624 W_SwitchWeapon(actor, WEP_HOOK, weaponentity);
625 actor.hook_switchweapon = key_pressed;
626 Weapon h = WEP_HOOK;
627 block_weapon = (this.m_weapon == h && (button_atck || key_pressed));
628 h.wr_think(h, actor, weaponentity, block_weapon ? 1 : 0);
629 }
630 }
631
632 v_forward = fo;
633 v_right = ri;
634 v_up = up;
635
636 if (!block_weapon)
637 {
638 Weapon e = this.m_weapon;
639 TC(Weapon, e);
640 if (w != WEP_Null)
641 e.wr_think(e, actor, weaponentity, button_atck | (button_atck2 << 1));
642 else if (e)
643 e.wr_gonethink(e, actor, weaponentity);
644 }
645
646 if (time + this.weapon_frametime * 0.5 >= this.weapon_nextthink)
647 {
648 if (this.weapon_think)
649 {
650 v_forward = fo;
651 v_right = ri;
652 v_up = up;
653 Weapon wpn = this.m_weapon;
654 this.weapon_think(wpn, actor, weaponentity, button_atck | (button_atck2 << 1));
655 }
656 else
657 bprint("\{1}^1ERROR: undefined weapon think function for ", actor.netname, "\n");
658 }
659 }
660}
661
662void W_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
663{
664 flash.owner = actor;
665 flash.angles_z = random() * 360;
666
667 entity w_ent = actor.(weaponentity);
668 entity exterior = actor.exteriorweaponentity;
669
670 setattachment(flash, w_ent, (gettagindex(w_ent, "shot") ? "shot" : "tag_shot"));
671 setorigin(flash, offset);
672
673 entity xflash = spawn();
674 copyentity_qc(flash, xflash);
675
676 flash.viewmodelforclient = actor;
677
678 if (w_ent.oldorigin.x > 0)
679 {
680 setattachment(xflash, exterior, "");
681 setorigin(xflash, w_ent.oldorigin + offset);
682 }
683 else
684 {
685 setattachment(xflash, exterior, (gettagindex(exterior, "shot") ? "shot" : "tag_shot"));
686 setorigin(xflash, offset);
687 }
688}
689
690void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use, .entity weaponentity)
691{
692 if (MUTATOR_CALLHOOK(W_DecreaseAmmo, actor, actor.(weaponentity), ammo_use))
693 return;
694 if ((actor.items & IT_UNLIMITED_AMMO) && !wep.reloading_ammo)
695 return;
696
697 ammo_use = M_ARGV(2, float);
698
699 entity w_ent = actor.(weaponentity);
700
701 // if this weapon is reloadable, decrease its load. Else decrease the player's ammo
702 if (wep.reloading_ammo)
703 {
704 w_ent.clip_load -= ammo_use;
705 w_ent.(weapon_load[w_ent.m_weapon.m_id]) = w_ent.clip_load;
706 }
707 else if (wep.ammo_type != RES_NONE)
708 {
709 float ammo = GetResource(actor, wep.ammo_type);
710 if (ammo < ammo_use)
711 backtrace(sprintf(
712 "W_DecreaseAmmo(%.2f): '%s' subtracted too much %s from '%s', resulting with '%.2f' left... "
713 "Please notify the developers immediately with a copy of this backtrace!\n",
714 ammo_use, wep.netname, wep.ammo_type.netname, actor.netname, ammo));
715
716 SetResource(actor, wep.ammo_type, ammo - ammo_use);
717 }
718}
719
720// weapon reloading code
721
725
727void W_ReloadedAndReady(Weapon thiswep, entity actor, .entity weaponentity, int fire)
728{
729 entity w_ent = actor.(weaponentity);
730 Weapon wpn = w_ent.m_weapon;
731
732 w_ent.clip_load = w_ent.old_clip_load; // restore the ammo counter, in case we still had ammo in the weapon before reloading
733
734 // if the gun uses no ammo, max out weapon load, else decrease ammo as we increase weapon load
735 if (!w_ent.reload_ammo_min || (actor.items & IT_UNLIMITED_AMMO) || wpn.ammo_type == RES_NONE)
736 w_ent.clip_load = w_ent.reload_ammo_amount;
737 else
738 {
739 // make sure we don't add more ammo than we have
740 float ammo = GetResource(actor, wpn.ammo_type);
741 float load = min(w_ent.reload_ammo_amount - w_ent.clip_load, ammo);
742 w_ent.clip_load += load;
743 SetResource(actor, wpn.ammo_type, ammo - load);
744 }
745 w_ent.(weapon_load[w_ent.m_weapon.m_id]) = w_ent.clip_load;
746
747 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
748 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
749 // so your weapon is disabled for a few seconds without reason
750
751 // ATTACK_FINISHED(actor, weaponentity) -= w_ent.reload_time - 1;
752
753 w_ready(wpn, actor, weaponentity, PHYS_INPUT_BUTTON_ATCK(actor) | (PHYS_INPUT_BUTTON_ATCK2(actor) << 1));
754}
755
756void W_Reload(entity actor, .entity weaponentity, float sent_ammo_min, Sound sent_sound)
757{
758 TC(Sound, sent_sound);
759 // set global values to work with
760 entity this = actor.(weaponentity);
761 Weapon e = this.m_weapon;
762
763 if (MUTATOR_CALLHOOK(W_Reload, actor))
764 return;
765
766 this.reload_ammo_min = sent_ammo_min;
767 this.reload_ammo_amount = e.reloading_ammo;
768 this.reload_time = e.reloading_time;
769 strcpy(actor.reload_sound, Sound_fixpath(sent_sound));
770
771 // don't reload weapons that don't have the RELOADABLE flag
772 if (!(e.spawnflags & WEP_FLAG_RELOADABLE))
773 {
774 LOG_TRACE("Warning: Attempted to reload a weapon that does not have the WEP_FLAG_RELOADABLE flag. Fix your code!\n");
775 return;
776 }
777
778 // return if reloading is disabled for this weapon
779 if (!this.reload_ammo_amount)
780 return;
781
782 // our weapon is fully loaded, no need to reload
783 if (this.clip_load >= this.reload_ammo_amount)
784 return;
785
786 // no ammo, so nothing to load
787 if (e.ammo_type != RES_NONE
788 && !GetResource(actor, e.ammo_type) && this.reload_ammo_min
789 && !(actor.items & IT_UNLIMITED_AMMO))
790 {
791 if (autocvar_g_weaponswitch_debug == 2 && weaponslot(weaponentity) > 0)
792 return; // in this case the primary weapon will do the switching when it runs out of ammo (TODO: do this same check but for other slots)
793 if (IS_REAL_CLIENT(actor) && actor.reload_complain < time)
794 {
795 play2(actor, SND(UNAVAILABLE));
796 sprint(actor, strcat("You don't have enough ammo to reload the ^2", this.m_weapon.m_name, "\n"));
797 actor.reload_complain = time + 1;
798 }
799 // switch away if the amount of ammo is not enough to keep using this weapon
800 if (!(e.wr_checkammo1(e, actor, weaponentity) + e.wr_checkammo2(e, actor, weaponentity)))
801 {
802 this.clip_load = -1; // reload later
803 W_SwitchToOtherWeapon(actor, weaponentity);
804 }
805 return;
806 }
807 if (this)
808 {
809 if (this.wframe == WFRAME_RELOAD)
810 return;
811
812 // allow switching away while reloading, but this will cause a new reload!
813 this.state = WS_READY;
814 }
815
816 // now begin the reloading process
817
818 _sound(actor, CH_WEAPON_SINGLE, actor.reload_sound, VOL_BASE, ATTEN_NORM);
819
820 // do not set ATTACK_FINISHED in reload code any more. This causes annoying delays if eg: You start reloading a weapon,
821 // then quickly switch to another weapon and back. Reloading is canceled, but the reload delay is still there,
822 // so your weapon is disabled for a few seconds without reason
823
824 // ATTACK_FINISHED(actor, weaponentity) = max(time, ATTACK_FINISHED(actor, weaponentity)) + this.reload_time + 1;
825
826 weapon_thinkf(actor, weaponentity, WFRAME_RELOAD, this.reload_time, W_ReloadedAndReady);
827
828 if (this.clip_load < 0)
829 this.clip_load = 0;
830 this.old_clip_load = this.clip_load;
831 this.clip_load = this.(weapon_load[this.m_weapon.m_id]) = -1;
832}
833
834void W_DropEvent(.void(Weapon, entity actor, .entity) event, entity player, int weapon_type, entity weapon_item, .entity weaponentity)
835{
836 Weapon w = REGISTRY_GET(Weapons, weapon_type);
837 weapon_dropevent_item = weapon_item;
838 w.event(w, player, weaponentity);
839}
float frame
primary framegroup animation (strength = 1 - lerpfrac - lerpfrac3 - lerpfrac4)
Definition anim.qh:6
void animdecide_setaction(entity e, float action, float restart)
const int ANIMACTION_MELEE
const int ANIMACTION_SHOOT
#define MUTATOR_CALLHOOK(id,...)
Definition base.qh:143
#define boolean(value)
Definition bool.qh:9
float dmg
Definition breakable.qc:12
int W_GunAlign(entity this, int preferred_align)
bool drag_undraggable(entity draggee, entity dragger)
Definition cheats.qc:901
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
void SetResource(entity e, Resource res_type, float amount)
Sets the current amount of resource the given entity will have.
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
string netname
Client name.
Definition client.qh:91
vector v_angle
Definition client.qh:145
Purpose: common player state, usable on client and server Client: singleton representing the viewed p...
Definition state.qh:8
string netname
Definition resources.qh:27
fields which are explicitly/manually set are marked with "M", fields set automatically are marked wit...
Definition weapon.qh:37
virtual void wr_setup()
(SERVER) setup weapon data
Definition weapon.qh:91
virtual void wr_think()
(SERVER) logic to run every frame
Definition weapon.qh:93
Resource ammo_type
M: ammotype : main ammo type.
Definition weapon.qh:51
string mdl
M: modelname : name of model (without g_ v_ or h_ prefixes).
Definition weapon.qh:60
int m_id
Definition weapon.qh:38
virtual void wr_checkammo2()
(SERVER) checks ammo for weapon second
Definition weapon.qh:100
string netname
M: refname : reference name name.
Definition weapon.qh:79
int spawnflags
M: flags : WEPSPAWNFLAG_... combined.
Definition weapon.qh:55
virtual void wr_checkammo1()
(SERVER) checks ammo for weapon primary
Definition weapon.qh:95
virtual void wr_gonethink()
(SERVER) logic to run when weapon is lost
Definition weapon.qh:131
float alpha
Definition items.qc:13
entity owner
Definition main.qh:87
const int IT_UNLIMITED_AMMO
Definition item.qh:23
#define setmodel(this, m)
Definition model.qh:26
#define M_ARGV(x, type)
Definition events.qh:17
float race_penalty
Definition player.qh:59
entity hook
Definition player.qh:238
#define IS_DEAD(s)
Definition player.qh:244
#define PHYS_INPUT_BUTTON_HOOK(s)
Definition player.qh:157
#define IS_PLAYER(s)
Definition player.qh:242
#define PHYS_INPUT_BUTTON_ATCK(s)
Definition player.qh:152
#define PHYS_INPUT_BUTTON_ATCK2(s)
Definition player.qh:154
int timeout_status
Definition stats.qh:87
float game_starttime
Definition stats.qh:82
float game_stopped
Definition stats.qh:81
OffhandHook OFFHAND_HOOK
Definition hook.qh:83
#define EFMASK_CHEAP
Definition constants.qh:101
vector v_up
float modelindex
float frametime
const float SOLID_NOT
float effects
float time
vector v_right
float nextthink
float colormap
vector v_forward
const float ATTN_NORM
#define spawn
entity tag_entity
vector glowmod
int tag_index
#define CSQCMODEL_AUTOUPDATE(e)
float m_alpha
#define CSQCMODEL_AUTOINIT(e)
void W_SwitchWeapon_Force(Player this, Weapon w,.entity weaponentity)
Definition selection.qc:237
int state
#define MAKE_VECTORS(angles, forward, right, up)
Same as the makevectors builtin but uses the provided locals instead of the v_* globals.
entity viewmodelforclient
float EF_LOWPRECISION
#define gettagindex
model
Definition ent_cs.qc:164
skin
Definition ent_cs.qc:168
Weapons
Definition guide.qh:113
#define IL_EACH(this, cond, body)
#define TC(T, sym)
Definition _all.inc:82
#define STAT(...)
Definition stats.qh:94
#define LOG_WARNF(...)
Definition log.qh:59
#define LOG_TRACE(...)
Definition log.qh:74
#define backtrace(msg)
Definition log.qh:96
void sprint(float clientnum, string text,...)
float random(void)
void bprint(string text,...)
float min(float f,...)
IntrusiveList g_mines
Definition minelayer.qh:87
string W_Model(string w_mdl)
Definition all.qc:233
@ STATUSEFFECT_REMOVE_CLEAR
Effect is being forcibly removed without calling any additional mechanics.
Definition all.qh:30
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
void Send_Notification(NOTIF broadcast, entity client, MSG net_type, Notification net_name,...count)
Definition all.qc:1500
void copyentity_qc(entity src, entity dst)
Definition oo.qh:88
#define NULL
Definition post.qh:14
float deadflag
Definition progsdefs.qc:149
#define REGISTRY_GET(id, i)
Definition registry.qh:62
#define round_handler_IsActive()
#define round_handler_IsRoundStarted()
bool W_SwitchWeapon(entity this, Weapon w,.entity weaponentity)
Definition selection.qc:265
void W_SwitchToOtherWeapon(entity this,.entity weaponentity)
Perform weapon to attack (weaponstate and attack_finished check is here).
Definition selection.qc:247
#define w_getbestweapon(ent, wepent)
Definition selection.qh:23
bool autocvar_g_weaponswitch_debug
Definition selection.qh:7
bool autocvar_g_weaponswitch_debug_alternate
Definition selection.qh:8
#define setthink(e, f)
vector
Definition self.qh:96
#define setcefc(e, f)
const float TIMEOUT_ACTIVE
Definition common.qh:49
bool W_DualWielding(entity player)
Definition common.qc:20
const int CH_WEAPON_SINGLE
Definition sound.qh:9
#define Sound_fixpath(this)
Definition sound.qh:141
const float VOL_BASE
Definition sound.qh:36
const int CH_WEAPON_A
Definition sound.qh:7
#define _sound(e, c, s, v, a)
Definition sound.qh:43
const float ATTEN_NORM
Definition sound.qh:30
#define sound(e, c, s, v, a)
Definition sound.qh:52
void play2(entity e, string filename)
Definition all.qc:116
#define SND(id)
Definition all.qh:35
#define PS(this)
Definition state.qh:18
void StatusEffects_remove(StatusEffect this, entity actor, int removal_type)
bool StatusEffects_active(StatusEffect this, entity actor)
#define strcpy(this, s)
Definition string.qh:51
Header file that describes the resource system.
float ammo
Definition sv_turrets.qh:43
#define IS_SPEC(v)
Definition utils.qh:10
#define IS_REAL_CLIENT(v)
Definition utils.qh:17
#define FOREACH_CLIENT(cond, body)
Definition utils.qh:52
float weapon_nextthink
Definition view.qc:288
void wframe_send(entity actor, entity weaponentity, int wepframe, float attackrate, bool restartanim)
Definition all.qc:577
void CL_WeaponEntity_SetModel(entity this, string name, bool _anim)
supported formats:
Definition all.qc:341
int m_gunalign
Definition all.qh:412
vector anim_idle
Definition all.qh:426
#define WEPSET(id)
Definition all.qh:47
vector weaponentity_glowmod(Weapon wep, int c, entity wepent)
Definition all.qh:404
string weaponname
Definition all.qh:415
entity weaponchild
Definition all.qh:402
entity exteriorweaponentity
Definition all.qh:403
WFRAME wframe
Definition all.qh:441
#define WEP_CVAR(wep, name)
Definition all.qh:339
#define WepSet_FromWeapon(it)
Definition all.qh:48
const int MAX_WEAPONSLOTS
Definition weapon.qh:16
const int WEP_FLAG_RELOADABLE
can has reload
Definition weapon.qh:254
const int WS_READY
idle frame
Definition weapon.qh:33
const int WEP_TYPE_MELEE_PRI
primary attack is melee swing (for animation)
Definition weapon.qh:257
entity weaponentities[MAX_WEAPONSLOTS]
Definition weapon.qh:17
const int WS_CLEAR
no weapon selected
Definition weapon.qh:29
const int WS_RAISE
raise frame
Definition weapon.qh:30
const int WS_INUSE
fire state
Definition weapon.qh:32
const int WEP_FLAG_DUALWIELD
weapon can be dual wielded
Definition weapon.qh:259
const int WEP_TYPE_MELEE_SEC
secondary attack is melee swing (for animation)
Definition weapon.qh:258
int weaponslot(.entity weaponentity)
Definition weapon.qh:19
const int WS_DROP
deselecting frame
Definition weapon.qh:31
float prevwarntime
void W_DecreaseAmmo(Weapon wep, entity actor, float ammo_use,.entity weaponentity)
float W_WeaponSpeedFactor(entity this)
float reload_complain
void W_Reload(entity actor,.entity weaponentity, float sent_ammo_min, Sound sent_sound)
void weapon_thinkf(entity actor,.entity weaponentity, WFRAME fr, float t, void(Weapon thiswep, entity actor,.entity weaponentity, int fire) func)
bool weapon_prepareattack(Weapon thiswep, entity actor,.entity weaponentity, bool secondary, float attacktime)
void CL_Weaponentity_Think(entity this)
int w_deadflag
bool weaponUseForbidden(entity player)
vector CL_Weapon_GetShotOrg(int wpn)
void w_ready(Weapon thiswep, entity actor,.entity weaponentity, int fire)
string reload_sound
float weapon_frametime
void W_AttachToShotorg(entity actor,.entity weaponentity, entity flash, vector offset)
void weapon_prepareattack_do(entity actor,.entity weaponentity, bool secondary, float attacktime)
float prevdryfire
void W_DropEvent(.void(Weapon, entity actor,.entity) event, entity player, int weapon_type, entity weapon_item,.entity weaponentity)
float reload_ammo_amount
void W_WeaponFrame(Player actor,.entity weaponentity)
void W_ReloadedAndReady(Weapon thiswep, entity actor,.entity weaponentity, int fire)
Finish the reloading process, and do the ammo transfer.
float W_WeaponRateFactor(entity this)
float reload_ammo_min
void CL_SpawnWeaponentity(entity actor,.entity weaponentity)
Spawn weaponentity for client.
bool weapon_prepareattack_check(Weapon thiswep, entity actor,.entity weaponentity, bool secondary, float attacktime)
float reload_time
string w_weaponname
bool weapon_prepareattack_checkammo(Weapon thiswep, entity actor, bool secondary,.entity weaponentity)
bool weaponLocked(entity player)
void w_enddrop(Weapon thiswep, entity actor,.entity weaponentity, int fire)
int w_dmg
void CL_ExteriorWeaponentity_Think(entity this)
void W_ResetGunAlign(entity player, int preferred_alignment)
bool CL_Weaponentity_CustomizeEntityForClient(entity this, entity client)
bool hook_switchweapon
entity weapon_dropevent_item
float autocvar_g_weaponspeedfactor
int old_clip_load
#define ATTACK_FINISHED(ent, w)
float bulletcounter
entity weaponentity_fld
const int W_TICSPERFRAME
float autocvar_g_weaponratefactor
float weapon_load[REGISTRY_MAX(Weapons)]
#define INDEPENDENT_ATTACK_FINISHED
void wepent_link(entity wep)
Definition wepent.qc:159
Weapon m_weapon
Definition wepent.qh:26
Weapon m_switchweapon
Definition wepent.qh:25
int clip_load
Definition wepent.qh:14
Weapon m_switchingweapon
Definition wepent.qh:27
int clip_size
Definition wepent.qh:15
float default_weapon_alpha
Definition world.qh:73
float default_player_alpha
Definition world.qh:72
bool sv_ready_restart_after_countdown
Definition world.qh:115