I work on a hexagon map based browser game. I have this: http://www.dark-project.cz/wesnoth/map-view/1 and now I have a problem I want to mark the fields at which the unit can go (it has limited movement).
If the movement is 1 there isn’t any problem but if it’s higher it doesn’t work right (try it).
I work with a coordinates system http://www.dark-project.cz/wesnoth/coor.png
My actual js is here: http://www.dark-project.cz/wesnoth/js/map/base.js
In other question ( Movement algorithm on a hexagon map) @unkulunkulu recommend me to use the BFS algorithm. But I have no experience with algorythms like this and its implementing into javascript and then its use on hexagon map. He say that the BFS algorithm is better for that because I can easy expand it later (add some obstacles etc.).
If you have some link to a javascript tutorial about this or something similar it will be amazing.
The algotithm being suggested is roughly this:
Take start square and mark it as distance 0. Add it to a list of processed hexes.
Find all valid adjacent hexes (ie ones you can move into).
Mark all these squares as distance 1. Add them to a list of processed hexes.
Find all valid hexes adjacent to the distance 1 hexesthat are not in the processed hexes list.
Mark all these squares as distance 2. Add them to a list of processed hexes.
…
X. Find all valid hexes adjacent to the distance N hexesthat are not in the processed hexes list.
X+1. Mark all these squares as distance N+1. Add them to a list of processed hexes.
The idea of the breadth first searching is that you iterate all the possibilities out from the inside so you will always find the shortest distance to each hex.
After comments from Unkulunkulu I have rethought the best implementation for this (and may change again after further input). You should have a list of processed hexes as above and also a queue of hexes awaiting processing. The unprocessed hexes list starts with just the start hex with distance 0 and you go through this list until it is exhausted. When processing a hex all new found unprocessed hexes get added to this list with their distance in. So once you have processed the distance 0 hex you should have added all the distance 1 hexes. They get processed and by the time they are done you will have all your distance 2 hexes in there…
For what its worth I think my original suggestion of recursion fails from an efficiency point of view and depending on the distance you want to go a potential stack overflow issue. 🙂