Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sleep(1)
- local tArgs = { ... }
- if #tArgs ~= 3 then
- print( "Usage: clear_room <length> <width> <height>" )
- return
- end
- local length = tonumber(tArgs[1])
- local width = tonumber(tArgs[2])
- local height = tonumber(tArgs[3])
- directions = {"north", "east", "south", "west"}
- x_pos, y_pos, z_pos, direction = 0, 0, 0, 1
- local function turn_to(d)
- while direction ~= d do
- turtle.turnRight()
- direction = (direction % 4) + 1
- end
- end
- local function go_to_position(x, y, z, d)
- while z_pos ~= z do
- if z_pos > z then
- turtle.down()
- z_pos = z_pos - 1;
- else
- turtle.up()
- z_pos = z_pos + 1;
- end
- end
- if x_pos > x then
- turn_to(4)
- while x_pos ~= x do
- turtle.forward()
- x_pos = x_pos - 1;
- end
- else
- turn_to(2)
- while x_pos ~= x do
- turtle.forward()
- x_pos = x_pos + 1;
- end
- end
- if y_pos > y then
- turn_to(3)
- while y_pos ~= y do
- turtle.forward()
- y_pos = y_pos - 1;
- end
- else
- turn_to(1)
- while y_pos ~= y do
- turtle.forward()
- y_pos = y_pos + 1;
- end
- end
- turn_to(d)
- end
- local function empty_inventory()
- for i = 1, 9 do
- turtle.select(i)
- turtle.drop()
- end
- end
- local function check_inventory()
- local open_slots = 0
- for i = 1, 9 do
- local number_items = turtle.getItemCount(i)
- if number_items == 0 then
- open_slots = open_slots + 1;
- end
- end
- return open_slots;
- end
- local function dig_column(height)
- -- Dig from the top down
- turtle.dig()
- for z = 1, height-1 do
- turtle.down()
- z_pos = z_pos - 1
- turtle.dig()
- print("I'm at (" .. x_pos .. "," .. y_pos .. "," .. z_pos .. ")")
- end
- -- Wait a second for any falling sand/gravel
- sleep(2)
- -- Dig any remaining sand/gravel
- while turtle.detect() do
- turtle.dig()
- sleep(1)
- end
- -- Move back up
- for z = 1, height-1 do
- turtle.up()
- z_pos = z_pos + 1
- print("I'm at (" .. x_pos .. "," .. y_pos .. "," .. z_pos .. ")")
- end
- -- If there's only one open slot left, let's go drop off my stuff
- local open_slots = check_inventory()
- if open_slots == 1 then
- print("All full up. Dropping off items.");
- local return_x, return_y, return_z, direction = x_pos, y_pos, z_pos, direction
- go_to_position(0, 0, 0, 3)
- empty_inventory()
- go_to_position(return_x, return_y, return_z, direction)
- -- empty_inventory()
- end
- end
- go_to_position(x_pos, y_pos, height-1, direction)
- for x = 1, width do
- -- Turtle always starts inside the room, so go one less than
- -- you would expect
- for y = 1, length-1 do
- dig_column(height)
- turtle.forward()
- if x % 2 == 1 then
- y_pos = y_pos + 1
- else
- y_pos = y_pos - 1
- end
- end
- if(x < width) then -- Stops turtle at top left corner of room
- -- Turn the proper direction, dig the column out, then face the new
- -- row in preparation to start clearing it
- if x % 2 == 1 then
- turtle.turnRight()
- direction = (direction % 4) + 1
- dig_column(height)
- turtle.forward()
- x_pos = x_pos + 1
- turtle.turnRight()
- direction = (direction % 4) + 1
- else
- turtle.turnLeft()
- direction = (direction % 4) - 1
- dig_column(height)
- turtle.forward()
- x_pos = x_pos + 1
- turtle.turnLeft()
- direction = (direction % 4) - 1
- end
- end
- end
- go_to_position(0, 0, 0, 3)
- empty_inventory()
- go_to_position(0, 0, 0, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement