7 #include <client/defs.qh>
8 #include "../constants.qh"
10 #include <lib/warpzone/anglestransform.qh>
11 #include <lib/warpzone/common.qh>
12 #include <lib/warpzone/client.qh>
14 #include <client/autocvars.qh>
15 #include "../deathtypes/all.qh"
16 #include <lib/csqcmodel/interpolate.qh>
17 #include "../physics/movetypes/movetypes.qh"
18 #include <client/main.qh>
19 #include <lib/csqcmodel/cl_model.qh>
22 #include <common/items/_mod.qh>
23 #include <lib/warpzone/anglestransform.qh>
24 #include <lib/warpzone/common.qh>
25 #include <lib/warpzone/util_server.qh>
26 #include <lib/warpzone/server.qh>
27 #include "../constants.qh"
28 #include "../stats.qh"
29 #include "../teams.qh"
31 #include "../monsters/all.qh"
33 #include <server/weapons/csqcprojectile.qh>
34 #include <server/weapons/tracing.qh>
35 #include "../t_items.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"
54 #define IMPLEMENTATION
58 // WEAPON PLUGIN SYSTEM
60 WepSet _WepSet_FromWeapon(int a)
71 return '0 0 1' * power2of(a);
73 return '0 1 0' * power2of(a);
75 return '1 0 0' * power2of(a);
78 void WriteWepSet(float dst, WepSet w)
80 if (Weapons_MAX > 48) WriteInt72_t(dst, w);
81 else if (Weapons_MAX > 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 (Weapons_MAX > 48) return ReadInt72_t();
97 if (Weapons_MAX > 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 = Weapons_from(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";
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[Weapons_MAX];
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 = Weapons_from(si);
159 int sj = W_FixWeaponOrder_BuildImpulseList_buf[j];
160 Weapon e2 = Weapons_from(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 void W_RandomWeapons(entity e, float n)
201 remaining = e.weapons;
203 for (i = 0; i < n; ++i)
205 RandomSelection_Init();
206 FOREACH(Weapons, it != WEP_Null, {
207 if (remaining & (it.m_wepset))
208 RandomSelection_Add(it, 0, string_null, 1, 1);
210 Weapon w = RandomSelection_chosen_ent;
211 result |= WepSet_FromWeapon(w);
212 remaining &= ~WepSet_FromWeapon(w);
217 string GetAmmoPicture(.int ammotype)
221 case ammo_shells: return ITEM_Shells.m_icon;
222 case ammo_nails: return ITEM_Bullets.m_icon;
223 case ammo_rockets: return ITEM_Rockets.m_icon;
224 case ammo_cells: return ITEM_Cells.m_icon;
225 case ammo_plasma: return ITEM_Plasma.m_icon;
226 case ammo_fuel: return ITEM_JetpackFuel.m_icon;
227 default: return ""; // wtf, no ammo type?
232 .int GetAmmoFieldFromNum(int i)
236 case 0: return ammo_shells;
237 case 1: return ammo_nails;
238 case 2: return ammo_rockets;
239 case 3: return ammo_cells;
240 case 4: return ammo_plasma;
241 case 5: return ammo_fuel;
242 default: return ammo_none;
246 int GetAmmoStat(.int ammotype)
250 case ammo_shells: return STAT_SHELLS;
251 case ammo_nails: return STAT_NAILS;
252 case ammo_rockets: return STAT_ROCKETS;
253 case ammo_cells: return STAT_CELLS;
254 case ammo_plasma: return STAT_PLASMA.m_id;
255 case ammo_fuel: return STAT_FUEL.m_id;
261 string W_Sound(string w_snd)
263 string output = strcat("weapons/", w_snd);
265 MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
266 return M_ARGV(1, string);
272 string W_Model(string w_mdl)
274 string output = strcat("models/weapons/", w_mdl);
275 MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
276 return M_ARGV(1, string);
280 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
302 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
309 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
312 else if (autocvar_g_shootfromeye)
316 else if (autocvar_g_shootfromcenter)
321 else if ((s = autocvar_g_shootfromfixedorigin) != "")
324 if (y_is_right) v.y = -v.y;
325 if (v.x != 0) vecs.x = v.x;
330 else // just do the same as top
332 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
338 #define shotorg_adjust shotorg_adjust_values
343 * 1. simple animated model, muzzle flash handling on h_ model:
344 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
346 * shot = muzzle end (shot origin, also used for muzzle flashes)
347 * shell = casings ejection point (must be on the right hand side of the gun)
348 * weapon = attachment for v_tuba.md3
349 * v_tuba.md3 - first and third person model
350 * g_tuba.md3 - pickup model
352 * 2. simple animated model, muzzle flash handling on v_ model:
353 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
355 * weapon = attachment for v_tuba.md3
356 * v_tuba.md3 - first and third person model
358 * shot = muzzle end (shot origin, also used for muzzle flashes)
359 * shell = casings ejection point (must be on the right hand side of the gun)
360 * g_tuba.md3 - pickup model
362 * 3. fully animated model, muzzle flash handling on h_ model:
363 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
365 * shot = muzzle end (shot origin, also used for muzzle flashes)
366 * shell = casings ejection point (must be on the right hand side of the gun)
367 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
368 * v_tuba.md3 - third person model
369 * g_tuba.md3 - pickup model
371 * 4. fully animated model, muzzle flash handling on v_ model:
372 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
374 * shot = muzzle end (shot origin)
375 * shell = casings ejection point (must be on the right hand side of the gun)
376 * v_tuba.md3 - third person model
378 * shot = muzzle end (for muzzle flashes)
379 * g_tuba.md3 - pickup model
382 * this.origin, this.angles
384 * this.movedir, this.view_ofs
391 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim)
396 if (this.weaponchild) delete(this.weaponchild);
397 this.weaponchild = NULL;
398 this.movedir = '0 0 0';
399 this.spawnorigin = '0 0 0';
400 this.oldorigin = '0 0 0';
401 this.anim_fire1 = '0 1 0.01';
402 this.anim_fire2 = '0 1 0.01';
403 this.anim_idle = '0 1 0.01';
404 this.anim_reload = '0 1 0.01';
408 // if there is a child entity, hide it until we're sure we use it
409 if (this.weaponchild) this.weaponchild.model = "";
410 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
411 int v_shot_idx; // used later
412 (v_shot_idx = gettagindex(this, "shot")) || (v_shot_idx = gettagindex(this, "tag_shot"));
414 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
415 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
416 this.anim_fire1 = animfixfps(this, '0 1 0.01', '0 0 0');
417 this.anim_fire2 = animfixfps(this, '1 1 0.01', '0 0 0');
418 this.anim_idle = animfixfps(this, '2 1 0.01', '0 0 0');
419 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
421 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
422 // if we don't, this is a "real" animated model
424 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
426 if (!this.weaponchild)
428 this.weaponchild = new(weaponchild);
430 this.weaponchild.drawmask = MASK_NORMAL;
431 this.weaponchild.renderflags |= RF_VIEWMODEL;
434 _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
435 setsize(this.weaponchild, '0 0 0', '0 0 0');
436 setattachment(this.weaponchild, this, t);
440 if (this.weaponchild) delete(this.weaponchild);
441 this.weaponchild = NULL;
444 setsize(this, '0 0 0', '0 0 0');
445 setorigin(this, '0 0 0');
446 this.angles = '0 0 0';
449 this.viewmodelforclient = NULL;
451 this.renderflags &= ~RF_VIEWMODEL;
453 if (v_shot_idx) // v_ model attached to invisible h_ model
455 this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
460 if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
462 this.movedir = gettaginfo(this, idx);
466 LOG_WARNF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong",
468 this.movedir = '0 0 0';
473 // v_ model attached to invisible h_ model
475 && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
477 this.spawnorigin = gettaginfo(this.weaponchild, idx);
479 else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
481 this.spawnorigin = gettaginfo(this, idx);
485 LOG_WARNF("weapon model %s does not support the 'shell' tag, will display casings wrong",
487 this.spawnorigin = this.movedir;
492 this.oldorigin = '0 0 0'; // use regular attachment
497 if (this.weaponchild)
498 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
500 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
503 this.oldorigin = this.movedir - gettaginfo(this, idx);
508 "weapon model %s does not support the 'handle' tag "
509 "and neither does the v_ model support the 'shot' tag, "
510 "will display muzzle flashes TOTALLY wrong\n",
512 this.oldorigin = '0 0 0'; // there is no way to recover from this
517 this.viewmodelforclient = this.owner;
519 this.renderflags |= RF_VIEWMODEL;
523 this.view_ofs = '0 0 0';
525 if (this.movedir.x >= 0)
527 int algn = STAT(GUNALIGN, this.owner);
528 vector v = this.movedir;
529 this.movedir = shotorg_adjust(v, false, false, algn);
530 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
532 int compressed_shotorg = compressShotOrigin(this.movedir);
533 // make them match perfectly
535 this.movedir = decompressShotOrigin(this.owner.stat_shotorg = compressed_shotorg);
537 this.movedir = decompressShotOrigin(compressed_shotorg);
540 this.spawnorigin += this.view_ofs; // offset the casings origin by the same amount
542 // check if an instant weapon switch occurred
543 setorigin(this, this.view_ofs);
545 // reset animstate now
546 this.wframe = WFRAME_IDLE;
547 setanim(this, this.anim_idle, true, false, true);
553 REGISTER_NET_TEMP(wframe)
555 NET_HANDLE(wframe, bool isNew)
561 bool restartanim = ReadByte();
562 anim_set(viewmodel, a, !restartanim, restartanim, restartanim);
563 viewmodel.state = ReadByte();
564 viewmodel.weapon_nextthink = ReadFloat();
565 viewmodel.alpha = ReadByte() / 255;
566 switch (viewmodel.state)
569 viewmodel.weapon_switchdelay = activeweapon.switchdelay_raise;
572 viewmodel.weapon_switchdelay = activeweapon.switchdelay_drop;
575 viewmodel.weapon_switchdelay = 0;
583 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim)
585 if (!IS_REAL_CLIENT(actor)) return;
586 int channel = MSG_ONE;
588 WriteHeader(channel, wframe);
589 WriteCoord(channel, a.x);
590 WriteCoord(channel, a.y);
591 WriteCoord(channel, a.z);
592 WriteByte(channel, restartanim);
593 WriteByte(channel, weaponentity.state);
594 WriteFloat(channel, weaponentity.weapon_nextthink);
595 WriteByte(channel, weaponentity.m_alpha * 255);
599 REGISTER_NET_C2S(w_whereis)
601 void Weapon_whereis(Weapon this, entity cl);
602 NET_HANDLE(w_whereis, bool)
604 Weapon wpn = ReadRegistered(Weapons);
605 if (wpn != WEP_Null) Weapon_whereis(wpn, sender);
609 void w_whereis(Weapon this)
611 int channel = MSG_C2S;
612 WriteHeader(channel, w_whereis);
613 WriteRegistered(Weapons, channel, this);
615 CLIENT_COMMAND(weapon_find, "Show spawn locations of a weapon")
619 case CMD_REQUEST_COMMAND:
624 FOREACH(Weapons, it != WEP_Null, w_whereis(it));
629 FOREACH(Weapons, it != WEP_Null && !(STAT(WEAPONS) & it.m_wepset), w_whereis(it));
632 FOREACH(Weapons, it != WEP_Null && it.netname == s,
640 LOG_INFOF("Incorrect parameters for ^2%s^7\n", "weapon_find");
642 case CMD_REQUEST_USAGE:
644 LOG_INFO("\nUsage:^3 cl_cmd weapon_find weapon\n");
645 LOG_INFO(" Where 'weapon' is the lowercase weapon name, 'all' or 'unowned'.\n");