Skip to content

Commit

Permalink
fixed pathfinding issue. planning untested. added tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
bitcraft committed Apr 23, 2013
1 parent 6a2c543 commit 9b83fd0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 24 deletions.
44 changes: 24 additions & 20 deletions npc/pirate/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,24 @@ def get_position(entity, memory):

class LookAction(CalledOnceContext):
def enter(self):
print "LOOKING"
self.parent.environment.look(self.parent)


class MoveAction(ActionContext):
def enter(self):
self.path = self.parent.environment.pathfind(self.startpoint,
self.endpoint)

# remove the first node, which is the starting node
self.path.pop()

def update(self, time):
print self, "moving?"
if self.path:
pos = self.path.pop()
self.parent.environment.set_position(self.parent,
(self.parent.environment, pos))
else:
self.finish()

def setStartpoint(self, pos):
self.startpoint = pos
Expand All @@ -46,34 +57,27 @@ class PickupAction(CalledOnceContext):


class DrinkRumAction(ActionContext):
def start(self):
self.caller.drunkness = 1
super(drink_rum, self).start()
def enter(self):
self.drunkness = 1

def update(self, time):
if self.valid():
self.caller.drunkness += 1
if self.caller.drunkness == 5:
self.finish()
else:
self.fail()

def finish(self):
super(drink_rum, self).finish()
self.drunkness += 1
if self.drunkness == 5:
self.finish()

exported_actions = []


exported_actions = []
### ACTION BUILDERS
###

class move_to_entity(ActionBuilder):
"""
return a list of action that this caller is able to move with
you MUST have a mechanism that depletes a counter when moving, otherwise
the planner will loop lessly moving the agent around to different places.
"""

def get_actions(self, caller, memory):
here = get_position(caller, memory)
visited = []

for pct in memory.of_class(PositionPrecept):
Expand All @@ -83,8 +87,10 @@ def get_actions(self, caller, memory):
visited.append(pct.position)

action = MoveAction(caller)
action.setStartpoint(here)
action.setEndpoint(pct.position[1])
action.effects.append(PositionGoal(caller, pct.position))
print ">>> MOVE >>>", action, action.startpoint, action.endpoint
yield action

exported_actions.append(move_to_entity)
Expand All @@ -110,7 +116,6 @@ class drink_rum(ActionBuilder):
"""
drink rum that is in caller's inventory
"""

def get_actions(self, caller, memory):
for pct in memory.of_class(PositionPrecept):
if pct.position[0] == 'self' and pct.entity.name == "rum":
Expand All @@ -123,7 +128,6 @@ def get_actions(self, caller, memory):
exported_actions.append(drink_rum)



class look(ActionBuilder):
def get_actions(self, caller, memory):
action = LookAction(caller)
Expand Down
1 change: 1 addition & 0 deletions pathfinding/astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def search(start, finish, factory):

finishNode = factory(finish)
startNode = factory(start)
print finishNode, startNode
startNode.h = calcH(startNode, finishNode)

# used to locate nodes in the heap and modify their f scores
Expand Down
15 changes: 13 additions & 2 deletions pygoap/environment2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,27 @@ def get_surrounding(self, position):
"""
Return all positions around this one.
"""

x, y = position

return ((x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1),
(x+1, y-1), (x+1, y), (x+1, y+1))

def calc_h(self, position1, position2):
return distance(position1, position2)

def factory(self, position):
return Node(position)

# EPIC HACK
# fix this when position conventions are standardized
try:
if len(position[1]) == 2:
x, y = position[1]
else:
x, y = position
except TypeError:
x, y = position

return Node((x, y))


class XYEnvironment(Environment, Pathfinding2D):
Expand Down
2 changes: 1 addition & 1 deletion pygoap/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def plan(parent, builders, start_action, start_memory, goal):
if success:
path = [keyNode.action]
while keyNode.parent is not None:
path.append(keyNode.action)
keyNode = keyNode.parent
path.append(keyNode.action)

return path

Expand Down
1 change: 1 addition & 0 deletions pygoap/tiledenvironment.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def render(self, surface):

for t in self.entities:
env, (x, y) = self.get_position(t)
print env, x, y
x *= self.tiledmap.tilewidth
y *= self.tiledmap.tileheight

Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def run_once():
elif time == 3:
rum = ObjectBase("rum")
formosa.add(rum)
formosa.set_position(rum, (formosa, (0,2)))
formosa.set_position(rum, (formosa, (2,5)))

elif time == 6:
wench = Human("Female", "wench")
Expand Down
31 changes: 31 additions & 0 deletions tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from pygoap.actions import *
from pygoap.goals import *

from pygoap.agent import GoapAgent
from pygoap.environment import Environment


class PrintActionContext(CalledOnceContext):
def enter(self):
print "hello world"

class PrintAction(ActionBuilder):
def get_actions(self, caller, memory):
action = PrintActionContext(caller)
action.effects.append(SimpleGoal(introduced_self=True))
yield action



agent = GoapAgent()
agent.add_action(PrintAction())

friendly_goal = SimpleGoal(introduced_self=True)
agent.add_goal(friendly_goal)

env = Environment()
env.add(agent)

env.update(1)


0 comments on commit 9b83fd0

Please sign in to comment.