RogueSharp has had GoalMaps since version 1 however they never performed as well as I had hoped so I didn’t promote the feature. As of version 3.0.0-pre the feature has been cleaned up and I’m now ready to present a sample application of their use. The GoalMap feature was originally inspired by The Incredible Power of Dijkstra Maps post on RogueBasin.
Sample GoalMap Application
The code for the Sample GoalMap application is on Bitbucket:
Left Click – Set the start location for the Goal Map Calculation
Right Click – Toggle a Goal on/off
O – Toggle obstacle on/off
A – Toggle between Goal avoidance and Goal seeking
C – Clear all Goals, Paths and Obstacles
GoalMap – The GoalMap class is used in RogueSharp for assigning weights to Cells on the Map which can then be used for finding paths or building desire-driven AI
Goal – The Goals in a GoalMap represent weighted locations on the Map that will be used when calculating paths for either seeking or avoiding Goals. The smaller the weight the more desirable the location will be to either seek or avoid. When finding paths both the distance to the Goal and the weight of the Goal will be taken into account.
Obstacle – An Obstacle is a location that a Path must not pass through when calculating paths with GoalMaps. Examples of possible obstacles could be an ally, an enemy, water, or anything that we don’t want to pass through. By default any Map Cells that are not marked as walkable will be Obstalces. For instance, there will be no need to set Obstacles for each wall in the Map.
// Create a new map that only has a wall around the border var map = Map.Create( new BorderOnlyMapCreationStrategy <Map>( 50, 50 ) ); // Create a new GoalMap for the map var goalMap = new GoalMap( map ); // Add a Goal at X=10, Y=25 with a weight of 10 // Lower weights are more desirable goalMap.AddGoal( 10, 25, 10 ); // Add a second Goal at X=40, Y=15 with a weight of 10 goalMap.AddGoal( 40, 15, 10 ); // Add an obstacle at X=1, Y=2 // An obstacle is something that a Path must not go through // Examples: an ally, an enemy, water, etc. goalMap.AddObstacle( 1, 2 ); // Find a "best" path to a Goal based on Goal weight and distance to the Goal // Start the path from X=1, Y=1 // The path will not pass through obstacles Path path = goalMap.FindPath( 1, 1 ); // Find a "best" path away from or avoiding Goals based on Goal weight and distance // Start the path from X=20, Y=20 // The path will not pass through obstacles Path pathAvoiding = goalMap.FindPathAvoidingGoals( 20, 20 );