Untitled conversation
- User
-- Dijkstra's Algorithm in 3D implemented in LuaJIT for MoonLoader SA-MP -- This implementation uses the LuaJIT FFI library for performance optimization local ffi = require("ffi") -- Define a simple priority queue using a binary heap local PriorityQueue = {} PriorityQueue.__index = PriorityQueue function PriorityQueue.new() return setmetatable({ heap = {} }, PriorityQueue) end function PriorityQueue:push(priority, item) local heap = self.heap local node = { priority = priority, item = item } table.insert(heap, node) self:_siftUp(#heap) end function PriorityQueue:pop() local heap = self.heap if #heap == 0 then return nil end local root = heap[1] local last = table.remove(heap) if #heap > 0 then heap[1] = last self:_siftDown(1) end return root.priority, root.item end function PriorityQueue:_siftUp(idx) local heap = self.heap while idx > 1 do local parent = math.floor(idx / 2) if heap[idx].priority >= heap[parent].priority then break end heap[idx], heap[parent] = heap[parent], heap[idx] idx = parent end end function PriorityQueue:_siftDown(idx) local heap = self.heap local size = #heap while true do local left = idx * 2 local right = left + 1 local smallest = idx if left <= size and heap[left].priority < heap[smallest].priority then smallest = left end if right <= size and heap[right].priority < heap[smallest].priority then smallest = right end if smallest == idx then break end heap[idx], heap[smallest] = heap[smallest], heap[idx] idx = smallest end end function PriorityQueue:isEmpty() return #self.heap == 0 end -- Implement Dijkstra's algorithm in 3D function dijkstra3D(grid, gridSize, start, goal) --[[ grid: a 3D array of traversal costs/accessibility grid[z][y][x] gives the cost to move to cell (x, y, z) Cells with cost == math.huge are considered impassable gridSize: a table { Nx, Ny, Nz }, grid dimensions start: { x, y, z } starting coordinates goal: { x, y, z } ending coordinates Returns: distances: a 1D FFI array of minimum distances from start path: table of coordinates representing the shortest path ]] -- Initialize local Nx, Ny, Nz = gridSize[1], gridSize[2], gridSize[3] local totalSize = Nx * Ny * Nz local distances = ffi.new("double[?]", totalSize) local visited = ffi.new("bool[?]", totalSize) local cameFrom = {} for i = 0, totalSize - 1 do distances[i] = math.huge visited[i] = false end -- Function to map 3D coordinates to 1D index local function index(x, y, z) return x + y * Nx + z * Nx * Ny end -- Define possible movements (6-directional) local directions = { {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1} } -- Start position local sx, sy, sz = start[1], start[2], start[3] local ex, ey, ez = goal[1], goal[2], goal[3] -- Priority queue: (distance, {x, y, z}) local frontier = PriorityQueue.new() frontier:push(0, { sx, sy, sz }) local startIdx = index(sx, sy, sz) distances[startIdx] = 0 cameFrom[startIdx] = nil -- Main loop while not frontier:isEmpty() do local currentDist, current = frontier:pop() local x, y, z = current[1], current[2], current[3] local idx = index(x, y, z) if not visited[idx] then visited[idx] = true -- Goal check if x == ex and y == ey and z == ez then break end -- Explore neighbors for _, dir in ipairs(directions) do local nx, ny, nz = x + dir[1], y + dir[2], z + dir[3] if nx >= 0 and nx < Nx and ny >= 0 and ny < Ny and nz >= 0 and nz < Nz then local nIdx = index(nx, ny, nz) local cost = grid[nz][ny][nx] if not visited[nIdx] and cost ~= math.huge then local newDist = currentDist + cost if newDist < distances[nIdx] then distances[nIdx] = newDist cameFrom[nIdx] = { x, y, z } frontier:push(newDist, { nx, ny, nz }) end end end end end end -- Reconstruct path local path = {} local goalIdx = index(ex, ey, ez) if distances[goalIdx] ~= math.huge then local x, y, z = ex, ey, ez while not (x == sx and y == sy and z == sz) do table.insert(path, 1, { x, y, z }) -- Insert at the beginning local prev = cameFrom[index(x, y, z)] if not prev then break end x, y, z = prev[1], prev[2], prev[3] end table.insert(path, 1, { sx, sy, sz }) else path = nil -- No path found end return distances, path end -- Example usage function main() while not isSampAvailable() do wait(0) end local Nx, Ny, Nz = 10, 10, 5 -- Define grid size local grid = {} for z = 0, Nz - 1 do grid[z] = {} for y = 0, Ny - 1 do grid[z][y] = {} for x = 0, Nx - 1 do grid[z][y][x] = 1 -- Default cost end end end grid[2][5][5] = math.huge -- Create obstacles by setting cost to math.huge -- Impassable cell at (5, 5, 2) local start = {0, 0, 0} local goal = {9, 9, 4} local _, path = dijkstra3D(grid, {Nx, Ny, Nz}, start, goal) if path then print("Path found:") for i, pos in ipairs(path) do print(string.format("Step %d: (%d, %d, %d)", i, pos[1], pos[2], pos[3])) end else print("No path found.") end while true do wait(0) -- Additional logic can be added here to utilize the path within the game end end - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.