Advertisement
HandieAndy

slave_init.lua

May 12th, 2019
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. local shell = require("shell")
  2. local component = require("component")
  3. local event = require("event")
  4. local modem = component.modem
  5. local robot = require("robot")
  6. local computer = require("computer")
  7. local serialization = require("serialization")
  8. local sides = require("sides")
  9. local rs = component.redstone
  10. local ic = component.inventory_controller
  11.  
  12. local PORT = 10002
  13. local MY_DATA = {}
  14.  
  15. local BLOCK_NAME = "minecraft:dirt"
  16. local BLOCK_DATA = 0
  17.  
  18. local function getDependencies()
  19.     -- First get all dependencies.
  20.     print("Fetching the latest movescript...")
  21.     shell.execute("pastebin get 4c2AN8Jw /lib/movescript.lua -f")
  22. end
  23.  
  24. getDependencies()
  25. local ms = require("movescript")
  26.  
  27. local function selectItemByName(item_name, item_data)
  28.     for i=1,16 do
  29.         local stack = ic.getStackInInternalSlot(i)
  30.         if (stack ~= nil and stack.name == item_name and stack.damage == item_data) then
  31.             robot.select(i)
  32.             return true
  33.         end
  34.     end
  35.     return false
  36. end
  37.  
  38. local function selectEmptySlot()
  39.     for i = 1, 16 do
  40.         local stack = ic.getStackInInternalSlot(i)
  41.         if (stack == nil) then
  42.             robot.select(i)
  43.             return true
  44.         end
  45.     end
  46.     return false
  47. end
  48.  
  49. local function fetchItemFromChest(item_name, item_damage)
  50.     local size = ic.getInventorySize(sides.front)
  51.     if (size == nil) then
  52.         return false
  53.     end
  54.     for i = 1, size do
  55.         local stack = ic.getStackInSlot(sides.front, i)
  56.         if (stack ~= nil and stack.name == item_name and (item_damage == nil or stack.damage == item_damage)) then
  57.             selectEmptySlot()
  58.             ic.suckFromSlot(sides.front, i)
  59.             return true
  60.         end
  61.     end
  62.     return false
  63. end
  64.  
  65. local function getDirt()
  66.     for i = 1, 4 do
  67.         local success = fetchItemFromChest("minecraft:dirt", 0)
  68.         if not success then
  69.             i = i - 1
  70.             print("Please add some dirt to the inventory and press enter.")
  71.             io.read()
  72.         end
  73.     end
  74. end
  75.  
  76. local function returnToStart()
  77.     local s1 = "U2R" .. MY_DATA.current_length .. "FL" .. MY_DATA.index .. "FD"
  78.     ms.execute(s1)
  79. end
  80.  
  81. local function returnToCurrentPosition()
  82.     local s2 = MY_DATA.index .. "FR" .. MY_DATA.current_length .. "F"
  83.     ms.execute(s2)
  84. end
  85.  
  86. local function flattenSpot()
  87.     -- Dig out top part.
  88.     local displacement = 0
  89.     local success, data = robot.detectUp()
  90.     while (success) do
  91.         ms.execute("d_U")
  92.         success, data = robot.detectUp()
  93.         displacement = displacement + 1
  94.     end
  95.     ms.execute(displacement .. "D")
  96.  
  97.     -- Fill in floor.
  98.     displacement = 0
  99.     success, data = robot.detectDown()
  100.     while ((not success) or (data == "replaceable")) do
  101.         ms.execute("d_D")
  102.         success, data = robot.detectDown()
  103.         displacement = displacement + 1
  104.     end
  105.     for i = 1, displacement do
  106.         ms.execute("U")
  107.         local has_dirt = selectItemByName(BLOCK_NAME, BLOCK_DATA)
  108.         if not has_dirt then
  109.             print("Don't have any dirt. Going back for refill.")
  110.             -- Go back and refill.
  111.             ms.execute((displacement - i) .. "U")
  112.             returnToStart()
  113.             ms.execute("L")
  114.             -- Now facing an inventory with dirt.
  115.             getDirt()
  116.             ms.execute("L")
  117.             returnToCurrentPosition()
  118.             ms.execute((displacement - i) .. "D")
  119.         end
  120.  
  121.         robot.placeDown()
  122.     end
  123. end
  124.  
  125. local function chargeUntilFull()
  126.     local max = computer.maxEnergy()
  127.     local current = computer.energy()
  128.     while (current / max < 0.95) do
  129.         print("Charging... " .. ((current / max) * 100) .. "%")
  130.         rs.setOutput(sides.front, 15)
  131.         os.sleep(1)
  132.         current = computer.energy()
  133.     end
  134.     print("Fully charged.")
  135. end
  136.  
  137. local function ensureEnergyLevel(ratio)
  138.     if (computer.energy() / computer.maxEnergy() < ratio) then
  139.         print("Energy below threshold, going to recharge.")
  140.         returnToStart()
  141.         ms.execute("R")
  142.         chargeUntilFull()
  143.         ms.execute("R")
  144.         returnToCurrentPosition()
  145.     end
  146. end
  147.  
  148. modem.open(PORT)
  149. -- Tell the master we're ready.
  150. print("Ready to receive instructions.")
  151. modem.broadcast(PORT, "READY")
  152.  
  153. -- Wait for a movescript to the start location.
  154. local _, _, sender, port, dist, msg, data = event.pull("modem_message")
  155. local master_address = sender
  156. print("Got instructions from master: " .. msg)
  157. MY_DATA = serialization.unserialize(data)
  158. print("Got metadata: " .. data)
  159. robot.select(1)
  160. ic.equip()
  161. ms.execute("R")
  162. robot.suck()
  163. ms.execute("L")
  164.  
  165. print("Moving to start...")
  166. ms.execute(msg)
  167.  
  168. print("Beginning to dig length of " .. MY_DATA.length)
  169. for i = 1, MY_DATA.length do
  170.     -- Check energy level.
  171.     ms.execute("d_F")
  172.     MY_DATA.current_length = i
  173.     ensureEnergyLevel(0.4)
  174.     -- Check inventory space.
  175.     flattenSpot()
  176. end
  177.  
  178. print("Done! Returning to start.")
  179. returnToStart()
  180. modem.send(sender, PORT, "DONE")
  181. modem.close()
  182.  
  183. computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement