From 59993fdc74afad7b210da7849f8ce25631153201 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Mon, 18 May 2015 19:15:17 -0400 Subject: SHERLOCK: Replace magic numbers with enums and constants --- engines/sherlock/inventory.cpp | 46 +++++++++++++++---------------------- engines/sherlock/inventory.h | 12 ++++++++-- engines/sherlock/objects.cpp | 18 +++++++-------- engines/sherlock/objects.h | 9 +++++--- engines/sherlock/scalpel/darts.cpp | 9 ++++---- engines/sherlock/scene.cpp | 4 ++-- engines/sherlock/user_interface.cpp | 34 +++++++++++++-------------- 7 files changed, 68 insertions(+), 64 deletions(-) (limited to 'engines') diff --git a/engines/sherlock/inventory.cpp b/engines/sherlock/inventory.cpp index b7d973e289..283164c534 100644 --- a/engines/sherlock/inventory.cpp +++ b/engines/sherlock/inventory.cpp @@ -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; @@ -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)); @@ -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); } @@ -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; @@ -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; } @@ -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 { @@ -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)) { diff --git a/engines/sherlock/inventory.h b/engines/sherlock/inventory.h index eb5aebdd7c..979507a387 100644 --- a/engines/sherlock/inventory.h +++ b/engines/sherlock/inventory.h @@ -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; @@ -87,9 +95,9 @@ public: 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); diff --git a/engines/sherlock/objects.cpp b/engines/sherlock/objects.cpp index 05a058a3bb..310afbf94b 100644 --- a/engines/sherlock/objects.cpp +++ b/engines/sherlock/objects.cpp @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); @@ -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]); } } @@ -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; @@ -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; diff --git a/engines/sherlock/objects.h b/engines/sherlock/objects.h index 4e7b006844..0b3a26f1f9 100644 --- a/engines/sherlock/objects.h +++ b/engines/sherlock/objects.h @@ -142,11 +142,12 @@ public: }; 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); }; @@ -154,7 +155,7 @@ struct ActionType { 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; @@ -163,6 +164,8 @@ struct UseType { }; enum { TURNON_OBJ = 0x20, TURNOFF_OBJ = 0x40 }; +#define USE_COUNT 4 + class Object { private: static SherlockEngine *_vm; @@ -217,7 +220,7 @@ public: int _seqCounter2; // Counter of calling frame sequence uint _seqSize; // Tells where description starts ActionType _aMove; - UseType _use[4]; + UseType _use[USE_COUNT]; Object(); diff --git a/engines/sherlock/scalpel/darts.cpp b/engines/sherlock/scalpel/darts.cpp index 6da8d6848e..29b67216b9 100644 --- a/engines/sherlock/scalpel/darts.cpp +++ b/engines/sherlock/scalpel/darts.cpp @@ -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" }; /*----------------------------------------------------------------*/ @@ -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()); @@ -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; } diff --git a/engines/sherlock/scene.cpp b/engines/sherlock/scene.cpp index b75531baf6..b092bbce65 100644 --- a/engines/sherlock/scene.cpp +++ b/engines/sherlock/scene.cpp @@ -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]); } } diff --git a/engines/sherlock/user_interface.cpp b/engines/sherlock/user_interface.cpp index e2ad307aa4..ffcbddb920 100644 --- a/engines/sherlock/user_interface.cpp +++ b/engines/sherlock/user_interface.cpp @@ -1156,7 +1156,7 @@ void UserInterface::doInvControl() { COMMAND_HIGHLIGHTED, "^^"); inv.freeGraphics(); inv.loadGraphics(); - inv.putInv(1); + inv.putInv(SLAM_DISPLAY); inv.invCommands(true); } else if (((found == 5 && events._released) || _key == Common::KEYCODE_MINUS || _key == Common::KEYCODE_KP_MINUS) && inv._invIndex > 0) { @@ -1164,7 +1164,7 @@ void UserInterface::doInvControl() { screen.print(Common::Point(INVENTORY_POINTS[4][2], CONTROLS_Y1 + 1), COMMAND_HIGHLIGHTED, "^"); inv.freeGraphics(); inv.loadGraphics(); - inv.putInv(1); + inv.putInv(SLAM_DISPLAY); inv.invCommands(true); } else if (((found == 6 && events._released) || _key == Common::KEYCODE_PLUS || _key == Common::KEYCODE_KP_PLUS) && (inv._holdings - inv._invIndex) > 6) { @@ -1172,7 +1172,7 @@ void UserInterface::doInvControl() { screen.print(Common::Point(INVENTORY_POINTS[6][2], CONTROLS_Y1 + 1), COMMAND_HIGHLIGHTED, "_"); inv.freeGraphics(); inv.loadGraphics(); - inv.putInv(1); + inv.putInv(SLAM_DISPLAY); inv.invCommands(true); } else if (((found == 7 && events._released) || _key == '.') && (inv._holdings - inv._invIndex) > 6) { inv._invIndex += 6; @@ -1182,7 +1182,7 @@ void UserInterface::doInvControl() { screen.print(Common::Point(INVENTORY_POINTS[7][2], CONTROLS_Y1 + 1), COMMAND_HIGHLIGHTED, "_"); inv.freeGraphics(); inv.loadGraphics(); - inv.putInv(1); + inv.putInv(SLAM_DISPLAY); inv.invCommands(true); } else { // If something is being given, make sure it's to a person @@ -1226,7 +1226,7 @@ void UserInterface::doInvControl() { int temp = _selector; // Save the selector _selector = -1; - inv.putInv(1); + inv.putInv(SLAM_DISPLAY); _selector = temp; // Restore it temp = inv._invMode; inv._invMode = INVMODE_USE55; @@ -1300,7 +1300,7 @@ void UserInterface::doLookControl() { tempSurface.blitFrom(screen._backBuffer2, Common::Point(0, 0), Common::Rect(0, CONTROLS_Y1, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT)); - inv.drawInventory(128); + inv.drawInventory(INVENTORY_DONT_DISPLAY); banishWindow(true); // Restore the ui @@ -1405,19 +1405,19 @@ void UserInterface::doMainControl() { pushButton(6); _selector = _oldSelector = -1; _menuMode = INV_MODE; - inv.drawInventory(1); + inv.drawInventory(PLAIN_INVENTORY); break; case 'U': pushButton(7); _selector = _oldSelector = -1; _menuMode = USE_MODE; - inv.drawInventory(2); + inv.drawInventory(USE_INVENTORY_MODE); break; case 'G': pushButton(8); _selector = _oldSelector = -1; _menuMode = GIVE_MODE; - inv.drawInventory(3); + inv.drawInventory(GIVE_INVENTORY_MODE); break; case 'J': pushButton(9); @@ -1904,7 +1904,7 @@ void UserInterface::printObjectDesc(const Common::String &str, bool firstTime) { // Reload the inventory graphics and draw the inventory inv.loadInv(); - inv.putInv(2); + inv.putInv(SLAM_SECONDARY_BUFFER); inv.freeInv(); banishWindow(1); @@ -2182,7 +2182,7 @@ void UserInterface::checkUseAction(const UseType *use, const Common::String &inv // Scan for target item int targetNum = -1; if (giveMode) { - for (int idx = 0; idx < 4 && targetNum == -1; ++idx) { + for (int idx = 0; idx < USE_COUNT && targetNum == -1; ++idx) { if ((use[idx]._target.equalsIgnoreCase("*GIVE*") || use[idx]._target.equalsIgnoreCase("*GIVEP*")) && use[idx]._names[0].equalsIgnoreCase(invName)) { // Found a match @@ -2192,7 +2192,7 @@ void UserInterface::checkUseAction(const UseType *use, const Common::String &inv } } } else { - for (int idx = 0; idx < 4 && targetNum == -1; ++idx) { + for (int idx = 0; idx < USE_COUNT && targetNum == -1; ++idx) { if (use[idx]._target.equalsIgnoreCase(invName)) targetNum = idx; } @@ -2216,7 +2216,7 @@ void UserInterface::checkUseAction(const UseType *use, const Common::String &inv if (!talk._talkToAbort) { Object &obj = scene._bgShapes[objNum]; - for (int idx = 0; idx < 4 && !talk._talkToAbort; ++idx) { + for (int idx = 0; idx < NAMES_COUNT && !talk._talkToAbort; ++idx) { if (obj.checkNameForCodes(action._names[idx], messages)) { if (!talk._talkToAbort) printed = true; @@ -2308,7 +2308,7 @@ void UserInterface::checkAction(ActionType &action, const char *const messages[] events.setCursor(WAIT); bool printed = false; - for (int nameIdx = 0; nameIdx < 4; ++nameIdx) { + for (int nameIdx = 0; nameIdx < NAMES_COUNT; ++nameIdx) { if (action._names[nameIdx].hasPrefix("*") && action._names[nameIdx].size() >= 2 && toupper(action._names[nameIdx][1]) == 'W') { if (obj.checkNameForCodes(Common::String(action._names[nameIdx].c_str() + 2), messages)) { @@ -2319,7 +2319,7 @@ void UserInterface::checkAction(ActionType &action, const char *const messages[] } bool doCAnim = true; - for (int nameIdx = 0; nameIdx < 4; ++nameIdx) { + for (int nameIdx = 0; nameIdx < NAMES_COUNT; ++nameIdx) { if (action._names[nameIdx].hasPrefix("*") && action._names[nameIdx].size() >= 2) { char ch = toupper(action._names[nameIdx][1]); @@ -2349,7 +2349,7 @@ void UserInterface::checkAction(ActionType &action, const char *const messages[] people.walkToCoords(pt, dir); } - for (int nameIdx = 0; nameIdx < 4; ++nameIdx) { + for (int nameIdx = 0; nameIdx < NAMES_COUNT; ++nameIdx) { if (action._names[nameIdx].hasPrefix("*") && action._names[nameIdx].size() >= 2 && toupper(action._names[nameIdx][1]) == 'F') { if (obj.checkNameForCodes(action._names[nameIdx].c_str() + 2, messages)) { @@ -2363,7 +2363,7 @@ void UserInterface::checkAction(ActionType &action, const char *const messages[] scene.startCAnim(cAnimNum, action._cAnimSpeed); if (!talk._talkToAbort) { - for (int nameIdx = 0; nameIdx < 4 && !talk._talkToAbort; ++nameIdx) { + for (int nameIdx = 0; nameIdx < NAMES_COUNT && !talk._talkToAbort; ++nameIdx) { if (obj.checkNameForCodes(action._names[nameIdx], messages)) { if (!talk._talkToAbort) printed = true; -- cgit v1.2.3