From e253efce6361efd14b0de7556368586fb3f74d93 Mon Sep 17 00:00:00 2001 From: Walter Julius Hennecke Date: Sat, 13 Apr 2013 22:30:58 +0200 Subject: [PATCH] Updated lua functions --- code/game/lua_game.c | 141 +++++++++++++++++++++++++++++++------------ 1 file changed, 103 insertions(+), 38 deletions(-) diff --git a/code/game/lua_game.c b/code/game/lua_game.c index fb727f0..4610504 100644 --- a/code/game/lua_game.c +++ b/code/game/lua_game.c @@ -14,7 +14,7 @@ static int Game_Print(lua_State *L) { lua_getglobal(L, "tostring"); - LUA_DEBUG("Game_Print - start: "); + LUA_DEBUG("BEGIN - game.Print"); for(i = 1; i <= n; i++) { @@ -27,8 +27,8 @@ static int Game_Print(lua_State *L) { if(s == NULL) { - LUA_DEBUG("Game_Print - return: no string"); - return luaL_error(L, "'tostring' must return a string to 'print'"); + LUA_DEBUG("ERROR - game.Print - no string"); + return 1; } Q_strcat(buf, sizeof(buf), s); @@ -38,7 +38,7 @@ static int Game_Print(lua_State *L) { G_Printf("%s\n", buf); - LUA_DEBUG("Game_Print - return: printed string"); + LUA_DEBUG("END - game.Print"); return 0; } @@ -55,7 +55,7 @@ static int Game_CenterPrint(lua_State *L) { lua_getglobal(L, "tostring"); - LUA_DEBUG("Game_CenterPrint - start: "); + LUA_DEBUG("BEGIN - game.CenterPrint"); for(i = 1; i < n; i++) { @@ -68,8 +68,9 @@ static int Game_CenterPrint(lua_State *L) { if(s == NULL) { - LUA_DEBUG("Game_CenterPrint - return: no string"); - return luaL_error(L, "'tostring' must return a string to 'print'"); + LUA_DEBUG("ERROR - game.CenterPrint - no string"); + lua_pushboolean(L, qfalse); + return 1; } Q_strcat(buf, sizeof(buf), s); @@ -80,8 +81,9 @@ static int Game_CenterPrint(lua_State *L) { trap_SendServerCommand(clNum, va("servercprint \"" S_COLOR_WHITE "%s\n\"", buf)); - LUA_DEBUG("Game_CenterPrint - return: printed string"); - return 0; + LUA_DEBUG("END - game.CenterPrint"); + lua_pushboolean(L, qtrue); + return 1; } // game.ClientPrint(integer clientNum, string text) @@ -98,7 +100,7 @@ static int Game_ClientPrint(lua_State *L) { lua_getglobal(L, "tostring"); - LUA_DEBUG("Game_ClientPrint - start: "); + LUA_DEBUG("BEGIN - game.ClientPrint"); for(i = 1; i < n; i++) { const char *s; @@ -110,8 +112,9 @@ static int Game_ClientPrint(lua_State *L) { if(s == NULL) { - LUA_DEBUG("Game_ClientPrint - return: no string"); - return luaL_error(L, "'tostring' must return a string to 'print'"); + LUA_DEBUG("BEGIN - game.ClientPrint - no string"); + lua_pushboolean(L, qfalse); + return 1; } Q_strcat(buf, sizeof(buf), s); @@ -121,12 +124,16 @@ static int Game_ClientPrint(lua_State *L) { if(clNum != -1) { player = &g_entities[clNum]; - if(player && player->client) + if(player && player->client) { G_PrintfClient(player, "%s", buf); - } else + } + } else { G_PrintfClientAll("%s", buf); - LUA_DEBUG("Game_ClientPrint - return: printed string"); - return 0; + } + + LUA_DEBUG("END - game.ClientPrint"); + lua_pushboolean(L, qtrue); + return 1; } // game.MessagePrint(integer clientNum, string text) @@ -142,7 +149,7 @@ static int Game_MessagePrint(lua_State *L) { lua_getglobal(L, "tostring"); - LUA_DEBUG("Game_MessagePrint - start: "); + LUA_DEBUG("BEGIN - game.MessagePrint"); for(i = 1; i < n; i++) { @@ -155,8 +162,9 @@ static int Game_MessagePrint(lua_State *L) { if(s == NULL) { - LUA_DEBUG("Game_MessagePrint - return: no string"); - return luaL_error(L, "'tostring' must return a string to 'print'"); + LUA_DEBUG("ERROR - game.MessagePrint - no string"); + lua_pushboolean(L, qfalse); + return 1; } Q_strcat(buf, sizeof(buf), s); @@ -166,8 +174,9 @@ static int Game_MessagePrint(lua_State *L) { trap_SendServerCommand(clNum, va("servermsg \"" S_COLOR_WHITE "%s\n\"", buf)); - LUA_DEBUG("Game_MessagePrint - return: printed string"); - return 0; + LUA_DEBUG("END - game.MessagePrint"); + lua_pushboolean(L, qfalse); + return 1; } // game.SetGlobal(string name, value) @@ -177,13 +186,21 @@ static int Game_MessagePrint(lua_State *L) { static int Game_SetGlobal(lua_State *L) { char *name; + LUA_DEBUG("BEGIN - game.SetGlobal"); + name = (char *)luaL_checkstring(L, 1); - if(!name) return 0; + if(name == NULL) { + LUA_DEBUG("ERROR - game.SetGlobal - name NULL"); + lua_pushboolean(L, qfalse); + return 0; + } lua_pushvalue(L, 2); lua_setglobal(L, name); + LUA_DEBUG("END - game.SetGlobal"); + lua_pushboolean(L, qtrue); return 0; } @@ -193,12 +210,18 @@ static int Game_SetGlobal(lua_State *L) { static int Game_GetGlobal(lua_State *L) { char *name; + LUA_DEBUG("BEGIN - game.SetGlobal"); + name = (char *)luaL_checkstring(L, 1); - if(!name) return 0; + if(name == NULL) { + LUA_DEBUG("ERROR - game.SetGlobal - name NULL"); + lua_pushnil(L); + return 0; + } lua_getglobal(L, name); - + LUA_DEBUG("END - game.SetGlobal"); return 1; } @@ -207,13 +230,17 @@ static int Game_GetGlobal(lua_State *L) { // string greensound, string yellowsound, string redsound, string bluesound, integer mode) static int Game_AlertSetup(lua_State *L) { - if(luaAlertState) { + LUA_DEBUG("BEGIN - game.AlertSetup"); + + if(luaAlertState != NULL) { + LUA_DEBUG("ERROR - game.AlertSetup - luaArlterState != NULL"); lua_pushboolean(L, 0); return 1; } luaAlertState = (luaAlertState_t *)malloc(sizeof(luaAlertState_t)); - if(!luaAlertState) { + if(luaAlertState == NULL) { + LUA_DEBUG("ERROR - game.AlertSetup - luaAlertState NULL"); lua_pushboolean(L, 0); return 1; } @@ -232,6 +259,7 @@ static int Game_AlertSetup(lua_State *L) { luaAlertState->cond = 0; + LUA_DEBUG("END - game.AlertSetup"); lua_pushboolean(L, 1); return 1; } @@ -240,26 +268,38 @@ static int Game_AlertAddShader(lua_State *L) { int cond; char *shader; - if(!luaAlertState) { + LUA_DEBUG("BEGIN - game.AlertAddShader"); + + if(luaAlertState == NULL) { + LUA_DEBUG("ERROR - game.AlertAddShader - luaAlertShader NULL"); lua_pushboolean(L, 0); return 1; } cond = luaL_checkint(L, 1); if(cond < 0 || cond > 3) { + LUA_DEBUG("ERROR - game.AlertAddShader - cond out of range"); lua_pushboolean(L, 0); return 1; } shader = (char *)luaL_checkstring(L, 2); - if(!shader || (strlen(shader) + 1) > MAX_QPATH) { + if(shader == NULL) { + LUA_DEBUG("ERROR - game.AlertAddShader - shader NULL"); lua_pushboolean(L, 0); return 1; } - if(!luaAlertState->shaders[cond]) { + if((strlen(shader) + 1) > MAX_QPATH) { + LUA_DEBUG("ERROR - game.AlertAddShader - strlen(shader)+1 > MAX_QPATH"); + lua_pushboolean(L, 0); + return 1; + } + + if(luaAlertState->shaders[cond] == NULL) { luaAlertState->shaders[cond] = (char *)malloc(sizeof(char) * (strlen(shader) + 1)); - if(!luaAlertState->shaders[cond]) { + if(luaAlertState->shaders[cond] == NULL) { + LUA_DEBUG("ERROR - game.AlertAddShader - alloc failed"); lua_pushboolean(L, 0); return 1; } @@ -267,7 +307,8 @@ static int Game_AlertAddShader(lua_State *L) { } else { void *tmp = realloc(luaAlertState->shaders[cond], sizeof(char) * (strlen(luaAlertState->shaders[cond]) + strlen(shader) + 1)); - if(!tmp){ + if(tmp == NULL){ + LUA_DEBUG("ERROR - game.AlertAddShader - realloc failed"); lua_pushboolean(L, 0); return 1; } @@ -276,6 +317,7 @@ static int Game_AlertAddShader(lua_State *L) { } lua_pushboolean(L, 1); + LUA_DEBUG("END - game.AlertAddShader"); return 1; } @@ -292,9 +334,12 @@ static int Game_Alert(lua_State *L) { // Returns the current level time in milliseconds. static int Game_Leveltime(lua_State * L) { + LUA_DEBUG("BEGIN - game.Leveltime"); + lua_pushinteger(L, level.time); - LUA_DEBUG("Game_Leveltime - start/return: leveltime=%d", level.time); + LUA_DEBUG("INFO - game.Leveltime - start/return: leveltime=%d", level.time); + LUA_DEBUG("BEGIN - game.Leveltime"); return 1; } @@ -304,29 +349,41 @@ static int Game_Damage(lua_State *L) { vec_t *dir = NULL, *point = NULL; int damage = 0, dflags = 0, mod = 0; + LUA_DEBUG("BEGIN - game.Damage"); + lent = Lua_GetEntity(L, 1); - if(!lent || !lent->e) return 1; + if(lent == NULL || lent->e == NULL) { + LUA_DEBUG("ERROR - game.Damage - entity NULL"); + lua_pushboolean(L, qfalse); + return 1; + } targ = lent->e; if(!lua_isnil(L, 2)) { lent = Lua_GetEntity(L, 2); - if(lent && lent->e) + if(lent && lent->e) { inflictor = lent->e; + } } if(!lua_isnil(L, 3)) { lent = Lua_GetEntity(L, 3); - if(lent && lent->e) + if(lent && lent->e) { attacker = lent->e; + } } - if(!lua_isnil(L, 4)) + if(!lua_isnil(L, 4)) { dir = Lua_GetVector(L, 4); - if(!lua_isnil(L, 5)) + } + if(!lua_isnil(L, 5)) { point = Lua_GetVector(L, 5); + } damage = (int)luaL_checknumber(L, 6); dflags = (int)luaL_checknumber(L, 7); mod = (int)luaL_checknumber(L, 8); G_Damage(targ, inflictor, attacker, dir, point, damage, dflags, mod); + lua_pushboolean(L, qtrue); + LUA_DEBUG("END - game.Damage"); return 1; } @@ -334,13 +391,21 @@ static int Game_Repair(lua_State *L) { lent_t *lent; float rate; + LUA_DEBUG("BEGIN - game.Repair"); + lent = Lua_GetEntity(L, 1); - if(!lent || !lent->e) return 1; + if(lent == NULL || lent->e == NULL) { + LUA_DEBUG("ERROR - game.Repair - entity NULL"); + lua_pushboolean(L, qfalse); + return 1; + } rate = (float)luaL_checknumber(L, 2); G_Repair(lent->e, NULL, rate); // FIXME ... trance ent? + LUA_DEBUG("END - game.Repair"); + lua_pushboolean(L, qtrue); return 1; }