#help with a* pathfinding for hexagon tiles

1 messages · Page 1 of 1 (latest)

ruby sand
#
local function astar(from,to)
    local active = from
    local lowest_score = 9999
    local neighbors = {}
    local dots = {}

    repeat
        neighbors = workspace:GetPartBoundsInBox(active.CFrame,Vector3.new(6, 1, 6)) -- a little bigger than a hexagon
        for i,v in pairs(neighbors) do
            if v.Name == "region" and v ~= from then -- mustnt be "mountain"
                local score = (to.Position-v.Position).Magnitude
                if score<lowest_score then
                    active = v
                    lowest_score = score
                    table.insert(dots,active)
                end
            end
        end
        if #dots > 0 then -- checking if any valid region was found
            active = dots[#dots]
        end

        if lowest_score == 9999 and active ~= to then -- preventing infinite loop
            print("No valid path found")
            return {}
        end

    until active == to
    return dots
end

this gives the path, but its really shaky and it isn't the shortest path. can anyone help me, please?

normal finch
#

step 1: use a hexagon grid, not a spatial query