SHARE
TWEET

Maze Huje

a guest Mar 19th, 2017 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------noncopy----------
  2.  
  3.  
  4. NIGHTMODE = true
  5.  
  6. Maze = {
  7.     seed = nil,
  8.     width = nil,
  9.     height = nil,
  10.     cellSize = nil,
  11.     cellCount = 0,
  12.     cells = {},
  13.     winCell = {x=0, y=0},
  14.     startCell = {x=0, y=0},
  15.     util = {
  16.         getCellId = function(x, y)
  17.             return x + y * Maze.width
  18.         end,
  19.         getCell = function(x, y)
  20.             return Maze.cells[Maze.util.getCellId(x, y)]
  21.         end,
  22.         getCellXY = function (id)
  23.             return {x = id % Maze.width, y = math.floor(id / Maze.width)}
  24.         end,
  25.         toGlobal = function(n)
  26.             return n * Maze.cellSize + Maze.cellSize / 2
  27.         end,
  28.         getNeighbors = function(id)
  29.             local ra = {}
  30.             if (id == nil) then return ra end
  31.             if (id + 1 <= Maze.cellCount and not Maze.util.isRightEdge(id) and not Maze.cells[id + 1].visited) then table.insert(ra, id + 1) end
  32.             if (id - 1 >= 0 and not Maze.util.isLeftEdge(id) and not Maze.cells[id - 1].visited) then table.insert(ra, id - 1) end
  33.             if (id + Maze.width <= Maze.cellCount and not Maze.cells[id + Maze.width].visited) then table.insert(ra, id + Maze.width) end
  34.             if (id - Maze.width >= 0 and not Maze.cells[id - Maze.width].visited) then table.insert(ra, id - Maze.width) end
  35.             return ra
  36.         end,
  37.         isRightEdge = function(id)
  38.             return Maze.util.isLeftEdge(id + 1)
  39.         end,
  40.         isLeftEdge = function(id)
  41.             return id % Maze.width == 0
  42.         end
  43.     },
  44.     init = function(s, c, w, h)
  45.         Maze.seed = s
  46.         Maze.cellSize = c
  47.         Maze.width = w
  48.         Maze.height = h
  49.        
  50.         if (Maze.seed == nil) then Maze.seed = os.time() end
  51.         if (Maze.cellSize == nil) then Maze.cellSize = 50 end
  52.         if (Maze.width == nil) then Maze.width = math.floor(800 / Maze.cellSize) end
  53.         if (Maze.height == nil) then Maze.height = math.floor(400 / Maze.cellSize) end
  54.        
  55.         Maze.cellCount = Maze.width * Maze.height
  56.         math.randomseed(Maze.seed)
  57.     end,
  58.     generate = function()
  59.         local visited = {}
  60.        
  61.         Maze.winCell.x = math.floor(math.random() * Maze.width)
  62.         Maze.winCell.y = math.floor(math.random() * Maze.height)
  63.        
  64.         for i=0,Maze.cellCount do
  65.             Maze.cells[i] = {top = true, bot = true, left = true, right = true, visited = false}
  66.         end
  67.        
  68.         local count = Maze.cellCount
  69.         local operator = {Maze.util.getCellId(Maze.winCell.x, Maze.winCell.y)}
  70.         local overflow = 0
  71.         local endOp = 0
  72.         while count > 0 do
  73.             local neighbors = Maze.util.getNeighbors(operator[#operator])
  74.             local id = nil
  75.            
  76.             if (next(neighbors) ~= nil) then
  77.                 id = neighbors[math.random(#neighbors)]
  78.             end
  79.            
  80.            
  81.             if (id ~= nil and Maze.cells[id].visited) then
  82.                 id = nil
  83.             end
  84.            
  85.             if (id == nil) then
  86.                 table.remove(operator)
  87.                 overflow = overflow + 1
  88.                 if (overflow > Maze.cellCount) then break end
  89.             else
  90.                 overflow = 0
  91.                 local diff = id - operator[#operator]
  92.                
  93.                 if (diff == -1) then
  94.                     Maze.cells[id].right = false
  95.                     Maze.cells[operator[#operator]].left = false
  96.                 elseif (diff == 1) then
  97.                     Maze.cells[id].left = false
  98.                     Maze.cells[operator[#operator]].right = false
  99.                 elseif (diff == 16) then
  100.                     Maze.cells[id].top = false
  101.                     Maze.cells[operator[#operator]].bot = false
  102.                 elseif (diff == -16) then
  103.                     Maze.cells[id].bot = false
  104.                     Maze.cells[operator[#operator]].top = false
  105.                 end
  106.  
  107.                 Maze.cells[id].visited = true
  108.                 table.insert(operator, id)
  109.                 endOp = id
  110.                 count = count - 1
  111.             end
  112.         end
  113.         Maze.startCell = Maze.util.getCellXY(endOp)
  114.     end,
  115.     build = function()
  116.         for i=0,Maze.cellCount do
  117.             local x = Maze.cellSize * (i % Maze.width)
  118.             local y = Maze.cellSize * math.floor(i / Maze.width)
  119.             if (Maze.cells[i].top) then tfm.exec.addPhysicObject(4 * i + 0, x + Maze.cellSize / 2 - 5, y - 5, {type=0, restitution=0.2, friction=1, width=Maze.cellSize + 10, height=10, groundCollision=false}) end
  120.             if (Maze.cells[i].bot) then tfm.exec.addPhysicObject(4 * i + 1, x + Maze.cellSize / 2 - 5, y + Maze.cellSize - 5, {type=0, restitution=0.2, friction=1, width=Maze.cellSize + 10, height=10, groundCollision=false}) end
  121.             if (Maze.cells[i].left) then tfm.exec.addPhysicObject(4 * i + 2, x - 5, y + Maze.cellSize / 2, {type=0, restitution=0.2, friction=1, width=10, height=Maze.cellSize, groundCollision=false}) end
  122.             if (Maze.cells[i].right) then tfm.exec.addPhysicObject(4 * i + 3, x + Maze.cellSize - 5, y + Maze.cellSize / 2, {type=0, restitution=0.2, friction=1, width=10, height=Maze.cellSize, groundCollision=false}) end
  123.         end
  124.     end
  125. }
  126.  
  127. function newMap()
  128.     Maze.generate()
  129.     tfm.exec.newGame('<C><P ' .. NIGHTMODE .. ' /><Z><S><S P="0,0,0.3,0.2,0,0,0,0" L="800" o="6a7495" X="400" c="3" Y="-50" T="12" H="100" /><S H="400" L="100" o="6a7495" X="850" c="3" Y="200" T="12" P="0,0,0.3,0.2,0,0,0,0" /><S H="400" L="100" o="6a7495" X="-50" c="3" Y="200" T="12" P="0,0,0.3,0.2,0,0,0,0" /><S H="100" L="800" o="6a7495" X="400" c="3" Y="450" T="12" P="0,0,0.3,0.2,0,0,0,0" /><S L="45" o="6a7495" H="164" X="17" Y="-103" T="12" P="0,0,0,0,0,0,0,0" /><S P="0,0,0,0,0,0,0,0" L="45" o="6a7495" X="778" Y="-106" T="12" H="164" /></S><D><DS Y="-82" X="402" /><T Y="' .. Maze.util.toGlobal(Maze.startCell.y) .. '" X="' .. Maze.util.toGlobal(Maze.startCell.x) .. '" /><F Y="' .. Maze.util.toGlobal(Maze.winCell.y) .. '" X="' .. Maze.util.toGlobal(Maze.winCell.x) .. '" /></D><O /></Z></C>')
  130.     Maze.build()
  131.     for name,mouse in pairs(tfm.get.room.playerList) do
  132.         tfm.exec.movePlayer(name, Maze.util.toGlobal(Maze.startCell.x), Maze.util.toGlobal(Maze.startCell.y), false, 0, 0, false)
  133.     end
  134. end
  135.  
  136. function eventPlayerWon(name, te, ter)
  137.     newMap()
  138. end
  139.  
  140. Maze.init()
  141. tfm.exec.disableAutoShaman(true)
  142. tfm.exec.disableAutoNewGame(true)
  143. tfm.exec.disableAfkDeath(true)
  144. if (NIGHTMODE) then NIGHTMODE = 'N="19"' else NIGHTMODE = '' end
  145. newMap()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top