changeset 100:485d866a847f

updateObjectDB now saves equipment and inventory too.
author KarstenBock@gmx.net
date Wed, 28 Sep 2011 15:01:04 +0200
parents 6e4daff93e7d
children 824f3068ef2a
files gamemodel.py gamescenecontroller.py
diffstat 2 files changed, 57 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/gamemodel.py	Wed Sep 28 12:58:18 2011 +0200
+++ b/gamemodel.py	Wed Sep 28 15:01:04 2011 +0200
@@ -76,6 +76,7 @@
         self.map_files = {}
         self.agents = {}
         self.agents[self.ALL_AGENTS_KEY] = {}
+        self.items = {}
         self.engine = engine
         self.fife_model = engine.getModel()
 
@@ -466,7 +467,9 @@
                     item_data = {}
                     item_data = self.checkAttributes(item_data, item_type)
                     if item_data.has_key("containable"):
-                        item = self.create_item(item_data, world, item_type, data)
+                        item = self.create_item(
+                            self.createUniqueID(data["ID"]), 
+                            item_data, world, item_type)
                     else:
                         raise Exception("Item %s is not containable." % item_type)
                 else:
@@ -474,9 +477,10 @@
                     if self.game_state.hasObject(identifier):
                         item = self.game_state.getObjectById(identifier)
                     else:
-                        agents = self.getAgentsOfActiveMap()
-                        item_data = agents[identifier]
-                        item = self.createAgent(identifier, item_data, world)
+                        item_data = self.items[identifier]["Entity"]
+                        item_type = item_data["containable"]["item_type"]
+                        item = self.create_item(identifier, item_data,
+                                                world, item_type)
                         
                 container.put_item(obj.container, item.containable)
 
@@ -488,7 +492,9 @@
                     item_data = {}
                     item_data = self.checkAttributes(item_data, item_type)
                     if item_data.has_key("containable") and item_data.has_key("equipable"):
-                        item = self.create_item(item_data, world, item_type, data)
+                        item = self.create_item(
+                            self.createUniqueID(data["ID"]), 
+                            item_data, world, item_type)
                     else:
                         raise Exception("Item %s is not containable or equipable." % item_type)
                 else:
@@ -496,14 +502,14 @@
                     if self.game_state.hasObject(identifier):
                         item = self.game_state.getObjectById(identifier)
                     else:
-                        agents = self.getAgentsOfActiveMap()
-                        item_data = agents[identifier]
-                        item = self.createAgent(identifier, item_data, world)
+                        item_data = self.items[identifier]["Entity"]
+                        item_type = item_data["containable"]["item_type"]
+                        item = self.create_item(identifier, item_data,
+                                                world, item_type)
                 equip.equip(obj.equip, item.equipable, slot)
         return obj
 
-    def create_item(self, item_data, world, item_type, data):
-        identifier = self.createUniqueID(data["ID"])
+    def create_item(self, identifier, item_data, world, item_type):
         item = createEntity(item_data, identifier, world, None)
         item.containable.item_type = item_type
         self.game_state.addObject(identifier, None, item)
@@ -690,13 +696,15 @@
             identifier = entity.general.identifier
             agent_data = {}
             map_id = self.game_state.getMapOfObject(identifier)
-            if not map_id:
-                continue
-            if self.agents[self.ALL_AGENTS_KEY].has_key(identifier):
-                agent_data = self.agents[self.ALL_AGENTS_KEY][identifier]
+            if map_id:
+                if self.agents[self.ALL_AGENTS_KEY].has_key(identifier):
+                    agent_data = self.agents[self.ALL_AGENTS_KEY][identifier]
+                else:
+                    agent_data = self.agents[map_id][identifier]
             else:
-                agent_data = self.agents[map_id][identifier]
-            
+                if not self.items.has_key(identifier):
+                    self.items[identifier] = {}
+                agent_data = self.items[identifier]
             entity_data = {}
             entity_data["general"] = {"identifier": identifier}
             for name, component in components.components.iteritems():
@@ -704,19 +712,15 @@
                     comp_data = {}
                     comp_vals = getattr(entity, name)
                     #Items that are in containers will be saved with them.
-                    if name == "equipable" and entity.equipable.wearer or \
-                       name == "containable" and entity.containable.container:
-                        continue
-                    else:
-                        for field in component.saveable_fields:
-                            try:
-                                comp_data[field] = getattr(comp_vals, field)
-                            except AttributeError:                            
-                                #The entity doesn't have this specific value,
-                                #ignore it
-                                pass
-                        if comp_data:
-                            entity_data[name] = comp_data
+                    for field in component.saveable_fields:
+                        try:
+                            comp_data[field] = getattr(comp_vals, field)
+                        except AttributeError:                            
+                            #The entity doesn't have this specific value,
+                            #ignore it
+                            pass
+                    if comp_data:
+                        entity_data[name] = comp_data
                     if name == "fifeagent":
                         if entity.fifeagent.layer:
                             layer = entity.fifeagent.layer
@@ -725,12 +729,30 @@
                             agent_data["Position"] = (loc.x, loc.y, loc.z)
                             agent_data["Map"] = map_id
                             agent_data["Rotation"]  = inst.getRotation()
-                    elif name == "container":
-                        #TODO: Save Inventory
-                        pass
+                    elif name == "container" and hasattr(comp_vals, 
+                                                         "children"):
+                        inventory_data = {}
+                        inventory_data["Slots"] = len(comp_vals.children)
+                        items = []
+                        for child in comp_vals.children:
+                            if not child:
+                                continue
+                            items.append(
+                                {"ID": child.entity.general.identifier}
+                            )
+                        inventory_data["Items"] = items
+                        agent_data["Inventory"] = inventory_data
                     elif name == "equip":
-                        #TODO: Save Equipment
-                        pass                        
+                        equip_data = {}
+                        for field in component.fields:
+                            if(hasattr(comp_vals, field)):
+                                equipable = getattr(comp_vals, field)
+                                if equipable:
+                                    equip_data[field] = {
+                                        "ID": 
+                                         equipable.entity.general.identifier
+                                    }
+                        agent_data["Equipment"] = equip_data
             agent_data["Entity"] = entity_data
             
         
--- a/gamescenecontroller.py	Wed Sep 28 12:58:18 2011 +0200
+++ b/gamescenecontroller.py	Wed Sep 28 15:01:04 2011 +0200
@@ -343,6 +343,7 @@
                 self.model.game_state.deleteObjectsFromMap(
                     self.model.game_state.current_map_name
                 )
+                self.model.game_state.deleteObjectsFromMap(None)
             
             self.model.loadMap(self.model.target_map_name)