Advertisement
CaptainSpaceCat

TerrariLua V1

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