Advertisement
CaptainSpaceCat

TerrariLua V2

Jun 11th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.94 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.  
  29. function generateWorld(filename)
  30.     local sFile = fs.open(filename, "w")
  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.close()
  51. end
  52.  
  53. function displayWorld()
  54.     term.setBackgroundColor(colors.black)
  55.     term.clear()
  56.     term.setCursorPos(midW + 1, midH + 1)
  57.     term.setBackgroundColor(colors.lightBlue)
  58.     term.write(pDir)
  59.     for x = xPos - midW, xPos + midW do
  60.         for y = (yPos > 100 - midH) and 100 or yPos + midH, (yPos <= midH) and 1 or yPos - midH, -1 do
  61.             if world[x][y] ~= "air" then
  62.                 term.setCursorPos(xPos + midW - x + 1, midH + yPos - y + 1)
  63.                 term.setBackgroundColor(blockinfo[world[x][y]])
  64.                 term.write(" ")
  65.             end
  66.         end
  67.     end
  68. end
  69.  
  70. function loadWorld(filename)
  71.         world = nil
  72.         local lFile = fs.open(filename, "r")
  73.         loadstring("world = {\n" .. lFile.readAll() .. "\n}")()
  74.         lFile.close()
  75. end
  76.  
  77. function updateWorld()
  78.     if not world[xPos - midW - 3] or not world[xPos + midW + 3] then
  79.         if not world[xPos - midW - 3] then
  80.             world[xPos - midW - 3] = {}
  81.             for y = 1, 50 do
  82.                 world[xPos - midW - 3][y] = tostring(blocks[math.random(y/1.2, y)])
  83.             end
  84.             for y = 51, 100 do
  85.                 world[xPos - midW - 3][y] = "air"
  86.             end
  87.         elseif not world[xPos + midW + 3] then
  88.             world[xPos + midW + 3] = {}
  89.             for y = 1, 50 do
  90.                 world[xPos + midW + 3][y] = tostring(blocks[math.random(y/1.2, y)])
  91.             end
  92.             for y = 51, 100 do
  93.                 world[xPos + midW + 3][y] = "air"
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. function saveWorld(filename)
  100.     local sFile = fs.open(filename, "w")
  101.     local worldlen = 0
  102.     local worldmid = nil
  103.     for i, v in pairs(world) do
  104.         worldlen = worldlen + 1
  105.         if i == 0 then
  106.             worldmid = worldlen
  107.         end
  108.     end
  109.     for x = -(worldlen - worldmid), worldmid - 1 do
  110.     sFile.write("  [" .. tostring(x) .. "] = {")
  111.                 for y = 1, 100 do
  112.                         sFile.write("\"" .. world[x][y] .. "\"")
  113.                         if y == 100 and x == worldmid - 1 then
  114.                                 sFile.writeLine("}")
  115.                         elseif y == 100 then
  116.                             sFile.writeLine("},")
  117.             else
  118.                                 sFile.write(", ")
  119.                         end
  120.                 end
  121.         sFile.flush()
  122.     end
  123.     sFile.close()
  124. end
  125.  
  126. term.setBackgroundColor(colors.black)
  127. term.clear()
  128. term.setCursorPos(midW - 4, 2)
  129. term.write("TerrariLua")
  130. term.setCursorPos(midW - 5, 8)
  131. term.write("Start  Game")
  132. term.setCursorPos(midW - 4, 10)
  133. term.write("Exit Game")
  134. term.setBackgroundColor(colors.blue)
  135. while true do
  136.     local events = {os.pullEvent()}
  137.     if events[1] == "mouse_click" and events[2] == 1 then
  138.         if events[3] >= midW - 5 and events[3] <= midW + 5 and events[4] == 8 then
  139.             term.setCursorPos(midW - 5, 8)
  140.             term.write("Start  Game")
  141.             sleep(.2)
  142.             break
  143.         end
  144.         if events[3] >= midW - 4 and events[3] <= midW + 4 and events[4] == 10 then
  145.             term.setCursorPos(midW - 4, 10)
  146.             term.write("Exit Game")
  147.             sleep(.2)
  148.             os.exit()
  149.         end
  150.     end
  151. end
  152. term.setBackgroundColor(colors.black)
  153. term.clear()
  154. term.setCursorPos(midW - 4, 2)
  155. term.write("Start Menu")
  156. term.setCursorPos(midW - 4, 8)
  157. term.write("New  Game")
  158. term.setCursorPos(midW - 4, 10)
  159. term.write("Load Game")
  160. term.setBackgroundColor(colors.blue)
  161. local startUp = nil
  162. while true do
  163.     local events = {os.pullEvent()}
  164.     if events[1] == "mouse_click" and events[2] == 1 then
  165.         if events[3] >= midW - 4 and events[3] <= midW + 4 and events[4] == 8 then
  166.             term.setCursorPos(midW - 4, 8)
  167.             term.write("New  Game")
  168.             startUp = "new"
  169.             sleep(.2)
  170.             break
  171.         end
  172.         if events[3] >= midW - 4 and events[3] <= midW + 4 and events[4] == 10 then
  173.             term.setCursorPos(midW - 4, 10)
  174.             term.write("Load Game")
  175.             startUp = "load"
  176.             sleep(.2)
  177.             break
  178.         end
  179.     end
  180. end
  181. term.setBackgroundColor(colors.black)
  182. term.clear()
  183. term.setCursorPos(1, 1)
  184. term.write("Input World Name: ")
  185. local filename = nil
  186. if startUp == "new" then
  187.     filename = tostring(read())
  188.     generateWorld(filename)
  189.     loadWorld(filename)
  190. elseif startUp == "load" then
  191.     filename = tostring(read())
  192.     loadWorld(filename)
  193. end
  194. while true do
  195.     updateWorld()
  196.     displayWorld()
  197.     term.setCursorPos(1, 1)
  198.     term.write(tostring(xPos) .. "," .. tostring(yPos))
  199.     sleep(.05)
  200.     local events = {os.pullEvent()}
  201.     if events[1] == "key" then
  202.         if events[2] == 200 then --up
  203.             pDir = "^"
  204.             if world[xPos][yPos + 1] == "air" then
  205.                 yPos = yPos + 1
  206.             end
  207.         elseif events[2] == 205 then --right
  208.             pDir = ">"
  209.             if world[xPos - 1][yPos] == "air" then
  210.                 xPos = xPos - 1
  211.             end
  212.         elseif events[2] == 208 then --down
  213.             pDir = "v"
  214.             if world[xPos][yPos - 1] == "air" then
  215.                 yPos = yPos - 1
  216.             end
  217.         elseif events[2] == 203 then --left
  218.             pDir = "<"
  219.             if world[xPos + 1][yPos] == "air" then
  220.                 xPos = xPos + 1
  221.             end
  222.         end
  223.         if events[2] == 17 then
  224.             pDir = "^"
  225.         elseif events[2] == 30 then
  226.             pDir = "<"
  227.         elseif events[2] == 31 then
  228.             pDir = "v"
  229.         elseif events[2] == 32 then
  230.             pDir = ">"
  231.         end
  232.         if events[2] == 45 then
  233.             local changeB = "air"
  234.             local oTemp = fs.open("temp", "w")
  235.             local changeX, changeY = nil, nil
  236.             local ready = false
  237.             if pDir == "^" then
  238.                 changeX, changeY = xPos, yPos + 1
  239.             elseif pDir == ">" then
  240.                 changeX, changeY = xPos - 1, yPos
  241.             elseif pDir == "v" then
  242.                 changeX, changeY = xPos, yPos - 1
  243.             elseif pDir == "<" then
  244.                 changeX, changeY = xPos + 1, yPos
  245.             end
  246.             if world[changeX][changeY] ~= "bedrock" then
  247.                 world[changeX][changeY] = "air"
  248.             end
  249.         end
  250.         if events[2] == 46 then
  251.             local changeB = "stone"
  252.             local oTemp = fs.open("temp", "w")
  253.             local changeX, changeY = nil, nil
  254.             local ready = false
  255.             if pDir == "^" then
  256.                 changeX, changeY = xPos, yPos + 1
  257.             elseif pDir == ">" then
  258.                 changeX, changeY = xPos - 1, yPos
  259.             elseif pDir == "v" then
  260.                 changeX, changeY = xPos, yPos - 1
  261.             elseif pDir == "<" then
  262.                 changeX, changeY = xPos + 1, yPos
  263.             end
  264.             if world[changeX][changeY] == "air" then
  265.                 world[changeX][changeY] = "stone"
  266.             end
  267.         end
  268.         if events[2] == 29 then
  269.             saveWorld(filename)
  270.         end
  271.     end    
  272. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement