Advertisement
t4ggno

CC:Tweaked - Placer

Mar 16th, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- =============================================================
  2. -- == Predefine variables
  3. local WIDTH = 0
  4. local DEPTH = 0
  5. local MEASURE = false;
  6. local INVENTORY_SIZE = 16
  7.  
  8. -- List of accepted fuels
  9. local ACCEPTED_FUELS = {"minecraft:coal_block", "minecraft:coal", "minecraft:charcoal"}
  10.  
  11. -- List of blacklisted items
  12. local BLACKLIST_ITEMS = {"minecraft:torch"}
  13.  
  14. -- =============================================================
  15. -- == Argument parsing
  16.  
  17. -- Receive arguments and perform some basic validation
  18. if #arg == 2 then
  19.     WIDTH = tonumber(arg[1])
  20.     DEPTH = tonumber(arg[2])
  21. elseif #arg == 1 then
  22.     MEASURE = true;
  23.     DEPTH = tonumber(arg[2])
  24. else
  25.     MEASURE = true;
  26. end
  27.  
  28. -- =============================================================
  29. -- == Functions
  30.  
  31. function Refuel(once)
  32.     if not once then
  33.         once = false
  34.     end
  35.     local fuelLimit = turtle.getFuelLimit()
  36.     print("Fuel-Limit: " .. fuelLimit)
  37.     while true do
  38.  
  39.         print("Current fuel level: " .. turtle.getFuelLevel())
  40.         if turtle.getFuelLevel() < fuelLimit / 2 then
  41.             print("Refuel...")
  42.             for i = 1, INVENTORY_SIZE do
  43.                 local currentItem = turtle.getItemDetail(i)
  44.                 if currentItem ~= nil then
  45.                     local foundInFuelList = false
  46.                     for x = 1, #ACCEPTED_FUELS do
  47.                         if currentItem.name == ACCEPTED_FUELS[x] then
  48.                             foundInFuelList = true
  49.                             break
  50.                         end
  51.                     end
  52.                     if foundInFuelList then
  53.                         turtle.select(i)
  54.                         if turtle.refuel(0) then
  55.                             turtle.refuel()
  56.                         end
  57.                     end
  58.                 end
  59.             end
  60.             turtle.select(1)
  61.             print("New fuel level: " .. turtle.getFuelLevel())
  62.         end
  63.  
  64.         if (once) then
  65.             break
  66.         else
  67.             sleep(5)
  68.         end
  69.     end
  70. end
  71.  
  72. -- Place Sequence
  73. function PlaceSequence()
  74.     for depth = 1, DEPTH do
  75.  
  76.         if depth ~= 1 then
  77.             turtle.forward()
  78.         end
  79.  
  80.         -- Rotate turtle depending on depth
  81.         if depth % 2 == 0 then
  82.             turtle.turnLeft()
  83.         else
  84.             turtle.turnRight()
  85.         end
  86.  
  87.         for width = 1, WIDTH - 1 do
  88.  
  89.             Refuel(true)
  90.  
  91.             if turtle.detectDown() then
  92.                 turtle.digDown()
  93.             end
  94.  
  95.             local foundAtIndex = nil
  96.             for i = 1, INVENTORY_SIZE do
  97.                 local currentItem = turtle.getItemDetail(i)
  98.                 if currentItem ~= nil then
  99.  
  100.                     local foundInBlacklist = false
  101.  
  102.                     for x = 1, #BLACKLIST_ITEMS do
  103.                         if currentItem.name == BLACKLIST_ITEMS[x] then
  104.                             foundInBlacklist = true
  105.                             break
  106.                         end
  107.                     end
  108.  
  109.                     for x = 1, #ACCEPTED_FUELS do
  110.                         if currentItem.name == ACCEPTED_FUELS[x] then
  111.                             foundInBlacklist = true
  112.                             break
  113.                         end
  114.                     end
  115.  
  116.                     if not foundInBlacklist then
  117.                         foundAtIndex = i
  118.                         break
  119.                     end
  120.                 end
  121.             end
  122.  
  123.             if (foundAtIndex == nil) then
  124.                 error("[ERROR] No block found to place")
  125.             end
  126.  
  127.             turtle.select(foundAtIndex)
  128.             if not turtle.placeDown() then
  129.                 error("[ERROR] Failed to place block :/")
  130.             end
  131.             turtle.forward()
  132.  
  133.             if width % 4 == 0 and depth % 2 == 1 and (depth - 1) % 4 == 0 then
  134.                 print("Try to place torch...")
  135.                 local found = false;
  136.                 for inventoryPosition = 1, INVENTORY_SIZE do
  137.                     local currentItem = turtle.getItemDetail(inventoryPosition)
  138.                     if currentItem ~= nil and currentItem.name == "minecraft:torch" then
  139.                         found = true
  140.                         turtle.select(inventoryPosition)
  141.                         turtle.turnLeft()
  142.                         turtle.turnLeft()
  143.                         turtle.place()
  144.                         turtle.turnRight()
  145.                         turtle.turnRight()
  146.                         turtle.select(1)
  147.                         break
  148.                     end
  149.                 end
  150.                 if not found then
  151.                     print("[WARN] No torch found :(")
  152.                 end
  153.             end
  154.         end
  155.  
  156.         -- Rotate turtle back depending on depth
  157.         if depth % 2 == 0 then
  158.             turtle.turnRight()
  159.         else
  160.             turtle.turnLeft()
  161.         end
  162.     end
  163. end
  164.  
  165. -- =============================================================
  166. -- == Refuel before start
  167.  
  168. Refuel(true)
  169.  
  170. -- =============================================================
  171. -- == Measue
  172.  
  173. if MEASURE then
  174.     Refuel(true)
  175.     print("Start measuring...")
  176.  
  177.     -- Measure width
  178.     turtle.turnRight()
  179.     while turtle.forward() == true do
  180.         WIDTH = WIDTH + 1;
  181.     end
  182.     turtle.turnLeft()
  183.     turtle.turnLeft()
  184.     for width = 1, WIDTH do
  185.         if turtle.forward() == false then
  186.             error("[MEASURE] Failed to move back")
  187.         end
  188.     end
  189.     WIDTH = WIDTH + 1
  190.     print("Width: " .. WIDTH)
  191.     turtle.turnRight()
  192.  
  193.     -- Measure Depth
  194.     while turtle.forward() == true do
  195.         DEPTH = DEPTH + 1;
  196.     end
  197.     turtle.turnLeft()
  198.     turtle.turnLeft()
  199.     for depth = 1, DEPTH do
  200.         if turtle.forward() == false then
  201.             error("[MEASURE] Failed to move back")
  202.         end
  203.     end
  204.     DEPTH = DEPTH + 1
  205.     print("Depth: " .. DEPTH)
  206.     turtle.turnRight()
  207.     turtle.turnRight()
  208. end
  209.  
  210. -- =============================================================
  211. -- == Validate values
  212.  
  213. if WIDTH <= 0 or DEPTH <= 0 then
  214.     print("Both the height and width arguments must be greater and 1")
  215.     return
  216. end
  217.  
  218. -- =============================================================
  219. -- == Execute mining
  220.  
  221. -- =============================================================
  222. -- == Execute mining
  223.  
  224. -- Refuel
  225. Refuel(true)
  226.  
  227. -- Start mining loop
  228. PlaceSequence()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement