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
|
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
|
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
|
// 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 };
@interface Ident : OFObject
@property (nonatomic) int type; // one of ID_* above
@property (nonatomic) char *name;
@property (nonatomic) int min, max; // ID_VAR
@property (nonatomic) int *storage; // ID_VAR
@property (nonatomic) void (*fun)(); // ID_VAR, ID_COMMAND
@property (nonatomic) int narg; // ID_VAR, ID_COMMAND
@property (nonatomic) char *action; // ID_ALIAS
@property (copy, nonatomic) OFString *name;
@property (nonatomic) int min, max; // ID_VAR
@property (nonatomic) int *storage; // ID_VAR
@property (nonatomic) void (*fun)(); // ID_VAR, ID_COMMAND
@property (nonatomic) int narg; // ID_VAR, ID_COMMAND
@property (copy, nonatomic) OFString *action; // ID_ALIAS
@property (nonatomic) bool persist;
@end
@implementation Ident
@end
void
itoa(char *s, int i)
{
sprintf_s(s)("%d", i);
}
char *
exchangestr(char *o, char *n)
exchangestr(char *o, const char *n)
{
gp()->deallocstr(o);
return newstring(n);
}
// contains ALL vars/commands/aliases
OFMutableDictionary<OFString *, Ident *> *idents;
void
alias(char *name, char *action)
{
@autoreleasepool {
Ident *b = idents[@(name)];
if (!b) {
Ident *b = [[Ident alloc] init];
b.type = ID_ALIAS;
b.name = newstring(name);
b.action = newstring(action);
b.name = @(name);
b.action = @(action);
b.persist = true;
idents[@(name)] = b;
idents[b.name] = b;
} else {
if (b.type == ID_ALIAS)
b.action = exchangestr(b.action, action);
b.action = @(action);
else
conoutf(
@"cannot redefine builtin %s with an alias",
name);
}
}
}
COMMAND(alias, ARG_2STR)
int
variable(char *name, int min, int cur, int max, int *storage, void (*fun)(),
bool persist)
{
if (idents == nil)
idents = [[OFMutableDictionary alloc] init];
@autoreleasepool {
Ident *v = [[Ident alloc] init];
v.type = ID_VAR;
v.name = name;
v.name = @(name);
v.min = min;
v.max = max;
v.storage = storage;
v.fun = fun;
v.persist = persist;
idents[@(name)] = v;
idents[v.name] = v;
}
return cur;
}
void
setvar(char *name, int i)
|
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
-
+
-
+
|
if (!completesize) {
completesize = (int)strlen(s) - 1;
completeidx = 0;
}
__block int idx = 0;
[idents enumerateKeysAndObjectsUsingBlock:^(
OFString *name, Ident *ident, bool *stop) {
if (strncmp(ident.name, s + 1, completesize) == 0 &&
if (strncmp(ident.name.UTF8String, s + 1, completesize) == 0 &&
idx++ == completeidx) {
strcpy_s(s, "/");
strcat_s(s, ident.name);
strcat_s(s, ident.name.UTF8String);
}
}];
completeidx++;
if (completeidx >= idx)
completeidx = 0;
}
|
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
|
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
|
-
+
+
-
-
-
+
+
+
+
-
+
|
"settings\n// modify settings in game, or put settings in "
"autoexec.cfg to override anything\n\n");
writeclientinfo(f);
fprintf(f, "\n");
[idents enumerateKeysAndObjectsUsingBlock:^(
OFString *name, Ident *ident, bool *stop) {
if (ident.type == ID_VAR && ident.persist) {
fprintf(f, "%s %d\n", ident.name, *ident.storage);
fprintf(f, "%s %d\n", ident.name.UTF8String,
*ident.storage);
}
}];
fprintf(f, "\n");
writebinds(f);
fprintf(f, "\n");
[idents enumerateKeysAndObjectsUsingBlock:^(
OFString *name, Ident *ident, bool *stop) {
if (ident.type == ID_ALIAS && !strstr(ident.name, "nextmap_")) {
fprintf(
f, "alias \"%s\" [%s]\n", ident.name, ident.action);
if (ident.type == ID_ALIAS &&
!strstr(ident.name.UTF8String, "nextmap_")) {
fprintf(f, "alias \"%s\" [%s]\n", ident.name.UTF8String,
ident.action.UTF8String);
}
}];
fclose(f);
}
COMMAND(writecfg, ARG_NONE);
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)
|
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
-
+
-
+
-
+
|
{
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);
COMMANDN(loop, loopa, ARG_2STR)
COMMANDN(while, whilea, ARG_2STR)
COMMANDN(if, ifthen, ARG_3STR)
COMMAND(onrelease, ARG_DWN1)
COMMAND(exec, ARG_1CSTR)
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);
COMMANDN(+, add, ARG_2EXP)
int
mul(int a, int b)
{
return a * b;
}
COMMANDN(*, mul, ARG_2EXP);
COMMANDN(*, mul, ARG_2EXP)
int
sub(int a, int b)
{
return a - b;
}
COMMANDN(-, sub, ARG_2EXP);
COMMANDN(-, sub, ARG_2EXP)
int
divi(int a, int b)
{
return b ? a / b : 0;
}
COMMANDN(div, divi, ARG_2EXP);
COMMANDN(div, divi, ARG_2EXP)
int
mod(int a, int b)
{
return b ? a % b : 0;
}
COMMAND(mod, ARG_2EXP);
COMMAND(mod, ARG_2EXP)
int
equal(int a, int b)
{
return (int)(a == b);
}
COMMANDN(=, equal, ARG_2EXP);
COMMANDN(=, equal, ARG_2EXP)
int
lt(int a, int b)
{
return (int)(a < b);
}
COMMANDN(<, lt, ARG_2EXP);
COMMANDN(<, lt, ARG_2EXP)
int
gt(int a, int b)
{
return (int)(a > b);
}
COMMANDN(>, gt, ARG_2EXP);
COMMANDN(>, gt, ARG_2EXP)
int
strcmpa(char *a, char *b)
{
return strcmp(a, b) == 0;
}
COMMANDN(strcmp, strcmpa, ARG_2EST);
COMMANDN(strcmp, strcmpa, ARG_2EST)
int
rndn(int a)
{
return a > 0 ? rnd(a) : 0;
}
COMMANDN(rnd, rndn, ARG_1EXP);
COMMANDN(rnd, rndn, ARG_1EXP)
int
explastmillis()
{
return lastmillis;
}
COMMANDN(millis, explastmillis, ARG_1EXP);
COMMANDN(millis, explastmillis, ARG_1EXP)
|