From c769e71c720dda2befda78214ec18837d36f5d18 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 5 May 2013 21:07:07 +0100
Subject: [PATCH] [Fixing] Y NO WERK!?

---
 dat/missions/cargo.lua |  4 ++--
 src/misn_lua.c         | 22 ++++++++++++----------
 src/mission.c          | 11 ++++++-----
 src/space.c            |  2 ++
 4 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua
index 0007a0c..31e7e20 100644
--- a/dat/missions/cargo.lua
+++ b/dat/missions/cargo.lua
@@ -67,13 +67,13 @@ function create()
   end
   
   if misn_type == "Cargo" then
-    misn_setDesc(string.format(misn_desc[1], planet, system, carg_mass, carg_type))
+    misn.setDesc(string.format(misn_desc[1], planet, system, carg_mass, carg_type))
     reward = misn_dist * carg_mass * (250+rnd.int(150)) +
         carg_mass * (150+rnd.int(75)) +
         rnd.int(1500)
   elseif misn_type == "Rush" then
     misn_time = time.get() + time.units(2) +
-        rnd.int(time.units(2), time.units(4)) * misn_dest
+        rnd.int(time.units(2), time.units(4)) * misn_dist
     misn.setDesc(string.format(misn_desc[2], planet, system,
         carg_mass, carg_type, time.str(misn_time),
         time.str(misn_time), time.str(misn_time-time.get())))
diff --git a/src/misn_lua.c b/src/misn_lua.c
index 3d9f1bd..631a2fb 100644
--- a/src/misn_lua.c
+++ b/src/misn_lua.c
@@ -1,5 +1,4 @@
 #include "lua.h"
-#include "lualib.h"
 #include "lauxlib.h"
 
 #include "hook.h"
@@ -67,7 +66,7 @@ static int misn_setReward(lua_State* L);
 static int misn_factions(lua_State* L);
 static int misn_accept(lua_State* L);
 static int misn_finish(lua_State* L);
-static const luaL_Reg misn_methods[] = {
+static const luaL_reg misn_methods[] = {
   { "setTitle",     misn_setTitle   },
   { "setDesc",      misn_setDesc    },
   { "setReward",    misn_setReward  },
@@ -93,7 +92,7 @@ static int space_getPlanet(lua_State* L);
 static int space_getSystem(lua_State* L);
 static int space_landName(lua_State* L);
 static int space_jumpDist(lua_State* L);
-static const luaL_Reg space_methods[] = {
+static const luaL_reg space_methods[] = {
   { "getPlanet",    space_getPlanet },
   { "getSystem",    space_getSystem },
   { "landName",     space_landName  },
@@ -118,7 +117,7 @@ static int player_addCargo(lua_State* L);
 static int player_rmCargo(lua_State* L);
 static int player_pay(lua_State* L);
 static int player_msg(lua_State* L);
-static const luaL_Reg player_methods[] = {
+static const luaL_reg player_methods[] = {
   { "freeCargo",    player_freeSpace  },
   { "addCargo",     player_addCargo   },
   { "rmCargo",      player_rmCargo    },
@@ -129,7 +128,7 @@ static const luaL_Reg player_methods[] = {
 
 // RND.
 static int rnd_int(lua_State*L);
-static const luaL_Reg rnd_methods[] = {
+static const luaL_reg rnd_methods[] = {
   { "int",    rnd_int },
   { 0, 0 }
 };
@@ -138,7 +137,7 @@ static const luaL_Reg rnd_methods[] = {
 static int tk_msg(lua_State* L);
 static int tk_yesno(lua_State* L);
 static int tk_input(lua_State* L);
-static const luaL_Reg tk_methods[] = {
+static const luaL_reg tk_methods[] = {
   { "msg",    tk_msg    },
   { "yesno",  tk_yesno  },
   { "input",  tk_input  },
@@ -150,7 +149,7 @@ static int hook_land(lua_State* L);
 static int hook_takeoff(lua_State* L);
 static int hook_time(lua_State* L);
 static int hook_pilotDeath(lua_State* L);
-static const luaL_Reg hook_methods[] = {
+static const luaL_reg hook_methods[] = {
   { "land",       hook_land       },
   { "takeoff",    hook_takeoff    },
   { "time",       hook_time       },
@@ -167,8 +166,8 @@ int misn_loadLibs(lua_State* L) {
   luaL_register(L, "time",      time_methods);
   luaL_register(L, "player",    player_methods);
   luaL_register(L, "rnd",       rnd_methods);
-  luaL_register(L, "hook",      hook_methods);
   luaL_register(L, "tk",        tk_methods);
+  luaL_register(L, "hook",      hook_methods);
   return 0;
 }
 
@@ -178,6 +177,7 @@ int misn_run(Mission* misn, char* func) {
   char* err;
 
   cur_mission = misn;
+  misn_delete = 0;
 
   lua_getglobal(misn->L, func);
   if((ret = lua_pcall(misn->L, 0, 0, 0))) {
@@ -252,6 +252,7 @@ static int misn_factions(lua_State* L) {
 
   dat = cur_mission->data;
 
+  // We'll push all the factions in table form.
   lua_newtable(L);
   for(i = 0; i < dat->avail.nfactions; i++) {
     lua_pushnumber(L, i+1);   // Index, starts with 1.
@@ -471,7 +472,7 @@ static int space_getPlanet(lua_State* L) {
     i = lua_tonumber(L, -1);
     planets = space_getFactionPlanet(&nplanets, &i, 1);
   }
-  else if(lua_isstring(L, 1)) {
+  else if(lua_isstring(L, -1)) {
     i = faction_get((char*) lua_tostring(L, -1));
     planets = space_getFactionPlanet(&nplanets, &i, 1);
   }
@@ -718,7 +719,8 @@ static int hook_generic(lua_State* L, char* stack) {
   char* func;
   
   MIN_ARGS(1);
-
+  
+  // Make sure mission is a player mission.
   for(i = 0; i < MISSION_MAX; i++)
     if(player_missions[i].id == cur_mission->id)
       break;
diff --git a/src/mission.c b/src/mission.c
index 7618a30..02a1c8b 100644
--- a/src/mission.c
+++ b/src/mission.c
@@ -82,6 +82,7 @@ static int mission_init(Mission* mission, MissionData* misn) {
   luaopen_string(mission->L); // string.format can be very useful.
   misn_loadLibs(mission->L);  // Load our custom libraries.
 
+  // Load the file.
   buf = pack_readfile(DATA, misn->lua, &bufsize);
   if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
     ERR("Error loading mission file: %s", misn->lua);
@@ -263,7 +264,7 @@ Mission* missions_computer(int* n, int faction, char* planet, char* system) {
 
 // Return location based on string.
 static int mission_location(char* loc) {
-  if(strcmp(loc, "None")==0)            return MIS_AVAIL_NONE;
+  if(strcmp(loc,      "None")==0)       return MIS_AVAIL_NONE;
   else if(strcmp(loc, "Computer")==0)   return MIS_AVAIL_COMPUTER;
   else if(strcmp(loc, "Bar")==0)        return MIS_AVAIL_BAR;
   else if(strcmp(loc, "Outfit")==0)     return MIS_AVAIL_OUTFIT;
@@ -301,7 +302,7 @@ static MissionData* mission_parse(const xmlNodePtr parent) {
       do {
         if(xml_isNode(cur, "unique"))
           mis_setFlag(tmp, MISSION_UNIQUE);
-      } while((cur = cur->next));
+      } while(xml_nextNode(cur));
     }
     else if(xml_isNode(node, "avail")) { // Mission availability.
       cur = node->children;
@@ -328,9 +329,9 @@ static MissionData* mission_parse(const xmlNodePtr parent) {
         }
         else if(xml_isNode(cur, "req"))
           tmp->avail.req = strdup(xml_get(cur));
-      } while((cur = cur->next));
+      } while(xml_nextNode(cur));
     }
-  } while((node = node->next));
+  } while(xml_nextNode(node));
 #define MELEMENT(o,s) \
   if(o) WARN("Mission '%s' missing/invalid '"s"' element", tmp->name)
   MELEMENT(tmp->lua==NULL, "lua");
@@ -371,7 +372,7 @@ int missions_load(void) {
       memcpy(mission_stack+mission_nstack-1, tmp, sizeof(MissionData));
       free(tmp);
     }
-  } while((node = node->next));
+  } while(xml_nextNode(node));
 
   xmlFreeDoc(doc);
   free(buf);
diff --git a/src/space.c b/src/space.c
index 45d1af8..3b6df35 100644
--- a/src/space.c
+++ b/src/space.c
@@ -1016,6 +1016,8 @@ void space_exit(void) {
   int i,j;
   
   // Free the names.
+  //if(planetname_stack) free(planetname_stack);
+  //if(systemname_stack) free(systemname_stack);
   if(planetname_stack) {
   	 free(planetname_stack);
   	 planetname_stack = NULL;