Advertisement
Silasko

Farmer SetFieldSize

Aug 4th, 2023 (edited)
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. local SLOT_COUNT = 16
  2. colLength = 0
  3. rowLength = 0
  4.  
  5. function refuelTurtle()
  6.     for slot = 1, SLOT_COUNT, 1 do
  7.         turtle.select(slot)
  8.             local item = turtle.getItemDetail(slot)
  9.             if (turtle.refuel(2)) then
  10.                 print("Turtle refueled")
  11.                 return true
  12.             end
  13.     end
  14. end
  15.  
  16. function checkFuel()
  17.     local fuel = turtle.getFuelLevel()
  18.     if(fuel < 20) then
  19.        refuelTurtle()
  20.     else
  21.         print("No fuel needed")
  22.     end
  23. end
  24.  
  25. function detectStartPoint()
  26.     local state, itemName = turtle.inspectDown()
  27.     if(itemName.name == "minecraft:log") then
  28.         checkFuel()
  29.         defx,defy,defz = gps.locate()
  30.         print("Default GPS coords: ",defx,defy,defz)
  31.         turtle.up()
  32.         turtle.forward()
  33.     end
  34. end
  35.  
  36. function detectFieldColSize()
  37.     while turtle.detect() == false do
  38.         turtle.forward()
  39.     end
  40.         local state, itemName = turtle.inspect()
  41.         if(itemName.name == "minecraft:fence") then
  42.                 local x,y,z = gps.locate()
  43.                 print("GPS coords: ",x,y,z)
  44.                 leng = math.abs(defx-x)
  45.                 if(leng > 0) then
  46.                     colLength = leng
  47.                 else
  48.                     colLength = math.abs(defz-z)
  49.                 end
  50.             print("Column length:",colLength)
  51.         end
  52.     turtle.turnRight()
  53.     detectFieldRowSize()
  54. end
  55.  
  56. function detectFieldRowSize()
  57.     while turtle.detect() == false do
  58.         turtle.forward()
  59.     end
  60.         local state, itemName = turtle.inspect()
  61.         if(itemName.name == "minecraft:fence") then
  62.                 local x,y,z = gps.locate()
  63.                 print("GPS coords: ",x,y,z)
  64.                 leng = math.abs(defz-z)
  65.                 if(leng > 0) then
  66.                     rowLength = leng
  67.                 else
  68.                     rowLength = math.abs(defz-z)
  69.                 end
  70.             print("Row length:",rowLength)
  71.         end
  72.     turtle.turnRight()
  73.     returnHome()
  74. end
  75.  
  76. function returnHome()
  77.     col = colLength
  78.     row = rowLength
  79.     while col > 0 do
  80.         turtle.forward()
  81.         col = col-1
  82.     end
  83.     turtle.turnRight()
  84.     while row > 0 do
  85.         turtle.forward()
  86.         row = row-1
  87.     end
  88.     local x,y,z = gps.locate()
  89.     print("Position:", x," ,", y, " ,", z)
  90.     print("Return Home completed")
  91.     turtle.turnRight()
  92.     turtle.down()
  93. end
  94.  
  95. function moveFarmer()
  96.     turtle.forward()
  97. end
  98.  
  99. function turnLeft()
  100.     turtle.turnLeft()
  101.     turtle.forward()
  102.     turtle.turnLeft()
  103. end
  104.  
  105. function turnRight()
  106.     turtle.turnRight()
  107.     turtle.forward()
  108.     turtle.turnRight()
  109. end
  110.  
  111. function start()
  112.     checkFuel()
  113.     detectStartPoint()
  114.     detectFieldColSize()
  115.     print("Field Detected, starting harvest")
  116.     turtle.up()
  117.     rowLength = rowLength+1
  118.  
  119.     for col = 1, rowLength, 1 do
  120.         for row = 1, colLength, 1 do
  121.             moveFarmer()
  122.             print(string.format("position: column %d   row %d ",col,row))
  123.         end
  124.         --start of each col
  125.         checkFuel()
  126.         if(math.fmod(col,2) == 0) then
  127.             turnLeft()
  128.         else
  129.             turnRight()
  130.         end
  131.  
  132.     end
  133.     print("Column length:",colLength)
  134.     print("Row length:",rowLength)
  135. end
  136.  
  137. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement