Lua实现A*算法

基于Lua语言实现的A*寻路算法,目前支持四个方向和0(可通过)1(不可通过)的简单寻路,可扩展性好。

local function new_point(x, y, dis, parent)
    local point = {}
    point.x = x
    point.y = y
    point.distance = dis
    point.parent = parent
    return point
end

local function cost_function(point, point_end)
    local gx = point.distance
    local hx = abs(point.x - point_end.x) + abs(point.y - point_end.y)
    return gx + hx
end

local function isEqual_point(point_a, point_b)
    return point_a.x == point_b.x and point_a.y == point_b.y
end

local function reverse_table(reverseTab)
    local tmp = {}
    for i = 1, #reverseTab do
        local key = #reverseTab + 1 - i
        tmp[i] = reverseTab[key]
    end
    return tmp
end

local function get_index(x, y, cols)
    return (x - 1) * cols + y - 1
end

-- local function parse_index(index, cols)
--     local row_index = (index//cols) + 1
--     local col_index = (index%cols) + 1
--     return row_index, col_index
-- end

local direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

function A_star(gragh, point_start, point_end)
    print(debug.traceback())
    local rows = #gragh
    local cols = #gragh[1]
    local point_start_index = get_index(point_start.x, point_start.y, cols)
    local open_list = {[point_start_index] = new_point(point_start.x, point_start.y, 0, nil)}
    local close_list = {}

    while next(open_list) do
        local neareast_point_index, neareast_point = next(open_list)
        local neareast_dis = cost_function(neareast_point, point_end)
        for index, cur_point in pairs(open_list) do
            local dis = cost_function(cur_point, point_end)
            if dis < neareast_dis then
                neareast_dis = dis
                neareast_point = cur_point
                neareast_point_index = index
            end
        end
        -- print("handled point: ", neareast_point.x, neareast_point.y)

        if isEqual_point(neareast_point, point_end) then
            local path = {}
            local point = neareast_point
            while point ~= nil do
                table.insert(path, point)
                point = point.parent
            end
            path = reverse_table(path)
            return path
        end

        for _, dir in ipairs(direction) do
            local new_x = neareast_point.x + dir[1]
            local new_y = neareast_point.y + dir[2]
            if new_x >= 1 and new_x <= rows and new_y >= 1 and new_y <= cols then
                local new_point_index = get_index(new_x, new_y, cols)
                if not close_list[new_point_index] and gragh[new_x][new_y] == 0 then
                    local new_dis = neareast_point.distance + 1
                    if open_list[new_point_index] then
                        if new_dis < open_list[new_point_index].distance then
                            open_list[new_point_index].distance = new_dis
                            open_list[new_point_index].parent = neareast_point
                        end
                    else
                        open_list[new_point_index] = new_point(new_x, new_y, new_dis, neareast_point)
                    end
                end
            end
        end

        open_list[neareast_point_index] = nil
        close_list[neareast_point_index] = neareast_point
    end
end

local path = A_star(
    {{0, 0, 1, 1, 0, 0},
     {0, 0, 1, 1, 1, 0},
     {0, 0, 1, 0, 0, 0},
     {0, 0, 1, 0, 0, 0},
     {1, 0, 0, 0, 0, 0}},
    new_point(1, 1),
    new_point(1, 5))

for _, point in ipairs(path) do
    print(point.x, point.y)
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值