Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local r=require("robot")
- local computer=require("computer")
- -- Farm for single 2x2 tree
- -- Requirements:
- -- World: Treecapitator mod
- -- Robot: Casing tier 1, CPU tier 1, 2x Memory tier 1, HDD tier 1 (with OpenOS), Inventory upgrade(s)
- -- Variables --
- try=30
- maxEnergy=computer.maxEnergy()
- -- Functions --
- function forw()
- local t=1
- while r.forward()~=true and t<try do
- local d1,d2=r.detect()
- if d2=="entity"
- then os.sleep(5) -- if entity, then we wait it to get out
- end
- t=t+1
- end
- end
- function down()
- local t=1
- while r.down()~=true and t<try do
- local d1,d2=r.detectDown()
- if d2=="entity"
- then os.sleep(5) -- if entity, then we wait it to get out
- end
- t=t+1
- end
- end
- function up()
- if r.up()~=true then -- if we can't pass
- local t=1
- while r.swingUp()~=true and t<(try*3) do t=t+1 end -- we try to cut through
- local t=1
- while r.up()~=true and t<try do t=t+1 end -- and try to pass again
- end
- end
- function checkbat()
- if computer.energy()>500 -- more than 500 -> true
- then return true
- else r.setLightColor(0xffff00) print("Battery low") return false
- end
- end
- function checkinv()
- if r.count(r.inventorySize()-2)=0
- then return true
- else r.setLightColor(0xffff00) print("Inventory is full") return false
- end
- end
- function checkdur()
- if r.durability()>0.25 -- for chainsaw and 45 logs per tree
- then return true
- else r.setLightColor(0xffff00) print("Tool worn out") return false
- end
- end
- function unload()
- local d1,d2=r.detect()
- if d2=="solid"
- then
- local i=2
- r.select(i)
- while i<r.inventorySize() do
- r.drop()
- i=i+1
- r.select(i)
- end
- else r.setLightColor(0xff0000) error("Wrong position")
- end
- r.select(1)
- print("Inventory emptied")
- end
- function forservice()
- r.turnAround()
- for i=1,4 do forw() end
- end
- function fromservice()
- r.turnAround()
- for i=1,4 do forw() end
- end
- function service()
- r.use()
- r.turnRight()
- unload()
- r.turnLeft()
- while maxEnergy-computer.energy() >= 300 do
- os.sleep(2)
- end
- print("Battery full")
- r.use()
- if checkdur()==false
- then r.setLightColor(0xff0000) error("Tool worn out")
- else r.setLightColor(0x00ff00)
- end
- end
- function suck()
- local t=-2
- while r.suckDown()==true and t<try
- do t=t+1
- end
- end
- function snp4x4()
- forw()
- suck()
- r.placeDown()
- forw()
- suck()
- r.placeDown()
- r.turnLeft()
- forw()
- r.turnLeft()
- suck()
- r.placeDown()
- r.forward()
- suck()
- r.placeDown()
- forw()
- r.turnLeft()
- forw()
- r.turnLeft()
- end
- -- Initialization --
- r.setLightColor(0xffff00)
- print("Initialization...")
- r.select(1)
- if checkbat()==false or checkinv()==false or checkdur()==false
- then service()
- end
- fromservice()
- down()
- -- Main --
- r.setLightColor(0x00ff00)
- print("Work!")
- while true do
- if checkbat()==false or checkinv()==false or checkdur()==false
- then up() forservice() service() fromservice() down()
- else r.setLightColor(0x00ff00)
- end
- local d1,d2=r.detect()
- if d2=="solid"
- then r.swing() up() os.sleep(5) snp4x4() down()
- end
- os.sleep(10)
- end
- -- End --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement