Viewmodels: cl_bobmodel: check onground for other players too
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / all.qc
1 #ifndef WEAPONS_ALL_C
2 #define WEAPONS_ALL_C
3
4 #include "all.qh"
5
6 #if defined(CSQC)
7         #include "../../client/defs.qh"
8         #include "../constants.qh"
9         #include "../stats.qh"
10         #include "../../lib/warpzone/anglestransform.qh"
11         #include "../../lib/warpzone/common.qh"
12         #include "../../lib/warpzone/client.qh"
13         #include "../util.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"
21 #elif defined(MENUQC)
22 #elif defined(SVQC)
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"
30         #include "../util.qh"
31         #include "../buffs/all.qh"
32         #include "../monsters/all.qh"
33         #include "config.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"
48 #endif
49 #ifndef MENUQC
50         #include "calculations.qc"
51 #endif
52 #define IMPLEMENTATION
53 #include "all.inc"
54 #undef IMPLEMENTATION
55
56 // WEAPON PLUGIN SYSTEM
57
58 WepSet WepSet_FromWeapon(int a)
59 {
60         a -= WEP_FIRST;
61         if (Weapons_MAX > 24)
62                 if (a >= 24)
63                 {
64                         a -= 24;
65                         if (Weapons_MAX > 48)
66                                 if (a >= 24)
67                                 {
68                                         a -= 24;
69                                         return '0 0 1' * power2of(a);
70                                 }
71                         return '0 1 0' * power2of(a);
72                 }
73         return '1 0 0' * power2of(a);
74 }
75 #ifdef SVQC
76         void WepSet_AddStat()
77         {
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);
81         }
82         void WepSet_AddStat_InMap()
83         {
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);
87         }
88         void WriteWepSet(float dst, WepSet w)
89         {
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);
93         }
94 #endif
95 #ifdef CSQC
96         WepSet WepSet_GetFromStat()
97         {
98                 WepSet w = '0 0 0';
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);
102                 return w;
103         }
104         WepSet WepSet_GetFromStat_InMap()
105         {
106                 WepSet w = '0 0 0';
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);
110                 return w;
111         }
112         WepSet ReadWepSet()
113         {
114                 if (Weapons_MAX > 48) return ReadInt72_t();
115                 if (Weapons_MAX > 24) return ReadInt48_t();
116                 return ReadInt24_t() * '1 0 0';
117         }
118 #endif
119
120 string W_FixWeaponOrder(string order, float complete)
121 {
122         return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
123 }
124 string W_NameWeaponOrder_MapFunc(string s)
125 {
126         entity wi;
127         if (s == "0" || stof(s))
128         {
129                 wi = get_weaponinfo(stof(s));
130                 if (wi != WEP_Null) return wi.netname;
131         }
132         return s;
133 }
134
135 string W_UndeprecateName(string s)
136 {
137         switch (s)
138         {
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";
145                 default: return s;
146         }
147 }
148 string W_NameWeaponOrder(string order)
149 {
150         return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
151 }
152 string W_NumberWeaponOrder_MapFunc(string s)
153 {
154         int i;
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);
159         return s;
160 }
161 string W_NumberWeaponOrder(string order)
162 {
163         return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
164 }
165
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)
169 {
170         float h;
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;
174 }
175 float W_FixWeaponOrder_BuildImpulseList_cmp(int i, int j, entity pass)
176 {
177         entity e1, e2;
178         float d;
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)
185                -
186                strstrofs(strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " "),
187                 sprintf(" %d ", W_FixWeaponOrder_BuildImpulseList_buf[j]), 0)
188         ;  // low char index first!
189 }
190 string W_FixWeaponOrder_BuildImpulseList(string o)
191 {
192         int i;
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,
197                 world);
198         o = "";
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);
203 }
204
205 string W_FixWeaponOrder_AllowIncomplete(string order)
206 {
207         return W_FixWeaponOrder(order, 0);
208 }
209
210 string W_FixWeaponOrder_ForceComplete(string order)
211 {
212         if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
213         return W_FixWeaponOrder(order, 1);
214 }
215
216 void W_RandomWeapons(entity e, float n)
217 {
218         int i, j;
219         WepSet remaining;
220         WepSet result;
221         remaining = e.weapons;
222         result = '0 0 0';
223         for (i = 0; i < n; ++i)
224         {
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);
230         }
231         e.weapons = result;
232 }
233
234 string GetAmmoPicture(.int ammotype)
235 {
236         switch (ammotype)
237         {
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?
245         }
246 }
247
248 #ifdef CSQC
249         .int GetAmmoFieldFromNum(int i)
250         {
251                 switch (i)
252                 {
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;
260                 }
261         }
262
263         int GetAmmoStat(.int ammotype)
264         {
265                 switch (ammotype)
266                 {
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;
273                         default: return -1;
274                 }
275         }
276 #endif
277
278 string W_Sound(string w_snd)
279 {
280         string output = strcat("weapons/", w_snd);
281 #ifdef SVQC
282                 MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
283                 return weapon_sound_output;
284 #else
285                 return output;
286 #endif
287 }
288
289 string W_Model(string w_mdl)
290 {
291         string output = strcat("models/weapons/", w_mdl);
292 #ifdef SVQC
293                 MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
294                 return weapon_model_output;
295 #else
296                 return output;
297 #endif
298 }
299
300 #ifndef MENUQC
301 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
302 {
303         switch (algn)
304         {
305                 default:
306                 case 3:
307                         // right alignment
308                         break;
309                 case 4:
310                         // left
311                         vecs.y = -vecs.y;
312                         break;
313                 case 1:
314                 case 2:
315                         // center
316                         vecs.y = 0;
317                         vecs.z -= 2;
318                         break;
319         }
320         return vecs;
321 }
322
323 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
324 {
325 #ifdef SVQC
326         string s;
327 #endif
328         if (visual)
329         {
330                 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
331         }
332 #ifdef SVQC
333         else if (autocvar_g_shootfromeye)
334         {
335                 vecs.y = vecs.z = 0;
336         }
337         else if (autocvar_g_shootfromcenter)
338         {
339                 vecs.y = 0;
340                 vecs.z -= 2;
341         }
342         else if ((s = autocvar_g_shootfromfixedorigin) != "")
343         {
344                 vector v = stov(s);
345                 if (y_is_right) v.y = -v.y;
346                 if (v.x != 0) vecs.x = v.x;
347                 vecs.y = v.y;
348                 vecs.z = v.z;
349         }
350 #endif
351         else  // just do the same as top
352         {
353                 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
354         }
355
356         return vecs;
357 }
358
359 #define shotorg_adjust shotorg_adjust_values
360
361 /**
362  * supported formats:
363  *
364  * 1. simple animated model, muzzle flash handling on h_ model:
365  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
366  *      tags:
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  *        weapon = attachment for v_tuba.md3
370  *    v_tuba.md3 - first and third person model
371  *    g_tuba.md3 - pickup model
372  *
373  * 2. simple animated model, muzzle flash handling on v_ model:
374  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
375  *      tags:
376  *        weapon = attachment for v_tuba.md3
377  *    v_tuba.md3 - first and third person model
378  *      tags:
379  *        shot = muzzle end (shot origin, also used for muzzle flashes)
380  *        shell = casings ejection point (must be on the right hand side of the gun)
381  *    g_tuba.md3 - pickup model
382  *
383  * 3. fully animated model, muzzle flash handling on h_ model:
384  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
385  *      tags:
386  *        shot = muzzle end (shot origin, also used for muzzle flashes)
387  *        shell = casings ejection point (must be on the right hand side of the gun)
388  *        handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
389  *    v_tuba.md3 - third person model
390  *    g_tuba.md3 - pickup model
391  *
392  * 4. fully animated model, muzzle flash handling on v_ model:
393  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
394  *      tags:
395  *        shot = muzzle end (shot origin)
396  *        shell = casings ejection point (must be on the right hand side of the gun)
397  *    v_tuba.md3 - third person model
398  *      tags:
399  *        shot = muzzle end (for muzzle flashes)
400  *    g_tuba.md3 - pickup model
401  *
402  * writes:
403  *   this.origin, this.angles
404  *   this.weaponchild
405  *   this.movedir, this.view_ofs
406  *   attachment stuff
407  *   anim stuff
408  * to free:
409  *   call again with ""
410  *   remove the ent
411  */
412 void CL_WeaponEntity_SetModel(entity this, string name)
413 {
414         if (name == "")
415         {
416                 this.model = "";
417                 if (this.weaponchild) remove(this.weaponchild);
418                 this.weaponchild = NULL;
419                 this.movedir = '0 0 0';
420                 this.spawnorigin = '0 0 0';
421                 this.oldorigin = '0 0 0';
422                 this.anim_fire1  = '0 1 0.01';
423                 this.anim_fire2  = '0 1 0.01';
424                 this.anim_idle   = '0 1 0.01';
425                 this.anim_reload = '0 1 0.01';
426         }
427         else
428         {
429                 // if there is a child entity, hide it until we're sure we use it
430                 if (this.weaponchild) this.weaponchild.model = "";
431                 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
432                 int v_shot_idx;  // used later
433                 (v_shot_idx = gettagindex(this, "shot")) || (v_shot_idx = gettagindex(this, "tag_shot"));
434
435                 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
436                 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
437                 this.anim_fire1  = animfixfps(this, '0 1 0.01', '0 0 0');
438                 this.anim_fire2  = animfixfps(this, '1 1 0.01', '0 0 0');
439                 this.anim_idle   = animfixfps(this, '2 1 0.01', '0 0 0');
440                 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
441
442                 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
443                 // if we don't, this is a "real" animated model
444                 string t;
445                 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
446                 {
447                         if (!this.weaponchild)
448                         {
449                                 this.weaponchild = new(weaponchild);
450 #ifdef CSQC
451                                 this.weaponchild.drawmask = MASK_NORMAL;
452                                 this.weaponchild.renderflags |= RF_VIEWMODEL;
453 #endif
454                         }
455                         _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
456                         setattachment(this.weaponchild, this, t);
457                 }
458                 else
459                 {
460                         if (this.weaponchild) remove(this.weaponchild);
461                         this.weaponchild = NULL;
462                 }
463
464                 setorigin(this, '0 0 0');
465                 this.angles = '0 0 0';
466                 this.frame = 0;
467 #ifdef SVQC
468                 this.viewmodelforclient = NULL;
469 #else
470                 this.renderflags &= ~RF_VIEWMODEL;
471 #endif
472                 if (v_shot_idx)  // v_ model attached to invisible h_ model
473                 {
474                         this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
475                 }
476                 else
477                 {
478                         int idx;
479                         if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
480                         {
481                                 this.movedir = gettaginfo(this, idx);
482                         }
483                         else
484                         {
485                                 LOG_WARNINGF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong\n",
486                                         this.model);
487                                 this.movedir = '0 0 0';
488                         }
489                 }
490                 {
491                         int idx = 0;
492                         // v_ model attached to invisible h_ model
493                         if (this.weaponchild
494                             && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
495                         {
496                                 this.spawnorigin = gettaginfo(this.weaponchild, idx);
497                         }
498                         else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
499                         {
500                                 this.spawnorigin = gettaginfo(this, idx);
501                         }
502                         else
503                         {
504                                 LOG_WARNINGF("weapon model %s does not support the 'shell' tag, will display casings wrong\n",
505                                         this.model);
506                                 this.spawnorigin = this.movedir;
507                         }
508                 }
509                 if (v_shot_idx)
510                 {
511                         this.oldorigin = '0 0 0';  // use regular attachment
512                 }
513                 else
514                 {
515                         int idx;
516                         if (this.weaponchild)
517                                 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
518                         else
519                                 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
520                         if (idx)
521                         {
522                                 this.oldorigin = this.movedir - gettaginfo(this, idx);
523                         }
524                         else
525                         {
526                                 LOG_WARNINGF(
527                                         "weapon model %s does not support the 'handle' tag "
528                                         "and neither does the v_ model support the 'shot' tag, "
529                                         "will display muzzle flashes TOTALLY wrong\n",
530                                         this.model);
531                                 this.oldorigin = '0 0 0';  // there is no way to recover from this
532                         }
533                 }
534
535 #ifdef SVQC
536                 this.viewmodelforclient = this.owner;
537 #else
538                 this.renderflags |= RF_VIEWMODEL;
539 #endif
540         }
541
542         this.view_ofs = '0 0 0';
543
544         if (this.movedir.x >= 0)
545         {
546 #ifdef SVQC
547                 int algn = this.owner.cvar_cl_gunalign;
548 #else
549                 int algn = autocvar_cl_gunalign;
550 #endif
551                 vector v = this.movedir;
552                 this.movedir = shotorg_adjust(v, false, false, algn);
553                 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
554         }
555         int compressed_shotorg = compressShotOrigin(this.movedir);
556         // make them match perfectly
557 #ifdef SVQC
558         this.movedir = decompressShotOrigin(this.owner.stat_shotorg = compressed_shotorg);
559 #else
560         this.movedir = decompressShotOrigin(compressed_shotorg);
561 #endif
562
563         this.spawnorigin += this.view_ofs;  // offset the casings origin by the same amount
564
565         // check if an instant weapon switch occurred
566         setorigin(this, this.view_ofs);
567         // reset animstate now
568         this.wframe = WFRAME_IDLE;
569         setanim(this, this.anim_idle, true, false, true);
570 }
571 #endif
572
573 #ifndef MENUQC
574
575 REGISTER_NET_TEMP(wframe)
576 #ifdef CSQC
577 NET_HANDLE(wframe, bool isNew)
578 {
579         vector a;
580         a.x = ReadCoord();
581     a.y = ReadCoord();
582     a.z = ReadCoord();
583         bool restartanim = ReadByte();
584         anim_set(viewmodel, a, !restartanim, restartanim, restartanim);
585         viewmodel.state = ReadByte();
586         viewmodel.alpha = ReadByte() / 255;
587         return true;
588 }
589 #endif
590
591 #ifdef SVQC
592 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim)
593 {
594         if (!IS_REAL_CLIENT(actor)) return;
595         int channel = MSG_ONE;
596         msg_entity = actor;
597         WriteHeader(channel, wframe);
598         WriteCoord(channel, a.x);
599         WriteCoord(channel, a.y);
600         WriteCoord(channel, a.z);
601         WriteByte(channel, restartanim);
602         WriteByte(channel, weaponentity.state);
603         WriteByte(channel, weaponentity.alpha * 255);
604 }
605 #endif
606
607 REGISTER_NET_TEMP(wglow)
608 #ifdef CSQC
609 NET_HANDLE(wglow, bool isNew)
610 {
611         vector g = '0 0 0';
612         g.x = ReadCoord();
613         g.y = ReadCoord();
614         g.z = ReadCoord();
615         viewmodel.glowmod = g;
616         return true;
617 }
618 #endif
619
620 #ifdef SVQC
621 void wglow_send(entity actor, vector g)
622 {
623         if (!IS_REAL_CLIENT(actor)) return;
624         int channel = MSG_ONE;
625         msg_entity = actor;
626         WriteHeader(channel, wglow);
627         WriteCoord(channel, g.x);
628         WriteCoord(channel, g.y);
629         WriteCoord(channel, g.z);
630 }
631 #endif
632
633 #endif
634
635 #endif