Advertisement
Codetemple

gFarm2h

Nov 24th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.36 KB | None | 0 0
  1. --gFarm v2
  2. --DistantTemple based ontutorial from ..?
  3. --Initialize variables
  4. local time = 0 -- time start at 0
  5. cycles = 0  -- times the harvest has been repeated
  6. delay = 450     -- delay befor next cycle
  7. slots = {}
  8. slots[1] = "sorter"
  9. slots[2] = ""
  10. slots[3] = ""
  11. slots[4] = ""
  12. slots[5] = ""
  13. slots[6] = ""
  14. slots[7] = "potato"
  15. slots[8] = "carrot"
  16. slots[9] = "beetroot_seeds"
  17. slots[10] = "pumpkin_seeds"
  18. slots[11] = "melon_seeds"
  19. slots[12] = "wheat_seeds"
  20. slots[13] = "wood"
  21. slots[14] = "log"
  22. slots[15] = "lava"
  23. slots[16] = "coal"
  24.  
  25. s = 5   -- default seed slot
  26. se =6   -- default upper seed slot
  27. sFuel = 16  -- default fuel slot
  28. h = 0   -- height variable start at 0 or entered later
  29. plant = {beet, wheat, pumpkin, melon, potato, carrot}
  30. plant.beet = {seed, veg}
  31. plant.wheat = {seed, veg}
  32. plant.pumpkin = {seed, veg}
  33. plant.melon = {seed, veg}
  34. plant.potatoe = {seed} --for consistency
  35. plant.carrot = {seed}
  36. plant.beet.seed     = 1     -- default slots in turtle
  37. plant.wheat.seed    = 2
  38. plant.pumpkin.seed  = 3
  39. plant.melon.seed    = 4
  40. plant.potatoe       = 5
  41. plant.carrot        = 6
  42. pushQty = 3 -- max fuel push each time
  43. --local slotsAvail {1 = false, 2 = false,  }
  44. levels = {["A"] = 0, ["B"] = 5}
  45. -- ["C"] = 9, ["D"] = 13, ["E"] = 17}  --[this is a list
  46. -- of the levels to harvest. the numbers ar how far the turtle goes
  47. -- from the ground to each one. order irrelevant
  48. xChest = -1 -- to be over chest, lift is at (0,0)
  49. xCoord = xChest -- assume alwas start in Chest location
  50. yChest = 0 -- beware of changing this
  51. yCoord = yChest --yCoord and xCoord are updated as turtle moves.
  52. xLift = 0
  53. yLift = 0
  54. xMinCoord = 0
  55. xMaxCoord = 11 --12 blocks
  56. yMinCoord = 0
  57. yMaxCoord = 8 -- 9 blocks
  58. xStart = xMinCoord --Set the starting X location equal to the min X coordinate
  59. xLimit = xMaxCoord --Sets the ending X location equal to the max X coordinate
  60. yStart = yMinCoord
  61. yLimit = yMaxCoord
  62. xDirection = 1 -- +1 goes forward and -1 goes backward
  63. yDirection = 1 -- added by DT
  64. stuckTimer = 3 --Number of seconds to delay before retrying move
  65. fHigh = 5000
  66. fLow = 2000
  67. fCritical = 500
  68. fStop = 150
  69.  
  70.  
  71. function rFuel()
  72. local function checkAndFuel()
  73.     local function ref(slot)
  74.         turtle.select(slot)
  75.         term.write("Fuel level = " .. fLevel)
  76.         turtle.refuel()
  77.         term.write("now level = " .. fLevel)
  78.     end
  79.         for i = 16, 1, -1 do
  80.             detail = turtle.getItemDetail(i)
  81.             if detail then     
  82.                 name = detail.name
  83.                 if string.match(name, "lava") then ref(i)
  84.                 elseif string.match(name, "coal") then print(" coal "..i); ref(i)   -- catches coal, charcoal and coal block
  85.                 elseif string.match(name, "wood") then ref(i)
  86.                 elseif string.match(name, "log") then ref(i)
  87.                 elseif fLevel<fLow then
  88.                     print("no lava, coal, wood, or logs found")
  89.                     print("I am short of fuel. Level is: " .. fLevel)
  90.                     print("Put fuel in my inventory or my supply chest")
  91.                     print("I will just eat anything burnable if I get below "..fCritical)
  92.                 else print("no ready fuel. Level = " .. fLevel)
  93.                 end
  94.                 if fLevel < fCritical then print("Heeelp starving")
  95.                 end
  96.             end
  97.         end
  98.     end
  99.     fLevel = turtle.getFuelLevel()
  100.    
  101.     detail = {}
  102.     checkAndFuel() -- normally does once
  103.     fLevel = turtle.getFuelLevel()
  104.     fLevel2=1; fLevel1 =0 --initialse and force once
  105.     while fLevel < fCritical and fLevel2 > fLevel1 do
  106.         fLevel1 = turtle.getFuelLevel()
  107.         checkAndFuel()
  108.         fLevel2 = turtle.getFuelLevel()
  109.     end
  110. end
  111.  
  112. function critFuel()
  113. --getFuel()
  114. fLevel = turtle.getFuelLevel()
  115.     while fLevel < fCritical do
  116.         print("Below critical not moving. Fill NOW. Level = " .. fLevel)
  117.         sleep(3)
  118.         crFuel()
  119.         fLevel = turtle.getFuelLevel()
  120.     end
  121. end
  122.  
  123. function crFuel()
  124.     fLevel = turtle.getFuelLevel()
  125.     if fLevel < fLow then rFuel()
  126.     end
  127. end
  128.  
  129. function getFuel()
  130.     meta = {}
  131.     item = {}
  132.     success, detail = turtle.inspectDown()
  133.     name = detail.name
  134.     if string.match(name, "chest") then
  135.         chest = peripheral.wrap("bottom")
  136.         for i = 1, chest.size() do
  137.             item = chest.getItem(i)
  138.             if chest.getItemMeta(i) then meta = chest.getItemMeta(i)
  139.                 if string.match(meta.name, "coal") then
  140.                     chest.pushItems("up", i, pushQty, sFuel) -- to any slot in turtle --max pushQty
  141.                 end
  142.             end
  143.         end
  144.     end
  145. end
  146.  
  147. function sortInvent()
  148. end
  149.  
  150. function pickIf() --inspects, and harvests if ready
  151.     success, block = turtle.inspectDown()
  152.     if success then
  153.         name = block.name; print(" name = ".. name)
  154.         meta = block.metadata; print(" meta = ".. meta)
  155.         if      name == "minecraft:beetroot" and meta == 3 then pick()
  156.         elseif  name == "minecraft:wheat"and meta == 7      then pick()
  157.         elseif  name == "minecraft:potatoe" and meta == 7   then pick()
  158.         elseif  name == "minecraft:carrot" and meta == 7    then pick()
  159.         elseif  name == "minecraft:melon"   then pick()
  160.         elseif  name == "minecraft:pumpkin" then pick()
  161.         else print("unlisted, or unripe" )
  162.         end
  163.     else
  164.         --print("no block down")
  165.         pick() -- hopefully won't get problematic stuff
  166.   --        need this to try to replant empty ground
  167.     end
  168. end
  169.  
  170. function pick()
  171.     turtle.digDown()
  172.     turtle.suckDown()
  173.     turtle.suck()   --unnesecary
  174.     plant()
  175. end
  176.  
  177. function findSeeds() --find in its own inventory
  178.   print("findseeds")
  179.   -- sleep(2)
  180.   full = false -- seed full flag empty by default
  181.   ss = 1
  182.   while not full do --loop start ###########
  183.  
  184.     turtle.select(s)
  185.     detail = turtle.getitemDetail(s)
  186.     if detail then --check seed full or not breakout check
  187.         if  string.match(detail.name,"seed") then
  188.             if turtle.getItemCount(s) >= 63 then full = true --set flag
  189.             end -- s is full count
  190.         else -- this must be a non seed in the slot! (no allowance for potatoe etc)
  191.             for e = 1, 16 do
  192.                 if turtle.getItemCount(e) == 0 then
  193.                     turtle.select(s)
  194.                     turtle.transferTo(e)
  195.                     if  turtle.getItemCount(s) == 0 then break end
  196.                 end
  197.             end
  198.         end
  199.     end
  200.    
  201.     local detail = turtle.getItemDetail(ss)
  202.     if detail then
  203.       print("detail = ".. detail.name)
  204.       local n = detail.name
  205.       local c = detail.count
  206.       if string.match(n,"seed") then
  207.         turtle.transferTo(s)
  208.       end  -- st match
  209.     end  -- success
  210.     ss = ss + 1
  211.   end -- while loop ############################
  212. end -- function findSeeds
  213.  
  214. function plant()
  215.     turtle.select(s)
  216.  if turtle.getItemCount(s) == 0 then
  217.    findSeeds()
  218.  end
  219.     --which slot ??????????
  220.     --check databse of locations for water etc and which crop
  221.     turtle.placeDown()
  222. end
  223.  
  224. function deposit() -- assumes start from lift at (0,0)
  225.     while xCoord > xChest and yCoord == yChest do
  226.         bk(); xCoord = xCoord - xDirection --bk into (-1,0) to be over chest
  227.     end
  228.     findSeeds() -- to put seeds in s before depositing in chest
  229. --should detect to check its a chest
  230.     for sl = 1, 16 do
  231.         if sl ~= s then -- need to add list and method of exclusions
  232.             if sl ~= sFuel then
  233.                 turtle.select(sl)
  234.                 turtle.refuel() -- so it uses up any fuel from all slots here. change if harvesting wood!!
  235.                 turtle.dropDown()
  236.             end
  237.         end
  238.     end
  239.     while xCoord < xLift and yCoord == yLift do
  240.         fd(); xCoord = xCoord + xDirection --bk into (-1,0) to be over chest
  241.     end
  242. end
  243.  
  244. function harvest()--fn h() -- must have lead in and lead out boundary conditions
  245.     --critFuel()
  246.     pickIf();
  247.     fd();   xCoord = xCoord + xDirection -- leave boundary (boundary condition breaker??)
  248.     pickIf()
  249.     fd();   xCoord = xCoord + xDirection -- leave boundary make double sure of that
  250.    
  251.     while yCoord >= yMinCoord and yCoord < yMaxCoord do -- >=yMin allowed as yCoord only increases iin these loops
  252.         while xCoord > xMinCoord and xCoord < xMaxCoord do
  253.             pickIf() -- inspects and picks  chains to pick and then plant
  254.             fd();   xCoord = xCoord + xDirection
  255.             --print(" xCo = " .. xCoord .. " yCo = " .. yCoord)
  256.         end -- for x
  257.         pickIf()
  258.         if xDirection == 1 then --Determine where the turtle should go based on its direction
  259.             turtle.turnLeft()--turn then Move to the next row
  260.             fd(); yCoord = yCoord + yDirection --DT update yCoord
  261.             turtle.turnLeft()
  262.             xDirection = -1 --hard coded heading  -x
  263.             pickIf(); fd(); xCoord = xCoord + xDirection -- leave boundary
  264.         elseif xDirection == -1 then -- ie if dir <> 1 so -1 -- dir ==1
  265.             turtle.turnRight()--Move to next row -- y<ymax
  266.             fd(); yCoord = yCoord + yDirection --DT update yCoord
  267.             turtle.turnRight()
  268.             xDirection = 1
  269.             pickIf() fd();  xCoord = xCoord + xDirection -- leave boundary
  270.         else
  271.             --print("yCoord reached yMaxCoord = " .. yCoord)
  272.             pickIf(); print("the never else in harvest")
  273.         end-- dir ==1 end of x
  274.         pickIf()
  275.         -- fd();    xCoord = xCoord + xDirection
  276.         --print("xCo = " .. xCoord .. " yCo = " .. yCoord .. " xDir = " .. xDirection)
  277.     end--for y
  278.     while xCoord >= xMinCoord and xCoord < xMaxCoord do -- y loop will not allow
  279.         -- so this row added to sort boundary conditions
  280.         pickIf() -- inspects and picks  chains to pick and then plant
  281.         fd();   xCoord = xCoord + xDirection
  282.         --print(" xCo = " .. xCoord .. " yCo = " .. yCoord)
  283.     end -- for x
  284.     pickIf()
  285. end-- for harvest
  286.  
  287.  
  288. function fd() -- may add number later-- local a = a or 1
  289.     while turtle.forward() == false do
  290.     print("Get out of my back-way! Fuel Level = " .. turtle.getFuelLevel())
  291.     if turtle.getFuelLevel() < 100 then
  292.     print("refuel me now ")
  293.         sleep(stuckTimer)
  294.         turtle.refuel()
  295.         end
  296.     end
  297. end
  298. function bk() -- may add number later
  299. -- local a = a or 1
  300.     while turtle.back() == false do
  301.     print("Get out of my back-way! Fuel Level = " .. turtle.getFuelLevel())
  302.     if turtle.getFuelLevel() < 100 then
  303.         print("refuel me now ")
  304.         sleep(stuckTimer)
  305.         turtle.refuel()
  306.     end
  307.     end
  308. end
  309. function returnToStorage()
  310.     print("R2S")
  311.     --print("AA xCoord = " .. xCoord .. "yCoord = " .. yCoord.. "xD = " .. xDirection.. "yD = " .. yDirection)
  312.     if xDirection == 1 then --Turn the turtle to face toward Y 0
  313.         turtle.turnRight(); yDirection = -1
  314.     else --assme xDirection == -1
  315.         turtle.turnLeft(); yDirection = -1
  316.     end --Turn the turtle to face toward Y 0
  317.     --print("BB xCoord = " .. xCoord .. "yCoord = " .. yCoord.. "xD = " .. xDirection.. "yD = " .. yDirection)
  318.     while yCoord > yLift do --changed from Chest/Park to start Nov 2018
  319.         fd(); yCoord = yCoord + yDirection -- as yDir is -1 this counts down
  320.     end--Move the turtle to Y 0
  321.     yDirection = 1 --(reset for next use)
  322.     --print("CC xCoord = " .. xCoord .. " yCoord = " .. yCoord.. " xD = " .. xDirection.. " yD = " .. yDirection)
  323.     turtle.turnLeft(); xDirection = 1 --Turn the turtle to face toward X +ve
  324.     --print("DD xCoord = " .. xCoord .. " yCoord = " .. yCoord.. " xD = " .. xDirection.. " yD = " .. yDirection)
  325.     while xCoord > xLift do --changed from Chest to start Nov 2018
  326.         bk(); xCoord = (xDirection * -1) + xCoord  --(*-1 for reverse!)
  327.     end
  328.     print("EE xCoord = " .. xCoord .. " yCoord = " .. yCoord.. " xD = " .. xDirection.. " yD = " .. yDirection)
  329.     sleep(3)
  330. end
  331.  
  332. function fromChestToStart()
  333.     print("Pk2St")
  334.     if xDirection == -1 then -- turn round -- face +x
  335.         turtle.turnRight();turtle.turnRight(); xDirection = xDirection * -1
  336.         print("turned round xDirection = ".. xDirection)
  337.     end
  338.     while xCoord < xLift do -- go forward to xLift...
  339.         fd(); xCoord = xCoord + xDirection  -- chest could be > 1 back from start.
  340.     end
  341.     --[[ the Chest location and the turtle must be on y start coordinate
  342.     and the chest must be there. Following code not implemented.
  343.     if yCoord > yStart do
  344.         turtle.turnRight() -- now facing -y
  345.         yDirection = -1
  346.         while yCoord > yStart do
  347.             fd(); yCoord = yCoord + yDirection -- as yDir is -1 this counts down
  348.         end--Move the turtle to Y 0
  349.         turtle.turnLeft() -- now facing +y
  350.         yDirection = 1; xDirection = 1 --facing +x
  351.     end
  352.     --]]
  353. end
  354.  
  355. function initialise()
  356. getFuel()
  357. crFuel() -- checks fuel and refuels if below lFuel
  358. critFuel()
  359. turtle.select(s) -- default I will overide in plant()
  360. xDirection = 1
  361. cycles = 0
  362. end
  363. function operate()
  364. getFuel()
  365. crFuel() -- checks fuel and refuels if below lFuel
  366. while xCoord < xLift and yCoord == yLift do --only if in line with chest
  367.     fd(); xCoord = xCoord + xDirection -- go forward to lift block (0,0)
  368. end
  369. for l,h in pairs(levels) do --see table at start
  370. critFuel()
  371.     print("Level ".. l .. " is up " .. h)
  372.     sleep(1)
  373.     for u = 1, h do turtle.up() end
  374.     --[[ fd();  xCoord = xCoord + xDirection    -- fd() and Update the X coordinate variable --Exit storage
  375.     remmed as it now lifts at (1,1) --]]
  376.     term.write("op: xCo = " .. xCoord .. " yCo = " .. yCoord)
  377.     harvest()--Harvest
  378.     returnToStorage() -- should arrive in lift location
  379.     for u = 1, h do turtle.down() end
  380.     deposit() -- to offload and later stock fuel and any seeds - for next level
  381. end
  382. while xCoord > xChest and yCoord == yChest do
  383.     bk(); xCoord = xCoord - xDirection --bk into (-1,0) to be over chest
  384. end
  385. cycles = cycles + 1; time = time + delay -- don't need both or even either
  386. myTimer = os.startTimer(delay) -- need to reset timer
  387. print("tmrset. time = "..time.."dly = ".. delay .. "cyc =" .. cycles)
  388. while xCoord < xLift and yCoord == yStart do
  389.     fd(); xCoord = xCoord + xDirection --bk into (-1,0) to be over chest
  390. end
  391. end
  392.  
  393. -- ##### MAIN PROGRAM FLOW #######
  394. initialise() --no movement
  395. operate() -- timer is set at end of operate
  396. deposit()
  397. fromChestToStart()
  398.  
  399. while true do --TIMING LOOP FOR 2ND AND ONWARDS
  400.   local event, par1 = os.pullEvent()
  401.   if event == "timer" --[[ and par1 == myTimer --]] then
  402.     print("operating now. Time = ".. time .. " par1 = " .. par1)
  403.     operate()
  404.     deposit()
  405.   elseif event == "key" then
  406.     print("You pressed "..keys.getName(par1).."!")
  407.     --insert key options here--break
  408.   end
  409. end
  410. print( "Harvest Complete" )
  411. print("should never get here!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement