Advertisement
Blackhome

CactusFarm

Jan 26th, 2025 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | Gaming | 0 0
  1. local x, y, z = ...
  2.  
  3. local move = require("move1")
  4.  
  5. local maxHeight = 0
  6.  
  7. local turtlePos = {0, 0, 0}
  8. local turtleLastPos = {turtlePos[1], turtlePos[2], turtlePos[3]}
  9.  
  10. local turtleVec = {1, 0}
  11. local turtleLastVec = {turtleVec[1], turtleVec[2]}
  12.  
  13. local workMode = 1
  14.  
  15. local function forward()
  16.     move.Forward()
  17.     turtlePos[1] = turtlePos[1] + turtleVec[1]
  18.     turtlePos[2] = turtlePos[2] + turtleVec[2]
  19. end
  20. local function upward()
  21.     move.Up()
  22.     turtlePos[3] = turtlePos[3] + 1
  23. end
  24. local function downward()
  25.     move.Down()
  26.     turtlePos[3] = turtlePos[3] - 1
  27. end
  28. local function turnLeft()
  29.     turtle.turnLeft()
  30.     local buffer = turtleVec[1]
  31.     turtleVec[1] = turtleVec[2]
  32.     turtleVec[2] = -buffer
  33. end
  34. local function turnRight()
  35.     turtle.turnRight()
  36.     local buffer = turtleVec[1]
  37.     turtleVec[1] = -turtleVec[2]
  38.     turtleVec[2] = buffer
  39. end
  40.  
  41. local function varInArr(var, arr)
  42.     for i = 1, #arr, 1 do
  43.         if var == arr[i] then
  44.             return true
  45.         end
  46.     end
  47.     return false
  48. end
  49.  
  50. local function selectItemFromList(itemList)
  51.     local itemA = turtle.getItemDetail()
  52.     if itemA then
  53.         if varInArr(itemA.name, itemList) then
  54.             return true
  55.         end
  56.     end
  57.  
  58.     for i=1, 16, 1 do
  59.         if turtle.getItemCount(i) > 0 then
  60.             turtle.select(i)
  61.             local itemB = turtle.getItemDetail()
  62.             if itemB then
  63.                 if varInArr(itemB.name, itemList) then
  64.                     return true
  65.                 end
  66.             end
  67.         end
  68.     end
  69.     return false
  70. end
  71.  
  72.  
  73.  
  74. local function goToStart()
  75.     if workMode == 1 then
  76.         turtleLastPos = {turtlePos[1], turtlePos[2], turtlePos[3]}
  77.         turtleLastVec = {turtleVec[1], turtleVec[2]}
  78.         workMode = 2
  79.     end
  80.  
  81.     while turtleVec[1] > -1 do
  82.         turnRight()
  83.     end
  84.     if (turtlePos[1] + turtlePos[2] + math.abs(turtlePos[3])) > 0 then
  85.         while turtlePos[3] < maxHeight do
  86.             upward()
  87.         end
  88.         while turtlePos[1] > 0 do
  89.             forward()
  90.         end
  91.         turnRight()
  92.         while turtlePos[2] > 0 do
  93.             forward()
  94.         end
  95.         turnLeft()
  96.         while turtlePos[3] > 0 do
  97.             downward()
  98.         end
  99.         while turtlePos[3] < 0 do
  100.             upward()
  101.         end
  102.     end
  103. end
  104.  
  105. local function goToLastPosition()
  106.     workMode = 3
  107.  
  108.     while turtlePos[3] < math.max(maxHeight, turtleLastPos[3]) do
  109.         upward()
  110.     end
  111.     turnLeft()
  112.     while turtlePos[2] < turtleLastPos[2] do
  113.         forward()
  114.     end
  115.     turnLeft()
  116.     while turtlePos[1] < turtleLastPos[1] do
  117.         forward()
  118.     end
  119.     while turtlePos[3] > turtleLastPos[3] do
  120.         downward()
  121.     end
  122.  
  123.     while not (turtleVec[1] == turtleLastVec[1] and turtleVec[2] == turtleLastVec[2]) do
  124.         turnLeft()
  125.     end
  126.  
  127.     workMode = 1
  128. end
  129.  
  130. local function getItemFromList(itemList)
  131.     local bItem = true
  132.     while not selectItemFromList(itemList) do
  133.         bItem = false
  134.         goToStart()
  135.         print("Item like ", itemList[1], " is needed. Pls input more of it.")
  136.         io.read()
  137.     end
  138.     if not bItem then
  139.         goToLastPosition()
  140.     end
  141. end
  142.  
  143. local height = 5
  144. local length = 9
  145. local width = 9
  146.  
  147. if x then
  148.     length = tonumber(x)
  149. end
  150. if y then
  151.     height = tonumber(y)
  152. end
  153. if z then
  154.     width = tonumber(z)
  155. end
  156.  
  157.  
  158. local bottomBlock = { "minecraft:dirt"}
  159. local sandBlock = { "minecraft:sand", "minecraft:red_sand" }
  160. local cactusBlock = { "minecraft:cactus" }
  161. local fenceBlock = { "minecraft:spruce_fence" }
  162.  
  163. forward()
  164. forward()
  165. turnRight()
  166. forward()
  167. forward()
  168. turnLeft()
  169. upward()
  170.  
  171. local cntZ = 0
  172. while cntZ < width do
  173.     local cntX = 0
  174.     while cntX < length do
  175.         local cntY = 0
  176.         while cntY < height do
  177.             getItemFromList(bottomBlock)
  178.             turtle.placeDown()
  179.             upward()
  180.             getItemFromList(sandBlock)
  181.             turtle.placeDown()
  182.             upward()
  183.             getItemFromList(cactusBlock)
  184.             turtle.placeDown()
  185.             if cntX % 2 == 0 then
  186.                 getItemFromList(fenceBlock)
  187.                 turtle.place()
  188.             end
  189.             upward()
  190.             upward()
  191.             cntY = cntY + 1
  192.         end
  193.         if turtlePos[3] > maxHeight then
  194.             maxHeight = turtlePos[3]
  195.         end
  196.  
  197.         if not (cntX == length - 1) then
  198.             forward()
  199.             forward()
  200.  
  201.             while turtlePos[3] > 1 do
  202.                 downward()
  203.             end
  204.         end
  205.  
  206.         cntX = cntX + 1
  207.  
  208.     end
  209.     if not (cntZ == width - 1) then
  210.         if cntZ % 2 == 0 then
  211.             turnRight()
  212.             forward()
  213.             forward()
  214.             turnRight()
  215.         else
  216.             turnLeft()
  217.             forward()
  218.             forward()
  219.             turnLeft()
  220.         end
  221.         while turtlePos[3] > 1 do
  222.             downward()
  223.         end
  224.     end
  225.     cntZ = cntZ + 1
  226. end
  227.  
  228. goToStart()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement