aboutsummaryrefslogtreecommitdiff
path: root/engines/parallaction/inventory.cpp
blob: c9e74b20741b89570f04bf93831f48ae5d9c5567 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stdafx.h"

#include "parallaction/parallaction.h"



namespace Parallaction {

//
//	inventory is a grid made of (at most) 30 cells, 24x24 pixels each,
//	arranged in 6 lines
//
//	inventory items are stored in cnv files in a 32x24 grid
//	but only 24x24 pixels are actually copied to graphic memory
//

#define INVENTORY_MAX_ITEMS 		30
#define INVENTORY_FIRST_ITEM		4		// first four entries are used up by verbs

#define INVENTORY_ITEMS_PER_LINE	5
#define INVENTORY_LINES 			6

#define INVENTORY_WIDTH 			(INVENTORY_ITEMS_PER_LINE*INVENTORYITEM_WIDTH)
#define INVENTORY_HEIGHT			(INVENTORY_LINES*INVENTORYITEM_HEIGHT)

static byte		*_buffer;
uint16			 _numInvLines = 0;
static Common::Point	 _invPosition;

InventoryItem _inventory[INVENTORY_MAX_ITEMS] = {
	{ kZoneDoor,		1 },		// open/close icon
	{ kZoneExamine, 	3 },		// examine icon
	{ kZoneGet, 		2 },		// pick up/use icon
	{ kZoneSpeak,		4 },		// speak icon
	{ 0,	0 },					// items...
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 },
	{ 0,	0 }
};


int16 getNumUsedSlots() {
	int16 num = 0;
	while (num < INVENTORY_MAX_ITEMS && _inventory[num]._id != 0)
		num++;
	return num;
}


//	get inventory item index at position (x,y)
//	in screen coordinates
//
int16 Parallaction::getHoverInventoryItem(int16 x, int16 y) {

	int16 slot = getNumUsedSlots();
	slot = (slot + 4) / INVENTORY_ITEMS_PER_LINE;

	Common::Rect r(INVENTORY_WIDTH, _numInvLines * INVENTORYITEM_HEIGHT);
	r.moveTo(_invPosition);

	if (!r.contains(Common::Point(x,y)))
		return -1;

	return ((x - _invPosition.x) / INVENTORYITEM_WIDTH) + (INVENTORY_ITEMS_PER_LINE * ((y - _invPosition.y) / INVENTORYITEM_HEIGHT));

}

void drawInventoryItem(uint16 pos, InventoryItem *item) {

	uint16 line = pos / INVENTORY_ITEMS_PER_LINE;
	uint16 col = pos % INVENTORY_ITEMS_PER_LINE;

	// FIXME: this will end up in a general blit function
	byte* s = _vm->_char._objs->getFramePtr(item->_index);
	byte* d = _buffer + col * INVENTORYITEM_WIDTH + line * _vm->_char._objs->_height * INVENTORY_WIDTH;
	for (uint32 i = 0; i < INVENTORYITEM_HEIGHT; i++) {
		memcpy(d, s, INVENTORYITEM_WIDTH);

		d += INVENTORY_WIDTH;
		s += INVENTORYITEM_PITCH;
	}

	return;
}



void refreshInventory() {
	for (uint16 i = 0; i < INVENTORY_MAX_ITEMS; i++)
		drawInventoryItem(i, &_inventory[i]);

	return;
}

int Parallaction::addInventoryItem(uint16 item) {

	int16 slot = getNumUsedSlots();
	if (slot == INVENTORY_MAX_ITEMS)
		return -1;

	_inventory[slot]._id = MAKE_INVENTORY_ID(item);
	_inventory[slot]._index = item;

	return 0;
}


void Parallaction::dropItem(uint16 v) {

	bool found = false;
	for (uint16 slot = 0; slot < INVENTORY_MAX_ITEMS - 1; slot++) {

		if (v + INVENTORY_FIRST_ITEM == _inventory[slot]._index) {
			found = true;
		}

		if (!found) continue;

		memcpy(&_inventory[slot], &_inventory[slot+1], sizeof(InventoryItem));
	}

	return;
}


int16 Parallaction::isItemInInventory(int32 v) {

	for (uint16 slot = 0; slot < INVENTORY_MAX_ITEMS; slot++) {
		if (_inventory[slot]._id == (uint)v)
			return 1;
	}

	return 0;
}


void drawBorder(const Common::Rect& r, byte *buffer, byte color) {

	byte *d = buffer + r.left + INVENTORY_WIDTH * r.top;

	memset(d, color, r.width());

	for (uint16 i = 0; i < r.height(); i++) {
		d[i * INVENTORY_WIDTH] = color;
		d[i * INVENTORY_WIDTH + r.width() - 1] = color;
	}

	d = buffer + r.left + INVENTORY_WIDTH * (r.bottom - 1);
	memset(d, color, r.width());

	return;
}

//
//	draws a color border around the specified position in the inventory
//
void highlightInventoryItem(int16 pos, byte color) {

	if (color != 12) color = 19;

	if (pos == -1) return;

	uint16 line = pos / INVENTORY_ITEMS_PER_LINE;
	uint16 col = pos % INVENTORY_ITEMS_PER_LINE;

	Common::Rect r(INVENTORYITEM_WIDTH, _vm->_char._objs->_height);
	r.moveTo(col * INVENTORYITEM_WIDTH, line * _vm->_char._objs->_height);

	drawBorder(r, _buffer, color);

	return;
}


int16 getInventoryItemIndex(int16 pos) {
	// TODO: should assert against the number of items actually contained,
	// not the theoretical limit.
	assert(pos >= 0 && pos < INVENTORY_MAX_ITEMS);
	return _inventory[pos]._index;
}



void jobShowInventory(void *parm, Job *j) {
//	printf("job_showInventory()...");

	int16 slot = getNumUsedSlots();
	_numInvLines = (slot + 4) / INVENTORY_ITEMS_PER_LINE;

	Common::Rect r(INVENTORY_WIDTH, _numInvLines * INVENTORYITEM_HEIGHT);

	r.moveTo(_invPosition);

	_vm->_gfx->copyRect(
		Gfx::kBitBack,
		r,
		_buffer,
		INVENTORY_WIDTH
	);

	return;
}



void jobHideInventory(void *parm, Job *j) {
//	printf("job_hideInventory()\n");

	static uint16 count = 0;

	_engineFlags |= kEngineBlockInput;

	count++;
	if (count == 2) {
		count = 0;
		j->_finished = 1;
		_engineFlags &= ~kEngineBlockInput;
	}

	Common::Rect r(INVENTORY_WIDTH, _numInvLines * INVENTORYITEM_HEIGHT);
	r.moveTo(_invPosition);

	_vm->_gfx->restoreBackground(r);

	return;
}



void openInventory() {
	_engineFlags |= kEngineInventory;

	int16 slot = getNumUsedSlots();
	uint16 lines = (slot + 4) / INVENTORY_ITEMS_PER_LINE;

	_invPosition.x = CLIP(_vm->_mousePos.x - (INVENTORY_WIDTH / 2), 0, (int)(_vm->_screenWidth - INVENTORY_WIDTH));
	_invPosition.y = CLIP(_vm->_mousePos.y - 2 - (lines * INVENTORYITEM_HEIGHT), 0, (int)(_vm->_screenHeight - lines * INVENTORYITEM_HEIGHT));

	refreshInventory();

	return;

}



void closeInventory() {
	_engineFlags &= ~kEngineInventory;
}

void initInventory() {
	_buffer = (byte*)malloc(INVENTORY_WIDTH * INVENTORY_HEIGHT);
}

void destroyInventory() {
	if (_buffer)
		free(_buffer);
	_buffer = 0;
}

void cleanInventory() {

	for (uint16 slot = INVENTORY_FIRST_ITEM; slot < INVENTORY_MAX_ITEMS; slot++) {
		_inventory[slot]._id = 0;
		_inventory[slot]._index = 0;
	}

	return;
}


} // namespace Parallaction