6 #include <client/defs.qh>
7 #include "../constants.qh"
9 #include <lib/warpzone/anglestransform.qh>
10 #include <lib/warpzone/common.qh>
11 #include <lib/warpzone/client.qh>
13 #include <client/autocvars.qh>
14 #include "../deathtypes/all.qh"
15 #include <lib/csqcmodel/interpolate.qh>
16 #include "../physics/movetypes/movetypes.qh"
17 #include <client/main.qh>
18 #include <lib/csqcmodel/cl_model.qh>
21 #include <common/items/_mod.qh>
22 #include <lib/warpzone/anglestransform.qh>
23 #include <lib/warpzone/common.qh>
24 #include <lib/warpzone/util_server.qh>
25 #include <lib/warpzone/server.qh>
26 #include "../constants.qh"
27 #include "../stats.qh"
28 #include "../teams.qh"
29 #include <common/util.qh>
30 #include "../monsters/_mod.qh"
32 #include <server/weapons/common.qh>
33 #include <server/weapons/csqcprojectile.qh>
34 #include <server/weapons/tracing.qh>
35 #include <server/items/spawning.qh>
36 #include <server/autocvars.qh>
37 #include <server/constants.qh>
38 #include <server/defs.qh>
39 #include "../notifications/all.qh"
40 #include "../deathtypes/all.qh"
41 #include <server/mutators/_mod.qh>
42 #include "../mapinfo.qh"
43 #include <server/command/_mod.qh>
44 #include <lib/csqcmodel/sv_model.qh>
45 #include <server/portals.qh>
46 #include <server/g_hook.qh>
49 #include "calculations.qc"
55 #include "weapon/_mod.inc"
58 // WEAPON PLUGIN SYSTEM
60 WepSet _WepSet_FromWeapon(int a)
63 if (REGISTRY_MAX(Weapons) > 24)
67 if (REGISTRY_MAX(Weapons) > 48)
71 return '0 0 1' * (2 ** a);
73 return '0 1 0' * (2 ** a);
75 return '1 0 0' * (2 ** a);
78 void WriteWepSet(float dst, WepSet w)
80 if (REGISTRY_MAX(Weapons) > 48) WriteInt72_t(dst, w);
81 else if (REGISTRY_MAX(Weapons) > 24) WriteInt48_t(dst, w);
82 else WriteInt24_t(dst, w.x);
86 WepSet WepSet_GetFromStat()
90 WepSet WepSet_GetFromStat_InMap()
92 return STAT(WEAPONSINMAP);
96 if (REGISTRY_MAX(Weapons) > 48) return ReadInt72_t();
97 if (REGISTRY_MAX(Weapons) > 24) return ReadInt48_t();
98 return ReadInt24_t() * '1 0 0';
102 string W_FixWeaponOrder(string order, float complete)
104 return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
106 string W_NameWeaponOrder_MapFunc(string s)
111 entity wi = REGISTRY_GET(Weapons, i);
112 if (wi != WEP_Null) return wi.netname;
117 string W_UndeprecateName(string s)
121 case "nex": return "vortex";
122 case "rocketlauncher": return "devastator";
123 case "laser": return "blaster";
124 case "minstanex": return "vaporizer";
125 case "grenadelauncher": return "mortar";
126 case "uzi": return "machinegun";
127 case "hmg": return "okhmg";
128 case "rpc": return "okrpc";
132 string W_NameWeaponOrder(string order)
134 return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
136 string W_NumberWeaponOrder_MapFunc(string s)
138 if (s == "0" || stof(s)) return s;
139 s = W_UndeprecateName(s);
140 FOREACH(Weapons, it != WEP_Null && it.netname == s, return ftos(i));
143 string W_NumberWeaponOrder(string order)
145 return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
148 float W_FixWeaponOrder_BuildImpulseList_buf[REGISTRY_MAX(Weapons)];
149 string W_FixWeaponOrder_BuildImpulseList_order;
150 void W_FixWeaponOrder_BuildImpulseList_swap(int i, int j, entity pass)
153 h = W_FixWeaponOrder_BuildImpulseList_buf[i];
154 W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
155 W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
157 float W_FixWeaponOrder_BuildImpulseList_cmp(int i, int j, entity pass)
159 int si = W_FixWeaponOrder_BuildImpulseList_buf[i];
160 Weapon e1 = REGISTRY_GET(Weapons, si);
161 int sj = W_FixWeaponOrder_BuildImpulseList_buf[j];
162 Weapon e2 = REGISTRY_GET(Weapons, sj);
163 int d = (e1.impulse + 9) % 10 - (e2.impulse + 9) % 10;
164 if (d != 0) return -d; // high impulse first!
165 return strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
166 sprintf(" %d ", si), 0)
168 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
169 sprintf(" %d ", sj), 0)
170 ; // low char index first!
172 string W_FixWeaponOrder_BuildImpulseList(string o)
175 W_FixWeaponOrder_BuildImpulseList_order = o;
176 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
177 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
178 heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp,
181 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
182 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
183 W_FixWeaponOrder_BuildImpulseList_order = string_null;
184 return substring(o, 1, -1);
187 string W_FixWeaponOrder_AllowIncomplete(entity this, string order)
189 return W_FixWeaponOrder(order, 0);
192 string W_FixWeaponOrder_ForceComplete(string order)
194 if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
195 return W_FixWeaponOrder(order, 1);
198 WepSet W_RandomWeapons(entity e, WepSet remaining, int n)
200 WepSet result = '0 0 0';
201 for (int j = 0; j < n; ++j)
203 RandomSelection_Init();
204 FOREACH(Weapons, it != WEP_Null, {
205 if (remaining & (it.m_wepset))
206 RandomSelection_AddEnt(it, 1, 1);
208 Weapon w = RandomSelection_chosen_ent;
209 result |= WepSet_FromWeapon(w);
210 remaining &= ~WepSet_FromWeapon(w);
215 string GetAmmoPicture(int ammotype)
219 case RES_SHELLS: return ITEM_Shells.m_icon;
220 case RES_BULLETS: return ITEM_Bullets.m_icon;
221 case RES_ROCKETS: return ITEM_Rockets.m_icon;
222 case RES_CELLS: return ITEM_Cells.m_icon;
223 case RES_PLASMA: return ITEM_Plasma.m_icon;
224 case RES_FUEL: return ITEM_JetpackFuel.m_icon;
225 default: return ""; // wtf, no ammo type?
229 string GetAmmoName(int ammotype)
233 case RES_SHELLS: return ITEM_Shells.m_name;
234 case RES_BULLETS: return ITEM_Bullets.m_name;
235 case RES_ROCKETS: return ITEM_Rockets.m_name;
236 case RES_CELLS: return ITEM_Cells.m_name;
237 case RES_PLASMA: return ITEM_Plasma.m_name;
238 case RES_FUEL: return ITEM_JetpackFuel.m_name;
239 default: return "batteries";
244 int GetAmmoTypeFromNum(int i)
248 case 0: return RES_SHELLS;
249 case 1: return RES_BULLETS;
250 case 2: return RES_ROCKETS;
251 case 3: return RES_CELLS;
252 case 4: return RES_PLASMA;
253 case 5: return RES_FUEL;
254 default: return RES_NONE;
258 int GetAmmoStat(int ammotype)
262 case RES_SHELLS: return STAT_SHELLS;
263 case RES_BULLETS: return STAT_NAILS;
264 case RES_ROCKETS: return STAT_ROCKETS;
265 case RES_CELLS: return STAT_CELLS;
266 case RES_PLASMA: return STAT_PLASMA.m_id;
267 case RES_FUEL: return STAT_FUEL.m_id;
273 string W_Sound(string w_snd)
275 string output = strcat("weapons/", w_snd);
276 MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
277 return M_ARGV(1, string);
280 string W_Model(string w_mdl)
282 string output = strcat("models/weapons/", w_mdl);
283 MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
284 return M_ARGV(1, string);
288 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
310 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
315 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
317 else if (STAT(SHOOTFROMEYE))
321 else if (STAT(SHOOTFROMCENTER))
326 else if ((s = G_SHOOTFROMFIXEDORIGIN) != "")
329 if (y_is_right) v.y = -v.y;
330 if (v.x != 0) vecs.x = v.x;
334 else // just do the same as top
336 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
342 #define shotorg_adjust shotorg_adjust_values
347 * 1. simple animated model, muzzle flash handling on h_ model:
348 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
350 * shot = muzzle end (shot origin, also used for muzzle flashes)
351 * shell = casings ejection point (must be on the right hand side of the gun)
352 * weapon = attachment for v_tuba.md3
353 * v_tuba.md3 - first and third person model
354 * g_tuba.md3 - pickup model
356 * 2. simple animated model, muzzle flash handling on v_ model:
357 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
359 * weapon = attachment for v_tuba.md3
360 * v_tuba.md3 - first and third person model
362 * shot = muzzle end (shot origin, also used for muzzle flashes)
363 * shell = casings ejection point (must be on the right hand side of the gun)
364 * g_tuba.md3 - pickup model
366 * 3. fully animated model, muzzle flash handling on h_ model:
367 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
369 * shot = muzzle end (shot origin, also used for muzzle flashes)
370 * shell = casings ejection point (must be on the right hand side of the gun)
371 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
372 * v_tuba.md3 - third person model
373 * g_tuba.md3 - pickup model
375 * 4. fully animated model, muzzle flash handling on v_ model:
376 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
378 * shot = muzzle end (shot origin)
379 * shell = casings ejection point (must be on the right hand side of the gun)
380 * v_tuba.md3 - third person model
382 * shot = muzzle end (for muzzle flashes)
383 * g_tuba.md3 - pickup model
386 * this.origin, this.angles
388 * this.movedir, this.view_ofs, this.movedir_aligned
395 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim)
399 vector oldmin = this.mins, oldmax = this.maxs;
400 setmodel(this, MDL_Null);
401 setsize(this, oldmin, oldmax);
402 if (this.weaponchild) delete(this.weaponchild);
403 this.weaponchild = NULL;
404 this.movedir = '0 0 0';
405 this.spawnorigin = '0 0 0';
406 this.oldorigin = '0 0 0';
407 this.anim_fire1 = '0 1 0.01';
408 this.anim_fire2 = '0 1 0.01';
409 this.anim_idle = '0 1 0.01';
410 this.anim_reload = '0 1 0.01';
414 // if there is a child entity, hide it until we're sure we use it
415 if (this.weaponchild) this.weaponchild.model = "";
416 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
417 int v_shot_idx; // used later
418 (v_shot_idx = gettagindex(this, "shot")) || (v_shot_idx = gettagindex(this, "tag_shot"));
420 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
421 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
422 this.anim_fire1 = animfixfps(this, '0 1 0.01', '0 0 0');
423 this.anim_fire2 = animfixfps(this, '1 1 0.01', '0 0 0');
424 this.anim_idle = animfixfps(this, '2 1 0.01', '0 0 0');
425 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
427 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
428 // if we don't, this is a "real" animated model
430 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
432 if (!this.weaponchild)
434 this.weaponchild = new(weaponchild);
436 this.weaponchild.drawmask = MASK_NORMAL;
437 this.weaponchild.renderflags |= RF_VIEWMODEL;
440 _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
441 setsize(this.weaponchild, '0 0 0', '0 0 0');
442 setattachment(this.weaponchild, this, t);
446 if (this.weaponchild) delete(this.weaponchild);
447 this.weaponchild = NULL;
450 setsize(this, '0 0 0', '0 0 0');
451 setorigin(this, '0 0 0');
452 this.angles = '0 0 0';
455 this.viewmodelforclient = NULL;
457 this.renderflags &= ~RF_VIEWMODEL;
459 if (v_shot_idx) // v_ model attached to invisible h_ model
461 this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
466 if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
468 this.movedir = gettaginfo(this, idx);
472 LOG_WARNF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong",
474 this.movedir = '0 0 0';
479 // v_ model attached to invisible h_ model
481 && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
483 this.spawnorigin = gettaginfo(this.weaponchild, idx);
485 else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
487 this.spawnorigin = gettaginfo(this, idx);
491 LOG_WARNF("weapon model %s does not support the 'shell' tag, will display casings wrong",
493 this.spawnorigin = this.movedir;
498 this.oldorigin = '0 0 0'; // use regular attachment
503 if (this.weaponchild)
504 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
506 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
509 this.oldorigin = this.movedir - gettaginfo(this, idx);
514 "weapon model %s does not support the 'handle' tag "
515 "and neither does the v_ model support the 'shot' tag, "
516 "will display muzzle flashes TOTALLY wrong\n",
518 this.oldorigin = '0 0 0'; // there is no way to recover from this
523 this.viewmodelforclient = this.owner;
525 this.renderflags |= RF_VIEWMODEL;
529 this.view_ofs = '0 0 0';
530 this.movedir_aligned = this.movedir;
532 if (this.movedir.x >= 0)
534 //int algn = STAT(GUNALIGN, this.owner);
535 int algn = W_GunAlign(this, STAT(GUNALIGN, this.owner));
537 this.m_gunalign = algn;
539 vector v = this.movedir;
540 this.movedir = shotorg_adjust(v, false, false, algn);
541 this.movedir_aligned = shotorg_adjust(v, false, true, algn);
542 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
544 int compressed_shotorg = compressShotOrigin(this.movedir);
545 // make them match perfectly
548 if (this.owner) STAT(SHOTORG, this.owner) = compressed_shotorg;
549 this.movedir = decompressShotOrigin(compressed_shotorg);
551 this.movedir = decompressShotOrigin(compressed_shotorg);
554 this.spawnorigin += this.view_ofs; // offset the casings origin by the same amount
556 // check if an instant weapon switch occurred
557 setorigin(this, this.view_ofs);
559 // reset animstate now
560 this.wframe = WFRAME_IDLE;
561 setanim(this, this.anim_idle, true, false, true);
567 REGISTER_NET_TEMP(wframe)
569 NET_HANDLE(wframe, bool isNew)
572 float t = ReadFloat();
573 int slot = ReadByte();
574 bool restartanim = ReadByte();
575 entity wepent = viewmodels[slot];
576 if(fr == WFRAME_IDLE)
577 wepent.animstate_looping = false; // we don't need to enforce idle animation
584 case WFRAME_IDLE: a = wepent.anim_idle; break;
585 case WFRAME_FIRE1: a = wepent.anim_fire1; break;
586 case WFRAME_FIRE2: a = wepent.anim_fire2; break;
587 case WFRAME_RELOAD: a = wepent.anim_reload; break;
590 anim_set(wepent, a, !restartanim, restartanim, restartanim);
592 wepent.state = ReadByte();
593 wepent.weapon_nextthink = ReadFloat();
594 switch (wepent.state)
597 wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_raise;
600 wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_drop;
603 wepent.weapon_switchdelay = 0;
611 void wframe_send(entity actor, entity weaponentity, int wepframe, float attackrate, bool restartanim)
613 if (!IS_REAL_CLIENT(actor)) return;
614 int channel = MSG_ONE;
616 WriteHeader(channel, wframe);
617 WriteByte(channel, wepframe);
618 WriteFloat(channel, attackrate);
619 WriteByte(channel, weaponslot(weaponentity.weaponentity_fld));
620 WriteByte(channel, restartanim);
621 WriteByte(channel, weaponentity.state);
622 WriteFloat(channel, weaponentity.weapon_nextthink);
626 REGISTER_NET_C2S(w_whereis)
628 void Weapon_whereis(Weapon this, entity cl);
629 NET_HANDLE(w_whereis, bool)
631 Weapon wpn = ReadRegistered(Weapons);
632 if (wpn != WEP_Null) Weapon_whereis(wpn, sender);
636 void w_whereis(Weapon this)
638 int channel = MSG_C2S;
639 WriteHeader(channel, w_whereis);
640 WriteRegistered(Weapons, channel, this);
642 CLIENT_COMMAND(weapon_find, "Show spawn locations of a weapon")
646 case CMD_REQUEST_COMMAND:
651 FOREACH(Weapons, it != WEP_Null, w_whereis(it));
656 FOREACH(Weapons, it != WEP_Null && !(STAT(WEAPONS) & it.m_wepset), w_whereis(it));
659 FOREACH(Weapons, it != WEP_Null && it.netname == s,
666 LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
667 case CMD_REQUEST_USAGE:
669 LOG_HELP("Usage:^3 cl_cmd weapon_find weapon");
670 LOG_HELP(" Where 'weapon' is the lowercase weapon name, 'all' or 'unowned'.");
678 void W_MuzzleFlash_Model_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
681 flash.angles_z = random() * 360;
683 entity view = actor.(weaponentity);
684 entity exterior = actor.exteriorweaponentity;
686 if (view.oldorigin.x > 0)
688 setattachment(flash, exterior, "");
689 setorigin(flash, view.oldorigin + offset);
693 if (gettagindex(exterior, "shot")) setattachment(flash, exterior, "shot");
694 else setattachment(flash, exterior, "tag_shot");
695 setorigin(flash, offset);
699 void W_MuzzleFlash_Model_AttachToShotorg(entity wepent, entity flash, vector offset)
701 flash.owner = wepent;
702 flash.angles_z = random() * 360;
704 if (gettagindex(wepent, "shot")) setattachment(flash, wepent, "shot");
705 else setattachment(flash, wepent, "tag_shot");
706 setorigin(flash, offset);
710 void W_MuzzleFlash_Model_Think(entity this)
715 this.nextthink = time + 0.05;
719 setthink(this, SUB_Remove);
720 this.nextthink = time;
721 this.realowner.muzzle_flash = NULL;
726 void W_MuzzleFlash_Model(entity wepent, entity muzzlemodel)
728 if(wepent.muzzle_flash == NULL)
729 wepent.muzzle_flash = spawn();
731 entity flash = wepent.muzzle_flash;
732 setmodel(flash, muzzlemodel); // precision set below
735 setthink(flash, W_MuzzleFlash_Model_Think);
736 flash.nextthink = time + 0.02;
739 flash.angles_z = random() * 180;
740 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT;
741 flash.owner = flash.realowner = wepent;
744 flash.drawmask = MASK_NORMAL;
748 REGISTER_NET_TEMP(w_muzzleflash)
751 void W_MuzzleFlash(Weapon thiswep, entity actor, .entity weaponentity, vector shotorg, vector shotdir)
753 // don't show an exterior muzzle effect for the off-hand
754 if(weaponslot(weaponentity) == 0)
756 Send_Effect_Except(thiswep.m_muzzleeffect, shotorg, shotdir * 1000, 1, actor);
758 if(thiswep.m_muzzlemodel != MDL_Null)
760 W_MuzzleFlash_Model(actor.exteriorweaponentity, thiswep.m_muzzlemodel);
761 W_MuzzleFlash_Model_AttachToShotorg(actor, weaponentity, actor.exteriorweaponentity.muzzle_flash, '5 0 0');
765 FOREACH_CLIENT(it == actor || (IS_SPEC(it) && it.enemy == actor),
767 if(!IS_REAL_CLIENT(it))
769 int channel = MSG_ONE;
771 WriteHeader(channel, w_muzzleflash);
772 WriteByte(channel, thiswep.m_id);
773 WriteByte(channel, weaponslot(weaponentity));
774 WriteVector(channel, shotorg);
778 NET_HANDLE(w_muzzleflash, bool isNew)
781 int weapon_id = ReadByte();
782 int slot = ReadByte();
783 vector sv_shotorg = ReadVector();
785 Weapon thiswep = REGISTRY_GET(Weapons, weapon_id);
786 vector viewangles = getpropertyvec(VF_CL_VIEWANGLES);
787 vector forward, right, up;
788 MAKE_VECTORS(viewangles, forward, right, up);
790 if(autocvar_chase_active)
792 // in third person mode, show the muzzle flash from the server side weapon position
793 // we don't have a view model to reference in this case
794 pointparticles(thiswep.m_muzzleeffect, sv_shotorg, forward * 1000, 1);
797 if(!autocvar_r_drawviewmodel) return;
799 entity wepent = viewmodels[slot];
800 // get the local player entity to calculate shot origin
801 entity rlplayer = CSQCModel_server2csqc(player_localentnum - 1);
803 rlplayer = csqcplayer; // fall back to the global
805 vector md = wepent.movedir_aligned;
806 vector vecs = ((md.x > 0) ? md : '0 0 0');
807 vector dv = forward * vecs.x + right * -vecs.y + up * vecs.z;
808 vector org = rlplayer.origin + rlplayer.view_ofs + dv;
810 pointparticles(thiswep.m_muzzleeffect, org, forward * 1000, 1);
812 if(thiswep.m_muzzlemodel != MDL_Null)
814 W_MuzzleFlash_Model(wepent, thiswep.m_muzzlemodel);
815 W_MuzzleFlash_Model_AttachToShotorg(wepent, wepent.muzzle_flash, '5 0 0');