Advertisement
HandieAndy

FillLine

May 11th, 2019
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. local ms = require("movescript")
  2. local component = require("component")
  3. local robot = require("robot")
  4. local ic = component.inventory_controller
  5.  
  6. local BLOCK_NAME = "minecraft:dirt"
  7. local BLOCK_DATA = 0
  8.  
  9. local arg = {...}
  10.  
  11. --[[
  12. Select an item, given its name and damage value.
  13. item_name - string: The id string for an item.
  14. item_data - number: The damage value, or variation of an item. Defaults to zero.
  15. return - boolean: True if at least one slot contains the item. That slot is now
  16. selected.
  17. --]]
  18. local function selectItemByName(item_name, item_data)
  19.     for i=1,16 do
  20.         local stack = ic.getStackInInternalSlot(i)
  21.         if (stack ~= nil and stack.name == item_name and stack.damage == item_data) then
  22.             robot.select(i)
  23.             return true
  24.         end
  25.     end
  26.     return false
  27. end
  28.  
  29. --[[
  30. Select an item, similar to selectItemByName, but if the item cannot be found,
  31. the user will be prompted to add it to the robot's inventory and press enter to
  32. continue.
  33. item_name - string: The id string for an item.
  34. item_data - number: The damage value, or variation of an item. Defaults to zero.
  35. return - nil: If set to be continuous, then if the item cannot be found, then
  36. the program will exit. If not, it will loop until the item is provided by the
  37. user.
  38. --]]
  39. local function selectSafely(item_name, item_data)
  40.     local success = selectItemByName(item_name, item_data)
  41.     while not success do
  42.         print("Cannot find "..item_name.." in inventory. Please add some, and press enter.")
  43.         io.read()
  44.         success = selectItemByName(item_name, item_data)
  45.     end
  46. end
  47.  
  48. local function flattenSpot()
  49.     -- Dig out top part.
  50.     local displacement = 0
  51.     local success, data = robot.detectUp()
  52.     while (success) do
  53.         ms.execute("d_U")
  54.         success, data = robot.detectUp()
  55.         displacement = displacement + 1
  56.     end
  57.     ms.execute(displacement .. "D")
  58.  
  59.     -- Fill in floor.
  60.     displacement = 0
  61.     success, data = robot.detectDown()
  62.     while ((not success) or (data == "replaceable")) do
  63.         ms.execute("d_D")
  64.         success, data = robot.detectDown()
  65.         displacement = displacement + 1
  66.     end
  67.     for i = 1, displacement do
  68.         ms.execute("U")
  69.         selectSafely(BLOCK_NAME, BLOCK_DATA)
  70.         robot.placeDown()
  71.     end
  72. end
  73.  
  74. local function fillLine(length)
  75.     for i = 1, length do
  76.         ms.execute("d_F")
  77.         flattenSpot()
  78.     end
  79. end
  80.  
  81. local function mainMenu()
  82.     print("[1] Use " .. BLOCK_NAME .. ":" .. BLOCK_DATA .. " as default.")
  83.     print("[2] Define your own block to use.")
  84.     choice = tonumber(io.read())
  85.     if choice == 2 then
  86.         print("What is the name of the block?")
  87.         BLOCK_NAME = io.read()
  88.         print("What is the data value?")
  89.         BLOCK_DATA = tonumber(io.read())
  90.     end
  91.  
  92.     print("What is the length of the area to fill? (How far forward)")
  93.     length = tonumber(io.read())
  94.  
  95.     fillLine(length)
  96. end
  97.  
  98. local function parseArgs()
  99.     if (arg[1] ~= nil and arg[2] ~= nil and arg[3] ~= nil) then
  100.         BLOCK_NAME = arg[1]
  101.         BLOCK_DATA = tonumber(arg[2])
  102.         return tonumber(arg[3])
  103.     else
  104.         return false
  105.     end
  106. end
  107.  
  108. local length = parseArgs()
  109. if (not length) then
  110.     mainMenu()
  111. else
  112.     fillLine(length)
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement