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