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