[Add] Can now save/load planet/system data.

This commit is contained in:
Allanis 2013-08-26 13:43:53 +01:00
parent ff172951e1
commit 800ac52895
4 changed files with 86 additions and 25 deletions

View File

@ -75,7 +75,8 @@ static void update_routine(double dt);
static void render_all(void); 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] argc Number of arguments.
* @param[in] argv Array of argc arguments. * @param[in] argv Array of argc arguments.
* @return EXIT_SUCCESS on success. * @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); gl_print(NULL, x, y, NULL, "%3.2f", fps);
} }
/* /**
* @brief Set the data module's name. * @brief Set the data module's name.
*/ */
static void data_name(void) { static void data_name(void) {
@ -500,7 +501,7 @@ static void data_name(void) {
xmlCleanupParser(); xmlCleanupParser();
} }
/* /**
* @brief Set the window caption. * @brief Set the window caption.
*/ */
static void window_caption(void) { static void window_caption(void) {

View File

@ -3,21 +3,11 @@
#include "log.h" #include "log.h"
#include "lephisto.h" #include "lephisto.h"
#include "rng.h" #include "rng.h"
#include "space.h"
#include "land.h" #include "land.h"
#include "lluadef.h" #include "lluadef.h"
#include "map.h" #include "map.h"
#include "llua_space.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 planetL_createmetatable(lua_State* L);
static int systemL_createmetatable(lua_State* L); static int systemL_createmetatable(lua_State* L);
@ -30,9 +20,6 @@ static const luaL_reg space_methods[] = {
{ 0, 0 } { 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. */ /* Planet metatable methods. */
static int planetL_eq(lua_State* L); static int planetL_eq(lua_State* L);
static int planetL_name(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 int planetL_services(lua_State* L);
static const luaL_reg planet_methods[] = { static const luaL_reg planet_methods[] = {
{ "__eq", planetL_eq }, { "__eq", planetL_eq },
{ "__tostring", planetL_name },
{ "name", planetL_name }, { "name", planetL_name },
{ "faction", planetL_faction }, { "faction", planetL_faction },
{ "class", planetL_class }, { "class", planetL_class },
@ -49,9 +35,6 @@ static const luaL_reg planet_methods[] = {
{ 0, 0 } { 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. */ /* System metatable methods. */
static int systemL_eq(lua_State* L); static int systemL_eq(lua_State* L);
static int systemL_name(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 int systemL_jumpdistance(lua_State* L);
static const luaL_reg system_methods[] = { static const luaL_reg system_methods[] = {
{ "__eq", systemL_eq }, { "__eq", systemL_eq },
{ "__tostring", systemL_name },
{ "name", systemL_name }, { "name", systemL_name },
{ "faction", systemL_faction }, { "faction", systemL_faction },
{ "jumpDist", systemL_jumpdistance }, { "jumpDist", systemL_jumpdistance },
@ -115,7 +97,7 @@ static int systemL_createmetatable(lua_State* L) {
/* -- PLANET -- */ /* -- PLANET -- */
/* Get planet at index. */ /* 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)) { if(lua_isuserdata(L, ind)) {
return (LuaPlanet*) lua_touserdata(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. */ /* 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; LuaPlanet* p;
p = (LuaPlanet*) lua_newuserdata(L, sizeof(LuaPlanet)); p = (LuaPlanet*) lua_newuserdata(L, sizeof(LuaPlanet));
*p = planet; *p = planet;
@ -134,6 +116,22 @@ static LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet) {
return p; 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. */ /* get a planet. */
static int planetL_get(lua_State* L) { static int planetL_get(lua_State* L) {
int i; int i;
@ -261,7 +259,7 @@ static int planetL_services(lua_State* L) {
/* -- SYSTEM -- */ /* -- SYSTEM -- */
/* Get system at index. */ /* 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)) { if(lua_isuserdata(L, ind)) {
return (LuaSystem*) lua_touserdata(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. */ /* 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; LuaSystem* s;
s = (LuaSystem*) lua_newuserdata(L, sizeof(LuaSystem)); s = (LuaSystem*) lua_newuserdata(L, sizeof(LuaSystem));
*s = sys; *s = sys;
@ -279,6 +277,22 @@ static LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys) {
return s; 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. */ /* Get a system. */
static int systemL_get(lua_State* L) { static int systemL_get(lua_State* L) {
LLUA_MIN_ARGS(1); LLUA_MIN_ARGS(1);

View File

@ -1,9 +1,28 @@
#pragma once #pragma once
#include "lua.h" #include "lua.h"
#include "space.h"
#define PLANET_METATABLE "Planet" #define PLANET_METATABLE "Planet"
#define SYSTEM_METATABLE "System" #define SYSTEM_METATABLE "System"
/* Lua wrappers. */
typedef struct LuaPlanet_ {
Planet* p;
} LuaPlanet;
typedef struct LuaSystem_ {
StarSystem* s;
} LuaSystem;
/* Load the space library. */ /* Load the space library. */
int lua_loadSpace(lua_State* L, int readonly); 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);

View File

@ -3,6 +3,7 @@
#include <malloc.h> #include <malloc.h>
#include "llua.h" #include "llua.h"
#include "llua_space.h"
#include "lluadef.h" #include "lluadef.h"
#include "rng.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) { static int mission_persistData(lua_State* L, xmlTextWriterPtr writer) {
LuaPlanet* p;
LuaSystem* s;
lua_pushnil(L); lua_pushnil(L);
/* nil. */ /* nil. */
while(lua_next(L, LUA_GLOBALSINDEX) != 0) { while(lua_next(L, LUA_GLOBALSINDEX) != 0) {
@ -556,6 +559,19 @@ static int mission_persistData(lua_State* L, xmlTextWriterPtr writer) {
mission_saveData(writer, "string", mission_saveData(writer, "string",
(char*)lua_tostring(L, -2), (char*)lua_tostring(L, -1)); (char*)lua_tostring(L, -2), (char*)lua_tostring(L, -1));
break; 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: default:
break; break;
} }
@ -567,6 +583,8 @@ static int mission_persistData(lua_State* L, xmlTextWriterPtr writer) {
/* Unpersist lua data. */ /* Unpersist lua data. */
static int mission_unpersistData(lua_State* L, xmlNodePtr parent) { static int mission_unpersistData(lua_State* L, xmlNodePtr parent) {
LuaPlanet p;
LuaSystem s;
xmlNodePtr node; xmlNodePtr node;
char* name, *type; char* name, *type;
@ -583,11 +601,20 @@ static int mission_unpersistData(lua_State* L, xmlNodePtr parent) {
lua_pushboolean(L, xml_getInt(node)); lua_pushboolean(L, xml_getInt(node));
else if(strcmp(type, "string")==0) else if(strcmp(type, "string")==0)
lua_pushstring(L, xml_get(node)); 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 { else {
WARN("Unknown lua data type!"); WARN("Unknown lua data type!");
return -1; return -1;
} }
/* Set as global. */
lua_setglobal(L, name); lua_setglobal(L, name);
free(type); free(type);