Advertisement
Guest User

farm.lua

a guest
Nov 18th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. -- Settings
  2.  
  3. local NAME = "Farm Pet"
  4. local HOST = "fpet.hackerspace"
  5. local PROTOCOL = "turtle"
  6.  
  7. -- Waypoints
  8. local waypoints = {
  9.     {722, 71, -3007},
  10.     {726, 70, -3007},
  11.     {726, 70, -2979},
  12.     {729, 70, -2979},
  13.     {729, 80, -3009},
  14.     {716, 82, -3009}
  15. }
  16.  
  17. local wpIndex = 0
  18. local waypointCount = 0
  19. for _ in pairs(waypoints)
  20. do
  21.     waypointCount = waypointCount + 1
  22. end
  23.  
  24. -- Rednet
  25.  
  26. local function initRednet()
  27.     print("Registering Turtle ...")
  28.     rednet.open("right")
  29.     rednet.host(HOST, PROTOCOL)
  30.     rednet.broadcast(NAME, "register_turtle")
  31.     print("Turtle registered.")
  32. end
  33.  
  34. local function openDoor()
  35.     local door = rednet.lookup("door", "blastdoor")
  36.     rednet.send(door, "open", "door")
  37. end
  38.  
  39. local function closeDoor()
  40.     local door = rednet.lookup("door", "blastdoor")
  41.     rednet.send(door, "close", "door")
  42. end
  43.  
  44. -- Transform
  45.  
  46. local destX, destY, destZ = 0,0,0
  47. local x,y,z = gps.locate()
  48. local rotation = 2
  49.  
  50. local function setWaypoint()
  51.     destX = waypoints[wpIndex][1]
  52.     destY = waypoints[wpIndex][2]
  53.     destZ = waypoints[wpIndex][3]
  54. end
  55.  
  56. local function foundWaypoint()
  57.     return (x==destX and y==destY and z==destZ)
  58. end
  59.  
  60. local function refuel()
  61.     turtle.suck()
  62.     turtle.refuel()
  63.     turtle.drop()
  64. end
  65.  
  66. local function setOrientation()
  67.     local neededRot = rotation
  68.    
  69.     print("Destination:", destX, destY, destZ)
  70.     print("Current Location:",x,y,z)
  71.    
  72.     if x~=destX or y~=destY or z~=destZ
  73.     then
  74.        
  75.     if z - destZ > 0
  76.     then
  77.         neededRot = 1
  78.     elseif z -destZ < 0
  79.     then
  80.         neededRot = 3
  81.     elseif x - destX < 0
  82.     then
  83.         neededRot = 4
  84.     elseif x - destZ > 0
  85.     then
  86.         neededRot = 2
  87.     end
  88.    
  89.     print("New Orientation: ", neededRot)
  90.    
  91.     while rotation ~= neededRot
  92.     do
  93.         if neededRot - rotation < 3 and neededRot - rotation > 0
  94.         then
  95.             turtle.turnLeft()
  96.             rotation = rotation + 1
  97.         else
  98.             turtle.turnRight()
  99.             rotation = rotation - 1
  100.         end
  101.        
  102.         if rotation > 4
  103.         then
  104.             rotation = 1
  105.         elseif rotation < 1
  106.         then
  107.             rotation = 4
  108.         end
  109.     end
  110.     end
  111. end
  112.  
  113. local function startMovement(direction)
  114.     print("Starting Movement ...")
  115.     while wpIndex < waypointCount + 2 and wpIndex > 0
  116.     do
  117.         print("Moving towards waypoint", wpIndex)
  118.        
  119.         -- Move forwards until there
  120.         while not foundWaypoint()
  121.         do        
  122.             turtle.forward()
  123.             x,y,z = gps.locate()
  124.        
  125.             if not x
  126.             then
  127.                 print("COULD NOT LOCATE")
  128.                 break
  129.             end
  130.        
  131.             local moveY = y ~= destY
  132.             while moveY
  133.             do
  134.                 -- print(y)
  135.                 if y < destY
  136.                 then
  137.                     if not turtle.up()
  138.                     then
  139.                         moveY = false
  140.                     else
  141.                         y = y + 1
  142.                         moveY = y ~= destY
  143.                     end
  144.                 elseif y > destY
  145.                 then
  146.                     if not turtle.down()
  147.                     then
  148.                         moveY = false
  149.                     else
  150.                         y = y - 1
  151.                         moveY = y ~= destY
  152.                     end
  153.                 end
  154.             end
  155.         end
  156.    
  157.         wpIndex = wpIndex + direction
  158.    
  159.         if wpIndex < waypointCount + 1 and wpIndex > 0
  160.         then
  161.             setWaypoint()
  162.             setOrientation()
  163.         end
  164.     end
  165. end
  166.  
  167. local function loadItems()
  168.  
  169.     local i,j = 1, 1
  170.     while i < 9
  171.     do
  172.         turtle.suck()
  173.         i = i + 1
  174.    
  175.         if i == 9 and j < 5
  176.         then
  177.             i = 1
  178.             j = j + 1
  179.             sleep(10)
  180.         end
  181.     end
  182.    
  183.     i = 1
  184.     while i < 17
  185.     do
  186.         local item = turtle.getItemDetail(i)
  187.         if item and item.name == "minecraft:sapling"
  188.         then
  189.            turtle.select(i)
  190.            turtle.drop()
  191.         end
  192.         i = i + 1
  193.     end
  194. end
  195.  
  196. local function unloadItems()
  197.     i = 1
  198.     while i < 17
  199.     do
  200.         turtle.select(i)
  201.         turtle.drop()
  202.         i = i + 1
  203.         sleep(1)
  204.     end
  205.     turtle.select(1)
  206. end
  207.  
  208. -- Init
  209.  
  210. print("Starting Farming ...")
  211.  
  212. initRednet()
  213. wpIndex = waypointCount
  214. setWaypoint()
  215.  
  216. -- Check if start location is correct
  217.  
  218. if x~=destX or y~=destY or z~=destZ
  219. then
  220.     print("Please plase turtle at correct location:")
  221.     print(destX, destY, destZ)
  222.     --return
  223. end
  224.  
  225. while true
  226. do
  227.     -- Collect stuff
  228.     loadItems()
  229.    
  230.     -- Move to base
  231.     wpIndex = waypointCount - 1
  232.     setWaypoint()
  233.     setOrientation()
  234.    
  235.     openDoor()
  236.     startMovement(-1)
  237.     closeDoor()
  238.    
  239.     -- Unload stuff
  240.     unloadItems()
  241.    
  242.     -- Refuel
  243.     turtle.down()
  244.     refuel()
  245.     openDoor()
  246.    
  247.     -- Move to farm
  248.     wpIndex = 2
  249.     setWaypoint()
  250.     setOrientation()
  251.     startMovement(1)
  252.     closeDoor()
  253. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement