Advertisement
Guest User

Transformice Maze

a guest
May 17th, 2015
1,536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.52 KB | None | 0 0
  1. NIGHTMODE = true
  2.  
  3. Maze = {
  4.     seed = nil,
  5.     width = nil,
  6.     height = nil,
  7.     cellSize = nil,
  8.     cellCount = 0,
  9.     cells = {},
  10.     winCell = {x=0, y=0},
  11.     startCell = {x=0, y=0},
  12.     util = {
  13.         getCellId = function(x, y)
  14.             return x + y * Maze.width
  15.         end,
  16.         getCell = function(x, y)
  17.             return Maze.cells[Maze.util.getCellId(x, y)]
  18.         end,
  19.         getCellXY = function (id)
  20.             return {x = id % Maze.width, y = math.floor(id / Maze.width)}
  21.         end,
  22.         toGlobal = function(n)
  23.             return n * Maze.cellSize + Maze.cellSize / 2
  24.         end,
  25.         getNeighbors = function(id)
  26.             local ra = {}
  27.             if (id == nil) then return ra end
  28.             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
  29.             if (id - 1 >= 0 and not Maze.util.isLeftEdge(id) and not Maze.cells[id - 1].visited) then table.insert(ra, id - 1) end
  30.             if (id + Maze.width <= Maze.cellCount and not Maze.cells[id + Maze.width].visited) then table.insert(ra, id + Maze.width) end
  31.             if (id - Maze.width >= 0 and not Maze.cells[id - Maze.width].visited) then table.insert(ra, id - Maze.width) end
  32.             return ra
  33.         end,
  34.         isRightEdge = function(id)
  35.             return Maze.util.isLeftEdge(id + 1)
  36.         end,
  37.         isLeftEdge = function(id)
  38.             return id % Maze.width == 0
  39.         end
  40.     },
  41.     init = function(s, c, w, h)
  42.         Maze.seed = s
  43.         Maze.cellSize = c
  44.         Maze.width = w
  45.         Maze.height = h
  46.        
  47.         if (Maze.seed == nil) then Maze.seed = os.time() end
  48.         if (Maze.cellSize == nil) then Maze.cellSize = 50 end
  49.         if (Maze.width == nil) then Maze.width = math.floor(800 / Maze.cellSize) end
  50.         if (Maze.height == nil) then Maze.height = math.floor(400 / Maze.cellSize) end
  51.        
  52.         Maze.cellCount = Maze.width * Maze.height
  53.         math.randomseed(Maze.seed)
  54.     end,
  55.     generate = function()
  56.         local visited = {}
  57.        
  58.         Maze.winCell.x = math.floor(math.random() * Maze.width)
  59.         Maze.winCell.y = math.floor(math.random() * Maze.height)
  60.        
  61.         for i=0,Maze.cellCount do
  62.             Maze.cells[i] = {top = true, bot = true, left = true, right = true, visited = false}
  63.         end
  64.        
  65.         local count = Maze.cellCount
  66.         local operator = {Maze.util.getCellId(Maze.winCell.x, Maze.winCell.y)}
  67.         local overflow = 0
  68.         local endOp = 0
  69.         while count > 0 do
  70.             local neighbors = Maze.util.getNeighbors(operator[#operator])
  71.             local id = nil
  72.            
  73.             if (next(neighbors) ~= nil) then
  74.                 id = neighbors[math.random(#neighbors)]
  75.             end
  76.            
  77.            
  78.             if (id ~= nil and Maze.cells[id].visited) then
  79.                 id = nil
  80.             end
  81.            
  82.             if (id == nil) then
  83.                 table.remove(operator)
  84.                 overflow = overflow + 1
  85.                 if (overflow > Maze.cellCount) then break end
  86.             else
  87.                 overflow = 0
  88.                 local diff = id - operator[#operator]
  89.                
  90.                 if (diff == -1) then
  91.                     Maze.cells[id].right = false
  92.                     Maze.cells[operator[#operator]].left = false
  93.                 elseif (diff == 1) then
  94.                     Maze.cells[id].left = false
  95.                     Maze.cells[operator[#operator]].right = false
  96.                 elseif (diff == 16) then
  97.                     Maze.cells[id].top = false
  98.                     Maze.cells[operator[#operator]].bot = false
  99.                 elseif (diff == -16) then
  100.                     Maze.cells[id].bot = false
  101.                     Maze.cells[operator[#operator]].top = false
  102.                 end
  103.  
  104.                 Maze.cells[id].visited = true
  105.                 table.insert(operator, id)
  106.                 endOp = id
  107.                 count = count - 1
  108.             end
  109.         end
  110.         Maze.startCell = Maze.util.getCellXY(endOp)
  111.     end,
  112.     build = function()
  113.         for i=0,Maze.cellCount do
  114.             local x = Maze.cellSize * (i % Maze.width)
  115.             local y = Maze.cellSize * math.floor(i / Maze.width)
  116.             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
  117.             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
  118.             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
  119.             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
  120.         end
  121.     end
  122. }
  123.  
  124. function newMap()
  125.     Maze.generate()
  126.     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>')
  127.     Maze.build()
  128.     for name,mouse in pairs(tfm.get.room.playerList) do
  129.         tfm.exec.movePlayer(name, Maze.util.toGlobal(Maze.startCell.x), Maze.util.toGlobal(Maze.startCell.y), false, 0, 0, false)
  130.     end
  131. end
  132.  
  133. function eventPlayerWon(name, te, ter)
  134.     newMap()
  135. end
  136.  
  137. Maze.init()
  138. tfm.exec.disableAutoShaman(true)
  139. tfm.exec.disableAutoNewGame(true)
  140. tfm.exec.disableAfkDeath(true)
  141. if (NIGHTMODE) then NIGHTMODE = 'N="19"' else NIGHTMODE = '' end
  142. newMap()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement