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 "engines/nancy/nancy.h"
24 #include "engines/nancy/graphics.h"
25 #include "engines/nancy/resource.h"
26 #include "engines/nancy/input.h"
27 #include "engines/nancy/sound.h"
28 #include "engines/nancy/util.h"
29 
30 #include "engines/nancy/action/orderingpuzzle.h"
31 
32 #include "engines/nancy/state/scene.h"
33 
34 namespace Nancy {
35 namespace Action {
36 
init()37 void OrderingPuzzle::init() {
38 	// Screen position is initialized in readData and fits exactly the bounds of all elements on screen.
39 	// This is a hacky way to make this particular action record work with this implementation's graphics manager
40 	_drawSurface.create(_screenPosition.width(), _screenPosition.height(), g_nancy->_graphicsManager->getInputPixelFormat());
41 	clearAllElements();
42 
43 	setTransparent(true);
44 
45 	g_nancy->_resource->loadImage(_imageName, _image);
46 
47 	setVisible(false);
48 
49 	RenderObject::init();
50 }
51 
readData(Common::SeekableReadStream & stream)52 void OrderingPuzzle::readData(Common::SeekableReadStream &stream) {
53 	readFilename(stream, _imageName);
54 	uint16 numElements = stream.readUint16LE();
55 
56 	_srcRects.reserve(numElements);
57 	for (uint i = 0; i < numElements; ++i) {
58 		_srcRects.push_back(Common::Rect());
59 		readRect(stream, _srcRects.back());
60 	}
61 
62 	stream.skip(16 * (15 - numElements));
63 
64 	_destRects.reserve(numElements);
65 	_drawnElements.reserve(numElements);
66 	for (uint i = 0; i < numElements; ++i) {
67 		_destRects.push_back(Common::Rect());
68 		readRect(stream, _destRects.back());
69 
70 		if (i == 0) {
71 			_screenPosition = _destRects[i];
72 		} else {
73 			_screenPosition.extend(_destRects[i]);
74 		}
75 
76 		_drawnElements.push_back(false);
77 	}
78 
79 	stream.skip(16 * (15 - numElements));
80 
81 	_sequenceLength = stream.readUint16LE();
82 
83 	_correctSequence.reserve(15);
84 	for (uint i = 0; i < 15; ++i) {
85 		_correctSequence.push_back(stream.readByte());
86 	}
87 
88 	_clickSound.read(stream, SoundDescription::kNormal);
89 	_solveExitScene.readData(stream);
90 	stream.skip(2); // shouldStopRendering, useless
91 	_flagOnSolve.label = stream.readSint16LE();
92 	_flagOnSolve.flag = (NancyFlag)stream.readByte();
93 	_solveSoundDelay = stream.readUint16LE();
94 	_solveSound.read(stream, SoundDescription::kNormal);
95 	_exitScene.readData(stream);
96 	stream.skip(2); // shouldStopRendering, useless
97 	_flagOnExit.label = stream.readSint16LE();
98 	_flagOnExit.flag = (NancyFlag)stream.readByte();
99 	readRect(stream, _exitHotspot);
100 }
101 
execute()102 void OrderingPuzzle::execute() {
103 	switch (_state) {
104 	case kBegin:
105 		init();
106 		registerGraphics();
107 		g_nancy->_sound->loadSound(_clickSound);
108 		g_nancy->_sound->loadSound(_solveSound);
109 		_state = kRun;
110 		// fall through
111 	case kRun:
112 		switch (_solveState) {
113 		case kNotSolved:
114 			if (_clickedSequence.size() != _sequenceLength) {
115 				return;
116 			}
117 
118 			for (uint i = 0; i < _sequenceLength; ++i) {
119 				if (_clickedSequence[i] != (int16)_correctSequence[i]) {
120 					return;
121 				}
122 			}
123 
124 			NancySceneState.setEventFlag(_flagOnSolve);
125 			_solveSoundPlayTime = g_nancy->getTotalPlayTime() + _solveSoundDelay * 1000;
126 			_solveState = kPlaySound;
127 			// fall through
128 		case kPlaySound:
129 			if (g_nancy->getTotalPlayTime() <= _solveSoundPlayTime) {
130 				break;
131 			}
132 
133 			g_nancy->_sound->playSound(_solveSound);
134 			_solveState = kWaitForSound;
135 			break;
136 		case kWaitForSound:
137 			if (!g_nancy->_sound->isSoundPlaying(_solveSound)) {
138 				_state = kActionTrigger;
139 			}
140 
141 			break;
142 		}
143 		break;
144 	case kActionTrigger:
145 		g_nancy->_sound->stopSound(_clickSound);
146 		g_nancy->_sound->stopSound(_solveSound);
147 
148 		if (_solveState == kNotSolved) {
149 			NancySceneState.changeScene(_exitScene);
150 			NancySceneState.setEventFlag(_flagOnExit);
151 		} else {
152 			NancySceneState.changeScene(_solveExitScene);
153 		}
154 
155 		finishExecution();
156 		break;
157 	}
158 }
159 
handleInput(NancyInput & input)160 void OrderingPuzzle::handleInput(NancyInput &input) {
161 	if (_solveState != kNotSolved) {
162 		return;
163 	}
164 
165 	if (NancySceneState.getViewport().convertViewportToScreen(_exitHotspot).contains(input.mousePos)) {
166 		g_nancy->_cursorManager->setCursorType(CursorManager::kExit);
167 
168 		if (input.input & NancyInput::kLeftMouseButtonUp) {
169 			_state = kActionTrigger;
170 		}
171 		return;
172 	}
173 
174 	for (int i = 0; i < (int)_destRects.size(); ++i) {
175 		if (NancySceneState.getViewport().convertViewportToScreen(_destRects[i]).contains(input.mousePos)) {
176 			g_nancy->_cursorManager->setCursorType(CursorManager::kHotspot);
177 
178 			if (input.input & NancyInput::kLeftMouseButtonUp) {
179 				g_nancy->_sound->playSound(_clickSound);
180 
181 				for (uint j = 0; j < _clickedSequence.size(); ++j) {
182 					if (_clickedSequence[j] == i && _drawnElements[i] == true) {
183 						undrawElement(i);
184 						if (_clickedSequence.back() == i) {
185 							_clickedSequence.pop_back();
186 						}
187 
188 						return;
189 					}
190 				}
191 
192 				_clickedSequence.push_back(i);
193 
194 				if (_clickedSequence.size() > (uint)_sequenceLength + 1) {
195 					clearAllElements();
196 				} else {
197 					drawElement(i);
198 				}
199 			}
200 			return;
201 		}
202 	}
203 }
204 
onPause(bool pause)205 void OrderingPuzzle::onPause(bool pause) {
206 	if (!pause) {
207 		registerGraphics();
208 	}
209 }
210 
drawElement(uint id)211 void OrderingPuzzle::drawElement(uint id) {
212 	_drawnElements[id] = true;
213 	Common::Point destPoint(_destRects[id].left - _screenPosition.left, _destRects[id].top - _screenPosition.top);
214 	_drawSurface.blitFrom(_image, _srcRects[id], destPoint);
215 	setVisible(true);
216 }
217 
undrawElement(uint id)218 void OrderingPuzzle::undrawElement(uint id) {
219 	_drawnElements[id] = false;
220 	Common::Rect bounds = _destRects[id];
221 	bounds.translate(-_screenPosition.left, -_screenPosition.top);
222 
223 	_drawSurface.fillRect(bounds, g_nancy->_graphicsManager->getTransColor());
224 	_needsRedraw = true;
225 }
226 
clearAllElements()227 void OrderingPuzzle::clearAllElements() {
228 	_drawSurface.clear(g_nancy->_graphicsManager->getTransColor());
229 	setVisible(false);
230 	_clickedSequence.clear();
231 	return;
232 }
233 
234 } // End of namespace Action
235 } // End of namespace Nancy
236