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 	case Common::KEYCODE_d:
290 		if (state.flags & Common::KBD_CTRL)
291 			_vm->_console->attach();
292 		break;
293 	default:
294 		break;
295 	}
296 	if (_vm->_improved && _guiEnabled) {
297 		switch (state.keycode) {
298 		case Common::KEYCODE_1:
299 			resetInputState();
300 			_inputVerb = ACTION_WALK;
301 			break;
302 		case Common::KEYCODE_2:
303 			resetInputState();
304 			_inputVerb = ACTION_LOOK;
305 			break;
306 		case Common::KEYCODE_3:
307 			resetInputState();
308 			_inputVerb = ACTION_TAKE;
309 			break;
310 		case Common::KEYCODE_4:
311 			resetInputState();
312 			_inputVerb = ACTION_OPEN;
313 			break;
314 		case Common::KEYCODE_5:
315 			resetInputState();
316 			_inputVerb = ACTION_CLOSE;
317 			break;
318 		case Common::KEYCODE_6:
319 			resetInputState();
320 			_inputVerb = ACTION_PRESS;
321 			break;
322 		case Common::KEYCODE_7:
323 			resetInputState();
324 			_inputVerb = ACTION_PULL;
325 			break;
326 		case Common::KEYCODE_8:
327 			resetInputState();
328 			_inputVerb = ACTION_USE;
329 			break;
330 		case Common::KEYCODE_9:
331 			resetInputState();
332 			_inputVerb = ACTION_TALK;
333 			break;
334 		case Common::KEYCODE_0:
335 			resetInputState();
336 			_inputVerb = ACTION_GIVE;
337 			break;
338 		default:
339 			break;
340 		}
341 	}
342 }
343 
resetInputState()344 void GameManager::resetInputState() {
345 	setObjectNull(_inputObject[0]);
346 	setObjectNull(_inputObject[1]);
347 	_inputVerb = ACTION_WALK;
348 	_processInput = false;
349 	_mouseClicked = false;
350 	_keyPressed = false;
351 	_key.reset();
352 	_mouseClickType = Common::EVENT_MOUSEMOVE;
353 
354 	processInput();
355 }
356 
processInput()357 void GameManager::processInput() {
358 	enum {
359 		onNone,
360 		onObject,
361 		onCmdButton,
362 		onInventory,
363 		onInventoryArrowUp,
364 		onInventoryArrowDown
365 	} mouseLocation;
366 
367 	if (_mouseField >= 0 && _mouseField < 256)
368 		mouseLocation = onObject;
369 	else if (_mouseField >= 256 && _mouseField < 512)
370 		mouseLocation = onCmdButton;
371 	else if (_mouseField >= 512 && _mouseField < 768)
372 		mouseLocation = onInventory;
373 	else if (_mouseField == 768)
374 		mouseLocation = onInventoryArrowUp;
375 	else if (_mouseField == 769)
376 		mouseLocation = onInventoryArrowDown;
377 	else
378 		mouseLocation = onNone;
379 
380 	if (_mouseClickType == Common::EVENT_LBUTTONUP) {
381 		if (_vm->_screen->isMessageShown()) {
382 			// Hide the message and consume the event
383 			_vm->removeMessage();
384 			if (mouseLocation != onCmdButton)
385 				return;
386 		}
387 
388 		switch(mouseLocation) {
389 		case onObject:
390 		case onInventory:
391 			// Fallthrough
392 			if (_inputVerb == ACTION_GIVE || _inputVerb == ACTION_USE) {
393 				if (isNullObject(_inputObject[0])) {
394 					_inputObject[0] = _currentInputObject;
395 					if (!_inputObject[0]->hasProperty(COMBINABLE))
396 						_processInput = true;
397 				} else {
398 					_inputObject[1] = _currentInputObject;
399 					_processInput = true;
400 				}
401 			} else {
402 				_inputObject[0] = _currentInputObject;
403 				if (!isNullObject(_currentInputObject))
404 					_processInput = true;
405 			}
406 			break;
407 		case onCmdButton:
408 			resetInputState();
409 			_inputVerb = static_cast<Action>(_mouseField - 256);
410 			break;
411 		case onInventoryArrowUp:
412 			if (_inventoryScroll >= 2)
413 				_inventoryScroll -= 2;
414 			break;
415 		case onInventoryArrowDown:
416 			if (_inventoryScroll < _inventory.getSize() - ARRAYSIZE(_guiInventory))
417 				_inventoryScroll += 2;
418 			break;
419 		case onNone:
420 			break;
421 		}
422 
423 	} else if (_mouseClickType == Common::EVENT_RBUTTONUP) {
424 		if (_vm->_screen->isMessageShown()) {
425 			// Hide the message and consume the event
426 			_vm->removeMessage();
427 			return;
428 		}
429 
430 		if (isNullObject(_currentInputObject))
431 			return;
432 
433 		if (mouseLocation == onObject || mouseLocation == onInventory) {
434 			_inputObject[0] = _currentInputObject;
435 			ObjectTypes type = _inputObject[0]->_type;
436 			if (type & OPENABLE)
437 				_inputVerb = (type & OPENED) ? ACTION_CLOSE : ACTION_OPEN;
438 			else if (type & PRESS)
439 				_inputVerb = ACTION_PRESS;
440 			else if (type & TALK)
441 				_inputVerb = ACTION_TALK;
442 			else
443 				_inputVerb = ACTION_LOOK;
444 
445 			_processInput = true;
446 		}
447 
448 	} else if (_mouseClickType == Common::EVENT_MOUSEMOVE) {
449 		int field = -1;
450 		int click = -1;
451 
452 		if ((_mouseY >= _guiCommandButton[0].top) && (_mouseY <= _guiCommandButton[0].bottom)) {
453 			/* command row */
454 			field = 9;
455 			while (_mouseX < _guiCommandButton[field].left - 1)
456 				field--;
457 			field += 256;
458 		} else if ((_mouseX >= 283) && (_mouseX <= 317) && (_mouseY >= 163) && (_mouseY <= 197)) {
459 			/* exit box */
460 			field = _exitList[(_mouseX - 283) / 7 + 5 * ((_mouseY - 163) / 7)];
461 		} else if ((_mouseY >= 161) && (_mouseX <= 270)) {
462 			/* inventory box */
463 			field = (_mouseX + 1) / 136 + ((_mouseY - 161) / 10) * 2;
464 			if (field + _inventoryScroll < _inventory.getSize())
465 				field += 512;
466 			else
467 				field = -1;
468 		} else if ((_mouseY >= 161) && (_mouseX >= 271) && (_mouseX < 279)) {
469 			/* inventory arrows */
470 			field = (_mouseY > 180) ? 769 : 768;
471 		} else {
472 			/* normal item */
473 			for (int i = 0; (_currentRoom->getObject(i)->_id != INVALIDOBJECT) &&
474 							(field == -1) && i < kMaxObject; i++) {
475 				click = _currentRoom->getObject(i)->_click;
476 				const MSNImage *image = _vm->_screen->getCurrentImage();
477 				if (click != 255 && image) {
478 					const MSNImage::ClickField *clickField = image->_clickField;
479 					do {
480 						if ((_mouseX >= clickField[click].x1) && (_mouseX <= clickField[click].x2) &&
481 							(_mouseY >= clickField[click].y1) && (_mouseY <= clickField[click].y2))
482 							field = i;
483 
484 						click = clickField[click].next;
485 					} while ((click != 0) && (field == -1));
486 				}
487 			}
488 		}
489 
490 		if (_mouseField != field) {
491 			switch (mouseLocation) {
492 			case onInventoryArrowUp:
493 			case onInventoryArrowDown:
494 				// Fallthrough
495 				_guiInventoryArrow[_mouseField - 768].setHighlight(false);
496 				break;
497 			case onInventory:
498 				_guiInventory[_mouseField - 512].setHighlight(false);
499 				break;
500 			case onCmdButton:
501 				_guiCommandButton[_mouseField - 256].setHighlight(false);
502 				break;
503 			case onObject:
504 			case onNone:
505 				// Fallthrough
506 				break;
507 			}
508 
509 			setObjectNull(_currentInputObject);
510 
511 			_mouseField = field;
512 			if (_mouseField >= 0 && _mouseField < 256)
513 				mouseLocation = onObject;
514 			else if (_mouseField >= 256 && _mouseField < 512)
515 				mouseLocation = onCmdButton;
516 			else if (_mouseField >= 512 && _mouseField < 768)
517 				mouseLocation = onInventory;
518 			else if (_mouseField == 768)
519 				mouseLocation = onInventoryArrowUp;
520 			else if (_mouseField == 769)
521 				mouseLocation = onInventoryArrowDown;
522 			else
523 				mouseLocation = onNone;
524 
525 			switch (mouseLocation) {
526 			case onInventoryArrowUp:
527 			case onInventoryArrowDown:
528 				// Fallthrough
529 				_guiInventoryArrow[_mouseField - 768].setHighlight(true);
530 				break;
531 			case onInventory:
532 				_guiInventory[_mouseField - 512].setHighlight(true);
533 				_currentInputObject = _inventory.get(_mouseField - 512 + _inventoryScroll);
534 				break;
535 			case onCmdButton:
536 				_guiCommandButton[_mouseField - 256].setHighlight(true);
537 				break;
538 			case onObject:
539 				_currentInputObject = _currentRoom->getObject(_mouseField);
540 				break;
541 			case onNone:
542 				break;
543 			}
544 		}
545 	}
546 }
547 
setObjectNull(Object * & obj)548 void GameManager::setObjectNull(Object *&obj) {
549 	obj = &_nullObject;
550 }
551 
isNullObject(Object * obj)552 bool GameManager::isNullObject(Object *obj) {
553 	return obj == &_nullObject;
554 }
555 
sentence(int number,bool brightness)556 void GameManager::sentence(int number, bool brightness) {
557 	if (number < 0)
558 		return;
559 	_vm->renderBox(0, 141 + _rowsStart[number] * 10, 320, _rows[number] * 10 - 1, brightness ? kColorWhite44 : kColorWhite25);
560 	if (_texts[_rowsStart[number]] == kStringDialogSeparator)
561 		_vm->renderText(kStringConversationEnd, 1, 142 + _rowsStart[number] * 10, brightness ? kColorRed : kColorDarkRed);
562 	else {
563 		for (int r = _rowsStart[number]; r < _rowsStart[number] + _rows[number]; ++r)
564 			_vm->renderText(_texts[r], 1, 142 + r * 10, brightness ? kColorGreen : kColorDarkGreen);
565 	}
566 }
567 
say(int textId)568 void GameManager::say(int textId) {
569 	Common::String str = _vm->getGameString(textId);
570 	if (!str.empty())
571 		say(str.c_str());
572 }
573 
say(const char * text)574 void GameManager::say(const char *text) {
575 	Common::String t(text);
576 	char *row[6];
577 	Common::String::iterator p = t.begin();
578 	uint numRows = 0;
579 	while (*p) {
580 		row[numRows++] = p;
581 		while ((*p != '\0') && (*p != '|')) {
582 			++p;
583 		}
584 		if (*p == '|') {
585 			*p = 0;
586 			++p;
587 		}
588 	}
589 
590 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
591 	_vm->renderBox(0, 141, 320, numRows * 10 - 1, kColorWhite25);
592 	for (uint r = 0; r < numRows; ++r)
593 		_vm->renderText(row[r], 1, 142 + r * 10, kColorDarkGreen);
594 	wait((t.size() + 20) * _vm->_textSpeed / 10, true);
595 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
596 }
597 
reply(int textId,int aus1,int aus2)598 void GameManager::reply(int textId, int aus1, int aus2) {
599 	Common::String str = _vm->getGameString(textId);
600 	if (!str.empty())
601 		reply(str.c_str(), aus1, aus2);
602 }
603 
reply(const char * text,int aus1,int aus2)604 void GameManager::reply(const char *text, int aus1, int aus2) {
605 	if (*text != '|')
606 		_vm->renderMessage(text, kMessageTop);
607 
608 	for (int z = (strlen(text) + 20) * _vm->_textSpeed / 40; z > 0; --z) {
609 		if (aus1)
610 			_vm->renderImage(aus1);
611 		wait(2, true);
612 		if (_keyPressed || _mouseClicked)
613 			z = 1;
614 		if (aus2)
615 			_vm->renderImage(aus2);
616 		wait(2, true);
617 		if (_keyPressed || _mouseClicked)
618 			z = 1;
619 	}
620 	if (*text != '|')
621 		_vm->removeMessage();
622 }
623 
dialog(int num,byte rowLength[6],int text[6],int number)624 int GameManager::dialog(int num, byte rowLength[6], int text[6], int number) {
625 	_vm->_allowLoadGame = false;
626 	_guiEnabled = false;
627 
628 	bool remove[6];
629 	for (int i = 0; i < 6; ++i)
630 		remove[i] = _currentRoom->sentenceRemoved(i, number);
631 
632 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
633 
634 	for (int i = 0; i < 6 ; ++i)
635 		_sentenceNumber[i] = -1;
636 
637 	int r = 0, rq = 0;
638 	for (int i = 0; i < num; ++i) {
639 		if (!remove[i]) {
640 			_rowsStart[i] = r;
641 			_rows[i] = rowLength[i];
642 			for (int j = 0; j < _rows[i]; ++j, ++r, ++rq) {
643 				_texts[r] = text[rq];
644 				_sentenceNumber[r] = i;
645 			}
646 			sentence(i, false);
647 		} else
648 			rq += rowLength[i];
649 	}
650 
651 	_currentSentence = -1;
652 	do {
653 		do {
654 			updateEvents();
655 			mousePosDialog(_mouseX, _mouseY);
656 			g_system->updateScreen();
657 			g_system->delayMillis(_vm->_delay);
658 		} while (!_mouseClicked && !_vm->shouldQuit());
659 	} while (_currentSentence == -1 && !_vm->shouldQuit());
660 
661 	_vm->renderBox(0, 138, 320, 62, kColorBlack);
662 
663 	if (number && _currentSentence != -1 && _texts[_rowsStart[_currentSentence]] != kStringDialogSeparator)
664 		_currentRoom->removeSentence(_currentSentence, number);
665 
666 	_guiEnabled = true;
667 	_vm->_allowLoadGame = true;
668 
669 	return _currentSentence;
670 }
671 
mousePosDialog(int x,int y)672 void GameManager::mousePosDialog(int x, int y) {
673 	int a = y < 141 ? -1 : _sentenceNumber[(y - 141) / 10];
674 	if (a != _currentSentence) {
675 		sentence(_currentSentence, false);
676 		_currentSentence = a;
677 		sentence(_currentSentence, true);
678 	}
679 }
680 
takeObject(Object & obj)681 void GameManager::takeObject(Object &obj) {
682 	if (obj.hasProperty(CARRIED))
683 		return;
684 
685 	if (obj._section != 0)
686 		_vm->renderImage(obj._section);
687 	obj._click = obj._click2 = 255;
688 	_inventory.add(obj);
689 }
690 
drawCommandBox()691 void GameManager::drawCommandBox() {
692 	for (int i = 0; i < ARRAYSIZE(_guiCommandButton); ++i) {
693 		_vm->renderBox(_guiCommandButton[i]);
694 		int space = (_guiCommandButton[i].width() - Screen::textWidth(_guiCommandButton[i].getText())) / 2;
695 		_vm->renderText(_guiCommandButton[i].getText(),
696 						_guiCommandButton[i].getTextPos().x + space,
697 						_guiCommandButton[i].getTextPos().y,
698 						_guiCommandButton[i].getTextColor());
699 	}
700 }
701 
drawInventory()702 void GameManager::drawInventory() {
703 	for (int i = 0; i < ARRAYSIZE(_guiInventory); ++i) {
704 		_vm->renderBox(_guiInventory[i]);
705 		_vm->renderText(_inventory.get(i + _inventoryScroll)->_name,
706 						_guiInventory[i].getTextPos().x,
707 						_guiInventory[i].getTextPos().y,
708 						_guiInventory[i].getTextColor());
709 	}
710 
711 	_vm->renderBox(_guiInventoryArrow[0]);
712 	_vm->renderBox(_guiInventoryArrow[1]);
713 	if (_inventory.getSize() > ARRAYSIZE(_guiInventory)) {
714 		if (_inventoryScroll != 0)
715 			_vm->renderText(_guiInventoryArrow[0]);
716 		if (_inventoryScroll + ARRAYSIZE(_guiInventory) < _inventory.getSize())
717 			_vm->renderText(_guiInventoryArrow[1]);
718 	}
719 }
720 
getInput(bool onlyKeys)721 void GameManager::getInput(bool onlyKeys) {
722 	while (!_vm->shouldQuit()) {
723 		updateEvents();
724 		if ((_mouseClicked && !onlyKeys) || _keyPressed)
725 			break;
726 		g_system->updateScreen();
727 		g_system->delayMillis(_vm->_delay);
728 	}
729 }
730 
roomBrightness()731 void GameManager::roomBrightness() {
732 }
733 
changeRoom(RoomId id)734 void GameManager::changeRoom(RoomId id) {
735 	_currentRoom = _rooms[id];
736 	_newRoom = true;
737 
738 	for (int i = 0; i < 25; i++)
739 		_exitList[i] = -1;
740 	for (int i = 0; i < kMaxObject; i++) {
741 		if (_currentRoom->getObject(i)->hasProperty(EXIT)) {
742 			byte r = _currentRoom->getObject(i)->_direction;
743 			_exitList[r] = i;
744 		}
745 	}
746 }
747 
wait(int ticks,bool checkInput)748 void GameManager::wait(int ticks, bool checkInput) {
749 	int32 end = _time + ticksToMsec(ticks);
750 	bool inputEvent = false;
751 	do {
752 		g_system->delayMillis(_vm->_delay);
753 		updateEvents();
754 		g_system->updateScreen();
755 		if (checkInput)
756 			inputEvent = _keyPressed || _mouseClicked;
757 	} while (_time < end && !_vm->shouldQuit() && !inputEvent);
758 }
759 
waitOnInput(int ticks,Common::KeyCode & keycode)760 bool GameManager::waitOnInput(int ticks, Common::KeyCode &keycode) {
761 	keycode = Common::KEYCODE_INVALID;
762 	int32 end = _time + ticksToMsec(ticks);
763 	do {
764 		g_system->delayMillis(_vm->_delay);
765 		updateEvents();
766 		g_system->updateScreen();
767 		if (_keyPressed) {
768 			keycode = _key.keycode;
769 			_key.reset();
770 			return true;
771 		} else if (_mouseClicked)
772 			return true;
773 	} while (_time < end  && !_vm->shouldQuit());
774 	return false;
775 }
776 
setAnimationTimer(int ticks)777 void GameManager::setAnimationTimer(int ticks) {
778 	_animationTimer = ticksToMsec(ticks);
779 }
780 
handleTime()781 void GameManager::handleTime() {
782 }
783 
pauseTimer(bool pause)784 void GameManager::pauseTimer(bool pause) {
785 	if (pause == _timerPaused)
786 		return;
787 
788 	if (pause) {
789 		_timerPaused = true;
790 		int32 delta = g_system->getMillis() - _oldTime;
791 		_timePaused = _time + delta;
792 	} else {
793 		_time = _timePaused;
794 		_oldTime = g_system->getMillis();
795 		_timerPaused = false;
796 	}
797 }
798 
loadTime()799 void GameManager::loadTime() {
800 }
801 
saveTime()802 void GameManager::saveTime() {
803 }
804 
screenShake()805 void GameManager::screenShake() {
806 	for (int i = 0; i < 12; ++i) {
807 		_vm->_system->setShakePos(0, 8);
808 		wait(1);
809 		_vm->_system->setShakePos(0, 0);
810 		wait(1);
811 	}
812 }
813 
showMenu()814 void GameManager::showMenu() {
815 	_vm->renderBox(0, 138, 320, 62, 0);
816 	_vm->renderBox(0, 140, 320, 9, kColorWhite25);
817 	drawCommandBox();
818 	_vm->renderBox(281, 161, 39, 39, kColorWhite25);
819 	drawInventory();
820 }
821 
drawMapExits()822 void GameManager::drawMapExits() {
823 }
824 
animationOff()825 void GameManager::animationOff() {
826 	_animationEnabled = false;
827 }
828 
animationOn()829 void GameManager::animationOn() {
830 	_animationEnabled = true;
831 }
832 
edit(Common::String & input,int x,int y,uint length)833 void GameManager::edit(Common::String &input, int x, int y, uint length) {
834 	bool isEditing = true;
835 	uint cursorIndex = input.size();
836 	// NOTE: Pixels for char needed = kFontWidth + 2px left and right side bearing
837 	int overdrawWidth = 0;
838 	Color background = kColorBlack;
839 
840 	if (_vm->_MSPart == 1) {
841 		overdrawWidth = ((int)((length + 1) * (kFontWidth + 2)) > (kScreenWidth - x)) ?
842 						kScreenWidth - x : (length + 1) * (kFontWidth + 2);
843 		background = kColorDarkBlue;
844 	} else if (_vm->_MSPart == 2) {
845 		overdrawWidth = ((int)((length + 1) * (kFontWidth2 + 2)) > (kScreenWidth - x))
846 			? kScreenWidth - x : (length + 1) * (kFontWidth2 + 2);
847 		background = kColorWhite35;
848 	}
849 	_guiEnabled = false;
850 	while (isEditing) {
851 		_vm->_screen->setTextCursorPos(x, y);
852 		_vm->_screen->setTextCursorColor(kColorWhite99);
853 		_vm->renderBox(x, y - 1, overdrawWidth, 9, background);
854 		for (uint i = 0; i < input.size(); ++i) {
855 			// Draw char highlight depending on cursor position
856 			if (i == cursorIndex) {
857 				_vm->renderBox(_vm->_screen->getTextCursorPos().x, y - 1,
858 							   Screen::textWidth(input[i]), 9, kColorWhite99);
859 				_vm->_screen->setTextCursorColor(background);
860 				_vm->renderText((uint16)input[i]);
861 				_vm->_screen->setTextCursorColor(kColorWhite99);
862 			} else
863 				_vm->renderText((uint16)input[i]);
864 		}
865 
866 		if (cursorIndex == input.size()) {
867 			_vm->renderBox(_vm->_screen->getTextCursorPos().x + 1, y - 1, 6, 9, background);
868 			_vm->renderBox(_vm->_screen->getTextCursorPos().x, y - 1, 1, 9, kColorWhite99);
869 		}
870 
871 		getInput(true);
872 		if (_vm->shouldQuit())
873 			break;
874 		switch (_key.keycode) {
875 		case Common::KEYCODE_RETURN:
876 		case Common::KEYCODE_ESCAPE:
877 			isEditing = false;
878 			break;
879 		case Common::KEYCODE_UP:
880 		case Common::KEYCODE_DOWN:
881 			cursorIndex = input.size();
882 			break;
883 		case Common::KEYCODE_LEFT:
884 			if (cursorIndex != 0)
885 				--cursorIndex;
886 			break;
887 		case Common::KEYCODE_RIGHT:
888 			if (cursorIndex != input.size())
889 				++cursorIndex;
890 			break;
891 		case Common::KEYCODE_DELETE:
892 			if (cursorIndex != input.size())
893 				input.deleteChar(cursorIndex);
894 			break;
895 		case Common::KEYCODE_BACKSPACE:
896 			if (cursorIndex != 0) {
897 				--cursorIndex;
898 				input.deleteChar(cursorIndex);
899 			}
900 			break;
901 		default:
902 			if (Common::isPrint(_key.ascii) && input.size() < length) {
903 				input.insertChar(_key.ascii, cursorIndex);
904 				++cursorIndex;
905 			}
906 			break;
907 		}
908 	}
909 	_guiEnabled = true;
910 }
911 
takeMoney(int amount)912 void GameManager::takeMoney(int amount) {
913 }
914 
drawStatus()915 void GameManager::drawStatus() {
916 	int index = static_cast<int>(_inputVerb);
917 	_vm->renderBox(0, 140, 320, 9, kColorWhite25);
918 	_vm->renderText(_vm->getGameString(guiStatusCommands[index]), 1, 141, kColorDarkGreen);
919 
920 	if (isNullObject(_inputObject[0]))
921 		_vm->renderText(_currentInputObject->_name);
922 	else {
923 		_vm->renderText(_inputObject[0]->_name);
924 		if (_inputVerb == ACTION_GIVE)
925 			_vm->renderText(kPhrasalVerbParticleGiveTo);
926 		else if (_inputVerb == ACTION_USE)
927 			_vm->renderText(kPhrasalVerbParticleUseWith);
928 
929 		_vm->renderText(_currentInputObject->_name);
930 	}
931 }
932 
dead(int messageId)933 void GameManager::dead(int messageId) {
934 	_vm->paletteFadeOut();
935 	_guiEnabled = false;
936 	if (_vm->_MSPart == 1)
937 		_vm->setCurrentImage(11);
938 	else if (_vm->_MSPart == 2)
939 		_vm->setCurrentImage(43);
940 	_vm->renderImage(0);
941 	_vm->renderMessage(messageId);
942 	if (_vm->_MSPart == 1)
943 		_sound->play(kAudioDeath);
944 	else if (_vm->_MSPart == 2)
945 		_sound->play(kAudioDeath2);
946 	_vm->paletteFadeIn();
947 	getInput();
948 	_vm->paletteFadeOut();
949 	_vm->removeMessage();
950 
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 	_inventory.clear();
960 	g_system->fillScreen(kColorBlack);
961 	_vm->paletteFadeIn();
962 
963 	_guiEnabled = true;
964 	_dead = true;
965 }
966 
invertSection(int section)967 int GameManager::invertSection(int section) {
968 	if (section < kSectionInvert)
969 		section += kSectionInvert;
970 	else
971 		section -= kSectionInvert;
972 
973 	return section;
974 }
975 
genericInteract(Action verb,Object & obj1,Object & obj2)976 bool GameManager::genericInteract(Action verb, Object &obj1, Object &obj2) {
977 	return false;
978 }
979 
handleInput()980 void GameManager::handleInput() {
981 }
982 
executeRoom()983 void GameManager::executeRoom() {
984 }
985 
drawGUI()986 void GameManager::drawGUI() {
987 	drawMapExits();
988 	drawInventory();
989 	drawStatus();
990 	drawCommandBox();
991 }
992 
993 }
994