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 "../buffs/all.qh"
15 #include "../../client/autocvars.qh"
16 #include "../deathtypes/all.qh"
17 #include "../../lib/csqcmodel/interpolate.qh"
18 #include "../movetypes/movetypes.qh"
19 #include "../../client/main.qh"
20 #include "../../lib/csqcmodel/cl_model.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 "../buffs/all.qh"
32 #include "../monsters/all.qh"
34 #include "../../server/weapons/csqcprojectile.qh"
35 #include "../../server/weapons/tracing.qh"
36 #include "../../server/t_items.qh"
37 #include "../../server/autocvars.qh"
38 #include "../../server/constants.qh"
39 #include "../../server/defs.qh"
40 #include "../notifications.qh"
41 #include "../deathtypes/all.qh"
42 #include "../../server/mutators/all.qh"
43 #include "../mapinfo.qh"
44 #include "../../server/command/common.qh"
45 #include "../../lib/csqcmodel/sv_model.qh"
46 #include "../../server/portals.qh"
47 #include "../../server/g_hook.qh"
50 #include "calculations.qc"
52 #define IMPLEMENTATION
56 // WEAPON PLUGIN SYSTEM
58 WepSet WepSet_FromWeapon(int a)
69 return '0 0 1' * power2of(a);
71 return '0 1 0' * power2of(a);
73 return '1 0 0' * power2of(a);
78 addstat(STAT_WEAPONS, AS_INT, weapons_x);
79 if (Weapons_MAX > 24) addstat(STAT_WEAPONS2, AS_INT, weapons_y);
80 if (Weapons_MAX > 48) addstat(STAT_WEAPONS3, AS_INT, weapons_z);
82 void WepSet_AddStat_InMap()
84 addstat(STAT_WEAPONSINMAP, AS_INT, weaponsinmap_x);
85 if (Weapons_MAX > 24) addstat(STAT_WEAPONSINMAP2, AS_INT, weaponsinmap_y);
86 if (Weapons_MAX > 48) addstat(STAT_WEAPONSINMAP3, AS_INT, weaponsinmap_z);
88 void WriteWepSet(float dst, WepSet w)
90 if (Weapons_MAX > 48) WriteInt72_t(dst, w);
91 else if (Weapons_MAX > 24) WriteInt48_t(dst, w);
92 else WriteInt24_t(dst, w.x);
96 WepSet WepSet_GetFromStat()
99 w.x = getstati(STAT_WEAPONS);
100 if (Weapons_MAX > 24) w.y = getstati(STAT_WEAPONS2);
101 if (Weapons_MAX > 48) w.z = getstati(STAT_WEAPONS3);
104 WepSet WepSet_GetFromStat_InMap()
107 w_x = getstati(STAT_WEAPONSINMAP);
108 if (Weapons_MAX > 24) w_y = getstati(STAT_WEAPONSINMAP2);
109 if (Weapons_MAX > 48) w_z = getstati(STAT_WEAPONSINMAP3);
114 if (Weapons_MAX > 48) return ReadInt72_t();
115 if (Weapons_MAX > 24) return ReadInt48_t();
116 return ReadInt24_t() * '1 0 0';
120 string W_FixWeaponOrder(string order, float complete)
122 return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
124 string W_NameWeaponOrder_MapFunc(string s)
127 if (s == "0" || stof(s))
129 wi = get_weaponinfo(stof(s));
130 if (wi != WEP_Null) return wi.netname;
135 string W_UndeprecateName(string s)
139 case "nex": return "vortex";
140 case "rocketlauncher": return "devastator";
141 case "laser": return "blaster";
142 case "minstanex": return "vaporizer";
143 case "grenadelauncher": return "mortar";
144 case "uzi": return "machinegun";
148 string W_NameWeaponOrder(string order)
150 return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
152 string W_NumberWeaponOrder_MapFunc(string s)
155 if (s == "0" || stof(s)) return s;
156 s = W_UndeprecateName(s);
157 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
158 if (s == get_weaponinfo(i).netname) return ftos(i);
161 string W_NumberWeaponOrder(string order)
163 return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
166 float W_FixWeaponOrder_BuildImpulseList_buf[Weapons_MAX];
167 string W_FixWeaponOrder_BuildImpulseList_order;
168 void W_FixWeaponOrder_BuildImpulseList_swap(int i, int j, entity pass)
171 h = W_FixWeaponOrder_BuildImpulseList_buf[i];
172 W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
173 W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
175 float W_FixWeaponOrder_BuildImpulseList_cmp(int i, int j, entity pass)
179 e1 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[i]);
180 e2 = get_weaponinfo(W_FixWeaponOrder_BuildImpulseList_buf[j]);
181 d = (e1.impulse + 9) % 10 - (e2.impulse + 9) % 10;
182 if (d != 0) return -d; // high impulse first!
183 return strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
184 sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[i]), 0)
186 strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
187 sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[j]), 0)
188 ; // low char index first!
190 string W_FixWeaponOrder_BuildImpulseList(string o)
193 W_FixWeaponOrder_BuildImpulseList_order = o;
194 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
195 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
196 heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp,
199 for (i = WEP_FIRST; i <= WEP_LAST; ++i)
200 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
201 W_FixWeaponOrder_BuildImpulseList_order = string_null;
202 return substring(o, 1, -1);
205 string W_FixWeaponOrder_AllowIncomplete(string order)
207 return W_FixWeaponOrder(order, 0);
210 string W_FixWeaponOrder_ForceComplete(string order)
212 if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
213 return W_FixWeaponOrder(order, 1);
216 void W_RandomWeapons(entity e, float n)
221 remaining = e.weapons;
223 for (i = 0; i < n; ++i)
225 RandomSelection_Init();
226 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
227 if (remaining & WepSet_FromWeapon(j)) RandomSelection_Add(world, j, string_null, 1, 1);
228 result |= WepSet_FromWeapon(RandomSelection_chosen_float);
229 remaining &= ~WepSet_FromWeapon(RandomSelection_chosen_float);
234 string GetAmmoPicture(.int ammotype)
238 case ammo_shells: return ITEM_Shells.m_icon;
239 case ammo_nails: return ITEM_Bullets.m_icon;
240 case ammo_rockets: return ITEM_Rockets.m_icon;
241 case ammo_cells: return ITEM_Cells.m_icon;
242 case ammo_plasma: return ITEM_Plasma.m_icon;
243 case ammo_fuel: return ITEM_JetpackFuel.m_icon;
244 default: return ""; // wtf, no ammo type?
249 .int GetAmmoFieldFromNum(int i)
253 case 0: return ammo_shells;
254 case 1: return ammo_nails;
255 case 2: return ammo_rockets;
256 case 3: return ammo_cells;
257 case 4: return ammo_plasma;
258 case 5: return ammo_fuel;
259 default: return ammo_none;
263 int GetAmmoStat(.int ammotype)
267 case ammo_shells: return STAT_SHELLS;
268 case ammo_nails: return STAT_NAILS;
269 case ammo_rockets: return STAT_ROCKETS;
270 case ammo_cells: return STAT_CELLS;
271 case ammo_plasma: return STAT_PLASMA;
272 case ammo_fuel: return STAT_FUEL;
278 string W_Sound(string w_snd)
280 #define extensions(X) X(wav) X(ogg)
281 #define tryext(ext) { if (fexists(strcat("sound/", output = strcat("weapons/", w_snd, "." #ext)))) break; }
288 output = strcat("weapons/", w_snd);
293 MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
294 return weapon_sound_output;
300 string W_Model(string w_mdl)
302 string output = strcat("models/weapons/", w_mdl);
304 MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
305 return weapon_model_output;
312 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
334 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
341 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
344 else if (autocvar_g_shootfromeye)
348 else if (autocvar_g_shootfromcenter)
353 else if ((s = autocvar_g_shootfromfixedorigin) != "")
356 if (y_is_right) v.y = -v.y;
357 if (v.x != 0) vecs.x = v.x;
362 else // just do the same as top
364 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
370 #define shotorg_adjust shotorg_adjust_values
375 * 1. simple animated model, muzzle flash handling on h_ model:
376 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
378 * shot = muzzle end (shot origin, also used for muzzle flashes)
379 * shell = casings ejection point (must be on the right hand side of the gun)
380 * weapon = attachment for v_tuba.md3
381 * v_tuba.md3 - first and third person model
382 * g_tuba.md3 - pickup model
384 * 2. simple animated model, muzzle flash handling on v_ model:
385 * h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
387 * weapon = attachment for v_tuba.md3
388 * v_tuba.md3 - first and third person model
390 * shot = muzzle end (shot origin, also used for muzzle flashes)
391 * shell = casings ejection point (must be on the right hand side of the gun)
392 * g_tuba.md3 - pickup model
394 * 3. fully animated model, muzzle flash handling on h_ model:
395 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
397 * shot = muzzle end (shot origin, also used for muzzle flashes)
398 * shell = casings ejection point (must be on the right hand side of the gun)
399 * handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
400 * v_tuba.md3 - third person model
401 * g_tuba.md3 - pickup model
403 * 4. fully animated model, muzzle flash handling on v_ model:
404 * h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
406 * shot = muzzle end (shot origin)
407 * shell = casings ejection point (must be on the right hand side of the gun)
408 * v_tuba.md3 - third person model
410 * shot = muzzle end (for muzzle flashes)
411 * g_tuba.md3 - pickup model
414 * this.origin, this.angles
416 * this.movedir, this.view_ofs
423 void CL_WeaponEntity_SetModel(entity this, string name)
428 if (this.weaponchild) remove(this.weaponchild);
429 this.weaponchild = NULL;
430 this.movedir = '0 0 0';
431 this.spawnorigin = '0 0 0';
432 this.oldorigin = '0 0 0';
433 this.anim_fire1 = '0 1 0.01';
434 this.anim_fire2 = '0 1 0.01';
435 this.anim_idle = '0 1 0.01';
436 this.anim_reload = '0 1 0.01';
440 // if there is a child entity, hide it until we're sure we use it
441 if (this.weaponchild) this.weaponchild.model = "";
442 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
443 int v_shot_idx; // used later
444 (v_shot_idx = gettagindex(this, "shot")) || (v_shot_idx = gettagindex(this, "tag_shot"));
446 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
447 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
448 this.anim_fire1 = animfixfps(this, '0 1 0.01', '0 0 0');
449 this.anim_fire2 = animfixfps(this, '1 1 0.01', '0 0 0');
450 this.anim_idle = animfixfps(this, '2 1 0.01', '0 0 0');
451 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
453 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
454 // if we don't, this is a "real" animated model
456 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
458 if (!this.weaponchild)
460 this.weaponchild = new(weaponchild);
462 this.weaponchild.drawmask = MASK_NORMAL;
465 _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
466 setattachment(this.weaponchild, this, t);
470 if (this.weaponchild) remove(this.weaponchild);
471 this.weaponchild = NULL;
474 setorigin(this, '0 0 0');
475 this.angles = '0 0 0';
478 this.viewmodelforclient = NULL;
480 this.renderflags &= ~RF_VIEWMODEL;
482 if (v_shot_idx) // v_ model attached to invisible h_ model
484 this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
489 if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
491 this.movedir = gettaginfo(this, idx);
495 LOG_WARNINGF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong\n",
497 this.movedir = '0 0 0';
502 // v_ model attached to invisible h_ model
504 && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
506 this.spawnorigin = gettaginfo(this.weaponchild, idx);
508 else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
510 this.spawnorigin = gettaginfo(this, idx);
514 LOG_WARNINGF("weapon model %s does not support the 'shell' tag, will display casings wrong\n",
516 this.spawnorigin = this.movedir;
521 this.oldorigin = '0 0 0'; // use regular attachment
526 if (this.weaponchild)
527 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
529 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
532 this.oldorigin = this.movedir - gettaginfo(this, idx);
537 "weapon model %s does not support the 'handle' tag "
538 "and neither does the v_ model support the 'shot' tag, "
539 "will display muzzle flashes TOTALLY wrong\n",
541 this.oldorigin = '0 0 0'; // there is no way to recover from this
546 this.viewmodelforclient = this.owner;
548 this.renderflags |= RF_VIEWMODEL;
552 this.view_ofs = '0 0 0';
554 if (this.movedir.x >= 0)
557 int algn = this.owner.cvar_cl_gunalign;
559 int algn = autocvar_cl_gunalign;
561 vector v = this.movedir;
562 this.movedir = shotorg_adjust(v, false, false, algn);
563 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
565 int compressed_shotorg = compressShotOrigin(this.movedir);
566 // make them match perfectly
568 this.movedir = decompressShotOrigin(this.owner.stat_shotorg = compressed_shotorg);
570 this.movedir = decompressShotOrigin(compressed_shotorg);
573 this.spawnorigin += this.view_ofs; // offset the casings origin by the same amount
575 // check if an instant weapon switch occurred
576 setorigin(this, this.view_ofs);
577 // reset animstate now
578 this.wframe = WFRAME_IDLE;
579 setanim(this, this.anim_idle, true, false, true);
585 REGISTER_NET_TEMP(wframe, bool isNew)
592 bool restartanim = ReadByte();
593 setanim(viewmodel, a, restartanim == false, restartanim, restartanim);
594 viewmodel.state = ReadByte();
599 void wframe_send(entity actor, .entity weaponentity, vector a, bool restartanim)
601 if (!IS_REAL_CLIENT(actor)) return;
602 int channel = MSG_ONE;
604 WriteHeader(channel, wframe);
605 WriteCoord(channel, a.x);
606 WriteCoord(channel, a.y);
607 WriteCoord(channel, a.z);
608 WriteByte(channel, restartanim);
609 WriteByte(channel, actor.(weaponentity).state);