Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local midW, midH = math.floor(w/2), math.floor(h/2)
- local rooms = {
- [1] = {false, true, true, true},
- [2] = {true, false, true, true},
- [3] = {true, true, false, true},
- [4] = {true, true, true, false},
- [5] = {false, false, true, true},
- [6] = {true, false, false, true},
- [7] = {true, true, false, false},
- [8] = {false, true, true, false},
- [9] = {false, true, false, true},
- [10] = {true, false, true, false},
- [11] = {false, false, false, true},
- [12] = {true, false, false, false},
- [13] = {false, true, false, false},
- [14] = {false, false, true, false},
- [15] = {false, false, false, false}
- }
- --[[local available = {
- ["1"] = {1},
- ["2"] = {2},
- ["3"] = {3},
- ["4"] = {4},
- ["12"] = {1, 2, 5},
- ["23"] = {2, 3, 6},
- ["34"] = {3, 4, 7},
- ["14"] = {1, 4, 8},
- ["13"] = {1, 3, 9},
- ["24"] = {2, 4, 10},
- ["123"] = {1, 2, 3, 5, 6, 11},
- ["234"] = {2, 3, 4, 6, 7, 12},
- ["134"] = {1, 3, 4, 7, 8, 13},
- ["124"] = {1, 2, 4, 5, 8, 14},
- ["1234"] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
- }]]
- local available = {
- ["1"] = {1},
- ["2"] = {2},
- ["3"] = {3},
- ["4"] = {4},
- ["12"] = {1, 2},
- ["23"] = {2, 3},
- ["34"] = {3, 4},
- ["14"] = {1, 4},
- ["13"] = {1, 3},
- ["24"] = {2, 4},
- ["123"] = {1, 2, 3},
- ["234"] = {2, 3, 4},
- ["134"] = {1, 3, 4},
- ["124"] = {1, 2, 4},
- ["1234"] = {1, 2, 3, 4}
- }
- local surroundTab = {
- "____###########################################____",
- "__######## ########__",
- "_##### #####_",
- "_### ###_",
- "### ###",
- "### ###",
- "## ##",
- "## ##",
- "## ##",
- "## ##",
- "## ##",
- "## ##",
- "## ##",
- "### ###",
- "### ###",
- "_### ###_",
- "_##### #####_",
- "__######## ########__",
- "____###########################################____",
- }
- function draw(words,x,y,txtcol,backcol)
- local w,h = term.getSize()
- words = tostring(words)
- words = words or ""
- txtcol = txtcol or colors.white
- backcol = backcol or colors.black
- term.setCursorPos(x, y)
- term.setTextColor(txtcol)
- term.setBackgroundColor(backcol)
- write(words)
- end
- function mazeGenProcedural(mapW, mapH)
- local maze = {}
- local list = {}
- for x = 1, mapW do
- maze[x] = {}
- for y = 1, mapH do
- maze[x][y] = {}
- list[#list + 1] = {x, y}
- for i = 1, 4 do
- maze[x][y][i] = true
- end
- end
- end
- while #list > 0 do
- local current = table.remove(list, math.random(1, #list))
- local usableCells = ""
- if current[2] > 1 and not (maze[current[1]][current[2] - 1][1] and maze[current[1]][current[2] - 1][2] and maze[current[1]][current[2] - 1][3] and maze[current[1]][current[2] - 1][4]) then
- usableCells = usableCells .. "1"
- end
- if current[1] < mapW and not (maze[current[1] + 1][current[2]][1] and maze[current[1] + 1][current[2]][2] and maze[current[1] + 1][current[2]][3] and maze[current[1] + 1][current[2]][4]) then
- usableCells = usableCells .. "2"
- end
- if current[2] < mapH and not (maze[current[1]][current[2] + 1][1] and maze[current[1]][current[2] + 1][2] and maze[current[1]][current[2] + 1][3] and maze[current[1]][current[2] + 1][4]) then
- usableCells = usableCells .. "3"
- end
- if current[1] > 1 and not (maze[current[1] - 1][current[2]][1] and maze[current[1] - 1][current[2]][2] and maze[current[1] - 1][current[2]][3] and maze[current[1] - 1][current[2]][4]) then
- usableCells = usableCells .. "4"
- end
- if usableCells == "" then
- if current[2] > 1 then
- usableCells = usableCells .. "1"
- elseif current[1] < mapW then
- usableCells = usableCells .. "2"
- elseif current[2] < mapH then
- usableCells = usableCells .. "3"
- elseif current[1] > 1 then
- usableCells = usableCells .. "4"
- end
- end
- usableCells = rooms[available[usableCells][math.random(1, #available[usableCells])]]
- for i = 1, 4 do
- if maze[current[1]][current[2]][i] then
- maze[current[1]][current[2]][i] = usableCells[i]
- end
- if not usableCells[i] then
- if current[2] > 1 and i == 1 then
- maze[current[1]][current[2] - 1][3] = false
- elseif current[1] < mapW and i == 2 then
- maze[current[1] + 1][current[2]][4] = false
- elseif current[2] < mapH and i == 3 then
- maze[current[1]][current[2] + 1][1] = false
- elseif current[1] > 1 and i == 4 then
- maze[current[1] - 1][current[2]][2] = false
- end
- end
- end
- end
- for x, v in pairs(maze) do
- for y, e in pairs(v) do
- if e[1] and e[2] and e[3] and e[4] then
- maze[x][y] = {false, false, false, false}
- end
- end
- end
- local gen = maze
- maze = {}
- for i = 1, mapW*2 + 1 do
- maze[i] = {}
- end
- for i = 1, mapH*2 + 1 do
- maze[1][i] = 2^15
- maze[mapW*2 + 1][i] = 2^15
- end
- for x = 2, mapW*2 do
- for y = 1, mapH*2 + 1 do
- if y == 1 or y == mapH*2 + 1 then
- maze[x][y] = 2^15
- elseif x%2 == 0 and y%2 == 0 then
- maze[x][y] = 128
- elseif x%2 == 1 and y%2 == 1 then
- maze[x][y] = 2^15
- else
- if x%2 == 0 then
- if gen[x/2][(y - 1)/2][2] and gen[x/2][(y + 1)/2][4] then
- maze[x][y] = 2^15
- else
- maze[x][y] = 128
- end
- elseif x%2 == 1 then
- if gen[(x - 1)/2][y/2][3] and gen[(x + 1)/2][y/2][1] then
- maze[x][y] = 2^15
- else
- maze[x][y] = 128
- end
- end
- end
- end
- end
- local map = {}
- for x, v in pairs(maze) do
- for i = 0, 9 do
- map[x*10 - i] = {}
- end
- for y, e in pairs(v) do
- for i = 0, 9 do
- for n = 0, 9 do
- map[x*10 - i][y*10 - n] = e
- end
- end
- end
- end
- return maze, map
- end
- local maze, map = mazeGenProcedural(24, 8)
- local imgLocation = {34, 14}
- local pLocation = {{34, 14}, {35, 15}}
- local feet = {2, 1}
- local changeFeet, changeMap = true, true
- function eventHandler(events)
- if events[1] == "key" then
- if events[2] == keys.up and map[pLocation[feet[2]][1]][pLocation[feet[2]][2] - 2] ~= 2^15 then
- pLocation[feet[2]][2] = pLocation[feet[2]][2] - 2
- imgLocation[2] = imgLocation[2] - 1
- changeFeet = true
- elseif events[2] == keys.down and map[pLocation[feet[1]][1]][pLocation[feet[1]][2] + 2] ~= 2^15 then
- pLocation[feet[1]][2] = pLocation[feet[1]][2] + 2
- imgLocation[2] = imgLocation[2] + 1
- changeFeet = true
- elseif events[2] == keys.left and map[pLocation[feet[2]][1] - 2][pLocation[feet[2]][2]] ~= 2^15 then
- pLocation[feet[2]][1] = pLocation[feet[2]][1] - 2
- imgLocation[1] = imgLocation[1] - 1
- changeFeet = true
- elseif events[2] == keys.right and map[pLocation[feet[1]][1] + 2][pLocation[feet[1]][2]] ~= 2^15 then
- pLocation[feet[1]][1] = pLocation[feet[1]][1] + 2
- imgLocation[1] = imgLocation[1] + 1
- changeFeet = true
- end
- end
- end
- function drawRelevantImage(xPos, yPos, img)
- local xSurround = 1
- local ySurround = 1
- for x = xPos < midW and 1 or xPos - midW + 1, xPos + midW < #img and xPos + midW + 1 or #img do
- for y = yPos < midH and 1 or yPos - midH + 1, yPos + midH < #img[x] and yPos + midH + 1 or #img[x] do
- term.setCursorPos(x + midW - xPos, y + midH - yPos)
- term.setTextColor(colors.black)
- if surroundTab[ySurround]:sub(xSurround, xSurround) == "_" then
- term.setBackgroundColor(colors.black)
- term.write(" ")
- else
- term.setBackgroundColor(img[x][y])
- term.write(surroundTab[ySurround]:sub(xSurround, xSurround))
- end
- ySurround = ySurround + 1
- end
- xSurround = xSurround + 1
- ySurround = 1
- end
- end
- while true do
- if changeMap then
- drawRelevantImage(imgLocation[1], imgLocation[2], map)
- changeMap = false
- end
- if changeFeet then
- if feet[1] == 1 then
- feet = {2, 1}
- else
- feet = {1, 2}
- end
- draw(" ", midW + feet[1] - 1, midH, _, colors.lightBlue)
- draw(" ", midW + feet[2] - 1, midH + 1, _, colors.lightBlue)
- changeFeet = false
- end
- local events = {os.pullEvent()}
- local tempMotion = {imgLocation[1], imgLocation[2]}
- eventHandler(events)
- if tempMotion[1] ~= imgLocation[1] or tempMotion[2] ~= imgLocation[2] then
- changeMap = true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement