My game has 3D environment which is partitioned in 2d cells(nodes). I have implemented A* for path finding.
Now I am just breaking my head to how to implement the algorithm to move the hero to the nearest attackable range node/point when the enemy is out of attack able range.
Which means, it should satisfy two conditions
- Target whithin attackable range
- No obstacles in between the target and attacker
See below picture (pardon for my bad drawings)
- Brown boxes are obstacles
- Attacker/hero is in the cell highlighted with Turquoise color dot, the circle is the radius/size of the model.
- Target is the red dotted color cell, surrounded with red color circle to indicate the size of the model
- Green circle is the radius where hero has to move to some where inside that location to attack (its the radius of hero's attack range)
- Blue dots are the A* path to reach from hero position to target position
When the player issues attack command, hero should move to the nearest attack able node and start attacking.
Finally I thought of below algorithm
- Calculate radius to scan - This is the sum of attacker radius + attacker weapon radius + target radius
- Get nodes fall within the radius (from step 1) from targets position
- Find path from attacker to target using A*
- Keep walking in the path (from step 3)
- check if current node is in the scanned nodes (from step 1)
- If true,
- Check that no obstacle are in between
- If true, attack, if false keep walking (repeat step 4)
If we put together the algorithm and the figure1
The A* gets the path of 9 node to reach the target After moving 5 steps, it enters the attackable range, since there is obstacle between the target, it take two more steps where it finds no obstacles and stats attacking.
I am not sure if this is the optimized method to achieve this but I am not getting alternative solution.
Very particular to the steps 2-5 which looks little weird and process intensive to me.
I am using actionscript 3 and Flare3D (Stage3D API)