> Specifically, I would like when the player clicks on a unit, all the
> hexes that the unit can move to are highlighted, so the player can just
> then click on the desired destination hex. My design includes the
> concept of movement points and terrain costs, so it's not as simple as
> counting out a given number of hexes.
I infer that your terrain is irregular, and that the cost of moving to
and through each hex counts. Here's a brute-force approach:
For every position that is adjacent to your starting position, figure
out the cost of moving to that position. For each position that you
can "afford", add it to a list of candidate moves, along with its cost
and how you got there. (That is, define a class that consists of a
position, a cost, and a sequence of directions, and maintain an array
of instances of that class.)
Then, for each position that you can afford to move to, decrement your
"budget" by its cost, and check every position that is adjacent to it,
etc. Stop when you can no longer afford to move to any adjacent
position.
You will be left with a list of potential moves and costs of executing
each move. Many of these moves will be to the same position -- one
will be the least-cost way, so you can discard the rest. Sort your
array by position, then run through it and discard the duplicate
positions, favoring the least-cost one.
Note that this algorithm doesn't depend on any particular board
geometry -- hex, square, or hyper-web. You just need to be able to
enumerate all positions that are "next to" any given positionm, and
the meaning of "next to" is up to you.
This can probably be made much more efficient by various clever
techniques that I don't know. But it might get you started in case
nobody posts anything else today. :)
lj
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
|