Advertisement
Codetemple

gFarm v2d

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