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(filename)
- local sFile = fs.open(filename, "w")
- 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.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 loadWorld(filename)
- world = nil
- local lFile = fs.open(filename, "r")
- loadstring("world = {\n" .. lFile.readAll() .. "\n}")()
- lFile.close()
- end
- function updateWorld()
- if not world[xPos - midW - 3] or not world[xPos + midW + 3] then
- if not world[xPos - midW - 3] then
- world[xPos - midW - 3] = {}
- for y = 1, 50 do
- world[xPos - midW - 3][y] = tostring(blocks[math.random(y/1.2, y)])
- end
- for y = 51, 100 do
- world[xPos - midW - 3][y] = "air"
- end
- elseif not world[xPos + midW + 3] then
- world[xPos + midW + 3] = {}
- for y = 1, 50 do
- world[xPos + midW + 3][y] = tostring(blocks[math.random(y/1.2, y)])
- end
- for y = 51, 100 do
- world[xPos + midW + 3][y] = "air"
- end
- end
- end
- end
- function saveWorld(filename)
- local sFile = fs.open(filename, "w")
- local worldlen = 0
- local worldmid = nil
- for i, v in pairs(world) do
- worldlen = worldlen + 1
- if i == 0 then
- worldmid = worldlen
- end
- end
- for x = -(worldlen - worldmid), worldmid - 1 do
- sFile.write(" [" .. tostring(x) .. "] = {")
- for y = 1, 100 do
- sFile.write("\"" .. world[x][y] .. "\"")
- if y == 100 and x == worldmid - 1 then
- sFile.writeLine("}")
- elseif y == 100 then
- sFile.writeLine("},")
- else
- sFile.write(", ")
- end
- end
- sFile.flush()
- end
- sFile.close()
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(midW - 4, 2)
- term.write("TerrariLua")
- term.setCursorPos(midW - 5, 8)
- term.write("Start Game")
- term.setCursorPos(midW - 4, 10)
- term.write("Exit Game")
- term.setBackgroundColor(colors.blue)
- while true do
- local events = {os.pullEvent()}
- if events[1] == "mouse_click" and events[2] == 1 then
- if events[3] >= midW - 5 and events[3] <= midW + 5 and events[4] == 8 then
- term.setCursorPos(midW - 5, 8)
- term.write("Start Game")
- sleep(.2)
- break
- end
- if events[3] >= midW - 4 and events[3] <= midW + 4 and events[4] == 10 then
- term.setCursorPos(midW - 4, 10)
- term.write("Exit Game")
- sleep(.2)
- os.exit()
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(midW - 4, 2)
- term.write("Start Menu")
- term.setCursorPos(midW - 4, 8)
- term.write("New Game")
- term.setCursorPos(midW - 4, 10)
- term.write("Load Game")
- term.setBackgroundColor(colors.blue)
- local startUp = nil
- while true do
- local events = {os.pullEvent()}
- if events[1] == "mouse_click" and events[2] == 1 then
- if events[3] >= midW - 4 and events[3] <= midW + 4 and events[4] == 8 then
- term.setCursorPos(midW - 4, 8)
- term.write("New Game")
- startUp = "new"
- sleep(.2)
- break
- end
- if events[3] >= midW - 4 and events[3] <= midW + 4 and events[4] == 10 then
- term.setCursorPos(midW - 4, 10)
- term.write("Load Game")
- startUp = "load"
- sleep(.2)
- break
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Input World Name: ")
- local filename = nil
- if startUp == "new" then
- filename = tostring(read())
- generateWorld(filename)
- loadWorld(filename)
- elseif startUp == "load" then
- filename = tostring(read())
- loadWorld(filename)
- end
- 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
- world[changeX][changeY] = "air"
- 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
- world[changeX][changeY] = "stone"
- end
- end
- if events[2] == 29 then
- saveWorld(filename)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement