aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/speciallocks.cpp
blob: 6470e58641c053a7d3ba9497b467bb5826c62a10 (plain)
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
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

 /*
 * This code is based on Labyrinth of Time code with assistance of
 *
 * Copyright (c) 1993 Terra Nova Development
 * Copyright (c) 2004 The Wyrmkeep Entertainment Co.
 *
 */

#include "common/file.h"

#include "gui/message.h"

#include "lab/lab.h"
#include "lab/anim.h"
#include "lab/dispman.h"
#include "lab/image.h"
#include "lab/labsets.h"
#include "lab/resource.h"
#include "lab/speciallocks.h"
#include "lab/utils.h"

namespace Lab {

#define BRICKOPEN      115
#define COMBINATIONUNLOCKED  130

enum TileScroll {
	kScrollLeft = 1,
	kScrollRight = 2,
	kScrollUp = 3,
	kScrollDown = 4
};

const uint16 INIT_TILE[4][4] = {
	{ 1, 5, 9, 13 },
	{ 2, 6, 10, 14 },
	{ 3, 7, 11, 15 },
	{ 4, 8, 12, 0 }
};

const uint16 SOLUTION[4][4] = {
	{ 7, 1, 8, 3 },
	{ 2, 11, 15, 4 },
	{ 9, 5, 14, 6 },
	{ 10, 13, 12, 0 }
};

const int COMBINATION_X[6] = { 45, 83, 129, 166, 211, 248 };

SpecialLocks::SpecialLocks(LabEngine *vm) : _vm(vm) {
	for (int i = 0; i < 16; i++)
		_tiles[i] = nullptr;

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++)
			_curTile[i][j] = INIT_TILE[i][j];
	}

	for (int i = 0; i < 6; i++)
		_combination[i] = 0;

	for (int i = 0; i < 10; i++)
		_numberImages[i] = nullptr;
}

SpecialLocks::~SpecialLocks() {
	for (int i = 0; i < 16; i++)
		delete _tiles[i];

	for (int imgIdx = 0; imgIdx < 10; imgIdx++) {
		delete _numberImages[imgIdx];
		_numberImages[imgIdx] = nullptr;
	}
}

void SpecialLocks::tileClick(Common::Point pos) {
	Common::Point realPos = _vm->_utils->vgaUnscale(pos);

	if ((realPos.x < 101) || (realPos.y < 26))
		return;

	int tileX = (realPos.x - 101) / 30;
	int tileY = (realPos.y -  26) / 25;

	if ((tileX < 4) && (tileY < 4))
		changeTile(tileX, tileY);
}

void SpecialLocks::changeTile(uint16 col, uint16 row) {
	int16 scrolltype = -1;

	if (row > 0) {
		if (_curTile[col][row - 1] == 0) {
			_curTile[col][row - 1] = _curTile[col][row];
			_curTile[col][row] = 0;
			scrolltype = kScrollDown;
		}
	}

	if (col > 0) {
		if (_curTile[col - 1][row] == 0) {
			_curTile[col - 1][row] = _curTile[col][row];
			_curTile[col][row] = 0;
			scrolltype = kScrollRight;
		}
	}

	if (row < 3) {
		if (_curTile[col][row + 1] == 0) {
			_curTile[col][row + 1] = _curTile[col][row];
			_curTile[col][row] = 0;
			scrolltype = kScrollUp;
		}
	}

	if (col < 3) {
		if (_curTile[col + 1][row] == 0) {
			_curTile[col + 1][row] = _curTile[col][row];
			_curTile[col][row] = 0;
			scrolltype = kScrollLeft;
		}
	}

	if (scrolltype != -1) {
		if (_vm->getFeatures() & GF_WINDOWS_TRIAL) {
			GUI::MessageDialog trialMessage("This puzzle is not available in the trial version of the game");
			trialMessage.runModal();
			return;
		}

		doTileScroll(col, row, scrolltype);
		bool check = true;
		row = 0;
		col = 0;

		while (row < 4) {
			while (col < 4) {
				check &= (_curTile[row][col] == SOLUTION[row][col]);
				col++;
			}

			row++;
			col = 0;
		}

		if (check) {
			// unlocked combination
			_vm->_conditions->inclElement(BRICKOPEN);
			_vm->_anim->_doBlack = true;
			_vm->_graphics->readPict("p:Up/BDOpen");
		}
	}
}

void SpecialLocks::combinationClick(Common::Point pos) {
	Common::Point realPos = _vm->_utils->vgaUnscale(pos);

	if (!Common::Rect(44, 63, 285, 99).contains(realPos))
		return;

	uint16 number = 0;
	if (realPos.x < 83)
		number = 0;
	else if (realPos.x < 127)
		number = 1;
	else if (realPos.x < 165)
		number = 2;
	else if (realPos.x < 210)
		number = 3;
	else if (realPos.x < 245)
		number = 4;
	else if (realPos.x < 286)
		number = 5;

	changeCombination(number);
}

void SpecialLocks::doTile(bool showsolution) {
	uint16 row = 0, col = 0, rowm, colm, num;
	int16 rows, cols;

	if (showsolution) {
		rowm = _vm->_utils->vgaScaleY(23);
		colm = _vm->_utils->vgaScaleX(27);

		rows = _vm->_utils->vgaScaleY(31);
		cols = _vm->_utils->vgaScaleX(105);
	} else {
		_vm->_graphics->rectFillScaled(97, 22, 220, 126, 0);

		rowm = _vm->_utils->vgaScaleY(25);
		colm = _vm->_utils->vgaScaleX(30);

		rows = _vm->_utils->vgaScaleY(25);
		cols = _vm->_utils->vgaScaleX(100);
	}

	while (row < 4) {
		while (col < 4) {
			if (showsolution)
				num = SOLUTION[col][row];
			else
				num = _curTile[col][row];

			if (showsolution || num)
				_tiles[num]->drawImage(cols + (col * colm), rows + (row * rowm));

			col++;
		}

		row++;
		col = 0;
	}
}

void SpecialLocks::showTileLock(const Common::String filename, bool showSolution) {
	_vm->_anim->_doBlack = true;
	_vm->_anim->_noPalChange = true;
	_vm->_graphics->readPict(filename);
	_vm->_anim->_noPalChange = false;
	_vm->_graphics->blackScreen();

	Common::File *tileFile = _vm->_resource->openDataFile(showSolution ? "P:TileSolution" : "P:Tile");

	int start = showSolution ? 0 : 1;

	for (int curBit = start; curBit < 16; curBit++)
		_tiles[curBit] = new Image(tileFile, _vm);

	delete tileFile;

	doTile(showSolution);
	_vm->_graphics->setPalette(_vm->_anim->_diffPalette, 256);
}

void SpecialLocks::doTileScroll(uint16 col, uint16 row, uint16 scrolltype) {
	int16 dX = 0, dY = 0, dx = 0, dy = 0, sx = 0, sy = 0;
	int last = 0;

	if (scrolltype == kScrollLeft) {
		dX = _vm->_utils->vgaScaleX(5);
		sx = _vm->_utils->vgaScaleX(5);
		last = 6;
	} else if (scrolltype == kScrollRight) {
		dX = _vm->_utils->vgaScaleX(-5);
		dx = _vm->_utils->vgaScaleX(-5);
		sx = _vm->_utils->vgaScaleX(5);
		last = 6;
	} else if (scrolltype == kScrollUp) {
		dY = _vm->_utils->vgaScaleY(5);
		sy = _vm->_utils->vgaScaleY(5);
		last = 5;
	} else if (scrolltype == kScrollDown) {
		dY = _vm->_utils->vgaScaleY(-5);
		dy = _vm->_utils->vgaScaleY(-5);
		sy = _vm->_utils->vgaScaleY(5);
		last = 5;
	}

	sx += _vm->_utils->svgaCord(2);

	uint16 x1 = _vm->_utils->vgaScaleX(100) + (col * _vm->_utils->vgaScaleX(30)) + dx;
	uint16 y1 = _vm->_utils->vgaScaleY(25) + (row * _vm->_utils->vgaScaleY(25)) + dy;

	byte *buffer = new byte[_tiles[1]->_width * _tiles[1]->_height * 2];

	for (int i = 0; i < last; i++) {
		_vm->waitTOF();
		scrollRaster(dX, dY, x1, y1, x1 + _vm->_utils->vgaScaleX(28) + sx, y1 + _vm->_utils->vgaScaleY(23) + sy, buffer);
		x1 += dX;
		y1 += dY;
	}

	delete[] buffer;
}

void SpecialLocks::scrollRaster(int16 dx, int16 dy, uint16 x1, uint16 y1, uint16 x2, uint16 y2, byte *buffer) {
	if (dx)
		_vm->_graphics->scrollDisplayX(dx, x1, y1, x2, y2, buffer);

	if (dy)
		_vm->_graphics->scrollDisplayY(dy, x1, y1, x2, y2, buffer);
}

void SpecialLocks::changeCombination(uint16 number) {
	const int solution[6] = { 0, 4, 0, 8, 7, 2 };

	Image display(_vm);

	if (_combination[number] < 9)
		(_combination[number])++;
	else
		_combination[number] = 0;

	uint16 combnum = _combination[number];

	display.setData(_vm->_graphics->getCurrentDrawingBuffer(), false);
	display._width = _vm->_graphics->_screenWidth;
	display._height = _vm->_graphics->_screenHeight;

	byte *buffer = new byte[_numberImages[1]->_width * _numberImages[1]->_height * 2];

	for (int i = 1; i <= (_numberImages[combnum]->_height / 2); i++) {
		if (i & 1)
			_vm->waitTOF();

		display.setData(_vm->_graphics->getCurrentDrawingBuffer(), false);
		_vm->_graphics->scrollDisplayY(2, _vm->_utils->vgaScaleX(COMBINATION_X[number]), _vm->_utils->vgaScaleY(65), _vm->_utils->vgaScaleX(COMBINATION_X[number]) + (_numberImages[combnum])->_width - 1, _vm->_utils->vgaScaleY(65) + (_numberImages[combnum])->_height, buffer);
		_numberImages[combnum]->blitBitmap(0, (_numberImages[combnum])->_height - (2 * i), &(display), _vm->_utils->vgaScaleX(COMBINATION_X[number]), _vm->_utils->vgaScaleY(65), (_numberImages[combnum])->_width, 2, false);
	}

	delete[] buffer;

	bool unlocked = true;
	for (int i = 0; i < 6; i++)
		unlocked &= (_combination[i] == solution[i]);

	if (unlocked)
		_vm->_conditions->inclElement(COMBINATIONUNLOCKED);
	else
		_vm->_conditions->exclElement(COMBINATIONUNLOCKED);
}

void SpecialLocks::showCombinationLock(const Common::String filename) {
	_vm->_anim->_doBlack = true;
	_vm->_anim->_noPalChange = true;
	_vm->_graphics->readPict(filename);
	_vm->_anim->_noPalChange = false;

	_vm->_graphics->blackScreen();

	Common::File *numFile = _vm->_resource->openDataFile("P:Numbers");

	for (int i = 0; i < 10; i++) {
		_numberImages[i] = new Image(numFile, _vm);
	}

	delete numFile;

	for (int i = 0; i <= 5; i++)
		_numberImages[_combination[i]]->drawImage(_vm->_utils->vgaScaleX(COMBINATION_X[i]), _vm->_utils->vgaScaleY(65));

	_vm->_graphics->setPalette(_vm->_anim->_diffPalette, 256);
}

void SpecialLocks::save(Common::OutSaveFile *file) {
	// Combination lock
	for (int i = 0; i < 6; i++)
		file->writeByte(_combination[i]);

	// Tiles
	for (int i = 0; i < 4; i++)
		for (int j = 0; j < 4; j++)
			file->writeUint16LE(_curTile[i][j]);
}

void SpecialLocks::load(Common::InSaveFile *file) {
	// Combination lock
	for (int i = 0; i < 6; i++)
		_combination[i] = file->readByte();

	// Tiles
	for (int i = 0; i < 4; i++)
		for (int j = 0; j < 4; j++)
			_curTile[i][j] = file->readUint16LE();
}

} // End of namespace Lab