diff --git a/npc/pirate/actions.py b/npc/pirate/actions.py index 9b3493a..a4f4b04 100644 --- a/npc/pirate/actions.py +++ b/npc/pirate/actions.py @@ -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 @@ -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): @@ -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) @@ -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": @@ -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) diff --git a/pathfinding/astar.py b/pathfinding/astar.py index 1ebe0e3..c371429 100644 --- a/pathfinding/astar.py +++ b/pathfinding/astar.py @@ -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 diff --git a/pygoap/environment2d.py b/pygoap/environment2d.py index ed0c79e..d1bdf6a 100644 --- a/pygoap/environment2d.py +++ b/pygoap/environment2d.py @@ -40,8 +40,8 @@ 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)) @@ -49,7 +49,18 @@ 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): diff --git a/pygoap/planning.py b/pygoap/planning.py index b8dd3b6..e6bdc04 100644 --- a/pygoap/planning.py +++ b/pygoap/planning.py @@ -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 diff --git a/pygoap/tiledenvironment.py b/pygoap/tiledenvironment.py index d88d885..59894f0 100644 --- a/pygoap/tiledenvironment.py +++ b/pygoap/tiledenvironment.py @@ -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 diff --git a/test.py b/test.py index 945472a..b83e97f 100644 --- a/test.py +++ b/test.py @@ -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") diff --git a/tutorial.py b/tutorial.py new file mode 100644 index 0000000..9d1582d --- /dev/null +++ b/tutorial.py @@ -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) + +