Compact weapon flag checks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / all.qh
1 #pragma once
2
3 #include <common/command/_mod.qh>
4 #include <common/stats.qh>
5 #include "config.qh"
6
7 #include "weapon.qh"
8
9 #ifdef SVQC
10 void WriteWepSet(float dest, WepSet w);
11 #endif
12
13 #ifdef CSQC
14 WepSet WepSet_GetFromStat();
15 WepSet WepSet_GetFromStat_InMap();
16 WepSet ReadWepSet();
17 #endif
18
19 #ifdef GAMEQC
20 #include "calculations.qh"
21 #include "projectiles.qh"
22 #include <common/models/all.qh>
23 #endif
24
25 #include <common/util.qh>
26
27 REGISTRY(Weapons, 72) // Increase as needed. Can be up to 72.
28 #define Weapons_from(i) _Weapons_from(i, WEP_Null)
29 REGISTER_REGISTRY(Weapons)
30 STATIC_INIT(WeaponPickup) { FOREACH(Weapons, true, it.m_pickup = NEW(WeaponPickup, it)); }
31
32 #ifdef SVQC
33 #include <server/bot/api.qh>
34 #endif
35
36 .WepSet m_wepset;
37 #define WEPSET(id) (WEP_##id.m_wepset)
38 #define WepSet_FromWeapon(it) ((it).m_wepset)
39 WepSet _WepSet_FromWeapon(int i);
40
41 GENERIC_COMMAND(dumpweapons, "Dump all weapons into weapons_dump.txt") // WEAPONTODO: make this work with other progs than just server
42 {
43     switch(request)
44     {
45         case CMD_REQUEST_COMMAND:
46         {
47             #ifdef SVQC
48             wep_config_file = -1;
49             wep_config_alsoprint = -1;
50             string filename = argv(1);
51
52             if(filename == "")
53             {
54                 filename = "weapons_dump.cfg";
55                 wep_config_alsoprint = false;
56             }
57             else if(filename == "-")
58             {
59                 filename = "weapons_dump.cfg";
60                 wep_config_alsoprint = true;
61             }
62             wep_config_file = fopen(filename, FILE_WRITE);
63
64             if(wep_config_file >= 0)
65             {
66                 Dump_Weapon_Settings();
67                 LOG_INFOF("Dumping weapons... File located in ^2data/data/%s^7.", filename);
68                 fclose(wep_config_file);
69                 wep_config_file = -1;
70                 wep_config_alsoprint = -1;
71             }
72             else
73             {
74                 LOG_INFOF("^1Error: ^7Could not open file '%s'!", filename);
75             }
76             #else
77             LOG_INFO(_("Weapons dump command only works with sv_cmd."));
78             #endif
79             return;
80         }
81
82         default:
83         case CMD_REQUEST_USAGE:
84         {
85             LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]");
86             LOG_INFO("  Where 'filename' is the file to write (default is weapons_dump.cfg),");
87             LOG_INFO("  if supplied with '-' output to console as well as default,");
88             LOG_INFO("  if left blank, it will only write to default.");
89             return;
90         }
91     }
92 }
93
94 #ifdef SVQC
95 entity W_PROP_reloader;
96 float autocvar_w_prop_interval = 5;
97 .void(Weapon this, int) wr_net;
98 void W_PROP_reload(int chan, entity to)
99 {
100     W_PROP_reloader.nextthink = time + autocvar_w_prop_interval;
101     msg_entity = to;
102     FOREACH(Weapons, true, {
103         it.wr_update(it);
104         void(Weapon, int) f = it.wr_net;
105         if (f) f(it, chan);
106     });
107 }
108 void W_PROP_think(entity this)
109 {
110     W_PROP_reload(MSG_ALL, NULL);
111 }
112 STATIC_INIT_LATE(W_PROP_reloader)
113 {
114     entity e = W_PROP_reloader = new_pure(W_PROP_reloader);
115     setthink(e, W_PROP_think);
116     W_PROP_think(e);
117 }
118 #endif
119
120 #define REGISTER_WEAPON(...) EVAL_REGISTER_WEAPON(OVERLOAD(REGISTER_WEAPON, __VA_ARGS__))
121 #define EVAL_REGISTER_WEAPON(...) __VA_ARGS__
122 #define REGISTER_WEAPON_2(id, inst) REGISTER(Weapons, WEP, id, m_id, inst)
123 /** TODO: deprecated - remove */
124 #define REGISTER_WEAPON_3(id, sname, inst) \
125     REGISTER_WEAPON_2(id, inst); \
126     [[alias("WEP_" #id)]] Weapon _wep_##sname
127
128 REGISTER_WEAPON(Null, NEW(Weapon));
129
130 Weapon Weapons_fromstr(string s)
131 {
132     FOREACH(Weapons, it != WEP_Null && it.netname == s, return it);
133     return WEP_Null;
134 }
135
136
137 #ifdef GAMEQC
138
139 // legacy w_prop mappings
140 #define X(fld, T) .T fld; .T wepvar_##fld = fld;
141 X(switchdelay_drop, float)
142 X(switchdelay_raise, float)
143 X(weaponreplace, string)
144 X(weaponstartoverride, float)
145 X(weaponstart, float)
146 X(weaponthrowable, float)
147 #ifdef SVQC
148 X(reload_ammo, float)
149 const .float reloading_ammo = reload_ammo;
150 X(reload_time, float)
151 const .float reloading_time = reload_time;
152 #endif
153 #undef X
154
155 #endif
156
157
158
159 #define W_PROPS(L, class, prefix) \
160     L(W_PROP_BEGIN, W_PROP, W_PROP_END, class, prefix) \
161     L(W_CONFIG_BEGIN, W_CONFIG, W_CONFIG_END, class, prefix) \
162     L(W_UPDATE_BEGIN, W_UPDATE, W_UPDATE_END, class, prefix) \
163     L(W_NET_BEGIN, W_NET, W_NET_END, class, prefix) \
164     /**/ \
165
166
167     #define W_PROP(class, wepname, fld, T, m)       W_PROP_##m(class, fld, T, wepname)
168     #define W_PROP_NONE(class, fld, T, wepname)     _W_PROP(class, fld, T, wepname)
169     #define W_PROP_PRI(class, fld, T, wepname)      _W_PROP(class, primary_##fld, T, wepname)
170     #define W_PROP_SEC(class, fld, T, wepname)      _W_PROP(class, secondary_##fld, T, wepname)
171     #define W_PROP_BOTH(class, fld, T, wepname) \
172             W_PROP_PRI(class, fld, T, wepname) \
173             W_PROP_SEC(class, fld, T, wepname)
174         #define W_PROP_BEGIN(class)
175                 #ifdef GAMEQC
176                         #define _W_PROP(class, fld, T, wepname) \
177                                 /* static */ T _W_PROP_CVAR(wepname, fld); \
178                                 ATTRIB(class, wepvar_##fld, T, _W_PROP_CVAR(wepname, fld));
179                         #define _W_PROP_CVAR(wepname, fld) autocvar_g_balance_##wepname##_##fld
180                 #else
181                         #define _W_PROP(class, fld, T, wepname)
182                         #define _W_PROP_CVAR(wepname, fld)
183                 #endif
184         #define W_PROP_END()
185
186
187
188     #define W_CONFIG(class, wepname, fld, T, m)     W_CONFIG_##m(class, fld, T, wepname)
189     #define W_CONFIG_NONE(class, fld, T, wepname)   _W_CONFIG(class, fld, T, wepname)
190     #define W_CONFIG_PRI(class, fld, T, wepname)    _W_CONFIG(class, primary_##fld, T, wepname)
191     #define W_CONFIG_SEC(class, fld, T, wepname)    _W_CONFIG(class, secondary_##fld, T, wepname)
192     #define W_CONFIG_BOTH(class, fld, T, wepname) \
193             W_CONFIG_PRI(class, fld, T, wepname) \
194             W_CONFIG_SEC(class, fld, T, wepname)
195         #ifdef SVQC
196                 #define W_CONFIG_BEGIN(class) METHOD(class, wr_config, void(class this)) {
197                         #define _W_CONFIG(class, fld, T, wepname) if (#wepname == this.netname) WEP_CONFIG_WRITE_CVARS(wepname, fld, T);
198                 #define W_CONFIG_END() }
199         #else
200                 #define W_CONFIG_BEGIN(class)
201                         #define _W_CONFIG(class, fld, T, wepname)
202                 #define W_CONFIG_END()
203         #endif
204
205
206     #define W_UPDATE(class, wepname, fld, T, m)     W_UPDATE_##m(class, fld, T, wepname)
207     #define W_UPDATE_NONE(class, fld, T, wepname)   _W_UPDATE(class, fld, T, wepname)
208     #define W_UPDATE_PRI(class, fld, T, wepname)    _W_UPDATE(class, primary_##fld, T, wepname)
209     #define W_UPDATE_SEC(class, fld, T, wepname)    _W_UPDATE(class, secondary_##fld, T, wepname)
210     #define W_UPDATE_BOTH(class, fld, T, wepname) \
211             W_UPDATE_PRI(class, fld, T, wepname) \
212             W_UPDATE_SEC(class, fld, T, wepname)
213         #ifdef GAMEQC
214                 .entity baseline, baseline_target;
215                 #define W_UPDATE_BEGIN(class) \
216                         METHOD(class, wr_update, void(class this)) \
217                         { \
218                                 noref entity b = this.baseline; \
219                                 if (!b) \
220                                 { \
221                                         b = this.baseline = new_pure(baseline); \
222                                         b.baseline_target = this; \
223                                 }
224                         #ifdef SVQC
225                                 #define _W_UPDATE(class, fld, T, wepname) \
226                                         { \
227                                                 T it = _W_PROP_CVAR(wepname, fld); \
228                                                 b.wepvar_##fld = this.wepvar_##fld; \
229                                                 this.wepvar_##fld = it; \
230                                         }
231                         #else
232                                 #define _W_UPDATE(class, fld, T, wepname)
233                         #endif
234                 #define W_UPDATE_END() }
235         #else
236                 #define W_UPDATE_BEGIN(class)
237                         #define _W_UPDATE(class, fld, T, wepname)
238                 #define W_UPDATE_END()
239         #endif
240
241
242     #define W_NET(class, wepname, fld, T, m)     W_NET_##m(class, fld, T, wepname)
243     #define W_NET_NONE(class, fld, T, wepname)   _W_NET(class, fld, T, wepname)
244     #define W_NET_PRI(class, fld, T, wepname)    _W_NET(class, primary_##fld, T, wepname)
245     #define W_NET_SEC(class, fld, T, wepname)    _W_NET(class, secondary_##fld, T, wepname)
246     #define W_NET_BOTH(class, fld, T, wepname) \
247             W_NET_PRI(class, fld, T, wepname) \
248             W_NET_SEC(class, fld, T, wepname)
249     #if defined(CSQC)
250         REGISTER_NET_TEMP(WeaponUpdate)
251         #define W_NET_BEGIN(class) METHOD(class, wr_net, void(class this, int i)) { int n = 0;
252             #define _W_NET(class, fld, T, wepname) \
253             { \
254                 if (++n == i) this.wepvar_##fld = Read_##T(); \
255             }
256             .void(Weapon this, int i) wr_net;
257             NET_HANDLE(WeaponUpdate, bool isnew)
258             {
259                 Weapon w = Weapons_from(ReadByte());
260                 for (int i; (i = ReadByte()); )
261                 {
262                     w.wr_net(w, i);
263                 }
264                 return true;
265             }
266         #define W_NET_END() }
267     #elif defined(SVQC)
268         REGISTER_NET_TEMP(WeaponUpdate)
269         #define W_NET_BEGIN(class) \
270             METHOD(class, wr_net, void(class this, int chan)) \
271             { \
272                 bool commit = false; \
273                 int i = 0;
274             #define _W_NET(class, fld, T, wepname) \
275                 { \
276                     ++i; \
277                     T it = this.wepvar_##fld; \
278                     if (chan == MSG_ONE || it != this.baseline.wepvar_##fld) \
279                     { \
280                         if (!commit) { commit = true; WriteHeader(chan, WeaponUpdate); WriteByte(chan, this.m_id); } \
281                         WriteByte(chan, i); Write_##T(chan, it); \
282                     } \
283                 }
284         #define W_NET_END() if (commit) WriteByte(chan, 0); }
285     #else
286         #define W_NET_BEGIN(class)
287             #define _W_NET(class, fld, T, wepname)
288         #define W_NET_END()
289     #endif
290
291
292
293 // read cvars from weapon settings
294 #define WEP_CVAR(wepname, name) (_wep_##wepname.wepvar_##name)
295 #define WEP_CVAR_PRI(wepname, name) WEP_CVAR(wepname, primary_##name)
296 #define WEP_CVAR_SEC(wepname, name) WEP_CVAR(wepname, secondary_##name)
297 #define WEP_CVAR_BOTH(wepname, isprimary, name) ((isprimary) ? WEP_CVAR_PRI(wepname, name) : WEP_CVAR_SEC(wepname, name))
298
299 const int WEP_FIRST = 1;
300 #define WEP_LAST (Weapons_COUNT - 1)
301 WepSet WEPSET_ALL;
302 WepSet WEPSET_SUPERWEAPONS;
303
304 #include "all.inc"
305
306 // TODO: remove after 0.8.2. Retains impulse number compatibility because 0.8.1 clients don't reload the weapons.cfg
307 #define WEP_HARDCODED_IMPULSES 20
308
309 // TODO: invert after 0.8.2. Will require moving 'best weapon' impulses
310 #define WEP_IMPULSE_BEGIN 230
311 #define WEP_IMPULSE_END bound(WEP_IMPULSE_BEGIN, WEP_IMPULSE_BEGIN + (Weapons_COUNT - 1) - 1, 253)
312
313 REGISTRY_SORT(Weapons, WEP_HARDCODED_IMPULSES + 1)
314 REGISTRY_CHECK(Weapons)
315
316 STATIC_INIT(register_weapons_done)
317 {
318         string inaccessible = "";
319     FOREACH(Weapons, true, {
320         WepSet set = it.m_wepset = _WepSet_FromWeapon(it.m_id = i);
321         WEPSET_ALL |= set;
322         if (it.spawnflags & WEP_FLAG_SUPERWEAPON) WEPSET_SUPERWEAPONS |= set;
323         if (it == WEP_Null) continue;
324         int imp = WEP_IMPULSE_BEGIN + it.m_id - 1;
325         if (imp <= WEP_IMPULSE_END)
326             localcmd(sprintf("alias weapon_%s \"impulse %d\"\n", it.netname, imp));
327         else
328                 inaccessible = strcat(inaccessible, "\n", it.netname);
329     });
330     if (inaccessible) LOG_TRACEF("Impulse limit exceeded, weapon(s) will not be directly accessible: %s", inaccessible);
331     #ifdef CSQC
332     FOREACH(Weapons, true, it.wr_init(it));
333     #endif
334     weaponorder_byid = "";
335     for (int i = Weapons_MAX - 1; i >= 1; --i)
336         if (Weapons_from(i))
337             weaponorder_byid = strcat(weaponorder_byid, " ", ftos(i));
338     weaponorder_byid = strzone(substring(weaponorder_byid, 1, -1));
339 }
340
341 #ifdef GAMEQC
342
343 .entity weaponchild;
344 .entity exteriorweaponentity;
345 vector weaponentity_glowmod(Weapon wep, entity actor, int c, entity wepent)
346 {
347     vector g;
348     if (!(g = wep.wr_glow(wep, actor, wepent))) g = colormapPaletteColor(c & 0x0F, true) * 2;
349     return g;
350 }
351
352 .int m_gunalign;
353
354 //.int weapon; // current weapon
355 .string weaponname; // name of .weapon
356
357 .vector spawnorigin; // for casings
358
359 // weapon animation vectors:
360 .vector anim_fire1;
361 .vector anim_fire2;
362 .vector anim_idle;
363 .vector anim_reload;
364
365 // static frame globals
366
367 ENUMCLASS(WFRAME)
368 CASE(WFRAME, DONTCHANGE)
369 CASE(WFRAME, FIRE1)
370 CASE(WFRAME, FIRE2)
371 CASE(WFRAME, IDLE)
372 CASE(WFRAME, RELOAD)
373 ENUMCLASS_END(WFRAME)
374
375 .WFRAME wframe;
376
377 #ifdef SVQC
378     #define G_SHOOTFROMFIXEDORIGIN autocvar_g_shootfromfixedorigin
379 #elif defined(CSQC)
380     string autocvar_cl_shootfromfixedorigin;
381     #define G_SHOOTFROMFIXEDORIGIN autocvar_cl_shootfromfixedorigin
382 #endif
383
384 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn);
385 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim);
386
387 #ifdef SVQC
388 void wframe_send(entity actor, entity weaponentity, vector a, bool restartanim);
389 #endif
390 #endif