0
\$\begingroup\$

I'm making a turn-based strategy game using Java and LibGDX. Now I want to code the AI.

I haven't written the AI code yet. I've simply designed it. The AI will have two components, one focused in tactics and resource management (create troops, determine who have strategical advantage, detect important objectives, etc) and a individual component, focused in assign the work to each unit, examine its possibilites and move the unit.

Now I'm facing an important problem. The map where the action take place is a grid-based map. Each terrain has different movement cost. I read about pathfinding and I think A* is a very good option to determine a good route between two points. However, imagine I have an unit with movement = 5 (i.e, it can move 5 tiles of movement cost = 1). My tactical AI has found an objective at a distance d = 20 tiles (Manhattan distance) from my unit.

My problem is the following: the unit won't be able to reach the objective in one turn. So the AI will have to store a list of position and execute them in various turns. I don't know how to solve this.

PS. In my unit code, I have a list called selectionMarks which stores all the possible places where the unit can go in this turn. This places are calculated recursively using a getSelectionMarks function.

\$\endgroup\$
4
\$\begingroup\$

Your pathfinding should return a list of way points to reach your objective for each unit. Since you are making a turn based game, I am assuming that the unit movement is like a chess piece. It is either on "this" tile or "that tile, and not in between. If this is the case you might end up with the scenario where a units movements runs out in the middle of the next tile. For example, if you have a unit with a movement of 5 and it needs to travel across 3 tiles each with a movement cost of 2. Clearly the unit has traveled across the first 2 tiles, but what about the third tile? Is the unit stopped on the third tile with 1 point left to go, or since the unit doesn't have enough movement left to traverse the third tile, it is stopped on the second tile and the remaining movement point goes unused. Now what happens if the units movement is 4 and it has to travel across the same 3 tiles. Where does it sit at the end of its turn? Is it on the second tile, or the third?

These are game design decisions. Does a unit have to move the full cost to be considered on that tile? Or is the unit considered on a tile if it has spent any movement points in that tile. Also, are all movement points spent each turn, or if the unit can not spend the full cost of movement on the next tile, then the remaining movement points are dumped and the turn ends?

If you are allowing a unit to spend all movement points, you can have a variable like traveledSoFar which holds the movement units spent on a tile at the end of a turn.

So, back to your question, your pathfinding should give you a list of way points, in your case, one tile, after another, leading to the objective. At the start, pull the first way point, after traveling through it, pull the next way point from the list. Do this until you have exhausted your movement points for that unit. Then on the next turn resume where you left off.

\$\endgroup\$
  • \$\begingroup\$ I like the traveledSoFar idea. I'm working right now in the problem and I'm trying to find some solutions to it. That can be an useful variable. \$\endgroup\$ – VictorSeven Aug 25 '14 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.