aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/quest/geas_state.cpp
blob: 9319772670a989ce1e50ec21df6deb15b0dc626e (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
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
/* 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.
 *
 */

#include "glk/quest/geas_state.h"
#include "glk/quest/geas_runner.h"
#include "glk/quest/geas_util.h"
#include "glk/quest/read_file.h"
#include "glk/quest/streams.h"

namespace Glk {
namespace Quest {

void Serializer::sync(bool &b) {
	byte v = b ? 1 : 0;
	syncAsByte(v);
	if (isLoading())
		b = v != 0;
}

void Serializer::sync(String &s) {
	Common::String str = s;
	Common::Serializer::syncString(str);
	if (isLoading())
		s = String(str.c_str());
}
	
void Serializer::sync(PropertyRecord &pr) {
	sync(pr.name);
	sync(pr.data);
}

void Serializer::sync(ObjectRecord &pr) {
	sync(pr.name);
	sync(pr.hidden);
	sync(pr.invisible);
	sync(pr.parent);
}

void Serializer::sync(ExitRecord &er) {
	sync(er.src);
	sync(er.dest);
}

void Serializer::sync(TimerRecord &tr) {
	sync(tr.name);
	sync(tr.is_running);
	syncAsUint32LE(tr.interval);
	syncAsUint32LE(tr.timeleft);
}

void Serializer::sync(SVarRecord &svr) {
	svr.sync(*this);
}

void Serializer::sync(IVarRecord &ivr) {
	ivr.sync(*this);
}

void Serializer::sync(GeasState &gs) {
	sync(gs.location);
	sync(gs.props);
	sync(gs.objs);
	sync(gs.exits);
	sync(gs.timers);
	sync(gs.svars);
	sync(gs.ivars);
}

/*----------------------------------------------------------------------*/

void SVarRecord::sync(Serializer &s) {
	s.sync(name);
	
	uint count = data.size();
	s.syncAsUint32LE(count);
	if (s.isLoading())
		data.resize(count);

	for (uint i = 0; i < size(); ++i)
		s.sync(data[i]);
}

/*----------------------------------------------------------------------*/

void IVarRecord::sync(Serializer &s) {
	s.sync(name);

	uint count = data.size();
	s.syncAsUint32LE(count);
	if (s.isLoading())
		data.resize(count);

	for (uint i = 0; i < size(); ++i)
		s.syncAsSint32LE(data[i]);
}

/*----------------------------------------------------------------------*/

GeasState::GeasState(GeasInterface &gi, const GeasFile &gf) {
	running = false;

	cerr << "GeasState::GeasState()" << endl;
	for (uint i = 0; i < gf.size("game"); i ++) {
		//const GeasBlock &go = gf.game[i];
		//register_block ("game", "game");
		ObjectRecord data;
		data.name = "game";
		data.parent = "";
		data.hidden = false;
		data.invisible = true;
		objs.push_back(data);
	}

	cerr << "GeasState::GeasState() done setting game" << endl;
	for (uint i = 0; i < gf.size("room"); i ++) {
		const GeasBlock &go = gf.block("room", i);
		ObjectRecord data;
		//data.name = go.lname;
		data.name = go.name;
		data.parent = "";
		data.hidden = data.invisible = true;
		//register_block (data.name, "room");
		objs.push_back(data);
	}

	cerr << "GeasState::GeasState() done setting rooms" << endl;
	for (uint i = 0; i < gf.size("object"); i++) {
		const GeasBlock &go = gf.block("object", i);
		ObjectRecord data;
		//data.name = go.lname;
		data.name = go.name;
		if (go.parent == "")
			data.parent = "";
		else
			//data.parent = lcase (param_contents (go.parent));
			data.parent = param_contents(go.parent);
		//register_block (data.name, "object");
		data.hidden = data.invisible = false;
		objs.push_back(data);
	}

	cerr << "GeasState::GeasState() done setting objects" << endl;
	for (uint i = 0; i < gf.size("timer"); i ++) {
		const GeasBlock &go = gf.block("timer", i);
		//cerr << "GS::GS: Handling timer " << go << "\n";
		TimerRecord tr;
		String interval = "", status = "";
		for (uint j = 0; j < go.data.size(); j ++) {
			String line = go.data[j];
			uint c1, c2;
			String tok = first_token(line, c1, c2);
			if (tok == "interval") {
				tok = next_token(line, c1, c2);
				if (!is_param(tok))
					gi.debug_print(nonparam("interval", line));
				else
					interval = param_contents(tok);
			} else if (tok == "enabled" || tok == "disabled") {
				if (status != "")
					gi.debug_print("Repeated status for timer");
				else
					status = tok;
			} else if (tok == "action") {
			} else {
				gi.debug_print("Bad timer line " + line);
			}
		}
		//tr.name = go.lname;
		tr.name = go.name;
		tr.is_running = (status == "enabled");
		tr.interval = tr.timeleft = parse_int(interval);
		//register_block (tr.name, "timer");
		timers.push_back(tr);
	}

	cerr << "GeasState::GeasState() done with timers" << endl;
	for (uint i = 0; i < gf.size("variable"); i ++) {
		const GeasBlock &go(gf.block("variable", i));
		cerr << "GS::GS: Handling variable #" << i << ": " << go << endl;
		String vartype;
		String value;
		for (uint j = 0; j < go.data.size(); j ++) {
			String line = go.data[j];
			cerr << "   Line #" << j << " of var: \"" << line << "\"" << endl;
			uint c1, c2;
			String tok = first_token(line, c1, c2);
			if (tok == "type") {
				tok = next_token(line, c1, c2);
				if (tok == "")
					gi.debug_print(String("Missing variable type in ")
					               + string_geas_block(go));
				else if (vartype != "")
					gi.debug_print(String("Redefining var. type in ")
					               + string_geas_block(go));
				else if (tok == "numeric" || tok == "String")
					vartype = tok;
				else
					gi.debug_print(String("Bad var. type ") + line);
			} else if (tok == "value") {
				tok = next_token(line, c1, c2);
				if (!is_param(tok))
					gi.debug_print(String("Expected parameter in " + line));
				else
					value = param_contents(tok);
			} else if (tok == "display" || tok == "onchange") {
			} else {
				gi.debug_print(String("Bad var. line: ") + line);
			}
		}
		if (vartype == "" || vartype == "numeric") {
			IVarRecord ivr;
			//ivr.name = go.lname;
			ivr.name = go.name;
			ivr.set(0, parse_int(value));
			ivars.push_back(ivr);
			//register_block (ivr.name, "numeric");
		} else {
			SVarRecord svr;
			//svr.name = go.lname;
			svr.name = go.name;
			svr.set(0, value);
			svars.push_back(svr);
			//register_block (svr.name, "String");
		}
	}
	//cerr << obj_types << endl;
	cerr << "GeasState::GeasState() done with variables" << endl;
}

void GeasState::load(Common::SeekableReadStream *rs) {
	Serializer s(rs, nullptr);
	s.sync(*this);
}

void GeasState::save(Common::WriteStream *ws) {
	Serializer s(nullptr, ws);
	s.sync(*this);
}

/*----------------------------------------------------------------------*/

Common::WriteStream &operator<<(Common::WriteStream &o, const PropertyRecord &pr) {
	o << pr.name << ", data == " << pr.data;
	return o;
}

Common::WriteStream &operator<<(Common::WriteStream &o, const ObjectRecord &objr) {
	o << objr.name << ", parent == " << objr.parent;
	if (objr.hidden)
		o << ", hidden";
	if (objr.invisible)
		o << ", invisible";
	return o;
}

Common::WriteStream &operator<<(Common::WriteStream &o, const ExitRecord er) {
	return o << er.src << ": " << er.dest;
}

Common::WriteStream &operator<<(Common::WriteStream &o, const TimerRecord &tr) {
	return o << tr.name << ": " << (tr.is_running ? "" : "not ") << "running ("
		<< tr.timeleft << " // " << tr.interval << ")";
}

Common::WriteStream &operator<<(Common::WriteStream &o, const SVarRecord &sr) {
	o << sr.name << ": ";
	if (sr.size() == 0)
		o << "(empty)";
	else if (sr.size() <= 1)
		o << "<" << sr.get(0) << ">";
	else
		for (uint i = 0; i < sr.size(); i++) {
			o << i << ": <" << sr.get(i) << ">";
			if (i + 1 < sr.size())
				o << ", ";
		}
	return o;
}

Common::WriteStream &operator<<(Common::WriteStream &o, const IVarRecord &ir) {
	o << ir.name << ": ";
	if (ir.size() == 0)
		o << "(empty)";
	else if (ir.size() <= 1)
		o << ir.get(0);
	else
		for (uint i = 0; i < ir.size(); i++) {
			o << i << ": " << ir.get(i);
			if (i + 1 < ir.size())
				o << ", ";
		}
	return o;
}

Common::WriteStream &operator<<(Common::WriteStream &o, const GeasState &gs) {
	o << "location == " << gs.location << "\nprops: \n";

	for (uint i = 0; i < gs.props.size(); i++)
		o << "    " << i << ": " << gs.props[i] << "\n";

	o << "objs:\n";
	for (uint i = 0; i < gs.objs.size(); i++)
		o << "    " << i << ": " << gs.objs[i] << "\n";

	o << "exits:\n";
	for (uint i = 0; i < gs.exits.size(); i++)
		o << "    " << i << ": " << gs.exits[i] << "\n";

	o << "timers:\n";
	for (uint i = 0; i < gs.timers.size(); i++)
		o << "    " << i << ": " << gs.timers[i] << "\n";

	o << "String variables:\n";
	for (uint i = 0; i < gs.svars.size(); i++)
		o << "    " << i << ": " << gs.svars[i] << "\n";

	o << "integer variables:\n";
	for (uint i = 0; i < gs.svars.size(); i++)
		o << "    " << i << ": " << gs.svars[i] << "\n";

	return o;
}

} // End of namespace Quest
} // End of namespace Glk