AdvancedActuators¶
This module contains the more advanced actuators. AdvancedActuators allow for more actions and not only movement. It can also be more advanced movement classes.
-
class
gamelib.Actuators.AdvancedActuators.
PathFinder
(game=None, actuated_object=None, circle_waypoints=True, parent=None)¶ Bases:
gamelib.Actuators.Actuator.Behavioral
Important
This module assume a one step movement. If you need more than one step, you will need to sub-class this module and re-implement next_waypoint().
This actuator is a bit different than the simple actuators (
SimpleActuators
) as it requires the knowledge of both the game object and the actuated object.The constructor takes the following parameters:
Parameters: - game (gamelib.Game.Game) – A reference to the instanciated game engine.
- actuated_object (gamelib.BoardItem.BoardItem) – The object to actuate. Deprecated in favor of parent. Only kept for backward compatibility.
- parent (gamelib.BoardItem.BoardItem) – The parent object to actuate.
- circle_waypoints (bool) – If True the next_waypoint() method is going to circle between the waypoints (when the last is visited, go back to the first)
-
add_waypoint
(row, column)¶ Add a waypoint to the list of waypoints.
Waypoints are used one after the other on a FIFO basis (First In, First Out).
If not destination (i.e destination == (None, None)) have been set yet, that method sets it.
Parameters: - row (int) – The “row” part of the waypoint’s coordinate.
- column – The “column” part of the waypoint’s coordinate.
Raises: HacInvalidTypeException – If any of the parameters is not an int.
Example:
pf = PathFinder(game=mygame, actuated_object=npc1) pf.add_waypoint(3,5) pf.add_waypoint(12,15)
-
clear_waypoints
()¶ Empty the waypoints stack.
Example:
pf.clear_waypoints()
-
current_path
()¶ This method simply return a copy of the current path of the actuator.
The current path is to be understood as: the list of positions still remaining. All positions that have already been gone through are removed from the stack.
Important
A copy of the path is returned for every call to that function so be wary of the performances impact.
Example:
mykillernpc.actuator = PathFinder( game=mygame, actuated_object=mykillernpc ) mykillernpc.actuator.set_destination( mygame.player.pos[0], mygame.player.pos[1] ) mykillernpc.actuator.find_path() for i in mykillernpc.actuator.current_path(): print(i)
-
current_waypoint
()¶ Return the currently active waypoint.
If no waypoint have been added, this function return None.
Returns: Either a None tuple or the current waypoint. Return type: A None tuple or a tuple of integer. Example:
(row,column) = pf.current_waypoint() pf.set_destination(row,column)
-
find_path
()¶ Find a path to the destination.
Destination (PathFinder.destination) has to be set beforehand. This method implements a Breadth First Search algorithm (Wikipedia) to find the shortest path to destination.
Example:
mykillernpc.actuator = PathFinder( game=mygame, actuated_object=mykillernpc ) mykillernpc.actuator.set_destination( mygame.player.pos[0], mygame.player.pos[1] ) mykillernpc.actuator.find_path()
Warning
PathFinder.destination is a tuple! Please use PathFinder.set_destination(x,y) to avoid problems.
-
next_action
()¶ That method needs to be implemented by all behavioral actuators or a NotImplementedError exception will be raised.
Raises: NotImplementedError
-
next_move
()¶ This method return the next move calculated by this actuator.
In the case of this PathFinder actuator, next move does the following:
- If the destination is not set return NO_DIR (see
Constants
) - If the destination is set, but the path is empty and actuated object’s position is different from destination: callfind_path()
- Look at the current waypoint, if the actuated object is not at that position return a direction from the
Constants
module. The direction is calculated from the difference betwen actuated object’s position and waypoint’s position. - If the actuated object is at the waypoint position, then call next_waypoint(), set the destination and return a direction. In this case, also call
find_path()
. - In any case, if there is no more waypoints in the path this method returns NO_DIR (see
Constants
)
Example:
seeker = NPC(model=Sprites.SKULL) seeker.actuator = PathFinder(game=mygame,actuated_object=seeker) while True: seeker.actuator.set_destination(mygame.player.pos[0],mygame.player.pos[1]) # next_move() will call find_path() for us. next_move = seeker.actuator.next_move() if next_move == Constants.NO_DIR: seeker.actuator.set_destination(mygame.player.pos[0],mygame.player.pos[1]) else: mygame.current_board().move(seeker,next_move,1)
- If the destination is not set return NO_DIR (see
-
next_waypoint
()¶ Return the next active waypoint.
If no waypoint have been added, this function return None. If there is no more waypoint in the stack:
- if PathFinder.circle_waypoints is True this function reset the waypoints stack and return the first one.
- else, return None.
Returns: Either a None tuple or the next waypoint. Return type: A None tuple or a tuple of integer. Example:
pf.circle_waypoints = True (row,column) = pf.next_waypoint() pf.set_destination(row,column)
-
pause
()¶ Set the actuator state to PAUSED.
Example:
mygame.pause()
-
remove_waypoint
(row, column)¶ Remove a waypoint from the stack.
This method removes the first occurrence of a waypoint in the stack.
If the waypoint cannot be found, it raises a ValueError exception. If the row and column parameters are not int, an HacInvalidTypeException is raised.
Parameters: - row (int) – The “row” part of the waypoint’s coordinate.
- column – The “column” part of the waypoint’s coordinate.
Raises: - HacInvalidTypeException – If any of the parameters is not an int.
- ValueError – If the waypoint is not found in the stack.
Example:
method()
-
set_destination
(row=0, column=0)¶ Set the targeted destination.
Parameters: - row (int) – “row” coordinate on the board grid
- column (int) – “column” coordinate on the board grid
Raises: HacInvalidTypeException – if row or column are not int.
Example:
mykillernpc.actuator.set_destination( mygame.player.pos[0], mygame.player.pos[1] )
-
start
()¶ Set the actuator state to RUNNING.
If the actuator state is not RUNNING, actuators’ next_move() function (and all derivatives) should not return anything.
Example:
mygame.start()
-
stop
()¶ Set the actuator state to STOPPED.
Example:
mygame.stop()