2015-03-21 02:01:52 +00:00
|
|
|
/* 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.
|
2015-05-09 16:04:13 +00:00
|
|
|
*
|
2015-03-21 02:01:52 +00:00
|
|
|
* 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.
|
2015-05-09 16:04:13 +00:00
|
|
|
*
|
2015-03-21 02:01:52 +00:00
|
|
|
* 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 "sherlock/inventory.h"
|
2015-03-22 13:02:31 +00:00
|
|
|
#include "sherlock/sherlock.h"
|
2015-06-26 00:42:02 +00:00
|
|
|
#include "sherlock/scalpel/scalpel_inventory.h"
|
2015-05-28 12:31:53 +00:00
|
|
|
#include "sherlock/scalpel/scalpel_user_interface.h"
|
2015-03-21 02:01:52 +00:00
|
|
|
|
|
|
|
namespace Sherlock {
|
|
|
|
|
2015-03-29 13:52:23 +00:00
|
|
|
InventoryItem::InventoryItem(int requiredFlag, const Common::String &name,
|
2015-05-07 17:33:44 +00:00
|
|
|
const Common::String &description, const Common::String &examine) :
|
|
|
|
_requiredFlag(requiredFlag), _name(name), _description(description),
|
2015-03-29 13:52:23 +00:00
|
|
|
_examine(examine), _lookFlag(0) {
|
|
|
|
}
|
|
|
|
|
2015-06-28 17:29:32 +00:00
|
|
|
InventoryItem::InventoryItem(int requiredFlag, const Common::String &name,
|
|
|
|
const Common::String &description, const Common::String &examine, const Common::String &verbName) :
|
|
|
|
_requiredFlag(requiredFlag), _name(name), _description(description),
|
|
|
|
_examine(examine), _lookFlag(0) {
|
|
|
|
_verb._verb = verbName;
|
|
|
|
}
|
|
|
|
|
2015-06-07 23:18:14 +00:00
|
|
|
void InventoryItem::synchronize(Serializer &s) {
|
2015-04-25 09:42:15 +00:00
|
|
|
s.syncAsSint16LE(_requiredFlag);
|
|
|
|
s.syncAsSint16LE(_lookFlag);
|
|
|
|
s.syncString(_name);
|
|
|
|
s.syncString(_description);
|
|
|
|
s.syncString(_examine);
|
|
|
|
}
|
|
|
|
|
2015-03-29 13:52:23 +00:00
|
|
|
/*----------------------------------------------------------------*/
|
|
|
|
|
2015-06-26 00:42:02 +00:00
|
|
|
Inventory *Inventory::init(SherlockEngine *vm) {
|
|
|
|
if (vm->getGameID() == GType_SerratedScalpel)
|
|
|
|
return new Scalpel::ScalpelInventory(vm);
|
|
|
|
else
|
|
|
|
return new Inventory(vm);
|
|
|
|
}
|
|
|
|
|
2015-03-22 13:02:31 +00:00
|
|
|
Inventory::Inventory(SherlockEngine *vm) : Common::Array<InventoryItem>(), _vm(vm) {
|
|
|
|
Common::fill(&_invShapes[0], &_invShapes[MAX_VISIBLE_INVENTORY], (ImageFile *)nullptr);
|
|
|
|
_invGraphicsLoaded = false;
|
|
|
|
_invIndex = 0;
|
|
|
|
_holdings = 0;
|
2015-03-31 01:07:01 +00:00
|
|
|
_invMode = INVMODE_EXIT;
|
2015-05-22 17:52:00 +00:00
|
|
|
for (int i = 0; i < 6; ++i)
|
|
|
|
_invShapes[i] = nullptr;
|
2015-03-22 13:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Inventory::~Inventory() {
|
|
|
|
freeGraphics();
|
|
|
|
}
|
|
|
|
|
2015-03-27 01:40:24 +00:00
|
|
|
void Inventory::freeInv() {
|
2015-03-22 13:02:31 +00:00
|
|
|
freeGraphics();
|
|
|
|
|
|
|
|
_names.clear();
|
|
|
|
_invGraphicsLoaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inventory::freeGraphics() {
|
|
|
|
for (uint idx = 0; idx < MAX_VISIBLE_INVENTORY; ++idx)
|
|
|
|
delete _invShapes[idx];
|
2015-05-07 17:33:44 +00:00
|
|
|
|
2015-03-22 13:02:31 +00:00
|
|
|
Common::fill(&_invShapes[0], &_invShapes[MAX_VISIBLE_INVENTORY], (ImageFile *)nullptr);
|
|
|
|
_invGraphicsLoaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inventory::loadInv() {
|
|
|
|
// Exit if the inventory names are already loaded
|
|
|
|
if (_names.size() > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Load the inventory names
|
|
|
|
Common::SeekableReadStream *stream = _vm->_res->load("invent.txt");
|
|
|
|
|
2015-05-09 15:25:05 +00:00
|
|
|
int streamSize = stream->size();
|
|
|
|
while (stream->pos() < streamSize) {
|
2015-03-22 13:02:31 +00:00
|
|
|
Common::String name;
|
|
|
|
char c;
|
|
|
|
while ((c = stream->readByte()) != 0)
|
|
|
|
name += c;
|
|
|
|
|
|
|
|
_names.push_back(name);
|
|
|
|
}
|
2015-05-07 17:33:44 +00:00
|
|
|
|
2015-03-22 13:02:31 +00:00
|
|
|
delete stream;
|
2015-03-29 13:52:23 +00:00
|
|
|
|
|
|
|
loadGraphics();
|
2015-03-22 13:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inventory::loadGraphics() {
|
|
|
|
if (_invGraphicsLoaded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Default all inventory slots to empty
|
|
|
|
Common::fill(&_invShapes[0], &_invShapes[MAX_VISIBLE_INVENTORY], (ImageFile *)nullptr);
|
|
|
|
|
2015-03-29 13:52:23 +00:00
|
|
|
for (int idx = _invIndex; (idx < _holdings) && (idx - _invIndex) < MAX_VISIBLE_INVENTORY; ++idx) {
|
2015-05-11 23:53:49 +00:00
|
|
|
// Get the name of the item to be displayed, figure out its accompanying
|
|
|
|
// .VGS file with its picture, and then load it
|
2015-03-22 13:02:31 +00:00
|
|
|
int invNum = findInv((*this)[idx]._name);
|
2015-06-14 17:35:09 +00:00
|
|
|
Common::String filename = Common::String::format("item%02d.vgs", invNum + 1);
|
|
|
|
|
2015-06-16 07:02:32 +00:00
|
|
|
if (!IS_3DO) {
|
2015-06-14 17:35:09 +00:00
|
|
|
// PC
|
|
|
|
_invShapes[idx - _invIndex] = new ImageFile(filename);
|
|
|
|
} else {
|
|
|
|
_invShapes[idx - _invIndex] = new ImageFile3DO(filename, kImageFile3DOType_RoomFormat);
|
|
|
|
}
|
2015-03-22 13:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_invGraphicsLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inventory::findInv(const Common::String &name) {
|
2015-04-25 09:42:15 +00:00
|
|
|
for (int idx = 0; idx < (int)_names.size(); ++idx) {
|
2015-05-17 19:41:42 +00:00
|
|
|
if (name.equalsIgnoreCase(_names[idx]))
|
2015-03-29 13:52:23 +00:00
|
|
|
return idx;
|
2015-03-22 13:02:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:30:31 +00:00
|
|
|
// Couldn't find the desired item
|
|
|
|
error("Couldn't find inventory item - %s", name.c_str());
|
2015-03-22 13:02:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 04:35:26 +00:00
|
|
|
int Inventory::putNameInInventory(const Common::String &name) {
|
|
|
|
Scene &scene = *_vm->_scene;
|
|
|
|
int matches = 0;
|
|
|
|
|
|
|
|
for (uint idx = 0; idx < scene._bgShapes.size(); ++idx) {
|
|
|
|
Object &o = scene._bgShapes[idx];
|
2015-05-17 19:41:42 +00:00
|
|
|
if (name.equalsIgnoreCase(o._name) && o._type != INVALID) {
|
2015-04-11 04:35:26 +00:00
|
|
|
putItemInInventory(o);
|
|
|
|
++matches;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inventory::putItemInInventory(Object &obj) {
|
|
|
|
Scene &scene = *_vm->_scene;
|
|
|
|
int matches = 0;
|
|
|
|
bool pickupFound = false;
|
|
|
|
|
|
|
|
if (obj._pickupFlag)
|
|
|
|
_vm->setFlags(obj._pickupFlag);
|
|
|
|
|
2015-05-18 23:15:17 +00:00
|
|
|
for (int useNum = 0; useNum < USE_COUNT; ++useNum) {
|
2015-05-17 19:41:42 +00:00
|
|
|
if (obj._use[useNum]._target.equalsIgnoreCase("*PICKUP*")) {
|
2015-04-11 04:35:26 +00:00
|
|
|
pickupFound = true;
|
|
|
|
|
2015-05-18 23:15:17 +00:00
|
|
|
for (int namesNum = 0; namesNum < NAMES_COUNT; ++namesNum) {
|
2015-04-11 04:35:26 +00:00
|
|
|
for (uint bgNum = 0; bgNum < scene._bgShapes.size(); ++bgNum) {
|
|
|
|
Object &bgObj = scene._bgShapes[bgNum];
|
2015-05-17 19:41:42 +00:00
|
|
|
if (obj._use[useNum]._names[namesNum].equalsIgnoreCase(bgObj._name)) {
|
2015-04-11 04:35:26 +00:00
|
|
|
copyToInventory(bgObj);
|
|
|
|
if (bgObj._pickupFlag)
|
|
|
|
_vm->setFlags(bgObj._pickupFlag);
|
|
|
|
|
|
|
|
if (bgObj._type == ACTIVE_BG_SHAPE || bgObj._type == NO_SHAPE || bgObj._type == HIDE_SHAPE) {
|
|
|
|
if (bgObj._imageFrame == nullptr || bgObj._frameNumber < 0)
|
|
|
|
// No shape to erase, so flag as hidden
|
|
|
|
bgObj._type = INVALID;
|
|
|
|
else
|
|
|
|
bgObj._type = REMOVE;
|
|
|
|
} else if (bgObj._type == HIDDEN) {
|
|
|
|
bgObj._type = INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
++matches;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pickupFound) {
|
|
|
|
// No pickup item found, so add the passed item
|
|
|
|
copyToInventory(obj);
|
|
|
|
matches = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matches == 0) {
|
|
|
|
if (!pickupFound)
|
|
|
|
matches = 1;
|
|
|
|
|
|
|
|
if (obj._type == ACTIVE_BG_SHAPE || obj._type == NO_SHAPE || obj._type == HIDE_SHAPE) {
|
|
|
|
if (obj._imageFrame == nullptr || obj._frameNumber < 0)
|
|
|
|
// No shape to erase, so flag as hidden
|
|
|
|
obj._type = INVALID;
|
|
|
|
else
|
|
|
|
obj._type = REMOVE;
|
|
|
|
} else if (obj._type == HIDDEN) {
|
|
|
|
obj._type = INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inventory::copyToInventory(Object &obj) {
|
2015-04-11 21:52:19 +00:00
|
|
|
InventoryItem invItem;
|
|
|
|
invItem._name = obj._name;
|
|
|
|
invItem._description = obj._description;
|
|
|
|
invItem._examine = obj._examine;
|
|
|
|
invItem._lookFlag = obj._lookFlag;
|
|
|
|
invItem._requiredFlag = obj._requiredFlag;
|
|
|
|
|
|
|
|
insert_at(_holdings, invItem);
|
|
|
|
++_holdings;
|
2015-04-11 04:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Inventory::deleteItemFromInventory(const Common::String &name) {
|
|
|
|
int invNum = -1;
|
|
|
|
|
|
|
|
for (int idx = 0; idx < (int)size() && invNum == -1; ++idx) {
|
2015-05-17 19:41:42 +00:00
|
|
|
if (name.equalsIgnoreCase((*this)[idx]._name))
|
2015-04-11 04:35:26 +00:00
|
|
|
invNum = idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invNum == -1)
|
|
|
|
// Item not present
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Item found, so delete it
|
|
|
|
remove_at(invNum);
|
|
|
|
--_holdings;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-07 23:18:14 +00:00
|
|
|
void Inventory::synchronize(Serializer &s) {
|
2015-04-22 03:51:03 +00:00
|
|
|
s.syncAsSint16LE(_holdings);
|
|
|
|
|
|
|
|
uint count = size();
|
|
|
|
s.syncAsUint16LE(count);
|
|
|
|
if (s.isLoading()) {
|
|
|
|
resize(count);
|
|
|
|
|
|
|
|
// Reset inventory back to start
|
|
|
|
_invIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint idx = 0; idx < size(); ++idx) {
|
2015-04-25 09:42:15 +00:00
|
|
|
(*this)[idx].synchronize(s);
|
|
|
|
|
2015-04-22 03:51:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 02:01:52 +00:00
|
|
|
} // End of namespace Sherlock
|