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