From 25adf125e43e2bdb30cf9fe16ebc1ff443263b3f Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Thu, 16 May 2013 19:15:00 +0100
Subject: [PATCH] [Change] Preparations for adding fuel.

---
 dat/ship.xml |  6 +++++
 src/outfit.c | 53 +++++++++++++++++----------------------
 src/outfit.h |  1 +
 src/pilot.c  | 17 ++++++++-----
 src/pilot.h  |  5 ++--
 src/ship.c   | 70 +++++++++++++++++++++-------------------------------
 src/ship.h   |  1 +
 src/xml.h    |  2 ++
 8 files changed, 74 insertions(+), 81 deletions(-)

diff --git a/dat/ship.xml b/dat/ship.xml
index c73ead0..1290f72 100644
--- a/dat/ship.xml
+++ b/dat/ship.xml
@@ -25,6 +25,7 @@
   <characteristics>
    <crew>2</crew>
    <mass>32</mass>
+   <fuel>400</fuel>
    <cap_weapon>25</cap_weapon>
    <cap_cargo>20</cap_cargo>
   </characteristics>
@@ -57,6 +58,7 @@
     <characteristics>
       <crew>2</crew>
       <mass>25</mass>
+      <fuel>300</fuel>
       <cap_weapon>30</cap_weapon>
       <cap_cargo>10</cap_cargo>
     </characteristics>
@@ -89,6 +91,7 @@
   <characteristics>
    <crew>35</crew>
    <mass>500</mass>
+   <fuel>300</fuel>
    <cap_weapon>180</cap_weapon>
    <cap_cargo>70</cap_cargo>
    </characteristics>
@@ -123,6 +126,7 @@
   <characteristics>
    <crew>9</crew>
    <mass>130</mass>
+   <fuel>400</fuel>
    <cap_weapon>90</cap_weapon>
    <cap_cargo>20</cap_cargo>
    </characteristics>
@@ -157,6 +161,7 @@
   <characteristics>
    <crew>13</crew>
    <mass>350</mass>
+   <fuel>500</fuel>
    <cap_weapon>50</cap_weapon>
    <cap_cargo>250</cap_cargo>
   </characteristics>
@@ -189,6 +194,7 @@
   <characteristics>
    <crew>1</crew>
    <mass>15</mass>
+   <fuel>1</fuel>
    <cap_weapon>24</cap_weapon>
    <cap_cargo>1</cap_cargo>
   </characteristics>
diff --git a/src/outfit.c b/src/outfit.c
index b551726..4b0c063 100644
--- a/src/outfit.c
+++ b/src/outfit.c
@@ -263,11 +263,11 @@ static void outfit_parseSAmmo(Outfit* tmp, const xmlNodePtr parent) {
   char str[PATH_MAX] = "\0";
 
   do {
-    if(xml_isNode(node, "thrust")) tmp->u.amm.thrust = xml_getFloat(node);
-    else if(xml_isNode(node, "turn")) tmp->u.amm.turn = xml_getFloat(node);
-    else if(xml_isNode(node, "speed")) tmp->u.amm.speed = xml_getFloat(node);
-    else if(xml_isNode(node, "energy")) tmp->u.amm.energy = xml_getFloat(node);
-    else if(xml_isNode(node, "duration"))
+    xmlr_float(node, "thrust", tmp->u.amm.thrust);
+    xmlr_float(node, "turn", tmp->u.amm.turn);
+    xmlr_float(node, "speed", tmp->u.amm.speed);
+    xmlr_float(node, "energy", tmp->u.amm.energy);
+    if(xml_isNode(node, "duration"))
       tmp->u.amm.duration = (unsigned int)1000.*xml_getFloat(node);
     else if(xml_isNode(node, "lockon"))
       tmp->u.amm.lockon = (unsigned int)1000.*xml_getFloat(node);
@@ -283,19 +283,19 @@ static void outfit_parseSAmmo(Outfit* tmp, const xmlNodePtr parent) {
     else if(xml_isNode(node, "damage")) {
       cur = node->children;
       do {
-        if(xml_isNode(cur, "armour")) tmp->u.amm.damage_armour = xml_getFloat(cur);
-        else if(xml_isNode(cur, "shield")) tmp->u.amm.damage_shield = xml_getFloat(cur);
+        xmlr_float(cur, "armour", tmp->u.amm.damage_armour);
+        xmlr_float(cur, "shield", tmp->u.amm.damage_shield);
       } while((cur = cur->next));
     }
   } while((node = node->next));
 #define MELEMENT(o,s) if(o) WARN("Outfit '%s' missing '"s"' element", tmp->name)
   MELEMENT(tmp->u.amm.gfx_space == NULL,  "gfx");
   MELEMENT((sound_lock != NULL) && (tmp->u.amm.sound == 0), "sound");
-  MELEMENT(tmp->u.amm.thrust==0,       "thrust");
+  MELEMENT(tmp->u.amm.thrust==0,          "thrust");
   MELEMENT(tmp->u.amm.turn==0,            "turn");
   MELEMENT(tmp->u.amm.speed==0,           "speed");
   MELEMENT(tmp->u.amm.duration==0,        "duration");
-  MELEMENT(tmp->u.amm.lockon==0,     "lockon");
+  MELEMENT(tmp->u.amm.lockon==0,          "lockon");
   MELEMENT(tmp->u.amm.damage_armour==0,   "armour' from element 'damage");
   MELEMENT(tmp->u.amm.damage_shield==0,   "shield' from element 'damage");
 #undef MELEMENT
@@ -308,21 +308,15 @@ static void outfit_parseSMod(Outfit* tmp, const xmlNodePtr parent) {
   // Load all the data.
   do {
     // Movement.
-    if(xml_isNode(node, "thrust"))
-      tmp->u.mod.thrust = xml_getFloat(node);
-    else if(xml_isNode(node, "turn"))
-      tmp->u.mod.turn = xml_getFloat(node);
-    else if(xml_isNode(node, "speed"))
-      tmp->u.mod.speed = xml_getFloat(node);
-
+    xmlr_float(node, "thrust", tmp->u.mod.thrust);
+    xmlr_float(node, "turn", tmp->u.mod.turn);
+    xmlr_float(node, "speed", tmp->u.mod.speed);
     // Health.
-    if(xml_isNode(node, "armour"))
-      tmp->u.mod.armour = xml_getFloat(node);
-    else if(xml_isNode(node, "shield"))
-      tmp->u.mod.shield = xml_getFloat(node);
-    else if(xml_isNode(node, "energy"))
-      tmp->u.mod.energy = xml_getFloat(node);
-    else if(xml_isNode(node, "armour_regen"))
+    xmlr_float(node, "armour", tmp->u.mod.armour);
+    xmlr_float(node, "shield", tmp->u.mod.shield);
+    xmlr_float(node, "energy", tmp->u.mod.energy);
+    xmlr_float(node, "fuel",   tmp->u.mod.fuel);
+    if(xml_isNode(node, "armour_regen"))
       tmp->u.mod.armour_regen = xml_getFloat(node)/60.0;
     else if(xml_isNode(node, "shield_regen"))
       tmp->u.mod.shield_regen = xml_getFloat(node)/60.0;
@@ -371,13 +365,12 @@ static Outfit* outfit_parse(const xmlNodePtr parent) {
     if(xml_isNode(node, "general")) {
       cur = node->children;
       do {
-        if(xml_isNode(cur, "max")) tmp->max = xml_getInt(cur);
-        else if(xml_isNode(cur, "tech"))  tmp->tech = xml_getInt(cur);
-        else if(xml_isNode(cur, "mass"))  tmp->mass = xml_getInt(cur);
-        else if(xml_isNode(cur, "price")) tmp->price = xml_getInt(cur);
-        else if(xml_isNode(cur, "description"))
-          tmp->description = strdup(xml_get(cur));
-        else if(xml_isNode(cur, "gfx_store")) {
+        xmlr_int(cur, "max", tmp->max);
+        xmlr_int(cur, "tech", tmp->tech);
+        xmlr_int(cur, "mass", tmp->mass);
+        xmlr_int(cur, "price", tmp->price);
+        xmlr_strd(cur, "description", tmp->description);
+        if(xml_isNode(cur, "gfx_store")) {
           snprintf(str, strlen(xml_get(cur))+sizeof(OUTFIT_GFX)+10,
                    OUTFIT_GFX"store/%s.png", xml_get(cur));
           tmp->gfx_store = gl_newImage(str);
diff --git a/src/outfit.h b/src/outfit.h
index ef21eab..a311f47 100644
--- a/src/outfit.h
+++ b/src/outfit.h
@@ -90,6 +90,7 @@ typedef struct Outfit_ {
       double armour, armour_regen;
       double shield, shield_regen;
       double energy, energy_regen;
+      double fuel;
     } mod;
     struct { // Afterburner.
       double thrust_perc, thrust_abs; // Percent and absolute thrust bonus.
diff --git a/src/pilot.c b/src/pilot.c
index 80ecb58..cda06ad 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -580,7 +580,7 @@ static void pilot_calcStats(Pilot* pilot) {
   int i;
   double q;
   Outfit* o;
-  double ac, sc, ec; // Temp health coeficients to set.
+  double ac, sc, ec, fc; // Temp health coeficients to set.
 
   // -- Set up the basic stuff.
   // Movement.
@@ -591,9 +591,11 @@ static void pilot_calcStats(Pilot* pilot) {
   ac = pilot->armour  / pilot->armour_max;
   sc = pilot->shield  / pilot->shield_max;
   ec = pilot->energy  / pilot->energy_max;
-  pilot->armour_max  = pilot->ship->armour;
-  pilot->shield_max  = pilot->ship->shield;
-  pilot->energy_max  = pilot->ship->energy;
+  fc = pilot->fuel    / pilot->fuel_max;
+  pilot->armour_max   = pilot->ship->armour;
+  pilot->shield_max   = pilot->ship->shield;
+  pilot->energy_max   = pilot->ship->energy;
+  pilot->fuel_max     = pilot->ship->fuel;
   pilot->armour_regen = pilot->ship->armour_regen;
   pilot->shield_regen = pilot->ship->shield_regen;
   pilot->energy_regen = pilot->ship->energy_regen;
@@ -615,7 +617,8 @@ static void pilot_calcStats(Pilot* pilot) {
       pilot->shield_regen += o->u.mod.shield_regen  * q;
       pilot->energy_max  += o->u.mod.energy    * q;
       pilot->energy_regen += o->u.mod.energy_regen  * q;
-
+      // Fuel.
+      pilot->fuel_max += o->u.mod.fuel * q;
     }
     else if(outfit_isAfterburner(pilot->outfits[i].outfit)) {
       // Set the afterburner.
@@ -627,6 +630,7 @@ static void pilot_calcStats(Pilot* pilot) {
   pilot->armour = ac * pilot->armour_max;
   pilot->shield = sc * pilot->shield_max;
   pilot->energy = ec * pilot->energy_max;
+  pilot->fuel   = fc * pilot->fuel_max;
 }
 
 // Pilot free cargo space.
@@ -792,10 +796,11 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, int faction,
   }
 
   // Set the pilot stats based on her ship and outfits.
-  // Hack to have full armour/shield/energy.
+  // Hack to have full armour/shield/energy/fuel.
   pilot->armour = pilot->armour_max = 1.;
   pilot->shield = pilot->shield_max = 1.;
   pilot->energy = pilot->energy_max = 1.;
+  pilot->fuel   = pilot->fuel_max   = 1.;
   pilot_calcStats(pilot);
 
   // Cargo.
diff --git a/src/pilot.h b/src/pilot.h
index aa89d93..6b9a814 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -78,10 +78,9 @@ typedef struct Pilot_ {
   double thrust, turn, speed;
 
   // Current health.
-  double armour, shield, energy;
-  double armour_max, shield_max, energy_max;
+  double armour, shield, energy, fuel;
+  double armour_max, shield_max, energy_max, fuel_max;
   double armour_regen, shield_regen, energy_regen;
-  double fuel; // Used only for jumps. TODO: make it do something.
 
   void (*think)(struct Pilot_*); // AI thinking for the pilot.
   void (*update)(struct Pilot_*, const double); // Update the pilot.
diff --git a/src/ship.c b/src/ship.c
index c90760f..6225807 100644
--- a/src/ship.c
+++ b/src/ship.c
@@ -85,7 +85,7 @@ static Ship* ship_parse(xmlNodePtr parent) {
   char str[PATH_MAX] = "\0";
   char* stmp;
 
-  tmp->name = xml_nodeProp(parent, "name");
+  xmlr_attr(parent, "name", tmp->name);
   if(tmp->name == NULL) WARN("Ship in "SHIP_DATA" has invalid or no name");
 
   node = parent->xmlChildrenNode;
@@ -104,60 +104,45 @@ static Ship* ship_parse(xmlNodePtr parent) {
       tmp->gfx_target = gl_newImage(str);
 
     }
-    else if(xml_isNode(node, "GUI"))
-      tmp->gui = strdup(xml_get(node));
-    else if(xml_isNode(node, "sound"))
+    xmlr_strd(node, "GUI", tmp->gui);
+    if(xml_isNode(node, "sound"))
       tmp->sound = sound_get(xml_get(node));
-    else if(xml_isNode(node, "class"))
-      tmp->class = xml_getInt(node);
-    else if(xml_isNode(node, "price"))
-      tmp->price = xml_getInt(node);
-    else if(xml_isNode(node, "tech"))
-      tmp->tech = xml_getInt(node);
-    else if(xml_isNode(node, "fabricator"))
-      tmp->fabricator = strdup(xml_get(node));
-    else if(xml_isNode(node, "description"))
-      tmp->description = strdup(xml_get(node));
-    else if(xml_isNode(node, "movement")) {
+    xmlr_int(node, "class", tmp->class);
+    xmlr_int(node, "price", tmp->price);
+    xmlr_int(node, "tech", tmp->tech);
+    xmlr_strd(node, "fabricator", tmp->fabricator);
+    xmlr_strd(node, "description", tmp->description);
+    if(xml_isNode(node, "movement")) {
       cur = node->children;
       do {
-        if(xml_isNode(cur, "thrust"))
-          tmp->thrust = xml_getInt(cur);
-        else if(xml_isNode(cur, "turn"))
-          tmp->turn = xml_getInt(cur);
-        else if(xml_isNode(cur, "speed"))
-          tmp->speed = xml_getInt(cur);
-      } while((cur = cur->next));
+        xmlr_int(cur, "thrust", tmp->thrust);
+        xmlr_int(cur, "turn", tmp->turn);
+        xmlr_int(cur, "speed", tmp->speed);
+      } while(xml_nextNode(cur));
     }
     else if(xml_isNode(node, "health")) {
       cur = node->children;
       do {
-        if(xml_isNode(cur, "armour"))
-          tmp->armour = (double)xml_getInt(cur);
-        else if(xml_isNode(cur, "shield"))
-          tmp->shield = (double)xml_getInt(cur);
-        else if(xml_isNode(cur, "energy"))
-          tmp->energy = (double)xml_getInt(cur);
-        else if(xml_isNode(cur, "armour_regen"))
+        xmlr_float(cur, "armour", tmp->armour);
+        xmlr_float(cur, "shield", tmp->shield);
+        xmlr_float(cur, "energy", tmp->energy);
+        if(xml_isNode(cur, "armour_regen"))
           tmp->armour_regen = (double)(xml_getInt(cur))/60.0;
         else if(xml_isNode(cur, "shield_regen"))
           tmp->shield_regen = (double)(xml_getInt(cur))/60.0;
         else if(xml_isNode(cur, "energy_regen"))
           tmp->energy_regen = (double)(xml_getInt(cur))/60.0;
-      } while((cur = cur->next));
+      } while(xml_nextNode(cur));
     }
     else if(xml_isNode(node, "characteristics")) {
       cur = node->children;
       do {
-        if(xml_isNode(cur, "crew"))
-          tmp->crew = xml_getInt(cur);
-        else if(xml_isNode(cur, "mass"))
-          tmp->mass = (double)xml_getInt(cur);
-        else if(xml_isNode(cur, "cap_weapon"))
-          tmp->cap_weapon = xml_getInt(cur);
-        else if(xml_isNode(cur, "cap_cargo"))
-          tmp->cap_cargo = xml_getInt(cur);
-      } while((cur = cur->next));
+        xmlr_int(cur, "crew", tmp->crew);
+        xmlr_float(cur, "mass", tmp->mass);
+        xmlr_int(cur, "fuel", tmp->fuel);
+        xmlr_int(cur, "cap_weapon", tmp->cap_weapon);
+        xmlr_int(cur, "cap_cargo", tmp->cap_cargo);
+      } while(xml_nextNode(cur));
     }
     else if(xml_isNode(node, "outfits")) {
       cur = node->children;
@@ -179,9 +164,9 @@ static Ship* ship_parse(xmlNodePtr parent) {
             ocur->next = otmp;
           }
         }
-      } while((cur = cur->next));
+      } while(xml_nextNode(cur));
     }
-  } while((node = node->next));
+  } while(xml_nextNode(node));
 
   tmp->thrust *= tmp->mass; // Helps keep number sane.
 
@@ -202,7 +187,8 @@ static Ship* ship_parse(xmlNodePtr parent) {
   MELEMENT(tmp->shield==0,          "shield");
   MELEMENT(tmp->shield_regen==0,    "shield_regen");
   MELEMENT(tmp->energy==0,          "energy");
-  MELEMENT(tmp->energy_regen==0,    "energt_regen");
+  MELEMENT(tmp->energy_regen==0,    "energy_regen");
+  MELEMENT(tmp->fuel==0,            "fuel");
   MELEMENT(tmp->crew==0,            "crew");
   MELEMENT(tmp->mass==0,            "mass");
   MELEMENT(tmp->cap_cargo==0,       "cap_cargo");
diff --git a/src/ship.h b/src/ship.h
index 07676ba..c4c3cf5 100644
--- a/src/ship.h
+++ b/src/ship.h
@@ -57,6 +57,7 @@ typedef struct Ship_ {
   // Characteristics.
   int crew;
   int mass;
+  int fuel; // How many jumps by default.
 
   // Health.
   double armour, armour_regen;
diff --git a/src/xml.h b/src/xml.h
index d33a78d..18945cc 100644
--- a/src/xml.h
+++ b/src/xml.h
@@ -34,6 +34,8 @@
   if(xml_isNode(n,s)) { f = xml_getFloat(n);  continue; }
 #define xmlr_str(n,s,str) \
   if(xml_isNode(n,s)) { str = xml_get(n);     continue; }
+#define xmlr_strd(n,s,str) \
+  if(xml_isNode(n,s)) { str = strdup(xml_get(n)); continue; }
 #define xmlr_attr(n,s,a) \
   a = xml_nodeProp(n,s)