Advertisement
sanderronde

build.lua

Jan 14th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. local tArgs = { ... }
  2. local src_url
  3. if #tArgs < 1 then
  4.     print( "Usage: build [pastebin_url]" )
  5.     return
  6. else
  7.     src_url = tArgs[1]
  8. end
  9.  
  10. local orientation = "north"
  11. local bot_x = 1
  12. local bot_y = 1
  13. local bot_z = 1
  14.  
  15. local function get_plan_str()
  16.     write( "Connecting to pastebin.com... " )
  17.     local response = http.get(
  18.         "https://pastebin.com/raw/"..textutils.urlEncode(src_url)
  19.     )
  20.        
  21.     if response then
  22.         print("Success.")
  23.        
  24.         local sResponse = response.readAll()
  25.         response.close()
  26.         return sResponse
  27.     else
  28.         print("Failed.")
  29.         return false
  30.     end
  31. end
  32.  
  33. local function split(inputstr, sep)
  34.     if sep == nil then
  35.         sep = "%s"
  36.     end
  37.     local t={} ; i=1
  38.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  39.             t[i] = str
  40.             i = i + 1
  41.     end
  42.     return t
  43. end
  44.  
  45. local function parse_meta_block(plan)
  46.     local meta = split(plan, "#")[1]
  47.     local dimensions = split(meta, ":")
  48.     return dimensions[1], dimensions[2], dimensions[3]
  49. end
  50.  
  51. local function parse_plan(plan_str)
  52.     -- Starts with x:y:z#
  53.     -- 0 = empty
  54.     local x, y, z = parse_meta_block(plan)
  55.  
  56.     local plan = {}
  57.     plan_str = split(split(plan_str, "#")[2], ",")
  58.  
  59.     local str_index = 1
  60.     for l_y = 1, y do
  61.         plan[l_y] = {}
  62.         for l_z = 1, z do
  63.             plan[l_y][l_z] = {}
  64.             for l_x = 1, x do
  65.                 plan[l_y][l_z][l_x] = plan_str[str_index]
  66.                 str_index = str_index + 1
  67.             end
  68.         end
  69.     end
  70.     return plan
  71. end
  72.  
  73. local function refuel(amount)
  74.     local fuelLevel = turtle.getFuelLevel()
  75.     if fuelLevel == "unlimited" then
  76.         return true
  77.     end
  78.    
  79.     if turtle.getFuelLevel() < amount then
  80.         local fueled = false
  81.         turtle.select(16)
  82.         if turtle.getItemCount(16) > 0 then
  83.             turtle.select(16)
  84.             if turtle.refuel(1) then
  85.                 while turtle.getItemCount(16) > 0 and turtle.getFuelLevel() < amount do
  86.                     turtle.refuel(1)
  87.                 end
  88.                 if turtle.getFuelLevel() >= amount then
  89.                     turtle.select(1)
  90.                     return true
  91.                 end
  92.             end
  93.         end
  94.         turtle.select(1)
  95.         return false
  96.     end
  97.    
  98.     return true
  99. end
  100.  
  101. local function turn(direction, amount)
  102.     for a = 1, amount do
  103.         change_orientation(direction)
  104.         if direction == "left" then
  105.             turtle.turnLeft()
  106.         else
  107.             turtle.turnRight()
  108.         end
  109.     end
  110. end
  111.  
  112. local function orient_to(direction)
  113.     while orientation ~= direction do
  114.         turn("left", 1)
  115.     end
  116. end
  117.  
  118. local function change_orientation(direction)
  119.     if direction == "left" then
  120.         if orientation == "north" then
  121.             orientation = "east"
  122.         elseif orientation == "east" then
  123.             orientation = "south"
  124.         elseif orientation == "south" then
  125.             orientation = "west"
  126.         else
  127.             orientation = "north"
  128.         end
  129.     else
  130.         if orientation == "north" then
  131.             orientation = "west"
  132.         elseif orientation == "east" then
  133.             orientation = "north"
  134.         elseif orientation == "south" then
  135.             orientation = "east"
  136.         else
  137.             orientation = "south"
  138.         end
  139.     end
  140. end
  141.  
  142. local function move_forward()
  143.     if not refuel(1) then
  144.         print("Waiting for fuel")
  145.         while not refuel(1) do
  146.             os.pullEvent("turtle_inventory")
  147.         end
  148.     end
  149.  
  150.     turtle.forward()
  151.     if orientation == "north" then
  152.         bot_z = bot_z - 1
  153.     elseif orientation == "south" then
  154.         bot_z = bot_z + 1
  155.     elseif orientation == "east" then
  156.         bot_x = bot_x + 1
  157.     elseif orientation == "west" then
  158.         bot_x = bot_x - 1
  159.     end
  160. end
  161.  
  162. local function has_block(number)
  163.     for s = 1, 15 do
  164.         if string.find(number, tostring(s)) ~= nil then
  165.             turtle.select(s)
  166.             if turtle.getItemCount() > 0 then
  167.                 return true
  168.             end
  169.         end
  170.     end
  171.     return false
  172. end
  173.  
  174. local function select_block_x(number)
  175.     if has_block(number) then
  176.         return
  177.     end
  178.  
  179.     print("Failed to find block for", number)
  180.     while not has_block(number) do
  181.         os.pullEvent("turtle_inventory")
  182.     end
  183. end
  184.  
  185. local function do_plan_block(number)
  186.     if number == 0 then
  187.         -- Empty
  188.         return
  189.     end
  190.  
  191.     select_block_x(number)
  192.     turtle.placeDown()
  193. end
  194.  
  195. local function buildZ(z_plan)
  196.     local start_x = nil
  197.     local end_x = nil
  198.     if orientation == "east" then
  199.         start_x = 1
  200.         end_x = #z_plan
  201.     else
  202.         start_x = #z_plan
  203.         end_x = 1
  204.     end
  205.  
  206.     for l_x = start_x, end_x do
  207.         do_plan_block(z_plan[l_x])
  208.     end
  209. end
  210.  
  211. local function buildY(y_plan)
  212.     for l_z = 1, #y_plan do
  213.         buildZ(y_plan[l_z])
  214.  
  215.         if l_z ~= #y_plan then
  216.             if orientation == "east" then
  217.                 turn("right", 1)
  218.                 move_forward()
  219.                 turn("right", 1)
  220.             elseif orientation == "west" then
  221.                 turn("left", 1)
  222.                 move_forward()
  223.                 turn("left", 1)
  224.             end
  225.         end
  226.     end
  227. end
  228.  
  229. local function move_up_y()
  230.     -- Should be left at coords X,Z or 1,Z
  231.     -- move to 1,1
  232.  
  233.     if bot_x > 1 then
  234.         -- X = X, not X = 1
  235.         orient_to("west")
  236.         while bot_x > 1 do
  237.             move_forward()
  238.         end
  239.     end
  240.     orient_to("north")
  241.     while bot_z > 1 do
  242.         move_forward()
  243.     end
  244. end
  245.  
  246. local function move_to_start(plan)
  247.     -- Move forward Z units
  248.     local z = #plan[1]
  249.  
  250.     for l_z = 1, z do
  251.         turtle.forward()
  252.     end
  253.     turn("right", 1)
  254. end
  255.  
  256. local function build(plan)
  257.     move_to_start(plan)
  258.     for y = 1, #plan do
  259.         buildY()
  260.         if y ~= #plan then
  261.             move_up_y()
  262.         end
  263.     end
  264. end
  265.  
  266. local function init()
  267.     local plan_str = get_plan_str()
  268.     if not plan_str then
  269.         return
  270.     end
  271.  
  272.     build(parse_plan(plan_str))
  273. end
  274.  
  275. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement