Skip to content

Commit

Permalink
ADL: Store items in a List instead of an Array
Browse files Browse the repository at this point in the history
Item IDs are not necessarily sequential
  • Loading branch information
waltervn committed Jun 6, 2016
1 parent b24f305 commit 2c8e0ce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
52 changes: 30 additions & 22 deletions engines/adl/adl.cpp
Expand Up @@ -379,7 +379,7 @@ void AdlEngine::drawPic(byte pic, Common::Point pos) const {
}

void AdlEngine::drawItems() const {
Common::Array<Item>::const_iterator item;
Common::List<Item>::const_iterator item;

uint dropped = 0;

Expand Down Expand Up @@ -435,17 +435,23 @@ Room &AdlEngine::getCurRoom() {
}

const Item &AdlEngine::getItem(uint i) const {
if (i < 1 || i > _state.items.size())
error("Item %i out of range [1, %i]", i, _state.items.size());
Common::List<Item>::const_iterator item;

return _state.items[i - 1];
for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->id == i)
return *item;

error("Item %i not found", i);
}

Item &AdlEngine::getItem(uint i) {
if (i < 1 || i > _state.items.size())
error("Item %i out of range [1, %i]", i, _state.items.size());
Common::List<Item>::iterator item;

for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->id == i)
return *item;

return _state.items[i - 1];
error("Item %i not found", i);
}

byte AdlEngine::getVar(uint i) const {
Expand All @@ -463,7 +469,7 @@ void AdlEngine::setVar(uint i, byte value) {
}

void AdlEngine::takeItem(byte noun) {
Common::Array<Item>::iterator item;
Common::List<Item>::iterator item;

for (item = _state.items.begin(); item != _state.items.end(); ++item) {
if (item->noun != noun || item->room != _state.room)
Expand Down Expand Up @@ -493,7 +499,7 @@ void AdlEngine::takeItem(byte noun) {
}

void AdlEngine::dropItem(byte noun) {
Common::Array<Item>::iterator item;
Common::List<Item>::iterator item;

for (item = _state.items.begin(); item != _state.items.end(); ++item) {
if (item->noun != noun || item->room != IDI_ANY)
Expand Down Expand Up @@ -645,12 +651,13 @@ Common::Error AdlEngine::loadGameState(int slot) {
if (size != _state.items.size())
error("Item count mismatch (expected %i; found %i)", _state.items.size(), size);

for (uint i = 0; i < size; ++i) {
_state.items[i].room = inFile->readByte();
_state.items[i].picture = inFile->readByte();
_state.items[i].position.x = inFile->readByte();
_state.items[i].position.y = inFile->readByte();
_state.items[i].state = inFile->readByte();
Common::List<Item>::iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item) {
item->room = inFile->readByte();
item->picture = inFile->readByte();
item->position.x = inFile->readByte();
item->position.y = inFile->readByte();
item->state = inFile->readByte();
}

size = inFile->readUint32BE();
Expand Down Expand Up @@ -724,12 +731,13 @@ Common::Error AdlEngine::saveGameState(int slot, const Common::String &desc) {
}

outFile->writeUint32BE(_state.items.size());
for (uint i = 0; i < _state.items.size(); ++i) {
outFile->writeByte(_state.items[i].room);
outFile->writeByte(_state.items[i].picture);
outFile->writeByte(_state.items[i].position.x);
outFile->writeByte(_state.items[i].position.y);
outFile->writeByte(_state.items[i].state);
Common::List<Item>::const_iterator item;
for (item = _state.items.begin(); item != _state.items.end(); ++item) {
outFile->writeByte(item->room);
outFile->writeByte(item->picture);
outFile->writeByte(item->position.x);
outFile->writeByte(item->position.y);
outFile->writeByte(item->state);
}

outFile->writeUint32BE(_state.vars.size());
Expand Down Expand Up @@ -982,7 +990,7 @@ int AdlEngine::o1_varSet(ScriptEnv &e) {
int AdlEngine::o1_listInv(ScriptEnv &e) {
OP_DEBUG_0("\tLIST_INVENTORY()");

Common::Array<Item>::const_iterator item;
Common::List<Item>::const_iterator item;

for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->room == IDI_ANY)
Expand Down
3 changes: 2 additions & 1 deletion engines/adl/adl.h
Expand Up @@ -139,6 +139,7 @@ enum {
};

struct Item {
byte id;
byte noun;
byte room;
byte picture;
Expand All @@ -157,7 +158,7 @@ struct Time {

struct State {
Common::Array<Room> rooms;
Common::Array<Item> items;
Common::List<Item> items;
Common::Array<byte> vars;

byte room;
Expand Down
6 changes: 3 additions & 3 deletions engines/adl/adl_v2.cpp
Expand Up @@ -213,7 +213,7 @@ int AdlEngine_v2::o2_isRandomGT(ScriptEnv &e) {
}

int AdlEngine_v2::o2_isNounNotInRoom(ScriptEnv &e) {
Common::Array<Item>::const_iterator item;
Common::List<Item>::const_iterator item;

for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->noun == e.getNoun() && (item->room == roomArg(e.arg(1))))
Expand All @@ -223,7 +223,7 @@ int AdlEngine_v2::o2_isNounNotInRoom(ScriptEnv &e) {
}

int AdlEngine_v2::o2_isCarryingSomething(ScriptEnv &e) {
Common::Array<Item>::const_iterator item;
Common::List<Item>::const_iterator item;

for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->room == IDI_ANY)
Expand All @@ -248,7 +248,7 @@ int AdlEngine_v2::o2_moveAllItems(ScriptEnv &e) {
byte room1 = roomArg(e.arg(1));
byte room2 = roomArg(e.arg(2));

Common::Array<Item>::iterator item;
Common::List<Item>::iterator item;

for (item = _state.items.begin(); item != _state.items.end(); ++item)
if (item->room == room1) {
Expand Down
4 changes: 3 additions & 1 deletion engines/adl/hires1.cpp
Expand Up @@ -233,8 +233,10 @@ void HiRes1Engine::initState() {
// Load item data from executable
_state.items.clear();
stream->seek(IDI_HR1_OFS_ITEMS);
while (stream->readByte() != 0xff) {
byte id;
while ((id = stream->readByte()) != 0xff) {
Item item = { };
item.id = id;
item.noun = stream->readByte();
item.room = stream->readByte();
item.picture = stream->readByte();
Expand Down
4 changes: 3 additions & 1 deletion engines/adl/hires2.cpp
Expand Up @@ -167,8 +167,10 @@ void HiRes2Engine::initState() {
stream.reset(_disk.createReadStream(0x21, 0x0, 0x00, 2));

_state.items.clear();
while (stream->readByte() != 0xff) {
byte id;
while ((id = stream->readByte()) != 0xff) {
Item item = { };
item.id = id;
item.noun = stream->readByte();
item.room = stream->readByte();
item.picture = stream->readByte();
Expand Down

0 comments on commit 2c8e0ce

Please sign in to comment.