aboutsummaryrefslogtreecommitdiff
path: root/engines/scumm/he/moonbase/moonbase_fow.cpp
blob: 48c2219926bff53613a760a7246a2e884c5f40e3 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* 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.
 *
 */

#include "common/config-manager.h"

#include "scumm/he/intern_he.h"
#include "scumm/he/moonbase/moonbase.h"

namespace Scumm {

#define FOW_ANIM_FRAME_COUNT	38

void Moonbase::initFOW() {
	_fowSentinelImage = -1;
	_fowSentinelState = -1;
	_fowSentinelConditionBits = 0;

	_fowFrameBaseNumber = 0;
	_fowAnimationFrames = 1;
	_fowCurrentFOWFrame = 0;

	_fowTileW = 0;
	_fowTileH = 0;

	_fowImage = nullptr;
	_fowClipX1 = 0;
	_fowClipY1 = 0;
	_fowClipX2 = 0;
	_fowClipY2 = 0;

	_fowDrawX = 0;
	_fowDrawY = 0;

	_fowVtx1 = 0;
	_fowVty1 = 0;
	_fowMvx = 0;
	_fowMvy = 0;
	_fowVw = 0;
	_fowVh = 0;

	_fowBlackMode = true;

	memset(_fowRenderTable, 0, 32768);
}

void Moonbase::releaseFOWResources() {
	if (_fowImage) {
		free(_fowImage);
		_fowImage = 0;
	}
}

bool Moonbase::setFOWImage(int image) {
	releaseFOWResources();

	if (!_fowImage) {
		Common::String fowImageFilename(ConfMan.get("MOONX_FOWImageFilename").c_str());

#if 0 // TODO
		if (!fowImageFilename.empty()) {
			void *wiz = loadWizFromFilename(fowImageFilename);

			if (wiz) {
				captureFOWImageFromLocation(wiz, file.size());
				free(wiz);
			}
		}
#endif

		if (!_fowImage && image < 0) {
			int resId;

			// PIECES  BUBBLES CIRCLES SIMPLE*  WEDGEY BUBBLE2
			// WEDGE2  SPIKEY  ANGLES  SMOOTHED WUZZY  SYS7-BEVELED
			if (image >= -12 && image <= -1)
				resId = 210 - image; // 211-222 range
			else
				resId = 214; // default, SIMPLE

			if (_fileName.empty()) { // We are running for the first time
				_fileName = _vm->generateFilename(-3);

				if (!_exe.loadFromEXE(_fileName))
					error("Cannot open file %s", _fileName.c_str());
			}

			Common::SeekableReadStream *stream = _exe.getResource(Common::kPERCData, resId);

			if (stream->size()) {
				_fowImage = (uint8 *)malloc(stream->size());

				stream->read(_fowImage, stream->size());
			}

			delete stream;
		}

		if (!_fowImage && image > 0)
			_fowImage = _vm->getResourceAddress(rtImage, image);

		if (!_fowImage)
			return false;
	}

	int nStates = _vm->_wiz->getWizImageStates(_fowImage);

	if (nStates > FOW_ANIM_FRAME_COUNT) {
		releaseFOWResources();
		return false;
	}

	_fowAnimationFrames = (nStates + FOW_ANIM_FRAME_COUNT - 1) / FOW_ANIM_FRAME_COUNT;

	_vm->_wiz->getWizImageDim(_fowImage, (nStates - 1), _fowTileW, _fowTileH);
	_fowBlackMode = !_vm->_wiz->isWizPixelNonTransparent(_fowImage, nStates - 1, 0, 0, 0);

	if (ConfMan.hasKey("EnableFOWRects"))
		_fowBlackMode = (ConfMan.getInt("EnableFOWRects") == 1);

	return true;
}

enum FOWElement {
	FOW_EMPTY	= 0,
	FOW_SOLID	= 1,

	FF_L		= 0x01,
	FF_R		= 0x02,
	FF_T		= 0x04,
	FF_B		= 0x08,
	FF_T_L		= 0x10,
	FF_T_R		= 0x20,
	FF_B_L		= 0x40,
	FF_B_R		= 0x80,
	FF_Q_A		= (FF_L | FF_T | FF_T_L),
	FF_Q_B		= (FF_R | FF_T | FF_T_R),
	FF_Q_C		= (FF_L | FF_B | FF_B_L),
	FF_Q_D		= (FF_R | FF_B | FF_B_R)
};

int Moonbase::readFOWVisibilityArray(int array, int y, int x) {
	if (readFromArray(array, y, x) > 0)
		return FOW_EMPTY;

	return FOW_SOLID;
}

void Moonbase::setFOWInfo(int fowInfoArray, int downDim, int acrossDim, int viewX, int viewY, int clipX1,
				int clipY1, int clipX2, int clipY2, int technique, int nFrame) {
	if (!_fowImage)
		return;

	_fowDrawX = clipX1;
	_fowDrawY = clipY1;

	_fowClipX1 = clipX1;
	_fowClipY1 = clipY1;
	_fowClipX2 = clipX2;
	_fowClipY2 = clipY2;

	// Figure out the number of tiles are involved
	int view_W = (clipX2 - clipX1) + 1;
	int view_H = (clipY2 - clipY1) + 1;

	int tw = _fowTileW;
	int th = _fowTileH;

	int dw = acrossDim;
	int dh = downDim;

	int dlw = dw * tw;
	int dlh = dh * th;

	_fowMvx = (0 <= viewX) ? (viewX % dlw) : (dlw - (-viewX % dlw));
	_fowMvy = (0 <= viewY) ? (viewY % dlh) : (dlh - (-viewY % dlh));

	_fowVtx1 = _fowMvx / tw;
	_fowVty1 = _fowMvy / th;

	_fowVw = (((_fowMvx + view_W + tw - 1) / tw) - _fowVtx1) + 1;
	_fowVh = (((_fowMvy + view_H + th - 1) / th) - _fowVty1) + 1;

	// Build the connectivity table
	int t = (_fowVty1 - 1); if (t >= dh) { t = 0; } else if (t < 0) { t = (dh - 1); }
	int m = (_fowVty1 + 0); if (m >= dh) { m = 0; } else if (m < 0) { m = (dh - 1); }
	int b = (_fowVty1 + 1); if (b >= dh) { b = 0; } else if (b < 0) { b = (dh - 1); }

	int il = (_fowVtx1 - 1); if (il >= dh) { il = 0; } else if (il < 0) { il = (dw - 1); }
	int ic = (_fowVtx1 + 0); if (ic >= dh) { ic = 0; } else if (ic < 0) { ic = (dw - 1); }
	int ir = (_fowVtx1 + 1); if (ir >= dh) { ir = 0; } else if (ir < 0) { ir = (dw - 1); }

	int dataOffset = (_fowVw * 3);
	int dataOffset2 = (dataOffset * 2);
	int *pOutterRenderTableA = _fowRenderTable;
	int *pOutterRenderTableB = pOutterRenderTableA + dataOffset;

	for (int ay = 0; ay < _fowVh; ay++) {
		int l = il;
		int c = ic;
		int r = ir;

		int *pRenderTableA = pOutterRenderTableA;
		int *pRenderTableB = pOutterRenderTableB;

		pOutterRenderTableA += dataOffset2;
		pOutterRenderTableB += dataOffset2;

		for (int ax = 0; ax < _fowVw; ax++) {
			int visibility = readFOWVisibilityArray(fowInfoArray, m, c);

			if (visibility == FOW_EMPTY) {
				int bits = 0;

				if (readFOWVisibilityArray(fowInfoArray, t, l) != 0) bits |= FF_T_L;
				if (readFOWVisibilityArray(fowInfoArray, t, c) != 0) bits |= FF_T;
				if (readFOWVisibilityArray(fowInfoArray, t, r) != 0) bits |= FF_T_R;
				if (readFOWVisibilityArray(fowInfoArray, m, l) != 0) bits |= FF_L;
				if (readFOWVisibilityArray(fowInfoArray, m, r) != 0) bits |= FF_R;
				if (readFOWVisibilityArray(fowInfoArray, b, l) != 0) bits |= FF_B_L;
				if (readFOWVisibilityArray(fowInfoArray, b, c) != 0) bits |= FF_B;
				if (readFOWVisibilityArray(fowInfoArray, b, r) != 0) bits |= FF_B_R;

				if (bits) {
					*pRenderTableA++ = 1;
					*pRenderTableB++ = 1;

					// Quadrant (A)
					if (bits & FF_Q_A) {
						*pRenderTableA++ = (
							((FF_L   & bits) ? 1 : 0) |
							((FF_T   & bits) ? 2 : 0) |
							((FF_T_L & bits) ? 4 : 0)
						) + 0;
					} else {
						*pRenderTableA++ = 0;
					}

					// Quadrant (B)
					if (bits & FF_Q_B) {
						*pRenderTableA++ = (
							((FF_R   & bits) ? 1 : 0) |
							((FF_T   & bits) ? 2 : 0) |
							((FF_T_R & bits) ? 4 : 0)
						) + 8;
					} else {
						*pRenderTableA++ = 0;
					}

					// Quadrant (C)
					if (bits & FF_Q_C) {
						*pRenderTableB++ = (
							((FF_L   & bits) ? 1 : 0) |
							((FF_B   & bits) ? 2 : 0) |
							((FF_B_L & bits) ? 4 : 0)
						) + 16;
					} else {
						*pRenderTableB++ = 0;
					}

					// Quadrant (D)
					if (bits & FF_Q_D) {
						*pRenderTableB++ = (
							((FF_R   & bits) ? 1 : 0) |
							((FF_B   & bits) ? 2 : 0) |
							((FF_B_R & bits) ? 4 : 0)
						) + 24;
					} else {
						*pRenderTableB++ = 0;
					}
				} else {
					*pRenderTableA++ = 0;
					*pRenderTableB++ = 0;
				}
			} else {
				if (_fowBlackMode) {
					*pRenderTableA++ = 2;
					*pRenderTableB++ = 2;
				} else {
					*pRenderTableA++ = 1;
					*pRenderTableA++ = 33;
					*pRenderTableA++ = 34;

					*pRenderTableB++ = 1;
					*pRenderTableB++ = 35;
					*pRenderTableB++ = 36;
				}
			}

			if (++l >= dw) { l = 0; }
			if (++c >= dw) { c = 0; }
			if (++r >= dw) { r = 0; }
		}

		if (++t >= dh) { t = 0; }
		if (++m >= dh) { m = 0; }
		if (++b >= dh) { b = 0; }
	}

	_fowCurrentFOWFrame = (nFrame >= 0) ? (nFrame % _fowAnimationFrames) : ((-nFrame) % _fowAnimationFrames);
	_fowFrameBaseNumber = (_fowCurrentFOWFrame * FOW_ANIM_FRAME_COUNT);
}

void Moonbase::renderFOWState(uint8 *destSurface, int dstPitch, int dstType, int dstw, int dsth, int x, int y, int srcw, int srch, int state, int flags) {
	int32 spotx, spoty;

	_vm->_wiz->getWizImageSpot(_fowImage, state, spotx, spoty);
	Common::Rect r(_fowClipX1, _fowClipY1, _fowClipX2, _fowClipY2);

	_vm->_wiz->drawWizImageEx(destSurface, _fowImage, 0, dstPitch, dstType, dstw, dsth, x - spotx, y - spoty, srcw, srch, state, &r, flags, 0, 0, 16, 0, 0);
}

static void blackRect_16bpp(uint8 *destSurface, int dstPitch, int dstw, int dsth, int x1, int y1, int x2, int y2) {
	byte *dst = destSurface + dstPitch * y1 + x1 * 2;
	int h = y2 - y1;
	int w = ((x2 - x1) + 1) * 2;

	while ( --h >= 0 ) {
		memset(dst, 0, w);
		dst += dstPitch;
	}
}

void Moonbase::renderFOW(uint8 *destSurface, int dstPitch, int dstType, int dstw, int dsth, int flags) {
	if (!_fowImage)
		return;

	const int *pOutterRenderTable = _fowRenderTable;
	int ixPos = ((_fowVtx1 * _fowTileW) - _fowMvx) + _fowDrawX;
	int yPos = ((_fowVty1 * _fowTileH) - _fowMvy) + _fowDrawY;
	int dataOffset = _fowVw * 3;
	int halfTileHeight = _fowTileH / 2;
	int cx2 = MIN(_fowClipX2, (dstw - 1));
	int cy2 = MIN(_fowClipY2, (dsth - 1));

	for (int ry = 0; ry < _fowVh; ry++) {
		int real_yPos = yPos;

		for (int i = 0; i < 2; i++) {
			const int *pRenderTable = pOutterRenderTable;
			pOutterRenderTable += dataOffset;

			int xPos = ixPos;

			for (int rx = 0; rx < _fowVw; rx++) {
				int nState = *pRenderTable++;

				if (nState != 0) {
					if (nState == 2) {
						int countLeft = (_fowVw - rx);
						int count = 0;

						for (; count < countLeft; count++) {
							if (*(pRenderTable + count) != 2)
								break;

							pRenderTable++;
							rx++;
						}
						count++;

						int x1 = xPos;
						int y1 = real_yPos;

						xPos += _fowTileW * count;
						int x2 = (xPos - 1);
						int y2 = ((y1 + halfTileHeight) - 1);

						x1 = MAX(0, x1);
						y1 = MAX(0, y1);
						x2 = MIN(x2, cx2);
						y2 = MIN(y2, cy2);

						if ((x2 >= x1) && (y2 >= y1) && (x1 <= _fowClipX2) && (y1 <= _fowClipY2))
							blackRect_16bpp(destSurface, dstPitch, dstw, dsth, x1, y1, x2, y2);
					} else {
						int subState;

						if ((subState = *pRenderTable++) != 0)
							renderFOWState(destSurface, dstPitch, dstType, dstw, dsth, xPos, yPos, _fowTileW, _fowTileH, (subState + _fowFrameBaseNumber), flags);

						if ((subState = *pRenderTable++) != 0)
							renderFOWState(destSurface, dstPitch, dstType, dstw, dsth, xPos, yPos, _fowTileW, _fowTileH, (subState + _fowFrameBaseNumber), flags);

						xPos += _fowTileW;
					}
				} else {
					xPos += _fowTileW;
				}
			}
			real_yPos += halfTileHeight;
		}
		yPos += _fowTileH;
	}
}

} // End of namespace Scumm