Advertisement
guitarplayer616

terrarlua

Jun 11th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.40 KB | None | 0 0
  1. local sides = {"left","right","up","down","forward","back"}
  2. for _,v in ipairs(sides) do
  3.   if peripheral.getType(v) == "modem" then
  4.     rednet.open(v)
  5.   end
  6. end
  7. local sFile = fs.open("world", "w")
  8. local chunk = 0
  9. local termW, termH = term.getSize()
  10. local midW, midH = math.floor(termW/2), math.floor(termH/2)
  11. local xPos, yPos = 0, 51
  12. local pDir = ">"
  13. local blockinfo = {
  14. ["bedrock"] = 128,
  15. ["stone"] = 256,
  16. ["dirt"] = 4096,
  17. ["grass"] = 32,
  18. ["air"] = 32768
  19. }
  20. world = nil
  21. local blocks = {}
  22. blocks[0] = "bedrock"
  23. blocks[1] = "bedrock"
  24. for i = 2, 24 do
  25.         blocks[i] = "stone"
  26. end
  27. for i = 25, 40 do
  28.         blocks[i] = "dirt"
  29. end
  30. for i = 41, 49 do
  31.         blocks[i] = "grass"
  32. end
  33. blocks[50] = "air"
  34. function generateWorld()
  35.         local sFile = fs.open("world", "w")
  36.         --sFile.writeLine("{")
  37.         for x = -midW - 3, midW + 3 do
  38.                 sFile.writeLine("  [" .. tostring(x) .. "] = {")
  39.                 for y = 1, 50 do
  40.                         sFile.writeLine("    [" .. tostring(y) .. "] = \"" .. tostring(blocks[math.random(y/1.2, y)]) .. "\",")
  41.                 end
  42.                 for y = 51, 100 do
  43.                         sFile.write("    [" .. tostring(y) .. "] = \"air\"")
  44.                         if y == 100 then
  45.                                 sFile.writeLine("")
  46.                         else
  47.                                 sFile.writeLine(",")
  48.                         end
  49.                 end
  50.                 if x == midW + 3 then
  51.                         sFile.writeLine("  }")
  52.                 else
  53.                         sFile.writeLine("  },")
  54.                 end
  55.         end
  56.         --sFile.writeLine("}")
  57.         sFile.close()
  58. end
  59.  
  60. function displayWorld()
  61.         term.setBackgroundColor(colors.black)
  62.         term.clear()
  63.         term.setCursorPos(midW + 1, midH + 1)
  64.         term.setBackgroundColor(colors.lightBlue)
  65.         term.write(pDir)
  66.         for x = xPos - midW, xPos + midW do
  67.                 for y = (yPos > 100 - midH) and 100 or yPos + midH, (yPos <= midH) and 1 or yPos - midH, -1 do
  68.                         if world[x][y] ~= "air" then
  69.                                 term.setCursorPos(xPos + midW - x + 1, midH + yPos - y + 1)
  70.                                 term.setBackgroundColor(blockinfo[world[x][y]])
  71.                                 term.write(" ")
  72.                         end
  73.                 end
  74.         end
  75. end
  76.  
  77. function updateWorld()
  78.         world = nil
  79.         local lFile = fs.open("world", "r")
  80.         loadstring("world = {\n" .. lFile.readAll() .. "\n}")()
  81.         lFile.close()
  82.         if not world[xPos - midW - 3] or not world[xPos + midW + 3] then
  83.                 local cFile = fs.open("world", "r")
  84.                 local temp = cFile.readAll()
  85.                 cFile.close()
  86.                 local sFile = fs.open("world", "w")
  87.                 if not world[xPos - midW - 3] then
  88.                         sFile.writeLine("  [" .. tostring(xPos - midW - 3) .. "] = {")
  89.                         for y = 1, 50 do
  90.                                 sFile.writeLine("    [" .. tostring(y) .. "] = \"" .. tostring(blocks[math.random(y/1.2, y)]) .. "\",")
  91.                         end
  92.                         for y = 51, 100 do
  93.                                 sFile.write("    [" .. tostring(y) .. "] = \"air\"")
  94.                                 if y == 100 then
  95.                                         sFile.writeLine("")
  96.                                 else
  97.                                         sFile.writeLine(",")
  98.                                 end
  99.                         end
  100.                         sFile.writeLine("  },")
  101.                         sFile.writeLine(temp)
  102.                 elseif not world[xPos + midW + 3] then
  103.                         sFile.writeLine(temp .. ",")
  104.                         sFile.writeLine("  [" .. tostring(xPos + midW + 3) .. "] = {")
  105.                         for y = 1, 50 do
  106.                                 sFile.writeLine("    [" .. tostring(y) .. "] = \"" .. tostring(blocks[math.random(y/1.2, y)]) .. "\",")
  107.                         end
  108.                         for y = 51, 100 do
  109.                                 sFile.write("    [" .. tostring(y) .. "] = \"air\"")
  110.                                 if y == 100 then
  111.                                         sFile.writeLine("")
  112.                                 else
  113.                                         sFile.writeLine(",")
  114.                                 end
  115.                         end
  116.                         sFile.writeLine("  }")
  117.                 end
  118.                 sFile.close()
  119.         end    
  120. end
  121.  
  122. generateWorld()
  123. while true do
  124.         updateWorld()
  125.         displayWorld()
  126.         term.setCursorPos(1, 1)
  127.         term.write(tostring(xPos) .. "," .. tostring(yPos))
  128.         sleep(.05)
  129.         local events = {os.pullEvent()}
  130.     if events[1]=="key" or events[1] == "rednet_message" then
  131.                 if events[2] == 200 or tonumber(events[3]) == 200 then --up
  132.                         pDir = "^"
  133.                         if world[xPos][yPos + 1] == "air" then
  134.                                 yPos = yPos + 1
  135.                         end
  136.                 elseif events[2] == 205 or tonumber(events[3]) == 205 then --right
  137.                         pDir = ">"
  138.                         if world[xPos - 1][yPos] == "air" then
  139.                                 xPos = xPos - 1
  140.                         end
  141.                 elseif events[2] == 208 or tonumber(events[3]) == 208 then --down
  142.                         pDir = "v"
  143.                         if world[xPos][yPos - 1] == "air" then
  144.                                 yPos = yPos - 1
  145.                         end
  146.                 elseif events[2] == 203 or tonumber(events[3]) == 203 then --left
  147.                         pDir = "<"
  148.                         if world[xPos + 1][yPos] == "air" then
  149.                                 xPos = xPos + 1
  150.                         end
  151.                 end
  152.                 if events[2] == 17 or tonumber(events[3]) == 17 then
  153.                         pDir = "^"
  154.                 elseif events[2] == 30 or tonumber(events[3]) == 30 then
  155.                         pDir = "<"
  156.                 elseif events[2] == 31 or tonumber(events[3]) == 31 then
  157.                         pDir = "v"
  158.                 elseif events[2] == 32 or tonumber(events[3]) == 32 then
  159.                         pDir = ">"
  160.                 end
  161.                 if events[2] == 45 or tonumber(events[3]) == 45 then
  162.                         local changeB = "air"
  163.                         local oTemp = fs.open("temp", "w")
  164.                         local changeX, changeY = nil, nil
  165.                         local ready = false
  166.                         if pDir == "^" then
  167.                                 changeX, changeY = xPos, yPos + 1
  168.                         elseif pDir == ">" then
  169.                                 changeX, changeY = xPos - 1, yPos
  170.                         elseif pDir == "v" then
  171.                                 changeX, changeY = xPos, yPos - 1
  172.                         elseif pDir == "<" then
  173.                                 changeX, changeY = xPos + 1, yPos
  174.                         end
  175.                         if world[changeX][changeY] ~= "bedrock" then
  176.                                 for line in io.lines("world") do
  177.                                         if line:sub(3, 4 + #tostring(changeX)) == "[" ..tostring(changeX) .. "]" then
  178.                                                 ready = true
  179.                                         end
  180.                                         if ready and line:sub(6, 5 + #tostring(changeY)) == tostring(changeY) then
  181.                                                 oTemp.writeLine("    [" .. tostring(changeY) .. "] = \"" .. changeB .."\",")
  182.                                                 ready = false
  183.                                         else
  184.                                                 oTemp.writeLine(line)
  185.                                         end
  186.                                 end
  187.                                 oTemp.close()
  188.                                 oTemp = fs.open("temp", "r")
  189.                                 local temp = oTemp.readAll()
  190.                                 oTemp.close()
  191.                                 local sFile = fs.open("world", "w")
  192.                                 sFile.write(temp)
  193.                                 sFile.close()
  194.                         end
  195.                 end
  196.                 if events[2] == 46 or tonumber(events[3]) == 46 then
  197.                         local changeB = "stone"
  198.                         local oTemp = fs.open("temp", "w")
  199.                         local changeX, changeY = nil, nil
  200.                         local ready = false
  201.                         if pDir == "^" then
  202.                                 changeX, changeY = xPos, yPos + 1
  203.                         elseif pDir == ">" then
  204.                                 changeX, changeY = xPos - 1, yPos
  205.                         elseif pDir == "v" then
  206.                                 changeX, changeY = xPos, yPos - 1
  207.                         elseif pDir == "<" then
  208.                                 changeX, changeY = xPos + 1, yPos
  209.                         end
  210.                         if world[changeX][changeY] == "air" then
  211.                                 for line in io.lines("world") do
  212.                                         if line:sub(3, 4 + #tostring(changeX)) == "[" ..tostring(changeX) .. "]" then
  213.                                                 ready = true
  214.                                         end
  215.                                         if ready and line:sub(6, 5 + #tostring(changeY)) == tostring(changeY) then
  216.                                                 oTemp.writeLine("    [" .. tostring(changeY) .. "] = \"" .. changeB .."\",")
  217.                                                 ready = false
  218.                                         else
  219.                                                 oTemp.writeLine(line)
  220.                                         end
  221.                                 end
  222.                                 oTemp.close()
  223.                                 oTemp = fs.open("temp", "r")
  224.                                 local temp = oTemp.readAll()
  225.                                 oTemp.close()
  226.                                 local sFile = fs.open("world", "w")
  227.                                 sFile.write(temp)
  228.                                 sFile.close()
  229.                         end
  230.                 end
  231.         end            
  232. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement