1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/system.h"
24 #include "graphics/cursorman.h"
25 #include "graphics/palette.h"
26 #include "gui/message.h"
27 
28 #include "supernova/screen.h"
29 #include "supernova/supernova.h"
30 #include "supernova/game-manager.h"
31 
32 namespace Supernova {
33 
serialize(Common::WriteStream * out)34 bool GameManager::serialize(Common::WriteStream *out) {
35 	return false;
36 }
37 
38 
deserialize(Common::ReadStream * in,int version)39 bool GameManager::deserialize(Common::ReadStream *in, int version) {
40 	return false;
41 }
42 
add(Object & obj)43 void Inventory::add(Object &obj) {
44 	if (_numObjects < kMaxCarry) {
45 		_inventory[_numObjects++] = &obj;
46 		obj.setProperty(CARRIED);
47 	}
48 
49 	if (getSize() > _inventoryScroll + 8) {
50 		_inventoryScroll = getSize() - 8;
51 		_inventoryScroll += _inventoryScroll % 2;
52 	}
53 }
54 
remove(Object & obj)55 void Inventory::remove(Object &obj) {
56 	for (int i = 0; i < _numObjects; ++i) {
57 		if (_inventory[i] == &obj) {
58 			if (_inventoryScroll >= 2 && getSize() % 2)
59 				_inventoryScroll -= 2;
60 
61 			--_numObjects;
62 			while (i < _numObjects) {
63 				_inventory[i] = _inventory[i + 1];
64 				++i;
65 			}
66 			obj.disableProperty(CARRIED);
67 		}
68 	}
69 }
70 
clear()71 void Inventory::clear() {
72 	for (int i = 0; i < _numObjects; ++i)
73 		_inventory[i]->disableProperty(CARRIED);
74 	_numObjects = 0;
75 	_inventoryScroll = 0;
76 }
77 
get(int index) const78 Object *Inventory::get(int index) const {
79 	if (index < _numObjects)
80 		return _inventory[index];
81 
82 	return _nullObject;
83 }
84 
get(ObjectId id) const85 Object *Inventory::get(ObjectId id) const {
86 	for (int i = 0; i < _numObjects; ++i) {
87 		if (_inventory[i]->_id == id)
88 			return _inventory[i];
89 	}
90 
91 	return _nullObject;
92 }
93 
94 
GuiElement()95 GuiElement::GuiElement()
96 	: _isHighlighted(false)
97 	, _bgColorNormal(kColorWhite25)
98 	, _bgColorHighlighted(kColorWhite44)
99 	, _bgColor(kColorWhite25)
100 	, _textColorNormal(kColorGreen)
101 	, _textColorHighlighted(kColorLightGreen)
102 	, _textColor(kColorGreen)
103 {
104 	memset(_text, 0, sizeof(_text));
105 }
106 
setText(const char * text)107 void GuiElement::setText(const char *text) {
108 	strncpy(_text, text, sizeof(_text) - 1);
109 }
110 
setTextPosition(int x,int y)111 void GuiElement::setTextPosition(int x, int y) {
112 	_textPosition = Common::Point(x, y);
113 }
114 
setSize(int x1,int y1,int x2,int y2)115 void GuiElement::setSize(int x1, int y1, int x2, int y2) {
116 	this->left = x1;
117 	this->top = y1;
118 	this->right = x2;
119 	this->bottom = y2;
120 
121 	_textPosition = Common::Point(x1 + 1, y1 + 1);
122 }
123 
setColor(int bgColor,int textColor,int bgColorHighlighted,int textColorHightlighted)124 void GuiElement::setColor(int bgColor, int textColor, int bgColorHighlighted, int textColorHightlighted) {
125 	_bgColor = bgColor;
126 	_textColor = textColor;
127 	_bgColorNormal = bgColor;
128 	_textColorNormal = textColor;
129 	_bgColorHighlighted = bgColorHighlighted;
130 	_textColorHighlighted = textColorHightlighted;
131 }
132 
setHighlight(bool isHighlighted_)133 void GuiElement::setHighlight(bool isHighlighted_) {
134 	if (isHighlighted_) {
135 		_bgColor = _bgColorHighlighted;
136 		_textColor = _textColorHighlighted;
137 	} else {
138 		_bgColor = _bgColorNormal;
139 		_textColor = _textColorNormal;
140 	}
141 }
142 
143 int GameManager::guiCommands[] = {
144 	kStringCommandGo, kStringCommandLook, kStringCommandTake, kStringCommandOpen, kStringCommandClose,
145 	kStringCommandPress, kStringCommandPull, kStringCommandUse, kStringCommandTalk, kStringCommandGive
146 };
147 
148 int GameManager::guiStatusCommands[] = {
149 	kStringStatusCommandGo, kStringStatusCommandLook, kStringStatusCommandTake, kStringStatusCommandOpen, kStringStatusCommandClose,
150 	kStringStatusCommandPress, kStringStatusCommandPull, kStringStatusCommandUse, kStringStatusCommandTalk, kStringStatusCommandGive
151 };
152 
GameManager(SupernovaEngine * vm,Sound * sound)153 GameManager::GameManager(SupernovaEngine *vm, Sound *sound)
154 	: _inventory(&_nullObject, _inventoryScroll)
155 	, _vm(vm)
156 	, _sound(sound)
157 	, _mouseClickType(Common::EVENT_INVALID) {
158 	initGui();
159 }
160 
~GameManager()161 GameManager::~GameManager() {
162 }
163 
destroyRooms()164 void GameManager::destroyRooms() {
165 }
166 
initState()167 void GameManager::initState() {
168 	_currentInputObject = &_nullObject;
169 	_inputObject[0] = &_nullObject;
170 	_inputObject[1] = &_nullObject;
171 	_inputVerb = ACTION_WALK;
172 	_processInput = false;
173 	_guiEnabled = true;
174 	_animationEnabled = true;
175 	_roomBrightness = 255;
176 	_mouseClicked = false;
177 	_keyPressed = false;
178 	_mouseX = -1;
179 	_mouseY = -1;
180 	_mouseField = -1;
181 	_inventoryScroll = 0;
182 	_oldTime = g_system->getMillis();
183 	_timerPaused = 0;
184 	_timePaused = false;
185 	_messageDuration = 0;
186 	_animationTimer = 0;
187 	_currentSentence = -1;
188 	for (int i = 0 ; i < 6 ; ++i) {
189 		_sentenceNumber[i] = -1;
190 		_texts[i] = kNoString;
191 		_rows[i] = 0;
192 		_rowsStart[i] = 0;
193 	}
194 
195 	_dead = false;
196 }
197 
initRooms()198 void GameManager::initRooms() {
199 }
200 
initGui()201 void GameManager::initGui() {
202 	int cmdCount = ARRAYSIZE(_guiCommandButton);
203 	int cmdAvailableSpace = 320 - (cmdCount - 1) * 2;
204 	for (int i = 0; i < cmdCount; ++i) {
205 		const Common::String &text = _vm->getGameString(guiCommands[i]);
206 		cmdAvailableSpace -= Screen::textWidth(text);
207 	}
208 
209 	int commandButtonX = 0;
210 	for (int i = 0; i < ARRAYSIZE(_guiCommandButton); ++i) {
211 		const Common::String &text = _vm->getGameString(guiCommands[i]);
212 		int width;
213 		if (i < cmdCount - 1) {
214 			int space = cmdAvailableSpace / (cmdCount - i);
215 			cmdAvailableSpace -= space;
216 			width = Screen::textWidth(text) + space;
217 		} else
218 			width = 320 - commandButtonX;
219 
220 		_guiCommandButton[i].setSize(commandButtonX, 150, commandButtonX + width, 159);
221 		_guiCommandButton[i].setText(text.c_str());
222 		_guiCommandButton[i].setColor(kColorWhite25, kColorDarkGreen, kColorWhite44, kColorGreen);
223 		commandButtonX += width + 2;
224 	}
225 
226 	for (int i = 0; i < ARRAYSIZE(_guiInventory); ++i) {
227 		int inventoryX = 136 * (i % 2);
228 		int inventoryY = 161 + 10 * (i / 2);
229 
230 		_guiInventory[i].setSize(inventoryX, inventoryY, inventoryX + 135, inventoryY + 9);
231 		_guiInventory[i].setColor(kColorWhite25, kColorDarkRed, kColorWhite35, kColorRed);
232 	}
233 	_guiInventoryArrow[0].setSize(272, 161, 279, 180);
234 	_guiInventoryArrow[0].setColor(kColorWhite25, kColorDarkRed, kColorWhite35, kColorRed);
235 	_guiInventoryArrow[0].setText("\x82");
236 	_guiInventoryArrow[0].setTextPosition(273, 166);
237 	_guiInventoryArrow[1].setSize(272, 181, 279, 200);
238 	_guiInventoryArrow[1].setColor(kColorWhite25, kColorDarkRed, kColorWhite35, kColorRed);
239 	_guiInventoryArrow[1].setText("\x83");
240 	_guiInventoryArrow[1].setTextPosition(273, 186);
241 }
242 
canSaveGameStateCurrently()243 bool GameManager::canSaveGameStateCurrently() {
244 	return false;
245 }
246 
updateEvents()247 void GameManager::updateEvents() {
248 }
249 
processInput(Common::KeyState & state)250 void GameManager::processInput(Common::KeyState &state) {
251 	_key = state;
252 
253 	switch (state.keycode) {
254 	case Common::KEYCODE_F1:
255 		// help
256 		if (!_guiEnabled)
257 			return;
258 		if (_vm->_MSPart == 1)
259 			_vm->showHelpScreen1();
260 		else if (_vm->_MSPart == 2)
261 			_vm->showHelpScreen2();
262 		break;
263 	case Common::KEYCODE_F2:
264 		// show game manual
265 		if (!_guiEnabled)
266 			return;
267 		_vm->showTextReader("doc");
268 		break;
269 	case Common::KEYCODE_F3:
270 		// show game info
271 		if (!_guiEnabled)
272 			return;
273 		_vm->showTextReader("inf");
274 		break;
275 	case Common::KEYCODE_F4:
276 		if (!_guiEnabled)
277 			return;
278 		_vm->setTextSpeed();
279 		break;
280 	case Common::KEYCODE_F5:
281 		_vm->openMainMenuDialog();
282 		break;
283 	case Common::KEYCODE_x:
284 		if (state.flags & Common::KBD_ALT) {
285 			if (_vm->quitGameDialog())
286 				_vm->quitGame();
287 		}
288 		break;
289 	default:
290 		break;
291 	}
292 	if (_vm->_improved && _guiEnabled) {
293 		switch (state.keycode) {
294 		case Common::KEYCODE_1:
295 			resetInputState();
296 			_inputVerb = ACTION_WALK;
297 			break;
298 		case Common::KEYCODE_2:
299 			resetInputState();
300 			_inputVerb = ACTION_LOOK;
301 			break;
302 		case Common::KEYCODE_3:
303 			resetInputState();
304 			_inputVerb = ACTION_TAKE;
305 			break;
306 		case Common::KEYCODE_4:
307 			resetInputState();
308 			_inputVerb = ACTION_OPEN;
309 			break;
310 		case Common::KEYCODE_5:
311 			resetInputState();
312 			_inputVerb = ACTION_CLOSE;
313 			break;
314 		case Common::KEYCODE_6:
315 			resetInputState();
316 			_inputVerb = ACTION_PRESS;
317 			break;
318 		case Common::KEYCODE_7:
319 			resetInputState();
320 			_inputVerb = ACTION_PULL;
321 			break;
322 		case Common::KEYCODE_8:
323 			resetInputState();
324 			_inputVerb = ACTION_USE;
325 			break;
326 		case Common::KEYCODE_9:
327 			resetInputState();
328 			_inputVerb = ACTION_TALK;
329 			break;
330 		case Common::KEYCODE_0:
331 			resetInputState();
332 			_inputVerb = ACTION_GIVE;
333 			break;
334 		default:
335 			break;
336 		}
337 	}
338 }
339 
resetInputState()340 void GameManager::resetInputState() {
341 	setObjectNull(_inputObject[0]);
342 	setObjectNull(_inputObject[1]);
343 	_inputVerb = ACTION_WALK;
344 	_processInput = false;
345 	_mouseClicked = false;
346 	_keyPressed = false;
347 	_key.reset();
348 	_mouseClickType = Common::EVENT_MOUSEMOVE;
349 
350 	processInput();
351 }
352 
processInput()353 void GameManager::processInput() {
354 	enum {
355 		onNone,
356 		onObject,
357 		onCmdButton,
358 		onInventory,
359 		onInventoryArrowUp,
360 		onInventoryArrowDown
361 	} mouseLocation;
362 
363 	if (_mouseField >= 0 && _mouseField < 256)
364 		mouseLocation = onObject;
365 	else if (_mouseField >= 256 && _mouseField < 512)
366 		mouseLocation = onCmdButton;
367 	else if (_mouseField >= 512 && _mouseField < 768)
368 		mouseLocation = onInventory;
369 	else if (_mouseField == 768)
370 		mouseLocation = onInventoryArrowUp;
371 	else if (_mouseField == 769)
372 		mouseLocation = onInventoryArrowDown;
373 	else
374 		mouseLocation = onNone;
375 
376 	if (_mouseClickType == Common::EVENT_LBUTTONUP) {
377 		if (_vm->_screen->isMessageShown()) {
378 			// Hide the message and consume the event
379 			_vm->removeMessage();
380 			if (mouseLocation != onCmdButton)
381 				return;
382 		}
383 
384 		switch(mouseLocation) {
385 		case onObject:
386 		case onInventory:
387 			// Fallthrough
388 			if (_inputVerb == ACTION_GIVE || _inputVerb == ACTION_USE) {
389 				if (isNullObject(_inputObject[0])) {
390 					_inputObject[0] = _currentInputObject;
391 					if (!_inputObject[0]->hasProperty(COMBINABLE))
392 						_processInput = true;
393 				} else {
394 					_inputObject[1] = _currentInputObject;
395 					_processInput = true;
396 				}
397 			} else {
398 				_inputObject[0] = _currentInputObject;
399 				if (!isNullObject(_currentInputObject))
400 					_processInput = true;
401 			}
402 			break;
403 		case onCmdButton:
404 			resetInputState();
405 			_inputVerb = static_cast<Action>(_mouseField - 256);
406 			break;
407 		case onInventoryArrowUp:
408 			if (_inventoryScroll >= 2)
409 				_inventoryScroll -= 2;
410 			break;
411 		case onInventoryArrowDown:
412 			if (_inventoryScroll < _inventory.getSize() - ARRAYSIZE(_guiInventory))
413 				_inventoryScroll += 2;
414 			break;
415 		case onNone:
416 		default:
417 			break;
418 		}
419 
420 	} else if (_mouseClickType == Common::EVENT_RBUTTONUP) {
421 		if (_vm->_screen->isMessageShown()) {
422 			// Hide the message and consume the event
423 			_vm->removeMessage();
424 			return;
425 		}
426 
427 		if (isNullObject(_currentInputObject))
428 			return;
429 
430 		if (mouseLocation == onObject || mouseLocation == onInventory) {
431 			_inputObject[0] = _currentInputObject;
432 			ObjectTypes type = _inputObject[0]->_type;
433 			if (type & OPENABLE)
434 				_inputVerb = (type & OPENED) ? ACTION_CLOSE : ACTION_OPEN;
435 			else if (type & PRESS)
436 				_inputVerb = ACTION_PRESS;
437 			else if (type & TALK)
438 				_inputVerb = ACTION_TALK;
439 			else
440 				_inputVerb = ACTION_LOOK;
441 
442 			_processInput = true;
443 		}
444 
445 	} else if (_mouseClickType == Common::EVENT_MOUSEMOVE) {
446 		int field = -1;
447 		int click = -1;
448 
449 		if ((_mouseY >= _guiCommandButton[0].top) && (_mouseY <= _guiCommandButton[0].bottom)) {
450 			/* command row */
451 			field = 9;
452 			while (_mouseX < _guiCommandButton[field].left - 1)
453 				field--;
454 			field += 256;
455 		} else if ((_mouseX >= 283) && (_mouseX <= 317) && (_mouseY >= 163) && (_mouseY <= 197)) {
456 			/* exit box */
457 			field = _exitList[(_mouseX - 283) / 7 + 5 * ((_mouseY - 163) / 7)];
458 		} else if ((_mouseY >= 161) && (_mouseX <= 270)) {
459 			/* inventory box */
460 			field = (_mouseX + 1) / 136 + ((_mouseY - 161) / 10) * 2;
461 			if (field + _inventoryScroll < _inventory.getSize())
462 				field += 512;
463 			else
464 				field = -1;
465 		} else if ((_mouseY >= 161) && (_mouseX >= 271) && (_mouseX < 279)) {
466 			/* inventory arrows */
467 			field = (_mouseY > 180) ? 769 : 768;
468 		} else {
469 			/* normal item */
470 			for (int i = 0; (_currentRoom->getObject(i)->_id != INVALIDOBJECT) &&
471 							(field == -1) && i < kMaxObject; i++) {
472 				click = _currentRoom->getObject(i)->_click;
473 				const MSNImage *image = _vm->_screen->getCurrentImage();
474 				if (click != 255 && image) {
475 					const MSNImage::ClickField *clickField = image->_clickField;
476 					do {
477 						if ((_mouseX >= clickField[click].x1) && (_mouseX <= clickField[click].x2) &&
478 							(_mouseY >= clickField[click].y1) && (_mouseY <= clickField[click].y2))
479 							field = i;
480 
481 						click = clickField[click].next;
482 					} while ((click != 0) && (field == -1));
483 				}
484 			}
485 		}
486 
487 		if (_mouseField != field) {
488 			switch (mouseLocation) {
489 			case onInventoryArrowUp:
490 			case onInventoryArrowDown:
491 				// Fallthrough
492 				_guiInventoryArrow[_mouseField - 768].setHighlight(false);
493 				break;
494 			case onInventory:
495 				_guiInventory[_mouseField - 512].setHighlight(false);
496 				break;
497 			case onCmdButton:
498 				_guiCommandButton[_mouseField - 256].setHighlight(false);
499 				break;
500 			case onObject:
501 			case onNone:
502 			default:
503 				// Fallthrough
504 				break;
505 			}
506 
507 			setObjectNull(_currentInputObject);
508 
509 			_mouseField = field;
510 			if (_mouseField >= 0 && _mouseField < 256)
511 				mouseLocation = onObject;
512 			else if (_mouseField >= 256 && _mouseField < 512)
513 				mouseLocation = onCmdButton;
514 			else if (_mouseField >= 512 && _mouseField < 768)
515 				mouseLocation = onInventory;
516 			else if (_mouseField == 768)
517 				mouseLocation = onInventoryArrowUp;
518 			else if (_mouseField == 769)
519 				mouseLocation = onInventoryArrowDown;
520 			else
521 				mouseLocation = onNone;
522 
523 			switch (mouseLocation) {
524 			case onInventoryArrowUp:
525 			case onInventoryArrowDown:
526 				// Fallthrough
527 				_guiInventoryArrow[_mouseField - 768].setHighlight(true);
528 				break;
529 			case onInventory:
530 				_guiInventory[_mouseField - 512].setHighlight(true);
531 				_currentInputObject = _inventory.get(_mouseField - 512 + _inventoryScroll);
532 				break;
533 			case onCmdButton:
534 				_guiCommandButton[_mouseField - 256].setHighlight(true);
535 				break;
536 			case onObject:
537 				_currentInputObject = _currentRoom->getObject(_mouseField);
538 				break;
539 			case onNone:
540 			default:
541 				break;
542 			}
543 		}
544 	}
545 }
546 
setObjectNull(Object * & obj)547 void GameManager::setObjectNull(Object *&obj) {
548 	obj = &_nullObject;
549 }
550 
isNullObject(Object * obj)551 bool GameManager::isNullObject(Object *obj) {
552 	return obj == &_nullObject;
553 }
554 
sentence(int number,bool brightness)555 void GameManager::sentence(int number, bool brightness) {
556 	if (number < 0)
557 		return;
558 	_vm->renderBox(0, 141 + _rowsStart[number] * 10, 320, _rows[number] * 10 - 1, brightness ? kColorWhite44 : kColorWhite25);
559 	if (_texts[_rowsStart[number]] == kStringDialogSeparator)
560 		_vm->renderText(kStringConversationEnd, 1, 142 + _rowsStart[number] * 10, brightness ? kColorRed : kColorDarkRed);
561 	else {
562 		for (int r = _rowsStart[number]; r < _rowsStart[number] + _rows[number]; ++r)
563 			_vm->renderText(_texts[r], 1, 142 + r * 10, brightness ? kColorGreen : kColorDarkGreen);
564 	}
565 }
566 
say(int textId)567 void GameManager::say(int textId) {
568 	Common::String str = _vm->getGameString(textId);
569 	if (!str.empty())
570 		say(str.c_str());
571 }
572 
say(const char * text)573 void GameManager::say(const char *text) {
574 	Common::String t(text);
575 	char *row[6];
576 	Common::String::iterator p = t.begin();
577 	uint numRows = 0;
578 	while (*p) {
579 		row[numRows++] = p;
580 		while ((*p != '\0') && (*p != '|')) {
581 			++p;
582 		}
583 		if (*p == '|') {
584 			*p = 0;
585 			++p;
586 		}
587 	}
588 
589 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
590 	_vm->renderBox(0, 141, 320, numRows * 10 - 1, kColorWhite25);
591 	for (uint r = 0; r < numRows; ++r)
592 		_vm->renderText(row[r], 1, 142 + r * 10, kColorDarkGreen);
593 	wait((t.size() + 20) * _vm->_textSpeed / 10, true);
594 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
595 }
596 
reply(int textId,int aus1,int aus2)597 void GameManager::reply(int textId, int aus1, int aus2) {
598 	Common::String str = _vm->getGameString(textId);
599 	if (!str.empty())
600 		reply(str.c_str(), aus1, aus2);
601 }
602 
reply(const char * text,int aus1,int aus2)603 void GameManager::reply(const char *text, int aus1, int aus2) {
604 	if (*text != '|')
605 		_vm->renderMessage(text, kMessageTop);
606 
607 	for (int z = (strlen(text) + 20) * _vm->_textSpeed / 40; z > 0; --z) {
608 		if (aus1)
609 			_vm->renderImage(aus1);
610 		wait(2, true);
611 		if (_keyPressed || _mouseClicked)
612 			z = 1;
613 		if (aus2)
614 			_vm->renderImage(aus2);
615 		wait(2, true);
616 		if (_keyPressed || _mouseClicked)
617 			z = 1;
618 	}
619 	if (*text != '|')
620 		_vm->removeMessage();
621 }
622 
dialog(int num,byte rowLength[6],int text[6],int number)623 int GameManager::dialog(int num, byte rowLength[6], int text[6], int number) {
624 	_vm->_allowLoadGame = false;
625 	_guiEnabled = false;
626 
627 	bool remove[6];
628 	for (int i = 0; i < 6; ++i)
629 		remove[i] = _currentRoom->sentenceRemoved(i, number);
630 
631 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
632 
633 	for (int i = 0; i < 6 ; ++i)
634 		_sentenceNumber[i] = -1;
635 
636 	int r = 0, rq = 0;
637 	for (int i = 0; i < num; ++i) {
638 		if (!remove[i]) {
639 			_rowsStart[i] = r;
640 			_rows[i] = rowLength[i];
641 			for (int j = 0; j < _rows[i]; ++j, ++r, ++rq) {
642 				_texts[r] = text[rq];
643 				_sentenceNumber[r] = i;
644 			}
645 			sentence(i, false);
646 		} else
647 			rq += rowLength[i];
648 	}
649 
650 	_currentSentence = -1;
651 	do {
652 		do {
653 			updateEvents();
654 			mousePosDialog(_mouseX, _mouseY);
655 			g_system->updateScreen();
656 			g_system->delayMillis(_vm->_delay);
657 		} while (!_mouseClicked && !_vm->shouldQuit());
658 	} while (_currentSentence == -1 && !_vm->shouldQuit());
659 
660 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
661 
662 	if (number && _currentSentence != -1 && _texts[_rowsStart[_currentSentence]] != kStringDialogSeparator)
663 		_currentRoom->removeSentence(_currentSentence, number);
664 
665 	_guiEnabled = true;
666 	_vm->_allowLoadGame = true;
667 
668 	return _currentSentence;
669 }
670 
mousePosDialog(int x,int y)671 void GameManager::mousePosDialog(int x, int y) {
672 	int a = y < 141 ? -1 : _sentenceNumber[(y - 141) / 10];
673 	if (a != _currentSentence) {
674 		sentence(_currentSentence, false);
675 		_currentSentence = a;
676 		sentence(_currentSentence, true);
677 	}
678 }
679 
takeObject(Object & obj)680 void GameManager::takeObject(Object &obj) {
681 	if (obj.hasProperty(CARRIED))
682 		return;
683 
684 	if (obj._section != 0)
685 		_vm->renderImage(obj._section);
686 	obj._click = obj._click2 = 255;
687 	_inventory.add(obj);
688 }
689 
drawCommandBox()690 void GameManager::drawCommandBox() {
691 	for (int i = 0; i < ARRAYSIZE(_guiCommandButton); ++i) {
692 		_vm->renderBox(_guiCommandButton[i]);
693 		int space = (_guiCommandButton[i].width() - Screen::textWidth(_guiCommandButton[i].getText())) / 2;
694 		_vm->renderText(_guiCommandButton[i].getText(),
695 						_guiCommandButton[i].getTextPos().x + space,
696 						_guiCommandButton[i].getTextPos().y,
697 						_guiCommandButton[i].getTextColor());
698 	}
699 }
700 
drawInventory()701 void GameManager::drawInventory() {
702 	for (int i = 0; i < ARRAYSIZE(_guiInventory); ++i) {
703 		_vm->renderBox(_guiInventory[i]);
704 		_vm->renderText(_inventory.get(i + _inventoryScroll)->_name,
705 						_guiInventory[i].getTextPos().x,
706 						_guiInventory[i].getTextPos().y,
707 						_guiInventory[i].getTextColor());
708 	}
709 
710 	_vm->renderBox(_guiInventoryArrow[0]);
711 	_vm->renderBox(_guiInventoryArrow[1]);
712 	if (_inventory.getSize() > ARRAYSIZE(_guiInventory)) {
713 		if (_inventoryScroll != 0)
714 			_vm->renderText(_guiInventoryArrow[0]);
715 		if (_inventoryScroll + ARRAYSIZE(_guiInventory) < _inventory.getSize())
716 			_vm->renderText(_guiInventoryArrow[1]);
717 	}
718 }
719 
getInput(bool onlyKeys)720 void GameManager::getInput(bool onlyKeys) {
721 	while (!_vm->shouldQuit()) {
722 		updateEvents();
723 		if ((_mouseClicked && !onlyKeys) || _keyPressed)
724 			break;
725 		g_system->updateScreen();
726 		g_system->delayMillis(_vm->_delay);
727 	}
728 }
729 
roomBrightness()730 void GameManager::roomBrightness() {
731 }
732 
changeRoom(RoomId id)733 void GameManager::changeRoom(RoomId id) {
734 	_currentRoom = _rooms[id];
735 	_newRoom = true;
736 
737 	for (int i = 0; i < 25; i++)
738 		_exitList[i] = -1;
739 	for (int i = 0; i < kMaxObject; i++) {
740 		if (_currentRoom->getObject(i)->hasProperty(EXIT)) {
741 			byte r = _currentRoom->getObject(i)->_direction;
742 			_exitList[r] = i;
743 		}
744 	}
745 }
746 
wait(int ticks,bool checkInput)747 void GameManager::wait(int ticks, bool checkInput) {
748 	int32 end = _time + ticksToMsec(ticks);
749 	bool inputEvent = false;
750 	do {
751 		g_system->delayMillis(_vm->_delay);
752 		updateEvents();
753 		g_system->updateScreen();
754 		if (checkInput)
755 			inputEvent = _keyPressed || _mouseClicked;
756 	} while (_time < end && !_vm->shouldQuit() && !inputEvent);
757 }
758 
waitOnInput(int ticks,Common::KeyCode & keycode)759 bool GameManager::waitOnInput(int ticks, Common::KeyCode &keycode) {
760 	keycode = Common::KEYCODE_INVALID;
761 	int32 end = _time + ticksToMsec(ticks);
762 	do {
763 		g_system->delayMillis(_vm->_delay);
764 		updateEvents();
765 		g_system->updateScreen();
766 		if (_keyPressed) {
767 			keycode = _key.keycode;
768 			_key.reset();
769 			return true;
770 		} else if (_mouseClicked)
771 			return true;
772 	} while (_time < end  && !_vm->shouldQuit());
773 	return false;
774 }
775 
setAnimationTimer(int ticks)776 void GameManager::setAnimationTimer(int ticks) {
777 	_animationTimer = ticksToMsec(ticks);
778 }
779 
handleTime()780 void GameManager::handleTime() {
781 }
782 
pauseTimer(bool pause)783 void GameManager::pauseTimer(bool pause) {
784 	if (pause == _timerPaused)
785 		return;
786 
787 	if (pause) {
788 		_timerPaused = true;
789 		int32 delta = g_system->getMillis() - _oldTime;
790 		_timePaused = _time + delta;
791 	} else {
792 		_time = _timePaused;
793 		_oldTime = g_system->getMillis();
794 		_timerPaused = false;
795 	}
796 }
797 
loadTime()798 void GameManager::loadTime() {
799 }
800 
saveTime()801 void GameManager::saveTime() {
802 }
803 
screenShake()804 void GameManager::screenShake() {
805 	for (int i = 0; i < 12; ++i) {
806 		_vm->_system->setShakePos(0, 8);
807 		wait(1);
808 		_vm->_system->setShakePos(0, 0);
809 		wait(1);
810 	}
811 }
812 
showMenu()813 void GameManager::showMenu() {
814 	_vm->renderBox(0, 138, 320, 62, 0);
815 	_vm->renderBox(0, 140, 320, 9, kColorWhite25);
816 	drawCommandBox();
817 	_vm->renderBox(281, 161, 39, 39, kColorWhite25);
818 	drawInventory();
819 }
820 
drawMapExits()821 void GameManager::drawMapExits() {
822 }
823 
animationOff()824 void GameManager::animationOff() {
825 	_animationEnabled = false;
826 }
827 
animationOn()828 void GameManager::animationOn() {
829 	_animationEnabled = true;
830 }
831 
edit(Common::String & input,int x,int y,uint length)832 void GameManager::edit(Common::String &input, int x, int y, uint length) {
833 	bool isEditing = true;
834 	uint cursorIndex = input.size();
835 	// NOTE: Pixels for char needed = kFontWidth + 2px left and right side bearing
836 	int overdrawWidth = 0;
837 	Color background = kColorBlack;
838 
839 	if (_vm->_MSPart == 1) {
840 		overdrawWidth = ((int)((length + 1) * (kFontWidth + 2)) > (kScreenWidth - x)) ?
841 						kScreenWidth - x : (length + 1) * (kFontWidth + 2);
842 		background = kColorDarkBlue;
843 	} else if (_vm->_MSPart == 2) {
844 		overdrawWidth = ((int)((length + 1) * (kFontWidth2 + 2)) > (kScreenWidth - x))
845 			? kScreenWidth - x : (length + 1) * (kFontWidth2 + 2);
846 		background = kColorWhite35;
847 	}
848 	_guiEnabled = false;
849 	while (isEditing) {
850 		_vm->_screen->setTextCursorPos(x, y);
851 		_vm->_screen->setTextCursorColor(kColorWhite99);
852 		_vm->renderBox(x, y - 1, overdrawWidth, 9, background);
853 		for (uint i = 0; i < input.size(); ++i) {
854 			// Draw char highlight depending on cursor position
855 			if (i == cursorIndex) {
856 				_vm->renderBox(_vm->_screen->getTextCursorPos().x, y - 1,
857 							   Screen::textWidth(input[i]), 9, kColorWhite99);
858 				_vm->_screen->setTextCursorColor(background);
859 				_vm->renderText((uint16)input[i]);
860 				_vm->_screen->setTextCursorColor(kColorWhite99);
861 			} else
862 				_vm->renderText((uint16)input[i]);
863 		}
864 
865 		if (cursorIndex == input.size()) {
866 			_vm->renderBox(_vm->_screen->getTextCursorPos().x + 1, y - 1, 6, 9, background);
867 			_vm->renderBox(_vm->_screen->getTextCursorPos().x, y - 1, 1, 9, kColorWhite99);
868 		}
869 
870 		getInput(true);
871 		if (_vm->shouldQuit())
872 			break;
873 		switch (_key.keycode) {
874 		case Common::KEYCODE_RETURN:
875 		case Common::KEYCODE_ESCAPE:
876 			isEditing = false;
877 			break;
878 		case Common::KEYCODE_UP:
879 		case Common::KEYCODE_DOWN:
880 			cursorIndex = input.size();
881 			break;
882 		case Common::KEYCODE_LEFT:
883 			if (cursorIndex != 0)
884 				--cursorIndex;
885 			break;
886 		case Common::KEYCODE_RIGHT:
887 			if (cursorIndex != input.size())
888 				++cursorIndex;
889 			break;
890 		case Common::KEYCODE_DELETE:
891 			if (cursorIndex != input.size())
892 				input.deleteChar(cursorIndex);
893 			break;
894 		case Common::KEYCODE_BACKSPACE:
895 			if (cursorIndex != 0) {
896 				--cursorIndex;
897 				input.deleteChar(cursorIndex);
898 			}
899 			break;
900 		default:
901 			if (Common::isPrint(_key.ascii) && input.size() < length) {
902 				input.insertChar(_key.ascii, cursorIndex);
903 				++cursorIndex;
904 			}
905 			break;
906 		}
907 	}
908 	_guiEnabled = true;
909 }
910 
takeMoney(int amount)911 void GameManager::takeMoney(int amount) {
912 }
913 
drawStatus()914 void GameManager::drawStatus() {
915 	int index = static_cast<int>(_inputVerb);
916 	_vm->renderBox(0, 140, 320, 9, kColorWhite25);
917 	_vm->renderText(_vm->getGameString(guiStatusCommands[index]), 1, 141, kColorDarkGreen);
918 
919 	if (isNullObject(_inputObject[0]))
920 		_vm->renderText(_currentInputObject->_name);
921 	else {
922 		_vm->renderText(_inputObject[0]->_name);
923 		if (_inputVerb == ACTION_GIVE)
924 			_vm->renderText(kPhrasalVerbParticleGiveTo);
925 		else if (_inputVerb == ACTION_USE)
926 			_vm->renderText(kPhrasalVerbParticleUseWith);
927 
928 		_vm->renderText(_currentInputObject->_name);
929 	}
930 }
931 
dead(int messageId)932 void GameManager::dead(int messageId) {
933 	_vm->paletteFadeOut();
934 	_guiEnabled = false;
935 	if (_vm->_MSPart == 1)
936 		_vm->setCurrentImage(11);
937 	else if (_vm->_MSPart == 2)
938 		_vm->setCurrentImage(43);
939 	_vm->renderImage(0);
940 	_vm->renderMessage(messageId);
941 	if (_vm->_MSPart == 1)
942 		_sound->play(kAudioDeath);
943 	else if (_vm->_MSPart == 2)
944 		_sound->play(kAudioDeath2);
945 	_vm->paletteFadeIn();
946 	getInput();
947 	_vm->paletteFadeOut();
948 	_vm->removeMessage();
949 
950 	_inventory.clear();
951 	destroyRooms();
952 	initRooms();
953 	initState();
954 	if (_vm->_MSPart == 1)
955 		changeRoom(CABIN_R3);
956 	else if (_vm->_MSPart == 2)
957 		changeRoom(AIRPORT);
958 	initGui();
959 	g_system->fillScreen(kColorBlack);
960 	_vm->paletteFadeIn();
961 
962 	_guiEnabled = true;
963 	_dead = true;
964 }
965 
invertSection(int section)966 int GameManager::invertSection(int section) {
967 	if (section < kSectionInvert)
968 		section += kSectionInvert;
969 	else
970 		section -= kSectionInvert;
971 
972 	return section;
973 }
974 
genericInteract(Action verb,Object & obj1,Object & obj2)975 bool GameManager::genericInteract(Action verb, Object &obj1, Object &obj2) {
976 	return false;
977 }
978 
handleInput()979 void GameManager::handleInput() {
980 }
981 
executeRoom()982 void GameManager::executeRoom() {
983 }
984 
drawGUI()985 void GameManager::drawGUI() {
986 	drawMapExits();
987 	drawInventory();
988 	drawStatus();
989 	drawCommandBox();
990 }
991 
992 }
993