From 21d25a5585c5b34203731265330435e5d12a18e0 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Thu, 11 Apr 2013 18:15:09 +0100
Subject: [PATCH] [Add] New Empire shipping cargo mission.

---
 dat/mission.xml           | 14 ++++++-
 dat/missions/es_cargo.lua | 78 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 dat/missions/es_cargo.lua

diff --git a/dat/mission.xml b/dat/mission.xml
index e8f9f73..8b8bbd7 100644
--- a/dat/mission.xml
+++ b/dat/mission.xml
@@ -8,7 +8,7 @@
       <location>None</location>
     </avail>
   </mission>
-  <mission name="Cargo Freight">
+  <mission name="Cargo">
     <lua>cargo</lua>
     <avail>
       <chance>750</chance>
@@ -29,5 +29,17 @@
       <alliance>Neutral</alliance>
     </avail>
   </mission>
+  <mission name = "Empire Shipping">
+    <lua>es_cargo</lua>
+    <req>
+      <flag>req_esd</flag>
+    </req>
+    <avail>
+      <chance>350</chance>
+      <location>Computer</location>
+      <alliance>Empire United</alliance>
+      <alliance>Neutral</alliance>
+    </avail>
+  </mission>
 </Missions>
 
diff --git a/dat/missions/es_cargo.lua b/dat/missions/es_cargo.lua
new file mode 100644
index 0000000..3e8bb57
--- /dev/null
+++ b/dat/missions/es_cargo.lua
@@ -0,0 +1,78 @@
+lang = lephisto.lang()
+if lang == "es" then
+  -- Not translated yet.
+else -- Default English.
+  misn_desc = "The Empire needs to ship %d tons of %s to %s in the %s system."
+  misn_reward = "%d Scred"
+  title = {}
+  title[1] = "ES: Ship to %s"
+  title[2] = "ES: Delivery to %s"
+  full_title = "Ship is full"
+  full_msg = "Your ship is too full. You need to make room for %d more tons if you want to be able to accept the mission."
+  accept_title = "Mission Accepted"
+  accept_msg  = "The Empire workers load %d tons of %s onto your ship."
+  toomany_title = "Too many missions"
+  toomany_msg = "You have too many active missions."
+  finish_title  = "Succesful Delivery"
+  finish_msg  = "The Empire workers unload the %s at the docks."
+  miss_title  = "Cargo Missing"
+  miss_msg  = "You are missing the %d tons of %s!"
+end
+
+-- Create the mission.
+function create()
+  -- Target destination.
+  local i = 0
+  repeat
+    planet = space.getPlanet(misn.factions())
+    i = i + 1
+  until planet ~= space.landName() or i > 10
+
+  -- Protect against inf loop.
+  if i > 10 then
+    misn.finish(false)
+  end
+  system = space.getSystem(planet)
+
+  -- Mission generics.
+  misn_type = "Cargo"
+  i = rnd.int(1)
+  misn.setTitle(string.format(title[i+1], planet))
+
+  -- More mission specifics.
+  carg_mass = rnd.int(10, 30)
+  i = rnd.int(1)
+  if i == 0 then carg_type = "Food"
+  elseif i == 1 then carg_type = "Ore"
+  end
+
+  misn.setDesc(string.format(misn_desc, carg_mass, carg_type, planet, system))
+  reward = carg_mass * (1250+rnd.int(250)) + rnd.int(7500)
+  misn.setReward(string.format(misn_reward, reward))
+end
+
+-- Mission is accepted.
+function accept()
+  if player.freeCargo() < carg_mass then
+    tk.msg(full_title, string.format(full_msg, carg_mass-player.freeCargo()))
+  elseif misn.accept() then -- Able to accept the mission, hoos BREAK after accepting.
+    tk.msg(accept_title, string.format(accept_msg, carg_mass, carg_type))
+    hook.lnad("land") -- Only hook after accepting.
+  else
+    tk.msg(toomany_title, toomany_msg)
+  end
+end
+
+-- Land hook.
+function land()
+  if space.landName() == planet then
+    if player.rmCargo(carg_id) then
+      player.pay(reward)
+      tk.msg(finish_title, string.format(finish_msg, carg_type))
+      misn.finish(true)
+    else
+      tk.msg(miss_title, string.format(miss_msg, carg_mass, carg_type))
+    end
+  end
+end
+