[SUPPORT] Advanced Turn Based Tile Toolkit

Thanks for the detailed example, @Selentic. You are correct in the cause of the problem The unit first calculates the quickest path to the tank, ignoring friendly units. This path includes mostly just tiles in the bottom row of tiles in your screenshot and not the ones above them. On this path, the closest possible tile is the one the unit ends up walking to. To fix this you can have the AI evaluate every possible tile it can move to and compare it to a criteria. In this case, after the AI unit has chosen its target you can loop through the Index Can Move To Array and for each index check the distance in tiles between that tile and the tile index of the target using the Get Distance Between Tiles function. The one that returns the lowest value is the index you set to be the target index of the AI unit’s move. You still won’t get perfect results 100% of the time with this solution (it does not take into account obstacles which might make some tiles worse choices), but in most cases it should work fine.

Edit: Actually, scrap that. I gave it some more thought and there will be many cases where this new solution will give you a worse outcome. I have another idea that should work better. After pathfinding store the CanMoveToArray in a new, separate array. Then run a new pathfinding from the tile index of the target unit (the tank). Set the move range to equal the cost of getting all the way to the tank minus the cost of getting to the tile found using the Find Closest Reachable Tile (Pass Through Friendlies) function. In this case that would be 11 (cost of getting to tank) - 3 (cost of getting to the tile the unit moves to in the bottom screenshot). Then, loop through the IndexCanMoveToArray and compare each index to the old CanMoveTo array you stored previously. Find the tile with the lowest cost and you have your target.

This is a pretty heavy duty way of doing this, but it should at least get the best tile every time. If I think of something more elegant I’ll let you know.