aboutsummaryrefslogtreecommitdiff
path: root/engines/mutationofjb/commands/changecommand.cpp
blob: c97fe726d76491fee4e31f9c8f8a16b3194e01bc (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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* 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 "mutationofjb/commands/changecommand.h"
#include "mutationofjb/script.h"
#include "mutationofjb/gamedata.h"

/** @file
 * "CHANGE" <entity> " " <register> " " <sceneId> " " <entityId> " " <value>
 *
 * Changes entity register value for specified scene.
 * <entity>   1B  Entity to change register for.
 *                Possible values:
 *                  'D' - door
 *                  'O' - object
 *                  'S' - static
 *                  ''  - scene
 * <register> 2B  Register name.
 * <sceneId>  2B  Scene ID.
 * <entityid> 2B  Entity ID.
 * <value>    *B  Value (variable length).
 */

namespace MutationOfJB {

bool ChangeCommandParser::parseValueString(const Common::String &valueString, bool changeEntity, uint8 &sceneId, uint8 &entityId, ChangeCommand::ChangeRegister &reg, ChangeCommand::ChangeOperation &op, ChangeCommandValue &ccv) {
	if (changeEntity) {
		if (valueString.size() < 8) {
			return false;
		}
	} else {
		if (valueString.size() < 7) {
			return false;
		}
	}

	sceneId = atoi(valueString.c_str() + 3);
	if (changeEntity) {
		entityId = atoi(valueString.c_str() + 6);
	}
	const char *val = "";
	if (changeEntity) {
		if (valueString.size() >= 9) {
			val = valueString.c_str() + 9;
		}
	} else {
		if (valueString.size() >= 6) {
			val = valueString.c_str() + 6;
		}
	}

	if (valueString.hasPrefix("NM")) {
		reg = ChangeCommand::NM;
		op = ChangeCommand::SetValue;
		strncpy(ccv._strVal, val, MAX_ENTITY_NAME_LENGTH);
	} else if (valueString.hasPrefix("LT")) {
		reg = ChangeCommand::LT;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("SX")) {
		reg = ChangeCommand::SX;
		ccv._wordVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("SY")) {
		reg = ChangeCommand::SY;
		ccv._wordVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("XX")) {
		reg = ChangeCommand::XX;
		ccv._wordVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("YY")) {
		reg = ChangeCommand::YY;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("XL")) {
		reg = ChangeCommand::XL;
		ccv._wordVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("YL")) {
		reg = ChangeCommand::YL;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("WX")) {
		reg = ChangeCommand::WX;
		ccv._wordVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("WY")) {
		reg = ChangeCommand::WY;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("AC")) {
		reg = ChangeCommand::AC;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("FA")) {
		reg = ChangeCommand::FA;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("FR")) {
		reg = ChangeCommand::FR;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("NA")) {
		reg = ChangeCommand::NA;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("FS")) {
		reg = ChangeCommand::FS;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("CA")) {
		reg = ChangeCommand::CA;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("DS")) {
		reg = ChangeCommand::DS;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("DL")) {
		reg = ChangeCommand::DL;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("ND")) {
		reg = ChangeCommand::ND;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("NO")) {
		reg = ChangeCommand::NO;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("NS")) {
		reg = ChangeCommand::NS;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("PF")) {
		reg = ChangeCommand::PF;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("PL")) {
		reg = ChangeCommand::PL;
		ccv._byteVal = parseInteger(val, op);
	} else if (valueString.hasPrefix("PD")) {
		reg = ChangeCommand::PD;
		ccv._byteVal = parseInteger(val, op);
	}

	return true;
}


bool ChangeDoorCommandParser::parse(const Common::String &line, ScriptParseContext &, Command *&command) {
	if (!line.hasPrefix("CHANGED ")) {
		return false;
	}
	uint8 sceneId = 0;
	uint8 objectId = 0;
	ChangeCommand::ChangeRegister reg;
	ChangeCommand::ChangeOperation op;
	ChangeCommandValue val;
	if (!parseValueString(line.c_str() + 8, true, sceneId, objectId, reg, op, val)) {
		return false;
	}

	command = new ChangeDoorCommand(sceneId, objectId, reg, op, val);
	return true;
}

bool ChangeObjectCommandParser::parse(const Common::String &line, ScriptParseContext &, Command *&command) {
	if (!line.hasPrefix("CHANGEO ")) {
		return false;
	}
	uint8 sceneId = 0;
	uint8 objectId = 0;
	ChangeCommand::ChangeRegister reg;
	ChangeCommand::ChangeOperation op;
	ChangeCommandValue val;
	if (!parseValueString(line.c_str() + 8, true, sceneId, objectId, reg, op, val)) {
		return false;
	}

	command = new ChangeObjectCommand(sceneId, objectId, reg, op, val);
	return true;
}

bool ChangeStaticCommandParser::parse(const Common::String &line, ScriptParseContext &, Command *&command) {
	if (!line.hasPrefix("CHANGES ")) {
		return false;
	}
	uint8 sceneId = 0;
	uint8 objectId = 0;
	ChangeCommand::ChangeRegister reg;
	ChangeCommand::ChangeOperation op;
	ChangeCommandValue val;
	if (!parseValueString(line.c_str() + 8, true, sceneId, objectId, reg, op, val)) {
		return false;
	}

	command = new ChangeStaticCommand(sceneId, objectId, reg, op, val);
	return true;
}

bool ChangeSceneCommandParser::parse(const Common::String &line, ScriptParseContext &, Command *&command) {
	if (!line.hasPrefix("CHANGE ")) {
		return false;
	}
	uint8 sceneId = 0;
	uint8 objectId = 0;
	ChangeCommand::ChangeRegister reg;
	ChangeCommand::ChangeOperation op;
	ChangeCommandValue val;
	if (!parseValueString(line.c_str() + 7, false, sceneId, objectId, reg, op, val)) {
		return false;
	}

	command = new ChangeSceneCommand(sceneId, objectId, reg, op, val);
	return true;
}

int ChangeCommandParser::parseInteger(const char *val, ChangeCommand::ChangeOperation &op) {
	op = ChangeCommand::SetValue;

	if (!val || !(*val)) {
		return 0;
	}

	if (val[0] == '\\') {
		op = ChangeCommand::SetValue;
		val++;
	} else if (val[0] == '+') {
		op = ChangeCommand::AddValue;
		val++;
	} else if (val[0] == '-') {
		op = ChangeCommand::SubtractValue;
		val++;
	}

	return atoi(val);
}


const char *ChangeCommand::getRegisterAsString() const {
	switch (_register) {
	case NM:
		return "NM";
	case LT:
		return "LT";
	case SX:
		return "SX";
	case SY:
		return "SY";
	case XX:
		return "XX";
	case YY:
		return "YY";
	case XL:
		return "XL";
	case YL:
		return "YL";
	case WX:
		return "WX";
	case WY:
		return "WY";
	case SP:
		return "SP";
	case AC:
		return "AC";
	case FA:
		return "FA";
	case FR:
		return "FR";
	case NA:
		return "NA";
	case FS:
		return "FS";
	case CA:
		return "CA";
	case DS:
		return "DS";
	case DL:
		return "DL";
	case ND:
		return "ND";
	case NO:
		return "NO";
	case NS:
		return "NS";
	case PF:
		return "PF";
	case PL:
		return "PL";
	case PD:
		return "PD";
	default:
		return "(unknown)";
	}
}

Common::String ChangeCommand::getValueAsString() const {
	switch (_register) {
	case NM:
		return Common::String::format("\"%s\"", _value._strVal);
	case LT:
	case YY:
	case YL:
	case WY:
	case SP:
	case AC:
	case FA:
	case FR:
	case NA:
	case FS:
	case CA:
	case DS:
	case DL:
	case ND:
	case NO:
	case NS:
	case PF:
	case PL:
	case PD:
		return Common::String::format("%d", static_cast<int>(_value._byteVal));
	case SX:
	case SY:
	case XX:
	case XL:
	case WX:
		return Common::String::format("%d", static_cast<int>(_value._wordVal));
	default:
		return "(unknown)";
	}
}

const char *ChangeCommand::getOperationAsString() const {
	switch (_operation) {
	case SetValue:
		return "=";
	case AddValue:
		return "+=";
	case SubtractValue:
		return "-=";
	default:
		return "(unknown)";
	}
}

Command::ExecuteResult ChangeDoorCommand::execute(ScriptExecutionContext &scriptExecCtx) {
	Scene *const scene = scriptExecCtx.getGameData().getScene(_sceneId);
	if (!scene) {
		return Finished;
	}

	Door *const door = scene->getDoor(_entityId);
	if (!door) {
		return Finished;
	}

	switch (_register) {
	case NM:
		strncpy(door->_name, _value._strVal, MAX_ENTITY_NAME_LENGTH);
		break;
	case LT:
		door->_destSceneId = _value._byteVal;
		break;
	case SX:
		door->_destX = _value._wordVal;
		break;
	case SY:
		door->_destY = _value._wordVal;
		break;
	case XX:
		door->_x = _value._wordVal;
		break;
	case YY:
		door->_y = _value._byteVal;
		break;
	case XL:
		door->_width = _value._wordVal;
		break;
	case YL:
		door->_height = _value._byteVal;
		break;
	case WX:
		door->_walkToX = _value._wordVal;
		break;
	case WY:
		door->_walkToY = _value._byteVal;
		break;
	case SP:
		door->_SP = _value._byteVal;
		break;
	default:
		warning("Object does not support changing this register.");
		break;
	}

	return Finished;
}

Common::String ChangeDoorCommand::debugString() const {
	return Common::String::format("SCENE%d.DOOR%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}

Command::ExecuteResult ChangeObjectCommand::execute(ScriptExecutionContext &scriptExecCtx) {
	Scene *const scene = scriptExecCtx.getGameData().getScene(_sceneId);
	if (!scene) {
		return Finished;
	}

	Object *const object = scene->getObject(_entityId, true);
	if (!object) {
		return Finished;
	}

	switch (_register) {
	case AC:
		object->_active = _value._byteVal;
		break;
	case FA:
		object->_firstFrame = _value._byteVal;
		break;
	case FR:
		object->_randomFrame = _value._byteVal;
		break;
	case NA:
		object->_numFrames = _value._byteVal;
		break;
	case FS:
		object->_roomFrameLSB = _value._byteVal;
		break;
	case CA:
		object->_currentFrame = _value._byteVal;
		break;
	case XX:
		object->_x = _value._wordVal;
		break;
	case YY:
		object->_y = _value._byteVal;
		break;
	case XL:
		object->_width = _value._wordVal;
		break;
	case YL:
		object->_height = _value._byteVal;
		break;
	case WX:
		object->_WX = _value._wordVal;
		break;
	case WY:
		object->_roomFrameMSB = _value._byteVal;
		break;
	case SP:
		object->_SP = _value._byteVal;
		break;
	default:
		warning("Object does not support changing this register.");
		break;
	}

	return Finished;
}

Common::String ChangeObjectCommand::debugString() const {
	return Common::String::format("SCENE%d.OBJECT%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}

Command::ExecuteResult ChangeStaticCommand::execute(ScriptExecutionContext &scriptExecCtx) {
	Scene *const scene = scriptExecCtx.getGameData().getScene(_sceneId);
	if (!scene) {
		return Finished;
	}

	Static *const stat = scene->getStatic(_entityId);
	if (!stat) {
		return Finished;
	}

	switch (_register) {
	case AC:
		stat->_active = _value._byteVal;
		break;
	case NM:
		strncpy(stat->_name, _value._strVal, MAX_ENTITY_NAME_LENGTH);
		break;
	case XX:
		stat->_x = _value._wordVal;
		break;
	case YY:
		stat->_y = _value._byteVal;
		break;
	case XL:
		stat->_width = _value._wordVal;
		break;
	case YL:
		stat->_height = _value._byteVal;
		break;
	case WX:
		stat->_walkToX = _value._wordVal;
		break;
	case WY:
		stat->_walkToY = _value._byteVal;
		break;
	case SP:
		stat->_walkToFrame = _value._byteVal;
		break;
	default:
		warning("Object does not support changing this register.");
		break;
	}

	return Finished;
}

Common::String ChangeStaticCommand::debugString() const {
	return Common::String::format("SCENE%d.STATIC%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}

Command::ExecuteResult ChangeSceneCommand::execute(ScriptExecutionContext &scriptExecCtx) {
	Scene *const scene = scriptExecCtx.getGameData().getScene(_sceneId);
	if (!scene) {
		return Finished;
	}

	switch (_register) {
	case DS:
		scene->_startup = _value._byteVal;
		break;
	case DL:
		scene->_delay = _value._byteVal;
		break;
	case ND:
		scene->_noDoors = _value._byteVal;
		break;
	case NO:
		scene->_noObjects = _value._byteVal;
		break;
	case NS:
		scene->_noStatics = _value._byteVal;
		break;
	case PF:
		scene->_palRotFirst = _value._byteVal;
		break;
	case PL:
		scene->_palRotLast = _value._byteVal;
		break;
	case PD:
		scene->_palRotDelay = _value._byteVal;
		break;
	default:
		warning("Scene does not support changing this register.");
		break;
	}

	return Finished;
}

Common::String ChangeSceneCommand::debugString() const {
	return Common::String::format("SCENE%d.%s %s %s", _sceneId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}

}