MrReps

Robot (OPC) compact machine automation

Jan 23rd, 2021 (edited)
908
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- A MrReps's program for automation of 3*3*3 compact Machines' structs in compact claustrophobia for opencomputer robot with inventory controller and angel upgrade
  2. -- Probably working on all other pack
  3. -- Place the robot on the front of the field
  4. -- Place three caches on each side of the robot (exept in front)
  5. -- Place a charger on the bottom (dont forget to put a redstone signals on it)
  6. -- Place a collector or something to get the crafted stuff
  7. -- You can use it freely ^^
  8. local component = require("component")
  9. local sides = require("sides")
  10. local robot = require("robot")
  11. local computer = require("computer")
  12. local event = require("event")
  13. local tty = require("tty")
  14. local craft_number
  15. local input
  16. local tempo
  17. local checking
  18.  
  19. function IsNumber(var)
  20.     if type(var) == "number" then
  21.         return true
  22.     else
  23.         return false
  24.     end
  25. end
  26.  
  27. function InputNumber()
  28.     local entree
  29.     local redo
  30.     repeat
  31.         entree = tonumber(io.read())
  32.         if IsNumber(entree) then
  33.             redo = true
  34.         else
  35.             redo = false
  36.             print("Not a valid number retry")
  37.         end
  38.     until(redo)
  39.     return entree    
  40. end
  41.  
  42. function Stop()
  43.     tty.clear()
  44.     print("That was nice to work with you :)")
  45.     print("computer will stop in 5 second!")
  46.     os.sleep(5)
  47.     tty.clear()
  48.     computer.shutdown()
  49. end
  50.  
  51. function Wall()
  52.     local continues = false
  53.     local count = 0
  54.     robot.select(1)
  55.     tty.clear()
  56.     print("You choose to make some wall !")
  57.     print("How many craft do you want to do ?")
  58.     craft_number = InputNumber()
  59.     tty.clear()
  60.     io.write("You will need:\n")
  61.     io.write(craft_number," Block of iron\n")
  62.     io.write((2*craft_number)," Redstone powder\n")
  63.     io.write("When you are facing the field:\n")
  64.     io.write("The robot will check that there is indeed\nthe material.\n")
  65.     io.write("  Inventory on the right need redstone\n")
  66.     io.write("  Inventory on the back need Iron Block\n")
  67.     io.write("Don't forget to place something to collect the\nloot !\n")
  68.     io.write("Do you want to continues ?\n")
  69.     io.write("Press any key to continues\n")
  70.     event.pull("key_down")    
  71.     --check
  72.     robot.turnRight()
  73.     checking = component.inventory_controller.getStackInSlot(sides.front,1)
  74.     if checking.name == "minecraft:redstone" then
  75.         if checking.size >= (craft_number*2) then
  76.             robot.turnRight()
  77.             checking = component.inventory_controller.getStackInSlot(sides.front,1)
  78.             if checking.name == "minecraft:iron_block" then
  79.                 if checking.size >= (craft_number) then
  80.                     continues = true
  81.                 end
  82.             end
  83.         end
  84.     end
  85.     robot.turnRight()
  86.     robot.turnRight()
  87.     if continues then
  88.         tty.clear()
  89.         print("Theire is enought material")
  90.         print("Robot starts his work !")
  91.         print("the robot will stop himself when he will finish")
  92.         os.sleep(2)
  93.         if craft_number > 1 then
  94.             for i=0, craft_number-1, 1 do
  95.                 tty.clear()
  96.                 io.write("Already ", count," blocks crafted !\n")
  97.                 robot.turnRight()
  98.                 robot.suck(2)
  99.                 robot.turnRight()
  100.                 robot.suck(1)
  101.                 robot.turnRight()
  102.                 robot.turnRight()
  103.                 robot.up()
  104.                 robot.select(2)
  105.                 robot.place()
  106.                 robot.up()
  107.                 robot.select(1)
  108.                 robot.place()
  109.                 robot.up()
  110.                 robot.up()
  111.                 robot.up()
  112.                 robot.drop(1)
  113.                 robot.down()
  114.                 robot.down()
  115.                 robot.down()
  116.                 robot.down()
  117.                 robot.down()
  118.                 os.sleep(5)
  119.                 count = count+1
  120.             end
  121.         else
  122.             robot.turnRight()
  123.             robot.suck(2)
  124.             robot.turnRight()
  125.             robot.suck(1)
  126.             robot.turnRight()
  127.             robot.turnRight()
  128.             robot.select(2)
  129.             robot.place()
  130.             robot.up()
  131.             robot.select(1)
  132.             robot.place()
  133.             robot.down()
  134.             robot.drop(1)
  135.         end
  136.         Stop()
  137.     else
  138.         print("not enought matherial !/ Or wrong one !")
  139.         os.sleep(3)
  140.     end      
  141. end
  142.  
  143. function PlaceThree()
  144.     robot.select(1)
  145.     robot.placeDown()
  146.     for i=0, 1, 1 do
  147.         robot.forward()
  148.         robot.placeDown()
  149.     end
  150. end
  151.  
  152. function PlaceNormalNine() --place a grid of 3*3 blocks
  153.     PlaceThree()
  154.     robot.turnRight()
  155.     robot.forward()
  156.     robot.turnRight()
  157.     PlaceThree()
  158.     robot.turnLeft()
  159.     robot.forward()
  160.     robot.turnLeft()
  161.     PlaceThree()
  162.     for i=0, 1, 1 do
  163.         robot.turnLeft()
  164.         robot.forward()
  165.         robot.forward()
  166.     end
  167.     robot.turnLeft()
  168.     robot.turnLeft()
  169. end
  170.  
  171. function PlaceHollow() --place a grid of 3*3 block with a another block in the center
  172.     PlaceThree()
  173.     robot.turnRight()
  174.     robot.forward()
  175.     robot.turnRight()
  176.     robot.placeDown()
  177.     robot.forward()
  178.     robot.select(2)
  179.     robot.placeDown()
  180.     robot.select(1)
  181.     robot.forward()
  182.     robot.placeDown()
  183.     robot.turnLeft()
  184.     robot.forward()
  185.     robot.turnLeft()
  186.     PlaceThree()
  187.     for i=0, 1, 1 do
  188.         robot.turnLeft()
  189.         robot.forward()
  190.         robot.forward()
  191.     end
  192.     robot.turnLeft()
  193.     robot.turnLeft()
  194. end
  195.  
  196. function MakeNormalCube() --make a normal 3*3*3 compact cube
  197.     robot.up()
  198.     robot.up()
  199.     robot.turnLeft()
  200.     robot.forward()
  201.     robot.turnRight()
  202.     robot.forward()
  203.     PlaceNormalNine()
  204.     robot.up()
  205.     PlaceHollow()
  206.     robot.up()
  207.     PlaceNormalNine()
  208.     robot.turnRight()
  209.     robot.forward()
  210.     robot.turnLeft()
  211.     robot.back()
  212.     robot.up()
  213.     robot.select(3)
  214.     robot.drop(1)
  215.     robot.down()
  216.     robot.down()
  217.     robot.down()
  218.     robot.down()
  219.     robot.down()
  220. end
  221.  
  222. function InfiniteCrafting()
  223.     local waintingTime
  224.     local count = 0
  225.     local pass = false
  226.     tty.clear()
  227.     io.write("You choose to make an infinite crafting\n")
  228.     io.write("Please choose the time between each craft attempt.\n")
  229.     io.write("(In the event of lacking ressources)\n")
  230.     io.write("When you are facing the field:\n")
  231.     io.write("  Inventory on the right need \nwall/obsidian\n")
  232.     io.write("  Inventory on the back need \nGold/Redstone Block\n")
  233.     io.write("  Inventory on the left redstone\npowder/enderpearl\n")
  234.     io.write("Place ender pearle/redstone powder in the left cache\n")
  235.     io.write("Put 999 to return, the time is in seconds\n")
  236.     waintingTime = InputNumber()
  237.     if waintingTime == 999 then
  238.         print("return to menu")
  239.     else
  240.         while true do
  241.             tty.clear()
  242.             robot.select(1)
  243.             io.write("You have already made ", count, " craft(s)\n")
  244.             io.write("To leave the program press ALT+CTRL+C\n")
  245.             io.write("not the best way but mheh that work :p\n")
  246.             robot.turnRight()
  247.             repeat
  248.                 checking = component.inventory_controller.getStackInSlot(sides.front,1)
  249.                 if checking.size >= 26 then
  250.                     pass = true
  251.                 else
  252.                     tty.clear()
  253.                     io.write("You have already made ", count, " craft(s)\n")
  254.                     io.write("To leave the program press ALT+CTRL+C\n")
  255.                     io.write("not the best way but mheh that work :p\n")
  256.                     io.write("waiting for obsidian/wall")
  257.                     os.sleep(waintingTime)
  258.                 end
  259.             until(pass)
  260.             robot.suck(26)
  261.             pass = false
  262.             robot.turnRight()
  263.             repeat
  264.                 checking = component.inventory_controller.getStackInSlot(sides.front,1)
  265.                 if checking.size >= 1 then
  266.                     pass = true
  267.                 else
  268.                     tty.clear()
  269.                     io.write("You have already made ", count, " craft(s)\n")
  270.                     io.write("To leave the program press ALT+CTRL+C\n")
  271.                     io.write("not the best way but mheh that work :p\n")
  272.                     io.write("waiting for Gold/Redstone Block")
  273.                     os.sleep(waintingTime)
  274.                 end
  275.             until(pass)
  276.             robot.suck(1)
  277.             pass = false
  278.             robot.turnRight()
  279.             repeat
  280.                 checking = component.inventory_controller.getStackInSlot(sides.front,1)
  281.                 if checking.size >= 1 then
  282.                     pass = true
  283.                 else
  284.                     tty.clear()
  285.                     io.write("You have already made ", count, " craft(s)\n")
  286.                     io.write("To leave the program press ALT+CTRL+C\n")
  287.                     io.write("not the best way but mheh that work :p\n")
  288.                     io.write("waiting for enderpearl/redstone powder")
  289.                     os.sleep(waintingTime)
  290.                 end
  291.             until(pass)
  292.             robot.suck(1)
  293.             pass = false
  294.             robot.turnRight()
  295.             MakeNormalCube()
  296.             os.sleep(25)--time for craft
  297.             count = count+1
  298.         end
  299.     end
  300. end
  301.  
  302. function SpecifiedCrafting()
  303.     local numbertocraft
  304.     local passing
  305.     local count = 0
  306.     tty.clear()
  307.     io.write("You choose to craft a specified amount\n")
  308.     io.write("Please say of many craft you want\n")
  309.     numbertocraft = InputNumber()
  310.     io.write("You will need:\n")
  311.     io.write((numbertocraft*26)," Block of wall/obsidian\n")
  312.     io.write(numbertocraft," Gold/Redstone Block\n")
  313.     io.write(numbertocraft," redstone powder/enderpearl \n")
  314.     io.write("The robot will check that there is indeed\nthe material\n")
  315.     io.write("When you are facing the field:\n")
  316.     io.write("  Inventory on the right need \nwall/obsidian\n")
  317.     io.write("  Inventory on the back need \nGold/Redstone Block\n")
  318.     io.write("  Inventory on the left redstone\npowder/enderpearl\n")
  319.     io.write("Don't forget to place something to collect the loot !\n")
  320.     io.write("Do you want to continues ?\n")
  321.     io.write("Press any key to continues\n")
  322.     event.pull("key_down")    
  323.     --checking
  324.     robot.turnRight()
  325.     checking = component.inventory_controller.getStackInSlot(sides.front,1)
  326.     if checking.size >= (26*numbertocraft) then
  327.         passing = true
  328.     else
  329.         passing = false
  330.     end
  331.     for i=0, 1, 1 do
  332.         robot.turnRight()
  333.         checking = component.inventory_controller.getStackInSlot(sides.front,1)
  334.         if checking.size >= numbertocraft then
  335.             passing = true
  336.         else
  337.             passing = false
  338.         end
  339.     end
  340.     robot.turnRight()
  341.     if passing then
  342.         for i=1, numbertocraft, 1 do
  343.             tty.clear()
  344.             io.write("there are ", (numbertocraft-count), " block left to craft")
  345.             robot.select(1)
  346.             robot.turnRight()
  347.             robot.suck(26)
  348.             robot.turnRight()
  349.             robot.suck(1)
  350.             robot.turnRight()
  351.             robot.suck(1)
  352.             robot.turnRight()
  353.             MakeNormalCube()
  354.             os.sleep(25) --time of craft
  355.             count = count+1
  356.         end
  357.         Stop()
  358.     else
  359.         io.write("not enought material")
  360.         os.sleep(2)
  361.     end
  362. end
  363.        
  364. function MachineMenu() --3*3*3 menu
  365.     local redo2 = false
  366.     tty.clear()
  367.     repeat
  368.         io.write("this program is WIP, the compact cube made with 3 type of block aren't implemented yet\n")
  369.         io.write("Choose your mod of crafting\n")
  370.         io.write("1- Infinite crafting\n")
  371.         io.write("2- Specified amount crafting\n")
  372.         io.write("0- Return\n")
  373.         tempo = InputNumber()
  374.         if tempo == 0 then
  375.             redo2 = true
  376.             tty.clear()
  377.         elseif tempo == 1 then
  378.             redo2 = true
  379.             InfiniteCrafting()
  380.         elseif tempo == 2 then
  381.             redo2 = true
  382.             SpecifiedCrafting()
  383.         else
  384.             tty.clear()
  385.             print("Wrong specification, pease retry !")
  386.         end
  387.     until(redo2)
  388. end
  389.  
  390.  
  391.  
  392. function Menu() --menu function
  393.     while true do
  394.         tty.clear()
  395.         io.write("Wellcome to the 0.1 CompactRobot\n")
  396.         io.write("\n-0 Poweroff\n")
  397.         io.write("-1 Making some Wall\n")
  398.         io.write("-2 Making some full 3*3*3 struct ?\n")
  399.         io.write("\nWhat is your choice ? ")
  400.         input = InputNumber()
  401.         if input == 0 then
  402.             Stop()
  403.         elseif input == 1 then
  404.             Wall()
  405.         elseif input == 2 then
  406.             MachineMenu()
  407.         end
  408.     end
  409. end
  410.  
  411. print("Hello, Robot is booting !")
  412. robot.select(1)
  413. while robot.detect() do
  414.     robot.turnRight()
  415. end
  416. Menu()
RAW Paste Data