From b565e6e6cb8ad6f2d320d73d6e229a5ad89b8c43 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sat, 30 Mar 2013 21:47:27 +0000
Subject: [PATCH] [Add] Give active mission ids and use the ids for removing
 hooks.

---
 src/hook.c    | 18 +++++++++---------
 src/hook.h    |  4 ++--
 src/land.c    |  3 +++
 src/mission.c |  2 ++
 src/mission.h |  3 +++
 5 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/hook.c b/src/hook.c
index 0d94a83..590a4b3 100644
--- a/src/hook.c
+++ b/src/hook.c
@@ -9,16 +9,16 @@
 typedef struct Hook_ {
   int id;
   lua_State* L;
-  char* parent;
+  unsigned int  parent;
   char* func;
   char* stack;
 } Hook;
 
 // The stack.
-static int    hook_id     = 0; // Unique hook id.
-static Hook*  hook_stack  = NULL;
-static int    hook_mstack = 0;
-static int    hook_nstack = 0;
+static unsigned int     hook_id     = 0; // Unique hook id.
+static Hook*            hook_stack  = NULL;
+static int              hook_mstack = 0;
+static int              hook_nstack = 0;
 
 int hook_run(Hook* hook) {
   lua_State* L;
@@ -28,14 +28,14 @@ int hook_run(Hook* hook) {
   lua_getglobal(L, hook->func);
   if(lua_pcall(L, 0, 0, 0))
     // 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));
 
   return 0;
 }
 
 // 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;
 
   // If the memory must grow.
@@ -76,10 +76,10 @@ void hook_rm(int id) {
   hook_nstack--;
 }
 
-void hook_rmParent(char* parent) {
+void hook_rmParent(unsigned int parent) {
   int 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);
       i--;
     }
diff --git a/src/hook.h b/src/hook.h
index 18385c0..5ce0565 100644
--- a/src/hook.h
+++ b/src/hook.h
@@ -2,9 +2,9 @@
 #include "lua.h"
 
 // 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_rmParent(char* parent);
+void hook_rmParent(unsigned int parent);
 
 // Run hook.
 int hooks_run(char* stack);
diff --git a/src/land.c b/src/land.c
index 4f41ccb..b91cd88 100644
--- a/src/land.c
+++ b/src/land.c
@@ -5,6 +5,7 @@
 #include "rng.h"
 #include "music.h"
 #include "economy.h"
+#include "hook.h"
 #include "land.h"
 
 // Global/main window.
@@ -765,6 +766,7 @@ void land(Planet* p) {
 
 
   landed = 1;
+  hooks_run("land");
 }
 
 // Takeoff from the planet.
@@ -797,5 +799,6 @@ void takeoff(void) {
   land_planet = NULL;
   window_destroy(land_wid);
   landed = 0;
+  hooks_run("takeoff");
 }
 
diff --git a/src/mission.c b/src/mission.c
index ede6249..2a2886c 100644
--- a/src/mission.c
+++ b/src/mission.c
@@ -7,6 +7,7 @@
 
 #include "lephisto.h"
 #include "log.h"
+#include "hook.h"
 #include "mission.h"
 
 // Current player missions.
@@ -46,6 +47,7 @@ int mission_create(MissionData* misn) {
 
 // Clean up a mission.
 static void mission_cleanup(Mission* misn) {
+  hook_rmParent(misn->id); // Remove existing hooks.
   misn->data = NULL;
   if(misn->title) free(misn->title);
   if(misn->desc)  free(misn->desc);
diff --git a/src/mission.h b/src/mission.h
index c3504eb..826ca35 100644
--- a/src/mission.h
+++ b/src/mission.h
@@ -13,6 +13,7 @@
 #define mis_setFlag(m,f)  ((m)->flags |= (f))
 #define mis_rmFlag(m,f)   ((m)->flags ^= (f))
 
+// Static mission data.
 typedef struct MissionData_ {
   char* name; // the name of the mission.
 
@@ -34,6 +35,8 @@ typedef struct MissionData_ {
 // Active mission.
 typedef struct Mission_ {
   MissionData* data;
+  // Unique mission identifier, used for keeping track of hooks.
+  unsigned int id;
 
   char* title;  // Not to be confused with name..
   char* desc;   // Description of the mission.