6 #include "../constants.qh"
8 #include <lib/warpzone/anglestransform.qh>
9 #include <lib/warpzone/common.qh>
10 #include <lib/warpzone/client.qh>
12 #include <client/autocvars.qh>
13 #include "../deathtypes/all.qh"
14 #include <lib/csqcmodel/interpolate.qh>
15 #include "../physics/movetypes/movetypes.qh"
16 #include <client/main.qh>
17 #include <lib/csqcmodel/cl_model.qh>
20 #include <common/items/_mod.qh>
21 #include <lib/warpzone/anglestransform.qh>
22 #include <lib/warpzone/common.qh>
23 #include <lib/warpzone/util_server.qh>
24 #include <lib/warpzone/server.qh>
25 #include "../constants.qh"
26 #include "../stats.qh"
27 #include "../teams.qh"
28 #include <common/util.qh>
29 #include "../monsters/_mod.qh"
31 #include <server/weapons/common.qh>
32 #include <server/weapons/csqcprojectile.qh>
33 #include <server/weapons/tracing.qh>
34 #include <server/items/spawning.qh>
35 #include <server/autocvars.qh>
36 #include <server/constants.qh>
37 #include "../notifications/all.qh"
38 #include "../deathtypes/all.qh"
39 #include <server/mutators/_mod.qh>
40 #include "../mapinfo.qh"
41 #include <server/command/_mod.qh>
42 #include <lib/csqcmodel/sv_model.qh>
43 #include <server/portals.qh>
44 #include <server/g_hook.qh>
47 #include "calculations.qc"
53 #include "weapon/_mod.inc"
56 // WEAPON PLUGIN SYSTEM
58 WepSet _WepSet_FromWeapon(int a)
61 if (REGISTRY_MAX(Weapons) > 24)
65 if (REGISTRY_MAX(Weapons) > 48)
69 return '0 0 1' * (2 ** a);
71 return '0 1 0' * (2 ** a);
73 return '1 0 0' * (2 ** a);
76 void WriteWepSet(float dst, WepSet w)
78 if (REGISTRY_MAX(Weapons) > 48) WriteInt72_t(dst, w);
79 else if (REGISTRY_MAX(Weapons) > 24) WriteInt48_t(dst, w);
80 else WriteInt24_t(dst, w.x);
84 WepSet WepSet_GetFromStat()
88 WepSet WepSet_GetFromStat_InMap()
90 return STAT(WEAPONSINMAP);
94 if (REGISTRY_MAX(Weapons) > 48) return ReadInt72_t();
95 if (REGISTRY_MAX(Weapons) > 24) return ReadInt48_t();
96 return ReadInt24_t() * '1 0 0';
100 string W_FixWeaponOrder(string order, float complete)
102 return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
104 string W_NameWeaponOrder_MapFunc(string s)
109 entity wi = REGISTRY_GET(Weapons, i);
110 if (wi != WEP_Null) return wi.netname;
115 string W_UndeprecateName(string s)
119 case "nex": return "vortex";
120 case "rocketlauncher": return "devastator";
121 case "laser": return "blaster";
122 case "minstanex": return "vaporizer";
123 case "grenadelauncher": return "mortar";
124 case "uzi": return "machinegun";
125 case "hmg": return "okhmg";
126 case "rpc": return "okrpc";
130 string W_NameWeaponOrder(string order)
132 return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
134 string W_NumberWeaponOrder_MapFunc(string s)
136 if (s == "0" || stof(s)) return s;
137 s = W_UndeprecateName(s);
138 FOREACH(Weapons, it != WEP_Null && it.netname == s, return ftos(i));
141 string W_NumberWeaponOrder(string order)
143 return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
146 float W_FixWeaponOrder_BuildImpulseList_buf[REGISTRY_MAX(Weapons)];
147 string W_FixWeaponOrder_BuildImpulseList_order;
148 void W_FixWeaponOrder_BuildImpulseList_swap(int i, int j, entity pass)
151 h = W_FixWeaponOrder_BuildImpulseList_buf[i];
152 W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
153 W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
155 float W_FixWeaponOrder_BuildImpulseList_cmp(int i, int j, entity pass)
157 int si = W_FixWeaponOrder_BuildImpulseList_buf[i];
158 Weapon e1 = REGISTRY_GET(Weapons, si);
159 int sj = W_FixWeaponOrder_BuildImpulseList_buf[j];
160 Weapon e2 = REGISTRY_GET(Weapons, sj);
161 int d = (e1.impulse + 9) % 10 - (e2.impulse + 9) % 10;
162 if (d != 0) return -d; // high impulse first!
163 return strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
164 sprintf(" %d ", si), 0)
166 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
167 sprintf(" %d ", sj), 0)
168 ; // low char index first!
170 string W_FixWeaponOrder_BuildImpulseList(string o)
173 W_FixWeaponOrder_BuildImpulseList_order = o;
174 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
175 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
176 heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp,
179 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
180 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
181 W_FixWeaponOrder_BuildImpulseList_order = string_null;
182 return substring(o, 1, -1);
185 string W_FixWeaponOrder_AllowIncomplete(entity this, string order)
187 return W_FixWeaponOrder(order, 0);
190 string W_FixWeaponOrder_ForceComplete(string order)
192 if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
193 return W_FixWeaponOrder(order, 1);
196 WepSet W_RandomWeapons(entity e, WepSet remaining, int n)
198 WepSet result = '0 0 0';
199 for (int j = 0; j < n; ++j)
201 RandomSelection_Init();
202 FOREACH(Weapons, it != WEP_Null, {
203 if (remaining & (it.m_wepset))
204 RandomSelection_AddEnt(it, 1, 1);
206 Weapon w = RandomSelection_chosen_ent;
207 result |= WepSet_FromWeapon(w);
208 remaining &= ~WepSet_FromWeapon(w);
213 string GetAmmoPicture(int ammotype)
217 case RES_SHELLS: return ITEM_Shells.m_icon;
218 case RES_BULLETS: return ITEM_Bullets.m_icon;
219 case RES_ROCKETS: return ITEM_Rockets.m_icon;
220 case RES_CELLS: return ITEM_Cells.m_icon;
221 case RES_PLASMA: return ITEM_Plasma.m_icon;
222 case RES_FUEL: return ITEM_JetpackFuel.m_icon;
223 default: return ""; // wtf, no ammo type?
227 string GetAmmoName(int ammotype)
231 case RES_SHELLS: return ITEM_Shells.m_name;
232 case RES_BULLETS: return ITEM_Bullets.m_name;
233 case RES_ROCKETS: return ITEM_Rockets.m_name;
234 case RES_CELLS: return ITEM_Cells.m_name;
235 case RES_PLASMA: return ITEM_Plasma.m_name;
236 case RES_FUEL: return ITEM_JetpackFuel.m_name;
237 default: return "batteries";
242 int GetAmmoTypeFromNum(int i)
246 case 0: return RES_SHELLS;
247 case 1: return RES_BULLETS;
248 case 2: return RES_ROCKETS;
249 case 3: return RES_CELLS;
250 case 4: return RES_PLASMA;
251 case 5: return RES_FUEL;
252 default: return RES_NONE;
256 int GetAmmoStat(int ammotype)
260 case RES_SHELLS: return STAT_SHELLS;
261 case RES_BULLETS: return STAT_NAILS;
262 case RES_ROCKETS: return STAT_ROCKETS;
263 case RES_CELLS: return STAT_CELLS;
264 case RES_PLASMA: return STAT_PLASMA.m_id;
265 case RES_FUEL: return STAT_FUEL.m_id;
271 string W_Sound(string w_snd)
273 string output = strcat("weapons/", w_snd);
274 MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
275 return M_ARGV(1, string);
278 string W_Model(string w_mdl)
280 string output = strcat("models/weapons/", w_mdl);
281 MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
282 return M_ARGV(1, string);
286 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
308 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
313 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
315 else if (STAT(SHOOTFROMEYE))
319 else if (STAT(SHOOTFROMCENTER))
324 else if ((s = G_SHOOTFROMFIXEDORIGIN) != "")
327 if (y_is_right) v.y = -v.y;
328 if (v.x != 0) vecs.x = v.x;
332 else // just do the same as top
334 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
340 #define shotorg_adjust shotorg_adjust_values
345 * 1. simple animated model, muzzle flash handling on h_ model:
346 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
348 * shot = muzzle end (shot origin, also used for muzzle flashes)
349 * shell = casings ejection point (must be on the right hand side of the gun)
350 * weapon = attachment for v_tuba.md3
351 * v_tuba.md3 - first and third person model
352 * g_tuba.md3 - pickup model
354 * 2. simple animated model, muzzle flash handling on v_ model:
355 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
357 * weapon = attachment for v_tuba.md3
358 * v_tuba.md3 - first and third 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 * g_tuba.md3 - pickup model
364 * 3. fully animated model, muzzle flash handling on h_ model:
365 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
367 * shot = muzzle end (shot origin, also used for muzzle flashes)
368 * shell = casings ejection point (must be on the right hand side of the gun)
369 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
370 * v_tuba.md3 - third person model
371 * g_tuba.md3 - pickup model
373 * 4. fully animated model, muzzle flash handling on v_ model:
374 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
376 * shot = muzzle end (shot origin)
377 * shell = casings ejection point (must be on the right hand side of the gun)
378 * v_tuba.md3 - third person model
380 * shot = muzzle end (for muzzle flashes)
381 * g_tuba.md3 - pickup model
384 * this.origin, this.angles
386 * this.movedir, this.view_ofs, this.movedir_aligned
393 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim)
397 vector oldmin = this.mins, oldmax = this.maxs;
398 setmodel(this, MDL_Null);
399 setsize(this, oldmin, oldmax);
400 if (this.weaponchild) delete(this.weaponchild);
401 this.weaponchild = NULL;
402 this.movedir = '0 0 0';
403 this.spawnorigin = '0 0 0';
404 this.oldorigin = '0 0 0';
405 this.anim_fire1 = '0 1 0.01';
406 this.anim_fire2 = '0 1 0.01';
407 this.anim_idle = '0 1 0.01';
408 this.anim_reload = '0 1 0.01';
412 // if there is a child entity, hide it until we're sure we use it
413 if (this.weaponchild) this.weaponchild.model = "";
414 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
415 int v_shot_idx; // used later
416 (v_shot_idx = gettagindex(this, "shot")) || (v_shot_idx = gettagindex(this, "tag_shot"));
418 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
419 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
420 this.anim_fire1 = animfixfps(this, '0 1 0.01', '0 0 0');
421 this.anim_fire2 = animfixfps(this, '1 1 0.01', '0 0 0');
422 this.anim_idle = animfixfps(this, '2 1 0.01', '0 0 0');
423 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
425 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
426 // if we don't, this is a "real" animated model
428 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
430 if (!this.weaponchild)
432 this.weaponchild = new(weaponchild);
434 this.weaponchild.drawmask = MASK_NORMAL;
435 this.weaponchild.renderflags |= RF_VIEWMODEL;
438 _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
439 setsize(this.weaponchild, '0 0 0', '0 0 0');
440 setattachment(this.weaponchild, this, t);
444 if (this.weaponchild) delete(this.weaponchild);
445 this.weaponchild = NULL;
448 setsize(this, '0 0 0', '0 0 0');
449 setorigin(this, '0 0 0');
450 this.angles = '0 0 0';
453 this.viewmodelforclient = NULL;
455 this.renderflags &= ~RF_VIEWMODEL;
457 if (v_shot_idx) // v_ model attached to invisible h_ model
459 this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
464 if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
466 this.movedir = gettaginfo(this, idx);
470 LOG_WARNF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong",
472 this.movedir = '0 0 0';
477 // v_ model attached to invisible h_ model
479 && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
481 this.spawnorigin = gettaginfo(this.weaponchild, idx);
483 else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
485 this.spawnorigin = gettaginfo(this, idx);
489 LOG_WARNF("weapon model %s does not support the 'shell' tag, will display casings wrong",
491 this.spawnorigin = this.movedir;
496 this.oldorigin = '0 0 0'; // use regular attachment
501 if (this.weaponchild)
502 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
504 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
507 this.oldorigin = this.movedir - gettaginfo(this, idx);
512 "weapon model %s does not support the 'handle' tag "
513 "and neither does the v_ model support the 'shot' tag, "
514 "will display muzzle flashes TOTALLY wrong\n",
516 this.oldorigin = '0 0 0'; // there is no way to recover from this
521 this.viewmodelforclient = this.owner;
523 this.renderflags |= RF_VIEWMODEL;
527 this.view_ofs = '0 0 0';
528 this.movedir_aligned = this.movedir;
530 if (this.movedir.x >= 0)
532 //int algn = STAT(GUNALIGN, this.owner);
533 int algn = W_GunAlign(this, STAT(GUNALIGN, this.owner));
535 this.m_gunalign = algn;
537 vector v = this.movedir;
538 this.movedir = shotorg_adjust(v, false, false, algn);
539 this.movedir_aligned = shotorg_adjust(v, false, true, algn);
540 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
542 int compressed_shotorg = compressShotOrigin(this.movedir);
543 // make them match perfectly
546 if (this.owner) STAT(SHOTORG, this.owner) = compressed_shotorg;
547 this.movedir = decompressShotOrigin(compressed_shotorg);
549 this.movedir = decompressShotOrigin(compressed_shotorg);
552 this.spawnorigin += this.view_ofs; // offset the casings origin by the same amount
554 // check if an instant weapon switch occurred
555 setorigin(this, this.view_ofs);
557 // reset animstate now
558 this.wframe = WFRAME_IDLE;
559 setanim(this, this.anim_idle, true, false, true);
565 REGISTER_NET_TEMP(wframe)
567 NET_HANDLE(wframe, bool isNew)
570 float t = ReadFloat();
571 int slot = ReadByte();
572 bool restartanim = ReadByte();
573 entity wepent = viewmodels[slot];
574 if(fr == WFRAME_IDLE)
575 wepent.animstate_looping = false; // we don't need to enforce idle animation
582 case WFRAME_IDLE: a = wepent.anim_idle; break;
583 case WFRAME_FIRE1: a = wepent.anim_fire1; break;
584 case WFRAME_FIRE2: a = wepent.anim_fire2; break;
585 case WFRAME_RELOAD: a = wepent.anim_reload; break;
588 anim_set(wepent, a, !restartanim, restartanim, restartanim);
590 wepent.state = ReadByte();
591 wepent.weapon_nextthink = ReadFloat();
592 switch (wepent.state)
595 wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_raise;
598 wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_drop;
601 wepent.weapon_switchdelay = 0;
609 void wframe_send(entity actor, entity weaponentity, int wepframe, float attackrate, bool restartanim)
611 if (!IS_REAL_CLIENT(actor)) return;
612 int channel = MSG_ONE;
614 WriteHeader(channel, wframe);
615 WriteByte(channel, wepframe);
616 WriteFloat(channel, attackrate);
617 WriteByte(channel, weaponslot(weaponentity.weaponentity_fld));
618 WriteByte(channel, restartanim);
619 WriteByte(channel, weaponentity.state);
620 WriteFloat(channel, weaponentity.weapon_nextthink);
624 REGISTER_NET_C2S(w_whereis)
626 void Weapon_whereis(Weapon this, entity cl);
627 NET_HANDLE(w_whereis, bool)
629 Weapon wpn = ReadRegistered(Weapons);
630 if (wpn != WEP_Null) Weapon_whereis(wpn, sender);
634 void w_whereis(Weapon this)
636 int channel = MSG_C2S;
637 WriteHeader(channel, w_whereis);
638 WriteRegistered(Weapons, channel, this);
640 CLIENT_COMMAND(weapon_find, "Show spawn locations of a weapon")
644 case CMD_REQUEST_COMMAND:
649 FOREACH(Weapons, it != WEP_Null, w_whereis(it));
654 FOREACH(Weapons, it != WEP_Null && !(STAT(WEAPONS) & it.m_wepset), w_whereis(it));
657 FOREACH(Weapons, it != WEP_Null && it.netname == s,
664 LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
665 case CMD_REQUEST_USAGE:
667 LOG_HELP("Usage:^3 cl_cmd weapon_find weapon");
668 LOG_HELP(" Where 'weapon' is the lowercase weapon name, 'all' or 'unowned'.");
676 void W_MuzzleFlash_Model_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
679 flash.angles_z = random() * 360;
681 entity view = actor.(weaponentity);
682 entity exterior = actor.exteriorweaponentity;
684 if (view.oldorigin.x > 0)
686 setattachment(flash, exterior, "");
687 setorigin(flash, view.oldorigin + offset);
691 if (gettagindex(exterior, "shot")) setattachment(flash, exterior, "shot");
692 else setattachment(flash, exterior, "tag_shot");
693 setorigin(flash, offset);
697 void W_MuzzleFlash_Model_AttachToShotorg(entity wepent, entity flash, vector offset)
699 flash.owner = wepent;
700 flash.angles_z = random() * 360;
702 if (gettagindex(wepent, "shot")) setattachment(flash, wepent, "shot");
703 else setattachment(flash, wepent, "tag_shot");
704 setorigin(flash, offset);
708 void W_MuzzleFlash_Model_Think(entity this)
713 this.nextthink = time + 0.05;
717 setthink(this, SUB_Remove);
718 this.nextthink = time;
719 this.realowner.muzzle_flash = NULL;
724 void W_MuzzleFlash_Model(entity wepent, entity muzzlemodel)
726 if(wepent.muzzle_flash == NULL)
727 wepent.muzzle_flash = spawn();
729 entity flash = wepent.muzzle_flash;
730 setmodel(flash, muzzlemodel); // precision set below
733 setthink(flash, W_MuzzleFlash_Model_Think);
734 flash.nextthink = time + 0.02;
737 flash.angles_z = random() * 180;
738 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT;
739 flash.owner = flash.realowner = wepent;
742 flash.drawmask = MASK_NORMAL;
746 REGISTER_NET_TEMP(w_muzzleflash)
749 void W_MuzzleFlash(Weapon thiswep, entity actor, .entity weaponentity, vector shotorg, vector shotdir)
751 // don't show an exterior muzzle effect for the off-hand
752 if(weaponslot(weaponentity) == 0)
754 Send_Effect_Except(thiswep.m_muzzleeffect, shotorg, shotdir * 1000, 1, actor);
756 if(thiswep.m_muzzlemodel != MDL_Null)
758 W_MuzzleFlash_Model(actor.exteriorweaponentity, thiswep.m_muzzlemodel);
759 W_MuzzleFlash_Model_AttachToShotorg(actor, weaponentity, actor.exteriorweaponentity.muzzle_flash, '5 0 0');
763 FOREACH_CLIENT(it == actor || (IS_SPEC(it) && it.enemy == actor),
765 if(!IS_REAL_CLIENT(it))
767 int channel = MSG_ONE;
769 WriteHeader(channel, w_muzzleflash);
770 WriteByte(channel, thiswep.m_id);
771 WriteByte(channel, weaponslot(weaponentity));
772 WriteVector(channel, shotorg);
776 NET_HANDLE(w_muzzleflash, bool isNew)
779 int weapon_id = ReadByte();
780 int slot = ReadByte();
781 vector sv_shotorg = ReadVector();
783 Weapon thiswep = REGISTRY_GET(Weapons, weapon_id);
784 vector viewangles = getpropertyvec(VF_CL_VIEWANGLES);
785 vector forward, right, up;
786 MAKE_VECTORS(viewangles, forward, right, up);
788 if(autocvar_chase_active)
790 // in third person mode, show the muzzle flash from the server side weapon position
791 // we don't have a view model to reference in this case
792 pointparticles(thiswep.m_muzzleeffect, sv_shotorg, forward * 1000, 1);
795 if(!autocvar_r_drawviewmodel) return;
797 entity wepent = viewmodels[slot];
798 // get the local player entity to calculate shot origin
799 entity rlplayer = CSQCModel_server2csqc(player_localentnum - 1);
801 rlplayer = csqcplayer; // fall back to the global
803 vector md = wepent.movedir_aligned;
804 vector vecs = ((md.x > 0) ? md : '0 0 0');
805 vector dv = forward * vecs.x + right * -vecs.y + up * vecs.z;
806 vector org = rlplayer.origin + rlplayer.view_ofs + dv;
808 pointparticles(thiswep.m_muzzleeffect, org, forward * 1000, 1);
810 if(thiswep.m_muzzlemodel != MDL_Null)
812 W_MuzzleFlash_Model(wepent, thiswep.m_muzzlemodel);
813 W_MuzzleFlash_Model_AttachToShotorg(wepent, wepent.muzzle_flash, '5 0 0');