]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/all.qc
Merge branch 'master' into TimePath/csqc_viewmodels
[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 #endif
453                         }
454                         _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
455                         setattachment(this.weaponchild, this, t);
456                 }
457                 else
458                 {
459                         if (this.weaponchild) remove(this.weaponchild);
460                         this.weaponchild = NULL;
461                 }
462
463                 setorigin(this, '0 0 0');
464                 this.angles = '0 0 0';
465                 this.frame = 0;
466 #ifdef SVQC
467                 this.viewmodelforclient = NULL;
468 #else
469                 this.renderflags &= ~RF_VIEWMODEL;
470 #endif
471                 if (v_shot_idx)  // v_ model attached to invisible h_ model
472                 {
473                         this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
474                 }
475                 else
476                 {
477                         int idx;
478                         if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
479                         {
480                                 this.movedir = gettaginfo(this, idx);
481                         }
482                         else
483                         {
484                                 LOG_WARNINGF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong\n",
485                                         this.model);
486                                 this.movedir = '0 0 0';
487                         }
488                 }
489                 {
490                         int idx = 0;
491                         // v_ model attached to invisible h_ model
492                         if (this.weaponchild
493                             && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
494                         {
495                                 this.spawnorigin = gettaginfo(this.weaponchild, idx);
496                         }
497                         else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
498                         {
499                                 this.spawnorigin = gettaginfo(this, idx);
500                         }
501                         else
502                         {
503                                 LOG_WARNINGF("weapon model %s does not support the 'shell' tag, will display casings wrong\n",
504                                         this.model);
505                                 this.spawnorigin = this.movedir;
506                         }
507                 }
508                 if (v_shot_idx)
509                 {
510                         this.oldorigin = '0 0 0';  // use regular attachment
511                 }
512                 else
513                 {
514                         int idx;
515                         if (this.weaponchild)
516                                 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
517                         else
518                                 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
519                         if (idx)
520                         {
521                                 this.oldorigin = this.movedir - gettaginfo(this, idx);
522                         }
523                         else
524                         {
525                                 LOG_WARNINGF(
526                                         "weapon model %s does not support the 'handle' tag "
527                                         "and neither does the v_ model support the 'shot' tag, "
528                                         "will display muzzle flashes TOTALLY wrong\n",
529                                         this.model);
530                                 this.oldorigin = '0 0 0';  // there is no way to recover from this
531                         }
532                 }
533
534 #ifdef SVQC
535                 this.viewmodelforclient = this.owner;
536 #else
537                 this.renderflags |= RF_VIEWMODEL;
538 #endif
539         }
540
541         this.view_ofs = '0 0 0';
542
543         if (this.movedir.x >= 0)
544         {
545 #ifdef SVQC
546                 int algn = this.owner.cvar_cl_gunalign;
547 #else
548                 int algn = autocvar_cl_gunalign;
549 #endif
550                 vector v = this.movedir;
551                 this.movedir = shotorg_adjust(v, false, false, algn);
552                 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
553         }
554         int compressed_shotorg = compressShotOrigin(this.movedir);
555         // make them match perfectly
556 #ifdef SVQC
557         this.movedir = decompressShotOrigin(this.owner.stat_shotorg = compressed_shotorg);
558 #else
559         this.movedir = decompressShotOrigin(compressed_shotorg);
560 #endif
561
562         this.spawnorigin += this.view_ofs;  // offset the casings origin by the same amount
563
564         // check if an instant weapon switch occurred
565         setorigin(this, this.view_ofs);
566         // reset animstate now
567         this.wframe = WFRAME_IDLE;
568         setanim(this, this.anim_idle, true, false, true);
569 }
570 #endif
571
572 #ifndef MENUQC
573
574 REGISTER_NET_TEMP(wframe)
575 #ifdef CSQC
576 NET_HANDLE(wframe, bool isNew)
577 {
578         vector a;
579         a.x = ReadCoord();
580     a.y = ReadCoord();
581     a.z = ReadCoord();
582         bool restartanim = ReadByte();
583         setanim(viewmodel, a, restartanim == false, restartanim, restartanim);
584         viewmodel.state = ReadByte();
585         viewmodel.alpha = ReadByte() / 255;
586         return true;
587 }
588 #endif
589
590 #ifdef SVQC
591 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim)
592 {
593         if (!IS_REAL_CLIENT(actor)) return;
594         int channel = MSG_ONE;
595         msg_entity = actor;
596         WriteHeader(channel, wframe);
597         WriteCoord(channel, a.x);
598         WriteCoord(channel, a.y);
599         WriteCoord(channel, a.z);
600         WriteByte(channel, restartanim);
601         WriteByte(channel, weaponentity.state);
602         WriteByte(channel, weaponentity.alpha * 255);
603 }
604 #endif
605
606 REGISTER_NET_TEMP(wglow)
607 #ifdef CSQC
608 NET_HANDLE(wglow, bool isNew)
609 {
610         vector g = '0 0 0';
611         g.x = ReadCoord();
612         g.y = ReadCoord();
613         g.z = ReadCoord();
614         viewmodel.glowmod = g;
615         return true;
616 }
617 #endif
618
619 #ifdef SVQC
620 void wglow_send(entity actor, vector g)
621 {
622         if (!IS_REAL_CLIENT(actor)) return;
623         int channel = MSG_ONE;
624         msg_entity = actor;
625         WriteHeader(channel, wglow);
626         WriteCoord(channel, g.x);
627         WriteCoord(channel, g.y);
628         WriteCoord(channel, g.z);
629 }
630 #endif
631
632 #endif
633
634 #endif