annotate src/parpg/behaviours/npc.py @ 190:a22e92090018

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