annotate src/parpg/behaviours/npc.py @ 144:a6b25c2cedab

Move comment to correct place.
author KarstenBock@gmx.net
date Mon, 03 Oct 2011 13:05:23 +0200
parents ecac92680bef
children 704145b96171
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.
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
39 self.wanderCounter = 0
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
40 self.wanderRate = 9
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
41
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
42 def getTargetLocation(self):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
43 """@rtype: fife.Location
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
44 @return: NPC's position"""
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
45 x = self.getX()
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
46 y = self.getY()
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
47 if self.state == base._AGENT_STATE_WANDER:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
48 """ Random Target Location """
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
49 l = [0, 0]
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
50 for i in range(len(l)):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
51 sign = randrange(0, 2)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
52 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
53 if sign == 0:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
54 dist *= -1
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
55 l[i] = dist
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
56 x += l[0]
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
57 y += l[1]
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
58 # Random walk is
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
59 # 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
60 l = fife.Location(self.agent.getLocation())
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
61 l.setLayerCoordinates(fife.ModelCoordinate(x, y))
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
62 return l
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
63
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
64 def onInstanceActionFinished(self, instance, action):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
65 """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
66 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
67 @type instance: fife.Instance
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
68 @param instance: self.agent
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
69 @type action: ???
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
70 @param action: ???
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
71 @return: None"""
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
72 if self.state == base._AGENT_STATE_WANDER:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
73 self.target_loc = self.getTargetLocation()
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
74 MovingAgentBehaviour.onInstanceActionFinished(self, instance, action)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
75
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
76
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
77 def idle(self):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
78 """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
79 based on the NPC's state.
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
80 @return: None"""
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
81 if self.state == base._AGENT_STATE_NONE:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
82 self.state = base._AGENT_STATE_IDLE
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
83 self.animate('stand')
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
84 elif self.state == base._AGENT_STATE_IDLE:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
85 if self.wanderCounter > self.wanderRate:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
86 self.wanderCounter = 0
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
87 self.state = base._AGENT_STATE_WANDER
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
88 else:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
89 self.wanderCounter += 1
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
90 self.state = base._AGENT_STATE_NONE
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
91
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
92 self.target_loc = self.getTargetLocation()
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
93 self.animate('stand')
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
94 elif self.state == base._AGENT_STATE_WANDER:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
95 self.wander(self.target_loc)
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
96 self.state = base._AGENT_STATE_NONE
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
97 elif self.state == base._AGENT_STATE_TALK:
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
98 self.animate('stand', self.pc.getLocation())
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
99
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
100 def wander(self, location):
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
101 """Nice slow movement for random walking.
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
102 @type location: fife.Location
ecac92680bef Added animation queue and method the base behaviour class.
KarstenBock@gmx.net
parents: 83
diff changeset
103 @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
104 @return: None"""
74
47e1345fbac2 Fixed NPC behaviour. NPCs are wandering around again.
KarstenBock@gmx.net
parents: 73
diff changeset
105 self.agent.move('walk', location, self.speed - 1)