vozdanac

vozdanac

Jan 22nd, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. -------------
  2. -- CONFIG: --
  3. -------------
  4.  
  5. local config = {
  6. -- Sleeping in seconds:
  7. sleep = {
  8. forRefuel = 5, -- Default: 10
  9. afterEmptying = 180 -- Default: 180
  10. },
  11.  
  12. -- Full name of crop as item and block and ripeness:
  13. crop = {
  14. block = "minecraft:potatoes",
  15. item = "minecraft:potato",
  16. ripe = 7 -- Default: 7 (crops have grow stages between 0 and 7)
  17. },
  18.  
  19. -- Blocks to listen to for instructions on how to move/interact:
  20. blocks = {
  21. forward = "minecraft:white_wool", -- optional (turtle will move forwards each step regardless, only useful for debugging)
  22. turnRight = "minecraft:green_wool", -- required
  23. turnLeft = "minecraft:blue_wool", -- required
  24.  
  25. emptyInventory = "minecraft:hopper", -- required
  26. refuelTurtle = "minecraft:yellow_wool" -- optional *(removing will require manual refueling)*
  27. },
  28.  
  29. -- Print out debug information on what the turtle is currently doing:
  30. debug = false -- Default: false
  31. }
  32.  
  33.  
  34. ----------------
  35. -- VARIABLES: --
  36. ----------------
  37.  
  38. -- Function getting called the next step:
  39. local BufferFunction = nil
  40.  
  41. -- Dictionary of block names and its effects on the turtle:
  42. local BlockList = {}
  43.  
  44.  
  45. ----------------
  46. -- FUNCTIONS: --
  47. ----------------
  48.  
  49. -- Function to print debug information: (with and without newline)
  50. local function debug_write(txt, ...)
  51. if not config.debug then return end
  52. io.write(string.format(txt, ...))
  53. end
  54. local function debug_print(txt, ...)
  55. debug_write(txt.."\n", ...)
  56. end
  57.  
  58. -- Print out current config at start:
  59. local function printWelcomeMessage()
  60. -- Clear screen:
  61. term.clear()
  62. term.setCursorPos(1, 1)
  63. -- Print text:
  64. local text = {
  65. "Welcome to the farming program!",
  66. "¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯",
  67. " Author: nirokay",
  68. " Repo: https://github.com/nirokay/Luadventures\n",
  69.  
  70. "Current config:",
  71. " Crop: " .. config.crop.item,
  72. " Idle & Refuel time: " .. config.sleep.afterEmptying .. "s and " .. config.sleep.forRefuel .. "s",
  73. " Debug printout: " .. tostring(config.debug)
  74. }
  75. print(table.concat(text, "\n"))
  76. sleep(2)
  77. end
  78.  
  79.  
  80. -- Add blocks to check for to BlockList dictionary:
  81. local function newBlock(blockName, blockFunction)
  82. local temp = {
  83. block = blockName,
  84. fn = blockFunction
  85. }
  86. BlockList[blockName] = blockFunction
  87. return temp
  88. end
  89.  
  90.  
  91. --------------------
  92. -- TURTLE BLOCKS: --
  93. --------------------
  94.  
  95. -- Movement:
  96. newBlock(config.blocks.forward, function(_)
  97. debug_print("Moving forwards...")
  98. -- Empty because move forward is called after every step.
  99. end)
  100. newBlock(config.blocks.turnRight, function(_)
  101. debug_print("Turning right...")
  102. turtle.turnRight()
  103. end)
  104. newBlock(config.blocks.turnLeft, function(_)
  105. debug_print("Turning left...")
  106. turtle.turnLeft()
  107. end)
  108.  
  109. -- Inventory:
  110. newBlock(config.blocks.emptyInventory, function(_)
  111. debug_print("Emptying inventory...")
  112. for i = 16, 1, -1 do
  113. turtle.select(i)
  114. turtle.dropDown()
  115. end
  116. debug_print(" > Going to sleep for %s seconds!", tostring(config.sleep.afterEmptying))
  117. sleep(config.sleep.afterEmptying)
  118. debug_print(" > Done sleeping.")
  119. end)
  120.  
  121. -- Refueling:
  122. newBlock(config.blocks.refuelTurtle, function(_)
  123. -- Sleep to allow items to get input through a hopper:
  124. debug_print("Refueling, going to sleep for %s seconds.", config.sleep.forRefuel)
  125. sleep(config.sleep.forRefuel)
  126.  
  127. -- Cycle through inventory and refuel:
  128. BufferFunction = function()
  129. debug_print(" > Done sleeping, refueling...")
  130. for i = 16, 1, -1 do
  131. turtle.select(i)
  132. turtle.refuel()
  133. end
  134. debug_print(" > Finished refueling!")
  135. end
  136. end)
  137.  
  138. -- Harvesting and Replanting:
  139. local function attemptReplanting()
  140. local currentItem = turtle.getItemDetail()
  141. -- Place potato from currently held item:
  142. if currentItem.name == config.crop.item then
  143. debug_print("Placing down crop!")
  144. turtle.placeDown()
  145. return
  146. end
  147.  
  148. -- Find potato in inventory and place it down: (more expensive on computation, please do not happen in praxis)
  149. debug_print("Locating crop to replant...")
  150. for i = 1, 16 do
  151. turtle.select(i)
  152. local newItem = turtle.getItemDetail()
  153. if newItem.name == config.crop.item then
  154. debug_print("Placing down crop!")
  155. turtle.placeDown()
  156. return
  157. end
  158. end
  159.  
  160. -- Failed to replant:
  161. debug_print("Could not locate crop in inventory to replant... carrying on.")
  162. end
  163. newBlock(config.crop.block, function(block)
  164. debug_write("Crop found... ")
  165. if block.state.age >= config.crop.ripe then
  166. debug_print("ready to harvest!")
  167. turtle.digDown()
  168. attemptReplanting()
  169. else
  170. debug_print("still growing...")
  171. end
  172. end)
  173.  
  174.  
  175. -----------
  176. -- MAIN: --
  177. -----------
  178.  
  179. local function main()
  180. -- Execute Buffer function from last step:
  181. if BufferFunction ~= nil then
  182. debug_print("Executing buffer function!")
  183. BufferFunction()
  184. BufferFunction = nil
  185. end
  186.  
  187. -- Check for the current block underneath turtle:
  188. local isBlock, block = turtle.inspectDown()
  189. if isBlock and block ~= nil then
  190. -- Execute Block function:
  191. if BlockList[block.name] ~= nil then
  192. BlockList[block.name](block)
  193. end
  194. end
  195.  
  196. -- Move forward: (gets called after every block check, because yeah... stuff)
  197. turtle.forward()
  198. end
  199.  
  200.  
  201. printWelcomeMessage()
  202. while true do
  203. main()
  204. end
  205.  
  206.  
Advertisement
Add Comment
Please, Sign In to add comment