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

Revision 622, 14.7 KB checked in by vaporice, 9 years ago (diff)

test

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env 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
114
115class NPCBehaviour(ActorBehaviour):
116    def __init__(self, Parent = None, Layer = None):
117        super(NPCBehaviour, self).__init__(Layer)
118       
119        self.parent = Parent
120        self.state = _AGENT_STATE_NONE
121        self.pc = None
122        self.target_loc = None
123        self.nextAction = None   
124        self.break_wandering = False
125        # hard code this for now
126        self.distRange = (2, 4)
127       
128    def getTargetLocation(self):
129        """@rtype: fife.Location
130           @return: NPC's position"""
131        x = self.getX()
132        y = self.getY()
133        if self.state == _AGENT_STATE_WANDER:
134            """ Random Target Location """
135            l = [0, 0]
136            for i in range(len(l)):
137                sign = randrange(0, 2)
138                dist = randrange(self.distRange[0], self.distRange[1])
139                if sign == 0:
140                    dist *= -1
141                l[i] = dist
142            x += l[0]
143            y += l[1]
144            # Random walk is
145            # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
146        l = fife.Location(self.agent.getLocation())
147        l.setLayerCoordinates(fife.ModelCoordinate(x, y))
148        return l
149
150    def onInstanceActionFinished(self, instance, action):
151        """What the NPC does when it has finished an action.
152           Called by the engine and required for InstanceActionListeners.
153           @type instance: fife.Instance
154           @param instance: self.agent (the NPC listener is listening for this
155                                        instance)
156           @type action: ???
157           @param action: ???
158           @return: None"""
159        if self.state == _AGENT_STATE_WANDER:
160            self.target_loc = self.getTargetLocation()
161        self.idle()
162       
163    def breakWandering(self):
164            self.break_wandering = True
165
166    def continueWandering(self):
167            self.break_wandering = False
168
169    def idle(self):
170        """Controls the NPC when it is idling. Different actions
171           based on the NPC's state.
172           @return: None"""
173        if self.state == _AGENT_STATE_NONE:
174            self.state = _AGENT_STATE_IDLE
175            self.agent.act('stand', self.agent.getFacingLocation())
176        elif self.state == _AGENT_STATE_IDLE:
177            self.target_loc = self.getTargetLocation()
178            self.agent.act('stand', self.agent.getFacingLocation())
179            if not self.break_wandering:
180                 self.state = _AGENT_STATE_WANDER
181        elif self.state == _AGENT_STATE_WANDER:
182            self.parent.wander(self.target_loc)
183            self.state = _AGENT_STATE_NONE
184        elif self.state == _AGENT_STATE_TALK:
185            self.agent.act('stand', self.pc.getLocation())
186           
187class CharacterBase(GameObject, Living, CharStats):
188    """Base class for Characters"""
189    def __init__(self, ID, agent_layer = None, inventory = None, 
190                 text = "", **kwargs):
191        GameObject.__init__( self, ID, text = text, **kwargs )
192        Living.__init__( self, **kwargs )
193        CharStats.__init__( self, **kwargs )
194       
195        self.behaviour = None
196       
197        if inventory == None:
198            self.inventory = Inventory()
199        else:
200            self.inventory = inventory
201
202        self.state = _AGENT_STATE_NONE
203        self.layer_id = agent_layer.getId()
204        self.createBehaviour(agent_layer)
205   
206    def createBehaviour(self, layer):
207        """Creates the behaviour for this actor.
208           @return: None"""
209        pass
210   
211    def setup(self):
212        """@return: None"""
213        self.behaviour.attachToLayer(self.ID)
214
215    def start(self):
216        """@return: None"""
217        self.behaviour.idle()
218
219    def teleport(self, location):
220        """Teleports a Character instantly to the given location.
221           @type location: fife.Location
222           @param location: Target coordinates for Character.
223           @return: None"""
224        self.state = _AGENT_STATE_IDLE
225        self.behaviour.nextAction = None 
226        self.behaviour.agent.setLocation(location)
227
228    def give (self, item, actor):
229        """Gives the specified item to the different actor. Raises an exception if the item was invalid or not found
230           @type item: Carryable
231           @param item: The item object to give
232           @param actor: Person to give item to"""
233        if item == None: 
234            raise ValueError("I don't have %s" % item.name)
235        self.inventory.takeItem(item)
236        actor.inventory.placeItem(item)           
237       
238    def hasItem(self, item_type):
239        """Returns wether an item is present in the players inventory or not
240        @param item_type: ID of the item
241        @type item_type: str
242        @return: True when the item is present, False when not"""
243        return self.inventory.findItem(item_type = item_type)
244
245    def itemCount(self, item_type = ""):
246        """Returns number of all items or items specified by item_type
247        the player has.
248        @param item_type: ID of the item, can be empty
249        @type item_type: str
250        @return: Number of items"""
251        return self.inventory.count(item_type)
252
253    def getLocation(self):
254        """Get the NPC's position as a fife.Location object. Basically a
255           wrapper.
256           @rtype: fife.Location
257           @return: the location of the NPC"""
258        return self.behaviour.agent.getLocation()
259   
260    def run(self, location):
261        """Makes the PC run to a certain location
262           @type location: fife.ScreenPoint
263           @param location: Screen position to run to.
264           @return: None"""
265        self.state = _AGENT_STATE_RUN
266        self.behaviour.nextAction = None
267        self.behaviour.agent.move('run', location, self.behaviour.speed+1)
268
269    def walk(self, location):
270        """Makes the PC walk to a certain location.
271           @type location: fife.ScreenPoint
272           @param location: Screen position to walk to.
273           @return: None"""
274        self.state = _AGENT_STATE_RUN
275        self.behaviour.nextAction = None 
276        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
277
278    def getStateForSaving(self):
279        """Returns state for saving
280        """
281        ret_dict = GameObject.getStateForSaving(self)
282        ret_dict["Inventory"] = self.inventory.serializeInventory()
283        return ret_dict
284
285    def _getCoords(self):
286        """Get-er property function"""
287        return (self.getLocation().getMapCoordinates().x, 
288                self.getLocation().getMapCoordinates().y)
289   
290    def _setCoords(self, coords):
291        """Set-er property function"""
292        map_coords = self.getLocation().getMapCoordinates()
293        map_coords.X, map_coords.Y = float(coords[0]), float (coords[1])
294        self.teleport(map_coords)
295   
296    coords = property (_getCoords, _setCoords, 
297        doc = "Property allowing you to get and set the object's \
298                coordinates via tuples")
299           
300class PlayerCharacter (CharacterBase):
301    """PC class"""
302    def __init__ (self, ID, agent_layer = None, inventory = None, 
303                  text = "Its you. Who would've thought that?", **kwargs):
304        if inventory == None:
305            inventory = Inventory()
306            inventory.placeItem(CarryableItem(ID=456, name="Dagger123"))
307            inventory.placeItem(CarryableItem(ID=555, name="Beer"))
308            inventory.placeItem(CarryableItem(ID = 616,
309                                    name = "Pamphlet",
310                                    image = "/gui/inv_images/inv_pamphlet.png"))
311        CharacterBase.__init__(self, ID, agent_layer, inventory, text, **kwargs)
312        self.people_i_know = set()
313        self.attributes.append("PC")
314 
315    def getStateForSaving(self):
316        """Returns state for saving
317        """
318        ret_dict = super(PlayerCharacter, self).getStateForSaving()
319        ret_dict["PeopleKnown"] = self.people_i_know
320        return ret_dict
321   
322    def meet(self, npc):
323        """Record that the PC has met a certain NPC
324           @type npc: str
325           @param npc: The NPC's name or id"""
326        if npc in self.people_i_know:
327            # we could raise an error here, but should probably be a warn
328            # raise RuntimeError("I already know %s" % npc)
329            return
330        self.people_i_know.add(npc)
331
332    def met(self, npc):
333        """Indicate whether the PC has met this npc before
334           @type npc: str
335           @param npc: The NPC's name or id
336           @return: None"""
337        return npc in self.people_i_know
338
339    def createBehaviour(self, layer):
340        """Creates the behaviour for this actor.
341           @return: None"""
342        self.behaviour = PCBehaviour(self, layer)
343 
344    def approach(self, location, action = None):
345        """Approaches a location and then perform an action (if set).
346           @type loc: fife.Location
347           @param loc: the location to approach
348           @type action: Action
349           @param action: The action to schedule for execution after the approach.
350           @return: None"""
351        self.state = _AGENT_STATE_APPROACH
352        self.behaviour.nextAction = action
353        boxLocation = tuple([int(float(i)) for i in location])
354        l = fife.Location(self.behaviour.agent.getLocation())
355        l.setLayerCoordinates(fife.ModelCoordinate(*boxLocation))
356        self.behaviour.agent.move('run', l, self.behaviour.speed+1)
357   
358class NonPlayerCharacter(CharacterBase, Scriptable):
359    """NPC class"""
360    def __init__(self, ID, agent_layer=None, name='NPC', \
361                 text = 'A nonplayer character', inventory = None, 
362                 real_name = 'NPC', dialogue = None, **kwargs):
363        # init game object
364        CharacterBase.__init__(self, ID, agent_layer = agent_layer, 
365                               inventory = inventory, name=name, 
366                               real_name=real_name, text = text, **kwargs)
367        Scriptable.__init__(self, **kwargs)
368
369        self.attributes.append("NPC")
370        self.dialogue = dialogue
371
372    def prepareStateForSaving(self, state):
373        """Prepares state for saving
374        @type state: dictionary
375        @param state: State of the object 
376        """
377        CharacterBase.prepareStateForSaving(self, state)
378        del state["behaviour"]
379
380    def getStateForSaving(self):
381        """Returns state for saving
382        """
383        ret_dict = CharacterBase.getStateForSaving(self)
384        ret_dict["Lives"] = self.lives
385        ret_dict["State"] = self.behaviour.state
386        return ret_dict
387
388    def createBehaviour(self, layer):
389        """Creates the behaviour for this actor.
390           @return None """
391        self.behaviour = NPCBehaviour(self, layer)
392
393    def wander(self, location):
394        """Nice slow movement for random walking.
395           @type location: fife.Location
396           @param location: Where the NPC will walk to.
397           @return: None"""
398        self.behaviour.agent.move('walk', location, self.behaviour.speed-1)
399
400    def talk(self, pc):
401        """Makes the NPC ready to talk to the PC
402           @return: None"""
403        self.behaviour.state = _AGENT_STATE_TALK
404        self.behaviour.pc = pc.behaviour.agent
405        self.behaviour.idle()
Note: See TracBrowser for help on using the repository browser.