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