Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local events
- 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}
- }
- 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
- function drawRelevantImage(xPos, yPos, img, col)
- 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
- if img[x][y] == 128 then
- term.setCursorPos(x + midW - xPos, y + midH - yPos)
- term.setBackgroundColor(col)
- term.write(" ")
- end
- end
- end
- end
- local maze, map = mazeGenProcedural(24, 8)
- local pLocation = {11, 11}
- local feet = {2, 1}
- local mapColor = colors.gray
- local backColor = colors.red
- local changeFeet, changeMap = true, true
- function backFlash()
- while true do
- term.setBackgroundColor(backColor)
- term.clear()
- sleep(1)
- if backColor == colors.black then
- backColor = colors.red
- else
- backcolor = colors.black
- end
- end
- end
- function pullTimedEvent(type, time)
- local timer, events
- if time then
- timer = os.startTimer(time)
- end
- if type then
- events = {os.pullEvent(type)}
- else
- events = {os.pullEvent()}
- end
- if events[1] ~= "timer" then
- os.cancelTimer(timer)
- return events
- end
- return nil
- end
- function main(events)
- end
- local coMain = coroutine.create(function()
- while true do
- if events[1] == "key" then
- if events[2] == keys.up and map[pLocation[1]][pLocation[2] - 1] ~= 2^15 and map[pLocation[1] + 1][pLocation[2] - 1] ~= 2^15 then
- pLocation[2] = pLocation[2] - 1
- changeMap, changeFeet = true, true
- elseif events[2] == keys.down and map[pLocation[1]][pLocation[2] + 2] ~= 2^15 and map[pLocation[1] + 1][pLocation[2] + 2] ~= 2^15 then
- pLocation[2] = pLocation[2] + 1
- changeMap, changeFeet = true, true
- elseif events[2] == keys.left and map[pLocation[1] - 1][pLocation[2]] ~= 2^15 and map[pLocation[1] - 1][pLocation[2] + 1] ~= 2^15 then
- pLocation[1] = pLocation[1] - 1
- changeMap, changeFeet = true, true
- elseif events[2] == keys.right and map[pLocation[1] + 2][pLocation[2]] ~= 2^15 and map[pLocation[1] + 2][pLocation[2] + 1] ~= 2^15 then
- pLocation[1] = pLocation[1] + 1
- changeMap, changeFeet = true, true
- end
- end
- if changeFeet then
- if feet[1] == 1 then
- feet = {2, 1}
- else
- feet = {1, 2}
- end
- changeFeet = false
- end
- end
- coroutine.yield()
- end
- end)
- local coFlash = coroutine.create(function()
- while true do
- if backColor == colors.black then
- backColor = colors.red
- else
- backColor = colors.black
- end
- term.setBackgroundColor(backColor)
- term.clear()
- drawRelevantImage(pLocation[1], pLocation[2], map, mapColor)
- draw(" ", midW + feet[1] - 1, midH, _, colors.lightBlue)
- draw(" ", midW + feet[2] - 1, midH + 1, _, colors.lightBlue)
- coroutine.yield()
- end
- end
- )
- while true do
- events = pullTimedEvent(_, 1)
- coroutine.resume(coMain)
- coroutine.resume(coFlash)
- sleep(.05)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement