diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua index 57606d4..183a69a 100644 --- a/dat/missions/cargo.lua +++ b/dat/missions/cargo.lua @@ -26,14 +26,12 @@ function accept() end function land() - if planet.name() == planet then + if space.landName() == planet then player.rmCargo(carg_type, carg_mass) player.pay(misn_reward) tk.msg("Mission Accomplished", string.format("The workers unload the %s at the docks.", carg_type)) - - -- Set the hooks. - hook.land("land") + misn_finish() end end diff --git a/src/hook.c b/src/hook.c index 593b34e..f88cb9a 100644 --- a/src/hook.c +++ b/src/hook.c @@ -8,8 +8,7 @@ // The hook. typedef struct Hook_ { int id; - lua_State* L; - unsigned int parent; + Mission* misn; char* func; char* stack; } Hook; @@ -20,22 +19,20 @@ static Hook* hook_stack = NULL; static int hook_mstack = 0; static int hook_nstack = 0; +// Extern. +extern int misn_run(Mission* misn, char* func); + int hook_run(Hook* hook) { - lua_State* L; - - L = hook->L; - - lua_getglobal(L, hook->func); - if(lua_pcall(L, 0, 0, 0)) + if(misn_run(hook->misn, hook->func)) // Error has accured. - WARN("Hook [%s] '%d' -> '%s' : %s", hook->stack, - hook->parent, hook->func, lua_tostring(L, -1)); + WARN("Hook [%s] '%d' -> '%s' failed", hook->stack, + hook->id, hook->func); return 0; } // Add/Remove hooks. -int hook_add(lua_State* L, unsigned int parent, char* func, char* stack) { +int hook_add(Mission* misn, char* func, char* stack) { Hook* new_hook; // If the memory must grow. @@ -47,8 +44,7 @@ int hook_add(lua_State* L, unsigned int parent, char* func, char* stack) { // Create the new hook. new_hook = &hook_stack[hook_nstack]; new_hook->id = ++hook_id; - new_hook->L = L; - new_hook->parent = parent; + new_hook->misn = misn; new_hook->func = func; new_hook->stack = stack; @@ -69,7 +65,10 @@ void hook_rm(int id) { else break; } - if(m == (hook_nstack-1)) return; + if(m == (hook_nstack-1)) { + hook_nstack--; + return; + } // Move it! memmove(&hook_stack[m+1], &hook_stack[m+2], sizeof(Hook)*(hook_nstack-(m+2))); @@ -79,7 +78,7 @@ void hook_rm(int id) { void hook_rmParent(unsigned int parent) { int i; for(i = 0; i < hook_nstack; i++) - if(parent == hook_stack[i].parent) { + if(parent == hook_stack[i].misn->id) { hook_rm(hook_stack[i].id); i--; } diff --git a/src/hook.h b/src/hook.h index 5ce0565..934dbeb 100644 --- a/src/hook.h +++ b/src/hook.h @@ -1,8 +1,8 @@ #pragma once -#include "lua.h" +#include "mission.h" // Add/Run hooks. -int hook_add(lua_State* L, unsigned int parent, char* func, char* stack); +int hook_add(Mission* misn, char* func, char* stack); void hook_rm(int id); void hook_rmParent(unsigned int parent); diff --git a/src/land.h b/src/land.h index f2efde7..9824385 100644 --- a/src/land.h +++ b/src/land.h @@ -2,6 +2,7 @@ #include "space.h" extern int landed; +extern Planet* land_planet; void land(Planet* p); void takeoff(void); diff --git a/src/misn_lua.c b/src/misn_lua.c index c425f16..1bc3ea6 100644 --- a/src/misn_lua.c +++ b/src/misn_lua.c @@ -9,6 +9,7 @@ #include "rng.h" #include "space.h" #include "toolkit.h" +#include "land.h" #include "misn_lua.h" // FUCK LUA!!! @@ -17,6 +18,7 @@ #define MIN_ARGS(n) if(lua_gettop(L) < n) return 0 static Mission* cur_mission = NULL; +static int misn_delete = 0; // If 1 delete current mission. // -- Libraries. -- @@ -25,18 +27,22 @@ static int misn_setTitle(lua_State* L); static int misn_setDesc(lua_State* L); static int misn_setReward(lua_State* L); static int misn_factions(lua_State* L); +static int misn_finish(lua_State* L); static const luaL_Reg misn_methods[] = { { "setTitle", misn_setTitle }, { "setDesc", misn_setDesc }, { "setReward", misn_setReward }, { "factions", misn_factions }, + { "finish", misn_finish }, { 0, 0 } }; // Space. static int space_getPlanet(lua_State* L); +static int space_landName(lua_State* L); static const luaL_Reg space_methods[] = { { "getPlanet", space_getPlanet }, + { "landName", space_landName }, { 0, 0 } }; @@ -91,15 +97,22 @@ int misn_loadLibs(lua_State* L) { // Run a mission function. int misn_run(Mission* misn, char* func) { int ret; + char* err; cur_mission = misn; lua_getglobal(misn->L, func); - if((ret = lua_pcall(misn->L, 0, 0, 0))) + if((ret = lua_pcall(misn->L, 0, 0, 0))) { // Did an oops. - WARN("Mission '%s' -> '%s' : %s", - cur_mission->data->name, func, lua_tostring(misn->L, -1)); + err = (char*) lua_tostring(misn->L, -1); + if(strcmp(err, "Mission Finished")) + WARN("Mission '%s' -> '%s' : %s", + cur_mission->data->name, func, err); + else ret = 0; + } + // Mission is finished. + if(misn_delete) mission_cleanup(cur_mission); cur_mission = NULL; return ret; @@ -146,13 +159,29 @@ static int misn_factions(lua_State* L) { return 0; } +static int misn_finish(lua_State* L) { + misn_delete = 1; + + lua_pushstring(L, "Mission Finished"); + lua_error(L); // Should not return.. + + return 0; +} + static int space_getPlanet(lua_State* L) { - (void)L; // TODO: Proper getPlanet implementation. - lua_pushstring(L, "SaraCraft"); + lua_pushstring(L, "KonoSphere"); return 1; } +static int space_landName(lua_State* L) { + if(landed) { + lua_pushstring(L, land_planet->name); + return 1; + } + return 0; +} + // -- Player. -- static int player_freeSpace(lua_State* L) { lua_pushnumber(L, pilot_freeCargo(player)); @@ -289,7 +318,7 @@ static int hook_land(lua_State* L) { cur_mission->data->name); return 0; } - hook_add(L, cur_mission->id, func, "land"); + hook_add(cur_mission, func, "land"); return 0; } diff --git a/src/player.c b/src/player.c index 60bedfd..33e59e9 100644 --- a/src/player.c +++ b/src/player.c @@ -59,9 +59,6 @@ extern int pilots; // Space stuff for GUI. extern StarSystem* systems_stack; -// Land stuff for the player_stack. -extern Planet* land_planet; - // GUI crap. typedef struct Radar_ { double x,y; // Position. diff --git a/src/spfx.c b/src/spfx.c index bf14da3..1989943 100644 --- a/src/spfx.c +++ b/src/spfx.c @@ -147,7 +147,7 @@ void spfx_clear(void) { static void spfx_destroy(SPFX* layer, int* nlayer, int spfx) { (*nlayer)--; - memmove(&layer[spfx], &layer[spfx+1], (*nlayer-spfx)*sizeof(SPFX)); + memmove(&layer[spfx], &layer[spfx+1], (*nlayer-spfx-1)*sizeof(SPFX)); } void spfx_update(const double dt) {