Advertisement
HandieAndy

terraformer

Jun 16th, 2018
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.78 KB | None | 0 0
  1. --[[
  2. Author: Andrew Lalis
  3. File: terraformer.lua
  4. Version: 1.0
  5. Last Modified: 16-06-2018
  6.    
  7. Description:
  8. This script enables a robot to make drastic, albeit slow, changes to the
  9. terrain of an area, through several sub-functions, such as flattening, filling,
  10. and removal of trees.
  11. --]]
  12.  
  13. --Require statements and componenent definitions.
  14. local robot = require("robot")
  15. local component = require("component")
  16. local ic = component.inventory_controller
  17.  
  18. --Runtime Constants defined for this robot.
  19. local SAPLING_NAME = "minecraft:sapling"
  20. local SAPLING_DATA = 0
  21. local BONEMEAL_NAME = "minecraft:dye"
  22. local BONEMEAL_DATA = 15
  23.  
  24. --Global configuration variables.
  25.  
  26. --[[
  27. Exits the program.
  28. --]]
  29. local function quit()
  30.     print("#--------------------------------#")
  31.     print("# Program exited.                #")
  32.     os.exit()
  33. end
  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. return - boolean: True if at least one slot contains the item. That slot is now
  40. selected.
  41. --]]
  42. local function selectItemByName(item_name, item_data)
  43.     for i=1,16 do
  44.         local stack = ic.getStackInInternalSlot(i)
  45.         if (stack ~= nil and stack.name == item_name and stack.damage == item_data) then
  46.             robot.select(i)
  47.             return true
  48.         end
  49.     end
  50.     return false
  51. end
  52.  
  53. --[[
  54. Select an item, similar to selectItemByName, but if the item cannot be found,
  55. the user will be prompted to add it to the robot's inventory and press enter to
  56. continue.
  57. item_name - string: The id string for an item.
  58. item_data - number: The damage value, or variation of an item. Defaults to zero.
  59. return - nil: If set to be continuous, then if the item cannot be found, then
  60. the program will exit. If not, it will loop until the item is provided by the
  61. user.
  62. --]]
  63. local function selectSafely(item_name, item_data)
  64.     local success = selectItemByName(item_name, item_data)
  65.     while not success do
  66.         print("Cannot find "..item_name.." in inventory. Please add some, and press enter.")
  67.         io.read()
  68.         success = selectItemByName(item_name, item_data)
  69.     end
  70. end
  71.  
  72. local function repeatUntilSuccess(robot_func)
  73.     local success = robot_func()
  74.     while not success do
  75.         success = robot_func()
  76.     end
  77. end
  78.  
  79. --[[
  80. Uses the robot's axe to chop a tree, and quits if the lumber axe provided has
  81. less than 10% durability.
  82. --]]
  83. local function chopTree()
  84.     local durability = robot.durability()
  85.     if continuous and (durability == nil or durability < 0.1) then
  86.         print("Inadequate tool to chop trees, exiting.")
  87.         quit()
  88.     end
  89.     while (durability == nil) or (durability < 0.1) do
  90.         print("Please ensure that a lumber axe with at least 10% durability is equipped in the tool slot, and press enter.")
  91.         io.read()
  92.         durability = robot.durability()
  93.     end
  94.     robot.swing()
  95. end
  96.  
  97. local function ensureToolDurability()
  98.     local n1, n2, n3 = robot.durability()
  99.     while (n1 == nil and n2 == "no tool equipped") or (n1 < 0.01) do
  100.         print("Please enter a valid tool with enough durability, and press enter.")
  101.         io.read()
  102.         n1, n2, n3 = robot.durability()
  103.     end
  104. end
  105.  
  106. local function safeSwing(func)
  107.     ensureToolDurability()
  108.     repeatUntilSuccess(func)
  109. end
  110.  
  111. local function flattenSpot()
  112.     -- First try to dig until it can't dig anymore.
  113.     local displacement = 0
  114.     while robot.detectUp() do
  115.         safeSwing(robot.swingUp)
  116.         repeatUntilSuccess(robot.up)
  117.         displacement = displacement + 1
  118.     end
  119.     for i=1, displacement do
  120.         repeatUntilSuccess(robot.down)
  121.     end
  122.     -- Then place any floor blocks if needed.
  123.     local success, data = robot.detectDown()
  124.     if not (success and data == "solid") then
  125.         -- Remove any grass or other obstructions below.
  126.         if success then
  127.             local obstructed = success
  128.             while obstructed do
  129.                 safeSwing(robot.swingDown)
  130.                 obstructed, data = robot.detectDown()
  131.             end
  132.         end
  133.         selectSafely("minecraft:dirt", 0)
  134.         repeatUntilSuccess(robot.placeDown)
  135.     end
  136. end
  137.  
  138. local function flattenArea(length, width)
  139.     for row=1, length do
  140.         print("Beginning row "..row.." of "..length..".")
  141.         if (robot.detect()) then
  142.             safeSwing(robot.swing)
  143.         end
  144.         repeatUntilSuccess(robot.forward)
  145.         if (row%2) == 1 then
  146.             robot.turnRight()
  147.         else
  148.             robot.turnLeft()
  149.         end
  150.         for col=1, width-1 do
  151.             flattenSpot()
  152.             if robot.detect() then
  153.                 safeSwing(robot.swing)
  154.             end
  155.             repeatUntilSuccess(robot.forward)
  156.         end
  157.         flattenSpot()
  158.         if (row%2) == 1 then
  159.             robot.turnLeft()
  160.         else
  161.             robot.turnRight()
  162.         end
  163.     end
  164. end
  165.  
  166. local function getNumberInput(str, lower_bound, upper_bound)
  167.     print(str)
  168.     local choice = tonumber(io.read())
  169.     while (choice == nil or choice < lower_bound or choice > upper_bound) do
  170.         print("Invalid input! Enter a number n such that "..lower_bound.." <= n <= "..upper_bound)
  171.         choice = tonumber(io.read())
  172.     end
  173.     return choice
  174. end
  175.  
  176. local function flattenMenu()
  177.     length = getNumberInput("Enter the length (distance forward).", 1, 256)
  178.     width = getNumberInput("Enter the width (distance to the right).", 1, 256)
  179.     print("Flattening a "..length.." x "..width.." area. Total of "..(length*width).." spots to flatten.")
  180.     flattenArea(length, width)
  181. end
  182.  
  183. local function treeRemovalMenu()
  184.     quit()-- Implement this!
  185. end
  186.  
  187. local function mainMenu()
  188.     print("# Andrew's Terraformer           #")
  189.     print("# Copyright 2018 Andrew Lalis    #")
  190.     print("#--------------------------------#")
  191.     print("1. Flatten")
  192.     print("2. Remove Trees (Not yet implemented)")
  193.     print("Please enter a number corresponding to one of the options above, or -1 to quit.")
  194.     local choice = tonumber(io.read())
  195.     if choice == 1 then
  196.         flattenMenu()
  197.     elseif choice == 2 then
  198.         treeRemovalMenu()
  199.     else
  200.         quit()
  201.     end
  202. end
  203.  
  204. --[[
  205. Main function in which the iterations of the cycle are performed.
  206. --]]
  207. local function main()
  208.     mainMenu()
  209.     quit()
  210. end
  211.  
  212. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement