Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- local src_url
- if #tArgs < 1 then
- print( "Usage: build [pastebin_url]" )
- return
- else
- src_url = tArgs[1]
- end
- local orientation = "north"
- local bot_x = 1
- local bot_y = 1
- local bot_z = 1
- local function get_plan_str()
- write( "Connecting to pastebin.com... " )
- local response = http.get(
- "https://pastebin.com/raw/"..textutils.urlEncode(src_url)
- )
- if response then
- print("Success.")
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- print("Failed.")
- return false
- end
- end
- local function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={} ; i=1
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- local function parse_meta_block(plan)
- local meta = split(plan, "#")[1]
- local dimensions = split(meta, ":")
- return dimensions[1], dimensions[2], dimensions[3]
- end
- local function parse_plan(plan_str)
- -- Starts with x:y:z#
- -- 0 = empty
- local x, y, z = parse_meta_block(plan)
- local plan = {}
- plan_str = split(split(plan_str, "#")[2], ",")
- local str_index = 1
- for l_y = 1, y do
- plan[l_y] = {}
- for l_z = 1, z do
- plan[l_y][l_z] = {}
- for l_x = 1, x do
- plan[l_y][l_z][l_x] = plan_str[str_index]
- str_index = str_index + 1
- end
- end
- end
- return plan
- end
- local function refuel(amount)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- if turtle.getFuelLevel() < amount then
- local fueled = false
- turtle.select(16)
- if turtle.getItemCount(16) > 0 then
- turtle.select(16)
- if turtle.refuel(1) then
- while turtle.getItemCount(16) > 0 and turtle.getFuelLevel() < amount do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= amount then
- turtle.select(1)
- return true
- end
- end
- end
- turtle.select(1)
- return false
- end
- return true
- end
- local function turn(direction, amount)
- for a = 1, amount do
- change_orientation(direction)
- if direction == "left" then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- end
- local function orient_to(direction)
- while orientation ~= direction do
- turn("left", 1)
- end
- end
- local function change_orientation(direction)
- if direction == "left" then
- if orientation == "north" then
- orientation = "east"
- elseif orientation == "east" then
- orientation = "south"
- elseif orientation == "south" then
- orientation = "west"
- else
- orientation = "north"
- end
- else
- if orientation == "north" then
- orientation = "west"
- elseif orientation == "east" then
- orientation = "north"
- elseif orientation == "south" then
- orientation = "east"
- else
- orientation = "south"
- end
- end
- end
- local function move_forward()
- if not refuel(1) then
- print("Waiting for fuel")
- while not refuel(1) do
- os.pullEvent("turtle_inventory")
- end
- end
- turtle.forward()
- if orientation == "north" then
- bot_z = bot_z - 1
- elseif orientation == "south" then
- bot_z = bot_z + 1
- elseif orientation == "east" then
- bot_x = bot_x + 1
- elseif orientation == "west" then
- bot_x = bot_x - 1
- end
- end
- local function has_block(number)
- for s = 1, 15 do
- if string.find(number, tostring(s)) ~= nil then
- turtle.select(s)
- if turtle.getItemCount() > 0 then
- return true
- end
- end
- end
- return false
- end
- local function select_block_x(number)
- if has_block(number) then
- return
- end
- print("Failed to find block for", number)
- while not has_block(number) do
- os.pullEvent("turtle_inventory")
- end
- end
- local function do_plan_block(number)
- if number == 0 then
- -- Empty
- return
- end
- select_block_x(number)
- turtle.placeDown()
- end
- local function buildZ(z_plan)
- local start_x = nil
- local end_x = nil
- if orientation == "east" then
- start_x = 1
- end_x = #z_plan
- else
- start_x = #z_plan
- end_x = 1
- end
- for l_x = start_x, end_x do
- do_plan_block(z_plan[l_x])
- end
- end
- local function buildY(y_plan)
- for l_z = 1, #y_plan do
- buildZ(y_plan[l_z])
- if l_z ~= #y_plan then
- if orientation == "east" then
- turn("right", 1)
- move_forward()
- turn("right", 1)
- elseif orientation == "west" then
- turn("left", 1)
- move_forward()
- turn("left", 1)
- end
- end
- end
- end
- local function move_up_y()
- -- Should be left at coords X,Z or 1,Z
- -- move to 1,1
- if bot_x > 1 then
- -- X = X, not X = 1
- orient_to("west")
- while bot_x > 1 do
- move_forward()
- end
- end
- orient_to("north")
- while bot_z > 1 do
- move_forward()
- end
- end
- local function move_to_start(plan)
- -- Move forward Z units
- local z = #plan[1]
- for l_z = 1, z do
- turtle.forward()
- end
- turn("right", 1)
- end
- local function build(plan)
- move_to_start(plan)
- for y = 1, #plan do
- buildY()
- if y ~= #plan then
- move_up_y()
- end
- end
- end
- local function init()
- local plan_str = get_plan_str()
- if not plan_str then
- return
- end
- build(parse_plan(plan_str))
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement