PDA

View Full Version : Calculating FG distance



jammaster
October 29th, 2021, 14:54
Hi! I am trying to write an extension that needs the distance between two actors, same as the one calculated by Fantast Grounds, when targetting or moving (see arrows in 49711). However, I have a really hard time to wrap my head around the distance they have implemented.
I have not found any functions that would do the calculation for me, so I'm afraid I will have to do it myself, but I wanted to ask here first, because what I am going to do is complicated, and it would be a shame to reinvent the wheel.

I am planning on using the Bresenhams algorithm(https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm), possible in combination with the 1-2-1-2 rule (which states that diagonal movement costs 1 square of movement normally, and 2 squares every second diagonal movement).

So do any of you know:

Whether or not this is already provided as a function somewhere?
Is there a defined (by FG team) algorithm for doing this?
Does the algorithm from above makes sense to implement?

Moon Wizard
October 29th, 2021, 21:11
That’s already supported using the settings in the 3.5E/PFRPG rulesets, as well as a house rule in there 5E ruleset.

There is an imagecontrol function to return the distance between two tokens (see Developer Guide APIs). It calculates distance using center of closest grid square of source token to center of closest square of target token.

Regards,
JPG

jammaster
October 30th, 2021, 14:24
Hi! Thank you for the answer, it looks very promising! However after trying around, I can not get it to work. The errors say that the function doesn't exist and I cant find in the documentation how to access it. I have tried:

ImageControl.onMeasurePointer
srcImage.onMeasurePointer (where srcImage is what I got from calling ImageManager.getImageControl)


The docs also state that "Distances will be automatically calculated for pointers and token paths based on the values specified in the distance tag, unless onMeasurePointer or onMeasureVector are defined (respectively).", but I don't really know what that means. When are they defined and when are they not?
I am writing the plugin for 5E, however I do not see any house rule, that would enable extensions to use this function. I feel like I dont fully understand what you mean with the house rule in your response.

Moon Wizard
October 30th, 2021, 15:44
onMeasurePointer and onMeasureVector are event functions that are called by client when we need to draw a distance on a token path or user pointer object. If they don't exist, then the existing settings are used. They are not "function calls" to get information, but events that can be overridden to replace internal client distance measurements.

If you need to get distances between tokens in your script, you would use imagecontrol.getDistanceBetween function call.
https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996645070/imagecontrol

Again, the 1-2 (i.e. 5-15) grid measurement used in Pathfinder and in 5E optional rules is already natively supported, and does not need any custom scripts. Applying the correct distance settings are all that is needed.

From 3.5E/Pathfinder:


<distance>
<base>5</base>
<diagmult>1.5</diagmult>
<suffix>'</suffix>
</distance>


Regards,
JPG

jammaster
October 30th, 2021, 16:03
Oooh I see! Also, I now see that the docs I've been using (https://www.fantasygrounds.com/refdoc/imagecontrol.xcp), which is terribly outdated. Thank you for a new refdoc! I will try it and come back with results.

jammaster
October 30th, 2021, 18:27
That works like a charm, thank you! However, what concerns me is the [FGU] in the function description. I assume its not supported in the non-unity version? Why? I remember that FG was capable of doing the same calculation.

Moon Wizard
October 30th, 2021, 23:19
At this point, we are only supporting updates to FGU as of August. I don’t remember without researching the code whether it works at all in FGC. Given that FGC is being phased out, it shouldn’t make much difference. Just build for FGU.

Regards,
JPG