Skip to content

Commit

Permalink
SHERLOCK: Replace magic numbers with enums and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dreammaster committed May 18, 2015
1 parent 4629373 commit 59993fd
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 64 deletions.
46 changes: 19 additions & 27 deletions engines/sherlock/inventory.cpp
Expand Up @@ -142,11 +142,8 @@ int Inventory::findInv(const Common::String &name) {

/**
* Display the character's inventory. The slamIt parameter specifies:
* 0 = Draw it on the back buffer, and don't display it
* 1 = Draw it on the back buffer, and then display it
* 2 = Draw it on the secondary back buffer, and don't display it
*/
void Inventory::putInv(int slamIt) {
void Inventory::putInv(InvSlamMode slamIt) {
Screen &screen = *_vm->_screen;
UserInterface &ui = *_vm->_ui;

Expand All @@ -158,7 +155,7 @@ void Inventory::putInv(int slamIt) {
loadGraphics();
}

if (slamIt != 2) {
if (slamIt != SLAM_SECONDARY_BUFFER) {
screen.makePanel(Common::Rect(6, 163, 54, 197));
screen.makePanel(Common::Rect(58, 163, 106, 197));
screen.makePanel(Common::Rect(110, 163, 158, 197));
Expand All @@ -170,13 +167,13 @@ void Inventory::putInv(int slamIt) {
// Iterate through displaying up to 6 objects at a time
for (int idx = _invIndex; idx < _holdings && (idx - _invIndex) < MAX_VISIBLE_INVENTORY; ++idx) {
int itemNum = idx - _invIndex;
Surface &bb = slamIt == 2 ? screen._backBuffer2 : screen._backBuffer1;
Surface &bb = slamIt == SLAM_SECONDARY_BUFFER ? screen._backBuffer2 : screen._backBuffer1;
Common::Rect r(8 + itemNum * 52, 165, 51 + itemNum * 52, 194);

// Draw the background
if (idx == ui._selector) {
bb.fillRect(r, 235);
} else if (slamIt == 2) {
} else if (slamIt == SLAM_SECONDARY_BUFFER) {
bb.fillRect(r, BUTTON_MIDDLE);
}

Expand All @@ -186,15 +183,15 @@ void Inventory::putInv(int slamIt) {
163 + ((33 - img.h) / 2)));
}

if (slamIt == 1)
if (slamIt == SLAM_DISPLAY)
screen.slamArea(6, 163, 308, 34);

if (slamIt != 2)
if (slamIt != SLAM_SECONDARY_BUFFER)
ui.clearInfo();

if (slamIt == 0) {
invCommands(0);
} else if (slamIt == 2) {
} else if (slamIt == SLAM_SECONDARY_BUFFER) {
screen._backBuffer = &screen._backBuffer2;
invCommands(0);
screen._backBuffer = &screen._backBuffer1;
Expand All @@ -203,20 +200,15 @@ void Inventory::putInv(int slamIt) {

/**
* Put the game into inventory mode and open the interface window.
* The flag parameter specifies the mode:
* 0 = plain inventory mode
* 2 = use inventory mode
* 3 = give inventory mode
* 128 = Draw window in the back buffer, but don't display it
*/
void Inventory::drawInventory(int flag) {
void Inventory::drawInventory(InvNewMode mode) {
Screen &screen = *_vm->_screen;
UserInterface &ui = *_vm->_ui;
int tempFlag = flag;
InvNewMode tempMode = mode;

loadInv();

if (flag == 128) {
if (mode == INVENTORY_DONT_DISPLAY) {
screen._backBuffer = &screen._backBuffer2;
}

Expand Down Expand Up @@ -249,20 +241,20 @@ void Inventory::drawInventory(int flag) {
screen.makeButton(Common::Rect(INVENTORY_POINTS[7][0], CONTROLS_Y1, INVENTORY_POINTS[7][1],
CONTROLS_Y1 + 10), INVENTORY_POINTS[7][2], "__");

if (tempFlag == 128)
flag = 1;
_invMode = (InvMode)flag;
if (tempMode == INVENTORY_DONT_DISPLAY)
mode = LOOK_INVENTORY_MODE;
_invMode = (InvMode)mode;

if (flag) {
ui._oldKey = INVENTORY_COMMANDS[flag];
if (mode != PLAIN_INVENTORY) {
ui._oldKey = INVENTORY_COMMANDS[(int)mode];
} else {
ui._oldKey = -1;
}

invCommands(0);
putInv(0);
putInv(SLAM_DONT_DISPLAY);

if (tempFlag != 128) {
if (tempMode != INVENTORY_DONT_DISPLAY) {
if (!ui._windowStyle) {
screen.slamRect(Common::Rect(0, CONTROLS_Y1, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT));
} else {
Expand Down Expand Up @@ -411,11 +403,11 @@ int Inventory::putItemInInventory(Object &obj) {
if (obj._pickupFlag)
_vm->setFlags(obj._pickupFlag);

for (int useNum = 0; useNum < 4; ++useNum) {
for (int useNum = 0; useNum < USE_COUNT; ++useNum) {
if (obj._use[useNum]._target.equalsIgnoreCase("*PICKUP*")) {
pickupFound = true;

for (int namesNum = 0; namesNum < 4; ++namesNum) {
for (int namesNum = 0; namesNum < NAMES_COUNT; ++namesNum) {
for (uint bgNum = 0; bgNum < scene._bgShapes.size(); ++bgNum) {
Object &bgObj = scene._bgShapes[bgNum];
if (obj._use[useNum]._names[namesNum].equalsIgnoreCase(bgObj._name)) {
Expand Down
12 changes: 10 additions & 2 deletions engines/sherlock/inventory.h
Expand Up @@ -47,6 +47,14 @@ enum InvMode {
INVMODE_USE55 = 255
};

enum InvNewMode {
PLAIN_INVENTORY = 0, LOOK_INVENTORY_MODE = 1, USE_INVENTORY_MODE = 2,
GIVE_INVENTORY_MODE = 3, INVENTORY_DONT_DISPLAY = 128
};

enum InvSlamMode { SLAM_DONT_DISPLAY, SLAM_DISPLAY = 1, SLAM_SECONDARY_BUFFER };


struct InventoryItem {
int _requiredFlag;
Common::String _name;
Expand Down Expand Up @@ -87,9 +95,9 @@ class Inventory : public Common::Array<InventoryItem> {

int findInv(const Common::String &name);

void putInv(int slamIt);
void putInv(InvSlamMode slamIt);

void drawInventory(int flag);
void drawInventory(InvNewMode flag);

void invCommands(bool slamIt);

Expand Down
18 changes: 9 additions & 9 deletions engines/sherlock/objects.cpp
Expand Up @@ -375,7 +375,7 @@ void ActionType::load(Common::SeekableReadStream &s) {
if (_cAnimSpeed & 0x80)
_cAnimSpeed = -(_cAnimSpeed & 0x7f);

for (int idx = 0; idx < 4; ++idx) {
for (int idx = 0; idx < NAMES_COUNT; ++idx) {
s.read(buffer, 12);
_names[idx] = Common::String(buffer);
}
Expand All @@ -399,7 +399,7 @@ void UseType::load(Common::SeekableReadStream &s) {
if (_cAnimSpeed & 0x80)
_cAnimSpeed = -(_cAnimSpeed & 0x7f);

for (int idx = 0; idx < 4; ++idx) {
for (int idx = 0; idx < NAMES_COUNT; ++idx) {
s.read(buffer, 12);
_names[idx] = Common::String(buffer);
}
Expand Down Expand Up @@ -518,7 +518,7 @@ void Object::load(Common::SeekableReadStream &s) {
_aMove.load(s);
s.skip(8);

for (int idx = 0; idx < 4; ++idx)
for (int idx = 0; idx < USE_COUNT; ++idx)
_use[idx].load(s);
}

Expand Down Expand Up @@ -671,8 +671,8 @@ void Object::checkObject() {

_delta = pt;
_frameNumber += 2;
} else if (v < 4) {
for (int idx = 0; idx < 4; ++idx) {
} else if (v < USE_COUNT) {
for (int idx = 0; idx < NAMES_COUNT; ++idx) {
checkNameForCodes(_use[v]._names[idx], nullptr);
}

Expand Down Expand Up @@ -928,7 +928,7 @@ void Object::setFlagsAndToggles() {
Scene &scene = *_vm->_scene;
Talk &talk = *_vm->_talk;

for (int useIdx = 0; useIdx < 4; ++useIdx) {
for (int useIdx = 0; useIdx < USE_COUNT; ++useIdx) {
if (_use[useIdx]._useFlag) {
if (!_vm->readFlags(_use[useIdx]._useFlag))
_vm->setFlags(_use[useIdx]._useFlag);
Expand All @@ -943,7 +943,7 @@ void Object::setFlagsAndToggles() {
}

if (!talk._talkToAbort) {
for (int idx = 0; idx < 4; ++idx)
for (int idx = 0; idx < NAMES_COUNT; ++idx)
scene.toggleObject(_use[useIdx]._names[idx]);
}
}
Expand Down Expand Up @@ -991,7 +991,7 @@ int Object::pickUpObject(const char *const messages[]) {
int numObjects = 0;

if (pickup == 99) {
for (int idx = 0; idx < 4 && !talk._talkToAbort; ++idx) {
for (int idx = 0; idx < NAMES_COUNT && !talk._talkToAbort; ++idx) {
if (checkNameForCodes(_use[0]._names[idx], nullptr)) {
if (!talk._talkToAbort)
printed = true;
Expand Down Expand Up @@ -1038,7 +1038,7 @@ int Object::pickUpObject(const char *const messages[]) {
ui._temp1 = 1;
}

for (int idx = 0; idx < 4 && !talk._talkToAbort; ++idx) {
for (int idx = 0; idx < NAMES_COUNT && !talk._talkToAbort; ++idx) {
if (checkNameForCodes(_use[0]._names[idx], nullptr)) {
if (!talk._talkToAbort)
printed = true;
Expand Down
9 changes: 6 additions & 3 deletions engines/sherlock/objects.h
Expand Up @@ -142,19 +142,20 @@ class Sprite {
};

enum { REVERSE_DIRECTION = 0x80 };
#define NAMES_COUNT 4

struct ActionType {
int _cAnimNum;
int _cAnimSpeed;
Common::String _names[4];
Common::String _names[NAMES_COUNT];

void load(Common::SeekableReadStream &s);
};

struct UseType {
int _cAnimNum;
int _cAnimSpeed;
Common::String _names[4];
Common::String _names[NAMES_COUNT];
int _useFlag; // Which flag USE will set (if any)
Common::String _target;

Expand All @@ -163,6 +164,8 @@ struct UseType {
};

enum { TURNON_OBJ = 0x20, TURNOFF_OBJ = 0x40 };
#define USE_COUNT 4

class Object {
private:
static SherlockEngine *_vm;
Expand Down Expand Up @@ -217,7 +220,7 @@ class Object {
int _seqCounter2; // Counter of calling frame sequence
uint _seqSize; // Tells where description starts
ActionType _aMove;
UseType _use[4];
UseType _use[USE_COUNT];

Object();

Expand Down
9 changes: 5 additions & 4 deletions engines/sherlock/scalpel/darts.cpp
Expand Up @@ -44,9 +44,10 @@ enum {
DART_COL_FORE = 5,
PLAYER_COLOR = 11
};
#define OPPONENTS_COUNT 4

const char *const OPPONENT_NAMES[5] = {
"Skipper", "Willy", "Micky", "Tom", "Bartender"
const char *const OPPONENT_NAMES[OPPONENTS_COUNT] = {
"Skipper", "Willy", "Micky", "Tom"
};

/*----------------------------------------------------------------*/
Expand Down Expand Up @@ -118,7 +119,7 @@ void Darts::playDarts() {

if (playerNumber == 0) {
screen.print(Common::Point(DART_INFO_X, DART_INFO_Y + 30), PLAYER_COLOR, "Holmes Wins!");
if (_level < 4)
if (_level < OPPONENTS_COUNT)
setFlagsForDarts(318 + _level);
} else {
screen.print(Common::Point(DART_INFO_X, DART_INFO_Y + 30), PLAYER_COLOR, "%s Wins!", _opponent.c_str());
Expand Down Expand Up @@ -210,7 +211,7 @@ void Darts::initDarts() {
_computerPlayer = 2;
} else {
// Check flags for opponents
for (int idx = 0; idx < 4; ++idx) {
for (int idx = 0; idx < OPPONENTS_COUNT; ++idx) {
if (_vm->readFlags(314 + idx))
_level = idx;
}
Expand Down
4 changes: 2 additions & 2 deletions engines/sherlock/scene.cpp
Expand Up @@ -706,14 +706,14 @@ void Scene::transitionToScene() {
// player is clear of the box
switch (obj._aType) {
case FLAG_SET:
for (int useNum = 0; useNum < 4; ++useNum) {
for (int useNum = 0; useNum < USE_COUNT; ++useNum) {
if (obj._use[useNum]._useFlag) {
if (!_vm->readFlags(obj._use[useNum]._useFlag))
_vm->setFlags(obj._use[useNum]._useFlag);
}

if (!talk._talkToAbort) {
for (int nameIdx = 0; nameIdx < 4; ++nameIdx) {
for (int nameIdx = 0; nameIdx < NAMES_COUNT; ++nameIdx) {
toggleObject(obj._use[useNum]._names[nameIdx]);
}
}
Expand Down

0 comments on commit 59993fd

Please sign in to comment.