Skip to content
Browse files

* Added the conditionaly subsystem to replace the mission checkCond s…

…tuff.
  • Loading branch information...
1 parent a9e8ad7 commit b35293d141dec34207a25a942d85649727e9ffcd @bobbens bobbens committed
Showing with 139 additions and 84 deletions.
  1. +117 −0 src/cond.c
  2. +17 −0 src/cond.h
  3. +2 −73 src/mission.c
  4. +3 −0 src/naev.c
  5. +0 −11 src/nlua_misn.c
View
117 src/cond.c
@@ -0,0 +1,117 @@
+/*
+ * See Licensing and Copyright notice in naev.h
+ */
+
+/**
+ * @file mission.c
+ *
+ * @brief Handles missions.
+ */
+
+
+#include "cond.h"
+
+#include "naev.h"
+
+#include "log.h"
+#include "nlua.h"
+#include "nluadef.h"
+
+
+static lua_State *cond_L = NULL; /** Conditional Lua state. */
+
+
+/**
+ * @brief Initializes the conditional subsystem.
+ */
+int cond_init (void)
+{
+ if (cond_L != NULL)
+ return 0;
+
+ cond_L = nlua_newState();
+ nlua_loadStandard(cond_L,1);
+
+ return 0;
+}
+
+
+/**
+ * @brief Destroys the conditional subsystem.
+ */
+void cond_exit (void)
+{
+ if (cond_L == NULL)
+ return;
+
+ lua_close(cond_L);
+ cond_L = NULL;
+}
+
+
+/**
+ * @brief Checks to see if a condition is true.
+ *
+ * @param cond Condition to check.
+ * @return 0 if is false, 1 if is true, -1 on error.
+ */
+int cond_check( const char* cond )
+{
+ int b;
+ int ret;
+
+ /* Lazy loading. */
+ if (cond_L == NULL) { /* must create the conditional environment */
+ cond_L = nlua_newState();
+ nlua_loadStandard(cond_L,1);
+ }
+
+ /* Load the string. */
+ lua_pushstring(cond_L, "return ");
+ lua_pushstring(cond_L, cond);
+ lua_concat(cond_L, 2);
+ ret = luaL_loadbuffer(cond_L, lua_tostring(cond_L,-1),
+ lua_strlen(cond_L,-1), "Lua Conditional");
+ switch (ret) {
+ case LUA_ERRSYNTAX:
+ WARN("Lua conditional syntax error");
+ return 0;
+ case LUA_ERRMEM:
+ WARN("Lua Conditional ran out of memory");
+ return 0;
+ default:
+ break;
+ }
+
+ /* Run the string. */
+ ret = lua_pcall( cond_L, 0, 1, 0 );
+ switch (ret) {
+ case LUA_ERRRUN:
+ WARN("Lua Conditional had a runtime error: %s", lua_tostring(cond_L, -1));
+ return 0;
+ case LUA_ERRMEM:
+ WARN("Lua Conditional ran out of memory");
+ return 0;
+ case LUA_ERRERR:
+ WARN("Lua Conditional had an error while handling error function");
+ return 0;
+ default:
+ break;
+ }
+
+ /* Check the result. */
+ if (lua_isboolean(cond_L, -1)) {
+ b = lua_toboolean(cond_L, -1);
+ lua_pop(cond_L, 1);
+ if (b)
+ return 1;
+ else
+ return 0;
+ }
+ WARN("Lua Conditional didn't return a boolean");
+
+ /* Clear stack. */
+ lua_settop(cond_L, 0);
+
+ return 0;
+}
View
17 src/cond.h
@@ -0,0 +1,17 @@
+/*
+ * See Licensing and Copyright notice in naev.h
+ */
+
+
+#ifndef COND_H
+# define COND_H
+
+
+int cond_init (void);
+void cond_exit (void);
+int cond_check( const char *cond );
+
+
+#endif /* COND_H */
+
+
View
75 src/mission.c
@@ -30,6 +30,7 @@
#include "player.h"
#include "base64.h"
#include "space.h"
+#include "cond.h"
#define XML_MISSION_ID "Missions" /**< XML document identifier */
@@ -63,7 +64,6 @@ static unsigned int mission_genID (void);
static int mission_init( Mission* mission, MissionData* misn, int load );
static void mission_freeData( MissionData* mission );
static int mission_alreadyRunning( MissionData* misn );
-static int mission_meetCond( MissionData* misn );
static int mission_meetReq( int mission, int faction,
const char* planet, const char* sysname );
static int mission_matchFaction( MissionData* misn, int faction );
@@ -229,71 +229,6 @@ static int mission_alreadyRunning( MissionData* misn )
}
-static lua_State* mission_cond_L = NULL; /**< Mission conditional Lua state. */
-/**
- * @brief Checks to see if the mission conditional data is met.
- *
- * @param misn Mission to check.
- * @return 1 if is met, 0 if it isn't.
- */
-static int mission_meetCond( MissionData* misn )
-{
- int b;
- int ret;
- char buf[256];
-
- /* Create environment if needed. */
- if (mission_cond_L == NULL) { /* must create the conditional environment */
- mission_cond_L = nlua_newState();
- misn_loadCondLibs( mission_cond_L );
- }
-
- /* Load the string. */
- snprintf( buf, 256, "return %s", misn->avail.cond ); /* Must convert to Lua syntax */
- ret = luaL_loadstring( mission_cond_L, buf );
- switch (ret) {
- case LUA_ERRSYNTAX:
- WARN("Mission '%s' Lua conditional syntax error", misn->name );
- return 0;
- case LUA_ERRMEM:
- WARN("Mission '%s' Lua Conditional ran out of memory", misn->name );
- return 0;
- default:
- break;
- }
-
- /* Run the string. */
- ret = lua_pcall( mission_cond_L, 0, 1, 0 );
- switch (ret) {
- case LUA_ERRRUN:
- WARN("Mission '%s' Lua Conditional had a runtime error: %s",
- misn->name, lua_tostring(mission_cond_L, -1));
- return 0;
- case LUA_ERRMEM:
- WARN("Mission '%s' Lua Conditional ran out of memory", misn->name);
- return 0;
- case LUA_ERRERR:
- WARN("Mission '%s' Lua Conditional had an error while handling error function",
- misn->name);
- return 0;
- default:
- break;
- }
-
- /* Check the result. */
- if (lua_isboolean(mission_cond_L, -1)) {
- b = lua_toboolean(mission_cond_L, -1);
- lua_pop(mission_cond_L, 1);
- if (b)
- return 1;
- else
- return 0;
- }
- WARN("Mission '%s' Conditional Lua didn't return a boolean", misn->name);
- return 0;
-}
-
-
/**
* @brief Checks to see if a mission meets the requirements.
*
@@ -326,7 +261,7 @@ static int mission_meetReq( int mission, int faction,
/* Must meet Lua condition. */
if ((misn->avail.cond != NULL) &&
- !mission_meetCond(misn))
+ !cond_check(misn->avail.cond))
return 0;
/* Must meet previous mission requirements. */
@@ -821,12 +756,6 @@ void missions_free (void)
free( mission_stack );
mission_stack = NULL;
mission_nstack = 0;
-
- /* frees the lua stack */
- if (mission_cond_L != NULL) {
- lua_close( mission_cond_L );
- mission_cond_L = NULL;
- }
}
View
3 src/naev.c
@@ -67,6 +67,7 @@
#include "nlua_var.h"
#include "map.h"
#include "event.h"
+#include "cond.h"
#define CONF_FILE "conf.lua" /**< Configuration file by default. */
@@ -232,6 +233,7 @@ int main( int argc, char** argv )
gui_init(); /* initializes the GUI graphics */
toolkit_init(); /* initializes the toolkit */
map_init(); /* initializes the map. */
+ cond_init(); /* Initialize conditional subsystem. */
/* Data loading */
load_all();
@@ -271,6 +273,7 @@ int main( int argc, char** argv )
gui_free(); /* cleans up the player's GUI */
weapon_exit(); /* destroys all active weapons */
pilots_free(); /* frees the pilots, they were locked up :( */
+ cond_exit(); /* destroy conditional subsystem. */
/* data unloading */
unload_all();
View
11 src/nlua_misn.c
@@ -107,17 +107,6 @@ int misn_loadLibs( lua_State *L )
lua_loadMusic(L,0);
return 0;
}
-/**
- * @brief Registers all the mission conditional libraries.
- *
- * @param L Lua state.
- * @return 0 on success.
- */
-int misn_loadCondLibs( lua_State *L )
-{
- nlua_loadStandard(L,1);
- return 0;
-}
/*
* individual library loading
*/

0 comments on commit b35293d

Please sign in to comment.
Something went wrong with that request. Please try again.