[Add] Give active mission ids and use the ids for removing hooks.

This commit is contained in:
Allanis 2013-03-30 21:47:27 +00:00
parent cefebd3d2c
commit b565e6e6cb
5 changed files with 19 additions and 11 deletions

View File

@ -9,16 +9,16 @@
typedef struct Hook_ { typedef struct Hook_ {
int id; int id;
lua_State* L; lua_State* L;
char* parent; unsigned int parent;
char* func; char* func;
char* stack; char* stack;
} Hook; } Hook;
// The stack. // The stack.
static int hook_id = 0; // Unique hook id. static unsigned int hook_id = 0; // Unique hook id.
static Hook* hook_stack = NULL; static Hook* hook_stack = NULL;
static int hook_mstack = 0; static int hook_mstack = 0;
static int hook_nstack = 0; static int hook_nstack = 0;
int hook_run(Hook* hook) { int hook_run(Hook* hook) {
lua_State* L; lua_State* L;
@ -28,14 +28,14 @@ int hook_run(Hook* hook) {
lua_getglobal(L, hook->func); lua_getglobal(L, hook->func);
if(lua_pcall(L, 0, 0, 0)) if(lua_pcall(L, 0, 0, 0))
// Error has accured. // Error has accured.
WARN("Hook [%s] '%s' -> '%s' : %s", hook->stack, WARN("Hook [%s] '%d' -> '%s' : %s", hook->stack,
hook->parent, hook->func, lua_tostring(L, -1)); hook->parent, hook->func, lua_tostring(L, -1));
return 0; return 0;
} }
// Add/Remove hooks. // Add/Remove hooks.
int hook_add(lua_State* L, char* parent, char* func, char* stack) { int hook_add(lua_State* L, unsigned int parent, char* func, char* stack) {
Hook* new_hook; Hook* new_hook;
// If the memory must grow. // If the memory must grow.
@ -76,10 +76,10 @@ void hook_rm(int id) {
hook_nstack--; hook_nstack--;
} }
void hook_rmParent(char* parent) { void hook_rmParent(unsigned int parent) {
int i; int i;
for(i = 0; i < hook_nstack; i++) for(i = 0; i < hook_nstack; i++)
if(strcmp(parent, hook_stack[i].parent) == 0) { if(parent == hook_stack[i].parent) {
hook_rm(hook_stack[i].id); hook_rm(hook_stack[i].id);
i--; i--;
} }

View File

@ -2,9 +2,9 @@
#include "lua.h" #include "lua.h"
// Add/Run hooks. // Add/Run hooks.
int hook_add(lua_State* L, char* parent, char* func, char* stack); int hook_add(lua_State* L, unsigned int parent, char* func, char* stack);
void hook_rm(int id); void hook_rm(int id);
void hook_rmParent(char* parent); void hook_rmParent(unsigned int parent);
// Run hook. // Run hook.
int hooks_run(char* stack); int hooks_run(char* stack);

View File

@ -5,6 +5,7 @@
#include "rng.h" #include "rng.h"
#include "music.h" #include "music.h"
#include "economy.h" #include "economy.h"
#include "hook.h"
#include "land.h" #include "land.h"
// Global/main window. // Global/main window.
@ -765,6 +766,7 @@ void land(Planet* p) {
landed = 1; landed = 1;
hooks_run("land");
} }
// Takeoff from the planet. // Takeoff from the planet.
@ -797,5 +799,6 @@ void takeoff(void) {
land_planet = NULL; land_planet = NULL;
window_destroy(land_wid); window_destroy(land_wid);
landed = 0; landed = 0;
hooks_run("takeoff");
} }

View File

@ -7,6 +7,7 @@
#include "lephisto.h" #include "lephisto.h"
#include "log.h" #include "log.h"
#include "hook.h"
#include "mission.h" #include "mission.h"
// Current player missions. // Current player missions.
@ -46,6 +47,7 @@ int mission_create(MissionData* misn) {
// Clean up a mission. // Clean up a mission.
static void mission_cleanup(Mission* misn) { static void mission_cleanup(Mission* misn) {
hook_rmParent(misn->id); // Remove existing hooks.
misn->data = NULL; misn->data = NULL;
if(misn->title) free(misn->title); if(misn->title) free(misn->title);
if(misn->desc) free(misn->desc); if(misn->desc) free(misn->desc);

View File

@ -13,6 +13,7 @@
#define mis_setFlag(m,f) ((m)->flags |= (f)) #define mis_setFlag(m,f) ((m)->flags |= (f))
#define mis_rmFlag(m,f) ((m)->flags ^= (f)) #define mis_rmFlag(m,f) ((m)->flags ^= (f))
// Static mission data.
typedef struct MissionData_ { typedef struct MissionData_ {
char* name; // the name of the mission. char* name; // the name of the mission.
@ -34,6 +35,8 @@ typedef struct MissionData_ {
// Active mission. // Active mission.
typedef struct Mission_ { typedef struct Mission_ {
MissionData* data; MissionData* data;
// Unique mission identifier, used for keeping track of hooks.
unsigned int id;
char* title; // Not to be confused with name.. char* title; // Not to be confused with name..
char* desc; // Description of the mission. char* desc; // Description of the mission.