wyvern.lib.properties
Interface CustomAI


public interface CustomAI

Interface for monsters that want to participate in the AI decision process. The interface allows monsters to determine various parameters that the AI uses for choosing what to do.

Version:
1.0, Mar 1, 2001
Author:
Steve Yegge

Method Summary
 int getLocationHeuristic(GameMap map, int x, int y, Commandable[] targets)
          Returns how badly the monster wants to avoid this square when chasing a player.
 

Method Detail

getLocationHeuristic

public int getLocationHeuristic(GameMap map,
                                int x,
                                int y,
                                Commandable[] targets)
Returns how badly the monster wants to avoid this square when chasing a player. The AI constructs a path to a target by looking at squares that the monster could traverse. Each square gets a "cost" assigned to it, where a higher cost means the monster wants to avoid the square more. The default cost is 1, meaning there's nothing stopping the monster from entering this square. If the monster implements the CustomAI interface, the AI will ask the monster to assign a cost to the passed square.

It's important to understand the scale that we use, so you can choose a cost that makes sense. The cost you return here is added to a "heuristic" value, which is the square of the distance between this location and the target nearest this location. So if the nearest target is 4 squares away, the heuristic value is 16. You'll want to assign a cost that makes your monster behave in some predictable but intelligent manner. For instance, if you want your monster to avoid going through fire, you could assign a cost of 20 if the terrain is lava or if it has a firewall in it. The monster will go through the fire as a last resort, but a cost of 20-30 will make it walk around the fire if possible. If you assign a cost of 100 or more, the monster is very unlikely to move into this square, and a cost of 1000 would pretty much guarantee it'll never move here. You can assign negative numbers if you want the monster to give preference to this square.

You should try to minimize the amount of computation that you do in your implementation of this method, since it's called every time your monster is about to move, and it's easy to want to try to do things that are expensive. It's OK to check the values of various properties for the objects in the square, but you should avoid searching areas (more than one square) or doing lots of other processing in this method, since you'll be taking away from the CPU available to other monsters.

This method is called on an "AI Thread", which does NOT have the "map exclusive" when it's running. You should put your entire method body in a try/catch block, since it's common (and expected) for you to get exceptions when you're iterating over objects in the map data structures and they change out from under you on another thread. This is great because it means AIs can take all the time they want to do their thinking without locking the game up, but it increases the amount of exception handling you have to do. If you get an exception, you should probably exit (returning a reasonable heuristic) and let the method get called again, rather than retrying over and over.

Parameters:
map - the map to move to (usually the same as the monster's map).
x - proposed map x location
y - proposed map y location
targets - a list of possible target players (or monsters) in the vicinity.
Returns:
a cost for the location.