Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sFile = fs.open("world", "w")
- local chunk = 0
- local termW, termH = term.getSize()
- local midW, midH = math.floor(termW/2), math.floor(termH/2)
- local xPos, yPos = 0, 51
- local pDir = ">"
- local blockinfo = {
- ["bedrock"] = 128,
- ["stone"] = 256,
- ["dirt"] = 4096,
- ["grass"] = 32,
- ["air"] = 32768
- }
- world = nil
- local blocks = {}
- blocks[0] = "bedrock"
- blocks[1] = "bedrock"
- for i = 2, 24 do
- blocks[i] = "stone"
- end
- for i = 25, 40 do
- blocks[i] = "dirt"
- end
- for i = 41, 49 do
- blocks[i] = "grass"
- end
- blocks[50] = "air"
- function generateWorld()
- local sFile = fs.open("world", "w")
- --sFile.writeLine("{")
- for x = -midW - 3, midW + 3 do
- sFile.writeLine(" [" .. tostring(x) .. "] = {")
- for y = 1, 50 do
- sFile.writeLine(" [" .. tostring(y) .. "] = \"" .. tostring(blocks[math.random(y/1.2, y)]) .. "\",")
- end
- for y = 51, 100 do
- sFile.write(" [" .. tostring(y) .. "] = \"air\"")
- if y == 100 then
- sFile.writeLine("")
- else
- sFile.writeLine(",")
- end
- end
- if x == midW + 3 then
- sFile.writeLine(" }")
- else
- sFile.writeLine(" },")
- end
- end
- --sFile.writeLine("}")
- sFile.close()
- end
- function displayWorld()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(midW + 1, midH + 1)
- term.setBackgroundColor(colors.lightBlue)
- term.write(pDir)
- for x = xPos - midW, xPos + midW do
- for y = (yPos > 100 - midH) and 100 or yPos + midH, (yPos <= midH) and 1 or yPos - midH, -1 do
- if world[x][y] ~= "air" then
- term.setCursorPos(xPos + midW - x + 1, midH + yPos - y + 1)
- term.setBackgroundColor(blockinfo[world[x][y]])
- term.write(" ")
- end
- end
- end
- end
- function updateWorld()
- world = nil
- local lFile = fs.open("world", "r")
- loadstring("world = {\n" .. lFile.readAll() .. "\n}")()
- lFile.close()
- if not world[xPos - midW - 3] or not world[xPos + midW + 3] then
- local cFile = fs.open("world", "r")
- local temp = cFile.readAll()
- cFile.close()
- local sFile = fs.open("world", "w")
- if not world[xPos - midW - 3] then
- sFile.writeLine(" [" .. tostring(xPos - midW - 3) .. "] = {")
- for y = 1, 50 do
- sFile.writeLine(" [" .. tostring(y) .. "] = \"" .. tostring(blocks[math.random(y/1.2, y)]) .. "\",")
- end
- for y = 51, 100 do
- sFile.write(" [" .. tostring(y) .. "] = \"air\"")
- if y == 100 then
- sFile.writeLine("")
- else
- sFile.writeLine(",")
- end
- end
- sFile.writeLine(" },")
- sFile.writeLine(temp)
- elseif not world[xPos + midW + 3] then
- sFile.writeLine(temp .. ",")
- sFile.writeLine(" [" .. tostring(xPos + midW + 3) .. "] = {")
- for y = 1, 50 do
- sFile.writeLine(" [" .. tostring(y) .. "] = \"" .. tostring(blocks[math.random(y/1.2, y)]) .. "\",")
- end
- for y = 51, 100 do
- sFile.write(" [" .. tostring(y) .. "] = \"air\"")
- if y == 100 then
- sFile.writeLine("")
- else
- sFile.writeLine(",")
- end
- end
- sFile.writeLine(" }")
- end
- sFile.close()
- end
- end
- generateWorld()
- while true do
- updateWorld()
- displayWorld()
- term.setCursorPos(1, 1)
- term.write(tostring(xPos) .. "," .. tostring(yPos))
- sleep(.05)
- local events = {os.pullEvent()}
- if events[1] == "key" then
- if events[2] == 200 then --up
- pDir = "^"
- if world[xPos][yPos + 1] == "air" then
- yPos = yPos + 1
- end
- elseif events[2] == 205 then --right
- pDir = ">"
- if world[xPos - 1][yPos] == "air" then
- xPos = xPos - 1
- end
- elseif events[2] == 208 then --down
- pDir = "v"
- if world[xPos][yPos - 1] == "air" then
- yPos = yPos - 1
- end
- elseif events[2] == 203 then --left
- pDir = "<"
- if world[xPos + 1][yPos] == "air" then
- xPos = xPos + 1
- end
- end
- if events[2] == 17 then
- pDir = "^"
- elseif events[2] == 30 then
- pDir = "<"
- elseif events[2] == 31 then
- pDir = "v"
- elseif events[2] == 32 then
- pDir = ">"
- end
- if events[2] == 45 then
- local changeB = "air"
- local oTemp = fs.open("temp", "w")
- local changeX, changeY = nil, nil
- local ready = false
- if pDir == "^" then
- changeX, changeY = xPos, yPos + 1
- elseif pDir == ">" then
- changeX, changeY = xPos - 1, yPos
- elseif pDir == "v" then
- changeX, changeY = xPos, yPos - 1
- elseif pDir == "<" then
- changeX, changeY = xPos + 1, yPos
- end
- if world[changeX][changeY] ~= "bedrock" then
- for line in io.lines("world") do
- if line:sub(3, 4 + #tostring(changeX)) == "[" ..tostring(changeX) .. "]" then
- ready = true
- end
- if ready and line:sub(6, 5 + #tostring(changeY)) == tostring(changeY) then
- oTemp.writeLine(" [" .. tostring(changeY) .. "] = \"" .. changeB .."\",")
- ready = false
- else
- oTemp.writeLine(line)
- end
- end
- oTemp.close()
- oTemp = fs.open("temp", "r")
- local temp = oTemp.readAll()
- oTemp.close()
- local sFile = fs.open("world", "w")
- sFile.write(temp)
- sFile.close()
- end
- end
- if events[2] == 46 then
- local changeB = "stone"
- local oTemp = fs.open("temp", "w")
- local changeX, changeY = nil, nil
- local ready = false
- if pDir == "^" then
- changeX, changeY = xPos, yPos + 1
- elseif pDir == ">" then
- changeX, changeY = xPos - 1, yPos
- elseif pDir == "v" then
- changeX, changeY = xPos, yPos - 1
- elseif pDir == "<" then
- changeX, changeY = xPos + 1, yPos
- end
- if world[changeX][changeY] == "air" then
- for line in io.lines("world") do
- if line:sub(3, 4 + #tostring(changeX)) == "[" ..tostring(changeX) .. "]" then
- ready = true
- end
- if ready and line:sub(6, 5 + #tostring(changeY)) == tostring(changeY) then
- oTemp.writeLine(" [" .. tostring(changeY) .. "] = \"" .. changeB .."\",")
- ready = false
- else
- oTemp.writeLine(line)
- end
- end
- oTemp.close()
- oTemp = fs.open("temp", "r")
- local temp = oTemp.readAll()
- oTemp.close()
- local sFile = fs.open("world", "w")
- sFile.write(temp)
- sFile.close()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement