Using GoalMaps in RogueSharp

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.

GoalMapSample

GoalMap Sample

Sample GoalMap Application

The code for the Sample GoalMap application is on Bitbucket:

https://bitbucket.org/FaronBracy/roguesharprlnetsamples/src/0cbcd866875e64f6456d5fe08c86602d5f9591b9/?at=GoalMap

Commands

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

Definitions

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.

GoalMapSampleApp

GoalMap Demo Application

Code Sample

// 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 );
Advertisements

2 thoughts on “Using GoalMaps in RogueSharp

  1. Kyle Berezin

    Can this be used as a general way to have weighted cells ie rubble/dificult terrain? I want my player to path to the nearest wall to his target if there is no path, and I could do that if I gave walls a very high weight.

    Reply
    1. Faron Bracy Post author

      Hello and thank you for visting.

      The RogueSharp implementation currently weights all floor tiles the same and all wall tiles the same. It wouldn’t be hard to update or extend the GoalMap.cs class to handle different weights for different terrain though. The current implementation is here -> https://bitbucket.org/FaronBracy/roguesharp/src/f682040a21b79cc1e91251505cb5ad5d0dac0cf4/RogueSharp/GoalMap.cs

      In hindsight the AddObstacles() method should have accepted a point and a weight as parameters. Right now it treats all obstacles as having the same weight as walls. I added the request to the things to add in RogueSharp v4. Until then I think you could try adding it to the source and see what kind of results you get. There might be a better way to do it that I’m not thinking about right now too.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s