annotate src/parpg/gamemodel.py @ 195:fabe303ab74f

Added RunScriptAction class.
author KarstenBock@gmx.net
date Sat, 19 Nov 2011 16:00:32 +0100
parents 728eabc921f2
children
rev   line source
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
1 # This file is part of PARPG.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
2
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
6 # (at your option) any later version.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
7
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
11 # GNU General Public License for more details.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
12
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
15
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
16 # there should be NO references to FIFE here!
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17 import sys
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
18 import os.path
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
19 import logging
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
20 from copy import deepcopy
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
21
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
22 from fife import fife
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
23 from fife.extensions.serializers.xmlobject import XMLObjectLoader
92
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 88
diff changeset
24 from parpg.bGrease.geometry import Vec2d
186
8cc26e89398c Moved code from CharacterCreationController to GameModel
KarstenBock@gmx.net
parents: 182
diff changeset
25 from serializers import XmlSerializer
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
26
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
27 from parpg import vfs
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
28 from gamestate import GameState
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
29 from gamemap import GameMap
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
30 from common.utils import locateFiles
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
31 from common.utils import parseBool
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
32 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError
64
32faacaf6f28 Added funcionality to load Entities from file
KarstenBock@gmx.net
parents: 12
diff changeset
33 from parpg.entities import createEntity
32faacaf6f28 Added funcionality to load Entities from file
KarstenBock@gmx.net
parents: 12
diff changeset
34 from parpg import behaviours
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
35 from parpg import components
187
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
36 from parpg.components import fifeagent, container, equip, character_statistics
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
37 import characterstatistics as char_stats
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
38
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
39 try:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
40 import xml.etree.cElementTree as ElementTree
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
41 except ImportError:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
42 import xml.etree.ElementTree as ElementTree
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
43
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
44 import yaml
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
45
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
46 logger = logging.getLogger('gamemodel')
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
47
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
48 class GameModel(object):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
49 """GameModel holds the logic for the game.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
50 Since some data (object position and so forth) is held in the
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
51 fife, and would be pointless to replicate, we hold a instance of
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
52 the fife view here. This also prevents us from just having a
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
53 function heavy controller."""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
54 ALL_AGENTS_KEY = "All"
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
55 MAX_ID_NUMBER = 1000
146
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
56 GENERIC_ITEM_GFX = "generic_item"
187
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
57 DEFAULT_STAT_VALUE = 50
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
58
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
59 def __init__(self, engine, settings):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
60 """Initialize the instance.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
61 @param engine: A fife.Engine object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
62 @type emgome: fife.Engine
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
63 @param setting: The applications settigns
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
64 @type setting: parpg.settings.Settings object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
65 @return: None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
66 self.settings = settings
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
67
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
68 self.map_change = False
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
69 self.load_saver = False
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
70 self.savegame = None
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
71 quests_directory = settings.parpg.QuestsPath
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
72 self.game_state = GameState(quests_dir=quests_directory)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
73 #self.game_state.quest_engine =
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
74 #self.game_state.quest_engine.readQuests()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
75 self.pc_run = 1
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
76 self.target_position = None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
77 self.target_map_name = None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
78 self.object_db = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
79 self.active_map = None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
80 self.map_files = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
81 self.agents = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
82 self.agents[self.ALL_AGENTS_KEY] = {}
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
83 self.items = {}
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
84 self.engine = engine
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
85 self.fife_model = engine.getModel()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
86
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
87 # set values from settings
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
88 maps_directory = settings.parpg.MapsPath
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
89 self.game_state.maps_file = '/'.join([maps_directory,
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
90 settings.parpg.MapsFile])
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
91 self.all_agents_file = '/'.join([maps_directory,
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
92 settings.parpg.AllAgentsFile])
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
93 objects_directory = self.settings.parpg.ObjectsPath
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
94 self.objects_directory = objects_directory
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
95 self.object_db_file = '/'.join([objects_directory,
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
96 settings.parpg.ObjectDatabaseFile])
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
97 self.dialogue_directory = settings.parpg.DialoguesPath
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
98 self.dialogues = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
99 self.agent_import_files = {}
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
100 self.obj_loader = XMLObjectLoader(self.engine)
186
8cc26e89398c Moved code from CharacterCreationController to GameModel
KarstenBock@gmx.net
parents: 182
diff changeset
101 # FIXME M. George Hansen 2011-06-06: character stats scripts aren't
8cc26e89398c Moved code from CharacterCreationController to GameModel
KarstenBock@gmx.net
parents: 182
diff changeset
102 # finished, unfortunately.
188
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
103 # NOTE Beliar 2011-11-05 Activated the stats. Testing needed if it
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
104 # works correctly, or if they are still unfinished.
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
105 primary_stats_file = (
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
106 vfs.VFS.open('character_scripts/primary_stats.xml')
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
107 )
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
108 self.primary_stats = XmlSerializer.deserialize(primary_stats_file)
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
109 secondary_stats_file = (
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
110 vfs.VFS.open('character_scripts/secondary_stats.xml')
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
111 )
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
112 self.secondary_stats = XmlSerializer.deserialize(secondary_stats_file)
d0aa0f3eecc1 Activaed loading of character statistic scripts.
KarstenBock@gmx.net
parents: 187
diff changeset
113
186
8cc26e89398c Moved code from CharacterCreationController to GameModel
KarstenBock@gmx.net
parents: 182
diff changeset
114
187
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
115 def create_stats(self, entity):
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
116 for primary_stat in self.primary_stats:
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
117 long_name = primary_stat.long_name
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
118 entity.characterstats.primary_stats[long_name] = (
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
119 char_stats.PrimaryStatisticValue(
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
120 primary_stat, entity.characterstats,
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
121 self.DEFAULT_STAT_VALUE)
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
122 )
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
123 for secondary_stat in self.secondary_stats:
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
124 name = secondary_stat.name
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
125 entity.characterstats.secondary_stats[name] = (
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
126 char_stats.SecondaryStatisticValue(secondary_stat,
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
127 entity.characterstats
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
128 )
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
129 )
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
130
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
131 def checkAttributes(self, attributes, template):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
132 """Checks for attributes that where not given in the map file
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
133 and fills them with values from the object database
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
134 @param attributes: attributes to check
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
135 @type attributes: Dictionary
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
136 @param template: Template from which the values will be used
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
137 @return: The modified attributes"""
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
138 if self.object_db.has_key(template):
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
139 db_attributes = deepcopy(self.object_db[template])
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
140 for key in db_attributes.keys():
113
7b0846025412 Fixed bug in checkAttributes that caused that values where not correctly set from the Template.
KarstenBock@gmx.net
parents: 108
diff changeset
141 if attributes.has_key(key):
7b0846025412 Fixed bug in checkAttributes that caused that values where not correctly set from the Template.
KarstenBock@gmx.net
parents: 108
diff changeset
142 tmp_attributes = db_attributes[key]
7b0846025412 Fixed bug in checkAttributes that caused that values where not correctly set from the Template.
KarstenBock@gmx.net
parents: 108
diff changeset
143 tmp_attributes.update(attributes[key])
7b0846025412 Fixed bug in checkAttributes that caused that values where not correctly set from the Template.
KarstenBock@gmx.net
parents: 108
diff changeset
144 attributes[key] = tmp_attributes
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
145 else:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
146 attributes[key] = db_attributes[key]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
147 return attributes
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
148
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
149 def isIDUsed(self, ID):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
150 if self.game_state.hasObject(ID):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
151 return True
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
152 for namespace in self.agents:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
153 if ID in self.agents[namespace]:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
154 return True
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
155 return False
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
156
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
157 def createUniqueID(self, ID):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
158 if self.isIDUsed(ID):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
159 id_number = 1
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
160 while self.isIDUsed(ID + "_" + str(id_number)):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
161 id_number += 1
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
162 if id_number > self.MAX_ID_NUMBER:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
163 raise ValueError(
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
164 "Number exceeds MAX_ID_NUMBER:" +
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
165 str(self.MAX_ID_NUMBER)
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
166 )
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
167
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
168 ID = ID + "_" + str(id_number)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
169 return ID
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
170
114
b10d12fbbb3f Added moveObject method to the GameModel class.
KarstenBock@gmx.net
parents: 113
diff changeset
171 def moveObject(self, object_id, new_map):
b10d12fbbb3f Added moveObject method to the GameModel class.
KarstenBock@gmx.net
parents: 113
diff changeset
172 """Moves the object to a new map, or in a container
b10d12fbbb3f Added moveObject method to the GameModel class.
KarstenBock@gmx.net
parents: 113
diff changeset
173 @param object_id: ID of the object
b10d12fbbb3f Added moveObject method to the GameModel class.
KarstenBock@gmx.net
parents: 113
diff changeset
174 @type object_id: str
b10d12fbbb3f Added moveObject method to the GameModel class.
KarstenBock@gmx.net
parents: 113
diff changeset
175 @param new_map: ID of the new map, or None
b10d12fbbb3f Added moveObject method to the GameModel class.
KarstenBock@gmx.net
parents: 113
diff changeset
176 @type object_id: str """
131
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
177 game_object = self.deleteObject(object_id)
114
b10d12fbbb3f Added moveObject method to the GameModel class.
KarstenBock@gmx.net
parents: 113
diff changeset
178 self.game_state.addObject(object_id, new_map, game_object)
146
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
179
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
180 def deleteObject(self, object_id):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
181 """Removes an object from the game
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
182 @param object_id: ID of the object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
183 @type object_id: str """
146
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
184 if self.agents["All"].has_key(object_id):
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
185 del self.agents["All"][object_id]
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
186 else:
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
187 del self.items[object_id]
131
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
188 return self.game_state.deleteObject(object_id)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
189
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
190 def save(self, path, filename):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
191 """Writes the saver to a file.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
192 @type filename: string
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
193 @param filename: the name of the file to write to
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
194 @return: None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
195 fname = '/'.join([path, filename])
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
196 try:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
197 save_file = open(fname, 'w')
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
198 except(IOError):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
199 sys.stderr.write("Error: Can't create save game: " + fname + "\n")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
200 return
131
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
201
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
202 save_state = {}
131
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
203 save_state["Agents"] = self.agents
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
204 save_state["Items"] = self.items
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
205 save_state["GameState"] = self.game_state.getStateForSaving()
131
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
206
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
207 yaml.dump(save_state, save_file)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
208
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
209 save_file.close()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
210
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
211 def load(self, path, filename):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
212 """Loads a saver from a file.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
213 @type filename: string
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
214 @param filename: the name of the file (including path) to load from
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
215 @return: None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
216 fname = '/'.join([path, filename])
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
217
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
218 try:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
219 load_file = open(fname, 'r')
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
220 except(IOError):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
221 sys.stderr.write("Error: Can't find save game file\n")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
222 return
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
223 self.deleteMaps()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
224 self.clearAgents()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
225
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
226 save_state = yaml.load(load_file)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
227 self.game_state.restoreFromState(save_state["GameState"])
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
228 maps = save_state["Agents"]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
229 for map_name in maps:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
230 for agent_name in maps[map_name]:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
231 agent = {agent_name:maps[map_name][agent_name]}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
232 self.addAgent(map_name, agent)
131
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
233 self.items = save_state["Items"]
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
234
131
0ffebdca7ba3 Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 130
diff changeset
235 load_file.close()
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
236
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
237 def teleport(self, agent, position):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
238 """Called when a an agent is moved instantly to a new position.
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
239 The setting of position may wan to be created as its own method down
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
240 the road.
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
241 @type position: String Tuple
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
242 @param position: X,Y coordinates passed from engine.changeMap
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
243 @return: fife.Location"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
244 logging.debug(position)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
245 coord = fife.DoublePoint3D(float(position[0]), float(position[1]), 0)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
246 location = fife.Location(self.active_map.agent_layer)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
247 location.setMapCoordinates(coord)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
248 agent.teleport(location)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
249
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
250 def getObjectAtCoords(self, coords):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
251 """Get the object which is at the given coords
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
252 @type coords: fife.Screenpoint
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
253 @param coords: Coordinates where to check for an object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
254 @rtype: fife.Object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
255 @return: An object or None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
256 instances = self.active_map.cameras[
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
257 self.active_map.my_cam_id].\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
258 getMatchingInstances(coords, self.active_map.agent_layer)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
259 # no object returns an empty tuple
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
260 if(instances != ()):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
261 front_y = 0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
262
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
263
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
264 for obj in instances:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
265 # check to see if this in our list at all
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
266 if(self.objectActive(obj.getId())):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
267 # check if the object is on the foreground
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
268 obj_map_coords = \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
269 obj.getLocation().getMapCoordinates()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
270 obj_screen_coords = self.active_map.\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
271 cameras[self.active_map.my_cam_id]\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
272 .toScreenCoordinates(obj_map_coords)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
273
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
274 if obj_screen_coords.y > front_y:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
275 #Object on the foreground
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
276 front_y = obj_screen_coords.y
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
277 return obj
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
278 else:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
279 return None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
280 else:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
281 return None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
282
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
283 def getCoords(self, click):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
284 """Get the map location x, y coordinates from the screen coordinates
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
285 @type click: fife.ScreenPoint
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
286 @param click: Screen coordinates
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
287 @rtype: fife.Location
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
288 @return: The map coordinates"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
289 coord = self.active_map.cameras[self.active_map.my_cam_id].\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
290 toMapCoordinates(click, False)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
291 coord.z = 0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
292 location = fife.Location(self.active_map.agent_layer)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
293 location.setMapCoordinates(coord)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
294 return location
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
295
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
296 def pause(self, paused):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
297 """ Pause/Unpause the game
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
298 @return: nothing"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
299 if self.active_map:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
300 self.active_map.pause(paused)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
301
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
302 def togglePause(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
303 """ Toggle paused state.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
304 @return: nothing"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
305 self.active_map.togglePause()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
306
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
307 def isPaused(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
308 """Returns wheter the game is paused or not"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
309 return self.active_map.isPaused()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
310
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
311 def readMapFiles(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
312 """Read all a available map-files and store them"""
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
313 maps_file = vfs.VFS.open(self.game_state.maps_file)
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
314 self.map_files = yaml.load(maps_file)["Maps"]
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
315
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
316 def addAgent(self, namespace, agent):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
317 """Adds an agent to the agents dictionary
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
318 @param namespace: the namespace where the agent is to be added to
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
319 @type namespace: str
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
320 @param agent: The agent to be added
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
321 @type agent: dict """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
322 from fife.extensions.serializers.xml_loader_tools import loadImportFile
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
323 if not self.agents.has_key(namespace):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
324 self.agents[namespace] = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
325
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
326 agent_values = agent.values()[0]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
327 unique_agent_id = self.createUniqueID(agent.keys()[0])
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
328 del agent[agent.keys()[0]]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
329 agent[unique_agent_id] = agent_values
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
330 self.agents[namespace].update(agent)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
331 object_model = ""
122
21dd4da40eab Added Graphics component.
KarstenBock@gmx.net
parents: 120
diff changeset
332 if agent_values["Entity"].has_key("graphics") \
21dd4da40eab Added Graphics component.
KarstenBock@gmx.net
parents: 120
diff changeset
333 and agent_values["Entity"]["graphics"].has_key("gfx"):
21dd4da40eab Added Graphics component.
KarstenBock@gmx.net
parents: 120
diff changeset
334 object_model = agent_values["Entity"]["graphics"]["gfx"]
146
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
335 elif agent_values.has_key("Template"):
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
336 template = self.object_db[agent_values["Template"]]
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
337 object_model = template["graphics"]["gfx"]
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
338 else:
146
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
339 object_model = self.GENERIC_ITEM_GFX
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
340 import_file = self.agent_import_files[object_model]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
341 loadImportFile(self.obj_loader, import_file, self.engine)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
342
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
343 def readAgentsOfMap(self, map_name):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
344 """Read the agents of the map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
345 @param map_name: Name of the map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
346 @type map_name: str """
137
140e5e93f026 Added ExamineContentsAction.
KarstenBock@gmx.net
parents: 136
diff changeset
347 #Get the agents of the map
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
348 map_agents_file = self.map_files[map_name].\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
349 replace(".xml", "_agents.yaml")
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
350 agents_data = vfs.VFS.open(map_agents_file)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
351 agents = yaml.load_all(agents_data)
137
140e5e93f026 Added ExamineContentsAction.
KarstenBock@gmx.net
parents: 136
diff changeset
352 self.agents[map_name] = {}
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
353 for agent in agents:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
354 if not agent == None:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
355 self.addAgent(map_name, agent)
193
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
356
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
357 def readScriptsOfMap(self, map_name, world):
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
358 """Read the scripts of the map
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
359 @param map_name: Name of the map
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
360 @type map_name: str
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
361 @param world: The current active world
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
362 @type world: parpg.world.World"""
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
363 map_scripts_file = (
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
364 self.map_files[map_name].replace(".xml", "_scripts.yaml")
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
365 )
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
366 if vfs.VFS.exists(map_scripts_file):
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
367 scripts_file = vfs.VFS.open(map_scripts_file)
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
368 scripts_data = yaml.load(scripts_file)
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
369 scripts = (scripts_data["Scripts"])
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
370 conditions = (
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
371 scripts_data["Conditions"] if
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
372 scripts_data.has_key("Conditions") else ()
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
373 )
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
374 scripting = world.systems.scripting
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
375 for name, actions in scripts.iteritems():
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
376 scripting.setScript(name, actions)
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
377 for condition in conditions:
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
378 scripting.addCondition(*condition)
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
379
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
380 def readAllAgents(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
381 """Read the agents of the all_agents_file and store them"""
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
382 agents_file = vfs.VFS.open(self.all_agents_file)
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
383 agents = yaml.load_all(agents_file)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
384 for agent in agents:
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
385 if agent is not None:
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
386 self.addAgent(self.ALL_AGENTS_KEY, agent)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
387
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
388 def getAgentsOfMap(self, map_name):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
389 """Returns the agents that are on the given map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
390 @param map_name: Name of the map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
391 @type map_name: str
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
392 @return: A dictionary with the agents of the map"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
393 if not self.agents.has_key(map_name):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
394 return {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
395 ret_dict = self.agents[map_name].copy()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
396 for agent_name, agent_value in self.agents[self.ALL_AGENTS_KEY]\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
397 .iteritems():
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
398 if agent_value["Map"] == map_name:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
399 ret_dict[agent_name] = agent_value
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
400 return ret_dict
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
401
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
402 def getAgentsOfActiveMap(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
403 """Returns the agents that are on active map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
404 @return: A dictionary with the agents of the map """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
405 return self.getAgentsOfMap(self.active_map.map.getId())
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
406
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
407 def clearAgents(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
408 """Resets the agents dictionary"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
409 self.agents = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
410 self.agents[self.ALL_AGENTS_KEY] = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
411
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
412 def loadMap(self, map_name):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
413 """Load a new map.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
414 @type map_name: string
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
415 @param map_name: Name of the map to load
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
416 @return: None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
417 if not map_name in self.game_state.maps:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
418 map_file = self.map_files[map_name]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
419 new_map = GameMap(self.engine, self)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
420 self.game_state.maps[map_name] = new_map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
421 new_map.load(map_file)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
422
64
32faacaf6f28 Added funcionality to load Entities from file
KarstenBock@gmx.net
parents: 12
diff changeset
423 def createAgent(self, agent, inst_id, world):
129
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
424 if self.game_state.hasObject(inst_id):
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
425 return None
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
426 entity_data = deepcopy(agent["Entity"])
122
21dd4da40eab Added Graphics component.
KarstenBock@gmx.net
parents: 120
diff changeset
427 entity_data["fifeagent"] = {}
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
428 template = None
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
429 if agent.has_key("Template"):
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
430 template = agent["Template"]
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
431 entity_data = self.checkAttributes(entity_data, template)
146
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
432 object_id = (entity_data["graphics"]["gfx"]
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
433 if entity_data.has_key("graphics") and
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
434 entity_data["graphics"].has_key("gfx")
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
435 else self.GENERIC_ITEM_GFX
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
436 )
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
437 map_obj = self.fife_model.getObject(str(object_id), "PARPG")
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
438 if not map_obj:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
439 logging.warning("Object with inst_id={0}, ns=PARPG, "
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
440 "could not be found. "
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
441 "Omitting...".format(str(object_id)))
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
442
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
443 x_pos = agent["Position"][0]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
444 y_pos = agent["Position"][1]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
445 z_pos = agent["Position"][2] if len(agent["Position"]) == 3 \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
446 else 0.0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
447 stack_pos = agent["Stackposition"] if \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
448 agent.has_key("StackPosition") \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
449 else None
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
450 inst = self.active_map.agent_layer.\
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
451 createInstance(map_obj,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
452 fife.ExactModelCoordinate(x_pos,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
453 y_pos,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
454 z_pos),
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
455 inst_id)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
456 inst.setId(inst_id)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
457
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
458 rotation = agent["Rotation"]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
459 inst.setRotation(rotation)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
460
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
461 fife.InstanceVisual.create(inst)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
462 if (stack_pos):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
463 inst.get2dGfxVisual().setStackPosition(int(stack_pos))
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
464
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
465 if (map_obj.getAction('default')):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
466 target = fife.Location(self.active_map.agent_layer)
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
467 inst.act('default', target, True)
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
468
120
1703dbb4fefb Added Behaviour component.
KarstenBock@gmx.net
parents: 119
diff changeset
469 if entity_data.has_key("behaviour"):
1703dbb4fefb Added Behaviour component.
KarstenBock@gmx.net
parents: 119
diff changeset
470 entity_data["fifeagent"]["behaviour"] = \
1703dbb4fefb Added Behaviour component.
KarstenBock@gmx.net
parents: 119
diff changeset
471 getattr(behaviours,
1703dbb4fefb Added Behaviour component.
KarstenBock@gmx.net
parents: 119
diff changeset
472 entity_data["behaviour"]["behaviour_type"])()
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
473 else:
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
474 entity_data["fifeagent"]["behaviour"] = behaviours.Base()
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
475 if self.dialogues.has_key(inst_id):
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
476 entity_data["dialogue"] = {}
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
477 entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
478 if (entity_data.has_key("containable") and not
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
479 entity_data["containable"].has_key("item_type")
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
480 ):
187
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
481 entity_data["containable"]["item_type"] = template
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
482
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
483 obj = self.createMapObject(self.active_map.agent_layer,
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
484 entity_data, inst_id, world)
105
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
485
187
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
486 if agent.has_key("Statistics"):
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
487 self.create_stats(obj)
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
488 for name, val in agent["Statistics"].iteritems():
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
489 obj.characterstats.primary_stats[name].value = val
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
490
105
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
491 if agent.has_key("Inventory"):
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
492 inv = agent["Inventory"]
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
493 self.createInventoryItems(inv, obj, world)
105
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
494
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
495 if agent.has_key("Equipment"):
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
496 for slot, data in agent["Equipment"].iteritems():
129
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
497 item = None
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
498 if data.has_key("type"):
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
499 item_type = data["type"]
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
500 item_data = {}
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
501 item_data = self.checkAttributes(item_data, item_type)
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
502 if (item_data.has_key("containable") and
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
503 item_data.has_key("equipable")):
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
504 item = self.createItem(
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
505 self.createUniqueID(data["ID"]),
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
506 item_data, world, item_type)
129
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
507 else:
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
508 raise Exception(
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
509 "Item %s is not containable or equipable." %
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
510 item_type
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
511 )
105
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
512 else:
129
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
513 identifier = data["ID"]
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
514 if self.game_state.hasObject(identifier):
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
515 item = self.game_state.getObjectById(identifier)
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
516 else:
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
517 item_data = self.items[identifier]["Entity"]
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
518 item_type = item_data["containable"]["item_type"]
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
519 item = self.createItem(identifier, item_data,
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
520 world, item_type)
129
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
521 equip.equip(obj.equip, item.equipable, slot)
135
e28c13a4802a Fixed graphical representation of lockables, that are open, when creating the agent.
KarstenBock@gmx.net
parents: 131
diff changeset
522 if (obj.fifeagent and (obj.lockable and not obj.lockable.closed)):
e28c13a4802a Fixed graphical representation of lockables, that are open, when creating the agent.
KarstenBock@gmx.net
parents: 131
diff changeset
523 obj.fifeagent.behaviour.animate("opened", repeating=True)
129
7583965ddcd6 If Items in containers or being equipped have no type the game will now try to get the object using the ID.
KarstenBock@gmx.net
parents: 128
diff changeset
524 return obj
108
2f928c913c78 Fixed item_type not being set when creating items.
KarstenBock@gmx.net
parents: 106
diff changeset
525
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
526 def createInventoryItems(self, inv, obj, world):
161
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
527 slots = inv["Slots"]
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
528 obj.container.children = list()
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
529 for x in xrange(slots):
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
530 obj.container.children.append(None)
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
531 items = inv["Items"] if inv.has_key("Items") else list()
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
532 for data in items:
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
533 item = None
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
534 slot = data["Slot"] if data.has_key("Slot") else -1
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
535 if data.has_key("type"):
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
536 item_type = data["type"]
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
537 item = self.createItemByType(item_type, data["ID"], world)
161
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
538 else:
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
539 identifier = data["ID"]
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
540 item = self.createItemByID(world, identifier)
161
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
541
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
542 container.put_item(obj.container, item.containable, slot)
d1f9652d0651 Fixed contents of containers, that are contained in other containers, not correctly restoring.
KarstenBock@gmx.net
parents: 157
diff changeset
543
162
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
544 def createItemByID(self, world, identifier):
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
545 if self.game_state.hasObject(identifier):
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
546 item = self.game_state.getObjectById(identifier)
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
547 else:
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
548 agent_data = self.items[identifier]
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
549 item_data = agent_data["Entity"]
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
550 item_type = item_data["containable"]["item_type"]
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
551 item = self.createItem(identifier, item_data,
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
552 world, item_type)
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
553 if item.container and agent_data.has_key("Inventory"):
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
554 self.createInventoryItems(agent_data["Inventory"],
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
555 item, world)
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
556 return item
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
557
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
558 def createItemByType(self, item_type, identifier, world):
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
559 item_data = {}
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
560 item_data = self.checkAttributes(item_data, item_type)
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
561 if item_data.has_key("containable"):
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
562 return self.createItem( self.createUniqueID(identifier),
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
563 item_data, world, item_type)
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
564 else:
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
565 raise Exception("Item %s is not containable." % item_type)
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
566
79c186b69603 Split code off from createInventoryItems to createItemByID and createItemByType.
KarstenBock@gmx.net
parents: 161
diff changeset
567 def createItem(self, identifier, item_data, world, item_type):
146
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
568 if not item_data["description"].has_key("view_name"):
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
569 item_data["description"]["view_name"] = (
6e1eb964a6e5 Fixed dropping items on the map.
KarstenBock@gmx.net
parents: 138
diff changeset
570 item_data["description"]["real_name"])
116
9b5498e3bda0 Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 114
diff changeset
571 item = createEntity(item_data, identifier, world, None)
108
2f928c913c78 Fixed item_type not being set when creating items.
KarstenBock@gmx.net
parents: 106
diff changeset
572 item.containable.item_type = item_type
116
9b5498e3bda0 Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 114
diff changeset
573 self.game_state.addObject(identifier, None, item)
167
097782ae9c77 createItem now calls UpdateObjectDB after it created the item.
KarstenBock@gmx.net
parents: 162
diff changeset
574 self.updateObjectDB(world)
108
2f928c913c78 Fixed item_type not being set when creating items.
KarstenBock@gmx.net
parents: 106
diff changeset
575 return item
105
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
576
64
32faacaf6f28 Added funcionality to load Entities from file
KarstenBock@gmx.net
parents: 12
diff changeset
577 def placeAgents(self, world):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
578 """Places the current maps agents """
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
579 if not self.active_map:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
580 return
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
581 agents = self.getAgentsOfMap(self.game_state.current_map_name)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
582 for agent in agents:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
583 if agent == "PlayerCharacter":
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
584 continue
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
585 if self.active_map.agent_layer.getInstances(agent):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
586 continue
64
32faacaf6f28 Added funcionality to load Entities from file
KarstenBock@gmx.net
parents: 12
diff changeset
587 self.createAgent(agents[agent], agent, world)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
588
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
589 def placePC(self, world):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
590 """Places the PlayerCharacter on the map"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
591 agent = self.agents[self.ALL_AGENTS_KEY]["PlayerCharacter"]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
592 inst_id = "PlayerCharacter"
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
593 self.createAgent(agent, inst_id, world)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
594
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
595 # create the PlayerCharacter agent
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
596 self.active_map.addPC()
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
597 #self.game_state.getObjectById("PlayerCharacter").fifeagent.start()
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
598 if agent.has_key("PeopleKnown"):
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
599 player = self.game_state.getObjectById("PlayerCharacter")
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
600 player.fifeagent.people_i_know = agent["PeopleKnown"]
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
601
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
602 def changeMap(self, map_name, target_position = None):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
603 """Registers for a map change on the next pump().
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
604 @type map_name: String
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
605 @param map_name: Id of the map to teleport to
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
606 @type map_file: String
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
607 @param map_file: Filename of the map to teleport to
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
608 @type target_position: Tuple
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
609 @param target_position: Position of PlayerCharacter on target map.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
610 @return None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
611 # set the parameters for the map change if moving to a new map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
612 if map_name != self.game_state.current_map_name:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
613 self.target_map_name = map_name
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
614 self.target_position = target_position
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
615 # issue the map change
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
616 self.map_change = True
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
617
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
618 def deleteMaps(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
619 """Clear all currently loaded maps from FIFE as well as clear our
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
620 local map cache
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
621 @return: nothing"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
622 self.engine.getModel().deleteMaps()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
623 self.engine.getModel().deleteObjects()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
624 self.game_state.clearObjects()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
625 self.game_state.maps = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
626
193
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
627 def setActiveMap(self, map_name, world):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
628 """Sets the active map that is to be rendered.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
629 @type map_name: String
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
630 @param map_name: The name of the map to load
193
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
631 @param world: The active world
728eabc921f2 Implemented loading scripts from files.
KarstenBock@gmx.net
parents: 188
diff changeset
632 @type world: parpg.world.World
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
633 @return: None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
634 # Turn off the camera on the old map before we turn on the camera
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
635 # on the new map.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
636 self.active_map.cameras[self.active_map.my_cam_id].setEnabled(False)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
637 # Make the new map active.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
638 self.active_map = self.game_state.maps[map_name]
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
639 self.active_map.makeActive()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
640 self.game_state.current_map_name = map_name
138
c9fba51214ed Fixed bug where the objects of a map where not correctly restored when changing between maps.
KarstenBock@gmx.net
parents: 137
diff changeset
641 if not self.agents.has_key(map_name):
c9fba51214ed Fixed bug where the objects of a map where not correctly restored when changing between maps.
KarstenBock@gmx.net
parents: 137
diff changeset
642 self.readAgentsOfMap(map_name)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
643
116
9b5498e3bda0 Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 114
diff changeset
644 def createMapObject (self, layer, attributes, inst_id, world):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
645 """Create an object and add it to the current map.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
646 @type layer: fife.Layer
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
647 @param layer: FIFE layer object exists in
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
648 @type attributes: Dictionary
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
649 @param attributes: Dictionary of all object attributes
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
650 @type instance: fife.Instance
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
651 @param instance: FIFE instance corresponding to the object
105
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
652 @return: The created object"""
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
653 # create the extra data
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
654 extra = {}
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
655 if layer is not None:
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
656 extra['fifeagent'] = {}
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
657 extra['fifeagent']['layer'] = layer
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
658
116
9b5498e3bda0 Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 114
diff changeset
659 obj = createEntity(attributes, inst_id, world, extra)
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
660 if obj:
105
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
661 self.addObject(layer, obj)
7829eb185d6f (Re)added setting of inventory and equipment in the object files.
KarstenBock@gmx.net
parents: 92
diff changeset
662 return obj
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
663
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
664 def addPC(self, layer, player_char):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
665 """Add the PlayerCharacter to the map
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
666 @type layer: fife.Layer
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
667 @param layer: FIFE layer object exists in
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
668 @type player_char: PlayerCharacter
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
669 @param player_char: PlayerCharacter object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
670 @type instance: fife.Instance
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
671 @param instance: FIFE instance of PlayerCharacter
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
672 @return: None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
673 # For now we copy the PlayerCharacter,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
674 # in the future we will need to copy
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
675 # PlayerCharacter specifics between the different PlayerCharacter's
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
676 player = self.game_state.getObjectById("PlayerCharacter")
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
677 player.fifeagent = player_char
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
678 player.fifeagent.setup()
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
679 player.fifeagent.behaviour.speed = self.settings.parpg.PCSpeed
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
680
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
681
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
682 def addObject(self, layer, obj):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
683 """Adds an object to the map.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
684 @type layer: fife.Layer
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
685 @param layer: FIFE layer object exists in
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
686 @type obj: GameObject
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
687 @param obj: corresponding object class
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
688 @type instance: fife.Instance
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
689 @param instance: FIFE instance of object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
690 @return: None"""
116
9b5498e3bda0 Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 114
diff changeset
691 ref = self.game_state.getObjectById(obj.general.identifier,
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
692 self.game_state.current_map_name)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
693 if ref is None:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
694 # no, add it to the game state
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
695 self.game_state.addObject(obj.general.identifier,
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
696 self.game_state.current_map_name, obj)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
697 else:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
698 # yes, use the current game state data
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
699 obj.fifeagent.pos.X = ref.X
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
700 obj.fifeagent.pos.Y = ref.Y
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
701 obj.fifeagent.gfx = ref.gfx
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
702
88
d89e88a90c9e Implemented creation of dynamic entities.
KarstenBock@gmx.net
parents: 84
diff changeset
703 if obj.fifeagent.behaviour:
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
704 obj.fifeagent.behaviour.parent = obj
73
58661f5b2f6b Removed layer as attribute of BaseBehaviour and added it as a parameter to the attachToLayer method.
KarstenBock@gmx.net
parents: 69
diff changeset
705 fifeagent.setup_behaviour(obj.fifeagent)
58661f5b2f6b Removed layer as attribute of BaseBehaviour and added it as a parameter to the attachToLayer method.
KarstenBock@gmx.net
parents: 69
diff changeset
706 obj.fifeagent.behaviour.speed = self.settings.parpg.PCSpeed
58661f5b2f6b Removed layer as attribute of BaseBehaviour and added it as a parameter to the attachToLayer method.
KarstenBock@gmx.net
parents: 69
diff changeset
707 #Start the behaviour
58661f5b2f6b Removed layer as attribute of BaseBehaviour and added it as a parameter to the attachToLayer method.
KarstenBock@gmx.net
parents: 69
diff changeset
708 obj.fifeagent.behaviour.idle()
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
709 # create the agent
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
710 #obj.setup()
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
711 #obj.behaviour.speed = self.settings.parpg.PCSpeed
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
712 # create the PlayerCharacter agent
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
713 #obj.start()
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
714 #if obj.trueAttr("AnimatedContainer"):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
715 # create the agent
69
ad75fa042b99 Changes to make PARPG "runable" with grease. (With the correct modified assets)
KarstenBock@gmx.net
parents: 66
diff changeset
716 #obj.setup()
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
717
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
718 def objectActive(self, ident):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
719 """Given the objects ID, pass back the object if it is active,
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
720 False if it doesn't exist or not displayed
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
721 @type ident: string
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
722 @param ident: ID of object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
723 @rtype: boolean
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
724 @return: Status of result (True/False)"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
725 for game_object in \
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
726 self.game_state.getObjectsFromMap(self.game_state.current_map_name):
116
9b5498e3bda0 Move the identifier field from the FifeAgent component to the new General component.
KarstenBock@gmx.net
parents: 114
diff changeset
727 if (game_object.general.identifier == ident):
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
728 # we found a match
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
729 return game_object
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
730 # no match
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
731 return False
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
732
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
733 def movePlayer(self, position):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
734 """Code called when the player should move to another location
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
735 @type position: fife.ScreenPoint
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
736 @param position: Screen position to move to
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
737 @return: None"""
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
738 player = self.game_state.getObjectById("PlayerCharacter")
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
739 if(self.pc_run == 1):
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
740 player.fifeagent.behaviour.run(position)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
741 else:
182
59c9ce2b8351 PARPG now works with, and needs Fife 0.3.3.
KarstenBock@gmx.net
parents: 167
diff changeset
742 player.fifeagent.behaviour.walk(position)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
743
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
744 def teleportAgent(self, agent, position):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
745 """Code called when an agent should teleport to another location
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
746 @type position: fife.ScreenPoint
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
747 @param position: Screen position to teleport to
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
748 @return: None"""
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
749 agent.teleport(position)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
750 self.agents[agent.ID]["Position"] = position
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
751
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
752 def readObjectDB(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
753 """Reads the Object Information Database from a file. """
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
754 database_file = vfs.VFS.open(self.object_db_file)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
755 database = yaml.load_all(database_file)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
756 for object_info in database:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
757 self.object_db.update(object_info)
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
758
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
759 def updateObjectDB(self, world):
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
760 """Updates the values in the object database with the worlds values"""
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
761
157
db6403c1a7a1 Fixed that dropped items cannot be picked up.
KarstenBock@gmx.net
parents: 155
diff changeset
762 all_agents = self.agents[self.ALL_AGENTS_KEY]
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
763 for entity in world.entities:
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
764 identifier = entity.general.identifier
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
765 agent_data = {}
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
766 map_id = self.game_state.getMapOfObject(identifier)
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
767 if map_id:
157
db6403c1a7a1 Fixed that dropped items cannot be picked up.
KarstenBock@gmx.net
parents: 155
diff changeset
768 if all_agents.has_key(identifier):
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
769 agent_data = self.agents[self.ALL_AGENTS_KEY][identifier]
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
770 else:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
771 agent_data = self.agents[map_id][identifier]
157
db6403c1a7a1 Fixed that dropped items cannot be picked up.
KarstenBock@gmx.net
parents: 155
diff changeset
772
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
773 else:
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
774 if not self.items.has_key(identifier):
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
775 self.items[identifier] = {}
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
776 agent_data = self.items[identifier]
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
777 entity_data = {}
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
778 entity_data["general"] = {"identifier": identifier}
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
779 for name, component in components.components.iteritems():
120
1703dbb4fefb Added Behaviour component.
KarstenBock@gmx.net
parents: 119
diff changeset
780 if getattr(entity, name):
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
781 comp_data = {}
128
2a661e259b8b updateObjectDB of GameModel now uses saveable_fields.
KarstenBock@gmx.net
parents: 122
diff changeset
782 comp_vals = getattr(entity, name)
2a661e259b8b updateObjectDB of GameModel now uses saveable_fields.
KarstenBock@gmx.net
parents: 122
diff changeset
783 #Items that are in containers will be saved with them.
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
784 for field in component.saveable_fields:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
785 try:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
786 comp_data[field] = getattr(comp_vals, field)
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
787 except AttributeError:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
788 #The entity doesn't have this specific value,
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
789 #ignore it
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
790 pass
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
791 if comp_data:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
792 entity_data[name] = comp_data
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
793 if name == "fifeagent":
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
794 if entity.fifeagent.layer:
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
795 layer = entity.fifeagent.layer
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
796 inst = layer.getInstance(identifier)
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
797 loc = inst.getLocation().getExactLayerCoordinates()
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
798 agent_data["Position"] = (loc.x, loc.y, loc.z)
157
db6403c1a7a1 Fixed that dropped items cannot be picked up.
KarstenBock@gmx.net
parents: 155
diff changeset
799 if all_agents.has_key(identifier):
db6403c1a7a1 Fixed that dropped items cannot be picked up.
KarstenBock@gmx.net
parents: 155
diff changeset
800 agent_data["Map"] = map_id
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
801 agent_data["Rotation"] = inst.getRotation()
187
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
802 elif name == "characterstats":
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
803 agent_data["Statistics"] = (
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
804 character_statistics.get_stat_values(
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
805 entity.characterstats
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
806 )["primary"]
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
807 )
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
808 elif name == "container" and hasattr(comp_vals,
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
809 "children"):
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
810 inventory_data = {}
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
811 inventory_data["Slots"] = len(comp_vals.children)
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
812 items = []
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
813 for child in comp_vals.children:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
814 if not child:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
815 continue
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
816 items.append(
136
e1fd4cda237d Added optional Slot value to the "Items" list in the agents file.
KarstenBock@gmx.net
parents: 135
diff changeset
817 {"ID": child.entity.general.identifier,
e1fd4cda237d Added optional Slot value to the "Items" list in the agents file.
KarstenBock@gmx.net
parents: 135
diff changeset
818 "Slot": child.slot}
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
819 )
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
820 inventory_data["Items"] = items
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
821 agent_data["Inventory"] = inventory_data
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
822 elif name == "equip":
130
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
823 equip_data = {}
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
824 for field in component.fields:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
825 if(hasattr(comp_vals, field)):
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
826 equipable = getattr(comp_vals, field)
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
827 if equipable:
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
828 equip_data[field] = {
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
829 "ID":
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
830 equipable.entity.general.identifier
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
831 }
9fcff924eb6f updateObjectDB now saves equipment and inventory too.
KarstenBock@gmx.net
parents: 129
diff changeset
832 agent_data["Equipment"] = equip_data
187
f612b4638744 Statistics are being saved and loaded.
KarstenBock@gmx.net
parents: 186
diff changeset
833 agent_data["Entity"] = entity_data
118
29869273f9e1 Fixed moving between maps.
KarstenBock@gmx.net
parents: 116
diff changeset
834
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
835 def getAgentImportFiles(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
836 """Searches the agents directory for import files """
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
837 filepaths = locateFiles("*.xml", self.objects_directory)
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
838 for filepath in filepaths:
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
839 try:
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
840 xml_file = vfs.VFS.open(filepath)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
841 root = ElementTree.parse(xml_file).getroot()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
842 if root.tag == "object":
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
843 self.agent_import_files[root.attrib["id"]] = filepath
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
844 except SyntaxError as error:
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
845 logging.error("Error parsing file {0}: {1}".format(filepath,
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
846 error))
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
847
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
848 def getDialogues(self):
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
849 """Searches the dialogue directory for dialogues """
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
850 files = locateFiles("*.yaml", self.dialogue_directory)
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
851 dialogue_parser = YamlDialogueParser()
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
852 for dialogue_filepath in files:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
853 # Note Technomage 2010-11-13: the new DialogueEngine uses its own
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
854 # parser now, YamlDialogueParser.
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
855 # dialogues = yaml.load_all(file(dialogue_file, "r"))
12
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
856 dialogue_file = vfs.VFS.open(dialogue_filepath)
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
857 try:
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
858 dialogue = dialogue_parser.load(dialogue_file)
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
859 except DialogueFormatError as error:
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
860 logging.error('unable to load dialogue file {0}: {1}'
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
861 .format(dialogue_filepath, error))
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
862 else:
d60f1dab8469 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
863 self.dialogues[dialogue.npc_name] = dialogue
0
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
864 # Note Technomage 2010-11-13: the below code is used to load
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
865 # multiple dialogues from a single file. Is this functionality
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
866 # used/necessary?
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
867 # for dialogue in dialogues:
1fd2201f5c36 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
868 # self.dialogues[dialogue["NPC"]] = dialogue