Advertisement
Link712011

OpenComputer - Lettuce farm

Jun 4th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.46 KB | None | 0 0
  1. local robot = require("robot")
  2. local side = require("sides")
  3. local component = require("component")
  4. local shell = require("shell")
  5. local computer = require("computer")
  6.  
  7. local field_size_x = 34
  8. local field_size_z = 9
  9.  
  10. -- Robot movement encapsulations
  11.  
  12. function forward(amount)
  13.     local i
  14.  
  15.     i = amount
  16.     if not amount then
  17.         i = 1
  18.     end
  19.     while i > 0 do
  20.         if robot.forward() then
  21.             i = i - 1
  22.         else
  23.             os.sleep(2)
  24.             print("Can't forward. Please move this shit.")
  25.         end
  26.     end
  27. end
  28.  
  29. function up(amount)
  30.     if not amount then
  31.         amount = 1
  32.     end
  33.     while amount > 0 do
  34.         if robot.up() then
  35.             amount = amount - 1
  36.         else
  37.             os.sleep(2)
  38.             print("Can't go up. Please move this shit.")
  39.         end
  40.     end
  41. end
  42.  
  43. function down(amount)
  44.     if not amount then
  45.         amount = 1
  46.     end
  47.     while amount > 0 do
  48.         if robot.down() then
  49.             amount = amount - 1
  50.         else
  51.             os.sleep(2)
  52.             print("Can't go down. Please move this shit.")
  53.         end
  54.     end
  55. end
  56.  
  57. function move_orientation(orientation)
  58.     if orientation == side.left then
  59.         robot.turnLeft()
  60.     elseif orientation == side.right then
  61.         robot.turnRight()
  62.     elseif orientation == side.back then
  63.         robot.turnRight()
  64.         robot.turnRight()
  65.     end
  66. end
  67.  
  68. function move_orientation_revert(orientation)
  69.     if orientation == robot.left then
  70.         robot.turnRight()
  71.     elseif orientation == robot.right then
  72.         robot.turnLeft()
  73.     elseif orientation == robot.back then
  74.         robot.turnLeft()
  75.         robot.turnLeft()
  76.     end
  77. end
  78.  
  79. function move(amount, orientation)
  80.     if orientation then
  81.         move_orientation(orientation)
  82.         if orientation == up then
  83.             up(amount)
  84.         elseif orientation == down then
  85.             down(amount)
  86.         else
  87.             forward(amount)
  88.         end
  89.         move_orientation_revert(orientation)
  90.     else
  91.         forward(amount)
  92.     end
  93. end
  94.  
  95.  
  96. -- Inventory gestion functions
  97.  
  98. function push_item_after_slot(from)
  99.     local slot = from + 1
  100.    
  101.     robot.select(from)
  102.     while slot < robot.inventorySize() and component.inventory_controller.getStackInInternalSlot(slot) do
  103.         slot = slot + 1
  104.     end
  105.     if slot < robot.inventorySize() and robot.transferTo(slot) then
  106.         return true
  107.     end
  108.     return false
  109. end
  110.  
  111. function select_item(item, meta)
  112.     local slot = 1
  113.     local size = robot.inventorySize()
  114.    
  115.     while slot <= size do
  116.         local data = component.inventory_controller.getStackInInternalSlot(slot)
  117.         if data and data.name == item and (not meta or data.damage == meta) then
  118.             robot.select(slot)
  119.             return true
  120.         end
  121.         slot = slot + 1
  122.     end
  123.     return false
  124. end
  125.  
  126. function select_next_item(slot, item, meta)
  127.     local data
  128.     local size = robot.inventorySize()
  129.    
  130.     while slot <= robot.inventorySize() do
  131.         data = component.inventory_controller.getStackInInternalSlot(slot)
  132.         if data and data.name == item and (not meta or meta == data.damage) then
  133.             robot.select(slot)
  134.             return true
  135.         end
  136.         slot = slot + 1
  137.     end
  138.     return false
  139. end
  140.  
  141. function repack_item(item, meta)
  142.     local slot = 1
  143.     local data
  144.    
  145.     if item == "minecraft:stone_pickaxe" or item == "minecraft:diamond_pickaxe" then
  146.         return true
  147.     end
  148.     while slot < robot.inventorySize() do
  149.         data = component.inventory_controller.getStackInInternalSlot(slot)
  150.         if not data or ((data.name == item and data.size < 64) and (not meta or meta == data.damage)) then
  151.             if select_next_item(slot + 1, item, meta) then
  152.                 robot.transferTo(slot)
  153.                 data = component.inventory_controller.getStackInInternalSlot(slot)
  154.             else
  155.                 return true
  156.             end
  157.         else
  158.             slot = slot + 1
  159.         end
  160.     end
  161.     return true
  162. end
  163.  
  164. function empty_slot_amount()
  165.     local slot = 1
  166.     local empty_slots = 0
  167.     local size = robot.inventorySize()
  168.    
  169.     while slot <= size do
  170.         if not component.inventory_controller.getStackInInternalSlot(slot) then
  171.             empty_slots = empty_slots + 1
  172.         end
  173.         slot = slot + 1
  174.     end
  175.     return empty_slots
  176. end
  177.  
  178. function select_item_out_of_workbench(item, meta)
  179.     local slot = 4
  180.     local data
  181.     local size = robot.inventorySize()
  182.    
  183.     while slot <= size do
  184.         if slot == 5 or slot == 9 then
  185.             slot = slot + 3
  186.         end
  187.         data = component.inventory_controller.getStackInInternalSlot(slot)
  188.         if data and data.name == item and (not meta or data.damage == meta) then
  189.             robot.select(slot)
  190.             return true
  191.         end
  192.         slot = slot + 1
  193.     end
  194.     return false
  195. end
  196.  
  197. function select_empty_slot()
  198.     local slot = robot.inventorySize()
  199.    
  200.     while slot > 0 do
  201.         if not component.inventory_controller.getStackInInternalSlot(slot) then
  202.             robot.select(slot)
  203.             return true
  204.         end
  205.         slot = slot - 1
  206.     end
  207.     return false
  208. end
  209.  
  210. -- Crafting functions
  211.  
  212. function place_item_for_craft(item, to, meta, amount)
  213.     local craft = {}
  214.    
  215.     if not amount then
  216.         amount = 1
  217.     end
  218.     craft[1] = 1
  219.     craft[2] = 2
  220.     craft[3] = 3
  221.     craft[4] = 5
  222.     craft[5] = 6
  223.     craft[6] = 7
  224.     craft[7] = 9
  225.     craft[8] = 10
  226.     craft[9] = 11
  227.     if to > 9 then
  228.         print("place_item_for_craft: slot can't be > than 9")
  229.         os.exit()
  230.     end
  231.     if select_item_out_of_workbench(item, meta) and robot.transferTo(craft[to], amount) then
  232.         return true
  233.     end
  234.     return false
  235. end
  236.  
  237. function free_crafting_table()
  238.     local slot = 1
  239.     local data
  240.     local tried = false
  241.    
  242.     while slot <= 11 do
  243.         if slot ~= 4 and slot ~= 8 then
  244.             if component.inventory_controller.getStackInInternalSlot(slot) and not push_item_after_slot(slot) and not tried then
  245.                 clean_inventory()
  246.                 tried = true
  247.             elseif component.inventory_controller.getStackInInternalSlot(slot) and not push_item_after_slot(slot) then
  248.                 return false
  249.             end
  250.         end
  251.         slot = slot + 1
  252.     end
  253.     return true
  254. end
  255.  
  256.  
  257. -- External storage gestion functions
  258.  
  259. function get_free_slot_in_chest(chest_side)
  260.     local slot
  261.     local inv_size
  262.     local data
  263.     local reason
  264.    
  265.     slot = 1
  266.     inv_size, reason = component.inventory_controller.getInventorySize(chest_side)
  267.     if not inv_size then
  268.         print("Failed to get free slot in chest: " .. reason)
  269.         return false
  270.     end
  271.     while slot < inv_size do
  272.         data = component.inventory_controller.getStackInSlot(chest_side, slot)
  273.         if not data then
  274.             return slot
  275.         end
  276.         slot = slot + 1
  277.     end
  278.     return false
  279. end
  280.  
  281. function dump_slot_to_chest(chest_side, from_slot, amount, to_slot)
  282.     if not to_slot then
  283.         to_slot = get_free_slot_in_chest(chest_side)
  284.         if not to_slot then
  285.             return false
  286.         end
  287.     end
  288.     robot.select(from_slot)
  289.     if not amount then
  290.         component.inventory_controller.dropIntoSlot(side.front, to_slot)
  291.     else
  292.         component.inventory_controller.dropIntoSlot(side.front, to_slot, amount)
  293.     end
  294.     return true
  295. end
  296.  
  297. function drop_item_to_chest(chest_side, item, meta)
  298.     while select_item(item, meta) do
  299.         if not dump_slot_to_chest(chest_side, robot.select()) then
  300.             return false
  301.         end
  302.     end
  303.     return true
  304. end
  305.  
  306. function get_item_from_chest(chest_side, name, meta, amount)
  307.     local slot
  308.     local data = {}
  309.     local chest_size
  310.    
  311.     chest_size = component.inventory_controller.getInventorySize(chest_side)
  312.     slot = 1
  313.     if not chest_size or chest_size <= 0 then
  314.         return false
  315.     end
  316.     while slot <= chest_size do
  317.         data = component.inventory_controller.getStackInSlot(side.front, slot)
  318.         if data and data.name == name and (not meta or data.damage == meta) then
  319.             if amount then
  320.                 component.inventory_controller.suckFromSlot(chest_side, slot, amount)
  321.             else
  322.                 component.inventory_controller.suckFromSlot(chest_side, slot)
  323.             end
  324.             return true
  325.         end
  326.         slot = slot + 1
  327.     end
  328.     return false
  329. end
  330.  
  331.  
  332. -- World interations functions
  333.  
  334. function get_bloc(orientation)
  335.     local bloc = {}
  336.    
  337.     bloc["bloc"] = component.geolyzer.analyze(orientation)
  338.     bloc["side"] = orientation
  339.     return bloc
  340. end
  341.  
  342. function get_blocs_around()
  343.     local bloc = {}
  344.    
  345.     bloc[1] = get_bloc(side.front)
  346.     bloc[2] = get_bloc(side.back)
  347.     bloc[3] = get_bloc(side.up)
  348.     bloc[4] = get_bloc(side.down)
  349.     bloc[5] = get_bloc(side.left)
  350.     bloc[6] = get_bloc(side.right)
  351.     return bloc
  352. end
  353.  
  354. function can_drop_in_chest()
  355.     local i
  356.    
  357.     i = 1
  358.     bloc = get_blocs_around()
  359.     while i <= #bloc do
  360.         if bloc[i]["bloc"].name == "ironchest:BlockIronChest" or bloc[i]["bloc"].name == "minecraft:chest" then
  361.             return bloc[i]["side"]
  362.         end
  363.         i = i + 1
  364.     end
  365.     return false
  366. end
  367.  
  368. function craft_and_store_food()
  369.     local chest_side
  370.    
  371.     chest_side = can_drop_in_chest()
  372.     if not chest_side then
  373.         print("No chest found")
  374.         return false
  375.     end
  376.     drop_item_to_chest(chest_side, "harvestcraft:lettuceItem")
  377.     if not free_crafting_table() or not place_item_for_craft("harvestcraft:potItem", 1, 0) then
  378.         return false
  379.     end
  380.     get_item_from_chest(chest_side, "harvestcraft:lettuceItem", nil, 21)
  381.     while place_item_for_craft("harvestcraft:lettuceItem", 2, 0, 21) do
  382.         component.crafting.craft()
  383.         select_empty_slot()
  384.         drop_item_to_chest(chest_side, "harvestcraft:stockItem")
  385.         get_item_from_chest(chest_side, "harvestcraft:lettuceItem", nil, 21)
  386.     end
  387.     return true
  388. end
  389.  
  390.  
  391. -- Core functions
  392.  
  393. function line(amount)
  394.     robot.placeDown()
  395.     while amount > 0 do
  396.         forward()
  397.         robot.placeDown()
  398.         amount = amount - 1
  399.     end
  400. end
  401.  
  402. function core(max_x, max_z)
  403.     local i
  404.  
  405.     i = 0
  406.     if not select_item("harvestcraft:potItem") then
  407.         return false
  408.     end
  409.     while i < max_z do
  410.         line(max_x)
  411.         if i < max_z - 1 then
  412.             if i % 2 == 0 then
  413.                 robot.turnLeft()
  414.             else
  415.                 robot.turnRight()
  416.             end
  417.             forward()
  418.             if i % 2 == 0 then
  419.                 robot.turnLeft()
  420.             else
  421.                 robot.turnRight()
  422.             end
  423.         end
  424.         i = i + 1
  425.     end
  426.     move_orientation(side.back)
  427.     forward(max_x)
  428.     move_orientation(side.left)
  429.     forward(max_z - 1)
  430.     move_orientation(side.left)
  431.     move_orientation(side.back)
  432.     craft_and_store_food()
  433.     move_orientation(side.back)
  434. end
  435. while true do
  436.     core(field_size_x, field_size_z)
  437.     os.sleep(200)
  438. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement