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