Advertisement
Doob

[OpenComputers] universalFarmer

Jun 9th, 2015
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. --[[example
  2. local computer = require('computer')
  3. local robot = require('robot')
  4. local farmer = require('farmer')
  5. local f_type = 'cactus'
  6. local timer = 900
  7.  
  8. local function check_energy()
  9.   energy = computer.energy()/computer.maxEnergy()
  10.   if energy <= 0.1 then
  11.     print('Уровень энергии менее 10%')
  12.     os.sleep(timer)
  13.   end
  14. end
  15.  
  16. local function dig(var)
  17.   if var == 'cactus' then
  18.     if robot.detectDown() then
  19.       robot.swingDown()os.sleep(0.4)
  20.       robot.down() os.sleep(0.4)
  21.       robot.swingDown() os.sleep(0.4)
  22.       robot.up()
  23.         end
  24.   elseif var == 'crops' then
  25.     robot.useDown()
  26.   elseif var == 'tree' then
  27.     local srd = 1
  28.     while robot.detectUp() do
  29.       robot.swingUp()
  30.       robot.up()
  31.       robot.swing()
  32.       srd = srd + 1
  33.         end
  34.     for d = 1, srd do
  35.       robot.down()
  36.     end
  37.   end
  38. end
  39.  
  40. while true do
  41.   check_energy()
  42.   dig(f_type)
  43.   farmer.killer()
  44.   robot.forward()
  45.   if robot.detect() then
  46.     dig(f_type)
  47.         farmer.killer()
  48.     if farmer.turn('left') then
  49.       if farmer.findChest() then
  50.         for i = 2, robot.inventorySize() do
  51.           robot.select(i)
  52.           robot.drop()
  53.         end
  54.         robot.turnAround()
  55.         robot.select(1)
  56.         os.sleep(timer)
  57.       end
  58.     end
  59.   end
  60. end
  61. ]]
  62. local farmer = {}
  63.  
  64. local robot = require 'robot'
  65. local side, tmps = 'left', 'left'
  66. local chest = 1
  67.  
  68. function farmer.killer() -- убийца сущностей
  69.   if robot.detect() then -- что-то нашел
  70.     local _, t = robot.detect()
  71.     if t == 'entity' then -- если перед роботом сущность
  72.       while robot.detect() do
  73.         robot.swing()
  74.       end
  75.     elseif t == 'replaceable' or t == 'passable' then
  76.       robot.swing()
  77.     end
  78.   end
  79. end
  80.  
  81. function farmer.findItem(name, damage) -- поиск предмета в контейнере перед носом
  82.   robot.select(1)
  83.   for sl = 1, component.inventory_controller.getInventorySize(3) do
  84.     if component.inventory_controller.getStackInSlot(3, sl) == nil then
  85.     else
  86.       item = component.inventory_controller.getStackInSlot(3, sl)
  87.       if item.name == name and item.damage == damage then
  88.         component.inventory_controller.suckFromSlot(3, sl)
  89.       end
  90.     end
  91.   end
  92. end
  93.  
  94. function farmer.findChest()--поиск сундука
  95.   side = tmps
  96.   while true do
  97.     if robot.detect() then --что-то есть!
  98.       farmer.killer()
  99.       robot.select(chest) --выбираем сундук
  100.       for a=1, 3 do
  101.         if robot.compare() then --это сундук?
  102.           return true -- да
  103.         else
  104.           robot.turnRight() --нет, поворачиваем направо
  105.         end
  106.       end
  107.     else --ничего нет
  108.       robot.forward() --едем дальше
  109.     end
  110.   end
  111. end
  112.  
  113. function farmer.turn() --поворотник
  114.   if side == 'left' then --если раньше поворачивал направо - стоит left
  115.     robot.turnLeft() --поворачиваем
  116.     if robot.detect() then --проверяем, есть ли блок
  117.       return true --если есть, то сообщаем про угол
  118.     else --иначе
  119.       robot.forward() --делаем шаг
  120.       robot.turnLeft() --и разворачиваем на новую линию
  121.     end
  122.     side = 'right'
  123. -----------------------------------------------------------
  124.   elseif side == 'right' then
  125.     robot.turnRight()
  126.     if robot.detect() then
  127.       return true
  128.     else
  129.       robot.forward()
  130.       robot.turnRight()
  131.     end
  132.     side = 'left'
  133.   end
  134. end
  135.  
  136. return farmer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement