Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- programa para crear un laberinto segun el metodo de Prim
- -- puede recargar materiales al final de cada linea
- -- dimensiones maximas (incluyendo marco)
- -- y: hacia delante, x: hacia la derecha
- dimXMax = 7
- dimYMax = 13
- -- dimensiones del laberinto (numero de pasillos)
- dimX = math.floor((dimXMax - 1)/2)
- dimY = math.floor((dimYMax - 1)/2)
- -- semilla aleatoria
- math.randomseed(0)
- -- funcion para representar una matriz
- function printMatrix(matrix)
- text = ""
- color = ""
- for i = 1, table.getn(matrix) do
- text = matrix[i][1]
- if matrix[i][1] == 1 then
- color = "0"
- else
- color = "f"
- end
- for j = 2, table.getn(matrix[i]) do
- text = text .. matrix[i][j]
- if matrix[i][j] == 1 then
- color = color .. "0"
- else
- color = color .. "f"
- end
- end
- term.blit(text,color,color)
- print(text)
- end
- end
- -- funcion que devuelve una matriz con los vecinos de un punto
- -- cada fila corresponde a un vecino
- -- las columnas representan: fila, columna y valor
- function neig(ip,jp,matrix)
- -- ip = fila del punto estudiado
- -- jp = columna del punto estudiado
- -- matrix = matriz
- -- calculamos el tamano de la matriz
- iMax = table.getn(matrix)
- jMax = table.getn(matrix[1])
- -- matriz de desplazamientos para los vecinos
- neigMat = {{-1,0},{0,-1},{ 1,0},{0, 1}}
- -- vector de vecinos
- neigs = {}
- for k = 1, 4 do
- -- coordenadas del vecino
- iNeig = ip + neigMat[k][1]
- jNeig = jp + neigMat[k][2]
- -- si las coordenadas estan detro de la matriz
- -- guardamos los datos
- if iNeig>0 and iNeig<=iMax and jNeig>0 and jNeig<=jMax then
- table.insert(neigs, {iNeig, jNeig, matrix[iNeig][jNeig]})
- end
- end
- -- devolvemos la matriz de vecinos
- return neigs
- end
- -- creamos una cuadricula de dimX de ancho y dimY de largo
- maze = {}
- walls = {}
- -- 0 = pared
- -- 1 = pasillo visitado
- -- 2 = pared seleccionable
- -- 3 = pasillo no visitado (sala)
- for i = 1, 2*dimY - 1 do
- maze[i] = {}
- for j = 1, 2*dimX - 1 do
- if j%2 == 1 and i%2 == 1 then
- maze[i][j] = 3
- else
- maze[i][j] = 0
- end
- end
- end
- -- selecciono una sala, la anado al pasillo y sus
- -- paredes las anado a la lista de seleccionables
- iSelect = 2*math.random(0,dimY-1) + 1
- jSelect = 2*math.random(0,dimX-1) + 1
- maze[iSelect][jSelect] = 1
- neigs = neig(iSelect, jSelect, maze)
- for kNeig = 1, table.getn(neigs) do
- iNeig = neigs[kNeig][1]
- jNeig = neigs[kNeig][2]
- maze[iNeig][jNeig] = 2
- table.insert(walls,{iNeig,jNeig})
- end
- -- bucle principal
- -- mientras queden paredes seleccionables
- while table.getn(walls) > 0 do
- -- selecciono una pared
- wallIndex = math.random(table.getn(walls))
- iWall = walls[wallIndex][1]
- jWall = walls[wallIndex][2]
- -- contamos el numero de salas adyacentes
- neigs = neig(iWall, jWall, maze)
- rooms = {}
- for k = 1, table.getn(neigs) do
- if neigs[k][3] == 3 then
- table.insert(rooms,neigs[k])
- end
- end
- -- si solo hay una sala
- if table.getn(rooms) == 1 then
- -- quitamos la pared que pasa a ser parte del pasillo
- maze[iWall][jWall] = 1
- -- la sala pasa tambien a formar parte del pasillo
- iRoom = rooms[1][1]
- jRoom = rooms[1][2]
- maze[iRoom][jRoom] = 1
- -- las paredes adyacentes a la sala pasan a la lista de seleccionables
- neigs = neig(iRoom, jRoom, maze)
- for k = 1, table.getn(neigs) do
- if neigs[k][3] == 0 then
- iNeig = neigs[k][1]
- jNeig = neigs[k][2]
- table.insert(walls, {iNeig,jNeig})
- maze[iNeig][jNeig] = 2
- end
- end
- else
- -- si hay un numero distinto de salas adayacentes, lo volvemos un muro normal
- maze[iWall][jWall] = 0
- end
- -- eliminamos de la lista la pared analizada
- table.remove(walls,wallIndex)
- end
- printMatrix(maze)
- -- construccion del laberinto
- turtle.select(1)
- -- funcion para buscar el siguiente slot con recursos
- -- si no quedan recursos devuelve false, en caso contrario true
- function placeBlock()
- while turtle.getItemCount() == 0 do
- turtle.select(turtle.getSelectedSlot() + 1)
- if turtle.getSelectedSlot() == 16 and turtle.getItemCount() == 0 then
- return false
- end
- end
- turtle.placeDown()
- return true
- end
- -- funcion para recoger mas bloques para construir de un inventario al final de una linea
- function reBlocks()
- if turtle.suck() then
- turtle.select(1)
- while turtle.suck() do
- end
- end
- end
- -- empezamos haciendo un marco
- placeBlock()
- for k = 1, table.getn(maze) + 1 do
- turtle.forward()
- placeBlock()
- end
- turtle.turnRight()
- for k = 1, table.getn(maze[1]) + 1 do
- turtle.forward()
- placeBlock()
- end
- turtle.turnRight()
- for k = 1, table.getn(maze) + 1 do
- turtle.forward()
- placeBlock()
- end
- turtle.turnRight()
- for k = 1, table.getn(maze[1]) do
- turtle.forward()
- placeBlock()
- end
- turtle.turnRight()
- -- contruimos el laberinto
- iMax = table.getn(maze)
- jMax = table.getn(maze[1])
- turtle.forward()
- for j = 1, jMax do
- -- contruimos una linea
- for ii = 1, iMax do
- if j%2 == 1 then
- i = iMax - ii + 1
- else
- i = ii
- end
- if maze[i][j] == 0 then
- placeBlock()
- end
- turtle.forward()
- end
- -- repostamos material
- reBlocks()
- -- damos la vuelta
- for k = 1,2 do
- if j%2 == 1 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- turtle.forward()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement