Merge branch 'master' into terencehill/glowmod_color_fix
[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 #include <common/effects/all.qh>
24 #endif
25
26 #include <common/util.qh>
27
28 REGISTRY(Weapons, 72) // Increase as needed. Can be up to 72.
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", false) // 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_HELP("Usage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]");
86             LOG_HELP("  Where 'filename' is the file to write (default is weapons_dump.cfg),");
87             LOG_HELP("  if supplied with '-' output to console as well as default,");
88             LOG_HELP("  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 REGISTRY_DEFINE_GET(Weapons, WEP_Null)
130
131 Weapon Weapon_from_name(string s)
132 {
133     FOREACH(Weapons, it != WEP_Null && it.netname == s, return it);
134     return WEP_Null;
135 }
136
137
138 #ifdef GAMEQC
139
140 // legacy w_prop mappings
141 #define X(fld, T) .T fld; .T wepvar_##fld = fld;
142 X(switchdelay_drop, float)
143 X(switchdelay_raise, float)
144 X(weaponreplace, string)
145 X(weaponstartoverride, float)
146 X(weaponstart, float)
147 X(weaponthrowable, float)
148 #ifdef SVQC
149 X(reload_ammo, float)
150 const .float reloading_ammo = reload_ammo;
151 X(reload_time, float)
152 const .float reloading_time = reload_time;
153 #endif
154 #undef X
155
156 #endif
157
158
159
160 #define W_PROPS(L, class, prefix) \
161     L(W_PROP_BEGIN, W_PROP, W_PROP_END, class, prefix) \
162     L(W_CONFIG_BEGIN, W_CONFIG, W_CONFIG_END, class, prefix) \
163     L(W_UPDATE_BEGIN, W_UPDATE, W_UPDATE_END, class, prefix) \
164     L(W_NET_BEGIN, W_NET, W_NET_END, class, prefix) \
165     /**/ \
166
167
168     #define W_PROP(class, wepname, fld, T, m)       W_PROP_##m(class, fld, T, wepname)
169     #define W_PROP_NONE(class, fld, T, wepname)     _W_PROP(class, fld, T, wepname)
170     #define W_PROP_PRI(class, fld, T, wepname)      _W_PROP(class, primary_##fld, T, wepname)
171     #define W_PROP_SEC(class, fld, T, wepname)      _W_PROP(class, secondary_##fld, T, wepname)
172     #define W_PROP_BOTH(class, fld, T, wepname) \
173             W_PROP_PRI(class, fld, T, wepname) \
174             W_PROP_SEC(class, fld, T, wepname)
175         #define W_PROP_BEGIN(class)
176                 #ifdef GAMEQC
177                         #define _W_PROP(class, fld, T, wepname) \
178                                 /* static */ T _W_PROP_CVAR(wepname, fld); \
179                                 ATTRIB(class, wepvar_##fld, T, _W_PROP_CVAR(wepname, fld));
180                         #define _W_PROP_CVAR(wepname, fld) autocvar_g_balance_##wepname##_##fld
181                 #else
182                         #define _W_PROP(class, fld, T, wepname)
183                         #define _W_PROP_CVAR(wepname, fld)
184                 #endif
185         #define W_PROP_END()
186
187
188
189     #define W_CONFIG(class, wepname, fld, T, m)     W_CONFIG_##m(class, fld, T, wepname)
190     #define W_CONFIG_NONE(class, fld, T, wepname)   _W_CONFIG(class, fld, T, wepname)
191     #define W_CONFIG_PRI(class, fld, T, wepname)    _W_CONFIG(class, primary_##fld, T, wepname)
192     #define W_CONFIG_SEC(class, fld, T, wepname)    _W_CONFIG(class, secondary_##fld, T, wepname)
193     #define W_CONFIG_BOTH(class, fld, T, wepname) \
194             W_CONFIG_PRI(class, fld, T, wepname) \
195             W_CONFIG_SEC(class, fld, T, wepname)
196         #ifdef SVQC
197                 #define W_CONFIG_BEGIN(class) METHOD(class, wr_config, void(class this)) {
198                         #define _W_CONFIG(class, fld, T, wepname) if (#wepname == this.netname) WEP_CONFIG_WRITE_CVARS(wepname, fld, T);
199                 #define W_CONFIG_END() }
200         #else
201                 #define W_CONFIG_BEGIN(class)
202                         #define _W_CONFIG(class, fld, T, wepname)
203                 #define W_CONFIG_END()
204         #endif
205
206
207     #define W_UPDATE(class, wepname, fld, T, m)     W_UPDATE_##m(class, fld, T, wepname)
208     #define W_UPDATE_NONE(class, fld, T, wepname)   _W_UPDATE(class, fld, T, wepname)
209     #define W_UPDATE_PRI(class, fld, T, wepname)    _W_UPDATE(class, primary_##fld, T, wepname)
210     #define W_UPDATE_SEC(class, fld, T, wepname)    _W_UPDATE(class, secondary_##fld, T, wepname)
211     #define W_UPDATE_BOTH(class, fld, T, wepname) \
212             W_UPDATE_PRI(class, fld, T, wepname) \
213             W_UPDATE_SEC(class, fld, T, wepname)
214         #ifdef GAMEQC
215                 .entity baseline, baseline_target;
216                 #define W_UPDATE_BEGIN(class) \
217                         METHOD(class, wr_update, void(class this)) \
218                         { \
219                                 noref entity b = this.baseline; \
220                                 if (!b) \
221                                 { \
222                                         b = this.baseline = new_pure(baseline); \
223                                         b.baseline_target = this; \
224                                 }
225                         #ifdef SVQC
226                                 #define _W_UPDATE(class, fld, T, wepname) \
227                                         { \
228                                                 T it = _W_PROP_CVAR(wepname, fld); \
229                                                 b.wepvar_##fld = this.wepvar_##fld; \
230                                                 this.wepvar_##fld = it; \
231                                         }
232                         #else
233                                 #define _W_UPDATE(class, fld, T, wepname)
234                         #endif
235                 #define W_UPDATE_END() }
236         #else
237                 #define W_UPDATE_BEGIN(class)
238                         #define _W_UPDATE(class, fld, T, wepname)
239                 #define W_UPDATE_END()
240         #endif
241
242
243     #define W_NET(class, wepname, fld, T, m)     W_NET_##m(class, fld, T, wepname)
244     #define W_NET_NONE(class, fld, T, wepname)   _W_NET(class, fld, T, wepname)
245     #define W_NET_PRI(class, fld, T, wepname)    _W_NET(class, primary_##fld, T, wepname)
246     #define W_NET_SEC(class, fld, T, wepname)    _W_NET(class, secondary_##fld, T, wepname)
247     #define W_NET_BOTH(class, fld, T, wepname) \
248             W_NET_PRI(class, fld, T, wepname) \
249             W_NET_SEC(class, fld, T, wepname)
250     #if defined(CSQC)
251         REGISTER_NET_TEMP(WeaponUpdate)
252         #define W_NET_BEGIN(class) METHOD(class, wr_net, void(class this, int i)) { int n = 0;
253             #define _W_NET(class, fld, T, wepname) \
254             { \
255                 if (++n == i) this.wepvar_##fld = Read_##T(); \
256             }
257             .void(Weapon this, int i) wr_net;
258             NET_HANDLE(WeaponUpdate, bool isnew)
259             {
260                 Weapon w = REGISTRY_GET(Weapons, ReadByte());
261                 for (int i; (i = ReadByte()); )
262                 {
263                     w.wr_net(w, i);
264                 }
265                 return true;
266             }
267         #define W_NET_END() }
268     #elif defined(SVQC)
269         REGISTER_NET_TEMP(WeaponUpdate)
270         #define W_NET_BEGIN(class) \
271             METHOD(class, wr_net, void(class this, int chan)) \
272             { \
273                 bool commit = false; \
274                 int i = 0;
275             #define _W_NET(class, fld, T, wepname) \
276                 { \
277                     ++i; \
278                     T it = this.wepvar_##fld; \
279                     if (chan == MSG_ONE || it != this.baseline.wepvar_##fld) \
280                     { \
281                         if (!commit) { commit = true; WriteHeader(chan, WeaponUpdate); WriteByte(chan, this.m_id); } \
282                         WriteByte(chan, i); Write_##T(chan, it); \
283                     } \
284                 }
285         #define W_NET_END() if (commit) WriteByte(chan, 0); }
286     #else
287         #define W_NET_BEGIN(class)
288             #define _W_NET(class, fld, T, wepname)
289         #define W_NET_END()
290     #endif
291
292
293
294 // read cvars from weapon settings
295 // cvars are created as such: g_balance_wepname_name
296 #define WEP_CVAR(wepname, name) (_wep_##wepname.wepvar_##name)
297 #define WEP_CVAR_PRI(wepname, name) WEP_CVAR(wepname, primary_##name)
298 #define WEP_CVAR_SEC(wepname, name) WEP_CVAR(wepname, secondary_##name)
299 #define WEP_CVAR_BOTH(wepname, isprimary, name) ((isprimary) ? WEP_CVAR_PRI(wepname, name) : WEP_CVAR_SEC(wepname, name))
300
301 const int WEP_FIRST = 1;
302 #define WEP_LAST (REGISTRY_COUNT(Weapons) - 1)
303 WepSet WEPSET_ALL;
304 WepSet WEPSET_SUPERWEAPONS;
305
306 #include "all.inc"
307
308 // TODO: remove after 0.8.2. Retains impulse number compatibility because 0.8.1 clients don't reload the weapons.cfg
309 #define WEP_HARDCODED_IMPULSES 20
310
311 // TODO: invert after 0.8.2. Will require moving 'best weapon' impulses
312 #define WEP_IMPULSE_BEGIN 230
313 #define WEP_IMPULSE_END bound(WEP_IMPULSE_BEGIN, WEP_IMPULSE_BEGIN + (REGISTRY_COUNT(Weapons) - 1) - 1, 253)
314
315 REGISTRY_SORT(Weapons, WEP_HARDCODED_IMPULSES + 1)
316 REGISTRY_CHECK(Weapons)
317
318 STATIC_INIT(register_weapons_done)
319 {
320         string inaccessible = "";
321     FOREACH(Weapons, true, {
322         WepSet set = it.m_wepset = _WepSet_FromWeapon(it.m_id = i);
323         WEPSET_ALL |= set;
324         if (it.spawnflags & WEP_FLAG_SUPERWEAPON) WEPSET_SUPERWEAPONS |= set;
325         if (it == WEP_Null) continue;
326         int imp = WEP_IMPULSE_BEGIN + it.m_id - 1;
327         if (imp <= WEP_IMPULSE_END)
328             localcmd(sprintf("alias weapon_%s \"impulse %d\"\n", it.netname, imp));
329         else
330                 inaccessible = strcat(inaccessible, "\n", it.netname);
331     });
332     if (inaccessible && autocvar_developer > 0) LOG_TRACEF("Impulse limit exceeded, weapon(s) will not be directly accessible: %s", inaccessible);
333     #ifdef CSQC
334     FOREACH(Weapons, true, it.wr_init(it));
335     #endif
336     weaponorder_byid = "";
337     for (int i = REGISTRY_MAX(Weapons) - 1; i >= 1; --i)
338         if (REGISTRY_GET(Weapons, i))
339             weaponorder_byid = strcat(weaponorder_byid, " ", ftos(i));
340     weaponorder_byid = strzone(substring(weaponorder_byid, 1, -1));
341 }
342
343 #ifdef GAMEQC
344
345 .entity weaponchild;
346 .entity exteriorweaponentity;
347 vector weaponentity_glowmod(Weapon wep, entity actor, int c, entity wepent)
348 {
349     vector g;
350     if (!(g = wep.wr_glow(wep, actor, wepent))) g = colormapPaletteColor(c & 0x0F, true);
351     return g;
352 }
353
354 .int m_gunalign;
355
356 //.int weapon; // current weapon
357 .string weaponname; // name of .weapon
358
359 .vector spawnorigin; // for casings
360
361 .vector movedir_aligned; // shot origin based on weapon alignment (unaffected by shootfromeye)
362
363 // weapon animation vectors:
364 .vector anim_fire1;
365 .vector anim_fire2;
366 .vector anim_idle;
367 .vector anim_reload;
368
369 .entity muzzle_flash;
370
371 // static frame globals
372
373 ENUMCLASS(WFRAME)
374 CASE(WFRAME, DONTCHANGE)
375 CASE(WFRAME, FIRE1)
376 CASE(WFRAME, FIRE2)
377 CASE(WFRAME, IDLE)
378 CASE(WFRAME, RELOAD)
379 ENUMCLASS_END(WFRAME)
380
381 .WFRAME wframe;
382
383 #ifdef SVQC
384     #define G_SHOOTFROMFIXEDORIGIN autocvar_g_shootfromfixedorigin
385 #elif defined(CSQC)
386     string autocvar_cl_shootfromfixedorigin;
387     #define G_SHOOTFROMFIXEDORIGIN autocvar_cl_shootfromfixedorigin
388 #endif
389
390 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn);
391 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim);
392
393 #ifdef CSQC
394 bool cvar_cl_accuracy_data_share;
395 REPLICATE(cvar_cl_accuracy_data_share, bool, "cl_accuracy_data_share");
396 bool cvar_cl_accuracy_data_receive;
397 REPLICATE(cvar_cl_accuracy_data_receive, bool, "cl_accuracy_data_receive");
398 #endif
399
400 #ifdef SVQC
401 void wframe_send(entity actor, entity weaponentity, int wepframe, float attackrate, bool restartanim);
402 #endif
403
404 #ifdef SVQC
405 void W_MuzzleFlash(Weapon thiswep, entity actor, .entity weaponentity, vector shotorg, vector shotdir);
406 #endif
407 #endif