diff options
author | Strangerke | 2019-07-23 08:28:10 +0200 |
---|---|---|
committer | Eugene Sandulenko | 2019-09-03 17:17:26 +0200 |
commit | 4b65e396c801e1a08c89de79ebfeaf2f20ada0a0 (patch) | |
tree | 102533b4424a370fa2b1c5c589053165d1c9bead /engines | |
parent | bf57f7befb5af0c7f3d95b96d6fd1f8a46c056ef (diff) | |
download | scummvm-rg350-4b65e396c801e1a08c89de79ebfeaf2f20ada0a0.tar.gz scummvm-rg350-4b65e396c801e1a08c89de79ebfeaf2f20ada0a0.tar.bz2 scummvm-rg350-4b65e396c801e1a08c89de79ebfeaf2f20ada0a0.zip |
HDB: Reduce some variable scopes, some janitorial work
Diffstat (limited to 'engines')
-rw-r--r-- | engines/hdb/gfx.cpp | 23 | ||||
-rw-r--r-- | engines/hdb/hdb.cpp | 5 | ||||
-rw-r--r-- | engines/hdb/input.cpp | 31 | ||||
-rw-r--r-- | engines/hdb/lua-script.cpp | 162 |
4 files changed, 98 insertions, 123 deletions
diff --git a/engines/hdb/gfx.cpp b/engines/hdb/gfx.cpp index 9dc3d3b76c..641f1790d6 100644 --- a/engines/hdb/gfx.cpp +++ b/engines/hdb/gfx.cpp @@ -734,9 +734,9 @@ bool Gfx::loadFont(const char *string) { debug(3, "leading: %d", _fontHeader.leading); // Loading _charInfoBlocks & creating character surfaces - int startPos = stream->pos(); // Position after _fontHeader - int curPos; // Position after reading cInfo - uint16 *ptr; + + // Position after _fontHeader + int startPos = stream->pos(); for (int i = 0; i < _fontHeader.numChars; i++) { CharInfo *cInfo = new CharInfo; cInfo->width = (int16)stream->readUint32LE(); @@ -744,7 +744,8 @@ bool Gfx::loadFont(const char *string) { debug(3, "Loaded _charInfoBlocks[%d]: width: %d, offset: %d", i, cInfo->width, cInfo->offset); - curPos = stream->pos(); + // Position after reading cInfo + int curPos = stream->pos(); _fontSurfaces[i].create(cInfo->width, _fontHeader.height, g_hdb->_format); @@ -752,7 +753,7 @@ bool Gfx::loadFont(const char *string) { stream->seek(startPos+cInfo->offset); for (int y = 0; y < _fontHeader.height; y++) { - ptr = (uint16 *)_fontSurfaces[i].getBasePtr(0, y); + uint16 *ptr = (uint16 *)_fontSurfaces[i].getBasePtr(0, y); for (int x = 0; x < cInfo->width; x++) { *ptr = TO_LE_16(stream->readUint16LE()); ptr++; @@ -782,11 +783,10 @@ void Gfx::drawText(const char *string) { // Word Wrapping int width = _eLeft; - unsigned char c; char cr[256]; // Carriage Return Array for (int i = 0; i < (int)strlen(string); i++) { - c = string[i]; + unsigned char c = string[i]; width += _charInfoBlocks[c]->width + _fontHeader.kerning + kFontIncrement; if (c == ' ') width += kFontSpace; @@ -806,7 +806,7 @@ void Gfx::drawText(const char *string) { // Draw the characters for (int j = 0; j < (int)strlen(string); j++) { - c = string[j]; + unsigned char c = string[j]; if (c == '\n' || cr[j]) { _cursorX = _eLeft; _cursorY += _fontHeader.height + _fontHeader.leading; @@ -848,10 +848,9 @@ void Gfx::getDimensions(const char *string, int *pixelsWide, int *lines) { return; } - int width, maxWidth, height; - maxWidth = 0; - width = _eLeft; - height = 1; + int maxWidth = 0; + int width = _eLeft; + int height = 1; for (int i = 0; i < (int)strlen(string); i++) { unsigned char c = string[i]; diff --git a/engines/hdb/hdb.cpp b/engines/hdb/hdb.cpp index 837bb962d3..3fb963af5a 100644 --- a/engines/hdb/hdb.cpp +++ b/engines/hdb/hdb.cpp @@ -908,10 +908,7 @@ Common::Error HDBGame::run() { lua->executeFile("test.lua"); #endif - AIEntity *e; - while (!shouldQuit()) { - Common::Event event; while (g_system->getEventManager()->pollEvent(event)) { switch (event.type) { @@ -960,7 +957,7 @@ Common::Error HDBGame::run() { _ai->processCines(); //_window->drawDialog(); - e = _ai->getPlayer(); + AIEntity *e = _ai->getPlayer(); if (e && e->level < 2) _ai->drawWayPoints(); diff --git a/engines/hdb/input.cpp b/engines/hdb/input.cpp index 91b3a97589..e345032368 100644 --- a/engines/hdb/input.cpp +++ b/engines/hdb/input.cpp @@ -121,8 +121,6 @@ uint16 Input::getButtons() { } void Input::stylusDown(int x, int y) { - int worldX, worldY; - GameState gs; static uint32 delay = 0, time; // Don't let the screen get clicked too fast @@ -134,7 +132,7 @@ void Input::stylusDown(int x, int y) { _stylusDown = true; _stylusDownX = x; _stylusDownY = y; - gs = g_hdb->getGameState(); + GameState gs = g_hdb->getGameState(); switch (gs) { case GAME_TITLE: @@ -183,9 +181,9 @@ void Input::stylusDown(int x, int y) { // Check for map dragging in debug Mode and place player there if ((GAME_PLAY == g_hdb->getGameState()) && g_hdb->getDebug() == 2) { - int mx, my; - + int mx, my; g_hdb->_map->getMapXY(&mx, &my); + mx = ((mx + _stylusDownY) / kTileWidth) * kTileWidth; my = ((my + _stylusDownY) / kTileHeight) * kTileHeight; g_hdb->_ai->setPlayerXY(mx, my); @@ -195,6 +193,7 @@ void Input::stylusDown(int x, int y) { } // Clicked in the world + int worldX, worldY; g_hdb->_map->getMapXY(&worldX, &worldY); worldX = ((worldX + x) / kTileWidth) * kTileWidth; worldY = ((worldY + y) / kTileHeight) * kTileHeight; @@ -317,6 +316,7 @@ void Input::updateMouseButtons(int l, int m, int r) { } void Input::updateKeys(Common::Event event, bool keyDown) { + static int current = 0, last = 0; if (keyDown && event.kbd.keycode == _keyQuit) { g_hdb->quitGame(); @@ -326,18 +326,15 @@ void Input::updateKeys(Common::Event event, bool keyDown) { uint16 buttons = getButtons(); // PAUSE key pressed? - { - static int current = 0, last = 0; - last = current; - if (keyDown && event.kbd.keycode == Common::KEYCODE_p && g_hdb->getGameState() == GAME_PLAY) { - current = 1; - if (!last) { - g_hdb->togglePause(); - g_hdb->_sound->playSound(SND_POP); - } - } else - current = 0; - } + last = current; + if (keyDown && event.kbd.keycode == Common::KEYCODE_p && g_hdb->getGameState() == GAME_PLAY) { + current = 1; + if (!last) { + g_hdb->togglePause(); + g_hdb->_sound->playSound(SND_POP); + } + } else + current = 0; if (!g_hdb->getPause()) { if (event.kbd.keycode == _keyUp) { diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp index abe3c3f78c..c0e3a57b1f 100644 --- a/engines/hdb/lua-script.cpp +++ b/engines/hdb/lua-script.cpp @@ -95,9 +95,9 @@ LuaScript::LuaScript() { } LuaScript::~LuaScript() { - if (_state) { + if (_state) lua_close(_state); - } + if (_globalLuaStream) delete _globalLuaStream; } @@ -206,7 +206,8 @@ void LuaScript::save(Common::OutSaveFile *out) { lua_printstack(_state); lua_getglobal(_state, "SaveState"); - lua_pushstring(_state, "tempSave"); // the save file will be ignored + // the save file will be ignored + lua_pushstring(_state, "tempSave"); lua_call(_state, 1, 0); g_hdb->_currentOutSaveFile = NULL; @@ -232,7 +233,8 @@ void LuaScript::loadSaveFile(Common::InSaveFile *in) { g_hdb->_currentInSaveFile = in; lua_getglobal(_state, "LoadState"); - lua_pushstring(_state, "tempSave"); // it will be ignored + // it will be ignored + lua_pushstring(_state, "tempSave"); lua_call(_state, 1, 0); @@ -405,8 +407,8 @@ static int cinePlaySound(lua_State *L) { } static int cinePlayVoice(lua_State *L) { - double index = lua_tonumber(L, 1); - double actor = lua_tonumber(L, 2); + double index = lua_tonumber(L, 1); + double actor = lua_tonumber(L, 2); g_hdb->_lua->checkParameters("cinePlayVoice", 2); @@ -700,14 +702,11 @@ static int cineCenterTextOut(lua_State *L) { } static int newDelivery(lua_State *L) { - const char *itemTextName, *itemGfxName; - const char *destTextName, *destGfxName, *id; - - itemTextName = lua_tostring(L, 1); - itemGfxName = lua_tostring(L, 2); - destTextName = lua_tostring(L, 3); - destGfxName = lua_tostring(L, 4); - id = lua_tostring(L, 5); + const char *itemTextName = lua_tostring(L, 1); + const char *itemGfxName = lua_tostring(L, 2); + const char *destTextName = lua_tostring(L, 3); + const char *destGfxName = lua_tostring(L, 4); + const char *id = lua_tostring(L, 5); g_hdb->_lua->checkParameters("newDelivery", 5); @@ -736,13 +735,13 @@ static int deliveriesLeft(lua_State *L) { } static int getEntityXY(lua_State *L) { - int x, y; const char *initName = lua_tostring(L, 1); g_hdb->_lua->checkParameters("getEntityXY", 1); lua_pop(L, 1); + int x, y; g_hdb->_ai->getEntityXY(initName, &x, &y); lua_pushnumber(L, x); @@ -751,8 +750,6 @@ static int getEntityXY(lua_State *L) { } static int setEntity(lua_State *L) { - AIEntity *e = NULL; - const char *entName = lua_tostring(L, 1); double x = lua_tonumber(L, 2); double y = lua_tonumber(L, 3); @@ -761,7 +758,7 @@ static int setEntity(lua_State *L) { g_hdb->_lua->checkParameters("setEntity", 4); lua_pop(L, 4); - e = g_hdb->_ai->locateEntity(entName); + AIEntity *e = g_hdb->_ai->locateEntity(entName); if (e) { e->x = (int)x * kTileWidth; e->tileX = (int)x; @@ -777,21 +774,19 @@ static int setEntity(lua_State *L) { } static int setEntDir(lua_State *L) { - AIEntity *e; - char buff[64]; - const char *entName = lua_tostring(L, 1); double d = lua_tonumber(L, 2); g_hdb->_lua->checkParameters("setEntDir", 2); lua_pop(L, 2); - e = g_hdb->_ai->locateEntity(entName); + AIEntity *e = g_hdb->_ai->locateEntity(entName); if (e) { int dd = (int)d; e->dir = (AIDir)dd; } else { + char buff[64]; sprintf(buff, "Could not SetEntDir on '%s'", entName); g_hdb->_window->openMessageBar(buff, 10); } @@ -908,13 +903,10 @@ static int setBackground(lua_State *L) { } static int dialog(lua_State *L) { - const char *title, *string, *more; - double tileIndex; - - title = lua_tostring(L, 1); - tileIndex = lua_tonumber(L, 2); - string = lua_tostring(L, 3); - more = lua_tostring(L, 4); + const char *title = lua_tostring(L, 1); + double tileIndex = lua_tonumber(L, 2); + const char *string = lua_tostring(L, 3); + const char *more = lua_tostring(L, 4); if (!more || more[0] == '0') more = NULL; @@ -933,11 +925,11 @@ static int dialogChoice(lua_State *L) { const char *func = lua_tostring(L, 3); const char *choice[10] = {0,0,0,0,0,0,0,0,0,0}; - int i, amount = lua_gettop(L) - 3; + int amount = lua_gettop(L) - 3; if (amount > 9) amount = 9; - for (i = 0; i < amount; i++) + for (int i = 0; i < amount; i++) choice[i] = lua_tostring(L, 4 + i); lua_pop(L, amount + 3); @@ -947,12 +939,8 @@ static int dialogChoice(lua_State *L) { } static int message(lua_State *L) { - const char *title; - double delay; - - title = lua_tostring(L, 1); - delay = lua_tonumber(L, 2); - + const char *title = lua_tostring(L, 1); + double delay = lua_tonumber(L, 2); g_hdb->_lua->checkParameters("message", 2); @@ -963,10 +951,10 @@ static int message(lua_State *L) { } static int animation(lua_State *L) { - double x = lua_tonumber(L, 1); - double y = lua_tonumber(L, 2); - double which = lua_tonumber(L, 3); - double playsnd = lua_tonumber(L, 4); + double x = lua_tonumber(L, 1); + double y = lua_tonumber(L, 2); + double which = lua_tonumber(L, 3); + double playsnd = lua_tonumber(L, 4); g_hdb->_lua->checkParameters("animation", 4); @@ -1012,16 +1000,16 @@ static int animation(lua_State *L) { static int spawnEntity(lua_State *L) { double type = lua_tonumber(L, 1); - double dir = lua_tonumber(L, 2); - double x = lua_tonumber(L, 3); - double y = lua_tonumber(L, 4); + double dir = lua_tonumber(L, 2); + double x = lua_tonumber(L, 3); + double y = lua_tonumber(L, 4); const char *funcInit = lua_tostring(L, 5); const char *funcAction = lua_tostring(L, 6); const char *funcUse = lua_tostring(L, 7); - double dir2 = lua_tonumber(L, 8); - double level = lua_tonumber(L, 9); - double value1 = lua_tonumber(L, 10); - double value2 = lua_tonumber(L, 11); + double dir2 = lua_tonumber(L, 8); + double level = lua_tonumber(L, 9); + double value1 = lua_tonumber(L, 10); + double value2 = lua_tonumber(L, 11); int t = (int)type; int d = (int)dir; @@ -1036,8 +1024,8 @@ static int spawnEntity(lua_State *L) { } static int addInvItem(lua_State *L) { - double type = lua_tonumber(L, 1); - double amount = lua_tonumber(L, 2); + double type = lua_tonumber(L, 1); + double amount = lua_tonumber(L, 2); const char *funcInit = lua_tostring(L, 3); const char *funcAction = lua_tostring(L, 4); const char *funcUse = lua_tostring(L, 5); @@ -1067,17 +1055,17 @@ static int keepInvItem(lua_State *L) { } static int queryInv(lua_State *L) { - const char *search; - int result; - - search = lua_tostring(L, 1); // get the passed-in search string + // get the passed-in search string + const char *search = lua_tostring(L, 1); g_hdb->_lua->checkParameters("queryInv", 1); lua_pop(L, 1); - result = g_hdb->_ai->queryInventory(search); // call the function & get return value - lua_pushnumber(L, result); // send the return value back to Lua + // call the function & get return value + int result = g_hdb->_ai->queryInventory(search); + // send the return value back to Lua + lua_pushnumber(L, result); return 1; } @@ -1087,41 +1075,40 @@ static int purgeInv(lua_State *L) { } static int queryInvItem(lua_State *L) { - double search; - int result, s1; - - search = lua_tonumber(L, 1); // get the passed-in search string - s1 = (int)search; + // get the passed-in search string + double search = lua_tonumber(L, 1); + int s1 = (int)search; g_hdb->_lua->checkParameters("queryInvItem", 1); lua_pop(L, 1); - result = g_hdb->_ai->queryInventoryType((AIType)s1); // call the function & get return value - lua_pushnumber(L, result); // send the return value back to Lua + // call the function & get return value + int result = g_hdb->_ai->queryInventoryType((AIType)s1); + // send the return value back to Lua + lua_pushnumber(L, result); return 1; } static int removeInv(lua_State *L) { - const char *search; - int result; - - search = lua_tostring(L, 1); // get the passed-in search string + // get the passed-in search string + const char *search = lua_tostring(L, 1); double number = lua_tonumber(L, 2); g_hdb->_lua->checkParameters("removeInv", 2); lua_pop(L, 2); - result = (int)g_hdb->_ai->removeInvItem(search, (int)number); // call the function & get return value - lua_pushnumber(L, result); // send the return value back to Lua + // call the function & get return value + int result = (int)g_hdb->_ai->removeInvItem(search, (int)number); + // send the return value back to Lua + lua_pushnumber(L, result); return 1; } static int removeInvItem(lua_State *L) { - int result; - - double search = lua_tonumber(L, 1); // get the passed-in type value + // get the passed-in type value + double search = lua_tonumber(L, 1); double number = lua_tonumber(L, 2); g_hdb->_lua->checkParameters("removeInvItem", 2); @@ -1129,8 +1116,10 @@ static int removeInvItem(lua_State *L) { lua_pop(L, 2); int s = (int)search; - result = (int)g_hdb->_ai->removeInvItemType((AIType)s, (int)number); // call the function & get return value - lua_pushnumber(L, result); // send the return value back to Lua + // call the function & get return value + int result = (int)g_hdb->_ai->removeInvItemType((AIType)s, (int)number); + // send the return value back to Lua + lua_pushnumber(L, result); return 1; } @@ -1145,22 +1134,18 @@ static int killTrigger(lua_State *L) { } static int startMusic(lua_State *L) { - bool error; - double song = lua_tonumber(L, 1); int s1 = (int)song; g_hdb->_lua->checkParameters("startMusic", 1); lua_pop(L, 1); - error = g_hdb->_sound->startMusic((SoundType)s1); + bool error = g_hdb->_sound->startMusic((SoundType)s1); return 0; } static int fadeInMusic(lua_State *L) { - bool error; - double song = lua_tonumber(L, 1); int s1 = (int)song; int ramp = (int)lua_tonumber(L, 2); @@ -1170,7 +1155,7 @@ static int fadeInMusic(lua_State *L) { g_hdb->_lua->checkParameters("fadeInMusic", 2); lua_pop(L, 2); - error = g_hdb->_sound->fadeInMusic((SoundType)s1, ramp); + bool error = g_hdb->_sound->fadeInMusic((SoundType)s1, ramp); return 0; } @@ -1240,14 +1225,13 @@ static int startMap(lua_State *L) { static int saveGlobal(lua_State *L) { const char *global = lua_tostring(L, 1); - int type; g_hdb->_lua->checkParameters("saveGlobal", 1); lua_pop(L, 1); lua_getglobal(L, global); - type = lua_type(L, 1); + int type = lua_type(L, 1); if (type == LUA_TNUMBER) { double value = lua_tonumber(L, 1); g_hdb->_lua->saveGlobalNumber(global, value); @@ -1798,7 +1782,7 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, const char *scrip // Running the code, the error handler function sets the top of the stack if (lua_pcall(_state, 0, 1, 0) != 0) { - // An error occurred, so dislay the reason and exit + // An error occurred, so display the reason and exit error("Couldn't prepare luaL_pcall errorhandler:\n%s", lua_tostring(_state, -1)); lua_pop(_state, 1); @@ -1814,7 +1798,9 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, const char *scrip } // Load GLOBAL.LUA and execute it - _globalLuaStream->seek(0); // Make sure we start from the beginning + + // Make sure we start from the beginning + _globalLuaStream->seek(0); if (!executeMPC(_globalLuaStream, "global code", "GLOBAL.LUA", _globalLuaLength)) { error("LuaScript::initScript: 'global code' failed to execute"); return false; @@ -1859,13 +1845,11 @@ void LuaScript::pushString(char *string) { } void LuaScript::pushFunction(char *func) { - int type; - if (!_systemInit) return; lua_getglobal(_state, func); - type = lua_type(_state, 1); + int type = lua_type(_state, 1); if (type != LUA_TFUNCTION && type != LUA_TNUMBER) { warning("pushFunction: Function '%s' doesn't exists", func); } @@ -1899,13 +1883,11 @@ bool LuaScript::callFunction(const char *name, int returns) { } void LuaScript::invokeLuaFunction(char *luaFunc, int x, int y, int value1, int value2) { - int type; - if (!_systemInit) return; lua_getglobal(_state, luaFunc); - type = lua_type(_state, 1); + int type = lua_type(_state, 1); #if 0 if (!strcmp(luaFunc, "ferretbed_use")) { |