File: puzzle_all.h

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (351 lines) | stat: -rw-r--r-- 9,946 bytes parent folder | download | duplicates (2)
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
/* 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/>.
 *
 */

#ifndef QDENGINE_MINIGAMES_PUZZLE_ALL_H
#define QDENGINE_MINIGAMES_PUZZLE_ALL_H

#include "common/debug.h"

#include "qdengine/qd_fwd.h"
#include "qdengine/qdcore/qd_minigame_interface.h"

namespace QDEngine {

const int puzzle_ep01[] = {
	220, 193, 340, 186, 464, 193, 577, 186, 227, 306,
	340, 306, 465, 305, 585, 306, 220, 419, 340, 426,
	464, 418, 577, 426
};

const int puzzle_ep02[] = {
	218, 197, 335, 194, 465, 178, 579, 181, 220, 305,
	347, 304, 473, 300, 582, 298, 222, 410, 342, 416,
	469, 426, 579, 427
};

const int puzzle_ep04[] = {
	205, 224, 298, 224, 404, 195, 503, 189, 601, 204,
	218, 319, 306, 308, 416, 287, 504, 263, 587, 285,
	218, 400, 313, 408, 418, 386, 504, 387, 588, 384
};

const int puzzle_ep05[] = {
	205, 223, 297, 223, 404, 194, 503, 189, 601, 203,
	217, 316, 305, 306, 415, 287, 503, 262, 586, 284,
	217, 397, 312, 405, 417, 384, 503, 384, 587, 382
};

const int puzzle_ep07[] = {
	199, 188, 276, 185, 362, 177, 444, 172, 531, 185,
	601, 183, 202, 263, 282, 258, 362, 245, 440, 248,
	524, 254, 598, 265, 200, 342, 282, 341, 357, 342,
	439, 341, 519, 344, 595, 340, 203, 423, 276, 420,
	359, 425, 441, 421, 525, 419, 602, 414
};

const int puzzle_ep08[] = {
	217, 188, 337, 181, 462, 188, 575, 181, 224, 302,
	338, 301, 462, 301, 582, 301, 217, 415, 337, 422,
	462, 414, 575, 421
};

const int puzzle_ep13[] = {
	196, 194, 273, 194, 362, 169, 445, 164, 538, 188,
	606, 188, 197, 266, 283, 255, 365, 231, 438, 235,
	533, 246, 593, 271, 197, 345, 283, 341, 354, 344,
	437, 341, 516, 348, 592, 340, 197, 425, 270, 424,
	357, 430, 440, 427, 526, 416, 601, 412
};

class qdPuzzleAllMiniGame : public qdMiniGameInterface {
public:
	qdPuzzleAllMiniGame(Common::String dll, Common::Language language) : _dll(dll), _language(language) {
		for (uint i = 0; i < ARRAYSIZE(_pieces); i++)
			_pieces[i] = nullptr;
	}
	~qdPuzzleAllMiniGame() {};

	bool init(const qdEngineInterface *engine_interface) {
		debugC(1, kDebugMinigames, "PuzzleAll::init(), dll: %s lang: %d", _dll.c_str(), _language);

		_engine = engine_interface;
		_scene = _engine->current_scene_interface();
		if (!_scene)
			return 0;

		if (_dll == "DLL\\Puzzle_ep01.dll") { // worm
			_numPieces = 12;
			_pieceCoords = puzzle_ep01;
		} else if (_dll == "DLL\\Puzzle_ep02.dll") { // mushrooms
			_numPieces = 12;
			_pieceCoords = puzzle_ep02;
		} else if (_dll == "DLL\\Puzzle_ep04.dll") { // bird
			_numPieces = 15;
			_pieceCoords = puzzle_ep04;
		} else if (_dll == "DLL\\Puzzle_ep05.dll") { // forest
			_numPieces = 15;
			_pieceCoords = puzzle_ep05;
		} else if (_dll == "DLL\\Puzzle_ep07.dll") { // bears
			_numPieces = 24;
			_pieceCoords = puzzle_ep07;
		} else if (_dll == "DLL\\Puzzle_ep08.dll") { // house
			_numPieces = 12;
			_pieceCoords = puzzle_ep08;
		} else if (_dll == "DLL\\Puzzle_ep13.dll") { // harbor
			_numPieces = 24;
			_pieceCoords = puzzle_ep13;
		}

		for (int i = 0; i < _numPieces; i++)
			_pieces[i] = _scene->object_interface(Common::String::format("\xee\xe1\xfa\xe5\xea\xf2_%i", i + 1).c_str());	// "объект_%i"

		_objFinal = _scene->object_interface("$\xf4\xe8\xed\xe0\xeb"); // "$финал"
		_objRan = _scene->object_interface("$\xe7\xe0\xef\xf3\xf1\xf2\xe8\xeb\xe8");	// "$запустили""
		_wasInited = false;

		if (_objFinal->is_state_active("\xed\xe5\xf2")) { // "нет"
			_isFinal = false;

			_minDepthPiece = findMinDepthPiece();
			return true;
		} else {
			if (_objFinal->is_state_active("\xe4\xe0"))	// "да"
				_isFinal = true;

			_minDepthPiece = findMinDepthPiece();
			return true;
		}

		return true;
	}

	bool quant(float dt) {
		debugC(3, kDebugMinigames, "PuzzleAll::quant(%f)", dt);

		if (!_wasInited && _objRan->is_state_active("\xed\xe5\xf2")) {	// "нет"
			_rotatingPiece = -1;
			_currentPieceState = -1;
			_pieceIsPut = false;
			_isFinal = false;
			_wasInited = 1;
			_objRan->set_state("\xe4\xe0");	// "да"
		}

		if (_isFinal)
			return true;

		if (!checkSolution() || _scene->mouse_object_interface()) {
			qdMinigameObjectInterface *mouseObj = _scene->mouse_object_interface();

			if (_pieceIsPut) {
				for (int i = 0; i < _numPieces; i++) {
					if (_pieces[i]->is_state_active("to_inv_flag_0")
							|| _pieces[i]->is_state_active("to_inv_flag_90")
							|| _pieces[i]->is_state_active("to_inv_flag_180")
							|| _pieces[i]->is_state_active("to_inv_flag_270")) {

						if (_pieces[i]->is_state_active("to_inv_flag_0"))
							_currentPieceState = _pieces[i]->state_index("inv_0");
						else if (_pieces[i]->is_state_active("to_inv_flag_90"))
							_currentPieceState = _pieces[i]->state_index("inv_90");
						else if (_pieces[i]->is_state_active("to_inv_flag_180"))
							_currentPieceState = _pieces[i]->state_index("inv_180");
						else if (_pieces[i]->is_state_active("to_inv_flag_270"))
							_currentPieceState = _pieces[i]->state_index("inv_270");

						_rotatingPiece = i;
						_pieces[i]->set_state("to_inv");
						_pieceIsPut = false;
					}
				}
			}

			if (mouseObj) {
				if (_rotatingPiece != -1) {
					_pieces[_rotatingPiece]->set_state(_currentPieceState);
					_currentPieceState = -1;
					_rotatingPiece = -1;
				}
			}

			if (_engine->is_mouse_event_active(qdmg::qdEngineInterfaceImpl::MOUSE_EV_LEFT_DOWN)) {
				mgVect2i mousePos = _engine->mouse_cursor_position();
				qdMinigameObjectInterface *obj = _scene->mouse_object_interface();

				if (obj) {
					if (obj->is_state_active("inv_0"))
						obj->set_state("0");
					else if (obj->is_state_active("inv_90"))
						obj->set_state("90");
					else if (obj->is_state_active("inv_180"))
						obj->set_state("180");
					else if (obj->is_state_active("inv_270"))
						obj->set_state("270");

					_minDepthPiece -= 250.0;

					mgVect3f coords = _scene->screen2world_coords(mousePos, _minDepthPiece);
					obj->set_R(coords);

					snapPieces();
				} else {
					_pieceIsPut = true;
				}
			}
		}

		if (_engine->is_mouse_event_active(qdmg::qdEngineInterfaceImpl::MOUSE_EV_RIGHT_DOWN)) {
			qdMinigameObjectInterface *obj = _scene->mouse_object_interface();

			if (obj) {
				if (obj->is_state_active("inv_0"))
					obj->set_state("inv_270");
				else if (obj->is_state_active("inv_90"))
					obj->set_state("inv_0");
				else if (obj->is_state_active("inv_180"))
					obj->set_state("inv_90");
				else if (obj->is_state_active("inv_270"))
					obj->set_state("inv_180");
			}
		}

		return true;
	}

	bool checkSolution() {
		if (_scene->mouse_object_interface())
			return false;

		for (int i = 0; i < _numPieces; i++)
			if (!_pieces[0]->is_state_active("0"))
				return false;

		mgVect2i piecePos;

		for (int i = 0; i < _numPieces; i++) {
			piecePos = _pieces[i]->screen_R();

			if (ABS(_pieceCoords[i * 2 + 0] - piecePos.x) > 10 ||
				ABS(_pieceCoords[i * 2 + 1] - piecePos.y) > 10)
				return false;
		}

		_isFinal = true;
		_objFinal->set_state("\xe4\xe0");	// "да"

		_minDepthPiece -= 250.0;

		mgVect3f coords = _scene->screen2world_coords(_objFinal->screen_R(), _minDepthPiece);
		_objFinal->set_R(coords);

		return true;
	}

	void snapPieces() {
		mgVect2i piecePos;
		mgVect3f newPiecePos;

		for (int i = 0; i < _numPieces; i++) {
			piecePos = _pieces[i]->screen_R();
			float depth = _scene->screen_depth(_pieces[i]->R());

			if (_pieces[i]->is_state_active("0")) {
				if (ABS(_pieceCoords[i * 2 + 0] - piecePos.x) <= 10 &&
						ABS(_pieceCoords[i * 2 + 1] - piecePos.y) <= 10) {
					piecePos.x = _pieceCoords[i * 2 + 0];
					piecePos.y = _pieceCoords[i * 2 + 1];

					newPiecePos = _scene->screen2world_coords(piecePos, depth);
					_pieces[i]->set_R(newPiecePos);
				}
			}
		}
	}

	bool finit() {
		debugC(1, kDebugMinigames, "PuzzleAll::finit()");

		if (_scene)  {
			_engine->release_scene_interface(_scene);
			_scene = 0;
		}

 		return true;
	}

	bool new_game(const qdEngineInterface *engine_interface) {
		return true;
	}

	int save_game(const qdEngineInterface *engine_interface, const qdMinigameSceneInterface *scene_interface, char *buffer, int buffer_size) {
		return 0;
	}

	int load_game(const qdEngineInterface *engine_interface, const qdMinigameSceneInterface *scene_interface, const char *buffer, int buffer_size) {
		return 0;
	}

	enum { INTERFACE_VERSION = 112 };
	int version() const {
		return INTERFACE_VERSION;
	}

private:
	float findMinDepthPiece() {
		float min = 100000.0;

		for (int i = 0; i < _numPieces; i++) {
			float depth = _scene->screen_depth(_pieces[i]->R());

			if (min > depth)
				min = depth;
		}

		return min;
	}

private:
	const qdEngineInterface *_engine = nullptr;
	qdMinigameSceneInterface *_scene = nullptr;

	qdMinigameObjectInterface *_pieces[24];
	const int *_pieceCoords = nullptr;

	qdMinigameObjectInterface *_objFinal = nullptr;
	qdMinigameObjectInterface *_objRan = nullptr;

	bool _wasInited = false;
	bool _isFinal = false;

	float _minDepthPiece = 0.0;
	int _rotatingPiece = -1;
	bool _pieceIsPut = true;
	int _currentPieceState = 0;

	int _numPieces = 12;

	Common::String _dll;
	Common::Language _language;
};

} // namespace QDEngine

#endif // QDENGINE_MINIGAMES_PUZZLE_ALL_H