File: game_oo.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (412 lines) | stat: -rw-r--r-- 10,763 bytes parent folder | download | duplicates (3)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "glk/comprehend/game_oo.h"
#include "glk/comprehend/comprehend.h"
#include "glk/comprehend/draw_surface.h"
#include "glk/comprehend/pics.h"
#include "common/md5.h"

namespace Glk {
namespace Comprehend {

enum OOToposRoomFlag {
	OO_ROOM_IN_SHIP = 1,
	OO_ROOM_FLAG_DARK = 2
};

enum OOToposFlag {
	OO_FLAG_9 = 9,
	OO_FLAG_13 = 13,
	OO_FLAG_22 = 22,
	OO_BRIGHT_ROOM = 25,
	OO_FLAG_WEARING_GOGGLES = 27,
	OO_FLAG_FLASHLIGHT_ON = 39,
	OO_FLAG_43 = 43,
	OO_FLAG_44 = 44,
	OO_FLAG_SUFFICIENT_FUEL = 51,
	OO_FLAG_REVERSE_VIDEO = 53,	// Effect of wearing goggles
	OO_FLAG_TOO_DARK = 55,
	OO_FLAG_TOO_BRIGHT = 56,
	OO_FLAG_58 = 58,
	OO_FLAG_59 = 59,
	OO_FLAG_READY_TO_DEPART = 60,
	OO_TRACTOR_BEAM = 71
};

enum OOToposItem {
	ITEM_SERUM_VIAL = 39
};

static const GameStrings OO_STRINGS = {
	EXTRA_STRING_TABLE(154)
};

OOToposGame::OOToposGame() : ComprehendGameV2(), _restartMode(RESTART_IMMEDIATE),
		_noFloodfill(UNSET), _stringVal1(0), _stringVal2(0),
		_printComputerMsg(true), _shipNotWorking(false) {
	_gameDataFile = "g0";

	// Extra strings are (annoyingly) stored in the game binary
	Common::File f;
	if (!f.open("novel.exe"))
		error("novel.exe is a required file");

	Common::String md5 = Common::computeStreamMD5AsString(f, 1024);
	f.close();

	if (md5 == "3fc2072f6996b17d2f21f0a92e53cdcc") {
		// DOS version from if-archive
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x16564, 0x17640));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x17702, 0x18600));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x186b2, 0x19b80));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x19c62, 0x1a590));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x1a634, 0x1b080));
	} else if (md5 == "e26858f2aaa9dcc28f468b07902813c5") {
		// DOS version from graphicsmagician.com
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x164c4, 0x175a0));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x17662, 0x18560));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x18612, 0x19ae0));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x19bc2, 0x1a4f0));
		_stringFiles.push_back(StringFile("NOVEL.EXE", 0x1a594, 0x1afe0));
	} else {
		error("Unrecognised novel.exe encountered");
	}

	_locationGraphicFiles.push_back("RA");
	_locationGraphicFiles.push_back("RB");
	_locationGraphicFiles.push_back("RC");
	_locationGraphicFiles.push_back("RD");
	_locationGraphicFiles.push_back("RE");
	_itemGraphicFiles.push_back("OA");
	_itemGraphicFiles.push_back("OB");
	_itemGraphicFiles.push_back("OC");
	_itemGraphicFiles.push_back("OD");

	_colorTable = 1;
	_gameStrings = &OO_STRINGS;
	_titleGraphicFile = "t0";
}

void OOToposGame::beforeGame() {
	// Draw the title
	g_comprehend->drawPicture(TITLE_IMAGE);

	// Print game information
	console_println("Story by Michael and Muffy Berlyn, graphics by Raim und Redlich and Brian Poff");
	console_println("IBM version by Jeffrey A. Jay. Copyright 1987  POLARWARE, Inc.");
	g_comprehend->readChar();

	g_comprehend->glk_window_clear(g_comprehend->_bottomWindow);
}

int OOToposGame::roomIsSpecial(uint room_index, uint *roomDescString) {
	Room *room = &_rooms[room_index];

	// Is the room dark
	if ((room->_flags & OO_ROOM_FLAG_DARK) &&
	        !(_flags[OO_FLAG_FLASHLIGHT_ON])) {
		if (roomDescString)
			*roomDescString = 0xb3;
		return ROOM_IS_DARK;
	}

	// Is the room too bright
	if (room_index == OO_BRIGHT_ROOM &&
	        !_flags[OO_FLAG_WEARING_GOGGLES]) {
		if (roomDescString)
			*roomDescString = 0x1c;
		return ROOM_IS_TOO_BRIGHT;
	}

	return ROOM_IS_NORMAL;
}

void OOToposGame::beforeTurn() {
	ComprehendGameV2::beforeTurn();

	if (_flags[OO_FLAG_TOO_DARK]) {
		// Show placeholder room if room is too dark
		_currentRoom = 55;
		_updateFlags |= UPDATE_GRAPHICS;
	} else if (_flags[OO_FLAG_TOO_BRIGHT]) {
		// Show placeholder room if room is too bright
		_currentRoom = 54;
		_updateFlags |= UPDATE_GRAPHICS;
	} else {
		YesNo nff = _flags[OO_FLAG_REVERSE_VIDEO] ? YES : NO;

		if (_noFloodfill != nff) {
			_noFloodfill = nff;
			_updateFlags |= UPDATE_GRAPHICS | UPDATE_ROOM_DESC;

			if (_noFloodfill == YES)
				g_comprehend->_drawFlags |= IMAGEF_REVERSE;
			else
				g_comprehend->_drawFlags &= ~IMAGEF_REVERSE;
		}
	}
}

void OOToposGame::beforePrompt() {
	// Handle the computer console if in front of it
	computerConsole();
}

void OOToposGame::afterPrompt() {
	ComprehendGameV2::afterPrompt();

	// WORKAROUND: Allow for the Apple 2 password in the DOS version
	if (!scumm_stricmp(_inputLine, "vug957a"))
		Common::strcpy_s(_inputLine, "tse957x");

	if (_currentRoom != _currentRoomCopy)
		_updateFlags |= UPDATE_GRAPHICS;
	_currentRoom = _currentRoomCopy;
}

void OOToposGame::handleSpecialOpcode() {
	switch (_specialOpcode) {
	case 1:
		// Update guard location
		randomizeGuardLocation();
		break;

	case 2:
		_restartMode = RESTART_IMMEDIATE;
		game_restart();
		break;

	case 3:
		_restartMode = RESTART_WITH_MSG;
		game_restart();
		break;

	case 4:
		_restartMode = RESTART_WITHOUT_MSG;
		game_restart();
		break;

	case 5:
		// Won the game
		g_comprehend->quitGame();
		break;

	case 6:
		// Save game
		game_save();
		break;

	case 7:
		// Restore game
		game_restore();
		break;

	case 8:
		// Computer response
		computerResponse();
		randomizeGuardLocation();
		break;

	case 9:
		// Checks the ship fuel
		checkShipFuel();
		randomizeGuardLocation();
		break;

	case 10:
		// Checks whether the ship is working
		checkShipWorking();
		break;

	default:
		break;
	}
}

bool OOToposGame::handle_restart() {
	_ended = false;

	if (_restartMode != RESTART_IMMEDIATE) {
		if (_restartMode == RESTART_WITH_MSG)
			console_println(stringLookup(_gameStrings->game_restart).c_str());

		if (tolower(console_get_key()) != 'r') {
			g_comprehend->quitGame();
			return false;
		}
	}

	loadGame();
	_updateFlags = UPDATE_ALL;
	return true;
}

void OOToposGame::synchronizeSave(Common::Serializer &s) {
	if (s.isSaving())
		_currentRoom = _currentRoomCopy;

	ComprehendGameV2::synchronizeSave(s);

	if (s.isLoading()) {
		_noFloodfill = UNSET;
		_currentRoomCopy = _currentRoom;

		beforeTurn();
	}
}

void OOToposGame::randomizeGuardLocation() {
	Item *item = get_item(22);
	if (_flags[OO_FLAG_13] && item->_room != _currentRoom) {
		if (getRandomNumber(255) > 128 && (_currentRoom == 3 || _currentRoom == 6))
			item->_room = _currentRoom;
	}
}

void OOToposGame::computerConsole() {
	if (_currentRoom == 57) {
		if (!_flags[OO_FLAG_9]) {
			// Mission Code:
			console_println(_strings2[129].c_str());
		} else if (!_flags[OO_FLAG_58]) {
			// Welcome back! I was wondering if you would be returning
			console_println(_strings2[131].c_str());
			_flags[OO_FLAG_58] = true;
			_printComputerMsg = true;
			checkShipWorking();
		} else if (_flags[OO_FLAG_59]) {
			checkShipDepart();
		} else if (_flags[OO_FLAG_43]) {
			// We can reach Mealy Sukas with the fuel we have left
			console_println(_strings2[142].c_str());
			_flags[OO_FLAG_59] = true;

			if (_flags[OO_FLAG_44])
				// The currency on Mealy Sukas is the 'frod'
				console_println(_strings2[144].c_str());
			else
				// Without evaluation data as to the current fuel prices
				console_println(_strings2[143].c_str());
		}
	}
}

void OOToposGame::computerResponse() {
	console_println(_strings2[145].c_str());
	if (_flags[OO_FLAG_43])
		console_println(_strings2[144].c_str());
	else
		console_println(_strings2[152].c_str());
}

void OOToposGame::checkShipWorking() {
	_stringVal1 = 164;
	_stringVal2 = 0;

	// Iterate through the ship's flags
	for (int idx = 42; idx < 51; ++idx, ++_stringVal1) {
		if (!_flags[idx]) {
			if (!_stringVal2) {
				// The following components are not installed
				printComputerMsg(_strings2[132].c_str());
				_stringVal2 = 1;
			}

			// Power Cylinder
			printComputerMsg(_strings[_stringVal1].c_str());
		}
	}

	_shipNotWorking = _stringVal2 != 0;
	if (!_shipNotWorking)
		// The ship is in working order
		printComputerMsg(_strings2[153].c_str());
}

void OOToposGame::checkShipFuel() {
	const byte ITEMS[7] = { 24, 27, 28, 29, 30, 31, 32 };
	_variables[0x4b] = 0;
	_stringVal1 = 68;
	_stringVal2 = 0;

	for (int idx = 168; idx < 175; ++idx, ++_stringVal1, ++_stringVal2) {
		if (_flags[idx]) {
			Item *item = get_item(ITEMS[_stringVal2] - 1);
			if (item->_room == ROOM_INVENTORY || (get_room(item->_room)->_flags & OO_ROOM_IN_SHIP) != 0) {
				Instruction varAdd(0x86, 0x4B, _stringVal1);
				execute_opcode(&varAdd, nullptr, nullptr);
			}
		}
	}

	// Computer: "Our current evaluation...
	Instruction strReplace(0xC9, 0x4B);
	execute_opcode(&strReplace, nullptr, nullptr);
	printComputerMsg(_strings2[146].c_str());

	FunctionState funcState;
	Instruction test(2, 75, 76);
	execute_opcode(&test, nullptr, nullptr);

	if (funcState._testResult) {
		// Computer: "We should now have enough
		_flags[OO_FLAG_SUFFICIENT_FUEL] = true;
		printComputerMsg(_strings2[151].c_str());
	} else {
		_flags[OO_FLAG_SUFFICIENT_FUEL] = false;
	}
}

void OOToposGame::checkShipDepart() {
	_printComputerMsg = false;
	checkShipWorking();
	checkShipFuel();
	_printComputerMsg = true;

	if (!_shipNotWorking && _flags[OO_FLAG_SUFFICIENT_FUEL]) {
		Item *item = get_item(ITEM_SERUM_VIAL - 1);
		if (item->_room == ROOM_INVENTORY || (get_room(item->_room)->_flags & OO_ROOM_IN_SHIP) != 0) {
			if (!_flags[OO_TRACTOR_BEAM]) {
				// I detect a tractor beam
				console_println(_strings2[77].c_str());
			} else if (!_flags[OO_FLAG_READY_TO_DEPART]) {
				// All systems check. Ready to depart
				_flags[OO_FLAG_22] = true;
				console_println(_strings2[79].c_str());
			} else {
				// Please close the airlock
				console_println(_strings2[76].c_str());
			}
		} else {
			// The serum vial is not aboard the ship
			console_println(_strings2[78].c_str());
		}
	}
}

void OOToposGame::printComputerMsg(const char *str) {
	if (_printComputerMsg)
		console_println(str);
}

} // namespace Comprehend
} // namespace Glk