From 82ee8a41a7711c4e6e2d51d0338ba07b9e58430e Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 24 Apr 2013 18:05:49 +0100
Subject: [PATCH] [Add] Cargo missions take into account fright distance.

---
 dat/missions/cargo.lua    |  6 ++++--
 dat/missions/es_cargo.lua |  5 ++++-
 src/misn_lua.c            | 30 ++++++++++++++++++++++++++++--
 src/space.h               | 22 +++++++++++-----------
 4 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua
index bca2bd2..c83ad74 100644
--- a/dat/missions/cargo.lua
+++ b/dat/missions/cargo.lua
@@ -33,8 +33,8 @@ function create()
   if i > 10 then
     misn.finish(false)
   end
-
   system = space.getSystem(planet)
+  misn_dist = space.jumpDist(system)
 
   -- Missions generic.
   misn_type = "Cargo"
@@ -48,7 +48,9 @@ function create()
   elseif i == 1 then carg_type = "Ore"
   end
   misn.setDesc(string.format(misn_desc, planet, system, carg_mass, carg_type))
-  reward = carg_mass * (750 + rnd.int(250)) + rnd.int(5000)
+  reward = misn_dist * carg_mass * (250+rnd.int(150)) +
+      carg_mass * (150+rnd.int(75)) +
+      rnd.int(1500)
   misn.setReward(string.format(misn_reward, reward))
 end
 
diff --git a/dat/missions/es_cargo.lua b/dat/missions/es_cargo.lua
index a745ee1..5a7d431 100644
--- a/dat/missions/es_cargo.lua
+++ b/dat/missions/es_cargo.lua
@@ -33,6 +33,7 @@ function create()
     misn.finish(false)
   end
   system = space.getSystem(planet)
+  misn_dist = space.jumpDist(system)
 
   -- Mission generics.
   misn_type = "Cargo"
@@ -47,7 +48,9 @@ function create()
   end
 
   misn.setDesc(string.format(misn_desc, carg_mass, carg_type, planet, system))
-  reward = carg_mass * (1250+rnd.int(250)) + rnd.int(7500)
+  reward = misn_dist * carg_mass * (500+rnd.int(250)) +
+      carg_mass * (250+rnd.int(150)) +
+      rnd.int(2500)
   misn.setReward(string.format(misn_reward, reward))
 end
 
diff --git a/src/misn_lua.c b/src/misn_lua.c
index 6aabc87..d4c2a8e 100644
--- a/src/misn_lua.c
+++ b/src/misn_lua.c
@@ -91,10 +91,12 @@ static const luaL_reg var_methods[] = {
 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[] = {
   { "getPlanet",    space_getPlanet },
   { "getSystem",    space_getSystem },
   { "landName",     space_landName  },
+  { "jumpDist",     space_jumpDist  },
   { 0, 0 }
 };
 
@@ -501,9 +503,8 @@ static int space_getPlanet(lua_State* L) {
 }
 
 static int space_getSystem(lua_State* L) {
-  char* planetname, *system;
-
   MIN_ARGS(1);
+  char* planetname, *system;
 
   if(lua_isstring(L, -1)) planetname = (char*) lua_tostring(L, -1);
   else return 0;
@@ -521,6 +522,31 @@ static int space_landName(lua_State* L) {
   return 0;
 }
 
+static int space_jumpDist(lua_State* L) {
+  MIN_ARGS(1);
+  StarSystem** s;
+  int jumps;
+  char* start, *goal;
+
+  if(lua_isstring(L, -1))
+    start = (char*)lua_tostring(L, -1);
+  else {
+    MISN_DEBUG("invalid parameter 1");
+    return 0;
+  }
+
+  if((lua_gettop(L) > 1) && lua_isstring(L, -2))
+    goal = (char*) lua_tostring(L, -2);
+  else
+    goal = cur_system->name;
+
+  s = system_getJumpPath(&jumps, start, goal);
+  free(s);
+
+  lua_pushnumber(L, jumps);
+  return 1;
+}
+
 // -- Time. --
 static int time_get(lua_State* L) {
   lua_pushnumber(L, ltime_get());
diff --git a/src/space.h b/src/space.h
index 5e28907..7460e26 100644
--- a/src/space.h
+++ b/src/space.h
@@ -39,7 +39,7 @@ typedef enum PlanetClass_ {
 } PlanetClass;
 
 // Planet services.
-#define PLANET_SERVICE_LAND    (1<<0) // Can we land?
+#define PLANET_SERVICE_LAND       (1<<0) // Can we land?
 #define PLANET_SERVICE_BASIC      (1<<1) // Refueling, spaceport bar, new.
 #define PLANET_SERVICE_COMMODITY  (1<<2)
 #define PLANET_SERVICE_OUTFITS    (1<<3)
@@ -47,24 +47,24 @@ typedef enum PlanetClass_ {
 #define planet_hasService(p,s)    ((p)->services & s)
 
 typedef struct Planet_ {
-  char* name; // Planet name
-  Vec2 pos;  // Position in star system.
+  char* name;               // Planet name
+  Vec2 pos;                 // Position in star system.
 
-  PlanetClass class;  // Planet type.
-  int faction;  // Planet faction.
+  PlanetClass class;        // Planet type.
+  int faction;              // Planet faction.
 
-  char* description;     // Planet description.
-  char* bar_description;   // Spaceport bar description.
-  unsigned int services;   // Offered services.
+  char* description;        // Planet description.
+  char* bar_description;    // Spaceport bar description.
+  unsigned int services;    // Offered services.
   Commodity** commodities;  // Commodities sold.
-  int ncommodities;     // Amount in stock.
+  int ncommodities;         // Amount in stock.
 
   // tech[0] stores global tech level (everything that and below) while
   // tech[1-PLANET_TECH_MAX] stores the unique tech levels.
   int tech[PLANET_TECH_MAX];
 
-  glTexture* gfx_space;   // Graphics in space.
-  glTexture* gfx_exterior; // Graphics in the exterior.
+  glTexture* gfx_space;     // Graphics in space.
+  glTexture* gfx_exterior;  // Graphics in the exterior.
 } Planet;
 
 // Star systems.