Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --gFarm v2
- --DistantTemple based ontutorial from ..?
- --Initialize variables
- local time = 0 -- time start at 0
- cycles = 0 -- times the harvest has been repeated
- delay = 450 -- delay befor next cycle
- slots = {}
- slots[1] = "sorter"
- slots[2] = ""
- slots[3] = ""
- slots[4] = ""
- slots[5] = ""
- slots[6] = ""
- slots[7] = "potato"
- slots[8] = "carrot"
- slots[9] = "beetroot_seeds"
- slots[10] = "pumpkin_seeds"
- slots[11] = "melon_seeds"
- slots[12] = "wheat_seeds"
- slots[13] = "wood"
- slots[14] = "log"
- slots[15] = "lava"
- slots[16] = "coal"
- s = 5 -- default seed slot
- se =6 -- default upper seed slot
- sFuel = 16 -- default fuel slot
- h = 0 -- height variable start at 0 or entered later
- plant = {beet, wheat, pumpkin, melon, potato, carrot}
- plant.beet = {seed, veg}
- plant.wheat = {seed, veg}
- plant.pumpkin = {seed, veg}
- plant.melon = {seed, veg}
- plant.potatoe = {seed} --for consistency
- plant.carrot = {seed}
- plant.beet.seed = 1 -- default slots in turtle
- plant.wheat.seed = 2
- plant.pumpkin.seed = 3
- plant.melon.seed = 4
- plant.potatoe = 5
- plant.carrot = 6
- pushQty = 3 -- max fuel push each time
- --local slotsAvail {1 = false, 2 = false, }
- levels = {["A"] = 0, ["B"] = 5}
- -- ["C"] = 9, ["D"] = 13, ["E"] = 17} --[this is a list
- -- of the levels to harvest. the numbers ar how far the turtle goes
- -- from the ground to each one. order irrelevant
- xChest = -1 -- to be over chest, lift is at (0,0)
- xCoord = xChest -- assume alwas start in Chest location
- yChest = 0 -- beware of changing this
- yCoord = yChest --yCoord and xCoord are updated as turtle moves.
- xLift = 0
- yLift = 0
- xMinCoord = 0
- xMaxCoord = 11 --12 blocks
- yMinCoord = 0
- yMaxCoord = 8 -- 9 blocks
- xStart = xMinCoord --Set the starting X location equal to the min X coordinate
- xLimit = xMaxCoord --Sets the ending X location equal to the max X coordinate
- yStart = yMinCoord
- yLimit = yMaxCoord
- xDirection = 1 -- +1 goes forward and -1 goes backward
- yDirection = 1 -- added by DT
- stuckTimer = 3 --Number of seconds to delay before retrying move
- fHigh = 5000
- fLow = 2000
- fCritical = 500
- fStop = 150
- function rFuel()
- local function checkAndFuel()
- local function ref(slot)
- turtle.select(slot)
- term.write("Fuel level = " .. fLevel)
- turtle.refuel()
- term.write("now level = " .. fLevel)
- end
- for i = 16, 1, -1 do
- detail = turtle.getItemDetail(i)
- if detail then
- name = detail.name
- if string.match(name, "lava") then ref(i)
- elseif string.match(name, "coal") then print(" coal "..i); ref(i) -- catches coal, charcoal and coal block
- elseif string.match(name, "wood") then ref(i)
- elseif string.match(name, "log") then ref(i)
- elseif fLevel<fLow then
- print("no lava, coal, wood, or logs found")
- print("I am short of fuel. Level is: " .. fLevel)
- print("Put fuel in my inventory or my supply chest")
- print("I will just eat anything burnable if I get below "..fCritical)
- else print("no ready fuel. Level = " .. fLevel)
- end
- if fLevel < fCritical then print("Heeelp starving")
- end
- end
- end
- end
- fLevel = turtle.getFuelLevel()
- detail = {}
- checkAndFuel() -- normally does once
- fLevel = turtle.getFuelLevel()
- fLevel2=1; fLevel1 =0 --initialse and force once
- while fLevel < fCritical and fLevel2 > fLevel1 do
- fLevel1 = turtle.getFuelLevel()
- checkAndFuel()
- fLevel2 = turtle.getFuelLevel()
- end
- end
- function critFuel()
- --getFuel()
- fLevel = turtle.getFuelLevel()
- while fLevel < fCritical do
- print("Below critical not moving. Fill NOW. Level = " .. fLevel)
- sleep(3)
- crFuel()
- fLevel = turtle.getFuelLevel()
- end
- end
- function crFuel()
- fLevel = turtle.getFuelLevel()
- if fLevel < fLow then rFuel()
- end
- end
- function getFuel()
- meta = {}
- item = {}
- success, detail = turtle.inspectDown()
- name = detail.name
- if string.match(name, "chest") then
- chest = peripheral.wrap("bottom")
- for i = 1, chest.size() do
- item = chest.getItem(i)
- if chest.getItemMeta(i) then meta = chest.getItemMeta(i)
- if string.match(meta.name, "coal") then
- chest.pushItems("up", i, pushQty, sFuel) -- to any slot in turtle --max pushQty
- end
- end
- end
- end
- end
- function sortInvent()
- end
- function pickIf() --inspects, and harvests if ready
- success, block = turtle.inspectDown()
- if success then
- name = block.name; print(" name = ".. name)
- meta = block.metadata; print(" meta = ".. meta)
- if name == "minecraft:beetroot" and meta == 3 then pick()
- elseif name == "minecraft:wheat"and meta == 7 then pick()
- elseif name == "minecraft:potatoe" and meta == 7 then pick()
- elseif name == "minecraft:carrot" and meta == 7 then pick()
- elseif name == "minecraft:melon" then pick()
- elseif name == "minecraft:pumpkin" then pick()
- else print("unlisted, or unripe" )
- end
- else
- --print("no block down")
- pick() -- hopefully won't get problematic stuff
- -- need this to try to replant empty ground
- end
- end
- function pick()
- turtle.digDown()
- turtle.suckDown()
- turtle.suck() --unnesecary
- plant()
- end
- function findSeeds() --find in its own inventory
- print("findseeds")
- -- sleep(2)
- full = false -- seed full flag empty by default
- ss = 1
- while not full do --loop start ###########
- turtle.select(s)
- detail = turtle.getitemDetail(s)
- if detail then --check seed full or not breakout check
- if string.match(detail.name,"seed") then
- if turtle.getItemCount(s) >= 63 then full = true --set flag
- end -- s is full count
- else -- this must be a non seed in the slot! (no allowance for potatoe etc)
- for e = 1, 16 do
- if turtle.getItemCount(e) == 0 then
- turtle.select(s)
- turtle.transferTo(e)
- if turtle.getItemCount(s) == 0 then break end
- end
- end
- end
- end
- local detail = turtle.getItemDetail(ss)
- if detail then
- print("detail = ".. detail.name)
- local n = detail.name
- local c = detail.count
- if string.match(n,"seed") then
- turtle.transferTo(s)
- end -- st match
- end -- success
- ss = ss + 1
- end -- while loop ############################
- end -- function findSeeds
- function plant()
- turtle.select(s)
- if turtle.getItemCount(s) == 0 then
- findSeeds()
- end
- --which slot ??????????
- --check databse of locations for water etc and which crop
- turtle.placeDown()
- end
- function deposit() -- assumes start from lift at (0,0)
- while xCoord > xChest and yCoord == yChest do
- bk(); xCoord = xCoord - xDirection --bk into (-1,0) to be over chest
- end
- findSeeds() -- to put seeds in s before depositing in chest
- --should detect to check its a chest
- for sl = 1, 16 do
- if sl ~= s then -- need to add list and method of exclusions
- if sl ~= sFuel then
- turtle.select(sl)
- turtle.refuel() -- so it uses up any fuel from all slots here. change if harvesting wood!!
- turtle.dropDown()
- end
- end
- end
- while xCoord < xLift and yCoord == yLift do
- fd(); xCoord = xCoord + xDirection --bk into (-1,0) to be over chest
- end
- end
- function harvest()--fn h() -- must have lead in and lead out boundary conditions
- --critFuel()
- pickIf();
- fd(); xCoord = xCoord + xDirection -- leave boundary (boundary condition breaker??)
- pickIf()
- fd(); xCoord = xCoord + xDirection -- leave boundary make double sure of that
- while yCoord >= yMinCoord and yCoord < yMaxCoord do -- >=yMin allowed as yCoord only increases iin these loops
- while xCoord > xMinCoord and xCoord < xMaxCoord do
- pickIf() -- inspects and picks chains to pick and then plant
- fd(); xCoord = xCoord + xDirection
- --print(" xCo = " .. xCoord .. " yCo = " .. yCoord)
- end -- for x
- pickIf()
- if xDirection == 1 then --Determine where the turtle should go based on its direction
- turtle.turnLeft()--turn then Move to the next row
- fd(); yCoord = yCoord + yDirection --DT update yCoord
- turtle.turnLeft()
- xDirection = -1 --hard coded heading -x
- pickIf(); fd(); xCoord = xCoord + xDirection -- leave boundary
- elseif xDirection == -1 then -- ie if dir <> 1 so -1 -- dir ==1
- turtle.turnRight()--Move to next row -- y<ymax
- fd(); yCoord = yCoord + yDirection --DT update yCoord
- turtle.turnRight()
- xDirection = 1
- pickIf() fd(); xCoord = xCoord + xDirection -- leave boundary
- else
- --print("yCoord reached yMaxCoord = " .. yCoord)
- pickIf(); print("the never else in harvest")
- end-- dir ==1 end of x
- pickIf()
- -- fd(); xCoord = xCoord + xDirection
- --print("xCo = " .. xCoord .. " yCo = " .. yCoord .. " xDir = " .. xDirection)
- end--for y
- while xCoord >= xMinCoord and xCoord < xMaxCoord do -- y loop will not allow
- -- so this row added to sort boundary conditions
- pickIf() -- inspects and picks chains to pick and then plant
- fd(); xCoord = xCoord + xDirection
- --print(" xCo = " .. xCoord .. " yCo = " .. yCoord)
- end -- for x
- pickIf()
- end-- for harvest
- function fd() -- may add number later-- local a = a or 1
- while turtle.forward() == false do
- print("Get out of my back-way! Fuel Level = " .. turtle.getFuelLevel())
- if turtle.getFuelLevel() < 100 then
- print("refuel me now ")
- sleep(stuckTimer)
- turtle.refuel()
- end
- end
- end
- function bk() -- may add number later
- -- local a = a or 1
- while turtle.back() == false do
- print("Get out of my back-way! Fuel Level = " .. turtle.getFuelLevel())
- if turtle.getFuelLevel() < 100 then
- print("refuel me now ")
- sleep(stuckTimer)
- turtle.refuel()
- end
- end
- end
- function returnToStorage()
- print("R2S")
- --print("AA xCoord = " .. xCoord .. "yCoord = " .. yCoord.. "xD = " .. xDirection.. "yD = " .. yDirection)
- if xDirection == 1 then --Turn the turtle to face toward Y 0
- turtle.turnRight(); yDirection = -1
- else --assme xDirection == -1
- turtle.turnLeft(); yDirection = -1
- end --Turn the turtle to face toward Y 0
- --print("BB xCoord = " .. xCoord .. "yCoord = " .. yCoord.. "xD = " .. xDirection.. "yD = " .. yDirection)
- while yCoord > yLift do --changed from Chest/Park to start Nov 2018
- fd(); yCoord = yCoord + yDirection -- as yDir is -1 this counts down
- end--Move the turtle to Y 0
- yDirection = 1 --(reset for next use)
- --print("CC xCoord = " .. xCoord .. " yCoord = " .. yCoord.. " xD = " .. xDirection.. " yD = " .. yDirection)
- turtle.turnLeft(); xDirection = 1 --Turn the turtle to face toward X +ve
- --print("DD xCoord = " .. xCoord .. " yCoord = " .. yCoord.. " xD = " .. xDirection.. " yD = " .. yDirection)
- while xCoord > xLift do --changed from Chest to start Nov 2018
- bk(); xCoord = (xDirection * -1) + xCoord --(*-1 for reverse!)
- end
- print("EE xCoord = " .. xCoord .. " yCoord = " .. yCoord.. " xD = " .. xDirection.. " yD = " .. yDirection)
- sleep(3)
- end
- function fromChestToStart()
- print("Pk2St")
- if xDirection == -1 then -- turn round -- face +x
- turtle.turnRight();turtle.turnRight(); xDirection = xDirection * -1
- print("turned round xDirection = ".. xDirection)
- end
- while xCoord < xLift do -- go forward to xLift...
- fd(); xCoord = xCoord + xDirection -- chest could be > 1 back from start.
- end
- --[[ the Chest location and the turtle must be on y start coordinate
- and the chest must be there. Following code not implemented.
- if yCoord > yStart do
- turtle.turnRight() -- now facing -y
- yDirection = -1
- while yCoord > yStart do
- fd(); yCoord = yCoord + yDirection -- as yDir is -1 this counts down
- end--Move the turtle to Y 0
- turtle.turnLeft() -- now facing +y
- yDirection = 1; xDirection = 1 --facing +x
- end
- --]]
- end
- function initialise()
- getFuel()
- crFuel() -- checks fuel and refuels if below lFuel
- critFuel()
- turtle.select(s) -- default I will overide in plant()
- xDirection = 1
- cycles = 0
- end
- function operate()
- getFuel()
- crFuel() -- checks fuel and refuels if below lFuel
- while xCoord < xLift and yCoord == yLift do --only if in line with chest
- fd(); xCoord = xCoord + xDirection -- go forward to lift block (0,0)
- end
- for l,h in pairs(levels) do --see table at start
- critFuel()
- print("Level ".. l .. " is up " .. h)
- sleep(1)
- for u = 1, h do turtle.up() end
- --[[ fd(); xCoord = xCoord + xDirection -- fd() and Update the X coordinate variable --Exit storage
- remmed as it now lifts at (1,1) --]]
- term.write("op: xCo = " .. xCoord .. " yCo = " .. yCoord)
- harvest()--Harvest
- returnToStorage() -- should arrive in lift location
- for u = 1, h do turtle.down() end
- deposit() -- to offload and later stock fuel and any seeds - for next level
- end
- while xCoord > xChest and yCoord == yChest do
- bk(); xCoord = xCoord - xDirection --bk into (-1,0) to be over chest
- end
- cycles = cycles + 1; time = time + delay -- don't need both or even either
- myTimer = os.startTimer(delay) -- need to reset timer
- print("tmrset. time = "..time.."dly = ".. delay .. "cyc =" .. cycles)
- while xCoord < xLift and yCoord == yStart do
- fd(); xCoord = xCoord + xDirection --bk into (-1,0) to be over chest
- end
- end
- -- ##### MAIN PROGRAM FLOW #######
- initialise() --no movement
- operate() -- timer is set at end of operate
- deposit()
- fromChestToStart()
- while true do --TIMING LOOP FOR 2ND AND ONWARDS
- local event, par1 = os.pullEvent()
- if event == "timer" --[[ and par1 == myTimer --]] then
- print("operating now. Time = ".. time .. " par1 = " .. par1)
- operate()
- deposit()
- elseif event == "key" then
- print("You pressed "..keys.getName(par1).."!")
- --insert key options here--break
- end
- end
- print( "Harvest Complete" )
- print("should never get here!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement