From 800ac52895efaa180fd02db6de92f1d716dc729b Mon Sep 17 00:00:00 2001 From: Allanis Date: Mon, 26 Aug 2013 13:43:53 +0100 Subject: [PATCH] [Add] Can now save/load planet/system data. --- src/lephisto.c | 7 +++--- src/llua_space.c | 58 ++++++++++++++++++++++++++++++------------------ src/llua_space.h | 19 ++++++++++++++++ src/mission.c | 27 ++++++++++++++++++++++ 4 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/lephisto.c b/src/lephisto.c index de01f54..2f59f03 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -75,7 +75,8 @@ static void update_routine(double dt); static void render_all(void); -/** @brief The entry point of Lephisto. +/** + * @brief The entry point of Lephisto. * @param[in] argc Number of arguments. * @param[in] argv Array of argc arguments. * @return EXIT_SUCCESS on success. @@ -445,7 +446,7 @@ static void display_fps(const double dt) { gl_print(NULL, x, y, NULL, "%3.2f", fps); } -/* +/** * @brief Set the data module's name. */ static void data_name(void) { @@ -500,7 +501,7 @@ static void data_name(void) { xmlCleanupParser(); } -/* +/** * @brief Set the window caption. */ static void window_caption(void) { diff --git a/src/llua_space.c b/src/llua_space.c index 268a4f8..cf06c47 100644 --- a/src/llua_space.c +++ b/src/llua_space.c @@ -3,21 +3,11 @@ #include "log.h" #include "lephisto.h" #include "rng.h" -#include "space.h" #include "land.h" #include "lluadef.h" #include "map.h" #include "llua_space.h" -/* Lua wrappers. */ -typedef struct LuaPlanet_ { - Planet* p; -} LuaPlanet; - -typedef struct LuaSystem_ { - StarSystem* s; -} LuaSystem; - static int planetL_createmetatable(lua_State* L); static int systemL_createmetatable(lua_State* L); @@ -30,9 +20,6 @@ static const luaL_reg space_methods[] = { { 0, 0 } }; -/* Internal planet methods. */ -static LuaPlanet* lua_toplanet(lua_State* L, int ind); -static LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet); /* Planet metatable methods. */ static int planetL_eq(lua_State* L); static int planetL_name(lua_State* L); @@ -41,7 +28,6 @@ static int planetL_class(lua_State* L); static int planetL_services(lua_State* L); static const luaL_reg planet_methods[] = { { "__eq", planetL_eq }, - { "__tostring", planetL_name }, { "name", planetL_name }, { "faction", planetL_faction }, { "class", planetL_class }, @@ -49,9 +35,6 @@ static const luaL_reg planet_methods[] = { { 0, 0 } }; -/* Internal system methods. */ -static LuaSystem* lua_tosystem(lua_State* L, int ind); -static LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys); /* System metatable methods. */ static int systemL_eq(lua_State* L); static int systemL_name(lua_State* L); @@ -59,7 +42,6 @@ static int systemL_faction(lua_State* L); static int systemL_jumpdistance(lua_State* L); static const luaL_reg system_methods[] = { { "__eq", systemL_eq }, - { "__tostring", systemL_name }, { "name", systemL_name }, { "faction", systemL_faction }, { "jumpDist", systemL_jumpdistance }, @@ -115,7 +97,7 @@ static int systemL_createmetatable(lua_State* L) { /* -- PLANET -- */ /* Get planet at index. */ -static LuaPlanet* lua_toplanet(lua_State* L, int ind) { +LuaPlanet* lua_toplanet(lua_State* L, int ind) { if(lua_isuserdata(L, ind)) { return (LuaPlanet*) lua_touserdata(L, ind); } @@ -125,7 +107,7 @@ static LuaPlanet* lua_toplanet(lua_State* L, int ind) { } /* Push a planet on the stack. */ -static LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet) { +LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet) { LuaPlanet* p; p = (LuaPlanet*) lua_newuserdata(L, sizeof(LuaPlanet)); *p = planet; @@ -134,6 +116,22 @@ static LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet) { return p; } +/* Check see if ind is a planet. */ +int lua_isplanet(lua_State* L, int ind) { + int ret; + + if(lua_getmetatable(L, ind)==0) + return 0; + lua_getfield(L, LUA_REGISTRYINDEX, PLANET_METATABLE); + + ret = 0; + if(lua_rawequal(L, -1, -2)) /* Does it have the correct mt? */ + ret = 1; + + lua_pop(L, 2); /* Remove both metatables. */ + return ret; +} + /* get a planet. */ static int planetL_get(lua_State* L) { int i; @@ -261,7 +259,7 @@ static int planetL_services(lua_State* L) { /* -- SYSTEM -- */ /* Get system at index. */ -static LuaSystem* lua_tosystem(lua_State* L, int ind) { +LuaSystem* lua_tosystem(lua_State* L, int ind) { if(lua_isuserdata(L, ind)) { return (LuaSystem*) lua_touserdata(L,ind); } @@ -270,7 +268,7 @@ static LuaSystem* lua_tosystem(lua_State* L, int ind) { } /* Pushes a system on the stack. */ -static LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys) { +LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys) { LuaSystem* s; s = (LuaSystem*) lua_newuserdata(L, sizeof(LuaSystem)); *s = sys; @@ -279,6 +277,22 @@ static LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys) { return s; } +/* Check to see if ind is a planet. */ +int lua_issystem(lua_State* L, int ind) { + int ret; + + if(lua_getmetatable(L, ind)==0) + return 0; + lua_getfield(L, LUA_REGISTRYINDEX, SYSTEM_METATABLE); + + ret = 0; + if(lua_rawequal(L, -1, -2)) /* Does it have the correct mt? */ + ret = 1; + + lua_pop(L, 2); /* Remove both metatables. */ + return ret; +} + /* Get a system. */ static int systemL_get(lua_State* L) { LLUA_MIN_ARGS(1); diff --git a/src/llua_space.h b/src/llua_space.h index 85a2a39..4861398 100644 --- a/src/llua_space.h +++ b/src/llua_space.h @@ -1,9 +1,28 @@ #pragma once #include "lua.h" +#include "space.h" #define PLANET_METATABLE "Planet" #define SYSTEM_METATABLE "System" +/* Lua wrappers. */ +typedef struct LuaPlanet_ { + Planet* p; +} LuaPlanet; +typedef struct LuaSystem_ { + StarSystem* s; +} LuaSystem; + /* Load the space library. */ int lua_loadSpace(lua_State* L, int readonly); +/* Planet operations. */ +LuaPlanet* lua_toplanet(lua_State* L, int ind); +LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet); +int lua_isplanet(lua_State* L, int ind); + +/* System operations. */ +LuaSystem* lua_tosystem(lua_State* L, int ind); +LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys); +int lua_issystem(lua_State* L, int ind); + diff --git a/src/mission.c b/src/mission.c index dca1e18..94f9e4e 100644 --- a/src/mission.c +++ b/src/mission.c @@ -3,6 +3,7 @@ #include #include "llua.h" +#include "llua_space.h" #include "lluadef.h" #include "rng.h" @@ -539,6 +540,8 @@ static int mission_saveData(xmlTextWriterPtr writer, char* type, } static int mission_persistData(lua_State* L, xmlTextWriterPtr writer) { + LuaPlanet* p; + LuaSystem* s; lua_pushnil(L); /* nil. */ while(lua_next(L, LUA_GLOBALSINDEX) != 0) { @@ -556,6 +559,19 @@ static int mission_persistData(lua_State* L, xmlTextWriterPtr writer) { mission_saveData(writer, "string", (char*)lua_tostring(L, -2), (char*)lua_tostring(L, -1)); break; + case LUA_TUSERDATA: + if(lua_isplanet(L, -1)) { + p = lua_toplanet(L, -1); + mission_saveData(writer, "planet", + (char*)lua_tostring(L, -2), p->p->name); + break; + } + else if(lua_issystem(L, -1)) { + s = lua_tosystem(L, -1); + mission_saveData(writer, "system", + (char*)lua_tostring(L, -2), s->s->name); + break; + } default: break; } @@ -567,6 +583,8 @@ static int mission_persistData(lua_State* L, xmlTextWriterPtr writer) { /* Unpersist lua data. */ static int mission_unpersistData(lua_State* L, xmlNodePtr parent) { + LuaPlanet p; + LuaSystem s; xmlNodePtr node; char* name, *type; @@ -583,11 +601,20 @@ static int mission_unpersistData(lua_State* L, xmlNodePtr parent) { lua_pushboolean(L, xml_getInt(node)); else if(strcmp(type, "string")==0) lua_pushstring(L, xml_get(node)); + else if(strcmp(type, "planet")==0) { + p.p = planet_get(xml_get(node)); + lua_pushplanet(L, p); + } + else if(strcmp(type, "system")==0) { + s.s = system_get(xml_get(node)); + lua_pushsystem(L, s); + } else { WARN("Unknown lua data type!"); return -1; } + /* Set as global. */ lua_setglobal(L, name); free(type);