hhhzzzsss

tradinghall.lua

Aug 16th, 2023 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.76 KB | None | 0 0
  1. if not turtle then
  2.     printError("Requires a Turtle")
  3.     return
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs ~= 1 then
  8.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  9.     print("Usage: " .. programName .. " <num units>")
  10.     return
  11. end
  12.  
  13. local numUnits = tonumber(tArgs[1])
  14.  
  15. local function fastFail(success)
  16.     if not success then
  17.         print("An unexpected error has occured! Aborting.")
  18.         exit()
  19.     end
  20. end
  21.  
  22. local function refuel(amount)
  23.     local fuelLevel = turtle.getFuelLevel()
  24.     if fuelLevel == "unlimited" then
  25.         return true
  26.     end
  27.  
  28.     turtle.select(16)
  29.     while turtle.getItemCount(16) > 0 and turtle.getFuelLevel() < amount do
  30.         if not turtle.refuel(1) then
  31.             return false
  32.         end
  33.     end
  34.  
  35.     if turtle.getFuelLevel() < amount then
  36.         return false
  37.     else
  38.         return true
  39.     end
  40. end
  41.  
  42. local function waitForFuel(amount)
  43.     if not refuel(amount) then
  44.         print("Please put fuel in slot 16")
  45.         while not refuel(amount) do
  46.             sleep(0.5)
  47.         end
  48.         print("Received sufficient fuel. Continuing...")
  49.     end
  50. end
  51.  
  52. local function selectItem(name)
  53.     for i=1, 15 do
  54.         itemDetail = turtle.getItemDetail(i)
  55.         if itemDetail and itemDetail.name == name then
  56.             turtle.select(i)
  57.             return true
  58.         end
  59.     end
  60.     return false
  61. end
  62.  
  63. local function countItem(name)
  64.     num = 0
  65.     for i=1, 15 do
  66.         itemDetail = turtle.getItemDetail(i)
  67.         if itemDetail and itemDetail.name == name then
  68.             num = num + itemDetail.count
  69.         end
  70.     end
  71.     return num
  72. end
  73.  
  74. local function waitForItem(name, amount)
  75.     if countItem(name) < amount then
  76.         print("Please provide more " .. name)
  77.         while countItem(name) < amount do
  78.             sleep(0.5)
  79.         end
  80.         print("Received sufficient " .. name .. ". Continuing...")
  81.     end
  82.     selectItem(name)
  83. end
  84.  
  85. local function waitForDrop()
  86.     if turtle.getItemCount() > 0 and not turtle.drop() then
  87.         print("Please make room for turtle to drop")
  88.         while turtle.getItemCount() > 0 and not turtle.drop() do
  89.             sleep(0.5)
  90.         end
  91.         print("Turtle successfully dropped. Continuing...")
  92.     end
  93. end
  94.  
  95. local function waitForDropDown()
  96.     if turtle.getItemCount() > 0 and not turtle.dropDown() then
  97.         print("Please make room for turtle to drop")
  98.         while turtle.getItemCount() > 0 and not turtle.dropDown() do
  99.             sleep(0.5)
  100.         end
  101.         print("Turtle successfully dropped. Continuing...")
  102.     end
  103. end
  104.  
  105. local function getBlock()
  106.     hasBlock, data = turtle.inspect()
  107.     if hasBlock then
  108.         return data.name
  109.     else
  110.         return "minecraft:air"
  111.     end
  112. end
  113.  
  114. local function getBlockDown()
  115.     hasBlock, data = turtle.inspectDown()
  116.     if hasBlock then
  117.         return data.name
  118.     else
  119.         return "minecraft:air"
  120.     end
  121. end
  122.  
  123. local function getBlockUp()
  124.     hasBlock, data = turtle.inspectUp()
  125.     if hasBlock then
  126.         return data.name
  127.     else
  128.         return "minecraft:air"
  129.     end
  130. end
  131.  
  132. local function tryForwards()
  133.     waitForFuel(1)
  134.     while not turtle.forward() do
  135.         if turtle.detect() then
  136.             if not turtle.dig() then
  137.                 return false
  138.             end
  139.         elseif not turtle.attack() then
  140.             sleep(0.5)
  141.         end
  142.     end
  143.     return true
  144. end
  145.  
  146. local function tryDown()
  147.     waitForFuel(1)
  148.     while not turtle.down() do
  149.         if turtle.detectDown() then
  150.             if not turtle.digDown() then
  151.                 return false
  152.             end
  153.         elseif not turtle.attackDown() then
  154.             sleep(0.5)
  155.         end
  156.     end
  157.     return true
  158. end
  159.  
  160. local function tryUp()
  161.     waitForFuel(1)
  162.     while not turtle.up() do
  163.         if turtle.detectUp() then
  164.             if not turtle.digUp() then
  165.                 return false
  166.             end
  167.         elseif not turtle.attackUp() then
  168.             sleep(0.5)
  169.         end
  170.     end
  171.     return true
  172. end
  173.  
  174. local function repeatForwards(amount)
  175.     for i=1, amount do
  176.         fastFail(tryForwards())
  177.     end
  178. end
  179.  
  180. local function repeatDown(amount)
  181.     for i=1, amount do
  182.         fastFail(tryDown())
  183.     end
  184. end
  185.  
  186. local function repeatUp(amount)
  187.     for i=1, amount do
  188.         fastFail(tryUp())
  189.     end
  190. end
  191.  
  192. local function stripForwards(amount)
  193.     for i=1, amount do
  194.         fastFail(tryForwards())
  195.         turtle.digDown()
  196.     end
  197. end
  198.  
  199. local function stripForwardsWithFloor(amount)
  200.     for i=1, amount do
  201.         fastFail(tryForwards())
  202.         turtle.digDown()
  203.         waitForItem("minecraft:cobblestone", 1)
  204.         turtle.placeDown()
  205.     end
  206. end
  207.  
  208. waitForFuel(512*numUnits)
  209. waitForItem("minecraft:cobblestone", 18*numUnits)
  210. waitForItem("minecraft:crimson_planks", 12*numUnits)
  211. waitForItem("minecraft:stone_brick_slab", 12*numUnits)
  212. waitForItem("minecraft:stone_brick_stairs", 2*numUnits)
  213. waitForItem("minecraft:lantern", 2*numUnits)
  214. waitForItem("minecraft:crimson_trapdoor", 2*numUnits)
  215.  
  216. for unit=1, numUnits do
  217.     fastFail(tryForwards())
  218.     repeatUp(4)
  219.     turtle.turnRight()
  220.  
  221.     for i=1, 2 do
  222.         stripForwards(8)
  223.         turtle.turnLeft()
  224.         stripForwards(1)
  225.         turtle.turnLeft()
  226.         stripForwards(8)
  227.         turtle.turnLeft()
  228.         stripForwards(1)
  229.         turtle.turnLeft()
  230.         repeatDown(2)
  231.     end
  232.  
  233.     stripForwardsWithFloor(8)
  234.     turtle.turnLeft()
  235.     stripForwardsWithFloor(1)
  236.     turtle.turnLeft()
  237.     stripForwardsWithFloor(8)
  238.     turtle.turnLeft()
  239.     stripForwardsWithFloor(1)
  240.     turtle.turnLeft()
  241.  
  242.     fastFail(tryUp())
  243.  
  244.     waitForItem("minecraft:crimson_planks", 1)
  245.     turtle.placeDown()
  246.     fastFail(tryForwards())
  247.     waitForItem("minecraft:crimson_planks", 1)
  248.     turtle.placeDown()
  249.     fastFail(tryForwards())
  250.     turtle.turnLeft()
  251.     turtle.turnLeft()
  252.     waitForItem("minecraft:stone_brick_stairs", 1)
  253.     turtle.placeDown()
  254.     turtle.turnLeft()
  255.     fastFail(tryForwards())
  256.     turtle.turnRight()
  257.     fastFail(tryDown())
  258.     waitForItem("minecraft:crimson_trapdoor", 1)
  259.     turtle.placeUp()
  260.     fastFail(tryForwards())
  261.     fastFail(tryForwards())
  262.     fastFail(tryUp())
  263.     waitForItem("minecraft:crimson_planks", 1)
  264.     turtle.placeDown()
  265.     fastFail(tryUp())
  266.  
  267.     waitForItem("minecraft:crimson_planks", 1)
  268.     turtle.placeDown()
  269.     turtle.turnRight()
  270.     fastFail(tryForwards())
  271.     waitForItem("minecraft:crimson_planks", 1)
  272.     turtle.placeDown()
  273.     turtle.turnRight()
  274.     fastFail(tryForwards())
  275.     waitForItem("minecraft:crimson_planks", 1)
  276.     turtle.placeDown()
  277.     fastFail(tryForwards())
  278.     waitForItem("minecraft:lantern", 1)
  279.     turtle.placeDown()
  280.     fastFail(tryUp())
  281.  
  282.     waitForItem("minecraft:stone_brick_slab", 1)
  283.     turtle.placeDown()
  284.     turtle.turnRight()
  285.     fastFail(tryForwards())
  286.     waitForItem("minecraft:stone_brick_slab", 1)
  287.     turtle.placeDown()
  288.     turtle.turnRight()
  289.     fastFail(tryForwards())
  290.     waitForItem("minecraft:stone_brick_slab", 1)
  291.     turtle.placeDown()
  292.     fastFail(tryForwards())
  293.     waitForItem("minecraft:stone_brick_slab", 1)
  294.     turtle.placeDown()
  295.     turtle.turnRight()
  296.     fastFail(tryForwards())
  297.     waitForItem("minecraft:stone_brick_slab", 1)
  298.     turtle.placeDown()
  299.     turtle.turnRight()
  300.     fastFail(tryForwards())
  301.     waitForItem("minecraft:stone_brick_slab", 1)
  302.     turtle.placeDown()
  303.  
  304.     repeatForwards(7)
  305.     repeatDown(2)
  306.     turtle.turnRight()
  307.     turtle.turnRight()
  308.  
  309.     waitForItem("minecraft:crimson_planks", 1)
  310.     turtle.placeDown()
  311.     fastFail(tryForwards())
  312.     waitForItem("minecraft:crimson_planks", 1)
  313.     turtle.placeDown()
  314.     fastFail(tryForwards())
  315.     turtle.turnRight()
  316.     turtle.turnRight()
  317.     waitForItem("minecraft:stone_brick_stairs", 1)
  318.     turtle.placeDown()
  319.     turtle.turnRight()
  320.     fastFail(tryForwards())
  321.     turtle.turnLeft()
  322.     fastFail(tryDown())
  323.     waitForItem("minecraft:crimson_trapdoor", 1)
  324.     turtle.placeUp()
  325.     fastFail(tryForwards())
  326.     fastFail(tryForwards())
  327.     fastFail(tryUp())
  328.     waitForItem("minecraft:crimson_planks", 1)
  329.     turtle.placeDown()
  330.     fastFail(tryUp())
  331.  
  332.     waitForItem("minecraft:crimson_planks", 1)
  333.     turtle.placeDown()
  334.     turtle.turnLeft()
  335.     fastFail(tryForwards())
  336.     waitForItem("minecraft:crimson_planks", 1)
  337.     turtle.placeDown()
  338.     turtle.turnLeft()
  339.     fastFail(tryForwards())
  340.     waitForItem("minecraft:crimson_planks", 1)
  341.     turtle.placeDown()
  342.     fastFail(tryForwards())
  343.     waitForItem("minecraft:lantern", 1)
  344.     turtle.placeDown()
  345.     fastFail(tryUp())
  346.  
  347.     waitForItem("minecraft:stone_brick_slab", 1)
  348.     turtle.placeDown()
  349.     turtle.turnLeft()
  350.     fastFail(tryForwards())
  351.     waitForItem("minecraft:stone_brick_slab", 1)
  352.     turtle.placeDown()
  353.     turtle.turnLeft()
  354.     fastFail(tryForwards())
  355.     waitForItem("minecraft:stone_brick_slab", 1)
  356.     turtle.placeDown()
  357.     fastFail(tryForwards())
  358.     waitForItem("minecraft:stone_brick_slab", 1)
  359.     turtle.placeDown()
  360.     turtle.turnLeft()
  361.     fastFail(tryForwards())
  362.     waitForItem("minecraft:stone_brick_slab", 1)
  363.     turtle.placeDown()
  364.     turtle.turnLeft()
  365.     fastFail(tryForwards())
  366.     waitForItem("minecraft:stone_brick_slab", 1)
  367.     turtle.placeDown()
  368.  
  369.     repeatForwards(7)
  370.     turtle.turnRight()
  371.     fastFail(tryForwards())
  372.     repeatDown(3)
  373. end
  374.  
  375. for i=1, 15 do
  376.     itemDetail = turtle.getItemDetail(i)
  377.     if itemDetail and itemDetail.name ~= "minecraft:cobblestone" and itemDetail.name ~= "minecraft:crimson_planks" and itemDetail.name ~= "minecraft:stone_brick_slab" and itemDetail.name ~= "minecraft:stone_brick_stairs" and itemDetail.name ~= "minecraft:lantern" and itemDetail.name ~= "minecraft:crimson_trapdoor" then
  378.         turtle.select(i)
  379.         turtle.dropUp()
  380.     end
  381. end
Advertisement
Add Comment
Please, Sign In to add comment