Advertisement
Guest User

feller.lua

a guest
Feb 19th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local comp = require("computer")
  4. local magnet
  5. local red
  6.  
  7. function main()
  8.     if battest() > 30 then io.write("Battery check = ",battest(),"%\n")
  9.     else
  10.         io.write("Low battery level") return
  11.     end
  12. --Настройка
  13.     io.write("Введеите к-во деревьев: ")
  14.     count = io.read()
  15.    
  16.     io.write("Проверьте к-во саженцев в слоте\n\tНужно: ",count,", в слоте: ", robot.count())
  17.     io.read()
  18.    
  19.     io.write("Введеите задержку между проверками (в секундах):\n")
  20.     wait = toInteger(io.read())
  21.    
  22.     io.write("Режим работы: \n\t1.Ванильный\n(дерево рубиться целиком блок за блоком)\n\t2.Модифицированый\n(дерево рубиться полностью при срубании\nпервого блока, должен быть установлен мод)\n")
  23.     mode = io.read()
  24.    
  25.     range = 6 --растояние между саженцами (в блоках)
  26.     trees = 0
  27. -- 
  28.     io.write("Приступаю...\n")
  29.     for j = 1,count do
  30.         for i = 1,range do
  31.             robot.forward()
  32.         end
  33.         robot.turnLeft()
  34.         if not robot.detect() then robot.place() end
  35.         robot.turnRight()
  36.     end
  37.     robot.turnAround()
  38.     while true do
  39.         for i = 1,count*range do
  40.             robot.forward()
  41.         end
  42.         robot.turnAround()
  43.         if status() ~= nil then
  44.             while status() ~= nil do
  45.                 os.sleep(3)
  46.             end
  47.             io.write("tool equipped")
  48.         else os.sleep(wait)
  49.         end
  50.         for j = 1,count do
  51.             for i = 1,range do
  52.                 robot.forward()
  53.             end
  54.         tmp = check(trees,mode)
  55.         if tmp ~= trees and tmp ~= nil then
  56.             io.write("Срублено деревьев: ", tmp,"\n")
  57.             trees = tmp
  58.         end
  59.         end
  60.         robot.turnAround()
  61.     end
  62. end
  63.  
  64. function toInteger(anystring)
  65.     number = 0
  66.     for i = 0,anystring do
  67.         number = i
  68.     end
  69.     return number
  70. end
  71.  
  72. function battest()
  73.     return math.ceil(comp.energy()*100/comp.maxEnergy())
  74. end
  75.  
  76. function status()
  77.     inf,err = robot.durability()
  78.     if battest() < 30 then
  79.         red.setOutput(5,15)
  80.         io.write("Зарядка...")
  81.         while battest()~=100 do os.sleep(1) end
  82.         red.setOutput(5,0)
  83.         io.write("Зарядка завершена\n")
  84.     end
  85.     if robot.count(2) == 64 then unload() end
  86.     if inf == nil then print(err) return err
  87.     else if inf == 0 then return "low durability" end end
  88.     return nil
  89. end
  90.  
  91. function check(count,mode)
  92.     inf,err = robot.durability()
  93.     if inf == nil then print(err) return nil
  94.     else if inf == 0 then return nil end end
  95.     robot.turnLeft()
  96.     robot.up()
  97.     if robot.detect() then
  98.         robot.down()
  99.         robot.swing()
  100.         robot.forward()
  101.         if mode == "2" then harvest()
  102.         else chopUp() end
  103.         robot.turnAround()
  104.         robot.forward()
  105.         robot.turnAround()
  106.         robot.place()
  107.         robot.turnRight()
  108.         count = count+1
  109.     else
  110.         robot.down()
  111.         robot.turnRight()
  112.     end
  113.     if mode == "1" then harvest() end
  114.     return count
  115. end
  116.  
  117.  
  118. function harvest()
  119.   while magnet.suck() do
  120.   end
  121. end
  122.  
  123. function chopUp()
  124.     countblock = 0
  125.     while robot.detectUp() do
  126.         robot.swingUp()
  127.         robot.up()
  128.         countblock = countblock + 1
  129.     end
  130.     for i = 1,countblock do
  131.         robot.down()
  132.     end
  133. end
  134.  
  135. function unload()
  136.   robot.turnAround()
  137.   for c = 2,16 do
  138.     robot.select(c)
  139.     if robot.count() > 0 then
  140.       robot.drop()
  141.     end
  142.   end
  143.   robot.select(1)
  144.   robot.turnAround()
  145. end
  146.  
  147. if component.isAvailable("tractor_beam") and component.isAvailable("redstone") then
  148. red = component.redstone
  149. magnet = component.tractor_beam
  150. main()
  151. else io.write("Не хватает компонентов: \n")
  152. if  not component.isAvailable("tractor_beam") then io.write("\tПритягивающий луч\n") end
  153. if  not component.isAvailable("redstone") then io.write("\tПлата на красном камне\n") end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement