• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[TFS 1.3] Generate Maze Function

Apollos

Dude who does stuff
Joined
Apr 22, 2009
Messages
829
Solutions
123
Reaction score
655
Location
United States
This function uses recursive backtracking to create randomized mazes within a specified range. The maze is quite complex at times so very large areas are not necessarily needed. It may be used however you wish (talkaction, action, globalevent, etc).

Lua:
doGenerateMaze(fromPos, toPos, itemid[, duration = -1])

Parameters:
  • fromPos: Used for isInRange and should be most top left position of maze. (See image for more information)
  • toPos: Used for isInRange and should be most bottom right position of maze. (See image for more information)
  • itemid: Item id of the wall used within maze. (Example: 2767(bush) is used in image)
  • duration: How long (in milliseconds) until maze is reset to original state.
Things to note:
  • You must map maze in format shown in image, you may increase size but always keep it this way.
  • You may leave a few bushes missing in outside parameter of maze, for entrance and exit.
  • Make sure not to have duplicate wall items stacked on each other, otherwise it will only remove one wall and leave the other.
  • Increasing size of maze does have it's limits, at some point it will begin to cause lag.
  • If you do not want the maze to reset then either leave duration parameter blank or set it to -1.
  • Do not try to activate from zbizu's Luascript channel if you have duration set, there is issues with addEvent in his system and may cause a crash.
35563


data/lib/compat/compat.lua:
Lua:
function doGenerateMaze(fromPos, toPos, itemid, duration)
    local startPos, stack = Position(fromPos.x + 1, fromPos.y + 1, fromPos.z), {}
    local directions = {NORTH, SOUTH, EAST, WEST}
    duration = duration or -1
    function doMoveCell(currentPos)
        local cell_found = true
        while cell_found do
            stack[#stack + 1] = currentPos.x .. currentPos.y
            local cells = {}
            for i = 1, #directions do
                local cell_pos, wall_pos = Position(currentPos), Position(currentPos)
                cell_pos:getNextPosition(directions[i], 2)
                wall_pos:getNextPosition(directions[i], 1)
                if cell_pos:isInRange(fromPos, toPos) and not isInArray(stack, cell_pos.x .. cell_pos.y) then
                    cells[#cells + 1] = {cell = cell_pos, wall = wall_pos}
                end
            end
            if #cells > 0 then
                local random = math.random(#cells)
                local wall = Tile(cells[random].wall):getItemById(itemid)
                if wall then
                    wall:remove()
                    if duration > 0 then
                        addEvent(Game.createItem, duration, itemid, 1, cells[random].wall)
                    end
                end
                doMoveCell(cells[random].cell)
            else
                cell_found = false
            end
        end
    end
    doMoveCell(startPos)
end


Example Preview:
Lua:
function onThink(interval)
    doGenerateMaze(Position(975, 879, 7), Position(1023, 905, 7), 2767, interval / 2)
    return true
end

35567
 
Last edited:
Back
Top