6 #include <client/main.qh>
7 #include <common/constants.qh>
8 #include <common/deathtypes/all.qh>
9 #include <common/physics/movetypes/movetypes.qh>
10 #include <common/stats.qh>
11 #include <common/util.qh>
12 #include <common/weapons/calculations.qc>
13 #include <common/weapons/weapon/_mod.inc>
14 #include <lib/csqcmodel/cl_model.qh>
15 #include <lib/csqcmodel/interpolate.qh>
16 #include <lib/warpzone/anglestransform.qh>
17 #include <lib/warpzone/client.qh>
18 #include <lib/warpzone/common.qh>
20 #include <common/weapons/weapon/_mod.inc>
22 #include <common/constants.qh>
23 #include <common/deathtypes/all.qh>
24 #include <common/items/_mod.qh>
25 #include <common/mapinfo.qh>
26 #include <common/monsters/_mod.qh>
27 #include <common/notifications/all.qh>
28 #include <common/stats.qh>
29 #include <common/teams.qh>
30 #include <common/util.qh>
31 #include <common/weapons/calculations.qc>
32 #include <common/weapons/config.qc>
33 #include <common/weapons/config.qh>
34 #include <common/weapons/weapon/_mod.inc>
35 #include <lib/csqcmodel/sv_model.qh>
36 #include <lib/warpzone/anglestransform.qh>
37 #include <lib/warpzone/common.qh>
38 #include <lib/warpzone/server.qh>
39 #include <lib/warpzone/util_server.qh>
40 #include <server/autocvars.qh>
41 #include <server/command/_mod.qh>
42 #include <server/hook.qh>
43 #include <server/items/spawning.qh>
44 #include <server/mutators/_mod.qh>
45 #include <server/portals.qh>
46 #include <server/weapons/common.qh>
47 #include <server/weapons/csqcprojectile.qh>
48 #include <server/weapons/tracing.qh>
52 // WEAPON PLUGIN SYSTEM
54 WepSet _WepSet_FromWeapon(int a)
57 if (REGISTRY_MAX(Weapons) > 24)
61 if (REGISTRY_MAX(Weapons) > 48)
65 return '0 0 1' * (2 ** a);
67 return '0 1 0' * (2 ** a);
69 return '1 0 0' * (2 ** a);
72 void WriteWepSet(float dst, WepSet w)
74 if (REGISTRY_MAX(Weapons) > 48) WriteInt72_t(dst, w);
75 else if (REGISTRY_MAX(Weapons) > 24) WriteInt48_t(dst, w);
76 else WriteInt24_t(dst, w.x);
80 WepSet WepSet_GetFromStat()
84 WepSet WepSet_GetFromStat_InMap()
86 return STAT(WEAPONSINMAP);
90 if (REGISTRY_MAX(Weapons) > 48) return ReadInt72_t();
91 if (REGISTRY_MAX(Weapons) > 24) return ReadInt48_t();
92 return ReadInt24_t() * '1 0 0';
96 string W_FixWeaponOrder(string order, float complete)
98 return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
100 string W_NameWeaponOrder_MapFunc(string s)
105 entity wi = REGISTRY_GET(Weapons, i);
106 if (wi != WEP_Null) return wi.netname;
111 string W_UndeprecateName(string s)
115 case "nex": return "vortex";
116 case "rocketlauncher": return "devastator";
117 case "laser": return "blaster";
118 case "minstanex": return "vaporizer";
119 case "grenadelauncher": return "mortar";
120 case "uzi": return "machinegun";
121 case "hmg": return "okhmg";
122 case "rpc": return "okrpc";
126 string W_NameWeaponOrder(string order)
128 return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
130 string W_NumberWeaponOrder_MapFunc(string s)
132 if (s == "0" || stof(s)) return s;
133 s = W_UndeprecateName(s);
134 FOREACH(Weapons, it != WEP_Null && it.netname == s, return ftos(i));
137 string W_NumberWeaponOrder(string order)
139 return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
142 float W_FixWeaponOrder_BuildImpulseList_buf[REGISTRY_MAX(Weapons)];
143 string W_FixWeaponOrder_BuildImpulseList_order;
144 void W_FixWeaponOrder_BuildImpulseList_swap(int i, int j, entity pass)
147 h = W_FixWeaponOrder_BuildImpulseList_buf[i];
148 W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
149 W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
151 float W_FixWeaponOrder_BuildImpulseList_cmp(int i, int j, entity pass)
153 int si = W_FixWeaponOrder_BuildImpulseList_buf[i];
154 Weapon e1 = REGISTRY_GET(Weapons, si);
155 int sj = W_FixWeaponOrder_BuildImpulseList_buf[j];
156 Weapon e2 = REGISTRY_GET(Weapons, sj);
157 int d = (e1.impulse + 9) % 10 - (e2.impulse + 9) % 10;
158 if (d != 0) return -d; // high impulse first!
159 string s = strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " ");
160 return strstrofs(s, sprintf(" %d ", si), 0)
161 - strstrofs(s, sprintf(" %d ", sj), 0); // low char index first!
163 string W_FixWeaponOrder_BuildImpulseList(string o)
166 W_FixWeaponOrder_BuildImpulseList_order = o;
167 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
168 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
169 heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp,
172 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
173 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
174 W_FixWeaponOrder_BuildImpulseList_order = string_null;
175 return substring(o, 1, -1);
178 string W_FixWeaponOrder_AllowIncomplete(entity this, string order)
180 return W_FixWeaponOrder(order, 0);
183 string W_FixWeaponOrder_ForceComplete(string order)
185 if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
186 return W_FixWeaponOrder(order, 1);
189 WepSet W_RandomWeapons(entity e, WepSet remaining, int n)
191 WepSet result = '0 0 0';
192 for (int j = 0; j < n; ++j)
194 RandomSelection_Init();
195 FOREACH(Weapons, it != WEP_Null, {
196 if (remaining & (it.m_wepset))
197 RandomSelection_AddEnt(it, 1, 1);
199 Weapon w = RandomSelection_chosen_ent;
200 result |= WepSet_FromWeapon(w);
201 remaining &= ~WepSet_FromWeapon(w);
206 string GetAmmoPicture(int ammotype)
210 case RES_SHELLS: return ITEM_Shells.m_icon;
211 case RES_BULLETS: return ITEM_Bullets.m_icon;
212 case RES_ROCKETS: return ITEM_Rockets.m_icon;
213 case RES_CELLS: return ITEM_Cells.m_icon;
214 case RES_PLASMA: return ITEM_Plasma.m_icon;
215 case RES_FUEL: return ITEM_JetpackFuel.m_icon;
216 default: return ""; // wtf, no ammo type?
220 string GetAmmoName(int ammotype)
224 case RES_SHELLS: return ITEM_Shells.m_name;
225 case RES_BULLETS: return ITEM_Bullets.m_name;
226 case RES_ROCKETS: return ITEM_Rockets.m_name;
227 case RES_CELLS: return ITEM_Cells.m_name;
228 case RES_PLASMA: return ITEM_Plasma.m_name;
229 case RES_FUEL: return ITEM_JetpackFuel.m_name;
230 default: return "batteries";
235 int GetAmmoTypeFromNum(int i)
239 case 0: return RES_SHELLS;
240 case 1: return RES_BULLETS;
241 case 2: return RES_ROCKETS;
242 case 3: return RES_CELLS;
243 case 4: return RES_PLASMA;
244 case 5: return RES_FUEL;
245 default: return RES_NONE;
249 int GetAmmoStat(int ammotype)
253 case RES_SHELLS: return STAT_SHELLS;
254 case RES_BULLETS: return STAT_NAILS;
255 case RES_ROCKETS: return STAT_ROCKETS;
256 case RES_CELLS: return STAT_CELLS;
257 case RES_PLASMA: return STAT_PLASMA.m_id;
258 case RES_FUEL: return STAT_FUEL.m_id;
264 string W_Sound(string w_snd)
266 string output = strcat("weapons/", w_snd);
267 MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
268 return M_ARGV(1, string);
271 string W_Model(string w_mdl)
273 string output = strcat("models/weapons/", w_mdl);
274 MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
275 return M_ARGV(1, string);
279 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
301 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
306 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
308 else if (STAT(SHOOTFROMEYE))
312 else if (STAT(SHOOTFROMCENTER))
317 else if ((s = G_SHOOTFROMFIXEDORIGIN) != "")
320 if (y_is_right) v.y = -v.y;
321 if (v.x != 0) vecs.x = v.x;
325 else // just do the same as top
327 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
333 #define shotorg_adjust shotorg_adjust_values
338 * 1. simple animated model, muzzle flash handling on h_ model:
339 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
341 * shot = muzzle end (shot origin, also used for muzzle flashes)
342 * shell = casings ejection point (must be on the right hand side of the gun)
343 * weapon = attachment for v_tuba.md3
344 * v_tuba.md3 - first and third person model
345 * g_tuba.md3 - pickup model
347 * 2. simple animated model, muzzle flash handling on v_ model:
348 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
350 * weapon = attachment for v_tuba.md3
351 * v_tuba.md3 - first and third person model
353 * shot = muzzle end (shot origin, also used for muzzle flashes)
354 * shell = casings ejection point (must be on the right hand side of the gun)
355 * g_tuba.md3 - pickup model
357 * 3. fully animated model, muzzle flash handling on h_ model:
358 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
360 * shot = muzzle end (shot origin, also used for muzzle flashes)
361 * shell = casings ejection point (must be on the right hand side of the gun)
362 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
363 * v_tuba.md3 - third person model
364 * g_tuba.md3 - pickup model
366 * 4. fully animated model, muzzle flash handling on v_ model:
367 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
369 * shot = muzzle end (shot origin)
370 * shell = casings ejection point (must be on the right hand side of the gun)
371 * v_tuba.md3 - third person model
373 * shot = muzzle end (for muzzle flashes)
374 * g_tuba.md3 - pickup model
377 * this.origin, this.angles
379 * this.movedir, this.view_ofs, this.movedir_aligned
386 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim)
390 vector oldmin = this.mins, oldmax = this.maxs;
391 setmodel(this, MDL_Null);
392 setsize(this, oldmin, oldmax);
393 if (this.weaponchild) delete(this.weaponchild);
394 this.weaponchild = NULL;
395 this.movedir = '0 0 0';
396 this.spawnorigin = '0 0 0';
397 this.oldorigin = '0 0 0';
398 this.anim_fire1 = '0 1 0.01';
399 this.anim_fire2 = '0 1 0.01';
400 this.anim_idle = '0 1 0.01';
401 this.anim_reload = '0 1 0.01';
405 // if there is a child entity, hide it until we're sure we use it
406 if (this.weaponchild) this.weaponchild.model = "";
407 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
408 int v_shot_idx; // used later
409 (v_shot_idx = gettagindex(this, "shot")) || (v_shot_idx = gettagindex(this, "tag_shot"));
411 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
412 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
413 this.anim_fire1 = animfixfps(this, '0 1 0.01', '0 0 0');
414 this.anim_fire2 = animfixfps(this, '1 1 0.01', '0 0 0');
415 this.anim_idle = animfixfps(this, '2 1 0.01', '0 0 0');
416 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
418 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
419 // if we don't, this is a "real" animated model
421 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
423 if (!this.weaponchild)
425 this.weaponchild = new(weaponchild);
427 this.weaponchild.drawmask = MASK_NORMAL;
428 this.weaponchild.renderflags |= RF_VIEWMODEL;
431 _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
432 setsize(this.weaponchild, '0 0 0', '0 0 0');
433 setattachment(this.weaponchild, this, t);
437 if (this.weaponchild) delete(this.weaponchild);
438 this.weaponchild = NULL;
441 setsize(this, '0 0 0', '0 0 0');
442 setorigin(this, '0 0 0');
443 this.angles = '0 0 0';
446 this.viewmodelforclient = NULL;
448 this.renderflags &= ~RF_VIEWMODEL;
450 if (v_shot_idx) // v_ model attached to invisible h_ model
452 this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
457 if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
459 this.movedir = gettaginfo(this, idx);
463 LOG_WARNF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong",
465 this.movedir = '0 0 0';
470 // v_ model attached to invisible h_ model
472 && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
474 this.spawnorigin = gettaginfo(this.weaponchild, idx);
476 else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
478 this.spawnorigin = gettaginfo(this, idx);
482 LOG_WARNF("weapon model %s does not support the 'shell' tag, will display casings wrong",
484 this.spawnorigin = this.movedir;
489 this.oldorigin = '0 0 0'; // use regular attachment
494 if (this.weaponchild)
495 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
497 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
500 this.oldorigin = this.movedir - gettaginfo(this, idx);
505 "weapon model %s does not support the 'handle' tag "
506 "and neither does the v_ model support the 'shot' tag, "
507 "will display muzzle flashes TOTALLY wrong\n",
509 this.oldorigin = '0 0 0'; // there is no way to recover from this
514 this.viewmodelforclient = this.owner;
516 this.renderflags |= RF_VIEWMODEL;
520 this.view_ofs = '0 0 0';
521 this.movedir_aligned = this.movedir;
523 if (this.movedir.x >= 0)
525 //int algn = STAT(GUNALIGN, this.owner);
526 int algn = W_GunAlign(this, STAT(GUNALIGN, this.owner));
528 this.m_gunalign = algn;
530 vector v = this.movedir;
531 this.movedir = shotorg_adjust(v, false, false, algn);
532 this.movedir_aligned = shotorg_adjust(v, false, true, algn);
533 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
535 int compressed_shotorg = compressShotOrigin(this.movedir);
536 // make them match perfectly
539 if (this.owner) STAT(SHOTORG, this.owner) = compressed_shotorg;
541 this.movedir = decompressShotOrigin(compressed_shotorg);
543 this.spawnorigin += this.view_ofs; // offset the casings origin by the same amount
545 // check if an instant weapon switch occurred
546 setorigin(this, this.view_ofs);
548 // reset animstate now
549 this.wframe = WFRAME_IDLE;
550 setanim(this, this.anim_idle, true, false, true);
556 REGISTER_NET_TEMP(wframe)
558 NET_HANDLE(wframe, bool isNew)
560 WFRAME fr = ReadByte();
561 float t = ReadFloat();
562 int slot = ReadByte();
563 bool restartanim = ReadByte();
564 entity wepent = viewmodels[slot];
565 if(fr == WFRAME_IDLE)
566 wepent.animstate_looping = false; // we don't need to enforce idle animation
573 case WFRAME_IDLE: a = wepent.anim_idle; break;
574 case WFRAME_FIRE1: a = wepent.anim_fire1; break;
575 case WFRAME_FIRE2: a = wepent.anim_fire2; break;
576 case WFRAME_RELOAD: a = wepent.anim_reload; break;
579 anim_set(wepent, a, !restartanim, restartanim, restartanim);
581 wepent.state = ReadByte();
582 wepent.weapon_nextthink = ReadFloat();
583 switch (wepent.state)
586 wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_raise;
589 wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_drop;
592 wepent.weapon_switchdelay = 0;
600 void wframe_send(entity actor, entity weaponentity, int wepframe, float attackrate, bool restartanim)
602 if (!IS_REAL_CLIENT(actor)) return;
603 int channel = MSG_ONE;
605 WriteHeader(channel, wframe);
606 WriteByte(channel, wepframe);
607 WriteFloat(channel, attackrate);
608 WriteByte(channel, weaponslot(weaponentity.weaponentity_fld));
609 WriteByte(channel, restartanim);
610 WriteByte(channel, weaponentity.state);
611 WriteFloat(channel, weaponentity.weapon_nextthink);
615 REGISTER_NET_C2S(w_whereis)
617 void Weapon_whereis(Weapon this, entity cl);
618 NET_HANDLE(w_whereis, bool)
620 Weapon wpn = ReadRegistered(Weapons);
621 if (wpn != WEP_Null) Weapon_whereis(wpn, sender);
625 void w_whereis(Weapon this)
627 int channel = MSG_C2S;
628 WriteHeader(channel, w_whereis);
629 WriteRegistered(Weapons, channel, this);
631 CLIENT_COMMAND(weapon_find, "Show spawn locations of a weapon")
635 case CMD_REQUEST_COMMAND:
640 FOREACH(Weapons, it != WEP_Null, w_whereis(it));
645 FOREACH(Weapons, it != WEP_Null && !(STAT(WEAPONS) & it.m_wepset), w_whereis(it));
648 FOREACH(Weapons, it != WEP_Null && it.netname == s,
655 LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
656 case CMD_REQUEST_USAGE:
658 LOG_HELP("Usage:^3 cl_cmd weapon_find weapon");
659 LOG_HELP(" Where 'weapon' is the lowercase weapon name, 'all' or 'unowned'.");
667 void W_MuzzleFlash_Model_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
670 flash.angles_z = random() * 360;
672 entity view = actor.(weaponentity);
673 entity exterior = actor.exteriorweaponentity;
675 if (view.oldorigin.x > 0)
677 setattachment(flash, exterior, "");
678 setorigin(flash, view.oldorigin + offset);
682 if (gettagindex(exterior, "shot")) setattachment(flash, exterior, "shot");
683 else setattachment(flash, exterior, "tag_shot");
684 setorigin(flash, offset);
688 void W_MuzzleFlash_Model_AttachToShotorg(entity wepent, entity flash, vector offset)
690 flash.owner = wepent;
691 flash.angles_z = random() * 360;
693 if (gettagindex(wepent, "shot")) setattachment(flash, wepent, "shot");
694 else setattachment(flash, wepent, "tag_shot");
695 setorigin(flash, offset);
699 void W_MuzzleFlash_Model_Think(entity this)
704 this.nextthink = time + 0.05;
708 setthink(this, SUB_Remove);
709 this.nextthink = time;
710 this.realowner.muzzle_flash = NULL;
715 void W_MuzzleFlash_Model(entity wepent, entity muzzlemodel)
717 if(wepent.muzzle_flash == NULL)
718 wepent.muzzle_flash = spawn();
720 entity flash = wepent.muzzle_flash;
721 setmodel(flash, muzzlemodel); // precision set below
724 setthink(flash, W_MuzzleFlash_Model_Think);
725 flash.nextthink = time + 0.02;
728 flash.angles_z = random() * 180;
729 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT;
730 flash.owner = flash.realowner = wepent;
733 flash.drawmask = MASK_NORMAL;
737 REGISTER_NET_TEMP(w_muzzleflash)
740 void W_MuzzleFlash(Weapon thiswep, entity actor, .entity weaponentity, vector shotorg, vector shotdir)
742 // don't show an exterior muzzle effect for the off-hand
743 if(weaponslot(weaponentity) == 0)
745 Send_Effect_Except(thiswep.m_muzzleeffect, shotorg, shotdir * 1000, 1, actor);
747 if(thiswep.m_muzzlemodel != MDL_Null)
749 W_MuzzleFlash_Model(actor.exteriorweaponentity, thiswep.m_muzzlemodel);
750 W_MuzzleFlash_Model_AttachToShotorg(actor, weaponentity, actor.exteriorweaponentity.muzzle_flash, '5 0 0');
754 FOREACH_CLIENT(it == actor || (IS_SPEC(it) && it.enemy == actor),
756 if(!IS_REAL_CLIENT(it))
758 int channel = MSG_ONE;
760 WriteHeader(channel, w_muzzleflash);
761 WriteByte(channel, thiswep.m_id);
762 WriteByte(channel, weaponslot(weaponentity));
763 WriteVector(channel, shotorg);
767 NET_HANDLE(w_muzzleflash, bool isNew)
770 int weapon_id = ReadByte();
771 int slot = ReadByte();
772 vector sv_shotorg = ReadVector();
774 Weapon thiswep = REGISTRY_GET(Weapons, weapon_id);
775 vector viewangles = getpropertyvec(VF_CL_VIEWANGLES);
776 vector forward, right, up;
777 MAKE_VECTORS(viewangles, forward, right, up);
779 if(autocvar_chase_active)
781 // in third person mode, show the muzzle flash from the server side weapon position
782 // we don't have a view model to reference in this case
783 pointparticles(thiswep.m_muzzleeffect, sv_shotorg, forward * 1000, 1);
786 if(!autocvar_r_drawviewmodel) return;
788 entity wepent = viewmodels[slot];
789 // get the local player entity to calculate shot origin
790 entity rlplayer = CSQCModel_server2csqc(player_localentnum - 1);
792 rlplayer = csqcplayer; // fall back to the global
794 vector md = wepent.movedir_aligned;
795 vector vecs = ((md.x > 0) ? md : '0 0 0');
796 vector dv = forward * vecs.x + right * -vecs.y + up * vecs.z;
797 vector org = rlplayer.origin + rlplayer.view_ofs + dv;
799 pointparticles(thiswep.m_muzzleeffect, org, forward * 1000, 1);
801 if(thiswep.m_muzzlemodel != MDL_Null)
803 W_MuzzleFlash_Model(wepent, thiswep.m_muzzlemodel);
804 W_MuzzleFlash_Model_AttachToShotorg(wepent, wepent.muzzle_flash, '5 0 0');