source: trunk/game/scripts/objects/actors.py @ 563

Revision 563, 15.4 KB checked in by beliar, 10 years ago (diff)

Ticket #200: Patch by Beliar.

  • Merging map_loading_change branch back into trunk

fixes[s:trac, t:200]

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18from fife import fife
19from base import GameObject, Living, Scriptable, CharStats
20from fife.extensions.fife_settings import Setting
21from composed import CarryableItem
22from scripts.inventory import Inventory
23from random import randrange
24
25"""All actors go here. Concrete classes only."""
26
27__all__ = ["PlayerCharacter", "NonPlayerCharacter",]
28
29TDS = Setting(app_name="PARPG",
30              settings_file="./settings.xml", 
31              settings_gui_xml="")
32_AGENT_STATE_NONE, _AGENT_STATE_IDLE, _AGENT_STATE_APPROACH, _AGENT_STATE_RUN, _AGENT_STATE_WANDER, _AGENT_STATE_TALK = xrange(6)
33
34class ActorBehaviour (fife.InstanceActionListener):
35    """Fife agent listener"""
36    def __init__(self, layer):
37        fife.InstanceActionListener.__init__(self)
38        self.layer = layer
39        self.agent = None
40        self.state = None
41        self.speed = 0
42        self.idle_counter = 1
43   
44    def attachToLayer(self, agent_ID):
45        """Attaches to a certain layer
46           @type agent_ID: String
47           @param agent_ID: ID of the layer to attach to.
48           @return: None"""
49        self.agent = self.layer.getInstance(agent_ID)
50        self.agent.addActionListener(self)
51        self.state = _AGENT_STATE_NONE
52        # TODO: rework/improve
53        self.speed = TDS.get("PARPG", "PCSpeed")-1
54       
55    def getX(self):
56        """Get the NPC's x position on the map.
57           @rtype: integer"
58           @return: the x coordinate of the NPC's location"""
59        return self.agent.getLocation().getLayerCoordinates().x
60
61    def getY(self):
62        """Get the NPC's y position on the map.
63           @rtype: integer
64           @return: the y coordinate of the NPC's location"""
65        return self.agent.getLocation().getLayerCoordinates().y
66       
67    def onNewMap(self, layer):
68        """Sets the agent onto the new layer."""
69        if self.agent is not None:
70            self.agent.removeActionListener(self)
71           
72        self.agent = layer.getInstance(self.parent.ID)
73        self.agent.addActionListener(self)
74        self.state = _AGENT_STATE_NONE
75        self.idle_counter = 1
76   
77    def idle(self):
78        """@return: None"""
79        self.state = _AGENT_STATE_IDLE
80        self.agent.act('stand', self.agent.getFacingLocation())
81
82    def onInstanceActionFinished(self, instance, action):
83        pass
84
85class PCBehaviour (ActorBehaviour):
86    def __init__(self, parent = None, layer = None):
87        super(PCBehaviour, self).__init__(layer)       
88        self.parent = parent
89        self.idle_counter = 1
90        # TODO: rework/improve
91        self.speed = TDS.get("PARPG", "PCSpeed")
92        self.nextAction = None
93        self.agent = None
94       
95    def onInstanceActionFinished(self, instance, action):
96        """@type instance: ???
97           @param instance: ???
98           @type action: ???
99           @param action: ???
100           @return: None"""
101        # First we reset the next behavior
102        act = self.nextAction
103        self.nextAction = None 
104        self.idle()
105       
106        if act:
107            act.execute()
108           
109        if(action.getId() != 'stand'):
110            self.idle_counter = 1
111        else:
112            self.idle_counter += 1           
113
114class PlayerCharacter (GameObject, Living, CharStats):
115    """PC class"""
116    def __init__ (self, ID, agent_layer = None, inventory = None, text = "Its you. Who would've thought that?", **kwargs):
117        GameObject.__init__( self, ID, text = text, **kwargs )
118        Living.__init__( self, **kwargs )
119        CharStats.__init__( self, **kwargs )
120
121        self.is_PC = True
122        self.behaviour = None
123       
124        # PC _has_ an inventory, he _is not_ one
125        if inventory == None:
126            self.inventory = Inventory()
127            self.inventory.placeItem(CarryableItem(ID=456, name="Dagger123"))
128            self.inventory.placeItem(CarryableItem(ID=555, name="Beer"))
129            self.inventory.placeItem(CarryableItem(ID = 616,
130                                                   name = "Pamphlet",
131                                                   image = "/gui/inv_images/inv_pamphlet.png"))
132        else:
133            self.inventory = inventory
134        self.peopleIknow = set()
135
136        self.state = _AGENT_STATE_NONE
137        self.layer_id = agent_layer.getId()
138        self.createBehaviour(agent_layer)
139   
140    def getLocation(self):
141        """Get the NPC's position as a fife.Location object. Basically a
142           wrapper.
143           @rtype: fife.Location
144           @return: the location of the NPC"""
145        return self.behaviour.agent.getLocation()
146
147    def getStateForSaving(self):
148        """Returns state for saving
149        """
150        ret_dict = super(PlayerCharacter, self).getStateForSaving()
151        ret_dict["Inventory"] = self.inventory.serializeInventory()
152        ret_dict["PeopleKnown"] = self.peopleIknow
153        return ret_dict
154   
155    def meet(self, npc):
156        """Record that the PC has met a certain NPC
157           @type npc: str
158           @param npc: The NPC's name or id"""
159        if npc in self.peopleIknow:
160            # we could raise an error here, but should probably be a warn
161            # raise RuntimeError("I already know %s" % npc)
162            return
163        self.peopleIknow.add(npc)
164
165    def met(self, npc):
166        """Indicate whether the PC has met this npc before
167           @type npc: str
168           @param npc: The NPC's name or id
169           @return: None"""
170        return npc in self.peopleIknow
171
172    def createBehaviour(self, layer):
173        """Creates the behaviour for this actor.
174           @return: None"""
175        self.behaviour = PCBehaviour(self, layer)
176   
177    def setup(self):
178        """@return: None"""
179        self.behaviour.attachToLayer(self.ID)
180
181    def start(self):
182        """@return: None"""
183        self.behaviour.idle()
184   
185    def run(self, location):
186        """Makes the PC run to a certain location
187           @type location: fife.ScreenPoint
188           @param location: Screen position to run to.
189           @return: None"""
190        self.state = _AGENT_STATE_RUN
191        self.behaviour.nextAction = None
192        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
193
194    def walk(self, location):
195        """Makes the PC walk to a certain location.
196           @type location: fife.ScreenPoint
197           @param location: Screen position to walk to.
198           @return: None"""
199        self.state = _AGENT_STATE_RUN
200        self.behaviour.nextAction = None 
201        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
202
203    def teleport(self, location):
204        """Teleports a PC instantly to the given location.
205           @type location: fife.Location
206           @param location: Target coordinates for PC.
207           @return: None"""
208        self.state = _AGENT_STATE_IDLE
209        self.behaviour.nextAction = None 
210        self.behaviour.agent.setLocation(location)
211
212    def approach(self, location, action = None):
213        """Approaches a location and then perform an action (if set).
214           @type loc: fife.Location
215           @param loc: the location to approach
216           @type action: Action
217           @param action: The action to schedule for execution after the approach.
218           @return: None"""
219        self.state = _AGENT_STATE_APPROACH
220        self.behaviour.nextAction = action
221        boxLocation = tuple([int(float(i)) for i in location])
222        l = fife.Location(self.behaviour.agent.getLocation())
223        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
224        self.behaviour.agent.move('run', l, self.behaviour.speed+1)
225   
226    def _getCoords(self):
227        """Get-er property function"""
228        return (self.getLocation().getMapCoordinates().x, 
229                self.getLocation().getMapCoordinates().y)
230   
231    def _setCoords(self, coords):
232        """Set-er property function"""
233        map_coords = self.getLocation().getMapCoordinates()
234        map_coords.X, map_coords.Y = float(coords[0]), float (coords[1])
235        self.teleport(map_coords)
236   
237    coords = property (_getCoords, _setCoords, 
238        doc = "Property allowing you to get and set the object's \
239                coordinates via tuples")
240
241
242class NPCBehaviour(ActorBehaviour):
243    def __init__(self, Parent = None, Layer = None):
244        super(NPCBehaviour, self).__init__(Layer)
245       
246        self.parent = Parent
247        self.state = _AGENT_STATE_NONE
248        self.pc = None
249        self.target_loc = None
250        self.nextAction = None
251       
252        # hard code this for now
253        self.distRange = (2, 4)
254       
255    def getTargetLocation(self):
256        """@rtype: fife.Location
257           @return: NPC's position"""
258        x = self.getX()
259        y = self.getY()
260        if self.state == _AGENT_STATE_WANDER:
261            """ Random Target Location """
262            l = [0, 0]
263            for i in range(len(l)):
264                sign = randrange(0, 2)
265                dist = randrange(self.distRange[0], self.distRange[1])
266                if sign == 0:
267                    dist *= -1
268                l[i] = dist
269            x += l[0]
270            y += l[1]
271            # Random walk is
272            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
273        l = fife.Location(self.agent.getLocation())
274        l.setLayerCoordinates(fife.ModelCoordinate(x, y))
275        return l
276
277    def onInstanceActionFinished(self, instance, action):
278        """What the NPC does when it has finished an action.
279           Called by the engine and required for InstanceActionListeners.
280           @type instance: fife.Instance
281           @param instance: self.agent (the NPC listener is listening for this
282                                        instance)
283           @type action: ???
284           @param action: ???
285           @return: None"""
286        if self.state == _AGENT_STATE_WANDER:
287            self.target_loc = self.getTargetLocation()
288        self.idle()
289       
290   
291    def idle(self):
292        """Controls the NPC when it is idling. Different actions
293           based on the NPC's state.
294           @return: None"""
295        if self.state == _AGENT_STATE_NONE:
296            self.state = _AGENT_STATE_IDLE
297            self.agent.act('stand', self.agent.getFacingLocation())
298        elif self.state == _AGENT_STATE_IDLE:
299            self.target_loc = self.getTargetLocation()
300            self.state = _AGENT_STATE_WANDER
301            self.agent.act('stand', self.agent.getFacingLocation())
302        elif self.state == _AGENT_STATE_WANDER:
303            self.parent.wander(self.target_loc)
304            self.state = _AGENT_STATE_NONE
305        elif self.state == _AGENT_STATE_TALK:
306            self.agent.act('stand', self.pc.getLocation())
307
308class NonPlayerCharacter(GameObject, Living, Scriptable, CharStats):
309    """NPC class"""
310    def __init__(self, ID, agent_layer=None, name='NPC', \
311                 text = 'A nonplayer character', inventory = None, 
312                 real_name = 'NPC', dialogue = None, **kwargs):
313        # init game object
314        GameObject.__init__(self, ID, name=name, real_name=real_name, text = text, **kwargs)
315        Living.__init__(self, **kwargs)
316        Scriptable.__init__(self, **kwargs)
317        CharStats.__init__(self, **kwargs)
318
319        self.is_NPC = True
320        self.behaviour = None
321        self.state = None
322       
323        if inventory == None:
324            self.inventory = Inventory()
325            self.inventory.placeItem(CarryableItem(ID = 632,
326                                                   name = "box",
327                                                   image = "/gui/inv_images/inv_giftbox.png"))
328
329        else:
330            self.inventory = inventory
331        self.layer_id = agent_layer.getId()
332        self.createBehaviour(agent_layer)       
333        self.dialogue = dialogue
334
335    def prepareStateForSaving(self, state):
336        """Prepares state for saving
337        @type state: dictionary
338        @param state: State of the object 
339        """
340        GameObject.prepareStateForSaving(self, state)
341        del state["behaviour"]
342
343    def getStateForSaving(self):
344        """Returns state for saving
345        """
346        ret_dict = super(NonPlayerCharacter, self).getStateForSaving()
347        ret_dict["Lives"] = self.is_living
348        ret_dict["State"] = self.behaviour.state
349        return ret_dict
350
351    def createBehaviour(self, layer):
352        """Creates the behaviour for this actor.
353           @return None """
354        self.behaviour = NPCBehaviour(self, layer)
355
356    def getLocation(self):
357        """Get the NPC's position as a fife.Location object. Basically a
358           wrapper.
359           @rtype: fife.Location
360           @return: the location of the NPC"""
361        return self.behaviour.agent.getLocation()
362   
363    def teleport(self, location):
364        """Teleports a NPC instantly to the given location.
365           @type location: fife.Location
366           @param location: Target coordinates for PC.
367           @return: None"""
368        self.state = _AGENT_STATE_IDLE
369        self.behaviour.nextAction = None 
370        self.behaviour.agent.setLocation(location)
371
372    def wander(self, location):
373        """Nice slow movement for random walking.
374           @type location: fife.Location
375           @param location: Where the NPC will walk to.
376           @return: None"""
377        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
378
379    def run(self, location):
380        """Faster movement than walk.
381           @type location: fife.Location
382           @param location: Where the NPC will run to."""
383        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
384
385    def talk(self, pc):
386        """Makes the NPC ready to talk to the PC
387           @return: None"""
388        self.behaviour.state = _AGENT_STATE_TALK
389        self.behaviour.pc = pc.behaviour.agent
390        self.behaviour.idle()
391       
392    def give (self, item, actor):
393        """Gives the specified item to the different actor. Raises an exception if the item was invalid or not found
394           @type item: Carryable
395           @param item: The item object to give
396           @param actor: Person to give item to"""
397        if item == None: 
398            raise ValueError("I don't have %s" % item.name)
399        self.inventory.takeItem(item)
400        actor.inventory.placeItem(item)           
401   
402    def setup(self):
403        """@return: None"""
404        self.behaviour.attachToLayer(self.ID)
405
406    def start(self):
407        """@return: None"""
408        self.behaviour.idle()
409
410    def _getCoords(self):
411        """Get-er property function"""
412        return (self.getLocation().getMapCoordinates().x, 
413                self.getLocation().getMapCoordinates().y)
414   
415    def _setCoords(self, coords):
416        """Set-er property function"""
417        map_coords = self.getLocation().getMapCoordinates()
418        map_coords.X, map_coords.Y = float(coords[0]), float (coords[1])
419        self.teleport(map_coords)
420
421    coords = property (_getCoords, _setCoords, 
422        doc = "Property allowing you to get and set the object's \
423                coordinates via tuples")
Note: See TracBrowser for help on using the repository browser.