Advertisement
HandieAndy

lumber_farm

Sep 30th, 2018
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. --[[
  2. Author: Andrew Lalis
  3. File: lumber_farm.lua
  4. Version: 2.0
  5. Last Modified: 04-11-2018
  6.    
  7. Description:
  8. This script will automate the farming of large spruce trees, and will chop and
  9. replant them, but not pick up items, since there are many mod items available
  10. which can do this more efficiently.
  11.  
  12. The robot should be given an 'unbreakable' tool with 5 reinforced upgrades.
  13. --]]
  14.  
  15. --Require statements and componenent definitions.
  16. local robot = require("robot")
  17. local component = require("component")
  18. local ms = require("movescript")
  19. local ic = component.inventory_controller
  20.  
  21. local move_to_start = "5F"
  22. local return_from_start = "5B"
  23.  
  24. local ROWS = 3
  25. local COLS = 2
  26. local TREE_SPACING = 3
  27.  
  28. -- Global counter.
  29. local TREES_CHOPPED = 0
  30.  
  31. --Runtime Constants defined for this robot.
  32. local SAPLING_NAME = "minecraft:sapling"
  33. local SAPLING_DATA = 1
  34.  
  35. --[[
  36. Select an item, given its name and damage value.
  37. item_name - string: The id string for an item.
  38. item_data - number: The damage value, or variation of an item. Defaults to zero.
  39. min_count - number: The minimum number of items to have.
  40. return - boolean: True if at least one slot contains the item. That slot is now
  41. selected.
  42. --]]
  43. local function selectItemByName(item_name, item_data, min_count)
  44.     for i=1,16 do
  45.         local stack = ic.getStackInInternalSlot(i)
  46.         if (stack ~= nil and stack.name == item_name and stack.damage == item_data and stack.size >= min_count) then
  47.             robot.select(i)
  48.             return true
  49.         end
  50.     end
  51.     return false
  52. end
  53.  
  54. --[[
  55. Select an item, similar to selectItemByName, but if the item cannot be found,
  56. the user will be prompted to add it to the robot's inventory and press enter to
  57. continue.
  58. item_name - string: The id string for an item.
  59. item_data - number: The damage value, or variation of an item. Defaults to zero.
  60. min_count - number: The minimum number of items to have.
  61. --]]
  62. local function selectSafely(item_name, item_data, min_count)
  63.     local success = selectItemByName(item_name, item_data, min_count)
  64.     while not success do
  65.         print("Cannot find "..min_count.."x "..item_name.." in inventory. Please add some, and press enter.")
  66.         io.read()
  67.         success = selectItemByName(item_name, item_data, min_count)
  68.     end
  69. end
  70.  
  71. --[[
  72. Gets the total number of items with the given data.
  73. item_name - string: The id string for an item.
  74. item_data - number: The damage value, or variation of an item. Defaults to zero.
  75. --]]
  76. local function getItemCount(item_name, item_data)
  77.     local count = 0
  78.     for i=1,16 do
  79.         local stack = ic.getStackInInternalSlot(i)
  80.         if (stack ~= nil and stack.name == item_name and stack.damage == item_data) then
  81.             count = count + stack.size
  82.         end
  83.     end
  84.     return count
  85. end
  86.  
  87. --[[
  88. return - bool: True if the tree is grown, false otherwise.
  89. --]]
  90. local function isTreeGrown()
  91.     local success, str = robot.detect()
  92.     return (success and str == "solid")
  93. end
  94.  
  95. --[[
  96. Plants a sapling, and if it can't place one at first, loops until it is
  97. possible. Assumes the robot is at the starting position:
  98. OO
  99. OO
  100. R
  101. Where O=dirt, R=robot.
  102. --]]
  103. local function plantTree()
  104.     local success, str = robot.detect()
  105.     if (success and (str == "passable" or str == "solid" or str == "replaceable")) then
  106.         return
  107.     end
  108.     local saplings_needed = 4
  109.     if (getItemCount(SAPLING_NAME, SAPLING_DATA) < saplings_needed) then
  110.         print("Not enough saplings. Needed: "..saplings_needed..". Add some and press ENTER.")
  111.         io.read()
  112.     end
  113.     selectSafely(SAPLING_NAME, SAPLING_DATA, 1)
  114.     ms.execute("2FRP")
  115.     selectSafely(SAPLING_NAME, SAPLING_DATA, 1)
  116.     ms.execute("LBP")
  117.     selectSafely(SAPLING_NAME, SAPLING_DATA, 1)
  118.     ms.execute("RP")
  119.     selectSafely(SAPLING_NAME, SAPLING_DATA, 1)
  120.     ms.execute("LBP")
  121. end
  122.  
  123. --[[
  124. Uses the robot's axe to chop a tree, and quits if the lumber axe provided has
  125. less than 10% durability.
  126. return - integer: 1 if the tree was chopped, 0 otherwise.
  127. --]]
  128. local function chopTree()
  129.     if (isTreeGrown()) then
  130.         ms.execute("S")
  131.         plantTree()
  132.         return 1
  133.     end
  134.     return 0
  135. end
  136.  
  137. --[[
  138. Moves to the next tree in a row.
  139. current_col - integer: The current column.
  140. col_count - integer: The total number of columns.
  141. --]]
  142. local function moveToNextTree(current_col, col_count)
  143.     if (current_col < col_count) then
  144.         ms.execute("d_LFR3FRFL"..(TREE_SPACING - 1).."F")
  145.     end
  146. end
  147.  
  148. --[[
  149. Moves to the next row.
  150. current_row - integer: The row that was just finished.
  151. row_count - integer: The total number of rows.
  152. col_count - integer: The total number of columns.
  153. --]]
  154. local function moveToNextRow(current_row, row_count, col_count)
  155.     local script = "d_LFL"..((TREE_SPACING + 2) * (col_count - 1)).."FLF"
  156.     if (current_row < row_count) then
  157.         script = script..(TREE_SPACING + 2).."FL"
  158.     else
  159.         script = script.."L"
  160.     end
  161.     ms.execute(script)
  162. end
  163.  
  164. --[[
  165. Moves back to the start of the orchard.
  166. row_count - integer: The total number of rows.
  167. --]]
  168. local function moveToOrchardStart(row_count)
  169.     ms.execute("d_L"..((TREE_SPACING + 2) * (row_count - 1)).."FR")
  170. end
  171.  
  172. --[[
  173. Performs a function at each tree in the orchard.
  174. rows - integer: The total number of rows.
  175. cols - integer: The total number of columns.
  176. func - function: The function to execute at each position.
  177. --]]
  178. local function doForEachTree(rows, cols, func)
  179.     ms.execute(move_to_start)
  180.     for i=1,rows do
  181.         for k=1,cols do
  182.             func()
  183.             moveToNextTree(k, cols)
  184.         end
  185.         moveToNextRow(i, rows, cols)
  186.     end
  187.     moveToOrchardStart(rows)
  188.     ms.execute(return_from_start)
  189. end
  190.  
  191. --[[
  192. Chops an array of trees. The robot starts facing the first row.
  193. --]]
  194. local function chopOrchard(rows, cols)
  195.     doForEachTree(rows, cols, chopTree)
  196. end
  197.  
  198. --[[
  199. Reads any given arguments and uses them for program constants instead of
  200. default values.
  201. args - table: the arguments passed to the program.
  202. --]]
  203. local function getSettingsFromArgs(args)
  204.     ROWS = tonumber(args[1])
  205.     COLS = tonumber(args[2])
  206.     TREE_SPACING = tonumber(args[3])
  207.     move_to_start = args[4]
  208.     return_from_start = args[5]
  209. end
  210.  
  211. local args = {...}
  212. if (#args == 5) then
  213.     getSettingsFromArgs(args)
  214. end
  215.  
  216. chopOrchard(ROWS, COLS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement