aboutsummaryrefslogtreecommitdiff
path: root/engines/macventure/script.h
blob: 49b69874e606896127f4d7c7d53d59c01b1001bb (plain)
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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#ifndef MACVENTURE_SCRIPT_H
#define MACVENTURE_SCRIPT_H

#include "macventure/container.h"
#include "macventure/world.h"

namespace MacVenture {

class Container;
class World;

typedef uint32 ObjID;
typedef int16 word;

class ScriptAsset {
public:
	ScriptAsset(ObjID id, Container *container);
	~ScriptAsset() {}

	void reset();
	uint8 fetch();
	bool hasNext();
	void branch(word amount);

	ObjID getId();

private:

	void loadInstructions();

private:
	ObjID _id;
	Container *_container;

	Common::Array<uint8> _instructions;
	uint32 _ip; // Instruction pointer
};

class EngineState {
public:
	EngineState() {
		clear();
	}

	void push(word data) {
		sp--;
		stack[sp] = unneg16(data);
	}

	word pop() {
		word v = stack[sp];
		sp++;
		return v;
	}

	word peek(word off) {
		return stack[sp + off];
	}

	void poke(word off, word val) {
		stack[sp + off] = unneg16(val);
	}

	void clear() {
		sp = 0x80;
		for (int i = 0; i < sp; i++) {
			stack[i] = 0;
		}
	}

	word size() {
		return 0x80 - sp;
	}

private:
	word unneg16(word data) {
		if (data < 0)
			data = ((-data) ^ 0xFFFF) + 1;

		return data;
	}

private:

	word stack[0x80];
	word sp;
};

struct FunCall {
	word func;
	word rank;

	FunCall(word f, word r) {
		func = f;
		rank = r;
	}
};

struct EngineFrame {
	ControlAction action;
	ObjID src;
	ObjID dest;
	int x;
	int y;
	EngineState state;
	Common::List<ScriptAsset> scripts;
	Common::Array<FunCall> saves;
	uint32 familyIdx;

	bool haltedInFirst;
	bool haltedInFamily;
	bool haltedInSaves;
};

class ScriptEngine {
public:
	ScriptEngine(MacVentureEngine *engine, World *world);
	~ScriptEngine();

public:
	bool runControl(ControlAction action, ObjID source, ObjID destination, Common::Point delta);
	bool resume(bool execAll);
	void reset();

private:
	bool execFrame(bool execAll);
	bool loadScript(EngineFrame * frame, uint32 scriptID);
	bool resumeFunc(EngineFrame * frame);
	bool runFunc(EngineFrame * frame);

private:

	// Aux
	word neg16(word val);
	word neg8(word val);
	word sumChildrenAttr(word obj, word attr, bool recursive);

	// Opcodes
	void op80GATT(EngineState *state, EngineFrame *frame);	//get attribute
	void op81SATT(EngineState *state, EngineFrame *frame);	//set attribute
	void op82SUCH(EngineState *state, EngineFrame *frame);	//sum children attribute
	void op83PUCT(EngineState *state, EngineFrame *frame);	//push selected control
	void op84PUOB(EngineState *state, EngineFrame *frame);	//push selected object
	void op85PUTA(EngineState *state, EngineFrame *frame);	//push target
	void op86PUDX(EngineState *state, EngineFrame *frame);	//push deltax
	void op87PUDY(EngineState *state, EngineFrame *frame);	//push deltay
	void op88PUIB(EngineState *state, EngineFrame *frame, ScriptAsset *script);//push immediate.b
	void op89PUI(EngineState *state, EngineFrame *frame, ScriptAsset *script);//push immediate
	void op8aGGLO(EngineState *state, EngineFrame *frame);	//get global
	void op8bSGLO(EngineState *state, EngineFrame *frame);	//set global
	void op8cRAND(EngineState *state, EngineFrame *frame);	//random
	void op8dCOPY(EngineState *state, EngineFrame *frame);	//copy
	void op8eCOPYN(EngineState *state, EngineFrame *frame);	//copyn
	void op8fSWAP(EngineState *state, EngineFrame *frame);	//swap

	void op90SWAPN(EngineState *state, EngineFrame *frame);	//swapn
	void op91POP(EngineState *state, EngineFrame *frame);	//pop
	void op92COPYP(EngineState *state, EngineFrame *frame);	//copy+1
	void op93COPYPN(EngineState *state, EngineFrame *frame);//copy+n
	void op94SHUFF(EngineState *state, EngineFrame *frame);	//shuffle
	void op95SORT(EngineState *state, EngineFrame *frame);	//sort
	void op96CLEAR(EngineState *state, EngineFrame *frame);	//clear stack
	void op97SIZE(EngineState *state, EngineFrame *frame);	//get stack size
	void op98ADD(EngineState *state, EngineFrame *frame);	//add
	void op99SUB(EngineState *state, EngineFrame *frame);	//subtract
	void op9aMUL(EngineState *state, EngineFrame *frame);	//multiply
	void op9bDIV(EngineState *state, EngineFrame *frame);	//divide
	void op9cMOD(EngineState *state, EngineFrame *frame);	//mod
	void op9dDMOD(EngineState *state, EngineFrame *frame);	//divmod
	void op9eABS(EngineState *state, EngineFrame *frame);	//abs
	void op9fNEG(EngineState *state, EngineFrame *frame);	//neg

	void opa0AND(EngineState *state, EngineFrame *frame);	//and
	void opa1OR(EngineState *state, EngineFrame *frame);	//or
	void opa2XOR(EngineState *state, EngineFrame *frame);	//xor
	void opa3NOT(EngineState *state, EngineFrame *frame);	//not
	void opa4LAND(EngineState *state, EngineFrame *frame);	//logical and
	void opa5LOR(EngineState *state, EngineFrame *frame);	//logical or
	void opa6LXOR(EngineState *state, EngineFrame *frame);	//logical xor
	void opa7LNOT(EngineState *state, EngineFrame *frame);	//logical not
	void opa8GTU(EngineState *state, EngineFrame *frame);	//gt? unsigned
	void opa9LTU(EngineState *state, EngineFrame *frame);	//lt? unsigned
	void opaaGTS(EngineState *state, EngineFrame *frame);	//gt? signed
	void opabLTS(EngineState *state, EngineFrame *frame);	//lt? signed
	void opacEQ(EngineState *state, EngineFrame *frame);	//eq?
	void opadEQS(EngineState *state, EngineFrame *frame);	//eq string?
	void opaeCONT(EngineState *state, EngineFrame *frame);	//contains
	void opafCONTW(EngineState *state, EngineFrame *frame); //contains word

	void opb0BRA(EngineState *state, EngineFrame *frame, ScriptAsset *script);	//bra
	void opb1BRAB(EngineState *state, EngineFrame *frame, ScriptAsset *script);	//bra.b
	void opb2BEQ(EngineState *state, EngineFrame *frame, ScriptAsset *script);	//beq
	void opb3BEQB(EngineState *state, EngineFrame *frame, ScriptAsset *script); //beq.b
	void opb4BNE(EngineState *state, EngineFrame *frame, ScriptAsset *script);	//bne
	void opb5BNEB(EngineState *state, EngineFrame *frame, ScriptAsset *script);	//bne.b
	void opb6CLAT(EngineState *state, EngineFrame *frame);	//call later
	void opb7CCA(EngineState *state, EngineFrame *frame);	//cancel call
	void opb8CLOW(EngineState *state, EngineFrame *frame);	//cancel low priority
	void opb9CHI(EngineState *state, EngineFrame *frame);	//cancel high priority
	void opbaCRAN(EngineState *state, EngineFrame *frame);	//cancel priority range
	bool opbbFORK(EngineState *state, EngineFrame *frame);	//fork
	bool opbcCALL(EngineState *state, EngineFrame *frame, ScriptAsset &script);	//call
	void opbdFOOB(EngineState *state, EngineFrame *frame);	//focus object
	void opbeSWOB(EngineState *state, EngineFrame *frame);	//swap objects
	void opbfSNOB(EngineState *state, EngineFrame *frame);	//snap object

	void opc0TEXI(EngineState *state, EngineFrame *frame);	//toggle exits
	void opc1PTXT(EngineState *state, EngineFrame *frame);	//print text
	void opc2PNEW(EngineState *state, EngineFrame *frame);	//print newline
	void opc3PTNE(EngineState *state, EngineFrame *frame);	//print text+nl
	void opc4PNTN(EngineState *state, EngineFrame *frame);	//print nl+text+nl
	void opc5PNUM(EngineState *state, EngineFrame *frame);	//print number
	void opc6P2(EngineState *state, EngineFrame *frame);	//push 2
	void opc7PLBG(EngineState *state, EngineFrame *frame);	//play sound in background
	void opc8PLAW(EngineState *state, EngineFrame *frame);	//play sound and wait
	void opc9WAIT(EngineState *state, EngineFrame *frame);	//wait for sound to finish?
	void opcaTIME(EngineState *state, EngineFrame *frame);	//get current time
	void opcbDAY(EngineState *state, EngineFrame *frame);	//get current day
	void opccCHLD(EngineState *state, EngineFrame *frame);	//get children
	void opcdNCHLD(EngineState *state, EngineFrame *frame); //get num children
	void opceVERS(EngineState *state, EngineFrame *frame);	//get engine version
	void opcfPSCE(EngineState *state, EngineFrame *frame);	//push scenario number

	void opd0P1(EngineState *state, EngineFrame *frame);	//push 1
	void opd1GOBD(EngineState *state, EngineFrame *frame);	//get object dimensions
	void opd2GOVP(EngineState *state, EngineFrame *frame);	//get overlap percent
	void opd3CAPC(EngineState *state, EngineFrame *frame);	//capture children
	void opd4RELC(EngineState *state, EngineFrame *frame);	//release children
	void opd5DLOG(EngineState *state, EngineFrame *frame);	//show speech dialog
	void opd6ACMD(EngineState *state, EngineFrame *frame);	//activate command
	void opd7LOSE(EngineState *state, EngineFrame *frame);	//lose game
	void opd8WIN(EngineState *state, EngineFrame *frame);	//win game
	void opd9SLEEP(EngineState *state, EngineFrame *frame);	//sleep
	void opdaCLICK(EngineState *state, EngineFrame *frame);	//click to continue
	void opdbROBQ(EngineState *state, EngineFrame *frame);	//run queue
	void opdcRSQ(EngineState *state, EngineFrame *frame);	//run sound queue
	void opddRTQ(EngineState *state, EngineFrame *frame);	//run text queue
	void opdeUPSC(EngineState *state, EngineFrame *frame);	//update screen
	void opdfFMAI(EngineState *state, EngineFrame *frame);	//flash main window

	void ope0CHGR(EngineState *state, EngineFrame *frame);	//cache graphic and object
	void ope1CHSO(EngineState *state, EngineFrame *frame);	//cache sound
	void ope2MDIV(EngineState *state, EngineFrame *frame);	//muldiv
	void ope3UPOB(EngineState *state, EngineFrame *frame);	//update object
	void ope4PLEV(EngineState *state, EngineFrame *frame);	//currently playing event?
	void ope5WEV(EngineState *state, EngineFrame *frame);	//wait for event to finish
	void ope6GFIB(EngineState *state, EngineFrame *frame);	//get fibonacci (joke)
	void ope7CFIB(EngineState *state, EngineFrame *frame);	//calc fibonacci

	void op00NOOP(byte op);

private:
	MacVentureEngine *_engine;
	World *_world;
	Common::List<EngineFrame> _frames;
	Container *_scripts;
};

} // End of namespace MacVenture

#endif