Advertisement
fyrkantis

GreenhouseTurtle

Jan 19th, 2025 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. local COAL, COMPOST, SOIL, BROWN, RED = "quark:charcoal_block", "farmersdelight:organic_compost", "farmersdelight:rich_soil", "minecraft:brown_mushroom", "minecraft:red_mushroom"
  2. local CHESTS = {
  3.     COAL={x=-419, y=71, z=-532, heading=1},
  4.     COMPOST={x=-419, y=71, z=-533, heading=1},
  5.     SOIL={x=-419, y=71, z=-534, heading=1},
  6.     BROWN={x=-419, y=72, z=-534, heading=1},
  7.     RED={x=-419, y=73, z=-534, heading=1}
  8. }
  9. local LANES = {
  10.     {x=-420, y=71, z=-533, heading=3, length=24, left=true, right=true},
  11.     {x=-420, y=71, z=-530, heading=3, length=24, right=true},
  12.     {x=-420, y=71, z=-536, heading=3, length=24, left=true}
  13. }
  14. local LANES_LENGTH = 3
  15. local laneIndex = 1
  16.  
  17. function calculatePositionHeading()
  18.     write("Calculating position... ")
  19.     local curX, curY, curZ = gps.locate()
  20.     print("Done!")
  21.    
  22.     write("Calculating compass heading... ")
  23.     local turns = 0 -- How many right turns the turtle just did to find an empty spot.
  24.     while turtle.detect() do
  25.         turtle.turnRight()
  26.         turns = turns + 1
  27.     end
  28.     turtle.forward()
  29.     local newX, _, newZ = gps.locate()
  30.    
  31.     -- Calculates heading.
  32.     local heading
  33.     if newZ < curZ then heading = 0
  34.     elseif newX > curX then heading = 1
  35.     elseif newZ > curZ then heading = 2
  36.     elseif newX < curX then heading = 3 end
  37.    
  38.     -- Returns to old position and heading.
  39.     turtle.back()
  40.     for _=1, turns % 4 do
  41.         turtle.turnLeft()
  42.     end
  43.     heading = (heading - turns) % 4
  44.    
  45.     print("Done!")
  46.     return {x=curX, y=curY, z=curZ, heading=heading}
  47. end
  48.  
  49. local pos = calculatePositionHeading()
  50.  
  51. function setOffset(distance)
  52.     if pos.heading == 0 then pos.z = pos.z - distance
  53.     elseif pos.heading == 1 then pos.x = pos.x + distance
  54.     elseif pos.heading == 2 then pos.z = pos.z + distance
  55.     elseif pos.heading == 3 then pos.x = pos.x - distance end
  56. end
  57. function offsetForward(newPos)
  58.     if pos.heading == 0 then return pos.z - newPos.z
  59.     elseif pos.heading == 1 then return newPos.x - pos.x
  60.     elseif pos.heading == 2 then return newPos.z - pos.z
  61.     elseif pos.heading == 3 then return pos.x - newPos.x end
  62. end
  63. function offsetSide(newPos) -- TODO: Re-use code better.
  64.     if pos.heading == 0 then return pos.x - newPos.x
  65.     elseif pos.heading == 1 then return pos.z - newPos.z
  66.     elseif pos.heading == 2 then return newPos.x - pos.x
  67.     elseif pos.heading == 3 then return newPos.z - pos.z end
  68. end
  69. function forward()
  70.     local moved = turtle.forward()
  71.     if moved then setOffset(1) end
  72.     return moved
  73. end
  74. function back()
  75.     local moved = turtle.forward()
  76.     if moved then setOffset(-1) end
  77.     return moved
  78. end
  79. function up()
  80.     local moved = turtle.up()
  81.     if moved then pos.y = pos.y + 1 end
  82.     return moved
  83. end
  84. function down()
  85.     local moved = turtle.down()
  86.     if moved then pos.y = pos.y - 1 end
  87.     return moved
  88. end
  89. function turnRight()
  90.     local moved = turtle.turnRight()
  91.     if moved then pos.heading = (pos.heading + 1) % 4 end
  92.     return moved
  93. end
  94. function turnLeft()
  95.     local moved = turtle.turnLeft()
  96.     if moved then pos.heading = (pos.heading - 1) % 4 end
  97.     return moved
  98. end
  99. function turnToward(newPos)
  100.     local moved = false
  101.     if offsetSide(newPos) > 0 then moved = turnLeft()
  102.     else moved = turnRight() end
  103.     return moved
  104. end
  105. function turnTo(newHeading)
  106.     if (pos.heading - newHeading) % 4 == 1 then turnLeft()
  107.     else while pos.heading ~= newHeading do turnRight() end end
  108. end
  109.  
  110. function goTo(newPos)
  111.     while true do
  112.         while offsetForward(newPos) > 0 do if not forward() then break end end
  113.         if pos.x == newPos.x and pos.y == newPos.y and pos.z == newPos.z then break end
  114.         turnToward(newPos)
  115.         while pos.y < newPos.y do if not up() then break end end
  116.         while pos.y > newPos.y do if not down() then break end end
  117.         while offsetForward(newPos) < 0 do turnToward(newPos) end
  118.     end
  119. end
  120. function goToChest(chest)
  121.     goTo(chest)
  122.     turnTo(chest.heading)
  123. end
  124.  
  125. function selectItem(name)
  126.     for i=1, 16 do
  127.         local item = turtle.getItemDetail(i)
  128.         if item ~= nil and item.name == name then
  129.             turtle.select(i)
  130.             return true
  131.         end
  132.     end
  133.     return false
  134. end
  135. function countItem(name)
  136.     local sum = 0
  137.     for i=1, 16 do
  138.         local item = turtle.getItemDetail(i)
  139.         if item ~= nil and item.name == name then
  140.             sum = sum + item.count
  141.         end
  142.     end
  143.     return sum
  144. end
  145.  
  146. function tendBlock()
  147.     local exists, block = turtle.inspect()
  148.     if block.name == SOIL then
  149.         write("Found rich soil... ")
  150.         selectItem(SOIL)
  151.         turtle.dig()
  152.         exists = false
  153.     end
  154.     if not exists then
  155.         if not selectItem(COMPOST) then
  156.             print("No organic compost!")
  157.             return
  158.         end
  159.         turtle.place()
  160.         print("Placed organic compost!")
  161.         if countItem(BROWN) >= 1 then
  162.             up()
  163.             local slot = turtle.getSelectedSlot() + 1
  164.             if slot <= 16 then turtle.select(slot) end
  165.             local item = turtle.getItemDetail()
  166.             if item == nil or item.name ~= BROWN then
  167.                 selectItem(BROWN)
  168.                 print("Found mushroom")
  169.             end
  170.             turtle.place()
  171.             down()
  172.         end
  173.     end
  174. end
  175.  
  176. while true do
  177.     goTo({x=-419, y=71, z=-529})
  178.     if countItem(SOIL) > 0 then
  179.         write("Dumping rich soil... ")
  180.         goToChest(CHESTS.SOIL)
  181.         selectItem(SOIL)
  182.         turtle.drop()
  183.         print("Done!")
  184.     end
  185.     if countItem(BROWN) > 32 then
  186.         write("Dumping rich brown mushrooms... ")
  187.         goToChest(CHESTS.BROWN)
  188.         selectItem(BROWN)
  189.         turtle.drop()
  190.         print("Done!")
  191.     end
  192.     if turtle.getFuelLevel() < 10000 then
  193.         selectItem(COAL)
  194.         turtle.refuel()
  195.     end
  196.     if countItem(COAL) < 32 then
  197.         write("Refueling coal... ")
  198.         goToChest(CHESTS.COAL)
  199.         selectItem(COAL)
  200.         turtle.suck(turtle.getItemSpace())
  201.         print("Done!")
  202.     end
  203.     if countItem(COMPOST) < 32 then
  204.         write("Refueling organic compost... ")
  205.         goToChest(CHESTS.COMPOST)
  206.         selectItem(COMPOST)
  207.         turtle.suck(64 - countItem(COMPOST))
  208.         print("Done!")
  209.     end
  210.     local lane = LANES[laneIndex]
  211.     write("Moving to lane "..laneIndex.."... ")
  212.     goTo(lane)
  213.     print("Done!")
  214.     turnTo(lane.heading)
  215.     print("Tending to lane "..laneIndex.."... ")
  216.     for _=1, lane.length do
  217.         if lane.left then
  218.             turnLeft()
  219.             tendBlock()
  220.             turnRight()
  221.         end
  222.         if lane.right then
  223.             turnRight()
  224.             tendBlock()
  225.             turnLeft()
  226.         end
  227.         if not forward() then
  228.             write("Aborted (blocked)")
  229.             break
  230.         end
  231.     end
  232.     laneIndex = (laneIndex % LANES_LENGTH) + 1
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement