t4ggno

CC:Tweaked - Filler

Apr 29th, 2021 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.30 KB | None | 0 0
  1. -- =============================================================
  2. -- == Predefine variables
  3. local HEIGHT = 0
  4. local WIDTH = 0
  5. local DEPTH = 0
  6. local MEASURE = false;
  7. local INVENTORY_SIZE = 16
  8. local CHECK_INVENTORY_EVERY = 20
  9.  
  10. -- List of accepted fuels
  11. local ACCEPTED_FUELS = {"minecraft:coal_block", "minecraft:coal", "minecraft:charcoal"}
  12.  
  13. -- List of accepted walls
  14. local ACCEPTED_WALLS = {"minecraft:planks"}
  15.  
  16. -- List of accepted filler
  17. local ACCEPTED_FILLER = {"minecraft:dirt", "minecraft:cobblestone"}
  18.  
  19. -- =============================================================
  20. -- == Argument parsing
  21.  
  22. -- Receive arguments and perform some basic validation
  23. if #arg == 3 then
  24.     HEIGHT = tonumber(arg[1])
  25.     WIDTH = tonumber(arg[2])
  26.     DEPTH = tonumber(arg[3])
  27. end
  28.  
  29. -- filler 7 12 12
  30. -- Wall: 508 = ~8
  31. -- Fill: 1176 = ~8
  32.  
  33. -- =============================================================
  34. -- == Functions
  35.  
  36. local function IndexOf(list, object)
  37.     local result
  38.     if type(list) == "table" then
  39.         for i = 1, #list do
  40.             -- print(object .. " " .. list[i] .. " " .. tostring(object == list[i]))
  41.             if object == list[i] then
  42.                 result = i
  43.                 break
  44.             end
  45.         end
  46.     end
  47.     return result
  48. end
  49.  
  50. local function Refuel(once)
  51.     if not once then
  52.         once = false
  53.     end
  54.     local fuelLimit = turtle.getFuelLimit()
  55.     print("Fuel-Limit: " .. fuelLimit)
  56.     while true and not once do
  57.         print("Current fuel level: " .. turtle.getFuelLevel())
  58.         if turtle.getFuelLevel() < fuelLimit / 2 then
  59.             print("Refuel...")
  60.             for inventoryPosition = 1, INVENTORY_SIZE do
  61.                 local currentItem = turtle.getItemDetail(inventoryPosition)
  62.                 if currentItem ~= nil then
  63.                     local indexOf = IndexOf(ACCEPTED_FUELS, currentItem.name)
  64.                     if indexOf ~= nil then
  65.                         turtle.select(indexOf)
  66.                         if turtle.refuel(0) then
  67.                             turtle.refuel()
  68.                         else
  69.                             error(currentItem.name .. " is not a valid fuel material")
  70.                         end
  71.                     end
  72.                 end
  73.             end
  74.             print("New fuel level: " .. turtle.getFuelLevel())
  75.         end
  76.         if not once then
  77.             sleep(5)
  78.         end
  79.     end
  80. end
  81.  
  82. -- Move and dig
  83. local function MoveUpAndDig()
  84.     while turtle.up() == false do
  85.         turtle.digUp()
  86.     end
  87. end
  88.  
  89. local function MoveForwardAndDig()
  90.     while turtle.forward() == false do
  91.         turtle.dig()
  92.     end
  93. end
  94.  
  95. local function MoveDownAndDig()
  96.     while turtle.down() == false do
  97.         turtle.digDown()
  98.     end
  99. end
  100.  
  101. -- Mining Sequence
  102. local function MineSequence()
  103.     for depth = 1, DEPTH do
  104.  
  105.         -- Move on forward
  106.         if depth ~= 1 then
  107.             MoveForwardAndDig()
  108.  
  109.             if depth % 2 == 0 then
  110.                 MoveDownAndDig()
  111.             else
  112.                 MoveUpAndDig()
  113.             end
  114.         end
  115.  
  116.         -- For every height
  117.         for height = 1, HEIGHT do
  118.  
  119.             if depth % 2 == 0 then
  120.                 MoveDownAndDig()
  121.             else
  122.                 MoveUpAndDig()
  123.             end
  124.  
  125.             -- Rotate turtle depending on height
  126.             if height % 2 == 0 then
  127.                 turtle.turnLeft()
  128.             else
  129.                 turtle.turnRight()
  130.             end
  131.  
  132.             for width = 1, WIDTH do
  133.  
  134.                 -- Predefine variable
  135.                 local wall
  136.                 local placed = false
  137.                 local toPlaceIndex
  138.  
  139.                 -- Check if wall or fill
  140.                 if depth == 1 or depth == DEPTH or height == 1 or height == HEIGHT or width == 1 or width == WIDTH then
  141.                     wall = true
  142.                 else
  143.                     wall = false
  144.                 end
  145.  
  146.                 -- Check inventory for element to place
  147.                 for inventoryPosition = 1, INVENTORY_SIZE do
  148.                     local currentItem = turtle.getItemDetail(inventoryPosition)
  149.                     local indexOf;
  150.                     if currentItem ~= nil then
  151.                         if wall then
  152.                             indexOf = IndexOf(ACCEPTED_WALLS, currentItem.name)
  153.                             if indexOf == nil then
  154.                                 currentItem = turtle.getItemDetail(inventoryPosition, true)
  155.                                 for currentTag in pairs(currentItem.tags) do
  156.                                     indexOf = IndexOf(ACCEPTED_WALLS, currentTag)
  157.                                     if indexOf ~= nil then
  158.                                         toPlaceIndex = inventoryPosition;
  159.                                         break
  160.                                     end
  161.                                 end
  162.                             else
  163.                                 toPlaceIndex = inventoryPosition;
  164.                             end
  165.                         else
  166.                             indexOf = IndexOf(ACCEPTED_FILLER, currentItem.name)
  167.                             if indexOf == nil and false then
  168.                                 currentItem = turtle.getItemDetail(inventoryPosition, true)
  169.                                 for currentTag in pairs(currentItem.tags) do
  170.                                     indexOf = IndexOf(ACCEPTED_FILLER, currentTag)
  171.                                     if indexOf ~= nil then
  172.                                         toPlaceIndex = inventoryPosition;
  173.                                         break
  174.                                     end
  175.                                 end
  176.                             else
  177.                                 toPlaceIndex = inventoryPosition;
  178.                             end
  179.                         end
  180.                     end
  181.                     if toPlaceIndex ~= nill then
  182.                         break
  183.                     end
  184.                 end
  185.  
  186.                 if toPlaceIndex == nil then
  187.                     if depth == 1 or depth == DEPTH or height == 1 or height == HEIGHT or width == 1 or width == WIDTH then
  188.                         error("Missing material: Wall")
  189.                     else
  190.                         error("Missing material: Fill")
  191.                     end
  192.                 end
  193.  
  194.                 if (toPlaceIndex ~= nil) then
  195.                     turtle.select(toPlaceIndex)
  196.                     if depth % 2 == 0 then
  197.                         if turtle.placeUp() then
  198.                             placed = true
  199.                         end
  200.                     else
  201.                         if turtle.placeDown() then
  202.                             placed = true
  203.                         end
  204.                     end
  205.                 end
  206.  
  207.                 if not placed then
  208.                     if depth == 1 or depth == DEPTH or height == 1 or height == HEIGHT or width == 1 or width == WIDTH then
  209.                         error("Something went wrong: Wall")
  210.                     else
  211.                         error("Something went wrong: Fill")
  212.                     end
  213.                 end
  214.  
  215.                 if width ~= WIDTH then
  216.                     MoveForwardAndDig()
  217.                 end
  218.             end
  219.  
  220.             -- Rotate turtle back depending on height
  221.             if height % 2 == 0 then
  222.                 turtle.turnRight()
  223.             else
  224.                 turtle.turnLeft()
  225.             end
  226.         end
  227.     end
  228. end
  229.  
  230. -- =============================================================
  231. -- == Validate values
  232.  
  233. if WIDTH <= 0 or HEIGHT <= 0 or DEPTH <= 0 then
  234.     print("Both the height and width arguments must be greater and 1")
  235.     return
  236. end
  237.  
  238. if HEIGHT % 2 == 1 then
  239.     print("The height should not be odd")
  240.     return
  241. end
  242.  
  243. -- =============================================================
  244. -- == Output every inventory item name
  245.  
  246. for inventoryPosition = 1, INVENTORY_SIZE do
  247.     local currentItem = turtle.getItemDetail(inventoryPosition, true)
  248.     if currentItem ~= nil then
  249.         -- print(textutils.serialize(currentItem))
  250.         -- print(currentItem.name)
  251.     end
  252. end
  253.  
  254. -- =============================================================
  255. -- == Execute mining
  256.  
  257. -- Refuel
  258. Refuel(true)
  259.  
  260. -- Start mining loop
  261. MineSequence()
  262. -- parallel.waitForAny(MineSequence, Refuel)
  263.  
Advertisement
Add Comment
Please, Sign In to add comment