comparison gamemodel.py @ 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
comparison
equal deleted inserted replaced
99:6e4daff93e7d 100:485d866a847f
74 self.object_db = {} 74 self.object_db = {}
75 self.active_map = None 75 self.active_map = None
76 self.map_files = {} 76 self.map_files = {}
77 self.agents = {} 77 self.agents = {}
78 self.agents[self.ALL_AGENTS_KEY] = {} 78 self.agents[self.ALL_AGENTS_KEY] = {}
79 self.items = {}
79 self.engine = engine 80 self.engine = engine
80 self.fife_model = engine.getModel() 81 self.fife_model = engine.getModel()
81 82
82 # set values from settings 83 # set values from settings
83 maps_directory = settings.parpg.MapsPath 84 maps_directory = settings.parpg.MapsPath
464 if data.has_key("type"): 465 if data.has_key("type"):
465 item_type = data["type"] 466 item_type = data["type"]
466 item_data = {} 467 item_data = {}
467 item_data = self.checkAttributes(item_data, item_type) 468 item_data = self.checkAttributes(item_data, item_type)
468 if item_data.has_key("containable"): 469 if item_data.has_key("containable"):
469 item = self.create_item(item_data, world, item_type, data) 470 item = self.create_item(
471 self.createUniqueID(data["ID"]),
472 item_data, world, item_type)
470 else: 473 else:
471 raise Exception("Item %s is not containable." % item_type) 474 raise Exception("Item %s is not containable." % item_type)
472 else: 475 else:
473 identifier = data["ID"] 476 identifier = data["ID"]
474 if self.game_state.hasObject(identifier): 477 if self.game_state.hasObject(identifier):
475 item = self.game_state.getObjectById(identifier) 478 item = self.game_state.getObjectById(identifier)
476 else: 479 else:
477 agents = self.getAgentsOfActiveMap() 480 item_data = self.items[identifier]["Entity"]
478 item_data = agents[identifier] 481 item_type = item_data["containable"]["item_type"]
479 item = self.createAgent(identifier, item_data, world) 482 item = self.create_item(identifier, item_data,
483 world, item_type)
480 484
481 container.put_item(obj.container, item.containable) 485 container.put_item(obj.container, item.containable)
482 486
483 if agent.has_key("Equipment"): 487 if agent.has_key("Equipment"):
484 for slot, data in agent["Equipment"].iteritems(): 488 for slot, data in agent["Equipment"].iteritems():
486 if data.has_key("type"): 490 if data.has_key("type"):
487 item_type = data["type"] 491 item_type = data["type"]
488 item_data = {} 492 item_data = {}
489 item_data = self.checkAttributes(item_data, item_type) 493 item_data = self.checkAttributes(item_data, item_type)
490 if item_data.has_key("containable") and item_data.has_key("equipable"): 494 if item_data.has_key("containable") and item_data.has_key("equipable"):
491 item = self.create_item(item_data, world, item_type, data) 495 item = self.create_item(
496 self.createUniqueID(data["ID"]),
497 item_data, world, item_type)
492 else: 498 else:
493 raise Exception("Item %s is not containable or equipable." % item_type) 499 raise Exception("Item %s is not containable or equipable." % item_type)
494 else: 500 else:
495 identifier = data["ID"] 501 identifier = data["ID"]
496 if self.game_state.hasObject(identifier): 502 if self.game_state.hasObject(identifier):
497 item = self.game_state.getObjectById(identifier) 503 item = self.game_state.getObjectById(identifier)
498 else: 504 else:
499 agents = self.getAgentsOfActiveMap() 505 item_data = self.items[identifier]["Entity"]
500 item_data = agents[identifier] 506 item_type = item_data["containable"]["item_type"]
501 item = self.createAgent(identifier, item_data, world) 507 item = self.create_item(identifier, item_data,
508 world, item_type)
502 equip.equip(obj.equip, item.equipable, slot) 509 equip.equip(obj.equip, item.equipable, slot)
503 return obj 510 return obj
504 511
505 def create_item(self, item_data, world, item_type, data): 512 def create_item(self, identifier, item_data, world, item_type):
506 identifier = self.createUniqueID(data["ID"])
507 item = createEntity(item_data, identifier, world, None) 513 item = createEntity(item_data, identifier, world, None)
508 item.containable.item_type = item_type 514 item.containable.item_type = item_type
509 self.game_state.addObject(identifier, None, item) 515 self.game_state.addObject(identifier, None, item)
510 return item 516 return item
511 517
688 694
689 for entity in world.entities: 695 for entity in world.entities:
690 identifier = entity.general.identifier 696 identifier = entity.general.identifier
691 agent_data = {} 697 agent_data = {}
692 map_id = self.game_state.getMapOfObject(identifier) 698 map_id = self.game_state.getMapOfObject(identifier)
693 if not map_id: 699 if map_id:
694 continue 700 if self.agents[self.ALL_AGENTS_KEY].has_key(identifier):
695 if self.agents[self.ALL_AGENTS_KEY].has_key(identifier): 701 agent_data = self.agents[self.ALL_AGENTS_KEY][identifier]
696 agent_data = self.agents[self.ALL_AGENTS_KEY][identifier] 702 else:
703 agent_data = self.agents[map_id][identifier]
697 else: 704 else:
698 agent_data = self.agents[map_id][identifier] 705 if not self.items.has_key(identifier):
699 706 self.items[identifier] = {}
707 agent_data = self.items[identifier]
700 entity_data = {} 708 entity_data = {}
701 entity_data["general"] = {"identifier": identifier} 709 entity_data["general"] = {"identifier": identifier}
702 for name, component in components.components.iteritems(): 710 for name, component in components.components.iteritems():
703 if getattr(entity, name): 711 if getattr(entity, name):
704 comp_data = {} 712 comp_data = {}
705 comp_vals = getattr(entity, name) 713 comp_vals = getattr(entity, name)
706 #Items that are in containers will be saved with them. 714 #Items that are in containers will be saved with them.
707 if name == "equipable" and entity.equipable.wearer or \ 715 for field in component.saveable_fields:
708 name == "containable" and entity.containable.container: 716 try:
709 continue 717 comp_data[field] = getattr(comp_vals, field)
710 else: 718 except AttributeError:
711 for field in component.saveable_fields: 719 #The entity doesn't have this specific value,
712 try: 720 #ignore it
713 comp_data[field] = getattr(comp_vals, field) 721 pass
714 except AttributeError: 722 if comp_data:
715 #The entity doesn't have this specific value, 723 entity_data[name] = comp_data
716 #ignore it
717 pass
718 if comp_data:
719 entity_data[name] = comp_data
720 if name == "fifeagent": 724 if name == "fifeagent":
721 if entity.fifeagent.layer: 725 if entity.fifeagent.layer:
722 layer = entity.fifeagent.layer 726 layer = entity.fifeagent.layer
723 inst = layer.getInstance(identifier) 727 inst = layer.getInstance(identifier)
724 loc = inst.getLocation().getExactLayerCoordinates() 728 loc = inst.getLocation().getExactLayerCoordinates()
725 agent_data["Position"] = (loc.x, loc.y, loc.z) 729 agent_data["Position"] = (loc.x, loc.y, loc.z)
726 agent_data["Map"] = map_id 730 agent_data["Map"] = map_id
727 agent_data["Rotation"] = inst.getRotation() 731 agent_data["Rotation"] = inst.getRotation()
728 elif name == "container": 732 elif name == "container" and hasattr(comp_vals,
729 #TODO: Save Inventory 733 "children"):
730 pass 734 inventory_data = {}
735 inventory_data["Slots"] = len(comp_vals.children)
736 items = []
737 for child in comp_vals.children:
738 if not child:
739 continue
740 items.append(
741 {"ID": child.entity.general.identifier}
742 )
743 inventory_data["Items"] = items
744 agent_data["Inventory"] = inventory_data
731 elif name == "equip": 745 elif name == "equip":
732 #TODO: Save Equipment 746 equip_data = {}
733 pass 747 for field in component.fields:
748 if(hasattr(comp_vals, field)):
749 equipable = getattr(comp_vals, field)
750 if equipable:
751 equip_data[field] = {
752 "ID":
753 equipable.entity.general.identifier
754 }
755 agent_data["Equipment"] = equip_data
734 agent_data["Entity"] = entity_data 756 agent_data["Entity"] = entity_data
735 757
736 758
737 def getAgentImportFiles(self): 759 def getAgentImportFiles(self):
738 """Searches the agents directory for import files """ 760 """Searches the agents directory for import files """