Merge branch 'master' into develop
[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 #define DEFAULT_FILENAME "weapons_dump.cfg"
42 // NOTE: dumpeffectinfo, dumpnotifs, dumpturrets and dumpweapons use similar code
43 GENERIC_COMMAND(dumpweapons, "Dump all turrets into " DEFAULT_FILENAME, false) // WEAPONTODO: make this work with other progs than just server
44 {
45     switch(request)
46     {
47         case CMD_REQUEST_COMMAND:
48         {
49             #ifdef SVQC
50             wep_config_file = -1;
51             wep_config_alsoprint = -1;
52             string filename = argv(1);
53
54             if(filename == "")
55             {
56                 filename = DEFAULT_FILENAME;
57                 wep_config_alsoprint = false;
58             }
59             else if(filename == "-")
60             {
61                 filename = DEFAULT_FILENAME;
62                 wep_config_alsoprint = true;
63             }
64             wep_config_file = fopen(filename, FILE_WRITE);
65
66             if(wep_config_file >= 0)
67             {
68                 Dump_Weapon_Settings();
69                 LOG_INFOF("Dumping weapons... File located in ^2data/data/%s^7.", filename);
70                 fclose(wep_config_file);
71                 wep_config_file = -1;
72                 wep_config_alsoprint = -1;
73             }
74             else
75             {
76                 LOG_INFOF("^1Error: ^7Could not open file '%s'!", filename);
77             }
78             #else
79             LOG_INFO("Weapons dump command only works with sv_cmd.");
80             #endif
81             return;
82         }
83
84         default:
85         case CMD_REQUEST_USAGE:
86         {
87             LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " dumpweapons [<filename>]");
88             LOG_HELPF("  Where <filename> is the file to write (default is %s),", DEFAULT_FILENAME);
89             LOG_HELP("  if supplied with '-' output to console as well as default,");
90             LOG_HELP("  if left blank, it will only write to default.");
91             return;
92         }
93     }
94 }
95 #undef DEFAULT_FILENAME
96
97 #ifdef SVQC
98 entity W_PROP_reloader;
99 float autocvar_w_prop_interval = 5;
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(entity this)
112 {
113     W_PROP_reload(MSG_ALL, NULL);
114 }
115 STATIC_INIT_LATE(W_PROP_reloader)
116 {
117     entity e = W_PROP_reloader = new_pure(W_PROP_reloader);
118     setthink(e, W_PROP_think);
119     W_PROP_think(e);
120 }
121 #endif
122
123 #define REGISTER_WEAPON(...) EVAL_REGISTER_WEAPON(OVERLOAD(REGISTER_WEAPON, __VA_ARGS__))
124 #define EVAL_REGISTER_WEAPON(...) __VA_ARGS__
125 #define REGISTER_WEAPON_2(id, inst) REGISTER(Weapons, WEP, id, m_id, inst)
126 /** TODO: deprecated - remove */
127 #define REGISTER_WEAPON_3(id, sname, inst) \
128     REGISTER_WEAPON_2(id, inst); \
129     [[alias("WEP_" #id)]] Weapon _wep_##sname
130
131 REGISTER_WEAPON(Null, NEW(Weapon));
132 REGISTRY_DEFINE_GET(Weapons, WEP_Null)
133
134 Weapon Weapon_from_name(string s)
135 {
136     FOREACH(Weapons, it != WEP_Null && it.netname == s, return it);
137     return WEP_Null;
138 }
139
140
141 #ifdef GAMEQC
142
143 // legacy w_prop mappings
144 #define X(fld, T) .T fld; .T wepvar_##fld = fld;
145 X(switchdelay_drop, float)
146 X(switchdelay_raise, float)
147 X(weaponreplace, string)
148 X(weaponstartoverride, float)
149 X(weaponstart, float)
150 X(weaponthrowable, float)
151 #ifdef SVQC
152 X(reload_ammo, float)
153 const .float reloading_ammo = reload_ammo;
154 X(reload_time, float)
155 const .float reloading_time = reload_time;
156 #endif
157 #undef X
158
159 #endif
160
161
162
163 #define W_PROPS(L, class, prefix) \
164     L(W_PROP_BEGIN, W_PROP, W_PROP_END, class, prefix) \
165     L(W_CONFIG_BEGIN, W_CONFIG, W_CONFIG_END, class, prefix) \
166     L(W_UPDATE_BEGIN, W_UPDATE, W_UPDATE_END, class, prefix) \
167     L(W_NET_BEGIN, W_NET, W_NET_END, class, prefix) \
168     /**/ \
169
170
171     #define W_PROP(class, wepname, fld, T, m)       W_PROP_##m(class, fld, T, wepname)
172     #define W_PROP_NONE(class, fld, T, wepname)     _W_PROP(class, fld, T, wepname)
173     #define W_PROP_PRI(class, fld, T, wepname)      _W_PROP(class, primary_##fld, T, wepname)
174     #define W_PROP_SEC(class, fld, T, wepname)      _W_PROP(class, secondary_##fld, T, wepname)
175     #define W_PROP_BOTH(class, fld, T, wepname) \
176             W_PROP_PRI(class, fld, T, wepname) \
177             W_PROP_SEC(class, fld, T, wepname)
178         #define W_PROP_BEGIN(class)
179                 #ifdef GAMEQC
180                         #define _W_PROP(class, fld, T, wepname) \
181                                 /* static */ T _W_PROP_CVAR(wepname, fld); \
182                                 ATTRIB(class, wepvar_##fld, T, _W_PROP_CVAR(wepname, fld));
183                         #define _W_PROP_CVAR(wepname, fld) autocvar_g_balance_##wepname##_##fld
184                 #else
185                         #define _W_PROP(class, fld, T, wepname)
186                         #define _W_PROP_CVAR(wepname, fld)
187                 #endif
188         #define W_PROP_END()
189
190
191
192     #define W_CONFIG(class, wepname, fld, T, m)     W_CONFIG_##m(class, fld, T, wepname)
193     #define W_CONFIG_NONE(class, fld, T, wepname)   _W_CONFIG(class, fld, T, wepname)
194     #define W_CONFIG_PRI(class, fld, T, wepname)    _W_CONFIG(class, primary_##fld, T, wepname)
195     #define W_CONFIG_SEC(class, fld, T, wepname)    _W_CONFIG(class, secondary_##fld, T, wepname)
196     #define W_CONFIG_BOTH(class, fld, T, wepname) \
197             W_CONFIG_PRI(class, fld, T, wepname) \
198             W_CONFIG_SEC(class, fld, T, wepname)
199         #ifdef SVQC
200                 #define W_CONFIG_BEGIN(class) METHOD(class, wr_config, void(class this)) {
201                         #define _W_CONFIG(class, fld, T, wepname) if (#wepname == this.netname) WEP_CONFIG_WRITE_CVARS(wepname, fld, T);
202                 #define W_CONFIG_END() }
203         #else
204                 #define W_CONFIG_BEGIN(class)
205                         #define _W_CONFIG(class, fld, T, wepname)
206                 #define W_CONFIG_END()
207         #endif
208
209
210     #define W_UPDATE(class, wepname, fld, T, m)     W_UPDATE_##m(class, fld, T, wepname)
211     #define W_UPDATE_NONE(class, fld, T, wepname)   _W_UPDATE(class, fld, T, wepname)
212     #define W_UPDATE_PRI(class, fld, T, wepname)    _W_UPDATE(class, primary_##fld, T, wepname)
213     #define W_UPDATE_SEC(class, fld, T, wepname)    _W_UPDATE(class, secondary_##fld, T, wepname)
214     #define W_UPDATE_BOTH(class, fld, T, wepname) \
215             W_UPDATE_PRI(class, fld, T, wepname) \
216             W_UPDATE_SEC(class, fld, T, wepname)
217         #ifdef GAMEQC
218                 .entity baseline, baseline_target;
219                 #define W_UPDATE_BEGIN(class) \
220                         METHOD(class, wr_update, void(class this)) \
221                         { \
222                                 noref entity b = this.baseline; \
223                                 if (!b) \
224                                 { \
225                                         b = this.baseline = new_pure(baseline); \
226                                         b.baseline_target = this; \
227                                 }
228                         #ifdef SVQC
229                                 #define _W_UPDATE(class, fld, T, wepname) \
230                                         { \
231                                                 T it = _W_PROP_CVAR(wepname, fld); \
232                                                 b.wepvar_##fld = this.wepvar_##fld; \
233                                                 this.wepvar_##fld = it; \
234                                         }
235                         #else
236                                 #define _W_UPDATE(class, fld, T, wepname)
237                         #endif
238                 #define W_UPDATE_END() }
239         #else
240                 #define W_UPDATE_BEGIN(class)
241                         #define _W_UPDATE(class, fld, T, wepname)
242                 #define W_UPDATE_END()
243         #endif
244
245
246     #define W_NET(class, wepname, fld, T, m)     W_NET_##m(class, fld, T, wepname)
247     #define W_NET_NONE(class, fld, T, wepname)   _W_NET(class, fld, T, wepname)
248     #define W_NET_PRI(class, fld, T, wepname)    _W_NET(class, primary_##fld, T, wepname)
249     #define W_NET_SEC(class, fld, T, wepname)    _W_NET(class, secondary_##fld, T, wepname)
250     #define W_NET_BOTH(class, fld, T, wepname) \
251             W_NET_PRI(class, fld, T, wepname) \
252             W_NET_SEC(class, fld, T, wepname)
253     #if defined(CSQC)
254         REGISTER_NET_TEMP(WeaponUpdate)
255         #define W_NET_BEGIN(class) METHOD(class, wr_net, void(class this, int i)) { int n = 0;
256             #define _W_NET(class, fld, T, wepname) \
257             { \
258                 if (++n == i) this.wepvar_##fld = Read_##T(); \
259             }
260             .void(Weapon this, int i) wr_net;
261             NET_HANDLE(WeaponUpdate, bool isnew)
262             {
263                 Weapon w = REGISTRY_GET(Weapons, ReadByte());
264                 for (int i; (i = ReadByte()); )
265                 {
266                     w.wr_net(w, i);
267                 }
268                 return true;
269             }
270         #define W_NET_END() }
271     #elif defined(SVQC)
272         REGISTER_NET_TEMP(WeaponUpdate)
273         #define W_NET_BEGIN(class) \
274             METHOD(class, wr_net, void(class this, int chan)) \
275             { \
276                 bool commit = false; \
277                 int i = 0;
278             #define _W_NET(class, fld, T, wepname) \
279                 { \
280                     ++i; \
281                     T it = this.wepvar_##fld; \
282                     if (chan == MSG_ONE || it != this.baseline.wepvar_##fld) \
283                     { \
284                         if (!commit) { commit = true; WriteHeader(chan, WeaponUpdate); WriteByte(chan, this.m_id); } \
285                         WriteByte(chan, i); Write_##T(chan, it); \
286                     } \
287                 }
288         #define W_NET_END() if (commit) WriteByte(chan, 0); }
289     #else
290         #define W_NET_BEGIN(class)
291             #define _W_NET(class, fld, T, wepname)
292         #define W_NET_END()
293     #endif
294
295
296
297 // read cvars from weapon settings
298 // cvars are created as such: g_balance_wepname_name
299 #define WEP_CVAR(wepname, name) (_wep_##wepname.wepvar_##name)
300 #define WEP_CVAR_PRI(wepname, name) WEP_CVAR(wepname, primary_##name)
301 #define WEP_CVAR_SEC(wepname, name) WEP_CVAR(wepname, secondary_##name)
302 #define WEP_CVAR_BOTH(wepname, isprimary, name) ((isprimary) ? WEP_CVAR_PRI(wepname, name) : WEP_CVAR_SEC(wepname, name))
303
304 const int WEP_FIRST = 1;
305 #define WEP_LAST (REGISTRY_COUNT(Weapons) - 1)
306 WepSet WEPSET_ALL;
307 WepSet WEPSET_SUPERWEAPONS;
308
309 #include "all.inc"
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, 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     string autocvar_g_shootfromfixedorigin;
385     #define G_SHOOTFROMFIXEDORIGIN autocvar_g_shootfromfixedorigin
386 #elif defined(CSQC)
387     string autocvar_cl_shootfromfixedorigin;
388     #define G_SHOOTFROMFIXEDORIGIN autocvar_cl_shootfromfixedorigin
389 #endif
390
391 vector shotorg_adjust_values(vector vecs, bool y_is_right, bool visual, int algn);
392 void CL_WeaponEntity_SetModel(entity this, string name, bool _anim);
393
394 REPLICATE_INIT(int, cvar_cl_gunalign);
395 REPLICATE_INIT(bool, cvar_cl_weapon_switch_reload);
396 REPLICATE_INIT(bool, cvar_cl_weapon_switch_fallback_to_impulse);
397 REPLICATE_INIT(int, cvar_cl_weaponimpulsemode);
398
399 REPLICATE_INIT(string, cvar_cl_weaponpriority);
400 REPLICATE_INIT(string, cvar_cl_weaponpriorities[10]);
401
402 #ifdef CSQC
403 REPLICATE_INIT(bool, cvar_cl_accuracy_data_share);
404 REPLICATE_INIT(bool, cvar_cl_accuracy_data_receive);
405 #endif
406
407 #ifdef SVQC
408 void wframe_send(entity actor, entity weaponentity, int wepframe, float attackrate, bool restartanim);
409
410 void W_MuzzleFlash(Weapon thiswep, entity actor, .entity weaponentity, vector shotorg, vector shotdir);
411
412 string W_FixWeaponOrder_ForceComplete_AndBuildImpulseList(entity this, string wo);
413 string W_FixWeaponOrder_AllowIncomplete(entity this, string order);
414 #endif
415
416 #endif