1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "dragons/actor.h"
24 #include "dragons/dragons.h"
25 #include "dragons/dragonini.h"
26 #include "dragons/background.h"
27 #include "dragons/inventory.h"
28 #include "dragons/bag.h"
29 #include "dragons/scene.h"
30 #include "dragons/talk.h"
31 #include "dragons/screen.h"
32
33 namespace Dragons {
34
35
36 static const struct {
37 int x, y;
38 } positionTable[4] = {
39 { 2, 0 },
40 { 206, 0 },
41 { 2, 158 },
42 { 206, 158 }
43 };
44
45 static const int16 bagBounceTable[4] = {
46 -5, -0xa, -5, 0
47 };
48
49 static const int16 invXPosTable[41] = {
50 0x0080, 0x00a0, 0x00c0, 0x0060, 0x0080, 0x00a0, 0x00c0, 0x00e0,
51 0x0100, 0x0020, 0x0040, 0x0060, 0x0080, 0x00a0, 0x00c0, 0x00e0,
52 0x0100, 0x0020, 0x0040, 0x0060, 0x0080, 0x00a0, 0x00c0, 0x00e0,
53 0x0100, 0x0020, 0x0040, 0x0060, 0x0080, 0x00a0, 0x00c0, 0x00e0,
54 0x0100, 0x0020, 0x0040, 0x0060, 0x0080, 0x00a0, 0x00c0, 0x00e0,
55 0x0100
56 };
57
58 static const int16 invYPosTable[41] = {
59 0x0028, 0x0028, 0x0028, 0x0040, 0x0040, 0x0040, 0x0040, 0x0040,
60 0x0040, 0x0058, 0x0058, 0x0058, 0x0058, 0x0058, 0x0058, 0x0058,
61 0x0058, 0x0070, 0x0070, 0x0070, 0x0070, 0x0070, 0x0070, 0x0070,
62 0x0070, 0x0088, 0x0088, 0x0088, 0x0088, 0x0088, 0x0088, 0x0088,
63 0x0088, 0x00a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0, 0x00a0,
64 0x00a0
65 };
66
Inventory(DragonsEngine * vm)67 Inventory::Inventory(DragonsEngine *vm) : _vm(vm) {
68 _state = Closed;
69 _sequenceId = 0;
70 _screenPositionIndex = 0;
71 _previousState = Closed;
72 _bag = nullptr;
73 _actor = nullptr;
74
75 _inventionBookPrevSceneUpdateFunc = nullptr;
76 _inventionBookPrevSceneId = 0;
77 _inventionBookPrevFlickerINISceneId = 0;
78 _inventionBookPrevFlickerINIPosition = Common::Point(0, 0);
79 }
80
init(ActorManager * actorManager,BackgroundResourceLoader * backgroundResourceLoader,Bag * bag,DragonINIResource * dragonIniResource)81 void Inventory::init(ActorManager *actorManager, BackgroundResourceLoader *backgroundResourceLoader, Bag *bag, DragonINIResource *dragonIniResource) {
82 _actor = actorManager->loadActor(1, 1); //Load inventory
83 _actor->_x_pos = 2;
84 _actor->_y_pos = 0;
85 _actor->_priorityLayer = 6;
86 _actor->_flags = 0;
87 _actor->_scale = DRAGONS_ENGINE_SPRITE_100_PERCENT_SCALE;
88 _actor->updateSequence(0);
89 _actor->_flags |= (ACTOR_FLAG_40 | Dragons::ACTOR_FLAG_80 | Dragons::ACTOR_FLAG_100 |
90 ACTOR_FLAG_200);
91 _sequenceId = 0;
92 _state = Closed;
93 _previousState = Closed;
94 _bag = bag;
95
96 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
97 actorManager->loadActor(0, i + ACTOR_INVENTORY_OFFSET); // TODO need to share resource between inventory item actors.
98 }
99
100 loadInventoryItemsFromSave();
101 }
102
103
loadScene(uint32 sceneId)104 void Inventory::loadScene(uint32 sceneId) {
105 if (_state == Closed) {
106 _sequenceId = _vm->isFlagSet(ENGINE_FLAG_400000) ? 1 : 0;
107 }
108
109 if (_sequenceId == 0 && _vm->getVar(7) == 1) {
110 _actor->updateSequence(5);
111 } else {
112 _actor->updateSequence(_sequenceId);
113 }
114
115 setPositionFromSceneId(sceneId);
116 }
117
updateVisibility()118 void Inventory::updateVisibility() {
119 _actor->_priorityLayer = _vm->isFlagSet(ENGINE_FLAG_10) ? (int16)6 : (int16)0;
120 }
121
getPosition()122 Common::Point Inventory::getPosition() {
123 return Common::Point(positionTable[_screenPositionIndex].x, positionTable[_screenPositionIndex].y);
124 }
125
setActorFlag400()126 void Inventory::setActorFlag400() {
127 _actor->setFlag(ACTOR_FLAG_400);
128 }
129
clearActorFlag400()130 void Inventory::clearActorFlag400() {
131 _actor->clearFlag(ACTOR_FLAG_400);
132 }
133
setPriority(uint16 priority)134 void Inventory::setPriority(uint16 priority) {
135 _actor->_priorityLayer = priority;
136 }
137
setActorSequenceId(int32 sequenceId)138 void Inventory::setActorSequenceId(int32 sequenceId) {
139 if (isActorSet()) {
140 _actor->_sequenceID = sequenceId;
141 }
142 }
143
updateActorSequenceId(int32 sequenceId)144 void Inventory::updateActorSequenceId(int32 sequenceId) {
145 if (isActorSet()) {
146 _actor->updateSequence(sequenceId);
147 }
148 }
149
resetSequenceId()150 void Inventory::resetSequenceId() {
151 _actor->updateSequence(_sequenceId);
152 }
153
openInventory()154 void Inventory::openInventory() {
155 //TODO 0x80030e8c
156 _sequenceId = 4;
157
158 if (!_vm->isFlagSet(ENGINE_FLAG_400000)) {
159 _sequenceId = 2;
160 }
161 _actor->updateSequence(_sequenceId);
162 _screenPositionIndex = 1;
163 _actor->_x_pos = positionTable[_screenPositionIndex].x;
164 if ((_sequenceId == 0) || (_sequenceId == 2)) {
165 _actor->_x_pos = positionTable[_screenPositionIndex].x + 0x32;
166 }
167 _actor->_y_pos = positionTable[_screenPositionIndex].y;
168 animateBagIn();
169
170 //TODO 0x800310e0 update cursor position.
171
172 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
173 Actor *item = _vm->_actorManager->getActor(i + ACTOR_INVENTORY_OFFSET);
174
175 item->_x_pos = item->_walkDestX = invXPosTable[i] + 0x10;
176 item->_y_pos = item->_walkDestY = invYPosTable[i] + 0xc;
177
178 if (_inventoryItemTbl[i]) {
179 item->_flags = 0; //clear all flags
180 item->_scale = DRAGONS_ENGINE_SPRITE_100_PERCENT_SCALE;
181 item->_priorityLayer = 0;
182 item->updateSequence(_vm->getINI(_inventoryItemTbl[i] - 1)->inventorySequenceId * 2 + 10);
183 item->setFlag(ACTOR_FLAG_200);
184 item->setFlag(ACTOR_FLAG_100);
185 item->setFlag(ACTOR_FLAG_80);
186 item->setFlag(ACTOR_FLAG_40);
187 item->_priorityLayer = 6;
188 }
189 }
190 }
191
animateBagIn()192 void Inventory::animateBagIn() {
193 _vm->clearFlags(ENGINE_FLAG_8);
194 _vm->setFlags(ENGINE_FLAG_80);
195
196 Common::Point pos = _bag->getPosition();
197 pos.y = -228;
198 int16 accel = 8;
199
200 // Drop bag down into position.
201 while (pos.y < 0) {
202 pos.y += accel;
203 _bag->updatePosition(pos);
204 _vm->waitForFrames(1);
205 accel += 2;
206 }
207
208 _vm->playOrStopSound(0x8001);
209
210 // Shake bag at the end.
211 for (int i = 0; i < 4; i++) {
212 pos.y = bagBounceTable[i];
213 _bag->updatePosition(pos);
214 _vm->waitForFrames(2);
215 }
216
217 _vm->setFlags(ENGINE_FLAG_8);
218 _vm->setFlags(ENGINE_FLAG_10);
219 }
220
animateBagOut()221 void Inventory::animateBagOut() {
222 _vm->playOrStopSound(0x8000);
223 Common::Point pos = _bag->getPosition();
224 if (pos.y != 0xc8) {
225 for (; pos.y != 0xc8; pos.y += 0x19) {
226 _bag->updatePosition(pos);
227 _vm->waitForFrames(1);
228 }
229 }
230 _vm->clearFlags(ENGINE_FLAG_80);
231 }
232
closeInventory()233 void Inventory::closeInventory() {
234 _vm->_actorManager->clearActorFlags(ACTOR_INVENTORY_OFFSET);
235 _screenPositionIndex = _vm->_dragonRMS->getInventoryPosition(_vm->getCurrentSceneId());
236
237 if (!_vm->isFlagSet(ENGINE_FLAG_400000)) {
238 _sequenceId = 0;
239 } else {
240 if (_previousState == InventionBookOpen) {
241 _sequenceId = 3;
242 } else {
243 _sequenceId = 1;
244 }
245 }
246 _actor->updateSequence(_sequenceId);
247 _actor->_x_pos = positionTable[_screenPositionIndex].x;
248 if (((_sequenceId == 0) || (_sequenceId == 2)) && ((_screenPositionIndex == 1 || (_screenPositionIndex == 3)))) {
249 _actor->_x_pos += 0x32;
250 }
251 _actor->_y_pos = positionTable[_screenPositionIndex].y;
252 animateBagOut();
253 }
254
draw()255 void Inventory::draw() {
256 if (_bag) {
257 _bag->draw();
258 }
259 }
260
getIniAtPosition(int16 x,int16 y)261 uint16 Inventory::getIniAtPosition(int16 x, int16 y) {
262 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
263 if (_inventoryItemTbl[i]) {
264 Actor *item = _vm->_actorManager->getActor(i + ACTOR_INVENTORY_OFFSET);
265 if (item->_x_pos - 0x10 <= x && x < item->_x_pos + 0x10
266 && item->_y_pos - 0xc <= y && y < item->_y_pos + 0xc) {
267 return _inventoryItemTbl[i];
268 }
269 }
270 }
271 return 0;
272 }
273
loadInventoryItemsFromSave()274 void Inventory::loadInventoryItemsFromSave() {
275 memset(_inventoryItemTbl, 0, sizeof(_inventoryItemTbl));
276 int j = 0;
277 for (int i = 0; i < _vm->_dragonINIResource->totalRecords() && j < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
278 DragonINI *ini = _vm->_dragonINIResource->getRecord(i);
279 if (ini->sceneId == 1) {
280 _inventoryItemTbl[j++] = i + 1;
281 }
282 }
283 }
284
openInventionBook()285 void Inventory::openInventionBook() {
286 _inventionBookPrevSceneUpdateFunc = _vm->getSceneUpdateFunction();
287 _vm->clearSceneUpdateFunction();
288 _vm->fadeToBlack();
289 _sequenceId = 2;
290 _actor->updateSequence(2);
291 _inventionBookPrevSceneId = _vm->getCurrentSceneId();
292 DragonINI *flicker = _vm->_dragonINIResource->getFlickerRecord();
293 if (flicker && flicker->actor) {
294 _inventionBookPrevFlickerINISceneId = flicker->sceneId;
295 _inventionBookPrevFlickerINIPosition = Common::Point(flicker->actor->_x_pos, flicker->actor->_y_pos);
296 flicker->sceneId = 0;
297 }
298 _vm->_scene->setSceneId(2);
299 _vm->_scene->loadScene(2, 0);
300 }
301
closeInventionBook()302 void Inventory::closeInventionBook() {
303 uint sceneId;
304
305 _vm->fadeToBlack();
306
307 DragonINI *flicker = _vm->_dragonINIResource->getFlickerRecord();
308 if (flicker) {
309 flicker->x = _inventionBookPrevFlickerINIPosition.x;
310 flicker->y = _inventionBookPrevFlickerINIPosition.y;
311 flicker->sceneId = _inventionBookPrevFlickerINISceneId;
312 }
313 _vm->_scene->setSceneId(_inventionBookPrevSceneId);
314
315 _sequenceId = 0;
316 setActorSequenceId(0);
317 setPositionFromSceneId(_inventionBookPrevSceneId);
318 sceneId = _vm->_scene->getSceneId();
319 if (((((sceneId == 0x23) || (sceneId == 0x2d)) || (sceneId == 0x2e)) || ((sceneId == 0x31 || (sceneId == 0x32)))) || (sceneId == 0x28)) {
320 if ((uint)_vm->_scene->getSceneId() == 0x27) {
321 _vm->getINI(0x206)->sceneId = 0;
322 }
323 } else {
324 if (sceneId != 0x27) {
325 if (sceneId != 0x1c && sceneId != 0x1d && sceneId != 0x21) {
326 _vm->_scene->loadScene(sceneId | 0x8000u, 0x1e);
327 _vm->setSceneUpdateFunction(_inventionBookPrevSceneUpdateFunc);
328 return;
329 }
330 if ((uint)_vm->_scene->getSceneId() == 0x27) {
331 _vm->getINI(0x206)->sceneId = 0;
332 }
333 } else {
334 _vm->getINI(0x206)->sceneId = 0;
335 }
336 }
337 _vm->_scene->loadScene(_vm->_scene->getSceneId(), 0x1e);
338 _vm->setSceneUpdateFunction(_inventionBookPrevSceneUpdateFunc);
339 }
340
setPositionFromSceneId(uint32 sceneId)341 void Inventory::setPositionFromSceneId(uint32 sceneId) {
342 _screenPositionIndex = _vm->_dragonRMS->getInventoryPosition(sceneId);
343
344 _actor->_x_pos = positionTable[_screenPositionIndex].x;
345 if ((_sequenceId == 0 || _sequenceId == 2) && (_screenPositionIndex == 1 || _screenPositionIndex == 3)) {
346 _actor->_x_pos += 0x32;
347 }
348 _actor->_y_pos = positionTable[_screenPositionIndex].y;
349 }
350
addItem(uint16 initId)351 bool Inventory::addItem(uint16 initId) {
352 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
353 if (_inventoryItemTbl[i] == 0) {
354 _inventoryItemTbl[i] = initId;
355 return true;
356 }
357 }
358
359 return false;
360 }
361
getInventoryItemActor(uint16 iniId)362 Actor *Inventory::getInventoryItemActor(uint16 iniId) {
363 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
364 if (_inventoryItemTbl[i] == iniId) {
365 return _vm->_actorManager->getActor(i + ACTOR_INVENTORY_OFFSET);
366 }
367 }
368 error("getInventoryItemActor(%d) not found", iniId);
369 }
370
replaceItem(uint16 existingIniId,uint16 newIniId)371 void Inventory::replaceItem(uint16 existingIniId, uint16 newIniId) {
372 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
373 if (_inventoryItemTbl[i] == existingIniId) {
374 _inventoryItemTbl[i] = newIniId;
375 return;
376 }
377 }
378 }
379
addItemIfPositionIsEmpty(uint16 iniId,uint16 x,uint16 y)380 bool Inventory::addItemIfPositionIsEmpty(uint16 iniId, uint16 x, uint16 y) {
381 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
382 Actor *actor = _vm->_actorManager->getActor(i + ACTOR_INVENTORY_OFFSET);
383 if ((((actor->_x_pos - 0x10 <= x) &&
384 (x < actor->_x_pos + 0x10)) &&
385 (actor->_y_pos - 0xc <= y)) &&
386 (y < actor->_y_pos + 0xc)) {
387 _inventoryItemTbl[i] = iniId;
388 return true;
389 }
390 }
391 return false;
392 }
393
clearItem(uint16 iniId)394 bool Inventory::clearItem(uint16 iniId) {
395 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
396 if (_inventoryItemTbl[i] == iniId) {
397 _inventoryItemTbl[i] = 0;
398 return true;
399 }
400 }
401 return false;
402 }
403
inventoryMissing()404 void Inventory::inventoryMissing() {
405 bool flag8Set;
406 uint32 textIndex;
407 static uint16 counter = 0;
408
409 DragonINI *flicker = _vm->_dragonINIResource->getFlickerRecord();
410 if (flicker->actor != nullptr) {
411 flicker->actor->clearFlag(ACTOR_FLAG_10);
412 if ((_vm->getCurrentSceneId() != 0x2e) || (flicker->actor->_resourceID != 0x91)) {
413 flicker->actor->setFlag(ACTOR_FLAG_4);
414 }
415 }
416 flag8Set = _vm->isFlagSet(ENGINE_FLAG_8);
417 _vm->clearFlags(ENGINE_FLAG_8);
418 if (counter == 0) {
419 textIndex = 0x114FA; //Hey! My bag is missing!
420 } else {
421 textIndex = 0x11538; //The Chancellor snaked my bag!
422 }
423 counter = counter + 1;
424 _vm->_talk->talkFromIni(0, textIndex);
425 if (flag8Set) {
426 _vm->setFlags(ENGINE_FLAG_8);
427 }
428 }
429
setPreviousState()430 void Inventory::setPreviousState() {
431 InventoryState tmpState = _state;
432 setState(_previousState);
433 _previousState = tmpState;
434 }
435
hasItem(uint16 iniId)436 bool Inventory::hasItem(uint16 iniId) {
437 for (int i = 0; i < DRAGONS_MAX_INVENTORY_ITEMS; i++) {
438 if (_inventoryItemTbl[i] == iniId) {
439 return true;
440 }
441 }
442 return false;
443 }
444
445 } // End of namespace Dragons
446