-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg_vc.cpp
400 lines (336 loc) · 8.91 KB
/
g_vc.cpp
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
/**********************************************************
g_vc.cpp
winverge
Copyright (C) 2000 Benjamin Eirich (vecna)
All Rights Reserved
**********************************************************/
#include "pcp.h"
#include "vc.h"
// ============================ data ============================
byte *mapvc; // MAP VC code buffer
byte *effectvc,*startupvc; // Effect VC code buffer / Startup.vcs
byte *vcdatabuf; // The data buffer for VC use
byte *code, *basevc; // current code data ptr
quad scriptofstbl[1024]; // Map script offset table
quad effectofstbl[1024]; // Effect script offset table
quad startupofstbl[1024]; // Startup script offset table
int numscripts; // Number of scripts in current map VC
quad varl[10]; // Chain/call pass variables
quad tvar[26]; // Temporary/Throwaway variables
char killvc=0; // abort VC loop;
// ============================ code ============================
void InitVCMem()
{
effectvc = new byte[25000];
startupvc = new byte[25000];
vcdatabuf = new byte[2000000];
// vcdatabuf = valloc(vcbufm,"vcdatabuf");
}
void LoadVC(FILE *f)
{
int b,e,mapvcs;
fread(&numscripts, 1, 4, f);
fread(&scriptofstbl, 4, numscripts, f);
b=ftell(f); // get beginning offset of map vc code
fseek(f, 0, SEEK_END); // seek to end of map vc code
e=ftell(f); // get end offset of map vc code
fseek(f, b, SEEK_SET); // reset file pos
mapvcs = e - b + 1; // calc total map vc code size (in bytes)
if (mapvc) delete[] mapvc; // free mapvc mem (if necessary)
mapvc = new byte[mapvcs]; // allocate necessary mapvc mem
fread(mapvc, mapvcs, 1, f); // read in the map vc code from disk
}
byte GrabC()
{
byte c = *code;
code++;
return c;
}
word GrabW()
{
word c, *ptr;
ptr = (word*) code;
c = *ptr;
code += 2;
return c;
}
quad GrabD()
{
quad c, *ptr;
ptr = (quad *) code;
c = *ptr;
code += 4;
return c;
}
void GrabString(char *str)
{
int i;
i = 0;
while (*code)
{
str[i] = *code;
code++;
i++;
}
str[i]=0;
code++;
}
void ProcessVar0Assign()
{
int t,w;
byte b;
t = GrabC();
b = GrabC();
switch (b)
{
case SET: WriteVar0(t,ResolveOperand()); return;
case INCSET: w=ReadVar0(t); w += ResolveOperand(); WriteVar0(t,w); return;
case DECSET: w=ReadVar0(t); w -= ResolveOperand(); WriteVar0(t,w); return;
case INCREMENT: w=ReadVar0(t); w++; WriteVar0(t,w); return;
case DECREMENT: w=ReadVar0(t); w--; WriteVar0(t,w); return;
}
}
void ProcessVar1Assign()
{
int t,a,w;
byte b;
t = GrabC();
a = ResolveOperand();
b = GrabC();
switch (b)
{
case SET: WriteVar1(t,a,ResolveOperand()); return;
case INCSET: w=ReadVar1(t,a); w+=ResolveOperand(); WriteVar1(t,a,w); return;
case DECSET: w=ReadVar1(t,a); w-=ResolveOperand(); WriteVar1(t,a,w); return;
case INCREMENT: w=ReadVar1(t,a); w++; WriteVar1(t,a,w); return;
case DECREMENT: w=ReadVar1(t,a); w--; WriteVar1(t,a,w); return;
}
}
void ProcessVar2Assign()
{
int t,a,c,w;
byte b;
t = GrabC();
a = ResolveOperand();
c = ResolveOperand();
b = GrabC();
switch (b)
{
case SET: WriteVar2(t,a,c,ResolveOperand()); return;
case INCSET: w=ReadVar2(t,a,c); w+=ResolveOperand(); WriteVar2(t,a,c,w); return;
case DECSET: w=ReadVar2(t,a,c); w-=ResolveOperand(); WriteVar2(t,a,c,w); return;
case INCREMENT: w=ReadVar2(t,a,c); w++; WriteVar2(t,a,c,w); return;
case DECREMENT: w=ReadVar2(t,a,c); w--; WriteVar2(t,a,c,w); return;
}
}
quad ResolveOperand()
{
quad cr;
byte c;
cr = ProcessOperand(); // Get base number
while (1)
{
c = GrabC();
switch (c)
{
case ADD: cr += ProcessOperand(); continue;
case SUB: cr -= ProcessOperand(); continue;
case DIV: cr = cr / ProcessOperand(); continue;
case MULT: cr = cr * ProcessOperand(); continue;
case MOD: cr = cr % ProcessOperand(); continue;
case OP_END: break;
}
break;
}
return cr;
}
int ProcessOperand()
{
byte op_desc;
int a,b,c;
op_desc = GrabC();
switch (op_desc)
{
case OP_IMMEDIATE: return GrabD();
case OP_VAR0: return ReadVar0(GrabC());
case OP_VAR1: a=GrabC();
b=ResolveOperand();
return ReadVar1(a,b);
case OP_VAR2: a=GrabC();
b=ResolveOperand();
c=ResolveOperand();
return ReadVar2(a,b,c);
case OP_GROUP: return ResolveOperand();
}
return -1;
}
void ProcessIf()
{
quad elseofs, arg1, arg2;
byte numargs,i;
char exec=1, controlbyte;
numargs = GrabC(); // Get number of arguements in this IF.
elseofs = GrabD(); // Get jumplocation if IF is false.
for (i=0; i<numargs; i++)
{
arg1 = ResolveOperand();
controlbyte = GrabC();
if (controlbyte==ZERO || controlbyte==NONZERO)
{
switch (controlbyte)
{
case ZERO: if (arg1) exec=0; break;
case NONZERO: if (!arg1) exec=0; break;
}
} else
{
arg2 = ResolveOperand();
switch (controlbyte)
{
case EQUALTO: if (arg1 != arg2) exec=0; break;
case NOTEQUAL: if (arg1 == arg2) exec=0; break;
case GREATERTHAN: if (arg1 <= arg2) exec=0; break;
case GREATERTHANOREQUAL: if (arg1 < arg2) exec=0; break;
case LESSTHAN: if (arg1 >= arg2) exec=0; break;
case LESSTHANOREQUAL: if (arg1 > arg2) exec=0; break;
}
}
if (!exec)
{
code = basevc + elseofs;
return;
}
}
}
void ProcessFor0()
{
quad vidx=0, min, max, incv, curval, t, bptr;
byte incs;
vidx = GrabC();
min = ResolveOperand();
max = ResolveOperand();
incs = GrabC();
incv = ResolveOperand();
bptr = (quad) code;
WriteVar0(vidx, min);
curval = min;
execloop:
code = (byte *) bptr;
ExecuteBlock();
t = ReadVar0(vidx);
if (incs){ t+=incv; curval+=incv; }
else { t-=incv; curval-=incv; }
WriteVar0(vidx, t);
// Now determine if we've passed the min/max
if (!incs && curval > min) return;
if (incs && curval <= max) goto execloop;
if (!incs && curval >= max) goto execloop;
}
void ProcessFor1()
{
quad vidx=0, min, max, incv, curval, t, bptr, varg;
byte incs;
vidx = GrabC();
varg = ResolveOperand();
min = ResolveOperand();
max = ResolveOperand();
incs = GrabC();
incv = ResolveOperand();
bptr = (quad) code;
WriteVar1(vidx, varg, min);
curval = min;
execloop:
code = (byte *) bptr;
ExecuteBlock();
t = ReadVar1(vidx, varg);
if (incs) { t+=incv; curval+=incv; }
else { t-=incv; curval-=incv; }
WriteVar1(vidx, varg, t);
// Now determine if we've passed the min/max
if (!incs && curval > min) return;
if (incs && curval <= max) goto execloop;
if (!incs && curval >= max) goto execloop;
}
void ProcessSwitch()
{
int realvalue,compvalue;
byte c, *next;
realvalue = ResolveOperand();
c = GrabC();
while (c != ENDSCRIPT)
{
compvalue = ResolveOperand();
next = (byte *) ((quad) GrabD());
if (compvalue != realvalue)
{
code = (byte *) ((int) basevc + (int) next);
c = GrabC();
continue;
}
ExecuteBlock();
c = GrabC();
}
}
void ExecuteScript(word s)
{
byte *buf,*basebuf; /* -- ric: 09/Jun/98 -- */
basebuf = basevc; /* save the old code buffers */
buf = code;
basevc = mapvc;
code = mapvc + scriptofstbl[s];
ExecuteBlock();
basevc = basebuf; /* -- ric: 09/Jun/98 -- */
code = buf; /* restore the old code buffers */
}
void ExecuteHookedScript(word s)
{
byte *codeb,abortvcb,*basevcb;
codeb = code;
abortvcb = killvc;
basevcb = basevc;
ExecuteScript(s);
killvc = abortvcb;
code = codeb;
basevc = basevcb;
}
void ExecuteEffect(word s)
{
byte *buf,*basebuf; /* -- ric: 09/Jun/98 -- */
basebuf = basevc; /* save the old code buffers */
buf = code;
basevc = effectvc;
code = effectvc + effectofstbl[s];
ExecuteBlock();
basevc = basebuf; /* -- ric: 09/Jun/98 -- */
code = buf; /* restore the old code buffers */
}
void ExecuteBlock()
{
byte c;
while (1)
{
if (killvc) { killvc=0; break; }
c = GrabC();
if (c == EXEC) { ExecLibFunc(GrabC()); continue; }
if (c == VAR0_ASSIGN) { ProcessVar0Assign(); continue; }
if (c == VAR1_ASSIGN) { ProcessVar1Assign(); continue; }
if (c == VAR2_ASSIGN) { ProcessVar2Assign(); continue; }
if (c == GENERAL_IF) { ProcessIf(); continue; }
if (c == FOR_LOOP0) { ProcessFor0(); continue; }
if (c == FOR_LOOP1) { ProcessFor1(); continue; }
if (c == GOTO) { code=basevc+GrabD(); continue; }
if (c == SWITCH) { ProcessSwitch(); continue; }
if (c == ENDSCRIPT) break;
}
}
void StartupScript()
{
byte *buf,*basebuf; /* -- ric: 09/Jun/98 -- */
basebuf = basevc; /* save the old code buffers */
buf = code;
basevc = startupvc;
code = startupvc;
ExecuteBlock();
basevc = basebuf; /* -- ric: 09/Jun/98 -- */
code = buf; /* restore the old code buffers */
}