Registry: use BITS everywhere
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / effectinfo.qc
1 #define EFFECTINFO_PARSER(on) \
2     on(type,                                        MY(type) \
3     ,{ ASSERT(n == 1 && "type");                    MY(type) = strzone(argv(1)); \
4     }, sprintf(" %s",                               (MY(type)) \
5     )) \
6     on(airfriction,                                 MY(airfriction) \
7     ,{ ASSERT(n == 1 && "airfriction");             MY(airfriction) = stof(argv(1)); \
8     }, sprintf(" %s",                               ftos(MY(airfriction)) \
9     )) \
10     on(alpha,                                       MY(alpha_min) ||                    MY(alpha_max) ||                MY(alpha_fade) \
11     ,{ ASSERT(n == 3 && "alpha");                   MY(alpha_min) = stof(argv(1));      MY(alpha_max) = stof(argv(2));  MY(alpha_fade) = stof(argv(3)); \
12     }, sprintf(" %s %s %s",                         ftos(MY(alpha_min)),                ftos(MY(alpha_max)),            ftos(MY(alpha_fade)) \
13     )) \
14     on(blend,                                       MY(blend) \
15     ,{ ASSERT(n == 1 && "blend");                   MY(blend) = strzone(argv(1)); \
16     }, sprintf(" %s",                               (MY(blend)) \
17     )) \
18     on(bounce,                                      MY(bounce) \
19     ,{ ASSERT(n == 1 && "bounce");                  MY(bounce) = stof(argv(1)); \
20     }, sprintf(" %s",                               ftos(MY(bounce)) \
21     )) \
22     on(color,                                       MY(color_min) ||                    MY(color_max) \
23     ,{ ASSERT(n == 2 && "color");                   MY(color_min) = strzone(argv(1));   MY(color_max) = strzone(argv(2)); \
24     }, sprintf(" %s %s",                            (MY(color_min)),                    (MY(color_max)) \
25     )) \
26     on(countabsolute,                               MY(countabsolute) \
27     ,{ ASSERT(n == 1 && "countabsolute");           MY(countabsolute) = stof(argv(1)); \
28     }, sprintf(" %s",                               ftos(MY(countabsolute)) \
29     )) \
30     on(count,                                       MY(count) \
31     ,{ ASSERT(n == 1 && "count");                   MY(count) = stof(argv(1)); \
32     }, sprintf(" %s",                               ftos(MY(count)) \
33     )) \
34     on(gravity,                                     MY(gravity) \
35     ,{ ASSERT(n == 1 && "gravity");                 MY(gravity) = stof(argv(1)); \
36     }, sprintf(" %s",                               ftos(MY(gravity)) \
37     )) \
38     on(lightcolor,                                  MY(lightcolor) \
39     ,{ ASSERT(n == 3 && "lightcolor");              MY(lightcolor) = vec3(stof(argv(1)), stof(argv(2)), stof(argv(3))); \
40     }, sprintf(" %v",                               (MY(lightcolor)) \
41     )) \
42     on(lightradiusfade,                             MY(lightradiusfade) \
43     ,{ ASSERT(n == 1 && "lightradiusfade");         MY(lightradiusfade) = stof(argv(1)); \
44     }, sprintf(" %s",                               ftos(MY(lightradiusfade)) \
45     )) \
46     on(lightradius,                                 MY(lightradius) \
47     ,{ ASSERT(n == 1 && "lightradius");             MY(lightradius) = stof(argv(1)); \
48     }, sprintf(" %s",                               ftos(MY(lightradius)) \
49     )) \
50     on(lighttime,                                   MY(lighttime) \
51     ,{ ASSERT(n == 1 && "lighttime");               MY(lighttime) = stof(argv(1)); \
52     }, sprintf(" %s",                               ftos(MY(lighttime)) \
53     )) \
54     on(liquidfriction,                              MY(liquidfriction) \
55     ,{ ASSERT(n == 1 && "liquidfriction");          MY(liquidfriction) = stof(argv(1)); \
56     }, sprintf(" %s",                               ftos(MY(liquidfriction)) \
57     )) \
58     on(notunderwater,                               MY(notunderwater) \
59     ,{ ASSERT(n == 0 && "notunderwater");           MY(notunderwater) = true; \
60     }, "" \
61     ) \
62     on(orientation,                                 MY(orientation) \
63     ,{ ASSERT(n == 1 && "orientation");             MY(orientation) = strzone(argv(1)); \
64     }, sprintf(" %s",                               (MY(orientation)) \
65     )) \
66     on(originjitter,                                MY(originjitter) \
67     ,{ ASSERT(n == 3 && "originjitter");            MY(originjitter) = vec3(stof(argv(1)), stof(argv(2)), stof(argv(3))); \
68     }, sprintf(" %v",                               (MY(originjitter)) \
69     )) \
70     on(originoffset,                                MY(originoffset) \
71     ,{ ASSERT(n == 3 && "originoffset");            MY(originoffset) = vec3(stof(argv(1)), stof(argv(2)), stof(argv(3))); \
72     }, sprintf(" %v",                               (MY(originoffset)) \
73     )) \
74     on(relativeoriginoffset,                        MY(relativeoriginoffset) \
75     ,{ ASSERT(n == 3 && "relativeoriginoffset");    MY(relativeoriginoffset) = vec3(stof(argv(1)), stof(argv(2)), stof(argv(3))); \
76     }, sprintf(" %v",                               (MY(relativeoriginoffset)) \
77     )) \
78     on(relativevelocityoffset,                      MY(relativevelocityoffset) \
79     ,{ ASSERT(n == 3 && "relativevelocityoffset");  MY(relativevelocityoffset) = vec3(stof(argv(1)), stof(argv(2)), stof(argv(3))); \
80     }, sprintf(" %v",                               (MY(relativevelocityoffset)) \
81     )) \
82     on(rotate,                                      MY(startangle_min) ||               MY(startangle_max) ||               MY(spin_min) ||                 MY(spin_max) \
83     ,{ ASSERT(n == 4 && "rotate");                  MY(startangle_min) = stof(argv(1)); MY(startangle_max) = stof(argv(2)); MY(spin_min) = stof(argv(3));   MY(spin_max) = stof(argv(4)); \
84     }, sprintf(" %s %s %s %s",                      ftos(MY(startangle_min)),           ftos(MY(startangle_max)),           ftos(MY(spin_min)),             ftos(MY(spin_max)) \
85     )) \
86     on(sizeincrease,                                MY(sizeincrease) \
87     ,{ ASSERT(n == 1 && "sizeincrease");            MY(sizeincrease) = stof(argv(1)); \
88     }, sprintf(" %s",                               ftos(MY(sizeincrease)) \
89     )) \
90     on(size,                                        MY(size_min) ||                     MY(size_max) \
91     ,{ ASSERT(n == 2 && "size");                    MY(size_min) = stof(argv(1));       MY(size_max) = stof(argv(2)); \
92     }, sprintf(" %s %s",                            ftos(MY(size_min)),                 ftos(MY(size_max)) \
93     )) \
94     on(staincolor,                                  MY(staincolor_min) ||                   MY(staincolor_max) \
95     ,{ ASSERT(n == 2 && "staincolor");              MY(staincolor_min) = strzone(argv(1));  MY(staincolor_max) = strzone(argv(2)); \
96     }, sprintf(" %s %s",                            (MY(staincolor_min)),                   (MY(staincolor_max)) \
97     )) \
98     on(stainsize,                                   MY(stainsize_min) ||                MY(stainsize_max) \
99     ,{ ASSERT(n == 2 && "stainsize");               MY(stainsize_min) = stof(argv(1));  MY(stainsize_max) = stof(argv(2)); \
100     }, sprintf(" %s %s",                            ftos(MY(stainsize_min)),            ftos(MY(stainsize_max)) \
101     )) \
102     on(staintex,                                    MY(staintex_min) ||                 MY(staintex_max) \
103     ,{ ASSERT(n == 2 && "staintex");                MY(staintex_min) = stof(argv(1));   MY(staintex_max) = stof(argv(2)); \
104     }, sprintf(" %s %s",                            ftos(MY(staintex_min)),             ftos(MY(staintex_max)) \
105     )) \
106     on(stretchfactor,                               MY(stretchfactor) \
107     ,{ ASSERT(n == 1 && "stretchfactor");           MY(stretchfactor) = stof(argv(1)); \
108     }, sprintf(" %s",                               ftos(MY(stretchfactor)) \
109     )) \
110     on(tex,                                         MY(tex_min) ||                      MY(tex_max) \
111     ,{ ASSERT(n == 2 && "tex");                     MY(tex_min) = stof(argv(1));        MY(tex_max) = stof(argv(2)); \
112     }, sprintf(" %s %s",                            ftos(MY(tex_min)),                  ftos(MY(tex_max)) \
113     )) \
114     on(time,                                        MY(time_min) ||                     MY(time_max) \
115     ,{ ASSERT(n == 2 && "time");                    MY(time_min) = stof(argv(1));       MY(time_max) = stof(argv(2)); \
116     }, sprintf(" %s %s",                            ftos(MY(time_min)),                 ftos(MY(time_max)) \
117     )) \
118     on(trailspacing,                                MY(trailspacing) \
119     ,{ ASSERT(n == 1 && "trailspacing");            MY(trailspacing) = stof(argv(1)); \
120     }, sprintf(" %s",                               ftos(MY(trailspacing)) \
121     )) \
122     on(underwater,                                  MY(underwater) \
123     ,{ ASSERT(n == 0 && "underwater");              MY(underwater) = true; \
124     }, "" \
125     ) \
126     on(velocityjitter,                              MY(velocityjitter) \
127     ,{ ASSERT(n == 3 && "velocityjitter");          MY(velocityjitter) = vec3(stof(argv(1)), stof(argv(2)), stof(argv(3))); \
128     }, sprintf(" %v",                               (MY(velocityjitter)) \
129     )) \
130     on(velocitymultiplier,                          MY(velocitymultiplier) \
131     ,{ ASSERT(n == 1 && "velocitymultiplier");      MY(velocitymultiplier) = stof(argv(1)); \
132     }, sprintf(" %s",                               ftos(MY(velocitymultiplier)) \
133     )) \
134     on(velocityoffset,                              MY(velocityoffset) \
135     ,{ ASSERT(n == 3 && "velocityoffset");          MY(velocityoffset) = vec3(stof(argv(1)), stof(argv(2)), stof(argv(3))); \
136     }, sprintf(" %v",                               (MY(velocityoffset)) \
137     )) \
138     /**/
139
140 CLASS(EffectInfo, Object)
141     ATTRIB(EffectInfo, effectinfo_name, string, string_null)
142     CONSTRUCTOR(EffectInfo, string s) {
143         CONSTRUCT(EffectInfo);
144         this.effectinfo_name = s;
145     }
146
147     #define FIELDS(MY) \
148     MY(airfriction, float, 0) \
149     MY(alpha_min, float, 0) \
150     MY(alpha_max, float, 0) \
151     MY(alpha_fade, float, 0) \
152     MY(blend, string, string_null) \
153     MY(bounce, float, 0) \
154     MY(color_min, string, string_null) \
155     MY(color_max, string, string_null) \
156     MY(countabsolute, float, 0) \
157     MY(count, float, 0) \
158     MY(gravity, float, 0) \
159     MY(lightcolor, vector, '0 0 0') \
160     MY(lightradiusfade, float, 0) \
161     MY(lightradius, float, 0) \
162     MY(lighttime, float, 0) \
163     MY(liquidfriction, float, 0) \
164     MY(notunderwater, bool, false) \
165     MY(orientation, string, string_null) \
166     MY(originjitter, vector, '0 0 0') \
167     MY(originoffset, vector, '0 0 0') \
168     MY(relativeoriginoffset, vector, '0 0 0') \
169     MY(relativevelocityoffset, vector, '0 0 0') \
170     MY(startangle_min, float, 0) \
171     MY(startangle_max, float, 0) \
172     MY(spin_min, float, 0) \
173     MY(spin_max, float, 0) \
174     MY(sizeincrease, float, 0) \
175     MY(size_min, float, 0) \
176     MY(size_max, float, 0) \
177     MY(staincolor_min, string, string_null) \
178     MY(staincolor_max, string, string_null) \
179     MY(stainsize_min, float, 0) \
180     MY(stainsize_max, float, 0) \
181     MY(staintex_min, float, 0) \
182     MY(staintex_max, float, 0) \
183     MY(stretchfactor, float, 0) \
184     MY(tex_min, float, 0) \
185     MY(tex_max, float, 0) \
186     MY(time_min, float, 0) \
187     MY(time_max, float, 0) \
188     MY(trailspacing, float, 0) \
189     MY(type, string, string_null) \
190     MY(underwater, bool, false) \
191     MY(velocityjitter, vector, '0 0 0') \
192     MY(velocitymultiplier, float, 0) \
193     MY(velocityoffset, vector, '0 0 0') \
194     /**/
195
196     #define MY(f, type, val) ATTRIB(EffectInfo, effectinfo_##f, type, val)
197     FIELDS(MY)
198     #undef MY
199
200     METHOD(EffectInfo, describe, string(EffectInfo this)) {
201         string s = sprintf("SUB(%s) {\n", this.effectinfo_name);
202         #define str_bool(it) (it ? "true" : "false")
203         #define str_float(it) ftos(it)
204         #define str_vector(it) vtos(it)
205         #define str_string(it) strcat("\"", it, "\"")
206         #define p(f, type, default) if (this.effectinfo_##f) { s = strcat(s, "\t", "MY("#f") = ", str_##type(this.effectinfo_##f), ";\n"); }
207         FIELDS(p)
208         #undef p
209         #undef MY
210         return strcat(s, "}\n");
211     }
212
213     METHOD(EffectInfo, dump, string(EffectInfo this)) {
214         string s = sprintf("effect %s\n", this.effectinfo_name);
215         #define MY(f) this.effectinfo_##f
216         #define p(k, isset, parse, unparse) if (isset) { s = strcat(s, "\t", #k, unparse, "\n"); }
217         EFFECTINFO_PARSER(p)
218         #undef p
219         #undef MY
220         return s;
221     }
222
223     #undef FIELDS
224 ENDCLASS(EffectInfo)
225
226 CLASS(EffectInfoGroup, Object)
227     ATTRIBARRAY(EffectInfoGroup, children, EffectInfo, 16)
228     ATTRIB(EffectInfoGroup, children_count, int, 0)
229 ENDCLASS(EffectInfoGroup)
230
231 void effectinfo_read()
232 {
233     int fh = fopen("effectinfo.txt", FILE_READ);
234     EffectInfo info = NULL;
235     for (string line; (line = fgets(fh)); ) {
236         int n = tokenize_console(line);
237         if (n == 0) continue;
238         n--;
239         string k = argv(0);
240         if (k == "effect") {
241             ASSERT(n == 1);
242             info = NEW(EffectInfo, strzone(argv(1)));
243             continue;
244         }
245         ASSERT(info != NULL);
246         switch (k) {
247             #define MY(f) info.effectinfo_##f
248             #define p(k, isset, parse, unparse) case #k: parse break;
249             EFFECTINFO_PARSER(p)
250             #undef p
251             #undef MY
252             default:
253                 LOG_WARNINGF("Unknown property '%s'\n", k);
254                 break;
255         }
256     }
257     fclose(fh);
258 }
259
260 void effectinfo_dump(int fh, bool alsoprint)
261 {
262     #define WRITE(s) do { \
263         fputs(fh, s); \
264         if (alsoprint) LOG_INFO(s); \
265     } while (0)
266     WRITE("// ********************************************** //\n");
267     WRITE("// ** WARNING - DO NOT MANUALLY EDIT THIS FILE ** //\n");
268     WRITE("// **                                          ** //\n");
269     WRITE("// **  This file is automatically generated by ** //\n");
270     WRITE("// **  code with the command 'dumpeffectinfo'. ** //\n");
271     WRITE("// **                                          ** //\n");
272     WRITE("// **  If you modify an effect, please         ** //\n");
273     WRITE("// **  regenerate this file with that command. ** //\n");
274     WRITE("// **                                          ** //\n");
275     WRITE("// ********************************************** //\n");
276     WRITE("\n");
277
278     for (EffectInfo it = NULL; (it = findfloat(it, instanceOfEffectInfo, true)); ) {
279         if (it.classname == "vtbl") continue;
280         string s = it.dump(it);
281         WRITE(s);
282     }
283     #undef WRITE
284 }
285
286 GENERIC_COMMAND(dumpeffectinfo, "Dump all effectinfo to effectinfo_dump.txt")
287 {
288     switch (request) {
289         case CMD_REQUEST_COMMAND: {
290             string filename = argv(1);
291                         bool alsoprint = false;
292             if (filename == "") {
293                 filename = "effectinfo_dump.txt";
294                 alsoprint = false;
295             } else if (filename == "-") {
296                 filename = "effectinfo_dump.txt";
297                 alsoprint = true;
298             }
299             int fh = fopen(filename, FILE_WRITE);
300             if (fh >= 0) {
301                 effectinfo_dump(fh, alsoprint);
302                 LOG_INFOF("Dumping effectinfo... File located at ^2data/data/%s^7.\n", filename);
303                                 LOG_INFOF("Reload with ^2cl_particles_reloadeffects data/%s^7.\n", filename);
304                 fclose(fh);
305             } else {
306                 LOG_WARNINGF("Could not open file '%s'!\n", filename);
307             }
308             return;
309         }
310         default:
311         case CMD_REQUEST_USAGE: {
312             LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpeffectinfo [filename]"));
313             LOG_INFO("  Where 'filename' is the file to write (default is effectinfo_dump.txt),\n");
314             LOG_INFO("  if supplied with '-' output to console as well as default,\n");
315             LOG_INFO("  if left blank, it will only write to default.\n");
316             return;
317         }
318     }
319 }
320
321
322 REGISTRY(EffectInfos, BITS(9))
323 REGISTER_REGISTRY(RegisterEffectInfos)
324 #define EFFECTINFO(name) \
325     [[accumulate]] void effectinfo_##name(EffectInfoGroup parent, EffectInfo this) { } \
326     REGISTER(RegisterEffectInfos, EFFECTINFO, EffectInfos, name, m_id, NEW(EffectInfoGroup)) { \
327         effectinfo_##name(this, NULL); \
328     }
329
330 #define MY(f) this.effectinfo_##f
331 #define DEF(name) EFFECTINFO(name)
332 #define SUB(name) \
333     [[accumulate]] void effectinfo_##name(EffectInfoGroup parent, EffectInfo this) { parent = EFFECTINFO_##name; parent.children[parent.children_count++] = this = NEW(EffectInfo, #name); } \
334     [[accumulate]] void effectinfo_##name(EffectInfoGroup parent, EffectInfo this)
335 #include "effectinfo.inc"
336 #undef MY
337 #undef DEF
338 #undef SUB