From c609780acc17d0755627c94d2e94c9b29802b9b4 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 8 May 2013 18:49:24 +0100
Subject: [PATCH] [Add] Improved xml #define magic stuff.

---
 dat/missions/cargo.lua       | 52 ++++++++++++++--------------
 dat/missions/emp_cargo00.lua | 26 +++++++-------
 dat/missions/emp_scout00.lua | 32 ++++++++---------
 dat/missions/es_cargo.lua    | 66 ++++++++++++++++++------------------
 src/pilot.c                  |  4 +--
 src/player.c                 | 34 +++++++++----------
 src/xml.h                    | 38 +++++++++++++++++++++
 7 files changed, 144 insertions(+), 108 deletions(-)

diff --git a/dat/missions/cargo.lua b/dat/missions/cargo.lua
index e95afa9..5350bef 100644
--- a/dat/missions/cargo.lua
+++ b/dat/missions/cargo.lua
@@ -33,15 +33,15 @@ function create()
    -- Target destination.
   local i = 0
   repeat
-    planet = space.getPlanet( misn.factions() )
+    planet = space.getPlanet(misn.factions())
     i = i + 1
   until planet ~= space.landName() or i > 10
    -- Infinite loop protection.
   if i > 10 then
     misn.finish(false)
   end
-  system = space.getSystem( planet )
-  misn_dist = space.jumpDist( system )
+  system = space.getSystem(planet)
+  misn_dist = space.jumpDist(system)
 
   -- Mission generics.
   i = rnd.int(6)
@@ -66,7 +66,7 @@ function create()
     misn_setTitle(string.format(title[i+21], carg_type, planet))
   end
 
-  -- More mission specifics
+  -- More mission specifics.
   if misn_type == "Cargo" or misn_type == "Rush" then
     carg_mass = rnd.int(10, 30)
     i = rnd.int(12) -- Set the goods.
@@ -84,16 +84,16 @@ function create()
   end
 
   if misn_type == "Cargo" then
-    misn.setDesc( string.format( misn_desc[1], planet, system, carg_mass, carg_type ) )
+    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() + time.units(2) +
         rnd.int(time.units(2), time.units(4)) * misn_dist
-    misn.setDesc( string.format( misn_desc[11], planet, system,
+    misn.setDesc(string.format( misn_desc[11], planet, system,
         carg_mass, carg_type,
-        time.str(misn_time), time.str(misn_time-time.get()) ) )
+        time.str(misn_time), time.str(misn_time-time.get())))
     reward = misn_dist * carg_mass * (450+rnd.int(250)) +
         carg_mass * (250+rnd.int(125)) +
         rnd.int(2500)
@@ -101,49 +101,49 @@ function create()
     misn.setDesc(string.format(misn_desc[21], carg_type, planet, system))
     reward = misn_dist * (1000 + rnd.int(500)) + rnd.int(2000)
   end
-  misn.setReward( string.format( misn_reward, reward ) )
+  misn.setReward(string.format(misn_reward, reward))
 end
 
--- Mission is accepted
+-- Mission is accepted.
 function accept()
   if player.freeCargo() < carg_mass then
-    tk.msg( full_title, string.format( full_msg, carg_mass-player.freeCargo() ))
+    tk.msg(full_title, string.format(full_msg, carg_mass-player.freeCargo()))
 
-  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 ))
+  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" )
+    -- 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 )
+    tk.msg(toomany_title, toomany_msg)
   end
 end
 
--- Land hook
+-- Land hook.
 function land()
   if space.landName() == planet then
-    if player.rmCargo( carg_id ) then
+    if player.rmCargo(carg_id) then
       player.pay( reward )
-      tk.msg( finish_title, string.format( finish_msg, carg_type ))
+      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 ))
+      tk.msg(miss_title, string.format( miss_msg, carg_mass, carg_type))
     end
   end
 end
 
--- Time hook
+-- Time hook.
 function timeup()
   if time.get() > misn_time then
-    player.msg( misn_time_msg )
+    player.msg(misn_time_msg)
     misn.finish(false)
   end
-  misn.setDesc( string.format( misn_desc[2], planet, system,
+  misn.setDesc(string.format( misn_desc[2], planet, system,
       carg_mass, carg_type,
-      time.str(misn_time), time.str(misn_time-time.get()) ) )
+      time.str(misn_time), time.str(misn_time-time.get())))
 end
 
diff --git a/dat/missions/emp_cargo00.lua b/dat/missions/emp_cargo00.lua
index 8f36422..226acd1 100644
--- a/dat/missions/emp_cargo00.lua
+++ b/dat/missions/emp_cargo00.lua
@@ -1,7 +1,7 @@
 lang = lephisto.lang()
 if lang == "es" then
-  -- Not translated atm
-else -- Default english
+  -- Not translated atm.
+else -- Default english.
   misn_title  = "Empire Recruitment"
   misn_reward = "%d credits"
   misn_desc   = "Deliver some parcels for the Empire to %s."
@@ -20,23 +20,23 @@ You aren't too sure of what to make of your encounter with the Empire, only time
 end
 
 function create()
-  -- Intro text
- tk.msg( title[1], text[1] )
- if tk.yesno( title[1], text[2] )
+  -- Intro text.
+ tk.msg(title[1], text[1])
+ if tk.yesno(title[1], text[2])
  then
   misn.accept()
   
   dest = space.getPlanet("Empire");
-  -- Mission details
+  -- Mission details.
     reward = 3000
     misn.setTitle(misn_title)
-    misn.setReward( string.format(misn_reward, reward) )
-    misn.setDesc( string.format(misn_desc,dest))
+    misn.setReward(string.format(misn_reward, reward))
+    misn.setDesc(string.format(misn_desc,dest))
 
-    -- Flavour text and mini-briefing
-    tk.msg( title[2], string.format( text[3], dest ))
+    -- Flavour text and mini-briefing.
+    tk.msg(title[2], string.format( text[3], dest))
 
-    -- Set up the goal
+    -- Set up the goal.
     parcels = player.addCargo("Parcels", 0)
     hook.land("land")
   end
@@ -46,8 +46,8 @@ function land()
   if space.landName() == dest then
     if player.rmCargo(parcels) then
       player.pay(reward)
-      -- More flavour text
-      tk.msg(title[3], string.format( text[4], dest ))
+      -- More flavour text.
+      tk.msg(title[3], string.format(text[4], dest))
       var.push("es_cargo", true)
       misn.finish(true)
     end
diff --git a/dat/missions/emp_scout00.lua b/dat/missions/emp_scout00.lua
index ea893d2..07d55e1 100644
--- a/dat/missions/emp_scout00.lua
+++ b/dat/missions/emp_scout00.lua
@@ -1,7 +1,7 @@
 lang = lephisto.lang()
 if lang == "es" then
-  -- Not translated atm
-else -- Default english
+  -- Not translated atm.
+else -- Default english.
   misn_title    = "Collective Scout"
   misn_reward   = ""
   misn_desc     = {}
@@ -23,8 +23,8 @@ else -- Default english
 end
 
 function create()
-  -- Intro text
-  if tk.yesno( title[1], string.format(text[1], player.name()) )
+  -- Intro text.
+  if tk.yesno(title[1], string.format(text[1], player.name()))
   then
     misn.accept()
 
@@ -34,13 +34,13 @@ function create()
     misn_base     = "Omega Station"
     misn_base_sys = "NCG-7291"
 
-    -- Mission details
+    -- Mission details.
     misn.setTitle(misn_title)
-    misn.setReward( misn_reward )
-    misn.setDesc( string.format(misn_desc[1],misn_nearby))
+    misn.setReward(misn_reward)
+    misn.setDesc(string.format(misn_desc[1],misn_nearby))
 
-    -- Flavour text and mini-briefing
-    tk.msg( title[2], string.format( text[2], misn_nearby, misn_base, misn_base_sys ))
+    -- Flavour text and mini-briefing.
+    tk.msg(title[2], string.format(text[2], misn_nearby, misn_base, misn_base_sys))
 
     hook.enter("enter")
     hook.land("land")
@@ -50,20 +50,20 @@ end
 function enter()
   sys = space.system()
 
-  -- Additional fleets
-  if sys == "NCG-7291" then -- Increase action for realism
+  -- Additional fleets.
+  if sys == "NCG-7291" then -- Increase action for realism.
     pilot.add("Empire Sml Defense")
     pilot.add("Collective Sml Swarm")
   elseif sys == misn_target then
     p = pilot.add("Collective Drone")
     for k,v in pairs(p) do
-      hook.pilotDeath( v, "kill")
+      hook.pilotDeath(v, "kill")
     end
   end
 
-  -- update mission
+  -- update mission.
   if misn_stage == 0 and sys == misn_target then
-    misn.setDesc( string.format(misn_desc[2],misn_base,misn_base_sys) )
+    misn.setDesc(string.format(misn_desc[2],misn_base,misn_base_sys))
     misn_stage = 1
   end
 end
@@ -72,13 +72,13 @@ function land()
   planet = space.landName()
 
   if misn_stage == 1 and  planet == misn_base then
-    tk.msg( title[3], string.format(text[3],misn_target) )
+    tk.msg(title[3], string.format(text[3],misn_target))
     misn.finish(true)
   end
 end
 
 function kill()
-  player.msg( msg_killdrone )
+  player.msg(msg_killdrone)
   misn.finish(false)
 end
 
diff --git a/dat/missions/es_cargo.lua b/dat/missions/es_cargo.lua
index 6fc72a2..088dcab 100644
--- a/dat/missions/es_cargo.lua
+++ b/dat/missions/es_cargo.lua
@@ -1,7 +1,7 @@
 lang = lephisto.lang()
 if lang == "es" then
-  -- Not translated atm
-else -- Default english
+  -- Not translated atm.
+else -- Default english.
   misn_desc     = "The Empire needs to ship %d tons of %s to %s in the %s system by %s (%s left)."
   misn_reward   = "%d credits"
   title = {}
@@ -20,31 +20,31 @@ else -- Default english
   miss_timeup   = "MISSION FAILED: You have failed to deliver the goods to the Empire on time!"
 end
 
---    Empire shipping missions are always timed, but quite lax on the schedules
---    pays a bit more then the rush missions
+--    Empire shipping missions are always timed, but quite lax on the schedules.
+--    Pays a bit more then the rush missions.
 
--- Create the mission
+-- Create the mission.
 function create()
-   -- Target destination
+   -- Target destination.
   local i = 0
   repeat
-    planet = space.getPlanet( misn.factions() )
+    planet = space.getPlanet(misn.factions())
     i = i + 1
   until planet ~= space.landName() or i > 10
-  -- Infinite loop protection
+  -- Infinite loop protection.
   if i > 10 then
     misn.finish(false)
   end
   system = space.getSystem( planet )
   misn_dist = space.jumpDist(system)
 
-  -- Mission generics
+  -- Mission generics.
   misn_type = "Cargo"
   i = rnd.int(1)
-  misn.setTitle( string.format(title[i+1], planet) )
+  misn.setTitle(string.format(title[i+1], planet))
 
-  -- More mission specifics
-  carg_mass = rnd.int( 10, 30 )
+  -- More mission specifics.
+  carg_mass = rnd.int(10, 30)
   i = rnd.int(12)
   if i < 5 then
     carg_type = "Food"
@@ -60,36 +60,36 @@ function create()
 
   misn_time = time.get() + time.units(5) +
       rnd.int(time.units(5), time.units(8)) * misn_dist
-  misn.setDesc( string.format( misn_desc, carg_mass, carg_type, planet, system,
-      time.str(misn_time), time.str(misn_time-time.get())) )
+  misn.setDesc(string.format( misn_desc, carg_mass, carg_type, planet, system,
+      time.str(misn_time), time.str(misn_time-time.get())))
   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 ) )
+  misn.setReward(string.format( misn_reward, reward))
 end
 
--- Mission is accepted
+-- 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, hooks BREAK after accepting
-    carg_id = player.addCargo( carg_type, carg_mass )
-    tk.msg( accept_title, string.format( accept_msg, carg_mass, carg_type ))
-    hook.land( "land" ) -- Only hook after accepting
-    hook.time( "timeup" )
+    tk.msg(full_title, string.format(full_msg, carg_mass-player.freeCargo()))
+  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))
+    hook.land("land") -- Only hook after accepting.
+    hook.time("timeup")
   else
-    tk.msg( toomany_title, toomany_msg )
+    tk.msg(toomany_title, toomany_msg)
   end
 end
 
--- Land hook
+-- 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 ))
+    if player.rmCargo(carg_id) then
+      player.pay(reward)
+      tk.msg(finish_title, string.format(finish_msg, carg_type))
 
-      -- Increase empire shipping mission counter
+      -- Increase empire shipping mission counter.
       n = var.peek("es_misn")
       if n ~= nil then
         var.push("es_misn", n+1)
@@ -99,18 +99,18 @@ function land()
 
       misn.finish(true)
     else
-      tk.msg( miss_title, string.format( miss_msg, carg_mass, carg_type ))
+      tk.msg(miss_title, string.format( miss_msg, carg_mass, carg_type))
     end
   end
 end
 
--- Time hook
+-- Time hook.
 function timeup()
   if time.get() > misn_time then
-    player.msg( miss_timeup )
+    player.msg(miss_timeup)
     misn.finish(false)
   end
-  misn.setDesc( string.format( misn_desc, carg_mass, carg_type, planet, system,
-      time.str(misn_time), time.str(misn_time-time.get())) )
+  misn.setDesc(string.format( misn_desc, carg_mass, carg_type, planet, system,
+      time.str(misn_time), time.str(misn_time-time.get())))
 end
 
diff --git a/src/pilot.c b/src/pilot.c
index d17f9ab..9bca452 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -29,7 +29,7 @@ int pilots = 0;
 static int mpilots = 0;
 
 extern Pilot* player;
-extern unsigned int combat_crating;
+extern unsigned int player_crating;
 
 // Stack of fleets.
 static Fleet* fleet_stack = NULL;
@@ -261,7 +261,7 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter,
     if(!pilot_isFlag(p, PILOT_DEAD)) {
       pilot_dead(p);
       // Adjust the combat rating based on pilot mass.
-      if(shooter == PLAYER_ID) combat_crating += MAX(1, p->ship->mass/50);
+      if(shooter == PLAYER_ID) player_crating += MAX(1, p->ship->mass/50);
     }
   }
 
diff --git a/src/player.c b/src/player.c
index cb64cca..1c5f10f 100644
--- a/src/player.c
+++ b/src/player.c
@@ -44,7 +44,7 @@ static char** player_lstack = NULL; // Names of the planet the ships are at.
 static int player_nstack    = 0;
 // Player global properties.
 char* player_name = NULL;       // Player name.
-int combat_crating = 0;         // Ze rating.
+int player_crating = 0;         // Ze rating.
 unsigned int player_flags = 0;  // Player flags.
 // Input.c
 double player_turn = 0.;        // Turn velocity from input.
@@ -197,8 +197,8 @@ static void player_newMake(void) {
           // MONIEZZ!!!! -- We call these SCred AKA SaraCred. Lame right?
           tmp = cur->children;
           do {
-            if(xml_isNode(tmp, "low")) l = xml_getInt(tmp);
-            else if(xml_isNode(tmp, "high")) h = xml_getInt(tmp);
+            xmlr_int(tmp, "low", l);
+            xmlr_int(tmp, "high", h);
           } while(xml_nextNode(tmp));
         }
         else if(xml_isNode(cur, "system")) {
@@ -207,18 +207,16 @@ static void player_newMake(void) {
             // System name. TODO: Chance based on percentage.
             if(xml_isNode(tmp, "name")) snprintf(system, 20, xml_get(tmp));
             // Position.
-            else if(xml_isNode(tmp, "x")) x = xml_getFloat(tmp);
-            else if(xml_isNode(tmp, "y")) y = xml_getFloat(tmp);
+            xmlr_float(tmp, "x", x);
+            xmlr_float(tmp, "y", y);
           } while(xml_nextNode(tmp));
         }
-        else if(xml_isNode(cur, "combat_crating"))
-          combat_crating = xml_getInt(cur);
-        
-        else if(xml_isNode(cur, "date")) {
+        xmlr_int(cur, "player_crating", player_crating);
+        if(xml_isNode(cur, "date")) {
           tmp = cur->children;
           do {
-            if(xml_isNode(tmp, "low")) tl = xml_getInt(tmp);
-            else if(xml_isNode(tmp, "high")) th = xml_getInt(tmp);
+            xmlr_int(tmp, "low", tl);
+            xmlr_int(tmp, "high", th);
           } while(xml_nextNode(tmp));
         }
       } while((cur = cur->next));
@@ -387,13 +385,13 @@ static char* player_ratings[] = {
 };
 
 const char* player_rating(void) {
-  if(combat_crating == 0)      return player_ratings[0];
-  else if(combat_crating < 50)    return player_ratings[1];
-  else if(combat_crating < 200)   return player_ratings[2];
-  else if(combat_crating < 500)   return player_ratings[3];
-  else if(combat_crating < 1000)   return player_ratings[4];
-  else if(combat_crating < 2500)   return player_ratings[5];
-  else if(combat_crating < 10000)  return player_ratings[6];
+  if(player_crating == 0)      return player_ratings[0];
+  else if(player_crating < 50)    return player_ratings[1];
+  else if(player_crating < 200)   return player_ratings[2];
+  else if(player_crating < 500)   return player_ratings[3];
+  else if(player_crating < 1000)   return player_ratings[4];
+  else if(player_crating < 2500)   return player_ratings[5];
+  else if(player_crating < 10000)  return player_ratings[6];
   else               return player_ratings[7];
 }
 
diff --git a/src/xml.h b/src/xml.h
index bf6219f..610a889 100644
--- a/src/xml.h
+++ b/src/xml.h
@@ -1,6 +1,9 @@
 #pragma once
 
 #include "libxml/parser.h"
+#include "libxml/xmlwriter.h"
+
+#include "log.h"
 
 #define XML_NODE_START  1
 #define XML_NODE_TEXT   3
@@ -20,3 +23,38 @@
 #define xml_getInt(n)   (atoi((char*)(n)->children->content))
 #define xml_getFloat(n) (atof((char*)(n)->children->content))
 
+// Reader crap.
+#define xmlr_int(n,s,i) \
+  if(xml_isNode(n,s)) { i = xml_getInt(n); continue; }
+#define xmlr_float(n,s,f) \
+  if(xml_isNode(n,s)) { f = xml_getFloat(n); continue; }
+
+// Writer crap.
+
+// Encompassing element.
+#define xmlw_startElem(w, str) \
+  if(xmlTextWriterStartElement(w, str) < 0) { \
+    ERR("xmlw: Unable to create start element"); return -1; }
+
+#define xmlw_endElem(w) \
+  if(xmlTextWriterEndElement(w) < 0) { \
+    ERR("xmlw: Unable to create end element"); return -1; }
+
+// Other stuff.
+#define xmlw_elem(w, n, str, args...) \
+  if(xmlTextWriteFormatElement(w, n, str, ## args) < 0) { \
+    ERR("xmlw: Unable to write format element"); return -1; }
+
+#define xmlw_attr(w, str, val) \
+  if(xmlTextWriteAttribute(w, str, val) < 0) { \
+    ERR("xmlw: Unable to write element attribute"); return -1; }
+
+// Document level.
+#define xmlw_start(w) \
+  if(xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL) < 0) { \
+    ERR("xmlw: Unable to start document"); return -1; }
+
+#define xmlw_done(w) \
+  if(xmlTextWriterEndDocument(w) < 0) { \
+    ERR("xmlw: Unable to end document"); return -1; }
+