refactor: hashing, offsetNeighbors #2468
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds a few useful functions to
const.ts
:hashOffsetCoords
andoffsetNeighbors
. It also puts the hashing function to use inpointFacade
.Hash details
I had previously implemented a private hashing function in
pointFacade
. But I thought it could be useful elsewhere, so I moved it toconst.ts
.An example useful case: sets. Sets are fast and practical. But they don't work with our
Hex
representation out of the box:That's because objects in sets are compared by reference. However, if we hash the
Hex
to a value, it can be compared to other hashed values in the set:I also refactored the hash function. Previously, it was
{x:number, y:number} => string
. Now it's{x:number, y:number} => number
.Note
The hashing function uses 16 bits for x, 16 bits for y. (Bitwise operations in JS are limited to 32 bits.) That gives us a max hex x of 65535, and a max hex y of 65535. That should be more than enough – the current maps have a max x of 15 and max y of 8 – but I thought it was worth pointing out, as it's a hard limit.
Fwiw, this happens to be the same hashing scheme that Wesnoth uses for locations.