Advertisement
Guest User

clear_room

a guest
Oct 23rd, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. sleep(1)
  2.  
  3. local tArgs = { ... }
  4. if #tArgs ~= 3 then
  5.     print( "Usage: clear_room <length> <width> <height>" )
  6.     return
  7. end
  8.  
  9. local length = tonumber(tArgs[1])
  10. local width = tonumber(tArgs[2])
  11. local height = tonumber(tArgs[3])
  12. directions = {"north", "east", "south", "west"}
  13.  
  14. x_pos, y_pos, z_pos, direction = 0, 0, 0, 1
  15.  
  16. local function turn_to(d)
  17.  while direction ~= d do
  18.   turtle.turnRight()
  19.   direction = (direction % 4) + 1
  20.  end
  21. end
  22.  
  23. local function go_to_position(x, y, z, d)
  24.  
  25.  while z_pos ~= z do
  26.   if z_pos > z then
  27.     turtle.down()
  28.     z_pos = z_pos - 1;
  29.   else
  30.     turtle.up()
  31.     z_pos = z_pos + 1;
  32.   end
  33.  end
  34.  
  35.  if x_pos > x then
  36.   turn_to(4)
  37.   while x_pos ~= x do
  38.     turtle.forward()
  39.     x_pos = x_pos - 1;
  40.   end
  41.  else
  42.   turn_to(2)
  43.   while x_pos ~= x do
  44.     turtle.forward()
  45.     x_pos = x_pos + 1;
  46.   end
  47.  end
  48.  
  49.  if y_pos > y then
  50.   turn_to(3)
  51.   while y_pos ~= y do
  52.     turtle.forward()
  53.     y_pos = y_pos - 1;
  54.   end
  55.  else
  56.   turn_to(1)
  57.   while y_pos ~= y do
  58.     turtle.forward()
  59.     y_pos = y_pos + 1;
  60.   end
  61.  end
  62.  
  63.  turn_to(d)
  64. end
  65.  
  66. local function empty_inventory()
  67.   for i = 1, 9 do
  68.     turtle.select(i)
  69.     turtle.drop()
  70.   end
  71. end
  72.  
  73. local function check_inventory()
  74.  
  75.   local open_slots = 0
  76.   for i = 1, 9 do
  77.     local number_items = turtle.getItemCount(i)
  78.     if number_items == 0 then
  79.       open_slots = open_slots + 1;
  80.     end
  81.   end
  82.  
  83.   return open_slots;
  84.  
  85. end
  86.  
  87. local function dig_column(height)
  88.  
  89.   -- Dig from the top down
  90.   turtle.dig()
  91.   for z = 1, height-1 do
  92.     turtle.down()
  93.     z_pos = z_pos - 1
  94.     turtle.dig()
  95.     print("I'm at (" .. x_pos .. "," .. y_pos .. "," .. z_pos .. ")")
  96.   end
  97.  
  98.   -- Wait a second for any falling sand/gravel
  99.   sleep(2)
  100.  
  101.   -- Dig any remaining sand/gravel
  102.   while turtle.detect() do
  103.     turtle.dig()
  104.     sleep(1)
  105.   end
  106.  
  107.   -- Move back up
  108.   for z = 1, height-1 do
  109.     turtle.up()
  110.     z_pos = z_pos + 1
  111.     print("I'm at (" .. x_pos .. "," .. y_pos .. "," .. z_pos .. ")")
  112.   end
  113.  
  114.   -- If there's only one open slot left, let's go drop off my stuff
  115.   local open_slots = check_inventory()
  116.   if open_slots == 1 then
  117.     print("All full up. Dropping off items.");
  118.    
  119.     local return_x, return_y, return_z, direction = x_pos, y_pos, z_pos, direction
  120.     go_to_position(0, 0, 0, 3)
  121.     empty_inventory()
  122.     go_to_position(return_x, return_y, return_z, direction)
  123.     -- empty_inventory()
  124.   end
  125.  
  126. end
  127.  
  128. go_to_position(x_pos, y_pos, height-1, direction)
  129. for x = 1, width do
  130.    
  131.   -- Turtle always starts inside the room, so go one less than
  132.   -- you would expect
  133.   for y = 1, length-1 do
  134.     dig_column(height)
  135.     turtle.forward()
  136.     if x % 2 == 1 then
  137.       y_pos = y_pos + 1
  138.     else
  139.       y_pos = y_pos - 1
  140.     end
  141.   end
  142.  
  143.   if(x < width) then -- Stops turtle at top left corner of room
  144.     -- Turn the proper direction, dig the column out, then face the new
  145.     -- row in preparation to start clearing it
  146.     if x % 2 == 1 then
  147.       turtle.turnRight()
  148.       direction = (direction % 4) + 1
  149.       dig_column(height)
  150.       turtle.forward()
  151.       x_pos = x_pos + 1
  152.       turtle.turnRight()
  153.       direction = (direction % 4) + 1
  154.     else
  155.       turtle.turnLeft()
  156.       direction = (direction % 4) - 1
  157.       dig_column(height)
  158.       turtle.forward()
  159.       x_pos = x_pos + 1
  160.       turtle.turnLeft()
  161.       direction = (direction % 4) - 1
  162.     end
  163.   end
  164.  
  165. end
  166. go_to_position(0, 0, 0, 3)
  167. empty_inventory()
  168. go_to_position(0, 0, 0, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement