From 3da2a4086b548c1c6a6d95cac553aa13d94e80e1 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 3 Apr 2013 19:38:00 +0100
Subject: [PATCH] [Fix] toolkit module for lua and accpet mission bug.

---
 dat/missions/cargo.lua |  4 +--
 src/land.c             | 10 ++++++
 src/misn_lua.c         | 72 ++++++++++++++++++++++++++++++++++++++----
 src/mission.c          | 11 ++++---
 src/mission.h          |  2 +-
 src/toolkit.c          | 38 ++++++++++++++++++++++
 src/toolkit.h          |  1 +
 7 files changed, 124 insertions(+), 14 deletions(-)

diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua
index 9f7671f..57606d4 100644
--- a/dat/missions/cargo.lua
+++ b/dat/missions/cargo.lua
@@ -19,7 +19,7 @@ end
 
 function accept()
   player.addCargo(carg_type, carg_mass)
-  toolkit.msg("Mission Accepted",
+  tk.msg("Mission Accepted",
         string.format("The workers load the %d tons of %s onto your ship.",
         carg_mass, carg_type))
   hook.land("land")
@@ -29,7 +29,7 @@ function land()
   if planet.name() == planet then
     player.rmCargo(carg_type, carg_mass)
     player.pay(misn_reward)
-    toolkit.msg("Mission Accomplished",
+    tk.msg("Mission Accomplished",
           string.format("The workers unload the %s at the docks.", carg_type))
   
   -- Set the hooks.
diff --git a/src/land.c b/src/land.c
index 0dc7e35..d594208 100644
--- a/src/land.c
+++ b/src/land.c
@@ -825,10 +825,20 @@ static void misn_close(char* str) {
 }
 
 static void misn_accept(char* str) {
+  int i;
   char* misn_name;
   (void)str;
 
   misn_name = toolkit_getList(secondary_wid, "lstMission");
+
+  if(strcmp(misn_name, "No Missions")==0) return;
+
+  for(i = 0; i < mission_ncomputer; i++)
+    if(mission_computer[i].title &&
+        (strcmp(misn_name, mission_computer[i].title)==0)) {
+      mission_accept(&mission_computer[i]);
+      return;
+    }
 }
 
 static void misn_genList(int first) {
diff --git a/src/misn_lua.c b/src/misn_lua.c
index 3ff195c..c425f16 100644
--- a/src/misn_lua.c
+++ b/src/misn_lua.c
@@ -8,6 +8,7 @@
 #include "lephisto.h"
 #include "rng.h"
 #include "space.h"
+#include "toolkit.h"
 #include "misn_lua.h"
 
 // FUCK LUA!!!                                                                  
@@ -52,17 +53,28 @@ static const luaL_Reg player_methods[] = {
   { 0, 0 }
 };
 
-// RNG.
+// RND.
 static int rnd_int(lua_State*L);
 static const luaL_Reg rnd_methods[] = {
   { "int",    rnd_int },
   { 0, 0 }
 };
 
+// Toolkit.
+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[] = {
+  { "msg",    tk_msg    },
+  { "yesno",  tk_yesno  },
+  { "input",  tk_input  },
+  { 0, 0 }
+};
+
 static int hook_land(lua_State* L);
 static const luaL_Reg hook_methods[] = {
   { "land", hook_land },
-  { 0, 0 },
+  { 0, 0 }
 };
 
 // Register all the libaries.
@@ -72,6 +84,7 @@ int misn_loadLibs(lua_State* L) {
   luaL_register(L, "player",  player_methods);
   luaL_register(L, "rnd",     rnd_methods);
   luaL_register(L, "hook",    hook_methods);
+  luaL_register(L, "tk",      tk_methods);
   return 0;
 }
 
@@ -152,9 +165,9 @@ static int player_addCargo(lua_State* L) {
 
   MIN_ARGS(2);
 
-  if(lua_isstring(L, -1)) cargo = commodity_get((char*) lua_tostring(L, -1));
+  if(lua_isstring(L, -2)) cargo = commodity_get((char*) lua_tostring(L, -2));
   else return 0;
-  if(lua_isnumber(L, -2)) quantity = (int)lua_tonumber(L, -2);
+  if(lua_isnumber(L, -1)) quantity = (int)lua_tonumber(L, -1);
   else return 0;
 
   ret = pilot_addCargo(player, cargo, quantity);
@@ -169,9 +182,9 @@ static int player_rmCargo(lua_State* L) {
 
   MIN_ARGS(2);
 
-  if(lua_isstring(L, -1)) cargo = commodity_get((char*) lua_tostring(L, -1));
+  if(lua_isstring(L, -2)) cargo = commodity_get((char*) lua_tostring(L, -2));
   else return 0;
-  if(lua_isnumber(L, -2)) quantity = (int) lua_tonumber(L, -2);
+  if(lua_isnumber(L, -1)) quantity = (int) lua_tonumber(L, -1);
   else return 0;
 
   ret = pilot_rmCargo(player, cargo, quantity);
@@ -193,6 +206,7 @@ static int player_pay(lua_State* L) {
   return 0;
 }
 
+// -- RND. --
 static int rnd_int(lua_State* L) {
   int o;
 
@@ -217,6 +231,52 @@ static int rnd_int(lua_State* L) {
   return 1;
 }
 
+// -- Toolkit. --
+
+static int tk_msg(lua_State* L) {
+  char* title, *str;
+  MIN_ARGS(2);
+
+  if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2);
+  else return 0;
+  if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
+  else return 0;
+
+  dialogue_msg(title, str);
+  return 0;
+}
+
+static int tk_yesno(lua_State* L) {
+  char* title, *str;
+  MIN_ARGS(2);
+
+  if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2);
+  else return 0;
+  if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
+  else return 0;
+
+  dialogue_YesNo(title, str);
+  return 0;
+}
+
+static int tk_input(lua_State* L) {
+  char* title, *str;
+  int min, max;
+  MIN_ARGS(4);
+
+  if(lua_isstring(L, -4)) title = (char*) lua_tostring(L, -4);
+  else return 0;
+  if(lua_isnumber(L, -3)) min = (int) lua_tonumber(L, -3);
+  else return 0;
+  if(lua_isnumber(L, -2)) max = (int) lua_tonumber(L, -2);
+  else return 0;
+  if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
+  else return 0;
+
+  dialogue_input(title, min, max, str);
+  return 0;
+}
+
 // -- HOOK --
 static int hook_land(lua_State* L) {
   char* func;
diff --git a/src/mission.c b/src/mission.c
index 3a13781..ef2ad6c 100644
--- a/src/mission.c
+++ b/src/mission.c
@@ -80,8 +80,8 @@ static int mission_init(Mission* mission, MissionData* misn) {
   return mission->id;
 }
 
-// Add a mission to the player, you can free the current mission safely.
-int mission_add(Mission* mission) {
+// Accept mission, used basically for mission computers.
+void mission_accept(Mission* mission) {
   int i;
 
   // Find last mission.
@@ -89,13 +89,14 @@ int mission_add(Mission* mission) {
     if(player_missions[i].data == NULL) break;
 
   // No missions left.
-  if(i >= MISSION_MAX) return -1;
+  if(i >= MISSION_MAX) return;
 
   // Copy it over.
   memcpy(&player_missions[i], mission, sizeof(Mission));
   memset(mission, 0, sizeof(Mission));
-
-  return player_missions[i].id;
+  
+  // Run the accept commandz.
+  misn_run(&player_missions[i], "accept");
 }
 
 // Clean up a mission.
diff --git a/src/mission.h b/src/mission.h
index 0ccdaee..92a1331 100644
--- a/src/mission.h
+++ b/src/mission.h
@@ -58,7 +58,7 @@ extern Mission player_mission[MISSION_MAX];
 // For mission computer.
 Mission* missions_computer(int* n, int faction, char* planet, char* system);
 // Player accepted mission - mission computer.
-void mission_accept(Mission* misn);
+void mission_accept(Mission* mission);
 
 // Load/Quit.
 int missions_load(void);
diff --git a/src/toolkit.c b/src/toolkit.c
index 2bef1b4..108a63e 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -148,6 +148,7 @@ static void toolkit_drawRect(double x, double y, double w, double h,
                              glColour* c, glColour* lc);
 // Dialogues..
 static void dialogue_alertClose(char* str);
+static void dialogue_msgClose(char* str);
 static void dialogue_YesNoClose(char* str);
 static void dialogue_inputClose(char* str);
 // Secondary loop hack.
@@ -1404,6 +1405,43 @@ static void dialogue_alertClose(char* str) {
     window_destroy(window_get("Warning"));
 }
 
+// Display an alert popup with only an OK button and a message.
+static unsigned int msg_wid = 0;
+void dialogue_msg(char* caption, const char* fmt, ...) {
+  char msg[256];
+  va_list ap;
+  int h;
+
+  if(msg_wid) return;
+
+  if(fmt == NULL) return;
+  else {
+    // Get the message.
+    va_start(ap, fmt);
+    vsprintf(msg, fmt, ap);
+    va_end(ap);
+  }
+
+  h = gl_printHeight(&gl_smallFont, 260, msg);
+
+  // Create the window.
+  msg_wid = window_create(caption, -1, -1, 300, 90+h);
+  window_addText(msg_wid, 20, -30, 260, h, 0, "txtMsg",
+                 &gl_smallFont, &cBlack, msg);
+
+  window_addButton(msg_wid, 135, 20, 50, 30, "btnOK", "OK",
+                 dialogue_msgClose);
+
+  toolkit_loop();
+}
+
+static void dialogue_msgClose(char* str) {
+  (void)str;
+  window_destroy(msg_wid);
+  msg_wid = 0;
+  loop_done = 0;
+}
+
 // Runs a dialogue with a Yes No button, return 1 if yes.
 static int yesno_result;
 static unsigned int yesno_wid = 0;
diff --git a/src/toolkit.h b/src/toolkit.h
index e095d8a..03358a8 100644
--- a/src/toolkit.h
+++ b/src/toolkit.h
@@ -45,6 +45,7 @@ void window_addInput(const unsigned int wid,
 
 // Popups and alerts.
 void  dialogue_alert(const char* fmt, ...);
+void  dialogue_msg(char* caption, const char* fmt, ...);
 int   dialogue_YesNo(char* caption, const char* fmt, ...);
 char* dialogue_input(char* title, int min, int max, const char* fmt, ...);