Advertisement
Guest User

Structure Script

a guest
Oct 7th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.47 KB | None | 0 0
  1. local robot = require("robot")
  2. local component = require("component")
  3. local ic = component.inventory_controller
  4. local args, opts = require("shell").parse(...)
  5. local inventory = {}
  6. local placeRow, placeColumn, getInventory, checkItemInInventoryAndSelect, placeStructure, resetPosition, gotoStart, getRequiredItemsFromCache, totalItemQuantityInInventory, haveItemsToCraft, throwCatalyst, moveToCharging, moveToPlacing;
  7.  
  8. local itemNames = {
  9.     AIR = "nil",
  10.     GOLD_BLOCK = "minecraft:gold_block",
  11.     COMPACT_WALL = "compactmachines3:wallbreakable",
  12.     REDSTONE = "minecraft:redstone",
  13.     REDSTONE_BLOCK = "minecraft:redstone_block",
  14.     OBISIDAN = "minecraft:obsidian",
  15.     ENDERPEARL = "minecraft:ender_pearl",
  16.     IRON_BLOCK = "minecraft:iron_block"
  17. }
  18.  
  19. local recipes = {
  20.     Compact5 = {
  21.         recipe = {
  22.             {
  23.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
  24.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
  25.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL}
  26.             },
  27.             {
  28.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
  29.                 {itemNames.COMPACT_WALL, itemNames.GOLD_BLOCK, itemNames.COMPACT_WALL},
  30.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL}
  31.             },
  32.             {
  33.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
  34.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL},
  35.                 {itemNames.COMPACT_WALL, itemNames.COMPACT_WALL, itemNames.COMPACT_WALL}
  36.             }
  37.         },
  38.        
  39.         items = {
  40.             {
  41.                 name = itemNames.COMPACT_WALL,
  42.                 quantity = 26
  43.             },
  44.             {
  45.                 name = itemNames.GOLD_BLOCK,
  46.                 quantity = 1
  47.             },
  48.             {
  49.                 name = itemNames.ENDERPEARL,
  50.                 quantity = 1
  51.             }
  52.         },
  53.  
  54.         catalyst = itemNames.ENDERPEARL,
  55.         restTime = 480 / 20
  56.     },
  57.     EnderPearl = {
  58.         recipe = {
  59.             {
  60.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
  61.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
  62.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN}
  63.             },
  64.             {
  65.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
  66.                 {itemNames.OBISIDAN, itemNames.REDSTONE_BLOCK, itemNames.OBISIDAN},
  67.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN}
  68.             },
  69.             {
  70.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
  71.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN},
  72.                 {itemNames.OBISIDAN, itemNames.OBISIDAN, itemNames.OBISIDAN}
  73.             }
  74.         },
  75.        
  76.         items = {
  77.             {
  78.                 name = itemNames.OBISIDAN,
  79.                 quantity = 26
  80.             },
  81.             {
  82.                 name = itemNames.REDSTONE_BLOCK,
  83.                 quantity = 1
  84.             },
  85.             {
  86.                 name = itemNames.REDSTONE,
  87.                 quantity = 1
  88.             }
  89.         },
  90.  
  91.         catalyst = itemNames.REDSTONE,
  92.         restTime = 200 / 20
  93.     },
  94.     CompactWall = {
  95.         recipe = {
  96.             {
  97.                 {},
  98.                 {itemNames.AIR, itemNames.IRON_BLOCK, itemNames.AIR},
  99.                 {}
  100.             },
  101.             {
  102.                 {},
  103.                 {itemNames.AIR, itemNames.REDSTONE, itemNames.AIR},
  104.                 {}
  105.             },
  106.             {
  107.                 {},
  108.                 {},
  109.                 {}
  110.             }
  111.         },
  112.        
  113.         items = {
  114.             {
  115.                 name = itemNames.IRON_BLOCK,
  116.                 quantity = 1
  117.             },
  118.             {
  119.                 name = itemNames.REDSTONE,
  120.                 quantity = 2
  121.             }
  122.         },
  123.  
  124.         catalyst = itemNames.REDSTONE,
  125.         restTime = 100 / 20
  126.     }
  127. }
  128.  
  129. function placeRow(row)
  130.     getInventory()
  131.     for index, block in pairs(row) do
  132.         robot.forward()
  133.         if checkItemInInventoryAndSelect(block) then
  134.             robot.placeDown()
  135.         end
  136.     end
  137. end
  138.  
  139. function placeColumn(column)
  140.     local lastColumn = 1
  141.  
  142.     for index, row in ipairs(column) do
  143.         if index == 2 then
  144.             robot.turnRight()
  145.             robot.forward()
  146.             robot.turnRight()
  147.             robot.back()
  148.         end
  149.  
  150.         if index == 3 then
  151.             robot.turnLeft()
  152.             robot.forward()
  153.             robot.turnLeft()
  154.             robot.back()
  155.         end
  156.         lastColumn = index
  157.         if #row ~= 0 then
  158.             placeRow(row)
  159.         else
  160.             robot.forward()
  161.             robot.forward()
  162.             robot.forward()
  163.         end
  164.     end
  165.  
  166.     return lastColumn
  167. end
  168.  
  169. function getInventory()
  170.     for i = 1, 16 do
  171.         if ic.getStackInInternalSlot(i) then
  172.             inventory[i] = {
  173.                 name = ic.getStackInInternalSlot(i).name,
  174.                 quantity = robot.count(i) or 1
  175.             }
  176.         else
  177.             inventory[i] = nil
  178.         end
  179.     end
  180. end
  181.  
  182. function checkItemInInventoryAndSelect(itemName)
  183.     local found = false
  184.  
  185.     for i=1,16 do
  186.         if inventory[i] and inventory[i].name == itemName then
  187.             if robot.count(i) < 2 then inventory[i] = nil end
  188.             found = true
  189.             robot.select(i)
  190.             break
  191.         end  
  192.     end
  193.  
  194.     return found
  195. end
  196.  
  197. function placeStructure(item, quantity, actual)
  198.     if actual == nil then actual = 1 end
  199.     if quantity == nil then quantity = 1 end
  200.     if actual > quantity then return print("Finished crafting") end
  201.     getInventory()
  202.    
  203.     if not haveItemsToCraft(item) then return print("The are items missing for the craft") end
  204.     moveToPlacing()
  205.    
  206.     for index, column in ipairs(item.recipe) do
  207.         local lastColumn = placeColumn(column)
  208.        
  209.         resetPosition(lastColumn)
  210.         if index ~= 3 then robot.up() end
  211.     end
  212.    
  213.     gotoStart()
  214.     moveToCharging()
  215.     throwCatalyst(item)
  216.     os.sleep(item.restTime)
  217.     getRequiredItemsFromCache(item)
  218.     placeStructure(item, quantity, actual + 1)
  219. end
  220.  
  221. function resetPosition(lastColumn)
  222.     if lastColumn == 1 then
  223.         for i = 1, 3 do
  224.             robot.back()
  225.         end
  226.     else if lastColumn == 2 then
  227.         robot.turnRight()
  228.         robot.forward()
  229.         robot.turnRight()
  230.         robot.back()
  231.     else if lastColumn == 3 then
  232.         for i = 1, 3 do
  233.             robot.back()
  234.         end
  235.  
  236.         robot.turnLeft()
  237.  
  238.         for i = 1, 2 do
  239.             robot.forward()
  240.         end
  241.  
  242.         robot.turnRight()
  243.     end
  244.     end
  245.     end
  246. end
  247.  
  248. function gotoStart()
  249.     for i = 1, 2 do
  250.         robot.down()
  251.     end
  252. end
  253.  
  254. function totalItemQuantityInInventory(itemName)
  255.     local total = 0
  256.  
  257.     for key, item in pairs(inventory) do
  258.         if item and item.name == itemName then  
  259.             if item.quantity then
  260.                 total = total + item.quantity
  261.             end
  262.         end
  263.     end
  264.  
  265.     return total
  266. end
  267.  
  268. function haveItemsToCraft(item)
  269.     local craftable = {}
  270.  
  271.     for key, value in pairs(item.items) do
  272.         if math.floor(totalItemQuantityInInventory(value.name) / value.quantity) >= 1 then craftable = true else craftable = false break end
  273.     end
  274.  
  275.     return craftable
  276. end
  277.  
  278. function throwCatalyst(item)
  279.     checkItemInInventoryAndSelect(item.catalyst)
  280.     os.sleep(0.5)
  281.     robot.drop(1)
  282. end
  283.  
  284. function moveToCharging()
  285.     robot.back()
  286.     robot.down()
  287. end
  288.  
  289. function moveToPlacing()
  290.     robot.forward()
  291.     robot.up()
  292. end
  293.  
  294. function getRequiredItemsFromCache(item)
  295.     for index, itemInfo in ipairs(item.items) do
  296.         if index == 1 then
  297.             robot.turnLeft()
  298.             robot.suck(itemInfo.quantity)
  299.             robot.turnRight()
  300.         end
  301.  
  302.         if index == 2 then
  303.             robot.turnRight()
  304.             robot.suck(itemInfo.quantity)
  305.             robot.turnLeft()
  306.         end
  307.  
  308.         if index == 3 then
  309.             robot.up()
  310.             robot.turnLeft()
  311.             robot.suck(itemInfo.quantity)
  312.             robot.turnRight()
  313.             robot.down()
  314.         end
  315.     end
  316. end
  317.  
  318. placeStructure(recipes[args[1]], tonumber(args[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement