-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_math.qc
231 lines (186 loc) · 5.17 KB
/
io_math.qc
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
const float IOM_SPAWNFLAG_REVERSE = 1;
const float IOM_SPAWNFLAG_BOUND_U = 1;
const float IOM_SPAWNFLAG_BOUND_L = 2;
const float IOM_SPAWNFLAG_FIRE_CHANGE_ONLY = 4;
const float IOM_SPAWNFLAG_FIRE_BOUNDS_ONLY = 8;
const float SPAWNFLAG_ALLOW_NONREAL = 64;
const float IOM_OP_ADD = 0;
const float IOM_OP_SUB = 1;
const float IOM_OP_MUL = 2;
const float IOM_OP_DIV = 3;
const float IOM_OP_MOD = 4;
const float IOM_OP_FLOOR = 5;
const float IOM_OP_CEIL = 6;
const float IOM_OP_MIN = 7;
const float IOM_OP_MAX = 8;
const float IOM_OP_ISNAN = 9;
const float IOM_OP_ISREAL = 10;
const float IOM_CMP_LT = 0;
const float IOM_CMP_LTE = 1;
const float IOM_CMP_GT = 2;
const float IOM_CMP_GTE = 3;
const float IOM_CMP_EQ = 4;
const float IOM_CMP_NE = 5;
static float(float x) AllowedValue = {
if ((self.spawnflags & SPAWNFLAG_ALLOW_NONREAL) || isfinite(x)) {
return TRUE;
} else {
return FALSE;
}
}
void() ApplyOp = {
float reverse = self.spawnflags & IOM_SPAWNFLAG_REVERSE;
float send;
switch (self.style) {
case IOM_OP_SUB:
if (reverse) {
send = self.io_arg2 - self.io_received;
} else {
send = self.io_received - self.io_arg2;
}
break;
case IOM_OP_MUL:
send = self.io_received * self.io_arg2;
break;
case IOM_OP_DIV:
if (reverse) {
send = self.io_arg2 / self.io_received;
} else {
send = self.io_received / self.io_arg2;
}
break;
case IOM_OP_MOD:
if (reverse) {
send = mod(self.io_arg2, self.io_received);
} else {
send = mod(self.io_received, self.io_arg2);
}
break;
case IOM_OP_FLOOR:
send = floor(self.io_received);
break;
case IOM_OP_CEIL:
send = ceil(self.io_received);
break;
case IOM_OP_MIN:
send = min(self.io_received, self.io_arg2);
break;
case IOM_OP_MAX:
send = max(self.io_received, self.io_arg2);
break;
case IOM_OP_ISNAN:
send = isnan(self.io_received);
break;
case IOM_OP_ISREAL:
send = isfinite(self.io_received);
break;
default: // IOM_OP_ADD
send = self.io_received + self.io_arg2;
}
if (AllowedValue(send)) {
self.io_send = send;
SUB_UseTargets();
} else {
dprint("math_op: dropping non-real result\n");
}
};
void() math_op = {
self.use = ApplyOp;
};
void() AddToAdder = {
float old_count = self.io_send;
float hit_bound = 0;
float change_clearance = 1;
float bound_clearance = 1;
if (!AllowedValue(self.io_received)) {
dprint("math_adder: ignoring non-real input\n");
return;
}
self.io_send+= self.io_received;
if (self.spawnflags & IOM_SPAWNFLAG_BOUND_U) {
if (self.io_send >= self.waitmax) {
hit_bound = 1;
self.io_send = self.waitmax;
}
}
if (self.spawnflags & IOM_SPAWNFLAG_BOUND_L) {
if (self.io_send <= self.waitmin) {
hit_bound = 1;
self.io_send = self.waitmin;
}
}
if (self.spawnflags & IOM_SPAWNFLAG_FIRE_BOUNDS_ONLY) {
if (!hit_bound) {
bound_clearance = 0;
}
}
if (self.spawnflags & IOM_SPAWNFLAG_FIRE_CHANGE_ONLY) {
if (self.io_send == old_count) {
change_clearance = 0;
}
}
if (bound_clearance && change_clearance) {
SUB_UseTargets();
}
};
void() math_adder = {
self.use = AddToAdder;
};
void() math_var = {
self.use = SUB_UseTrigger;
};
void() CompareAndFire = {
float result = 0;
switch(self.style) {
case IOM_CMP_LT:
result = self.io_received < self.io_arg2;
break;
case IOM_CMP_LTE:
result = self.io_received <= self.io_arg2;
break;
case IOM_CMP_GT:
result = self.io_received > self.io_arg2;
break;
case IOM_CMP_GTE:
result = self.io_received >= self.io_arg2;
break;
case IOM_CMP_NE:
result = self.io_received != self.io_arg2;
break;
default: // IOM_CMP_EQ
result = self.io_received == self.io_arg2;
}
if (result) {
SUB_UseTargets();
}
};
void() math_compare = {
self.use = CompareAndFire;
};
static const float GLOBAL_TYPE_QUERY = 0;
static const float GLOBAL_TYPE_STORE = 1;
static const float GLOBAL_TYPE_ADDER = 2;
void() GlobalQueryUse = {
self.io_send = LoadMapGlobal();
SUB_UseTargets();
};
void() GlobalStoreUse = {
StoreMapGlobal(self.io_received);
};
void() GlobalAddUse = {
float count = LoadMapGlobal();
count+= self.io_received;
StoreMapGlobal(count);
};
void() math_global = {
switch (self.style) {
case GLOBAL_TYPE_STORE:
self.use = GlobalStoreUse;
break;
case GLOBAL_TYPE_ADDER:
self.use = GlobalAddUse;
break;
default:
self.use = GlobalQueryUse;
}
};