From 9d6a1f4c881d8a77d7bde3244a6543b0cbe31718 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Fri, 18 Mar 2011 00:53:53 +0100 Subject: Improved error handling in chr_shake_screen Lua function It's better to use luaL_checkint than manually checking and printing out an error, since using the helper function makes a more informative error message. Also, scripts errors need to be raised before constructing objects, since due to the way Lua does error handling the destructors of the objects will not be called. Also fixed an issue with the 4th parameter, which was first cast to integer and then multiplied by 10000. Now the multiplication happens before the casting to integer. Reviewed-by: Crush --- src/scripting/lua.cpp | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) (limited to 'src/scripting/lua.cpp') diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index e0cd4e16..e48a274f 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -1396,7 +1396,6 @@ static int effect_create(lua_State *s) } - /** * * mana.chr_shake_screen( @@ -1410,35 +1409,24 @@ static int chr_shake_screen(lua_State *s) return 0; } + const int x = luaL_checkint(s, 2); + const int y = luaL_checkint(s, 3); + MessageOut msg(GPMSG_SHAKE); - if(!lua_isnumber(s, 2) || !lua_isnumber(s, 3)) - { - raiseScriptError(s, "lua chr_shake_screen called with illegal arguments."); - return 0; - } - else - { - int x = lua_tointeger(s, 2); - int y = lua_tointeger(s, 3); - msg.writeInt16(x); - msg.writeInt16(y); - } - if(lua_isnumber(s, 4)) - { - msg.writeInt16((int)lua_tonumber(s, 4) * 10000); - } - if(lua_isnumber(s, 5)) - { + msg.writeInt16(x); + msg.writeInt16(y); + + if (lua_isnumber(s, 4)) + msg.writeInt16((int) (lua_tonumber(s, 4) * 10000)); + if (lua_isnumber(s, 5)) msg.writeInt16(lua_tointeger(s, 5)); - } + c->getClient()->send(msg); return 0; } - - /** * Gets the exp total in a skill of a specific character * mana.chr_get_exp (being, skill) -- cgit v1.2.3-70-g09d2