aboutsummaryrefslogtreecommitdiff
path: root/engines/pegasus/items/item.cpp
blob: 8089f2b93de62e2203c27a87914bb63388c6577f (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
/* 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.
 *
 * Additional copyright for this file:
 * Copyright (C) 1995-1997 Presto Studios, Inc.
 *
 * 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/error.h"
#include "common/stream.h"

#include "pegasus/constants.h"
#include "pegasus/elements.h"
#include "pegasus/pegasus.h"
#include "pegasus/surface.h"
#include "pegasus/ai/ai_area.h"
#include "pegasus/items/item.h"
#include "pegasus/items/itemlist.h"
#include "pegasus/items/biochips/biochipitem.h"
#include "pegasus/items/inventory/inventoryitem.h"

namespace Pegasus {

Item::Item(const ItemID id, const NeighborhoodID neighborhood, const RoomID room, const DirectionConstant direction) : IDObject(id) {
	_itemNeighborhood = neighborhood;
	_itemRoom = room;
	_itemDirection = direction;
	_itemWeight = 1;
	_itemOwnerID = kNoActorID;
	_itemState = 0;

	PegasusEngine *vm = (PegasusEngine *)g_engine;

	Common::SeekableReadStream *info = vm->_resFork->getResource(kItemInfoResType, kItemBaseResID + id);
	if (info) {
		_itemInfo.infoLeftTime = info->readUint32BE();
		_itemInfo.infoRightStart = info->readUint32BE();
		_itemInfo.infoRightStop = info->readUint32BE();
		_itemInfo.dragSpriteNormalID = info->readUint16BE();
		_itemInfo.dragSpriteUsedID = info->readUint16BE();

		if (vm->isDemo()) {
			// Adjust info right times to account for the stuff that was chopped out of the
			// info right movies.
			// Assumes time scale of 600.

			// Gap times in seconds
			static const TimeValue kGap1 = 24;
			static const TimeValue kGap2 = 34;
			static const TimeValue kGap3 = 4;
			static const TimeValue kGap4 = 4;

			static const TimeValue kGapForGroup1 = kGap1;
			static const TimeValue kGapForGroup2 = kGapForGroup1 + kGap2;
			static const TimeValue kGapForGroup3 = kGapForGroup2 + kGap3;
			static const TimeValue kGapForGroup4 = kGapForGroup3 + kGap4;

			switch (id) {
			case kHistoricalLog:
			case kJourneymanKey:
			case kKeyCard:
				_itemInfo.infoRightStart -= 600 * kGapForGroup1;
				_itemInfo.infoRightStop -= 600 * kGapForGroup1;
				break;
			case kAIBiochip:
				_itemInfo.infoRightStart -= 600 * kGapForGroup2;
				_itemInfo.infoRightStop -= 600 * kGapForGroup2;
				break;
			case kMapBiochip:
				_itemInfo.infoRightStart -= 600 * kGapForGroup3;
				_itemInfo.infoRightStop -= 600 * kGapForGroup3;
				break;
			case kPegasusBiochip:
				_itemInfo.infoRightStart -= 600 * kGapForGroup4;
				_itemInfo.infoRightStop -= 600 * kGapForGroup4;
				break;
			}
		}

		delete info;
	} else {
		memset(&_itemInfo, 0, sizeof(_itemInfo));
	}

	Common::SeekableReadStream *middleAreaInfo = vm->_resFork->getResource(kMiddleAreaInfoResType, kItemBaseResID + id);
	if (middleAreaInfo) {
		_sharedAreaInfo = readItemState(middleAreaInfo);
		delete middleAreaInfo;
	} else {
		// Only kArgonPickup does not have this
		memset(&_sharedAreaInfo, 0, sizeof(_sharedAreaInfo));
	}

	Common::SeekableReadStream *extraInfo = vm->_resFork->getResource(kItemExtraInfoResType, kItemBaseResID + id);
	if (!extraInfo)
		error("Extra info not found for item %d", id);

	_itemExtras.numEntries = extraInfo->readUint16BE();
	_itemExtras.entries = new ItemExtraEntry[_itemExtras.numEntries];
	for (uint16 i = 0; i < _itemExtras.numEntries; i++) {
		_itemExtras.entries[i].extraID = extraInfo->readUint32BE();
		_itemExtras.entries[i].extraArea = extraInfo->readUint16BE();
		_itemExtras.entries[i].extraStart = extraInfo->readUint32BE();
		_itemExtras.entries[i].extraStop = extraInfo->readUint32BE();
	}

	delete extraInfo;

	g_allItems.push_back(this);
}

Item::~Item() {
	delete[] _sharedAreaInfo.entries;
	delete[] _itemExtras.entries;
}

void Item::writeToStream(Common::WriteStream *stream) {
	stream->writeUint16BE(_itemNeighborhood);
	stream->writeUint16BE(_itemRoom);
	stream->writeByte(_itemDirection);
	stream->writeUint16BE(_itemOwnerID);
	stream->writeUint16BE(_itemState);
}

void Item::readFromStream(Common::ReadStream *stream) {
	_itemNeighborhood = stream->readUint16BE();
	_itemRoom = stream->readUint16BE();
	_itemDirection = stream->readByte();
	_itemOwnerID = stream->readUint16BE();
	_itemState = stream->readUint16BE();
}

ActorID Item::getItemOwner() const {
	return _itemOwnerID;
}

void Item::setItemOwner(const ActorID owner) {
	_itemOwnerID = owner;

	if (owner == kNoActorID) {
		if (isSelected())
			deselect();
		removedFromInventory();
	} else {
		addedToInventory();
	}
}

WeightType Item::getItemWeight() {
	return _itemWeight;
}

ItemState Item::getItemState() const {
	return _itemState;
}

void Item::setItemState(const ItemState state) {
	if (state != _itemState) {
		_itemState = state;

		if (getItemType() == kInventoryItemType && ((PegasusEngine *)g_engine)->getCurrentInventoryItem() == (InventoryItem *)this)
			select();
		else if (getItemType() == kBiochipItemType && ((PegasusEngine *)g_engine)->getCurrentBiochip() == (BiochipItem *)this)
			select();
	}
}

void Item::getItemRoom(NeighborhoodID &neighborhood, RoomID &room, DirectionConstant &direction) const {
	neighborhood = _itemNeighborhood;
	room = _itemRoom;
	direction = _itemDirection;
}

void Item::setItemRoom(const NeighborhoodID neighborhood, const RoomID room, const DirectionConstant direction) {
	_itemNeighborhood = neighborhood;
	_itemRoom = room;
	_itemDirection = direction;

	if (neighborhood == kNoNeighborhoodID)
		pickedUp();
	else
		dropped();
}

NeighborhoodID Item::getItemNeighborhood() const {
	return _itemNeighborhood;
}

TimeValue Item::getSharedAreaTime() const {
	if (!_sharedAreaInfo.entries)
		return 0xffffffff;

	TimeValue time;
	ItemState state;

	findItemStateEntryByState(_sharedAreaInfo, _itemState, time);
	if (time == 0xffffffff)
		getItemStateEntry(_sharedAreaInfo, 0, state, time);

	return time;
}

// Must affect images in shared area.
void Item::select() {
	_isSelected = true;

	if (g_AIArea) {
		if (getItemType() == kInventoryItemType)
			g_AIArea->setAIAreaToTime(kInventorySignature, kMiddleAreaSignature, getSharedAreaTime());
		else
			g_AIArea->setAIAreaToTime(kBiochipSignature, kMiddleAreaSignature, getSharedAreaTime());
	}
}

void Item::deselect() {
	_isActive = false;
	_isSelected = false;

	if (g_AIArea) {
		if (getItemType() == kInventoryItemType)
			g_AIArea->setAIAreaToTime(kInventorySignature, kMiddleAreaSignature, 0xffffffff);
		else
			g_AIArea->setAIAreaToTime(kBiochipSignature, kMiddleAreaSignature, 0xffffffff);
	}
}

void Item::getItemStateEntry(ItemStateInfo info, uint32 index, ItemState &state, TimeValue &time) {
	if (index < info.numEntries) {
		state = info.entries[index].itemState;
		time = info.entries[index].itemTime;
	} else {
		state = kNoItemState;
		time = 0xffffffff;
	}
}

void Item::findItemStateEntryByState(ItemStateInfo info, ItemState state, TimeValue &time) {
	for (uint16 i = 0; i < info.numEntries; i++) {
		if (info.entries[i].itemState == state) {
			time = info.entries[i].itemTime;
			return;
		}
	}

	time = 0xffffffff;
}

ItemStateInfo Item::readItemState(Common::SeekableReadStream *stream) {
	ItemStateInfo info;

	info.numEntries = stream->readUint16BE();
	info.entries = new ItemStateEntry[info.numEntries];
	for (uint16 i = 0; i < info.numEntries; i++) {
		info.entries[i].itemState = stream->readSint16BE();
		info.entries[i].itemTime = stream->readUint32BE();
	}

	return info;
}

Sprite *Item::getDragSprite(const DisplayElementID id) const {
	PegasusEngine *vm = (PegasusEngine *)g_engine;
	Sprite *result = new Sprite(id);
	SpriteFrame *frame = new SpriteFrame();

	frame->initFromPICTResource(vm->_resFork, _itemInfo.dragSpriteNormalID, true);
	result->addFrame(frame, 0, 0);

	if (_itemInfo.dragSpriteNormalID != _itemInfo.dragSpriteUsedID) {
		frame = new SpriteFrame();
		frame->initFromPICTResource(vm->_resFork, _itemInfo.dragSpriteUsedID, true);
	}

	result->addFrame(frame, 0, 0);
	result->setCurrentFrameIndex(0);
	return result;
}

TimeValue Item::getInfoLeftTime() const {
	return _itemInfo.infoLeftTime;
}

void Item::getInfoRightTimes(TimeValue &start, TimeValue &stop) const {
	start = _itemInfo.infoRightStart;
	stop = _itemInfo.infoRightStop;
}

void Item::findItemExtra(const uint32 extraID, ItemExtraEntry &entry) {
	for (uint32 i = 0; i < _itemExtras.numEntries; i++) {
		if (_itemExtras.entries[i].extraID == extraID) {
			entry = _itemExtras.entries[i];
			return;
		}
	}
}

} // End of namespace Pegasus