In my latest version of the module for “Carrier Battles: Philippine Sea” I make some hex calculations that sometimes do not give the right result. Maybe somebody could advise me a better algorithm.
The situation is this: A Japanese is sent from an origin hex to a target hex some distance away. The target detects the raid when it is 0-5 hexes away from the target. I need to calculate the number of a hex lies the interception distance away from the target hex in a straight line. I have the distance between the origin and the target through the Range() function of attached counters.
I split the hex numbers into a) the first two digits, and b) the last two digits and treat the a and b parts separately. My algorithm goes like this: subtract target from origin, divide by distance, and multiply by Intercept Range, then round. My BeanShell code for the a part is this:
{((Math.abs(((RaidOrigin_a-RaidTarget_a)*100)/Distance*InterceptRange)+50)*((RaidOrigin_a-RaidTarget_a<0)?-1:1)/100)+RaidTarget_a}
The code for the b part is similar.
It works for most situations, but sometimes it gives a wrong result because the rounding of the a and b parts conspire to give a result that is off by one hex.
I can best illustrate this with an example.
Here the origin is Guam at 1605, the target is TF58 at 1410, and the interception range is 4, so the raid should be at 1506 or 1607, but it ended up at 1507 which is only 3 hexes away from the target.
.Perhaps there already exists a function that gives the straight path between two hexes so that one can count until the right hex is found for the raid?
We can ignore the situation where the interception is further away than the distance between origin and target because in that case the raid is placed in the origin hex.
