pf2e icon indicating copy to clipboard operation
pf2e copied to clipboard

RulerPF2e - Add Hexagonal Segment Highlighting

Open 7H3LaughingMan opened this issue 1 year ago • 2 comments

This adds "fat ruler" highlighting to the ruler for hexagonal grids. It does this in a universal way for both square grids and hexagonal grids.

Using grid offsets on a hexagonal grid leads to some wonky behaviour if you are only using them to get neighboring hexes. For example, the hex to the top-left might be to the top-left in offset notation but depending on the grid layout and the current row/hex it might be to the top or the left in offset notation.

The proper way would probably use offset coordinates for square grids, and cube coordinates for hexagonal grids. However, a third option is to just use x/y offsets from the origin to represent a token's shape which works on both types of grids.

This leads to another wonky behaviour with hexagonal grids. On a square grid if a point is on the vertex touching four different squares then Foundry is always going to pick the same grid when you get the offset coordinates of that point. However, on a hexagonal grid this is not always the case when the point is on the vertex touching three different hexes thanks to floating point math. So depending on the layout of the hexagonal grid and if you are on an odd or even row/column it will pick different hexes when you get the offset/cube coordinates of that point. This leads to some wonky behaviour when you ask Foundry to get a direct path between the two points, it can pick the hex to the top-left of the start point and the hex to the top-right of the end point which means the token appears to be going to far.

To resolve that problem we instead standardize which grid we are going to use for the direct path, if the token's center is in-between squares/hexes it always using the grid that is to the top-left of both the start and end point when getting the direct path for a segment. Then we can reverse this offset to determine where a token's center is at any point along the direct path, then use the offsets' from the token's center we get from the token's shape to get all the squares/hexes we need to highlight.

Below is a sample from moving a large creature on a hexagonal map.

Foundry_Virtual_Tabletop_VmE6gGwnKK.webm

Below is a sample from moving a large creature on a square map, showing that it still behaves the same.

Foundry_Virtual_Tabletop_x9NUxKYfpl.webm

7H3LaughingMan avatar Aug 09 '24 00:08 7H3LaughingMan

I think I'd rather stick to offsets but switch over to cube coordinates for hex grids.

stwlam avatar Aug 09 '24 17:08 stwlam

One thing to note, is that using point offsets from the token's center is always going to be accurate in representing a token's occupied space on the grid. Any changes to the grid requires the whole scene to be torn down and built back up, which results in a new ruler instance. It can also be done in such a way the doesn't require the requires doubling up on the logic since using cube coordinates also requires a big of extra logic to always ensure that the same hex is designated as the (0, 0, 0) for a token's shape.

If you still want to have it done in grid offset/cube coordinates that is fine, is that something you want to work on or is it fine for me to close this out and re-do it.

7H3LaughingMan avatar Aug 09 '24 18:08 7H3LaughingMan