From 2a502f5426162423896ad829a5167517a25dc454 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Thu, 2 May 2013 20:18:34 +0100
Subject: [PATCH] [Add] First time limit mission.

---
 dat/missions/cargo.lua | 67 ++++++++++++++++++++++++++++++------------
 src/misn_lua.c         | 19 +++++++++---
 src/mission.c          |  2 +-
 3 files changed, 65 insertions(+), 23 deletions(-)

diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua
index 2bf24b3..0239551 100644
--- a/dat/missions/cargo.lua
+++ b/dat/missions/cargo.lua
@@ -2,13 +2,16 @@ lang = lephisto.lang()
 if lang == "es" then
   -- Not translated.
 else -- Default English.
-  misn_desc     = "%s in the %s system needs a delivery of %d tons of %s."
+  misn_desc     = {}
+  misn_desc[1]  = "%s in the %s system needs a delivery of %d tons of %s."
+  misn_desc[2]  = "%s in the %s system needs a rush delivery of %d tons of %s before %s."
   misn_reward   = "%d Scred."
   title         = {}
   title[1]      = "Cargo delivery to %s"
   title[2]      = "Freight delivery to %s"
   title[3]      = "Transport to %s"
   title[4]      = "Delivery to %s"
+  title[5]      = "Rush Delivery to %s"
   full_title    = "Ship is full"
   full_msg      = "Your ship is too full. You need to make room for %d tons if you want to be able to accept the mission."
   accept_title  = "Mission Accepted"
@@ -37,29 +40,43 @@ function create()
   misn_dist = space.jumpDist(system)
 
   -- Missions generic.
-  misn_type = "Cargo"
-  i = rnd.int(3)
-  misn.setTitle(string.format(title[i+1], planet))
+  i = rnd.int(4)
+  if i < 4 then -- Cargo delivery.
+    misn_type = "Cargo"
+    i = rnd.int(3)
+    misn.setTitle(string.format(title[i+1], planet))
+  else -- Rush delivery.
+    misn_type = "Rush"
+    misn.setTitle(string.format(title[5], planet))
+  end
 
   -- More mission specifics.
   carg_mass = rnd.int(10, 30)
-  i = rnd.int(12)                                                              
-    if i < 5 then                                                               
-      carg_type = "Food"                                                              
-    elseif i < 8 then                                                           
-      carg_type = "Ore"                                                               
-    elseif i < 10 then                                                          
-      carg_type = "Industrial Goods"                                                  
-    elseif i < 12 then                                                          
-      carg_type = "Luxury Goods"                                                      
-    else                                                                          
+  i = rnd.int(12) -- Set the goods.
+    if i < 5 then
+      carg_type = "Food"
+    elseif i < 8 then
+      carg_type = "Ore"
+    elseif i < 10 then
+      carg_type = "Industrial Goods"
+    elseif i < 12 then
+      carg_type = "Luxury Goods"
+    else
       carg_type = "Medicine"
   end
   
-  misn.setDesc(string.format(misn_desc, planet, system, carg_mass, carg_type))
-  reward = misn_dist * carg_mass * (250+rnd.int(150)) +
-      carg_mass * (150+rnd.int(75)) +
-      rnd.int(1500)
+  if misn_type == "Cargo" then
+    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() + 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)))
+    reward = misn_dist * carg_mass * (450+rnd.int(250)) + rnd.int(3500)
+  end
+
   misn.setReward(string.format(misn_reward, reward))
 end
 
@@ -69,7 +86,13 @@ function accept()
     elseif misn.accept() then -- Able to accept the mission, hooks BREAK after accepting.
       carg_id = player.addCargo(carg_type, carg_mass)
       tk.msg(accept_title, string.format(accept_msg, carg_mass, carg_type))
+     
+      -- Set the hooks.
       hook.land("land"); -- Only hook after accepting.
+      if misn_type == "Rush" then -- Rush needs additional time hook.
+        hook.time("timeup")
+      end
+
     else
       tk.msg(toomany_title, toomany_msg)
     end
@@ -87,3 +110,11 @@ function land()
   end
 end
 
+-- Time hook.
+function timeup()
+  if time.get() > misn_time then
+    player.msg("You have failed to deliver the goods on time!")
+    misn.finish(false)
+  end
+end
+
diff --git a/src/misn_lua.c b/src/misn_lua.c
index d4c2a8e..13151cb 100644
--- a/src/misn_lua.c
+++ b/src/misn_lua.c
@@ -116,11 +116,13 @@ static int player_freeSpace(lua_State* L);
 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[] = {
   { "freeCargo",    player_freeSpace  },
   { "addCargo",     player_addCargo   },
   { "rmCargo",      player_rmCargo    },
   { "pay",          player_pay        },
+  { "msg",          player_msg        },
   { 0, 0 }
 };
 
@@ -280,10 +282,8 @@ static int misn_finish(lua_State* L) {
   int b;
 
   if(lua_isboolean(L, -1)) b = lua_toboolean(L, -1);
-  else {
-    MISN_DEBUG("Trying to finish without specifyint if mission is complete.");
-    return 0;
-  }
+  else return 0; // With no argument it won't delete the mission.
+  
   misn_delete = 1;
 
   if(b && mis_isFlag(cur_mission->data, MISSION_UNIQUE))
@@ -625,6 +625,17 @@ static int player_pay(lua_State* L) {
   return 0;
 }
 
+static int player_msg(lua_State* L) {
+  MIN_ARGS(1);
+  char* str;
+
+  if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
+  else return 0;
+
+  player_message(str);
+  return 0;
+}
+
 // -- RND. --
 static int rnd_int(lua_State* L) {
   int o;
diff --git a/src/mission.c b/src/mission.c
index 12c0b5a..7618a30 100644
--- a/src/mission.c
+++ b/src/mission.c
@@ -84,7 +84,7 @@ static int mission_init(Mission* mission, MissionData* misn) {
 
   buf = pack_readfile(DATA, misn->lua, &bufsize);
   if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
-    ERR("Error loading AI file: %s", misn->lua);
+    ERR("Error loading mission file: %s", misn->lua);
     ERR("%s", lua_tostring(mission->L, -1));
     WARN("Most likely Lua file has improper syntax, please check it");
     return -1;