From a5e6d0e51f20473dd0e9d406cd3c0abf7045c06b Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 21 Jul 2013 14:22:00 +0100
Subject: [PATCH] [Add] nebulae density and volatility in resource editor.

---
 utils/resedit/data.py     | 105 ++++++++++++++++++++++++++++++++++----
 utils/resedit/space.glade |  61 ++++++++++++++++++++--
 utils/resedit/space.py    |  41 +++++++++++++--
 3 files changed, 188 insertions(+), 19 deletions(-)

diff --git a/utils/resedit/data.py b/utils/resedit/data.py
index 02c3c7f..6e33b85 100644
--- a/utils/resedit/data.py
+++ b/utils/resedit/data.py
@@ -9,7 +9,7 @@ def uniq(alist): # Fastest order preserving.
   for a in s:
     alist.append(a)
 
-def load(xmlfile, tag, has_name=True, do_array=None, do_special=None):
+def load(xmlfile, tag, has_name=True, do_array=None, do_special=None, do_special2=None):
   dom = minidom.parse(xmlfile)
   xmlNodes = dom.getElementsByTagName(tag)
 
@@ -22,14 +22,14 @@ def load(xmlfile, tag, has_name=True, do_array=None, do_special=None):
     
     # Append the element to the dictionary.
     for bignode in filter(lambda x: x.nodeType==x.ELEMENT_NODE, xmlNode.childNodes):
-      mdic[bignode.nodeName], size = load_Tag(bignode, do_array, do_special)
+      mdic[bignode.nodeName], size = load_Tag(bignode, do_array, do_special, do_special2)
 
     dictionary[name] = mdic
 
   dom.unlink()
   return dictionary
 
-def load_Tag(node, do_array=None, do_special=None):
+def load_Tag(node, do_array=None, do_special=None, do_special2=None):
   i = 0
 
   # Figure out if we need an array or dic.
@@ -44,24 +44,68 @@ def load_Tag(node, do_array=None, do_special=None):
   for child in filter(lambda x: x.nodeType == x.ELEMENT_NODE, node.childNodes):
     n = 0
     
-    children, n = load_Tag(child, do_array, do_special)
+    children, n = load_Tag(child, do_array, do_special, do_special2)
 
     # Just slap the children on.
     if n > 0:
       section[child.nodeName] = children
 
     # Big ugly hack to use list instead of array.
+    #
+    # -- PARAMETER FORMAT --
+    # [KEY, ...]
+    #
+    # -- XML INPUT --
+    # <KEY>
+    #   <foo>xxx</foo>
+    #   <foo>yyy</foo>
+    #   ...
+    # </KEY>
+    #
+    # -- PYTHON OUTPUT --
+    # key:["xxx", "xxx", ...]
+    #
     elif use_array and node.nodeName in do_array:
       array.append(child.firstChild.data)
 
+
     # Uglier hack for special things.
+    #
+    # -- PARAMETER FORMAT --
+    # {KEY:VALUE, ...}
+    #
+    # -- XML INPUT --
+    # <KEY>
+    #   <foo VALUE="aaa">xxx</foo>
+    #   <foo VALUE="bbb">yyy</foo>
+    #   ...
+    # </KEY>
+    #
+    # -- PYTHON OUTPUT --
+    # KEY:[{"xxx":"aaa"}, {"yyy":"bbb"}]
+    #
     elif use_array and do_special != None and node.nodeName in do_special.keys():
       section[child.firstChild.data] = \
           child.attributes[do_special[node.nodeName]].value
       array.append(section)
 
-    elif n > 0:
-      section[child.nodeName] = children
+    # Maybe the ugly hacks are a litle overkill...
+    #
+    # -- PARAMETER FORMAT --
+    # { KEY:VALUE, ... }
+    # 
+    # -- XML INPUT --
+    # <KEY VALUE="aaa" ...>"xxx"</KEY>
+    #
+    # -- PYTHON OUTPUT --
+    # KEY:["xxx", "aaa"]
+    #
+    elif do_special2 != None and node.nodeName in do_special2.keys():
+      array2 = []
+      array2.append(child.firstChild.data)
+      for item in do_special2[node.nodeName]:
+        array2.append(child.attributes[do_special[node.nodeName]].value)
+      section[node.nodeName]
 
     # Normal way (but will overwrite lists).
     else:
@@ -75,7 +119,8 @@ def load_Tag(node, do_array=None, do_special=None):
   else:
     return section, i
   
-def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=None):
+def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=None, \
+    do_special2=None):
   """
   do_array is a DICTIONARY, not a list here
   """
@@ -89,7 +134,7 @@ def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=N
     if has_name:
       elem.setAttribute("name", key)
 
-      save_Tag(xml, elem, value, do_array, do_special)
+      save_Tag(xml, elem, value, do_array, do_special, do_special2)
     base.appendChild(elem)
   xml.appendChild(base)
 
@@ -99,11 +144,23 @@ def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=N
   
   xml.unlink()
 
-def save_Tag(xml, parent, data, do_array=None, do_special=None):
+def save_Tag(xml, parent, data, do_array=None, do_special=None, do_special2=None):
   for key, value in data.items():
     node = xml.createElement(key)
 
     # Check if it needs to parse an array instead of a dictionary.
+    #
+    # -- PARAMETER FORMAT --
+    # { KEY:VALUE, ... }
+    #
+    # -- PYTHON INPUT --
+    # KEY:["xxx", "yyy", ....]
+    #
+    # -- XML OUTPUT --
+    # <KEY>
+    #   <VALUE>xxx</VALUE>
+    #   <VALUE>yyy</VALUE>
+    # </KEY
     if do_array != None and key in do_array.keys():
       for text in value:
         node2 = xml.createElement(do_array[key])
@@ -112,6 +169,19 @@ def save_Tag(xml, parent, data, do_array=None, do_special=None):
         node.appendChild(node2)
 
     # Check see if we need to run the ULTRA UBER HACK!
+    #
+    # -- PARAMETER FORMAT --
+    # { KEY:[VALUE1, VALUE2] }
+    #
+    # -- PYTHON INPUT --
+    # KEY:[{"xxx":"aaa"}, {"yyy":"bbb"}]
+    #
+    # -- XML OUTPUT --
+    # <KEY>
+    #   <VALUE1 VALUE2="aaa">xxx</VALUE1>
+    #   <VALUE1 VALUE2="bbb">yyy</VALUE1>
+    # </KEY>
+    #
     elif do_special != None and key in do_special.keys():
       for item in value:
         for key2, value2 in item.items(): # Should only be one member.
@@ -121,9 +191,24 @@ def save_Tag(xml, parent, data, do_array=None, do_special=None):
           node2.appendChild(txtnode)
           node.appendChild(node2)
         
+    # If you thought the last hack was the ULTRA UBER HACK, think again!
+    #
+    # -- PARAMETER FORMAT --
+    # { KEY1:VALUE, ... }
+    # 
+    # -- PYTHON INPUT --
+    # KEY:{"xxx":"aaa"}
+    #
+    # -- XML OUTPUT --
+    # <KEY VALUE="aaa">"xxx"</KEY>
+    elif do_special2 != None and key in do_special2.keys():
+      for key2, value2 in value.items(): # Should only be one member.
+        txtnode = xml.createTextNode(str(key2))
+        node.appendChild(txtnode)
+        node.setAttribute(do_special2[key], value2)
 
     elif isinstance(value, dict):
-      save_Tag(xml, node, value, do_array, do_special)
+      save_Tag(xml, node, value, do_array, do_special, do_special2)
 
     # Standard dictionary approach.
     else:
diff --git a/utils/resedit/space.glade b/utils/resedit/space.glade
index 0b340e4..e1fc82d 100644
--- a/utils/resedit/space.glade
+++ b/utils/resedit/space.glade
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
+<!--Generated with glade3 3.4.5 on Mon Jun  9 18:52:13 2008 -->
 <glade-interface>
   <widget class="GtkWindow" id="winSystems">
     <property name="width_request">750</property>
@@ -198,14 +199,14 @@
                     <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
                   </widget>
                   <packing>
-                    <property name="x">4</property>
-                    <property name="y">162</property>
+                    <property name="x">3</property>
+                    <property name="y">219</property>
                   </packing>
                 </child>
                 <child>
                   <widget class="GtkTreeView" id="treJumps">
                     <property name="width_request">193</property>
-                    <property name="height_request">240</property>
+                    <property name="height_request">182</property>
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
                     <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -213,7 +214,7 @@
                   </widget>
                   <packing>
                     <property name="x">10</property>
-                    <property name="y">180</property>
+                    <property name="y">238</property>
                   </packing>
                 </child>
                 <child>
@@ -499,6 +500,58 @@
                     <property name="y">494</property>
                   </packing>
                 </child>
+                <child>
+                  <widget class="GtkLabel" id="label7">
+                    <property name="width_request">114</property>
+                    <property name="height_request">22</property>
+                    <property name="visible">True</property>
+                    <property name="label" translatable="yes">Nebu Density</property>
+                  </widget>
+                  <packing>
+                    <property name="x">3</property>
+                    <property name="y">172</property>
+                  </packing>
+                </child>
+                <child>
+                  <widget class="GtkSpinButton" id="spiNebuDensity">
+                    <property name="width_request">73</property>
+                    <property name="height_request">20</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+                    <property name="adjustment">0 0 1000 1 10 10</property>
+                  </widget>
+                  <packing>
+                    <property name="x">120</property>
+                    <property name="y">170</property>
+                  </packing>
+                </child>
+                <child>
+                  <widget class="GtkSpinButton" id="spiNebuVolatility">
+                    <property name="width_request">73</property>
+                    <property name="height_request">20</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
+                    <property name="adjustment">0 0 1000 1 10 10</property>
+                  </widget>
+                  <packing>
+                    <property name="x">120</property>
+                    <property name="y">200</property>
+                  </packing>
+                </child>
+                <child>
+                  <widget class="GtkLabel" id="label8">
+                    <property name="width_request">100</property>
+                    <property name="height_request">20</property>
+                    <property name="visible">True</property>
+                    <property name="label" translatable="yes">Nebu Volatility</property>
+                  </widget>
+                  <packing>
+                    <property name="x">13</property>
+                    <property name="y">201</property>
+                  </packing>
+                </child>
               </widget>
               <packing>
                 <property name="resize">True</property>
diff --git a/utils/resedit/space.py b/utils/resedit/space.py
index 1ee61c2..6ceaa39 100644
--- a/utils/resedit/space.py
+++ b/utils/resedit/space.py
@@ -25,12 +25,14 @@ class Space:
 
   def loadSystems(self, xmlfile):
     self.systems = data.load(xmlfile, "ssys", True,
-        ["jumps", "planets"], {"fleets":"chance"} )
+        ["jumps", "planets"], {"fleets":"chance"},
+        {"nebulae":"volatility"})
 
 
   def saveSystems(self, xmlfile):
     data.save( "ssys.xml", self.systems, "Systems", "ssys", True,
-        {"jumps":"jump","planets":"planet"}, {"fleets":["fleet","chance"]})
+        {"jumps":"jump","planets":"planet"}, {"fleets":["fleet","chance"]},
+        {"nebulae":"volatility"})
 
   def loadPlanets(self, xmlfile):
     self.planets = data.load(xmlfile, "planet", True, ["commodities"])
@@ -276,7 +278,7 @@ class Space:
 
   def __supdate(self, wgt=None, index=None, iter=None):
     """
-    Update the window
+    Update the star system window
     """
     
     # Store the current values.
@@ -298,8 +300,20 @@ class Space:
     for key, value in dic.items():
       self.__swidget(key).set_text(str(value))
 
-    # Load jumps.
+    # Load nebulae properties.
+    print "LOAD %s" % self.cur_system
+    print system["general"]
+    try:
+      for key, val in system["general"]["nebulae"].items():
+        self.__swidget("spiNebuDensity").set_text(str(key))
+        self.__swidget("spiNebuVolatility").set_text(str(val))
+    except:
+      system["general"]["nebulae"] = {}
+      nebu = system["general"]["nebuale"]['0'] = '0'
+      self.__swidget("spiNebuDensity").set_text("0")
+      self.__swidget("spiNebuVolatility").set_Text("0")
 
+    # Load jumps.
     jumps = gtk.ListStore(str)
     for jump in system["jumps"]: # Load the planets.
       treenode = jumps.append([jump])
@@ -336,6 +350,10 @@ class Space:
     self.__space_draw()
 
   def __pupdate(self, wgt=None, event=None):
+    '''
+    Update the current planet window.
+    '''
+
     # Store the current values
     if self.cur_planet != self.__curPlanet():
       self.__pstore()
@@ -446,8 +464,11 @@ class Space:
     self.__create_treCommodities()
   
   def __sstore(self):
+    '''
+    Store the system stuff.
+    '''
     sys_name = self.__swidget("inpName").get_text()
-    if sys_name == "":
+    if sys_name == "" or self.cur_system == self.__curSystem():
       return
     
     # Renamed the current system.
@@ -484,8 +505,18 @@ class Space:
     self.__sinpStore(system, "spiStars", "general", "stars")
     self.__sinpStore(system, "spiInterference", "general", "interference")
     self.__sinpStore(system, "spiAsteroids", "general", "asteroids")
+    # Nebulae.
+    print "SAVE %s" % self.cur_system
+    print system["general"]
+    system["general"]["nebulae"] = {}
+    density = self.__swidget("spiNebuDensity").get_text()
+    volatility = self.__swidget("spiNebuVolatility").get_text()
+    system["general"]["nebulae"]["density"] = volatility
 
   def __pstore(self):
+    '''
+    Store the planet stuff.
+    '''
     planet_name = self.__pwidget("inpName").get_text()
     if planet_name == "":
       return