annotate behaviours/npc.py @ 130:aea5a9229b4c

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