Advertisement
Guest User

lumber3.lua

a guest
Dec 25th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local r=require("robot")
  2. local computer=require("computer")
  3.  
  4. -- Farm for single 2x2 tree
  5.  
  6. -- Requirements:
  7. --   World: Treecapitator mod
  8. --   Robot: Casing tier 1, CPU tier 1, 2x Memory tier 1, HDD tier 1 (with OpenOS), Inventory upgrade(s)
  9.  
  10. -- Variables --
  11. try=30
  12. maxEnergy=computer.maxEnergy()
  13.  
  14. -- Functions --
  15. function forw()
  16.     local t=1
  17.     while r.forward()~=true and t<try do
  18.         local d1,d2=r.detect()
  19.         if d2=="entity"
  20.             then os.sleep(5) -- if entity, then we wait it to get out
  21.         end
  22.         t=t+1
  23.     end
  24. end
  25.  
  26. function down()
  27.     local t=1
  28.     while r.down()~=true and t<try do
  29.         local d1,d2=r.detectDown()
  30.         if d2=="entity"
  31.             then os.sleep(5) -- if entity, then we wait it to get out
  32.         end
  33.         t=t+1
  34.     end
  35. end
  36.  
  37. function up()
  38.     if r.up()~=true then -- if we can't pass
  39.         local t=1
  40.         while r.swingUp()~=true and t<(try*3) do t=t+1 end -- we try to cut through
  41.         local t=1
  42.         while r.up()~=true and t<try do t=t+1 end -- and try to pass again
  43.     end
  44. end
  45.  
  46. function checkbat()
  47.     if computer.energy()>500 -- more than 500 -> true
  48.         then return true
  49.         else r.setLightColor(0xffff00) print("Battery low") return false
  50.     end
  51. end
  52.  
  53. function checkinv()
  54.     if r.count(r.inventorySize()-2)=0
  55.         then return true
  56.         else r.setLightColor(0xffff00) print("Inventory is full") return false
  57.     end
  58. end
  59.  
  60. function checkdur()
  61.     if r.durability()>0.25 -- for chainsaw and 45 logs per tree
  62.         then return true
  63.         else r.setLightColor(0xffff00) print("Tool worn out") return false
  64.     end
  65. end
  66.  
  67. function unload()  
  68.     local d1,d2=r.detect()
  69.     if d2=="solid"
  70.         then
  71.             local i=2
  72.             r.select(i)
  73.             while i<r.inventorySize() do
  74.                 r.drop()
  75.                 i=i+1
  76.                 r.select(i)
  77.             end
  78.         else r.setLightColor(0xff0000) error("Wrong position")
  79.     end
  80.         r.select(1)
  81.         print("Inventory emptied")
  82.     end
  83.  
  84. function forservice()      
  85.     r.turnAround()
  86.     for i=1,4 do forw() end
  87. end
  88.  
  89. function fromservice()     
  90.     r.turnAround()
  91.     for i=1,4 do forw() end
  92. end
  93.  
  94. function service()
  95.     r.use()
  96.     r.turnRight()
  97.     unload()
  98.     r.turnLeft()
  99.     while maxEnergy-computer.energy() >= 300 do
  100.         os.sleep(2)
  101.         end
  102.     print("Battery full")
  103.     r.use()
  104.     if checkdur()==false
  105.         then r.setLightColor(0xff0000) error("Tool worn out")
  106.         else r.setLightColor(0x00ff00)
  107.         end
  108. end
  109.  
  110. function suck()
  111.     local t=-2
  112.     while r.suckDown()==true and t<try
  113.         do t=t+1
  114.     end
  115. end
  116.    
  117.    
  118. function snp4x4()
  119.     forw()
  120.     suck()
  121.     r.placeDown()
  122.     forw()
  123.     suck()
  124.     r.placeDown()
  125.     r.turnLeft()
  126.     forw()
  127.     r.turnLeft()
  128.     suck()
  129.     r.placeDown()
  130.     r.forward()
  131.     suck()
  132.     r.placeDown()
  133.     forw()
  134.     r.turnLeft()
  135.     forw()
  136.     r.turnLeft()
  137. end
  138.    
  139.    
  140. -- Initialization --
  141.  
  142. r.setLightColor(0xffff00)
  143. print("Initialization...")
  144. r.select(1)
  145. if checkbat()==false or checkinv()==false or checkdur()==false
  146.     then service()  
  147. end
  148. fromservice()
  149. down()
  150.  
  151.  
  152. -- Main --
  153.  
  154. r.setLightColor(0x00ff00)
  155. print("Work!")
  156. while true do
  157.  
  158.     if checkbat()==false or checkinv()==false or checkdur()==false
  159.         then up() forservice() service() fromservice() down()
  160.         else r.setLightColor(0x00ff00)
  161.     end
  162.  
  163.     local d1,d2=r.detect()
  164.     if d2=="solid"
  165.         then r.swing() up() os.sleep(5) snp4x4() down()
  166.     end
  167.    
  168.     os.sleep(10)
  169. end
  170.  
  171. -- End --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement