1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// command.cpp: implements the parsing and execution of a tiny script language which
// is largely backwards compatible with the quake console language.
#include "cube.h"
enum { ID_VAR, ID_COMMAND, ID_ALIAS };
struct ident
{
int type; // one of ID_* above
char *name;
int min, max; // ID_VAR
int *storage; // ID_VAR
void (*fun)(); // ID_VAR, ID_COMMAND
int narg; // ID_VAR, ID_COMMAND
char *action; // ID_ALIAS
bool persist;
};
void itoa(char *s, int i) { sprintf_s(s)("%d", i); };
char *exchangestr(char *o, char *n) { gp()->deallocstr(o); return newstring(n); };
hashtable<ident> *idents = NULL; // contains ALL vars/commands/aliases
void alias(char *name, char *action)
{
ident *b = idents->access(name);
if(!b)
{
name = newstring(name);
ident b = { ID_ALIAS, name, 0, 0, 0, 0, 0, newstring(action), true };
idents->access(name, &b);
}
else
{
if(b->type==ID_ALIAS) b->action = exchangestr(b->action, action);
else conoutf("cannot redefine builtin %s with an alias", name);
};
};
COMMAND(alias, ARG_2STR);
// variable's and commands are registered through globals, see cube.h
int variable(char *name, int min, int cur, int max, int *storage, void (*fun)(), bool persist)
{
if(!idents) idents = new hashtable<ident>;
ident v = { ID_VAR, name, min, max, storage, fun, 0, 0, persist };
idents->access(name, &v);
return cur;
};
void setvar(char *name, int i) { *idents->access(name)->storage = i; };
int getvar(char *name) { return *idents->access(name)->storage; };
bool identexists(char *name) { return idents->access(name)!=NULL; };
char *getalias(char *name)
{
ident *i = idents->access(name);
return i && i->type==ID_ALIAS ? i->action : NULL;
};
bool addcommand(char *name, void (*fun)(), int narg)
{
if(!idents) idents = new hashtable<ident>;
ident c = { ID_COMMAND, name, 0, 0, 0, fun, narg, 0, false };
idents->access(name, &c);
return false;
};
char *parseexp(char *&p, int right) // parse any nested set of () or []
{
int left = *p++;
char *word = p;
for(int brak = 1; brak; )
{
int c = *p++;
if(c=='\r') *(p-1) = ' '; // hack
if(c==left) brak++;
else if(c==right) brak--;
else if(!c) { p--; conoutf("missing \"%c\"", right); return NULL; };
};
char *s = newstring(word, p-word-1);
if(left=='(')
{
string t;
itoa(t, execute(s)); // evaluate () exps directly, and substitute result
s = exchangestr(s, t);
};
return s;
};
char *parseword(char *&p) // parse single argument, including expressions
{
p += strspn(p, " \t\r");
if(p[0]=='/' && p[1]=='/') p += strcspn(p, "\n\0");
if(*p=='\"')
{
p++;
char *word = p;
p += strcspn(p, "\"\r\n\0");
char *s = newstring(word, p-word);
if(*p=='\"') p++;
return s;
};
if(*p=='(') return parseexp(p, ')');
if(*p=='[') return parseexp(p, ']');
char *word = p;
p += strcspn(p, "; \t\r\n\0");
if(p-word==0) return NULL;
return newstring(word, p-word);
};
char *lookup(char *n) // find value of ident referenced with $ in exp
{
ident *id = idents->access(n+1);
if(id) switch(id->type)
{
case ID_VAR: string t; itoa(t, *(id->storage)); return exchangestr(n, t);
case ID_ALIAS: return exchangestr(n, id->action);
};
conoutf("unknown alias lookup: %s", n+1);
return n;
};
int execute(char *p, bool isdown) // all evaluation happens here, recursively
{
const int MAXWORDS = 25; // limit, remove
char *w[MAXWORDS];
int val = 0;
for(bool cont = true; cont;) // for each ; seperated statement
{
int numargs = MAXWORDS;
loopi(MAXWORDS) // collect all argument values
{
w[i] = "";
if(i>numargs) continue;
char *s = parseword(p); // parse and evaluate exps
if(!s) { numargs = i; s = ""; };
if(*s=='$') s = lookup(s); // substitute variables
w[i] = s;
};
p += strcspn(p, ";\n\0");
cont = *p++!=0; // more statements if this isn't the end of the string
char *c = w[0];
if(*c=='/') c++; // strip irc-style command prefix
if(!*c) continue; // empty statement
ident *id = idents->access(c);
if(!id)
{
val = ATOI(c);
if(!val && *c!='0') conoutf("unknown command: %s", c);
}
else switch(id->type)
{
case ID_COMMAND: // game defined commands
switch(id->narg) // use very ad-hoc function signature, and just call it
{
case ARG_1INT: if(isdown) ((void (__cdecl *)(int))id->fun)(ATOI(w[1])); break;
case ARG_2INT: if(isdown) ((void (__cdecl *)(int, int))id->fun)(ATOI(w[1]), ATOI(w[2])); break;
case ARG_3INT: if(isdown) ((void (__cdecl *)(int, int, int))id->fun)(ATOI(w[1]), ATOI(w[2]), ATOI(w[3])); break;
case ARG_4INT: if(isdown) ((void (__cdecl *)(int, int, int, int))id->fun)(ATOI(w[1]), ATOI(w[2]), ATOI(w[3]), ATOI(w[4])); break;
case ARG_NONE: if(isdown) ((void (__cdecl *)())id->fun)(); break;
case ARG_1STR: if(isdown) ((void (__cdecl *)(char *))id->fun)(w[1]); break;
case ARG_2STR: if(isdown) ((void (__cdecl *)(char *, char *))id->fun)(w[1], w[2]); break;
case ARG_3STR: if(isdown) ((void (__cdecl *)(char *, char *, char*))id->fun)(w[1], w[2], w[3]); break;
case ARG_5STR: if(isdown) ((void (__cdecl *)(char *, char *, char*, char*, char*))id->fun)(w[1], w[2], w[3], w[4], w[5]); break;
case ARG_DOWN: ((void (__cdecl *)(bool))id->fun)(isdown); break;
case ARG_DWN1: ((void (__cdecl *)(bool, char *))id->fun)(isdown, w[1]); break;
case ARG_1EXP: if(isdown) val = ((int (__cdecl *)(int))id->fun)(execute(w[1])); break;
case ARG_2EXP: if(isdown) val = ((int (__cdecl *)(int, int))id->fun)(execute(w[1]), execute(w[2])); break;
case ARG_1EST: if(isdown) val = ((int (__cdecl *)(char *))id->fun)(w[1]); break;
case ARG_2EST: if(isdown) val = ((int (__cdecl *)(char *, char *))id->fun)(w[1], w[2]); break;
case ARG_VARI: if(isdown)
{
string r; // limit, remove
r[0] = 0;
for(int i = 1; i<numargs; i++)
{
strcat_s(r, w[i]); // make string-list out of all arguments
if(i==numargs-1) break;
strcat_s(r, " ");
};
((void (__cdecl *)(char *))id->fun)(r);
break;
}
};
break;
case ID_VAR: // game defined variabled
if(isdown)
{
if(!w[1][0]) conoutf("%s = %d", c, *id->storage); // var with no value just prints its current value
else
{
if(id->min>id->max)
{
conoutf("variable is read-only");
}
else
{
int i1 = ATOI(w[1]);
if(i1<id->min || i1>id->max)
{
i1 = i1<id->min ? id->min : id->max; // clamp to valid range
conoutf("valid range for %s is %d..%d", c, id->min, id->max);
}
*id->storage = i1;
};
if(id->fun) ((void (__cdecl *)())id->fun)(); // call trigger function if available
};
};
break;
case ID_ALIAS: // alias, also used as functions and (global) variables
for(int i = 1; i<numargs; i++)
{
sprintf_sd(t)("arg%d", i); // set any arguments as (global) arg values so functions can access them
alias(t, w[i]);
};
char *action = newstring(id->action); // create new string here because alias could rebind itself
val = execute(action, isdown);
gp()->deallocstr(action);
break;
};
loopj(numargs) gp()->deallocstr(w[j]);
};
return val;
};
// tab-completion of all idents
int completesize = 0, completeidx = 0;
void resetcomplete() { completesize = 0; };
void complete(char *s)
{
if(*s!='/')
{
string t;
strcpy_s(t, s);
strcpy_s(s, "/");
strcat_s(s, t);
};
if(!s[1]) return;
if(!completesize) { completesize = (int)strlen(s)-1; completeidx = 0; };
int idx = 0;
enumerate(idents, ident *, id,
if(strncmp(id->name, s+1, completesize)==0 && idx++==completeidx)
{
strcpy_s(s, "/");
strcat_s(s, id->name);
};
);
completeidx++;
if(completeidx>=idx) completeidx = 0;
};
bool execfile(char *cfgfile)
{
string s;
strcpy_s(s, cfgfile);
char *buf = loadfile(path(s), NULL);
if(!buf) return false;
execute(buf);
free(buf);
return true;
};
void exec(char *cfgfile)
{
if(!execfile(cfgfile)) conoutf("could not read \"%s\"", cfgfile);
};
void writecfg()
{
FILE *f = fopen("config.cfg", "w");
if(!f) return;
fprintf(f, "// automatically written on exit, do not modify\n// delete this file to have defaults.cfg overwrite these settings\n// modify settings in game, or put settings in autoexec.cfg to override anything\n\n");
writeclientinfo(f);
fprintf(f, "\n");
enumerate(idents, ident *, id,
if(id->type==ID_VAR && id->persist)
{
fprintf(f, "%s %d\n", id->name, *id->storage);
};
);
fprintf(f, "\n");
writebinds(f);
fprintf(f, "\n");
enumerate(idents, ident *, id,
if(id->type==ID_ALIAS && !strstr(id->name, "nextmap_"))
{
fprintf(f, "alias \"%s\" [%s]\n", id->name, id->action);
};
);
fclose(f);
};
COMMAND(writecfg, ARG_NONE);
// below the commands that implement a small imperative language. thanks to the semantics of
// () and [] expressions, any control construct can be defined trivially.
void intset(char *name, int v) { string b; itoa(b, v); alias(name, b); };
void ifthen(char *cond, char *thenp, char *elsep) { execute(cond[0]!='0' ? thenp : elsep); };
void loopa(char *times, char *body) { int t = atoi(times); loopi(t) { intset("i", i); execute(body); }; };
void whilea(char *cond, char *body) { while(execute(cond)) execute(body); }; // can't get any simpler than this :)
void onrelease(bool on, char *body) { if(!on) execute(body); };
void concat(char *s) { alias("s", s); };
void concatword(char *s)
{
for(char *a = s, *b = s; *a = *b; b++) if(*a!=' ') a++;
concat(s);
};
int listlen(char *a)
{
if(!*a) return 0;
int n = 0;
while(*a) if(*a++==' ') n++;
return n+1;
};
void at(char *s, char *pos)
{
int n = atoi(pos);
loopi(n) s += strspn(s += strcspn(s, " \0"), " ");
s[strcspn(s, " \0")] = 0;
concat(s);
};
COMMANDN(loop, loopa, ARG_2STR);
COMMANDN(while, whilea, ARG_2STR);
COMMANDN(if, ifthen, ARG_3STR);
COMMAND(onrelease, ARG_DWN1);
COMMAND(exec, ARG_1STR);
COMMAND(concat, ARG_VARI);
COMMAND(concatword, ARG_VARI);
COMMAND(at, ARG_2STR);
COMMAND(listlen, ARG_1EST);
int add(int a, int b) { return a+b; }; COMMANDN(+, add, ARG_2EXP);
int mul(int a, int b) { return a*b; }; COMMANDN(*, mul, ARG_2EXP);
int sub(int a, int b) { return a-b; }; COMMANDN(-, sub, ARG_2EXP);
int divi(int a, int b) { return b ? a/b : 0; }; COMMANDN(div, divi, ARG_2EXP);
int mod(int a, int b) { return b ? a%b : 0; }; COMMAND(mod, ARG_2EXP);
int equal(int a, int b) { return (int)(a==b); }; COMMANDN(=, equal, ARG_2EXP);
int lt(int a, int b) { return (int)(a<b); }; COMMANDN(<, lt, ARG_2EXP);
int gt(int a, int b) { return (int)(a>b); }; COMMANDN(>, gt, ARG_2EXP);
int strcmpa(char *a, char *b) { return strcmp(a,b)==0; }; COMMANDN(strcmp, strcmpa, ARG_2EST);
int rndn(int a) { return a>0 ? rnd(a) : 0; }; COMMANDN(rnd, rndn, ARG_1EXP);
int explastmillis() { return lastmillis; }; COMMANDN(millis, explastmillis, ARG_1EXP);
|
|
|
|
<
|
|
|
|
|
|
|
|
>
|
<
|
<
|
>
|
>
|
<
<
>
>
>
>
>
>
>
>
>
>
|
>
|
|
<
|
<
>
|
>
>
|
|
>
|
>
>
|
|
|
|
>
>
>
|
>
>
>
>
|
>
>
>
>
|
>
>
|
|
|
>
|
|
>
|
|
|
|
>
|
|
|
|
<
|
>
|
>
|
>
|
>
>
|
>
|
>
|
|
<
|
>
>
|
|
|
|
>
|
|
>
|
|
<
|
|
|
|
>
|
|
|
>
|
>
|
|
|
>
|
|
>
>
>
>
>
>
|
|
<
<
>
|
<
|
>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
|
<
>
>
>
|
|
>
>
|
|
>
>
|
<
>
>
>
>
>
>
>
|
<
>
|
<
<
<
>
|
<
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
>
>
>
>
>
|
|
>
|
<
|
<
<
|
<
>
>
|
>
|
<
>
>
>
>
|
>
>
>
|
|
|
>
|
>
>
>
>
>
|
>
|
|
>
>
|
>
|
|
|
|
>
|
|
>
>
|
<
>
|
|
|
<
>
|
>
>
>
>
>
|
|
|
>
|
|
|
|
|
|
|
|
<
>
|
>
>
>
>
>
>
|
<
|
<
|
<
>
|
<
>
>
>
>
>
>
>
>
>
>
|
>
|
<
<
|
|
|
|
>
|
>
>
>
>
>
|
|
|
|
|
>
|
<
|
>
>
>
|
|
|
>
>
>
|
|
|
|
|
|
|
>
|
|
|
>
|
|
>
|
>
|
|
|
|
|
>
|
>
|
>
>
|
>
|
|
<
>
|
|
|
<
|
>
|
>
|
|
|
|
>
|
|
|
|
>
|
>
|
>
|
|
>
|
|
>
>
>
|
|
|
|
<
|
|
<
|
|
|
>
|
|
<
|
|
<
|
|
>
>
|
|
>
>
>
>
>
>
|
<
<
<
|
>
>
>
|
|
<
>
>
|
<
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
|
|
>
>
|
>
|
|
|
|
|
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
|
|
>
>
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
// command.cpp: implements the parsing and execution of a tiny script language
// which is largely backwards compatible with the quake console language.
#include "cube.h"
enum { ID_VAR, ID_COMMAND, ID_ALIAS };
struct ident {
int type; // one of ID_* above
char *name;
int min, max; // ID_VAR
int *storage; // ID_VAR
void (*fun)(); // ID_VAR, ID_COMMAND
int narg; // ID_VAR, ID_COMMAND
char *action; // ID_ALIAS
bool persist;
};
void
itoa(char *s, int i)
{
sprintf_s(s)("%d", i);
};
char *
exchangestr(char *o, char *n)
{
gp()->deallocstr(o);
return newstring(n);
};
hashtable<ident> *idents = NULL; // contains ALL vars/commands/aliases
void
alias(char *name, char *action)
{
ident *b = idents->access(name);
if (!b) {
name = newstring(name);
ident b = {
ID_ALIAS, name, 0, 0, 0, 0, 0, newstring(action), true};
idents->access(name, &b);
} else {
if (b->type == ID_ALIAS)
b->action = exchangestr(b->action, action);
else
conoutf(
"cannot redefine builtin %s with an alias", name);
};
};
COMMAND(alias, ARG_2STR);
// variable's and commands are registered through globals, see cube.h
int
variable(char *name, int min, int cur, int max, int *storage, void (*fun)(),
bool persist)
{
if (!idents)
idents = new hashtable<ident>;
ident v = {ID_VAR, name, min, max, storage, fun, 0, 0, persist};
idents->access(name, &v);
return cur;
};
void
setvar(char *name, int i)
{
*idents->access(name)->storage = i;
};
int
getvar(char *name)
{
return *idents->access(name)->storage;
};
bool
identexists(char *name)
{
return idents->access(name) != NULL;
};
char *
getalias(char *name)
{
ident *i = idents->access(name);
return i && i->type == ID_ALIAS ? i->action : NULL;
};
bool
addcommand(char *name, void (*fun)(), int narg)
{
if (!idents)
idents = new hashtable<ident>;
ident c = {ID_COMMAND, name, 0, 0, 0, fun, narg, 0, false};
idents->access(name, &c);
return false;
};
char *
parseexp(char *&p, int right) // parse any nested set of () or []
{
int left = *p++;
char *word = p;
for (int brak = 1; brak;) {
int c = *p++;
if (c == '\r')
*(p - 1) = ' '; // hack
if (c == left)
brak++;
else if (c == right)
brak--;
else if (!c) {
p--;
conoutf("missing \"%c\"", right);
return NULL;
};
};
char *s = newstring(word, p - word - 1);
if (left == '(') {
string t;
itoa(t,
execute(
s)); // evaluate () exps directly, and substitute result
s = exchangestr(s, t);
};
return s;
};
char *
parseword(char *&p) // parse single argument, including expressions
{
p += strspn(p, " \t\r");
if (p[0] == '/' && p[1] == '/')
p += strcspn(p, "\n\0");
if (*p == '\"') {
p++;
char *word = p;
p += strcspn(p, "\"\r\n\0");
char *s = newstring(word, p - word);
if (*p == '\"')
p++;
return s;
};
if (*p == '(')
return parseexp(p, ')');
if (*p == '[')
return parseexp(p, ']');
char *word = p;
p += strcspn(p, "; \t\r\n\0");
if (p - word == 0)
return NULL;
return newstring(word, p - word);
};
char *
lookup(char *n) // find value of ident referenced with $ in exp
{
ident *id = idents->access(n + 1);
if (id)
switch (id->type) {
case ID_VAR:
string t;
itoa(t, *(id->storage));
return exchangestr(n, t);
case ID_ALIAS:
return exchangestr(n, id->action);
};
conoutf("unknown alias lookup: %s", n + 1);
return n;
};
int
execute(char *p, bool isdown) // all evaluation happens here, recursively
{
const int MAXWORDS = 25; // limit, remove
char *w[MAXWORDS];
int val = 0;
for (bool cont = true; cont;) // for each ; seperated statement
{
int numargs = MAXWORDS;
loopi(MAXWORDS) // collect all argument values
{
w[i] = "";
if (i > numargs)
continue;
char *s = parseword(p); // parse and evaluate exps
if (!s) {
numargs = i;
s = "";
};
if (*s == '$')
s = lookup(s); // substitute variables
w[i] = s;
};
p += strcspn(p, ";\n\0");
cont = *p++ !=
0; // more statements if this isn't the end of the string
char *c = w[0];
if (*c == '/')
c++; // strip irc-style command prefix
if (!*c)
continue; // empty statement
ident *id = idents->access(c);
if (!id) {
val = ATOI(c);
if (!val && *c != '0')
conoutf("unknown command: %s", c);
} else
switch (id->type) {
case ID_COMMAND: // game defined commands
switch (id->narg) // use very ad-hoc function
// signature, and just call it
{
case ARG_1INT:
if (isdown)
((void(__cdecl *)(int))id->fun)(
ATOI(w[1]));
break;
case ARG_2INT:
if (isdown)
((void(__cdecl *)(
int, int))id->fun)(
ATOI(w[1]), ATOI(w[2]));
break;
case ARG_3INT:
if (isdown)
((void(__cdecl *)(int, int,
int))id->fun)(ATOI(w[1]),
ATOI(w[2]), ATOI(w[3]));
break;
case ARG_4INT:
if (isdown)
((void(__cdecl *)(int, int, int,
int))id->fun)(ATOI(w[1]),
ATOI(w[2]), ATOI(w[3]),
ATOI(w[4]));
break;
case ARG_NONE:
if (isdown)
((void(__cdecl *)())id->fun)();
break;
case ARG_1STR:
if (isdown)
((void(__cdecl *)(
char *))id->fun)(w[1]);
break;
case ARG_2STR:
if (isdown)
((void(__cdecl *)(
char *, char *))id->fun)(
w[1], w[2]);
break;
case ARG_3STR:
if (isdown)
((void(__cdecl *)(char *,
char *, char *))id->fun)(
w[1], w[2], w[3]);
break;
case ARG_5STR:
if (isdown)
((void(__cdecl *)(char *,
char *, char *, char *,
char *))id->fun)(w[1], w[2],
w[3], w[4], w[5]);
break;
case ARG_DOWN:
((void(__cdecl *)(bool))id->fun)(
isdown);
break;
case ARG_DWN1:
((void(__cdecl *)(bool,
char *))id->fun)(isdown, w[1]);
break;
case ARG_1EXP:
if (isdown)
val = ((int(__cdecl *)(
int))id->fun)(
execute(w[1]));
break;
case ARG_2EXP:
if (isdown)
val = ((int(__cdecl *)(int,
int))id->fun)(execute(w[1]),
execute(w[2]));
break;
case ARG_1EST:
if (isdown)
val = ((int(__cdecl *)(
char *))id->fun)(w[1]);
break;
case ARG_2EST:
if (isdown)
val = ((int(__cdecl *)(
char *, char *))id->fun)(
w[1], w[2]);
break;
case ARG_VARI:
if (isdown) {
string r; // limit, remove
r[0] = 0;
for (int i = 1; i < numargs;
i++) {
strcat_s(r,
w[i]); // make
// string-list
// out of all
// arguments
if (i == numargs - 1)
break;
strcat_s(r, " ");
};
((void(__cdecl *)(
char *))id->fun)(r);
break;
}
};
break;
case ID_VAR: // game defined variabled
if (isdown) {
if (!w[1][0])
conoutf("%s = %d", c,
*id->storage); // var with
// no value
// just
// prints its
// current
// value
else {
if (id->min > id->max) {
conoutf("variable is "
"read-only");
} else {
int i1 = ATOI(w[1]);
if (i1 < id->min ||
i1 > id->max) {
i1 =
i1 < id->min
? id->min
: id->max; // clamp to valid range
conoutf(
"valid "
"range for "
"%s is "
"%d..%d",
c, id->min,
id->max);
}
*id->storage = i1;
};
if (id->fun)
((void(__cdecl *)())id
->fun)(); // call
// trigger
// function
// if
// available
};
};
break;
case ID_ALIAS: // alias, also used as functions and
// (global) variables
for (int i = 1; i < numargs; i++) {
sprintf_sd(t)("arg%d",
i); // set any arguments as (global)
// arg values so functions can
// access them
alias(t, w[i]);
};
char *action = newstring(
id->action); // create new string here
// because alias could rebind
// itself
val = execute(action, isdown);
gp()->deallocstr(action);
break;
};
loopj(numargs) gp()->deallocstr(w[j]);
};
return val;
};
// tab-completion of all idents
int completesize = 0, completeidx = 0;
void
resetcomplete()
{
completesize = 0;
};
void
complete(char *s)
{
if (*s != '/') {
string t;
strcpy_s(t, s);
strcpy_s(s, "/");
strcat_s(s, t);
};
if (!s[1])
return;
if (!completesize) {
completesize = (int)strlen(s) - 1;
completeidx = 0;
};
int idx = 0;
enumerate(
idents, ident *, id,
if (strncmp(id->name, s + 1, completesize) == 0 &&
idx++ == completeidx) {
strcpy_s(s, "/");
strcat_s(s, id->name);
};);
completeidx++;
if (completeidx >= idx)
completeidx = 0;
};
bool
execfile(char *cfgfile)
{
string s;
strcpy_s(s, cfgfile);
char *buf = loadfile(path(s), NULL);
if (!buf)
return false;
execute(buf);
free(buf);
return true;
};
void
exec(char *cfgfile)
{
if (!execfile(cfgfile))
conoutf("could not read \"%s\"", cfgfile);
};
void
writecfg()
{
FILE *f = fopen("config.cfg", "w");
if (!f)
return;
fprintf(f, "// automatically written on exit, do not modify\n// delete "
"this file to have defaults.cfg overwrite these "
"settings\n// modify settings in game, or put settings in "
"autoexec.cfg to override anything\n\n");
writeclientinfo(f);
fprintf(f, "\n");
enumerate(
idents, ident *, id, if (id->type == ID_VAR && id->persist) {
fprintf(f, "%s %d\n", id->name, *id->storage);
};);
fprintf(f, "\n");
writebinds(f);
fprintf(f, "\n");
enumerate(
idents, ident *, id,
if (id->type == ID_ALIAS && !strstr(id->name, "nextmap_")) {
fprintf(f, "alias \"%s\" [%s]\n", id->name, id->action);
};);
fclose(f);
};
COMMAND(writecfg, ARG_NONE);
// below the commands that implement a small imperative language. thanks to the
// semantics of
// () and [] expressions, any control construct can be defined trivially.
void
intset(char *name, int v)
{
string b;
itoa(b, v);
alias(name, b);
};
void
ifthen(char *cond, char *thenp, char *elsep)
{
execute(cond[0] != '0' ? thenp : elsep);
};
void
loopa(char *times, char *body)
{
int t = atoi(times);
loopi(t)
{
intset("i", i);
execute(body);
};
};
void
whilea(char *cond, char *body)
{
while (execute(cond))
execute(body);
}; // can't get any simpler than this :)
void
onrelease(bool on, char *body)
{
if (!on)
execute(body);
};
void
concat(char *s)
{
alias("s", s);
};
void
concatword(char *s)
{
for (char *a = s, *b = s; *a = *b; b++)
if (*a != ' ')
a++;
concat(s);
};
int
listlen(char *a)
{
if (!*a)
return 0;
int n = 0;
while (*a)
if (*a++ == ' ')
n++;
return n + 1;
};
void
at(char *s, char *pos)
{
int n = atoi(pos);
loopi(n) s += strspn(s += strcspn(s, " \0"), " ");
s[strcspn(s, " \0")] = 0;
concat(s);
};
COMMANDN(loop, loopa, ARG_2STR);
COMMANDN(while, whilea, ARG_2STR);
COMMANDN(if, ifthen, ARG_3STR);
COMMAND(onrelease, ARG_DWN1);
COMMAND(exec, ARG_1STR);
COMMAND(concat, ARG_VARI);
COMMAND(concatword, ARG_VARI);
COMMAND(at, ARG_2STR);
COMMAND(listlen, ARG_1EST);
int
add(int a, int b)
{
return a + b;
};
COMMANDN(+, add, ARG_2EXP);
int
mul(int a, int b)
{
return a * b;
};
COMMANDN(*, mul, ARG_2EXP);
int
sub(int a, int b)
{
return a - b;
};
COMMANDN(-, sub, ARG_2EXP);
int
divi(int a, int b)
{
return b ? a / b : 0;
};
COMMANDN(div, divi, ARG_2EXP);
int
mod(int a, int b)
{
return b ? a % b : 0;
};
COMMAND(mod, ARG_2EXP);
int
equal(int a, int b)
{
return (int)(a == b);
};
COMMANDN(=, equal, ARG_2EXP);
int
lt(int a, int b)
{
return (int)(a < b);
};
COMMANDN(<, lt, ARG_2EXP);
int
gt(int a, int b)
{
return (int)(a > b);
};
COMMANDN(>, gt, ARG_2EXP);
int
strcmpa(char *a, char *b)
{
return strcmp(a, b) == 0;
};
COMMANDN(strcmp, strcmpa, ARG_2EST);
int
rndn(int a)
{
return a > 0 ? rnd(a) : 0;
};
COMMANDN(rnd, rndn, ARG_1EXP);
int
explastmillis()
{
return lastmillis;
};
COMMANDN(millis, explastmillis, ARG_1EXP);
|