Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------SOME VARIABLES--------------------
- t = turtle
- dataF = "sbdata/data"
- redstoneSleep = 1.0
- redstone.setOutput("bottom",false)
- seed = {g = 0, y = 0, s = 0, name=""}
- slot = {fuel = 1, sticks1 = 2, sticks2 = 3, seed = 4, extra = 5}
- pos={x = 1, z = 0, seed={} , actv={}, f = 2} --F(Face) = 2(North)
- pos.seed[0] = {x = -1, z = 0}
- pos.actv[0] = {x = -1, z = -1}
- pos.seed[1] = {x = 0, z = 1}
- pos.actv[1] = {x = 1, z = 1}
- pos.seed[2] = {x = 0, z = 0}
- pos.actv[2] = {x = 0, z = -1}
- pos.anlzer = {x=1,z=0}
- pos.chest = {x=-1,z=1}
- pos.bin = {x=1,z=-1}
- seedSlot = 4
- data={}
- -----------------LANG VARIABLES---------------------
- lang_noFuel = "Please insert a valid fuel in slot "..slot.fuel.."!"
- lang_noSticks = "Please insert Crop Sticks in slot "..slot.sticks1.." or "..slot.sticks2.."!"
- --lang_noRake = "Please insert a Hand Rake in slot "..slot.rake.."!"
- lang_noSeed = "Please insert a valid seeds in slot "..slot.seed.."!"
- lang_manySeeds = "There are too many seeds! Use 1 or 2 only."
- lang_maxedOut = "This seed is now 10/10/10!"
- lang_line = "---------------------------------------"
- lang_placingStick = "Placing sticks"
- lang_placingSeedsInAcv = "Placing seeds in Autonomous Activator"
- lang_placingSticksInAcv = "Placing sticks in Autonomous Activator"
- CCSA = "Computer Controlled Seed Analyzer"
- TRSHC = "Trash Can"
- CRPSTCK = "Crop Sticks without seeds"
- ----------------------------------------------------
- ----------------------------------------------------
- function loadData()
- if fs.exists(dataF) then
- local file = fs.open(dataF, "r")
- local data = file.readAll()
- file.close()
- pos.x = textutils.unserialize(data).x
- pos.z = textutils.unserialize(data).z
- pos.f = textutils.unserialize(data).f
- seed.g = textutils.unserialize(data).g
- seed.y = textutils.unserialize(data).y
- seed.s = textutils.unserialize(data).s
- seed.name = textutils.unserialize(data).name
- return true
- else
- -- data = {x = 1, z = 0, name = "", g = 0, y = 0, s = 0,}
- -- fs.makeDir("sbdata")
- -- local file = fs.open(dataF,"w")
- -- file.write(textutils.serialize(data))
- -- file.close()
- return false
- end
- end
- function saveData()
- local file = fs.open(dataF, "w")
- local data = {x = pos.x, z = pos.z, name = seed.name, g = seed.g, y = seed.y, s = seed.s, f = pos.f}
- file.write(textutils.serialize(data))
- file.close()
- sleep(0.1)
- end
- function clearData()
- print("Clear data")
- fs.delete(dataF)
- end
- ---------------BASIC TURTLE COMMANDS----------------
- function resetScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- function putInAnlzer()
- if checkCount(slot.seed,1) then
- lastSl = select(slot.seed)
- elseif checkCount(slot.sticks2,1) then
- lastSl = select(slot.sticks2)
- end
- succes = t.dropDown()
- select(lastSl)
- return succes
- end
- function wrapAnlzer()
- return peripheral.wrap("bottom")
- end
- function wrapModem()
- return peripheral.wrap("right")
- end
- function takeFromAnlzer()
- lastSl = select(slot.seed)
- succes = suckDown(64)
- select(lastSl)
- return succes
- end
- function isMaxedOut()
- if seed.g == 10 and seed.y == 10 and seed.s == 10 then
- return true
- end
- return false
- end
- function updateSeedMaxData(letEqualUpdate)
- local anl = wrapAnlzer()
- local modem = wrapModem()
- local g, y, s = anl.getSpecimenStats()
- if (seed.g+seed.s+seed.y)/3 < (g+y+s)/3 and not letEqualUpdate then
- seed.g = g
- seed.y = y
- seed.s = s
- modem.transmit(77, 78, "Seed is "..g.." / "..y.." / "..s)
- return true
- elseif (seed.g+seed.s+seed.y)/3 <= (g+y+s)/3 and letEqualUpdate then
- seed.g = g
- seed.y = y
- seed.s = s
- modem.transmit(77, 78, "Seed is "..g.." / "..y.." / "..s)
- return true
- end
- return false
- end
- function analyze(letEqualUpdate)
- move(pos.anlzer)
- print("Analyzing")
- if putInAnlzer() then
- local anl = wrapAnlzer()
- anl.analyze()
- while not anl.isAnalyzed() do
- os.sleep(0.2)
- end
- isUpdated = updateSeedMaxData(letEqualUpdate)
- if isMaxedOut()then
- takeFromAnlzer()
- seedMaxedOut()
- end
- takeFromAnlzer()
- return isUpdated
- end
- return false
- end
- function analyseSeed()
- if analyze() then
- if seedRepl > 1 then
- seedRepl = 0
- end
- breakStick(pos.seed[seedRepl])
- placeSeeds(pos.seed[seedRepl],pos.actv[seedRepl])
- trashSeed()
- storeYeld()
- seedRepl = seedRepl + 1
- else
- trashSeed()
- end
- end
- function redstoneOn()
- return redstone.setOutput("bottom",true)
- end
- function redstoneOff()
- return redstone.setOutput("bottom",false)
- end
- function placeSticks(tablePos)
- move(tablePos)
- print("Placing sticks")
- if sticks() then
- print(lang_placingStick)
- lastSelected = select(slot.sticks1)
- t.placeDown()
- select(lastSelected)
- end
- end
- function breakStick(tablePos)
- move(tablePos)
- print("Breaking sticks")
- t.digDown()
- end
- function placeSticksInActv(tablePos,doubleSticks)
- print("Placing sticks in right clicky machine")
- move(tablePos)
- if doubleSticks then
- sticks()
- dropDownFromSlot(slot.sticks1,1)
- end
- sticks()
- dropDownFromSlot(slot.sticks1,1)
- redstoneOn()
- sleep(redstoneSleep)
- redstoneOff()
- end
- function dropSeedsInActv(tablePos)
- move(tablePos)
- print("Placing seeds in right clicky machine")
- redstoneOn()
- dropDownFromSlot(slot.seed,1)
- sleep(redstoneSleep)
- redstoneOff()
- suckDownInSlot(12,64)
- if checkCount(12,1) then
- transferItem(12,slot.seed)
- print("Weeds detected ")
- return false
- end
- return true
- end
- function placeSeeds(seedPosTbl,actvPosTbl)
- placeSticks(seedPosTbl,false)
- while not dropSeedsInActv(actvPosTbl) do
- breakStick(seedPosTbl)
- placeSticks(seedPosTbl,false)
- end
- end
- function try(f, catch_f)
- local status, exception = pcall(f)
- if not status then
- catch_f(exception)
- end
- end
- function waitForSeedToGrow()
- move(pos.anlzer)
- print("Waiting for seeds to grow")
- local anl = wrapAnlzer()
- local modem = wrapModem()
- local hasPlant = false
- while not hasPlant do
- try(function()
- hasPlant = anl.hasPlant("WEST")
- end, function()
- placeSticksInActv(pos.actv[2],true)
- move(pos.anlzer)
- end)
- try(function()
- if anl.hasWeeds("WEST") then
- print("Removing weeds")
- breakStick(pos.seed[2])
- placeSticksInActv(pos.actv[2],true)
- move(pos.anlzer)
- end
- end, function()
- placeSticksInActv(pos.actv[2],true)
- move(pos.anlzer)
- end)
- sleep(1)
- modem.transmit(77, 78, "The turtle is still running")
- modem.transmit(77, 78, seed.name)
- modem.transmit(77, 78, "Seed is "..seed.g.." / "..seed.y.." / "..seed.s)
- end
- breakStick(pos.seed[2])
- return true
- end
- function trashSeed()
- move(pos.bin)
- print("Trashing seeds")
- --print(lang_)
- if compareItemInSlot(seed.name,slot.extra) then
- dropDownFromSlot(slot.extra,64)
- elseif compareItemInSlot(seed.name,slot.sticks2) then
- dropDownFromSlot(slot.sticks2,64)
- else
- dropDownFromSlot(slot.seed,64)
- end
- end
- function trashItem(slot)
- move(pos.bin)
- print("Trashing item")
- dropDownFromSlot(slot,64)
- end
- function storeYeld()
- move(pos.chest)
- print("Storing yeld")
- if not compareItemInSlot(seed.name,slot.extra) then
- dropDownFromSlot(slot.extra,64)
- elseif not compareItemInSlot("AgriCraft:cropsItem",slot.sticks2) then
- dropDownFromSlot(slot.sticks2,64)
- else
- dropDownFromSlot(slot.extra,64)
- end
- dropDownFromSlot(6,64)
- dropDownFromSlot(7,64)
- dropDownFromSlot(8,64)
- end
- --------------INVENTORY CONTROLLLER-----------------
- function select(slot)
- lastSl = t.getSelectedSlot()
- t.select(slot)
- return lastSl
- end
- function count(slot)
- return t.getItemCount(slot)
- end
- function checkCount(slot,number)
- if count(slot) >= number then
- return true
- end
- return false
- end
- function dropDown(number)
- return t.dropDown(number)
- end
- function dropDownFromSlot(slot,number)
- lastSelected = select(slot)
- if dropDown(number) then
- select(lastSelected)
- return true
- end
- select(lastSelected)
- return false
- end
- function suckDown(number)
- return t.suckDown(number)
- end
- function suckDownInSlot(slot,number)
- lastSelected = select(slot)
- if suckDown(number) then
- t.select(lastSelected)
- return true
- end
- select(lastSelected)
- return false
- end
- -----------------------------------------------
- function transferItem(fromSlot,toSlot)
- lastSl = select(fromSlot)
- t.transferTo(toSlot,64)
- select(lastSl)
- end
- function compareItemInSlot(item,slot)
- local itemInfo = t.getItemDetail(slot)
- if itemInfo ~= nil then
- --print("Comparing: "..item.." AND: "..itemInfo.name)
- if item == itemInfo.name then
- return true
- end
- end
- return false
- end
- function matchItemInSlot(item,slot)
- itemInfo = turtle.getItemDetail(slot)
- if itemInfo.name == item then
- return true
- end
- return false
- end
- ------------------TURTLE CHECKS-----------------------
- function fuel()
- lastSelected = t.getSelectedSlot()
- if t.getFuelLevel() < 70 then
- lastSelected = select(slot.fuel)
- if t.refuel(slot.fuel) then
- select(lastSelected)
- return true
- else
- noFuel()
- t.select(lastSelected)
- end
- end
- return true
- end
- function tidySticks()
- if compareItemInSlot("AgriCraft:cropsItem",slot.sticks1) then
- return true
- else
- if compareItemInSlot("AgriCraft:cropsItem",slot.sticks2) then
- transferItem(slot.sticks2,slot.sticks1)
- return true
- end
- end
- return false
- end
- function sticks()
- if tidySticks() then
- return true
- end
- noSticks()
- return true
- end
- function seeds()
- local seedCount = count(slot.seed)
- if seedCount > 2 then
- tooManySeeds()
- elseif seedCount == 2 or seedCount == 1 then
- if analyze() then
- local lastSelected = select(slot.seed)
- local seedInfo = t.getItemDetail()
- select(lastSelected)
- seed.name = seedInfo.name
- print("Seed set to: "..seed.name)
- return seedCount
- end
- end
- return 0
- end
- function rake()
- if compareItemInSlot("AgriCraft:handRake",slot.rake) then
- return true
- end
- noRake()
- return true
- end
- -------------------MESSAGES---------------------
- function noFuel()
- local modem = wrapModem()
- while not t.refuel(slot.fuel) do
- resetScreen()
- print (lang_noFuel)
- modem.transmit(77, 78, "Program stopped.")
- sleep(1)
- end
- end
- function noSticks()
- local modem = wrapModem()
- while not tidySticks() do
- resetScreen()
- print (lang_noSticks)
- modem.transmit(77, 78, "Program stopped.")
- sleep(1)
- end
- end
- function noRake()
- while not compareItemInSlot("AgriCraft:handRake",slot.rake) do
- resetScreen()
- print(lang_noRake)
- sleep(1)
- end
- return true
- end
- function noSeeds()
- local modem = wrapModem()
- while not checkCount(slot.seed,1) do
- resetScreen()
- print(lang_noSeed)
- modem.transmit(77, 78, "Program stopped.")
- sleep(1)
- end
- if seeds() >= 1 then
- resetScreen()
- return count(slot.seed)
- else
- noSeeds()
- end
- end
- function tooManySeeds()
- while checkCount(slot.seed,3) do
- resetScreen()
- print (lang_manySeeds)
- sleep(1)
- end
- return true
- end
- function seedMaxedOut()
- print("Seed is 10/10/10. Clearing crop sticks")
- local modem = wrapModem()
- modem.transmit(77, 78, "Seed is 10 / 10 / 10. Stopping program.")
- breakStick(pos.seed[0])
- if compareItemInSlot(seed.name,slot.sticks2) then
- trashItem(slot.sticks2)
- else
- trashItem(slot.extra)
- end
- storeYeld()
- breakStick(pos.seed[1])
- if compareItemInSlot(seed.name,slot.sticks2) then
- trashItem(slot.sticks2)
- else
- trashItem(slot.extra)
- end
- storeYeld()
- placeSeeds(pos.seed[1],pos.actv[1])
- placeSticksInActv(pos.actv[2],true)
- placeSticks(pos.seed[0])
- placeSticksInActv(pos.actv[0],false)
- move(pos.anlzer)
- clearData()
- modem.transmit(77, 78, "Program stopped.")
- error(lang_maxedOut)
- end
- -----------------TURTLE MOVEMENT----------------------
- function forward(n)
- for i = 1,n do
- fuel()
- if t.forward() then
- pos.z = pos.z - 1
- saveData()
- end
- end
- end
- function back(n)
- for i = 1,n do
- fuel()
- if t.back() then
- pos.z = pos.z + 1
- saveData()
- end
- end
- end
- function left(n)
- fuel()
- if t.turnLeft() then
- pos.f = pos.f - 1
- saveData()
- end
- for i = 1,n do
- fuel()
- if t.forward() then
- pos.x = pos.x - 1
- saveData()
- end
- end
- if t.turnRight() then
- pos.f = pos.f + 1
- saveData()
- end
- end
- function right(n)
- fuel()
- if t.turnRight() then
- pos.f = pos.f + 1
- saveData()
- end
- for i = 1,n do
- fuel()
- if t.forward() then
- pos.x = pos.x + 1
- saveData()
- end
- end
- if t.turnLeft() then
- pos.f = pos.f - 1
- saveData()
- end
- end
- function move(x,z)
- if z == nil then
- tbl = x
- x = tbl.x - pos.x
- z = tbl.z - pos.z
- else
- x = x - pos.x
- z = z - pos.z
- end
- if x > 0 then
- right(x)
- sleep(0.1)
- end
- if x < 0 then
- left(math.abs(x))
- sleep(0.1)
- end
- if z > 0 then
- back(z)
- sleep(0.1)
- end
- if z < 0 then
- forward(math.abs(z))
- sleep(0.1)
- end
- end
- ------------------------------------------------------
- ------------------------------------------------------
- resetScreen()
- seedRepl = 0
- select(1)
- function main()
- local modem = wrapModem()
- resetScreen()
- if fuel() and sticks() then
- if not loadData() then
- numOfSeeds = seeds()
- if numOfSeeds == 0 then
- numOfSeeds = noSeeds()
- end
- saveData()
- modem.transmit(77, 78, "The turtle is still running")
- modem.transmit(77, 78, seed.name)
- if numOfSeeds == 1 then
- placeSeeds(pos.seed[0],pos.actv[0])
- placeSticksInActv(pos.actv[2],true)
- waitForSeedToGrow()
- analyze(true)
- placeSeeds(pos.seed[1],pos.actv[1])
- end
- if numOfSeeds == 2 then
- placeSeeds(pos.seed[0],pos.actv[0])
- placeSeeds(pos.seed[1],pos.actv[1])
- end
- else
- modem.transmit(77, 78, "The turtle is still running")
- modem.transmit(77, 78, seed.name)
- modem.transmit(77, 78, "Seed is "..seed.g.." / "..seed.y.." / "..seed.s)
- if pos.f < 2 then
- t.turnRight()
- elseif pos.f > 2 then
- t.turnLeft()
- end
- move(pos.anlzer)
- local anl = wrapAnlzer()
- anl.analyze()
- for i=0,50 do
- if anl.isAnalyzed() then
- break
- end
- os.sleep(0.2)
- end
- if compareItemInSlot(seed.name,slot.seed) or compareItemInSlot(seed.name,slot.sticks2) or suckDownInSlot(slot.seed, 64) then
- analyseSeed()
- end
- move(pos.anlzer)
- end
- while not isMaxedOut() do
- modem.transmit(77, 78, "The turtle is still running")
- modem.transmit(77, 78, seed.name)
- placeSticksInActv(pos.actv[2],true)
- waitForSeedToGrow()
- analyseSeed()
- end
- end
- end --MAIN END
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement