b7c65ed960ff6e13eb7696d56c2718db25d7ab47
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / all.qc
1 #include "all.qh"
2 #ifndef WEAPONS_ALL_C
3 #define WEAPONS_ALL_C
4
5 #if defined(CSQC)
6         #include <client/defs.qh>
7         #include "../constants.qh"
8         #include "../stats.qh"
9         #include <lib/warpzone/anglestransform.qh>
10         #include <lib/warpzone/common.qh>
11         #include <lib/warpzone/client.qh>
12         #include "../util.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>
19 #elif defined(MENUQC)
20 #elif defined(SVQC)
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"
31     #include "config.qh"
32     #include <server/weapons/csqcprojectile.qh>
33     #include <server/weapons/tracing.qh>
34     #include "../t_items.qh"
35     #include <server/autocvars.qh>
36     #include <server/constants.qh>
37     #include <server/defs.qh>
38     #include "../notifications/all.qh"
39     #include "../deathtypes/all.qh"
40     #include <server/mutators/_mod.qh>
41     #include "../mapinfo.qh"
42     #include <server/command/_mod.qh>
43     #include <lib/csqcmodel/sv_model.qh>
44     #include <server/portals.qh>
45     #include <server/g_hook.qh>
46 #endif
47 #ifdef GAMEQC
48         #include "calculations.qc"
49 #endif
50 #ifdef SVQC
51         #include "config.qc"
52 #endif
53
54 #include "weapon/_mod.inc"
55
56
57 // WEAPON PLUGIN SYSTEM
58
59 WepSet _WepSet_FromWeapon(int a)
60 {
61         a -= WEP_FIRST;
62         if (Weapons_MAX > 24)
63                 if (a >= 24)
64                 {
65                         a -= 24;
66                         if (Weapons_MAX > 48)
67                                 if (a >= 24)
68                                 {
69                                         a -= 24;
70                                         return '0 0 1' * (2 ** a);
71                                 }
72                         return '0 1 0' * (2 ** a);
73                 }
74         return '1 0 0' * (2 ** a);
75 }
76 #ifdef SVQC
77         void WriteWepSet(float dst, WepSet w)
78         {
79                 if (Weapons_MAX > 48) WriteInt72_t(dst, w);
80                 else if (Weapons_MAX > 24) WriteInt48_t(dst, w);
81                 else WriteInt24_t(dst, w.x);
82         }
83 #endif
84 #ifdef CSQC
85         WepSet WepSet_GetFromStat()
86         {
87                 return STAT(WEAPONS);
88         }
89         WepSet WepSet_GetFromStat_InMap()
90         {
91                 return STAT(WEAPONSINMAP);
92         }
93         WepSet ReadWepSet()
94         {
95                 if (Weapons_MAX > 48) return ReadInt72_t();
96                 if (Weapons_MAX > 24) return ReadInt48_t();
97                 return ReadInt24_t() * '1 0 0';
98         }
99 #endif
100
101 string W_FixWeaponOrder(string order, float complete)
102 {
103         return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
104 }
105 string W_NameWeaponOrder_MapFunc(string s)
106 {
107         int i = stof(s);
108         if (s == "0" || i)
109         {
110                 entity wi = Weapons_from(i);
111                 if (wi != WEP_Null) return wi.netname;
112         }
113         return s;
114 }
115
116 string W_UndeprecateName(string s)
117 {
118         switch (s)
119         {
120                 case "nex": return "vortex";
121                 case "rocketlauncher": return "devastator";
122                 case "laser": return "blaster";
123                 case "minstanex": return "vaporizer";
124                 case "grenadelauncher": return "mortar";
125                 case "uzi": return "machinegun";
126                 case "hmg": return "okhmg";
127                 case "rpc": return "okrpc";
128                 default: return s;
129         }
130 }
131 string W_NameWeaponOrder(string order)
132 {
133         return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
134 }
135 string W_NumberWeaponOrder_MapFunc(string s)
136 {
137         if (s == "0" || stof(s)) return s;
138         s = W_UndeprecateName(s);
139         FOREACH(Weapons, it != WEP_Null && it.netname == s, return ftos(i));
140         return s;
141 }
142 string W_NumberWeaponOrder(string order)
143 {
144         return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
145 }
146
147 float W_FixWeaponOrder_BuildImpulseList_buf[Weapons_MAX];
148 string W_FixWeaponOrder_BuildImpulseList_order;
149 void W_FixWeaponOrder_BuildImpulseList_swap(int i, int j, entity pass)
150 {
151         float h;
152         h = W_FixWeaponOrder_BuildImpulseList_buf[i];
153         W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
154         W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
155 }
156 float W_FixWeaponOrder_BuildImpulseList_cmp(int i, int j, entity pass)
157 {
158         int si = W_FixWeaponOrder_BuildImpulseList_buf[i];
159         Weapon e1 = Weapons_from(si);
160         int sj = W_FixWeaponOrder_BuildImpulseList_buf[j];
161         Weapon e2 = Weapons_from(sj);
162         int d = (e1.impulse + 9) % 10 - (e2.impulse + 9) % 10;
163         if (d != 0) return -d;  // high impulse first!
164         return strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
165                 sprintf(" %d ", si), 0)
166                -
167                strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
168                 sprintf(" %d ", sj), 0)
169         ;  // low char index first!
170 }
171 string W_FixWeaponOrder_BuildImpulseList(string o)
172 {
173         int i;
174         W_FixWeaponOrder_BuildImpulseList_order = o;
175         for (i = WEP_FIRST; i <= WEP_LAST; ++i)
176                 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
177         heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp,
178                 NULL);
179         o = "";
180         for (i = WEP_FIRST; i <= WEP_LAST; ++i)
181                 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
182         W_FixWeaponOrder_BuildImpulseList_order = string_null;
183         return substring(o, 1, -1);
184 }
185
186 string W_FixWeaponOrder_AllowIncomplete(entity this, string order)
187 {
188         return W_FixWeaponOrder(order, 0);
189 }
190
191 string W_FixWeaponOrder_ForceComplete(string order)
192 {
193         if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
194         return W_FixWeaponOrder(order, 1);
195 }
196
197 void W_RandomWeapons(entity e, int n)
198 {
199 #ifdef SVQC
200         WepSet remaining = STAT(WEAPONS, e);
201 #else
202         WepSet remaining = e.weapons;
203 #endif
204         WepSet result = '0 0 0';
205         for (int j = 0; j < n; ++j)
206         {
207                 RandomSelection_Init();
208                 FOREACH(Weapons, it != WEP_Null, {
209                         if (remaining & (it.m_wepset))
210                                 RandomSelection_AddEnt(it, 1, 1);
211                 });
212                 Weapon w = RandomSelection_chosen_ent;
213                 result |= WepSet_FromWeapon(w);
214                 remaining &= ~WepSet_FromWeapon(w);
215         }
216 #ifdef SVQC
217         STAT(WEAPONS, e) = result;
218 #else
219         e.weapons = result;
220 #endif
221 }
222
223 string GetAmmoPicture(int ammotype)
224 {
225         switch (ammotype)
226         {
227                 case RESOURCE_SHELLS:  return ITEM_Shells.m_icon;
228                 case RESOURCE_BULLETS: return ITEM_Bullets.m_icon;
229                 case RESOURCE_ROCKETS: return ITEM_Rockets.m_icon;
230                 case RESOURCE_CELLS:   return ITEM_Cells.m_icon;
231                 case RESOURCE_PLASMA:  return ITEM_Plasma.m_icon;
232                 case RESOURCE_FUEL:    return ITEM_JetpackFuel.m_icon;
233                 default: return "";  // wtf, no ammo type?
234         }
235 }
236
237 #ifdef CSQC
238 int GetAmmoTypeFromNum(int i)
239 {
240         switch (i)
241         {
242                 case 0: return RESOURCE_SHELLS;
243                 case 1: return RESOURCE_BULLETS;
244                 case 2: return RESOURCE_ROCKETS;
245                 case 3: return RESOURCE_CELLS;
246                 case 4: return RESOURCE_PLASMA;
247                 case 5: return RESOURCE_FUEL;
248                 default: return RESOURCE_NONE;
249         }
250 }
251
252 int GetAmmoStat(int ammotype)
253 {
254         switch (ammotype)
255         {
256                 case RESOURCE_SHELLS: return STAT_SHELLS;
257                 case RESOURCE_BULLETS: return STAT_NAILS;
258                 case RESOURCE_ROCKETS: return STAT_ROCKETS;
259                 case RESOURCE_CELLS: return STAT_CELLS;
260                 case RESOURCE_PLASMA: return STAT_PLASMA.m_id;
261                 case RESOURCE_FUEL: return STAT_FUEL.m_id;
262                 default: return -1;
263         }
264 }
265 #endif
266
267 string W_Sound(string w_snd)
268 {
269         string output = strcat("weapons/", w_snd);
270         MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
271         return M_ARGV(1, string);
272 }
273
274 string W_Model(string w_mdl)
275 {
276         string output = strcat("models/weapons/", w_mdl);
277         MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
278         return M_ARGV(1, string);
279 }
280
281 #ifdef GAMEQC
282 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
283 {
284         switch (algn)
285         {
286                 default:
287                 case 3:
288                         // right alignment
289                         break;
290                 case 4:
291                         // left
292                         vecs.y = -vecs.y;
293                         break;
294                 case 1:
295                 case 2:
296                         // center
297                         vecs.y = 0;
298                         vecs.z -= 2;
299                         break;
300         }
301         return vecs;
302 }
303
304 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
305 {
306         string s;
307         if (visual)
308         {
309                 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
310         }
311         else if (STAT(SHOOTFROMEYE))
312         {
313                 vecs.y = vecs.z = 0;
314         }
315         else if (STAT(SHOOTFROMCENTER))
316         {
317                 vecs.y = 0;
318                 vecs.z -= 2;
319         }
320         else if ((s = G_SHOOTFROMFIXEDORIGIN) != "")
321         {
322                 vector v = stov(s);
323                 if (y_is_right) v.y = -v.y;
324                 if (v.x != 0) vecs.x = v.x;
325                 vecs.y = v.y;
326                 vecs.z = v.z;
327         }
328         else  // just do the same as top
329         {
330                 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
331         }
332
333         return vecs;
334 }
335
336 #define shotorg_adjust shotorg_adjust_values
337
338 /**
339  * supported formats:
340  *
341  * 1. simple animated model, muzzle flash handling on h_ model:
342  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
343  *      tags:
344  *        shot = muzzle end (shot origin, also used for muzzle flashes)
345  *        shell = casings ejection point (must be on the right hand side of the gun)
346  *        weapon = attachment for v_tuba.md3
347  *    v_tuba.md3 - first and third person model
348  *    g_tuba.md3 - pickup model
349  *
350  * 2. simple animated model, muzzle flash handling on v_ model:
351  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
352  *      tags:
353  *        weapon = attachment for v_tuba.md3
354  *    v_tuba.md3 - first and third person model
355  *      tags:
356  *        shot = muzzle end (shot origin, also used for muzzle flashes)
357  *        shell = casings ejection point (must be on the right hand side of the gun)
358  *    g_tuba.md3 - pickup model
359  *
360  * 3. fully animated model, muzzle flash handling on h_ model:
361  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
362  *      tags:
363  *        shot = muzzle end (shot origin, also used for muzzle flashes)
364  *        shell = casings ejection point (must be on the right hand side of the gun)
365  *        handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
366  *    v_tuba.md3 - third person model
367  *    g_tuba.md3 - pickup model
368  *
369  * 4. fully animated model, muzzle flash handling on v_ model:
370  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
371  *      tags:
372  *        shot = muzzle end (shot origin)
373  *        shell = casings ejection point (must be on the right hand side of the gun)
374  *    v_tuba.md3 - third person model
375  *      tags:
376  *        shot = muzzle end (for muzzle flashes)
377  *    g_tuba.md3 - pickup model
378  *
379  * writes:
380  *   this.origin, this.angles
381  *   this.weaponchild
382  *   this.movedir, this.view_ofs
383  *   attachment stuff
384  *   anim stuff
385  * to free:
386  *   call again with ""
387  *   remove the ent
388  */
389 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim)
390 {
391         if (name == "")
392         {
393                 vector oldmin = this.mins, oldmax = this.maxs;
394                 setmodel(this, MDL_Null);
395                 setsize(this, oldmin, oldmax);
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';
405         }
406         else
407         {
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"));
413
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');
420
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
423                 string t;
424                 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
425                 {
426                         if (!this.weaponchild)
427                         {
428                                 this.weaponchild = new(weaponchild);
429 #ifdef CSQC
430                                 this.weaponchild.drawmask = MASK_NORMAL;
431                                 this.weaponchild.renderflags |= RF_VIEWMODEL;
432 #endif
433                         }
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);
437                 }
438                 else
439                 {
440                         if (this.weaponchild) delete(this.weaponchild);
441                         this.weaponchild = NULL;
442                 }
443
444                 setsize(this, '0 0 0', '0 0 0');
445                 setorigin(this, '0 0 0');
446                 this.angles = '0 0 0';
447                 this.frame = 0;
448 #ifdef SVQC
449                 this.viewmodelforclient = NULL;
450 #else
451                 this.renderflags &= ~RF_VIEWMODEL;
452 #endif
453                 if (v_shot_idx)  // v_ model attached to invisible h_ model
454                 {
455                         this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
456                 }
457                 else
458                 {
459                         int idx;
460                         if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
461                         {
462                                 this.movedir = gettaginfo(this, idx);
463                         }
464                         else
465                         {
466                                 LOG_WARNF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong",
467                                         this.model);
468                                 this.movedir = '0 0 0';
469                         }
470                 }
471                 {
472                         int idx = 0;
473                         // v_ model attached to invisible h_ model
474                         if (this.weaponchild
475                             && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
476                         {
477                                 this.spawnorigin = gettaginfo(this.weaponchild, idx);
478                         }
479                         else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
480                         {
481                                 this.spawnorigin = gettaginfo(this, idx);
482                         }
483                         else
484                         {
485                                 LOG_WARNF("weapon model %s does not support the 'shell' tag, will display casings wrong",
486                                         this.model);
487                                 this.spawnorigin = this.movedir;
488                         }
489                 }
490                 if (v_shot_idx)
491                 {
492                         this.oldorigin = '0 0 0';  // use regular attachment
493                 }
494                 else
495                 {
496                         int idx;
497                         if (this.weaponchild)
498                                 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
499                         else
500                                 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
501                         if (idx)
502                         {
503                                 this.oldorigin = this.movedir - gettaginfo(this, idx);
504                         }
505                         else
506                         {
507                                 LOG_WARNF(
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",
511                                         this.model);
512                                 this.oldorigin = '0 0 0';  // there is no way to recover from this
513                         }
514                 }
515
516 #ifdef SVQC
517                 this.viewmodelforclient = this.owner;
518 #else
519                 this.renderflags |= RF_VIEWMODEL;
520 #endif
521         }
522
523         this.view_ofs = '0 0 0';
524
525         if (this.movedir.x >= 0)
526         {
527                 //int algn = STAT(GUNALIGN, this.owner);
528                 int algn = W_GunAlign(this, STAT(GUNALIGN, this.owner));
529         #ifdef SVQC
530                 this.m_gunalign = algn;
531         #endif
532                 vector v = this.movedir;
533                 this.movedir = shotorg_adjust(v, false, false, algn);
534                 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
535         }
536         int compressed_shotorg = compressShotOrigin(this.movedir);
537         // make them match perfectly
538 #ifdef SVQC
539     // null during init
540     if (this.owner) STAT(SHOTORG, this.owner) = compressed_shotorg;
541         this.movedir = decompressShotOrigin(compressed_shotorg);
542 #else
543         this.movedir = decompressShotOrigin(compressed_shotorg);
544 #endif
545
546         this.spawnorigin += this.view_ofs;  // offset the casings origin by the same amount
547
548         // check if an instant weapon switch occurred
549         setorigin(this, this.view_ofs);
550         if (!_anim) return;
551         // reset animstate now
552         this.wframe = WFRAME_IDLE;
553         setanim(this, this.anim_idle, true, false, true);
554 }
555 #endif
556
557 #ifdef GAMEQC
558
559 REGISTER_NET_TEMP(wframe)
560 #ifdef CSQC
561 NET_HANDLE(wframe, bool isNew)
562 {
563         vector a = ReadVector();
564     int slot = ReadByte();
565         bool restartanim = ReadByte();
566         entity wepent = viewmodels[slot];
567         if(a.x == wepent.anim_idle_x) // we don't need to enforce idle animation
568                 wepent.animstate_looping = false;
569         else
570                 anim_set(wepent, a, !restartanim, restartanim, restartanim);
571         wepent.state = ReadByte();
572         wepent.weapon_nextthink = ReadFloat();
573         switch (wepent.state)
574         {
575                 case WS_RAISE:
576                         wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_raise;
577                         break;
578                 case WS_DROP:
579                         wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_drop;
580                         break;
581         default:
582             wepent.weapon_switchdelay = 0;
583             break;
584         }
585         return true;
586 }
587 #endif
588
589 #ifdef SVQC
590 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim)
591 {
592         if (!IS_REAL_CLIENT(actor)) return;
593         int channel = MSG_ONE;
594         msg_entity = actor;
595         WriteHeader(channel, wframe);
596         WriteVector(channel, a);
597         WriteByte(channel, weaponslot(weaponentity.weaponentity_fld));
598         WriteByte(channel, restartanim);
599         WriteByte(channel, weaponentity.state);
600         WriteFloat(channel, weaponentity.weapon_nextthink);
601 }
602 #endif
603
604 REGISTER_NET_C2S(w_whereis)
605 #ifdef SVQC
606 void Weapon_whereis(Weapon this, entity cl);
607 NET_HANDLE(w_whereis, bool)
608 {
609         Weapon wpn = ReadRegistered(Weapons);
610         if (wpn != WEP_Null) Weapon_whereis(wpn, sender);
611         return true;
612 }
613 #else
614 void w_whereis(Weapon this)
615 {
616         int channel = MSG_C2S;
617         WriteHeader(channel, w_whereis);
618         WriteRegistered(Weapons, channel, this);
619 }
620 CLIENT_COMMAND(weapon_find, "Show spawn locations of a weapon")
621 {
622         switch (request)
623         {
624                 case CMD_REQUEST_COMMAND:
625                 {
626                         string s = argv(1);
627                         if (s == "all")
628                         {
629                                 FOREACH(Weapons, it != WEP_Null, w_whereis(it));
630                                 return;
631                         }
632                         if (s == "unowned")
633                         {
634                                 FOREACH(Weapons, it != WEP_Null && !(STAT(WEAPONS) & it.m_wepset), w_whereis(it));
635                                 return;
636                         }
637                         FOREACH(Weapons, it != WEP_Null && it.netname == s,
638                         {
639                                 w_whereis(it);
640                                 return;
641                         });
642                 }
643                 default:
644                 {
645                         LOG_INFOF("Incorrect parameters for ^2%s^7", "weapon_find");
646                 }
647                 case CMD_REQUEST_USAGE:
648                 {
649                         LOG_INFO("Usage:^3 cl_cmd weapon_find weapon");
650                         LOG_INFO("  Where 'weapon' is the lowercase weapon name, 'all' or 'unowned'.");
651                         return;
652                 }
653         }
654 }
655 #endif
656
657
658 #endif
659
660 #endif