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