annotate src/parpg/behaviours/npc.py @ 74:47e1345fbac2

Fixed NPC behaviour. NPCs are wandering around again.
author KarstenBock@gmx.net
date Wed, 07 Sep 2011 14:48:08 +0200
parents 58661f5b2f6b
children c3350fc9cd45
rev   line source
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
1 # This file is part of PARPG.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
2
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
6 # (at your option) any later version.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
7
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
11 # GNU General Public License for more details.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
12
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
15
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
16 from random import randrange
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
17
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
18 from fife import fife
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
19
59
2915dbb60940 Fixed error in setting the initial state of the NPC behaviour
KarstenBock@gmx.net
parents: 58
diff changeset
20 import base
2915dbb60940 Fixed error in setting the initial state of the NPC behaviour
KarstenBock@gmx.net
parents: 58
diff changeset
21 from base import BaseBehaviour
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
22
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
23 class NPCBehaviour(BaseBehaviour):
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
24 """This is a basic NPC behaviour"""
73
58661f5b2f6b Removed layer as attribute of BaseBehaviour and added it as a parameter to the attachToLayer method.
KarstenBock@gmx.net
parents: 59
diff changeset
25 def __init__(self, parent=None):
58661f5b2f6b Removed layer as attribute of BaseBehaviour and added it as a parameter to the attachToLayer method.
KarstenBock@gmx.net
parents: 59
diff changeset
26 super(NPCBehaviour, self).__init__()
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
27
58
d5491eb9e3e4 Set default value of layer parameter of BaseBehaviour to None
KarstenBock@gmx.net
parents: 57
diff changeset
28 self.parent = parent
59
2915dbb60940 Fixed error in setting the initial state of the NPC behaviour
KarstenBock@gmx.net
parents: 58
diff changeset
29 self.state = base._AGENT_STATE_NONE
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
30 self.pc = None
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
31 self.target_loc = None
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
32 self.nextAction = None
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
33
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
34 # hard code these for now
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
35 self.distRange = (2, 4)
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
36 # these are parameters to lower the rate of wandering
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
37 # wander rate is the number of "IDLEs" before a wander step
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
38 # this could be set for individual NPCs at load time
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
39 # or thrown out altogether.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
40 self.wanderCounter = 0
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
41 self.wanderRate = 9
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
42
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
43 def getTargetLocation(self):
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
44 """@rtype: fife.Location
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
45 @return: NPC's position"""
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
46 x = self.getX()
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
47 y = self.getY()
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
48 if self.state == base._AGENT_STATE_WANDER:
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
49 """ Random Target Location """
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
50 l = [0, 0]
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
51 for i in range(len(l)):
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
52 sign = randrange(0, 2)
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
53 dist = randrange(self.distRange[0], self.distRange[1])
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
54 if sign == 0:
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
55 dist *= -1
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
56 l[i] = dist
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
57 x += l[0]
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
58 y += l[1]
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
59 # Random walk is
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
60 # rl = randint(-1, 1);ud = randint(-1, 1);x += rl;y += ud
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
61 l = fife.Location(self.agent.getLocation())
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
62 l.setLayerCoordinates(fife.ModelCoordinate(x, y))
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
63 return l
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
64
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
65 def onInstanceActionFinished(self, instance, action):
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
66 """What the NPC does when it has finished an action.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
67 Called by the engine and required for InstanceActionListeners.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
68 @type instance: fife.Instance
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
69 @param instance: self.agent (the NPC listener is listening for this
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
70 instance)
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
71 @type action: ???
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
72 @param action: ???
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
73 @return: None"""
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
74 if self.state == base._AGENT_STATE_WANDER:
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
75 self.target_loc = self.getTargetLocation()
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
76 self.idle()
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
77
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
78
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
79 def idle(self):
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
80 """Controls the NPC when it is idling. Different actions
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
81 based on the NPC's state.
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
82 @return: None"""
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
83 if self.state == base._AGENT_STATE_NONE:
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
84 self.state = base._AGENT_STATE_IDLE
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
85 self.agent.act('stand', self.agent.getFacingLocation())
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
86 elif self.state == base._AGENT_STATE_IDLE:
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
87 if self.wanderCounter > self.wanderRate:
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
88 self.wanderCounter = 0
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
89 self.state = base._AGENT_STATE_WANDER
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
90 else:
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
91 self.wanderCounter += 1
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
92 self.state = base._AGENT_STATE_NONE
55
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
93
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
94 self.target_loc = self.getTargetLocation()
4311a0a5378c Moved behaviours from objects/actors.py to files seperate files inside a behaviours directory.
KarstenBock@gmx.net
parents:
diff changeset
95 self.agent.act('stand', self.agent.getFacingLocation())
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
96 elif self.state == base._AGENT_STATE_WANDER:
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
97 self.wander(self.target_loc)
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
98 self.state = base._AGENT_STATE_NONE
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
99 elif self.state == base._AGENT_STATE_TALK:
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
100 self.agent.act('stand', self.pc.getLocation())
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
101
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
102 def wander(self, location):
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
103 """Nice slow movement for random walking.
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
104 @type location: fife.Location
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
105 @param location: Where the NPC will walk to.
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
106 @return: None"""
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
107 self.agent.move('walk', location, self.speed - 1)