]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/weapons/all.qc
Merge branch 'master' into bones_was_here/q3compat
[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/autocvars.qh>
7         #include <client/main.qh>
8         #include <common/constants.qh>
9         #include <common/deathtypes/all.qh>
10         #include <common/physics/movetypes/movetypes.qh>
11         #include <common/stats.qh>
12         #include <common/util.qh>
13         #include <common/weapons/calculations.qc>
14         #include <common/weapons/weapon/_mod.inc>
15         #include <lib/csqcmodel/cl_model.qh>
16         #include <lib/csqcmodel/interpolate.qh>
17         #include <lib/warpzone/anglestransform.qh>
18         #include <lib/warpzone/client.qh>
19         #include <lib/warpzone/common.qh>
20 #elif defined(MENUQC)
21         #include <common/weapons/weapon/_mod.inc>
22 #elif defined(SVQC)
23         #include <common/constants.qh>
24         #include <common/deathtypes/all.qh>
25         #include <common/items/_mod.qh>
26         #include <common/mapinfo.qh>
27         #include <common/monsters/_mod.qh>
28         #include <common/notifications/all.qh>
29         #include <common/stats.qh>
30         #include <common/teams.qh>
31         #include <common/util.qh>
32         #include <common/weapons/calculations.qc>
33         #include <common/weapons/config.qc>
34         #include <common/weapons/config.qh>
35         #include <common/weapons/weapon/_mod.inc>
36         #include <lib/csqcmodel/sv_model.qh>
37         #include <lib/warpzone/anglestransform.qh>
38         #include <lib/warpzone/common.qh>
39         #include <lib/warpzone/server.qh>
40         #include <lib/warpzone/util_server.qh>
41         #include <server/autocvars.qh>
42         #include <server/command/_mod.qh>
43         #include <server/hook.qh>
44         #include <server/items/spawning.qh>
45         #include <server/mutators/_mod.qh>
46         #include <server/portals.qh>
47         #include <server/weapons/common.qh>
48         #include <server/weapons/csqcprojectile.qh>
49         #include <server/weapons/tracing.qh>
50 #endif
51
52
53 // WEAPON PLUGIN SYSTEM
54
55 WepSet _WepSet_FromWeapon(int a)
56 {
57         a -= WEP_FIRST;
58         if (REGISTRY_MAX(Weapons) > 24)
59                 if (a >= 24)
60                 {
61                         a -= 24;
62                         if (REGISTRY_MAX(Weapons) > 48)
63                                 if (a >= 24)
64                                 {
65                                         a -= 24;
66                                         return '0 0 1' * (2 ** a);
67                                 }
68                         return '0 1 0' * (2 ** a);
69                 }
70         return '1 0 0' * (2 ** a);
71 }
72 #ifdef SVQC
73         void WriteWepSet(float dst, WepSet w)
74         {
75                 if (REGISTRY_MAX(Weapons) > 48) WriteInt72_t(dst, w);
76                 else if (REGISTRY_MAX(Weapons) > 24) WriteInt48_t(dst, w);
77                 else WriteInt24_t(dst, w.x);
78         }
79 #endif
80 #ifdef CSQC
81         WepSet WepSet_GetFromStat()
82         {
83                 return STAT(WEAPONS);
84         }
85         WepSet WepSet_GetFromStat_InMap()
86         {
87                 return STAT(WEAPONSINMAP);
88         }
89         WepSet ReadWepSet()
90         {
91                 if (REGISTRY_MAX(Weapons) > 48) return ReadInt72_t();
92                 if (REGISTRY_MAX(Weapons) > 24) return ReadInt48_t();
93                 return ReadInt24_t() * '1 0 0';
94         }
95 #endif
96
97 string W_FixWeaponOrder(string order, float complete)
98 {
99         return fixPriorityList(order, WEP_FIRST, WEP_LAST, WEP_IMPULSE_BEGIN - WEP_FIRST, complete);
100 }
101 string W_NameWeaponOrder_MapFunc(string s)
102 {
103         int i = stof(s);
104         if (s == "0" || i)
105         {
106                 entity wi = REGISTRY_GET(Weapons, i);
107                 if (wi != WEP_Null) return wi.netname;
108         }
109         return s;
110 }
111
112 string W_UndeprecateName(string s)
113 {
114         switch (s)
115         {
116                 case "nex": return "vortex";
117                 case "rocketlauncher": return "devastator";
118                 case "laser": return "blaster";
119                 case "minstanex": return "vaporizer";
120                 case "grenadelauncher": return "mortar";
121                 case "uzi": return "machinegun";
122                 case "hmg": return "okhmg";
123                 case "rpc": return "okrpc";
124                 default: return s;
125         }
126 }
127 string W_NameWeaponOrder(string order)
128 {
129         return mapPriorityList(order, W_NameWeaponOrder_MapFunc);
130 }
131 string W_NumberWeaponOrder_MapFunc(string s)
132 {
133         if (s == "0" || stof(s)) return s;
134         s = W_UndeprecateName(s);
135         FOREACH(Weapons, it != WEP_Null && it.netname == s, return ftos(i));
136         return s;
137 }
138 string W_NumberWeaponOrder(string order)
139 {
140         return mapPriorityList(order, W_NumberWeaponOrder_MapFunc);
141 }
142
143 float W_FixWeaponOrder_BuildImpulseList_buf[REGISTRY_MAX(Weapons)];
144 string W_FixWeaponOrder_BuildImpulseList_order;
145 void W_FixWeaponOrder_BuildImpulseList_swap(int i, int j, entity pass)
146 {
147         float h;
148         h = W_FixWeaponOrder_BuildImpulseList_buf[i];
149         W_FixWeaponOrder_BuildImpulseList_buf[i] = W_FixWeaponOrder_BuildImpulseList_buf[j];
150         W_FixWeaponOrder_BuildImpulseList_buf[j] = h;
151 }
152 float W_FixWeaponOrder_BuildImpulseList_cmp(int i, int j, entity pass)
153 {
154         int si = W_FixWeaponOrder_BuildImpulseList_buf[i];
155         Weapon e1 = REGISTRY_GET(Weapons, si);
156         int sj = W_FixWeaponOrder_BuildImpulseList_buf[j];
157         Weapon e2 = REGISTRY_GET(Weapons, sj);
158         int d = (e1.impulse + 9) % 10 - (e2.impulse + 9) % 10;
159         if (d != 0) return -d;  // high impulse first!
160         string s = strcat(" ", W_FixWeaponOrder_BuildImpulseList_order, " ");
161         return strstrofs(s, sprintf(" %d ", si), 0)
162                 - strstrofs(s, sprintf(" %d ", sj), 0); // low char index first!
163 }
164 string W_FixWeaponOrder_BuildImpulseList(string o)
165 {
166         int i;
167         W_FixWeaponOrder_BuildImpulseList_order = o;
168         for (i = WEP_FIRST; i <= WEP_LAST; ++i)
169                 W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST] = i;
170         heapsort(WEP_LAST - WEP_FIRST + 1, W_FixWeaponOrder_BuildImpulseList_swap, W_FixWeaponOrder_BuildImpulseList_cmp,
171                 NULL);
172         o = "";
173         for (i = WEP_FIRST; i <= WEP_LAST; ++i)
174                 o = strcat(o, " ", ftos(W_FixWeaponOrder_BuildImpulseList_buf[i - WEP_FIRST]));
175         W_FixWeaponOrder_BuildImpulseList_order = string_null;
176         return substring(o, 1, -1);
177 }
178
179 string W_FixWeaponOrder_AllowIncomplete(entity this, string order)
180 {
181         return W_FixWeaponOrder(order, 0);
182 }
183
184 string W_FixWeaponOrder_ForceComplete(string order)
185 {
186         if (order == "") order = W_NumberWeaponOrder(cvar_defstring("cl_weaponpriority"));
187         return W_FixWeaponOrder(order, 1);
188 }
189
190 WepSet W_RandomWeapons(entity e, WepSet remaining, int n)
191 {
192         WepSet result = '0 0 0';
193         for (int j = 0; j < n; ++j)
194         {
195                 RandomSelection_Init();
196                 FOREACH(Weapons, it != WEP_Null, {
197                         if (remaining & (it.m_wepset))
198                                 RandomSelection_AddEnt(it, 1, 1);
199                 });
200                 Weapon w = RandomSelection_chosen_ent;
201                 result |= WepSet_FromWeapon(w);
202                 remaining &= ~WepSet_FromWeapon(w);
203         }
204         return result;
205 }
206
207 string GetAmmoPicture(int ammotype)
208 {
209         switch (ammotype)
210         {
211                 case RES_SHELLS:  return ITEM_Shells.m_icon;
212                 case RES_BULLETS: return ITEM_Bullets.m_icon;
213                 case RES_ROCKETS: return ITEM_Rockets.m_icon;
214                 case RES_CELLS:   return ITEM_Cells.m_icon;
215                 case RES_PLASMA:  return ITEM_Plasma.m_icon;
216                 case RES_FUEL:    return ITEM_JetpackFuel.m_icon;
217                 default: return "";  // wtf, no ammo type?
218         }
219 }
220
221 string GetAmmoName(int ammotype)
222 {
223         switch (ammotype)
224         {
225                 case RES_SHELLS:  return ITEM_Shells.m_name;
226                 case RES_BULLETS: return ITEM_Bullets.m_name;
227                 case RES_ROCKETS: return ITEM_Rockets.m_name;
228                 case RES_CELLS:   return ITEM_Cells.m_name;
229                 case RES_PLASMA:  return ITEM_Plasma.m_name;
230                 case RES_FUEL:    return ITEM_JetpackFuel.m_name;
231                 default:          return "batteries";
232         }
233 }
234
235 entity GetAmmoItem(int ammotype)
236 {
237         switch (ammotype)
238         {
239                 case RES_SHELLS:  return ITEM_Shells;
240                 case RES_BULLETS: return ITEM_Bullets;
241                 case RES_ROCKETS: return ITEM_Rockets;
242                 case RES_CELLS:   return ITEM_Cells;
243                 case RES_PLASMA:  return ITEM_Plasma;
244                 case RES_FUEL:    return ITEM_JetpackFuel;
245         }
246         LOG_WARNF("Invalid ammo type %d ", ammotype);
247         return NULL;
248         // WEAPONTODO: use this generic func to reduce duplication ?
249         // GetAmmoPicture  GetAmmoName  notif_arg_item_wepammo  ammo_pickupevalfunc ?
250 }
251
252 #ifdef CSQC
253 int GetAmmoTypeFromNum(int i)
254 {
255         switch (i)
256         {
257                 case 0: return RES_SHELLS;
258                 case 1: return RES_BULLETS;
259                 case 2: return RES_ROCKETS;
260                 case 3: return RES_CELLS;
261                 case 4: return RES_PLASMA;
262                 case 5: return RES_FUEL;
263                 default: return RES_NONE;
264         }
265 }
266
267 int GetAmmoStat(int ammotype)
268 {
269         switch (ammotype)
270         {
271                 case RES_SHELLS: return STAT_SHELLS;
272                 case RES_BULLETS: return STAT_NAILS;
273                 case RES_ROCKETS: return STAT_ROCKETS;
274                 case RES_CELLS: return STAT_CELLS;
275                 case RES_PLASMA: return STAT_PLASMA.m_id;
276                 case RES_FUEL: return STAT_FUEL.m_id;
277                 default: return -1;
278         }
279 }
280 #endif
281
282 string W_Sound(string w_snd)
283 {
284         string output = strcat("weapons/", w_snd);
285         MUTATOR_CALLHOOK(WeaponSound, w_snd, output);
286         return M_ARGV(1, string);
287 }
288
289 string W_Model(string w_mdl)
290 {
291         string output = strcat("models/weapons/", w_mdl);
292         MUTATOR_CALLHOOK(WeaponModel, w_mdl, output);
293         return M_ARGV(1, string);
294 }
295
296 #ifdef GAMEQC
297 vector shotorg_adjustfromclient(vector vecs, float y_is_right, float algn)
298 {
299         switch (algn)
300         {
301                 default:
302                 case 3:
303                         // right alignment
304                         break;
305                 case 4:
306                         // left
307                         vecs.y = -vecs.y;
308                         break;
309                 case 1:
310                 case 2:
311                         // center
312                         vecs.y = 0;
313                         vecs.z -= 2;
314                         break;
315         }
316         return vecs;
317 }
318
319 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn)
320 {
321         string s;
322         if (visual)
323         {
324                 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
325         }
326         else if (STAT(SHOOTFROMEYE))
327         {
328                 vecs.y = vecs.z = 0;
329         }
330         else if (STAT(SHOOTFROMCENTER))
331         {
332                 vecs.y = 0;
333                 vecs.z -= 2;
334         }
335         else if ((s = G_SHOOTFROMFIXEDORIGIN) != "")
336         {
337                 vector v = stov(s);
338                 if (y_is_right) v.y = -v.y;
339                 if (v.x != 0) vecs.x = v.x;
340                 vecs.y = v.y;
341                 vecs.z = v.z;
342         }
343         else  // just do the same as top
344         {
345                 vecs = shotorg_adjustfromclient(vecs, y_is_right, algn);
346         }
347
348         return vecs;
349 }
350
351 #define shotorg_adjust shotorg_adjust_values
352
353 /**
354  * supported formats:
355  *
356  * 1. simple animated model, muzzle flash handling on h_ model:
357  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
358  *      tags:
359  *        shot = muzzle end (shot origin, also used for muzzle flashes)
360  *        shell = casings ejection point (must be on the right hand side of the gun)
361  *        weapon = attachment for v_tuba.md3
362  *    v_tuba.md3 - first and third person model
363  *    g_tuba.md3 - pickup model
364  *
365  * 2. simple animated model, muzzle flash handling on v_ model:
366  *    h_tuba.dpm, h_tuba.dpm.framegroups - invisible model controlling the animation
367  *      tags:
368  *        weapon = attachment for v_tuba.md3
369  *    v_tuba.md3 - first and third person model
370  *      tags:
371  *        shot = muzzle end (shot origin, also used for muzzle flashes)
372  *        shell = casings ejection point (must be on the right hand side of the gun)
373  *    g_tuba.md3 - pickup model
374  *
375  * 3. fully animated model, muzzle flash handling on h_ model:
376  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
377  *      tags:
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  *        handle = corresponding to the origin of v_tuba.md3 (used for muzzle flashes)
381  *    v_tuba.md3 - third person model
382  *    g_tuba.md3 - pickup model
383  *
384  * 4. fully animated model, muzzle flash handling on v_ model:
385  *    h_tuba.dpm, h_tuba.dpm.framegroups - animated first person model
386  *      tags:
387  *        shot = muzzle end (shot origin)
388  *        shell = casings ejection point (must be on the right hand side of the gun)
389  *    v_tuba.md3 - third person model
390  *      tags:
391  *        shot = muzzle end (for muzzle flashes)
392  *    g_tuba.md3 - pickup model
393  *
394  * writes:
395  *   this.origin, this.angles
396  *   this.weaponchild
397  *   this.movedir, this.view_ofs, this.movedir_aligned
398  *   attachment stuff
399  *   anim stuff
400  * to free:
401  *   call again with ""
402  *   remove the ent
403  */
404 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim)
405 {
406         if (name == "")
407         {
408                 vector oldmin = this.mins, oldmax = this.maxs;
409                 setmodel(this, MDL_Null);
410                 setsize(this, oldmin, oldmax);
411                 if (this.weaponchild) delete(this.weaponchild);
412                 this.weaponchild = NULL;
413                 this.movedir = '0 0 0';
414                 this.spawnorigin = '0 0 0';
415                 this.oldorigin = '0 0 0';
416                 this.anim_fire1  = '0 1 0.01';
417                 this.anim_fire2  = '0 1 0.01';
418                 this.anim_idle   = '0 1 0.01';
419                 this.anim_reload = '0 1 0.01';
420         }
421         else
422         {
423                 // if there is a child entity, hide it until we're sure we use it
424                 if (this.weaponchild) this.weaponchild.model = "";
425                 _setmodel(this, W_Model(strcat("v_", name, ".md3")));
426                 int v_shot_idx;  // used later
427                 (v_shot_idx = gettagindex(this, "shot")) || (v_shot_idx = gettagindex(this, "tag_shot"));
428
429                 _setmodel(this, W_Model(strcat("h_", name, ".iqm")));
430                 // preset some defaults that work great for renamed zym files (which don't need an animinfo)
431                 this.anim_fire1  = animfixfps(this, '0 1 0.01', '0 0 0');
432                 this.anim_fire2  = animfixfps(this, '1 1 0.01', '0 0 0');
433                 this.anim_idle   = animfixfps(this, '2 1 0.01', '0 0 0');
434                 this.anim_reload = animfixfps(this, '3 1 0.01', '0 0 0');
435
436                 // if we have a "weapon" tag, let's attach the v_ model to it ("invisible hand" style model)
437                 // if we don't, this is a "real" animated model
438                 string t;
439                 if ((t = "weapon", gettagindex(this, t)) || (t = "tag_weapon", gettagindex(this, t)))
440                 {
441                         if (!this.weaponchild)
442                         {
443                                 this.weaponchild = new(weaponchild);
444 #ifdef CSQC
445                                 this.weaponchild.drawmask = MASK_NORMAL;
446                                 this.weaponchild.renderflags |= RF_VIEWMODEL;
447 #endif
448                         }
449                         _setmodel(this.weaponchild, W_Model(strcat("v_", name, ".md3")));
450                         setsize(this.weaponchild, '0 0 0', '0 0 0');
451                         setattachment(this.weaponchild, this, t);
452                 }
453                 else
454                 {
455                         if (this.weaponchild) delete(this.weaponchild);
456                         this.weaponchild = NULL;
457                 }
458
459                 setsize(this, '0 0 0', '0 0 0');
460                 setorigin(this, '0 0 0');
461                 this.angles = '0 0 0';
462                 this.frame = 0;
463 #ifdef SVQC
464                 this.viewmodelforclient = NULL;
465 #else
466                 this.renderflags &= ~RF_VIEWMODEL;
467 #endif
468                 if (v_shot_idx)  // v_ model attached to invisible h_ model
469                 {
470                         this.movedir = gettaginfo(this.weaponchild, v_shot_idx);
471                 }
472                 else
473                 {
474                         int idx;
475                         if ((idx = gettagindex(this, "shot")) || (idx = gettagindex(this, "tag_shot")))
476                         {
477                                 this.movedir = gettaginfo(this, idx);
478                         }
479                         else
480                         {
481                                 LOG_WARNF("weapon model %s does not support the 'shot' tag, will display shots TOTALLY wrong",
482                                         this.model);
483                                 this.movedir = '0 0 0';
484                         }
485                 }
486                 {
487                         int idx = 0;
488                         // v_ model attached to invisible h_ model
489                         if (this.weaponchild
490                                 && ((idx = gettagindex(this.weaponchild, "shell")) || (idx = gettagindex(this.weaponchild, "tag_shell"))))
491                         {
492                                 this.spawnorigin = gettaginfo(this.weaponchild, idx);
493                         }
494                         else if ((idx = gettagindex(this, "shell")) || (idx = gettagindex(this, "tag_shell")))
495                         {
496                                 this.spawnorigin = gettaginfo(this, idx);
497                         }
498                         else
499                         {
500                                 LOG_WARNF("weapon model %s does not support the 'shell' tag, will display casings wrong",
501                                         this.model);
502                                 this.spawnorigin = this.movedir;
503                         }
504                 }
505                 if (v_shot_idx)
506                 {
507                         this.oldorigin = '0 0 0';  // use regular attachment
508                 }
509                 else
510                 {
511                         int idx;
512                         if (this.weaponchild)
513                                 (idx = gettagindex(this, "weapon")) || (idx = gettagindex(this, "tag_weapon"));
514                         else
515                                 (idx = gettagindex(this, "handle")) || (idx = gettagindex(this, "tag_handle"));
516                         if (idx)
517                         {
518                                 this.oldorigin = this.movedir - gettaginfo(this, idx);
519                         }
520                         else
521                         {
522                                 LOG_WARNF(
523                                         "weapon model %s does not support the 'handle' tag "
524                                         "and neither does the v_ model support the 'shot' tag, "
525                                         "will display muzzle flashes TOTALLY wrong\n",
526                                         this.model);
527                                 this.oldorigin = '0 0 0';  // there is no way to recover from this
528                         }
529                 }
530
531 #ifdef SVQC
532                 this.viewmodelforclient = this.owner;
533 #else
534                 this.renderflags |= RF_VIEWMODEL;
535 #endif
536         }
537
538         this.view_ofs = '0 0 0';
539         this.movedir_aligned = this.movedir;
540
541         if (this.movedir.x >= 0)
542         {
543                 //int algn = STAT(GUNALIGN, this.owner);
544                 int algn = W_GunAlign(this, STAT(GUNALIGN, this.owner));
545         #ifdef SVQC
546                 this.m_gunalign = algn;
547         #endif
548                 vector v = this.movedir;
549                 this.movedir = shotorg_adjust(v, false, false, algn);
550                 this.movedir_aligned = shotorg_adjust(v, false, true, algn);
551                 this.view_ofs = shotorg_adjust(v, false, true, algn) - v;
552         }
553         int compressed_shotorg = compressShotOrigin(this.movedir);
554         // make them match perfectly
555 #ifdef SVQC
556         // null during init
557         if (this.owner) STAT(SHOTORG, this.owner) = compressed_shotorg;
558 #endif
559         this.movedir = decompressShotOrigin(compressed_shotorg);
560
561         this.spawnorigin += this.view_ofs;  // offset the casings origin by the same amount
562
563         // check if an instant weapon switch occurred
564         setorigin(this, this.view_ofs);
565         if (!_anim) return;
566         // reset animstate now
567         this.wframe = WFRAME_IDLE;
568         setanim(this, this.anim_idle, true, false, true);
569 }
570 #endif
571
572 #ifdef GAMEQC
573
574 REGISTER_NET_TEMP(wframe)
575 #ifdef CSQC
576 NET_HANDLE(wframe, bool isNew)
577 {
578         WFRAME fr = ReadByte();
579         float t = ReadFloat();
580         int slot = ReadByte();
581         bool restartanim = ReadByte();
582         entity wepent = viewmodels[slot];
583         if(fr == WFRAME_IDLE)
584                 wepent.animstate_looping = false; // we don't need to enforce idle animation
585         else
586         {
587                 vector a = '0 0 0';
588                 switch(fr)
589                 {
590                         default:
591                         case WFRAME_IDLE: a = wepent.anim_idle; break;
592                         case WFRAME_FIRE1: a = wepent.anim_fire1; break;
593                         case WFRAME_FIRE2: a = wepent.anim_fire2; break;
594                         case WFRAME_RELOAD: a = wepent.anim_reload; break;
595                 }
596                 a.z *= t;
597                 anim_set(wepent, a, !restartanim, restartanim, restartanim);
598         }
599         wepent.state = ReadByte();
600         wepent.weapon_nextthink = ReadFloat();
601         switch (wepent.state)
602         {
603                 case WS_RAISE:
604                         wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_raise;
605                         break;
606                 case WS_DROP:
607                         wepent.weapon_switchdelay = wepent.activeweapon.switchdelay_drop;
608                         break;
609                 default:
610                         wepent.weapon_switchdelay = 0;
611                         break;
612         }
613         return true;
614 }
615 #endif
616
617 #ifdef SVQC
618 void wframe_send(entity actor, entity weaponentity, int wepframe, float attackrate, bool restartanim)
619 {
620         if (!IS_REAL_CLIENT(actor)) return;
621         int channel = MSG_ONE;
622         msg_entity = actor;
623         WriteHeader(channel, wframe);
624         WriteByte(channel, wepframe);
625         WriteFloat(channel, attackrate);
626         WriteByte(channel, weaponslot(weaponentity.weaponentity_fld));
627         WriteByte(channel, restartanim);
628         WriteByte(channel, weaponentity.state);
629         WriteFloat(channel, weaponentity.weapon_nextthink);
630 }
631 #endif
632
633 REGISTER_NET_C2S(w_whereis)
634 #ifdef SVQC
635 void Weapon_whereis(Weapon this, entity cl);
636 NET_HANDLE(w_whereis, bool)
637 {
638         Weapon wpn = ReadRegistered(Weapons);
639         if (wpn != WEP_Null) Weapon_whereis(wpn, sender);
640         return true;
641 }
642 #else
643 void w_whereis(Weapon this)
644 {
645         int channel = MSG_C2S;
646         WriteHeader(channel, w_whereis);
647         WriteRegistered(Weapons, channel, this);
648 }
649 CLIENT_COMMAND(weapon_find, "Show spawn locations of a weapon")
650 {
651         switch (request)
652         {
653                 case CMD_REQUEST_COMMAND:
654                 {
655                         string s = argv(1);
656                         if (s == "all")
657                         {
658                                 FOREACH(Weapons, it != WEP_Null, w_whereis(it));
659                                 return;
660                         }
661                         if (s == "unowned")
662                         {
663                                 FOREACH(Weapons, it != WEP_Null && !(STAT(WEAPONS) & it.m_wepset), w_whereis(it));
664                                 return;
665                         }
666                         FOREACH(Weapons, it != WEP_Null && it.netname == s,
667                         {
668                                 w_whereis(it);
669                                 return;
670                         });
671                 }
672                 default:
673                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
674                 case CMD_REQUEST_USAGE:
675                 {
676                         LOG_HELP("Usage:^3 cl_cmd weapon_find weapon");
677                         LOG_HELP("  Where 'weapon' is the lowercase weapon name, 'all' or 'unowned'.");
678                         return;
679                 }
680         }
681 }
682 #endif
683
684 #ifdef SVQC
685 void W_MuzzleFlash_Model_AttachToShotorg(entity actor, .entity weaponentity, entity flash, vector offset)
686 {
687         flash.owner = actor;
688         flash.angles_z = random() * 360;
689
690         entity view = actor.(weaponentity);
691         entity exterior = actor.exteriorweaponentity;
692
693         if (view.oldorigin.x > 0)
694         {
695                 setattachment(flash, exterior, "");
696                 setorigin(flash, view.oldorigin + offset);
697         }
698         else
699         {
700                 if (gettagindex(exterior, "shot")) setattachment(flash, exterior, "shot");
701                 else setattachment(flash, exterior, "tag_shot");
702                 setorigin(flash, offset);
703         }
704 }
705 #elif defined(CSQC)
706 void W_MuzzleFlash_Model_AttachToShotorg(entity wepent, entity flash, vector offset)
707 {
708         flash.owner = wepent;
709         flash.angles_z = random() * 360;
710
711         if (gettagindex(wepent, "shot")) setattachment(flash, wepent, "shot");
712         else setattachment(flash, wepent, "tag_shot");
713         setorigin(flash, offset);
714 }
715 #endif
716
717 void W_MuzzleFlash_Model_Think(entity this)
718 {
719         this.frame += 2;
720         this.scale *= 0.5;
721         this.alpha -= 0.25;
722         this.nextthink = time + 0.05;
723
724         if(this.alpha <= 0)
725         {
726                 setthink(this, SUB_Remove);
727                 this.nextthink = time;
728                 this.realowner.muzzle_flash = NULL;
729                 return;
730         }
731 }
732
733 void W_MuzzleFlash_Model(entity wepent, entity muzzlemodel)
734 {
735         if(wepent.muzzle_flash == NULL)
736                 wepent.muzzle_flash = spawn();
737
738         entity flash = wepent.muzzle_flash;
739         setmodel(flash, muzzlemodel); // precision set below
740
741         flash.scale = 0.75;
742         setthink(flash, W_MuzzleFlash_Model_Think);
743         flash.nextthink = time + 0.02;
744         flash.frame = 2;
745         flash.alpha = 0.75;
746         flash.angles_z = random() * 180;
747         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT;
748         flash.owner = flash.realowner = wepent;
749
750 #ifdef CSQC
751         flash.drawmask = MASK_NORMAL;
752 #endif
753 }
754
755 REGISTER_NET_TEMP(w_muzzleflash)
756
757 #ifdef SVQC
758 void W_MuzzleFlash(Weapon thiswep, entity actor, .entity weaponentity, vector shotorg, vector shotdir)
759 {
760         // don't show an exterior muzzle effect for the off-hand
761         if(weaponslot(weaponentity) == 0)
762         {
763                 Send_Effect_Except(thiswep.m_muzzleeffect, shotorg, shotdir * 1000, 1, actor);
764
765                 if(thiswep.m_muzzlemodel != MDL_Null)
766                 {
767                         W_MuzzleFlash_Model(actor.exteriorweaponentity, thiswep.m_muzzlemodel);
768                         W_MuzzleFlash_Model_AttachToShotorg(actor, weaponentity, actor.exteriorweaponentity.muzzle_flash, '5 0 0');
769                 }
770         }
771
772         FOREACH_CLIENT(it == actor || (IS_SPEC(it) && it.enemy == actor),
773         {
774                 if(!IS_REAL_CLIENT(it))
775                         continue;
776                 int channel = MSG_ONE;
777                 msg_entity = it;
778                 WriteHeader(channel, w_muzzleflash);
779                 WriteByte(channel, thiswep.m_id);
780                 WriteByte(channel, weaponslot(weaponentity));
781                 WriteVector(channel, shotorg);
782         });
783 }
784 #elif defined(CSQC)
785 NET_HANDLE(w_muzzleflash, bool isNew)
786 {
787         return = true;
788         int weapon_id = ReadByte();
789         int slot = ReadByte();
790         vector sv_shotorg = ReadVector();
791
792         Weapon thiswep = REGISTRY_GET(Weapons, weapon_id);
793         vector viewangles = getpropertyvec(VF_CL_VIEWANGLES);
794         vector forward, right, up;
795         MAKE_VECTORS(viewangles, forward, right, up);
796
797         if(autocvar_chase_active)
798         {
799                 // in third person mode, show the muzzle flash from the server side weapon position
800                 // we don't have a view model to reference in this case
801                 pointparticles(thiswep.m_muzzleeffect, sv_shotorg, forward * 1000, 1);
802                 return;
803         }
804         if(!autocvar_r_drawviewmodel) return;
805
806         entity wepent = viewmodels[slot];
807         // get the local player entity to calculate shot origin
808         entity rlplayer = CSQCModel_server2csqc(player_localentnum - 1);
809         if(!rlplayer)
810                 rlplayer = csqcplayer; // fall back to the global
811
812         vector md = wepent.movedir_aligned;
813         vector vecs = ((md.x > 0) ? md : '0 0 0');
814         vector dv = forward * vecs.x + right * -vecs.y + up * vecs.z;
815         vector org = rlplayer.origin + rlplayer.view_ofs + dv;
816
817         pointparticles(thiswep.m_muzzleeffect, org, forward * 1000, 1);
818
819         if(thiswep.m_muzzlemodel != MDL_Null)
820         {
821                 W_MuzzleFlash_Model(wepent, thiswep.m_muzzlemodel);
822                 W_MuzzleFlash_Model_AttachToShotorg(wepent, wepent.muzzle_flash, '5 0 0');
823         }
824 }
825 #endif
826
827
828 #endif
829
830 #endif