annotate gamestate.py @ 189:4381be70e1bb tip

Fixed incompatibility with latest changes to bgrease.
author Beliar <KarstenBock@gmx.net>
date Sun, 08 Apr 2012 20:30:43 +0200
parents ab6a0fd1668a
children
rev   line source
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
1 # This file is part of PARPG.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
2
7a89ea5404b1 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
7a89ea5404b1 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
7a89ea5404b1 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
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
6 # (at your option) any later version.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
7
7a89ea5404b1 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,
7a89ea5404b1 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
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
11 # GNU General Public License for more details.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
12
7a89ea5404b1 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
7a89ea5404b1 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/>.
174
230d316cc43b Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents: 160
diff changeset
15 import math
230d316cc43b Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents: 160
diff changeset
16
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17 from parpg.quest_engine import QuestEngine
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
18
187
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
19 def script_help(object):
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
20 """Python's help() function with the no-parameters path disabled"""
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
21 help(object)
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
22
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
23 class GameState(object):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
24 """This class holds the current state of the game."""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
25 def __init__(self, quests_dir = None):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
26 self.player_character = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
27 self.quest_engine = QuestEngine(quests_dir)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
28 self.quest_engine.readQuests()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
29 self.objects = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
30 self.object_ids = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
31 self.current_map_name = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
32 self.maps = {}
58
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
33 self.npcs_met = set()
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 101
diff changeset
34 self.funcs = {
187
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
35 "__builtins__":None,
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
36 "help":script_help,
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
37 "sqrt":math.sqrt,
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
38 "log":math.log,
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
39 "str":str,
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
40 "meet":self.meet,
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
41 "met":self.met,
ab6a0fd1668a Added help, moveObject, deleteObject, putItemIntoContainer, equipItem, createItemByID and createItemByType functions to the GameEnvironment.
Beliar <KarstenBock@gmx.net>
parents: 184
diff changeset
42 }
183
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
43 self.locals = {}
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
44
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
45
73
5c751b9a3d8b The addObject method of the GameState class now accepts an object_id parameter, instead of it getting that value from the object.
KarstenBock@gmx.net
parents: 58
diff changeset
46 def addObject(self, object_id, map_id, game_object):
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
47 """Adds an object to the objects and object_ids
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
48 dictionaries.
73
5c751b9a3d8b The addObject method of the GameState class now accepts an object_id parameter, instead of it getting that value from the object.
KarstenBock@gmx.net
parents: 58
diff changeset
49 @param object_id: ID of the object
5c751b9a3d8b The addObject method of the GameState class now accepts an object_id parameter, instead of it getting that value from the object.
KarstenBock@gmx.net
parents: 58
diff changeset
50 @type object_id: str
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
51 @param map_id: ID of the map the object is on.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
52 If the object is in a container this has to be None
73
5c751b9a3d8b The addObject method of the GameState class now accepts an object_id parameter, instead of it getting that value from the object.
KarstenBock@gmx.net
parents: 58
diff changeset
53 @type map_id: str or None
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
54 @param object: object to be added
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
55 @type object: GameObject
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
56 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
57 if not self.object_ids.has_key(object_id):
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
58 if not self.objects.has_key(map_id):
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
59 self.objects[map_id] = {}
74
c9818290bbe7 Objects that are not on an actual map are now stored under the value None in the gamestates object dictionary.
KarstenBock@gmx.net
parents: 73
diff changeset
60 self.objects[map_id][object_id] = game_object
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
61 self.object_ids[object_id] = map_id
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
62
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
63 def deleteObject(self, object_id):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
64 """Removes an object from the dictionaries
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
65 @param object_id: ID of the object
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
66 @type object_id: str
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
67 @returns The deleted object
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
68 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
69 if self.hasObject(object_id):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
70 map_id = self.getMapOfObject(object_id)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
71 if map_id:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
72 inst = self.maps[map_id].agent_layer.getInstance(object_id)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
73 self.maps[map_id].agent_layer.deleteInstance(inst)
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
74 obj = self.objects[map_id][object_id]
74
c9818290bbe7 Objects that are not on an actual map are now stored under the value None in the gamestates object dictionary.
KarstenBock@gmx.net
parents: 73
diff changeset
75 del self.objects[map_id][object_id]
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
76 del self.object_ids[object_id]
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
77 return obj
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
78 return None
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
79
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
80
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
81 def getObjectsFromMap(self, map_id):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
82 """Gets all objects that are currently on the given map.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
83 @type map: String
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
84 @param map: The map name.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
85 @returns: The list of objects on this map. Or an empty list"""
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 101
diff changeset
86 return [i for i in self.getObjectDictOfMap(map_id).values()
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 101
diff changeset
87 if map_id in self.objects]
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 101
diff changeset
88
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 101
diff changeset
89
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 101
diff changeset
90 def getObjectDictOfMap(self, map_id):
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
91 if map_id in self.objects:
160
75c0b728ccf3 Further work on the scripting system.
KarstenBock@gmx.net
parents: 101
diff changeset
92 return self.objects[map_id]
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
93 return {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
94
88
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
95 def deleteObjectsFromMap(self, map_id):
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
96 """Deletes all objects of the given map.
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
97 @type map: String
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
98 @param map: The map name.
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
99 @returns: None"""
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
100 deleted_objs = []
88
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
101 if map_id in self.objects:
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
102 for obj in self.objects[map_id].copy():
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
103 deleted_objs.append(self.deleteObject(obj))
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
104 return deleted_objs
88
0411a4bcceee Fixed moving between maps.
KarstenBock@gmx.net
parents: 75
diff changeset
105
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
106 def hasObject(self, object_id):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
107 """Check if an object with the given id is present
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
108 @param object_id: ID of the object
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
109 @type object_id: str
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
110 @return: True if there is an object False if not
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
111 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
112 return self.object_ids.has_key(object_id)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
113
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
114 def getMapOfObject(self, object_id):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
115 """Returns the map the object is on.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
116 @param object_id: ID of the object
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
117 @type object_id: str
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
118 @return: Name of the map the object is on.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
119 If there is no such object or the object is in a container None is returned
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
120 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
121 if self.object_ids.has_key(object_id):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
122 return self.object_ids[object_id]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
123 return None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
124
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
125 def getObjectById(self, obj_id, map_id = None):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
126 """Gets an object by its object id and map id
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
127 @type obj_id: String
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
128 @param obj_id: The id of the object.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
129 @type map_id: String
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
130 @param map_id: It id of the map containing the object.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
131 @returns: The object or None."""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
132 if not map_id:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
133 map_id = self.getMapOfObject(obj_id)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
134 if not map_id in self.objects:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
135 self.objects[map_id] = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
136 if obj_id in self.objects[map_id]:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
137 return self.objects[map_id][obj_id]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
138
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
139 def clearObjects(self):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
140 """Delete all objects from the state
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
141 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
142 self.objects = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
143 self.object_ids = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
144
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
145 def getStateForSaving(self):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
146 """Prepares state for saving
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
147 @type state: dictionary
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
148 @param state: State of the object
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
149 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
150 ret_dict = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
151 ret_dict["CurrentMap"] = self.current_map_name
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
152 ret_dict["Quests"] = self.quest_engine.getStateForSaving()
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
153 ret_dict["NPCsMet"] = self.npcs_met
183
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
154 ret_dict["locals"] = self.locals
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
155 return ret_dict
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
156
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
157 def restoreFromState(self, state):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
158 """Restores the state"""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
159 self.current_map_name = state["CurrentMap"]
101
824f3068ef2a Fixed Saving and Loading.
KarstenBock@gmx.net
parents: 88
diff changeset
160 self.npcs_met = state["NPCsMet"]
183
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
161 self.locals = state["locals"]
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
162 self.quest_engine.readQuests()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
163 self.quest_engine.restoreFromState(state["Quests"])
58
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
164
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
165 def meet(self, npc):
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
166 """Record that the PC has met a certain NPC
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
167 @type npc: str
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
168 @param npc: The NPC's name or id"""
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
169 if npc in self.npcs_met:
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
170 # we could raise an error here, but should probably be a warn
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
171 # raise RuntimeError("I already know %s" % npc)
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
172 return
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
173 self.npcs_met.add(npc)
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
174
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
175 def met(self, npc):
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
176 """Indicate whether the PC has met this npc before
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
177 @type npc: str
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
178 @param npc: The NPC's name or id
d4f213b99d41 Added meet and met methods from the old player object to the gamestate.
KarstenBock@gmx.net
parents: 44
diff changeset
179 @return: None"""
174
230d316cc43b Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents: 160
diff changeset
180 return npc in self.npcs_met
230d316cc43b Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents: 160
diff changeset
181
230d316cc43b Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents: 160
diff changeset
182 def getGameEnvironment(self):
230d316cc43b Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents: 160
diff changeset
183 """Returns a 2 item list containing the entities and functions that
230d316cc43b Moved the getEnvironment method from the ScriptingSystem to the GameState and renamed it to getGameEnvironment.
Beliar
parents: 160
diff changeset
184 can work with it. This can be used in functions like eval or exec"""
183
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
185 globals = {}
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
186 globals.update(self.funcs)
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
187 globals.update(self.getObjectDictOfMap(self.current_map_name))
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
188
7b6aba7839ea getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary.
Beliar <KarstenBock@gmx.net>
parents: 180
diff changeset
189 return globals, self.locals