Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local shell = require("shell")
- local component = require("component")
- local event = require("event")
- local modem = component.modem
- local robot = require("robot")
- local computer = require("computer")
- local serialization = require("serialization")
- local sides = require("sides")
- local rs = component.redstone
- local ic = component.inventory_controller
- local PORT = 10002
- local MY_DATA = {}
- local BLOCK_NAME = "minecraft:dirt"
- local BLOCK_DATA = 0
- local function getDependencies()
- -- First get all dependencies.
- print("Fetching the latest movescript...")
- shell.execute("pastebin get 4c2AN8Jw /lib/movescript.lua -f")
- end
- getDependencies()
- local ms = require("movescript")
- local function selectItemByName(item_name, item_data)
- for i=1,16 do
- local stack = ic.getStackInInternalSlot(i)
- if (stack ~= nil and stack.name == item_name and stack.damage == item_data) then
- robot.select(i)
- return true
- end
- end
- return false
- end
- local function selectEmptySlot()
- for i = 1, 16 do
- local stack = ic.getStackInInternalSlot(i)
- if (stack == nil) then
- robot.select(i)
- return true
- end
- end
- return false
- end
- local function fetchItemFromChest(item_name, item_damage)
- local size = ic.getInventorySize(sides.front)
- if (size == nil) then
- return false
- end
- for i = 1, size do
- local stack = ic.getStackInSlot(sides.front, i)
- if (stack ~= nil and stack.name == item_name and (item_damage == nil or stack.damage == item_damage)) then
- selectEmptySlot()
- ic.suckFromSlot(sides.front, i)
- return true
- end
- end
- return false
- end
- local function getDirt()
- for i = 1, 4 do
- local success = fetchItemFromChest("minecraft:dirt", 0)
- if not success then
- i = i - 1
- print("Please add some dirt to the inventory and press enter.")
- io.read()
- end
- end
- end
- local function returnToStart()
- local s1 = "U2R" .. MY_DATA.current_length .. "FL" .. MY_DATA.index .. "FD"
- ms.execute(s1)
- end
- local function returnToCurrentPosition()
- local s2 = MY_DATA.index .. "FR" .. MY_DATA.current_length .. "F"
- ms.execute(s2)
- end
- local function flattenSpot()
- -- Dig out top part.
- local displacement = 0
- local success, data = robot.detectUp()
- while (success) do
- ms.execute("d_U")
- success, data = robot.detectUp()
- displacement = displacement + 1
- end
- ms.execute(displacement .. "D")
- -- Fill in floor.
- displacement = 0
- success, data = robot.detectDown()
- while ((not success) or (data == "replaceable")) do
- ms.execute("d_D")
- success, data = robot.detectDown()
- displacement = displacement + 1
- end
- for i = 1, displacement do
- ms.execute("U")
- local has_dirt = selectItemByName(BLOCK_NAME, BLOCK_DATA)
- if not has_dirt then
- print("Don't have any dirt. Going back for refill.")
- -- Go back and refill.
- ms.execute((displacement - i) .. "U")
- returnToStart()
- ms.execute("L")
- -- Now facing an inventory with dirt.
- getDirt()
- ms.execute("L")
- returnToCurrentPosition()
- ms.execute((displacement - i) .. "D")
- end
- robot.placeDown()
- end
- end
- local function chargeUntilFull()
- local max = computer.maxEnergy()
- local current = computer.energy()
- while (current / max < 0.95) do
- print("Charging... " .. ((current / max) * 100) .. "%")
- rs.setOutput(sides.front, 15)
- os.sleep(1)
- current = computer.energy()
- end
- print("Fully charged.")
- end
- local function ensureEnergyLevel(ratio)
- if (computer.energy() / computer.maxEnergy() < ratio) then
- print("Energy below threshold, going to recharge.")
- returnToStart()
- ms.execute("R")
- chargeUntilFull()
- ms.execute("R")
- returnToCurrentPosition()
- end
- end
- modem.open(PORT)
- -- Tell the master we're ready.
- print("Ready to receive instructions.")
- modem.broadcast(PORT, "READY")
- -- Wait for a movescript to the start location.
- local _, _, sender, port, dist, msg, data = event.pull("modem_message")
- local master_address = sender
- print("Got instructions from master: " .. msg)
- MY_DATA = serialization.unserialize(data)
- print("Got metadata: " .. data)
- robot.select(1)
- ic.equip()
- ms.execute("R")
- robot.suck()
- ms.execute("L")
- print("Moving to start...")
- ms.execute(msg)
- print("Beginning to dig length of " .. MY_DATA.length)
- for i = 1, MY_DATA.length do
- -- Check energy level.
- ms.execute("d_F")
- MY_DATA.current_length = i
- ensureEnergyLevel(0.4)
- -- Check inventory space.
- flattenSpot()
- end
- print("Done! Returning to start.")
- returnToStart()
- modem.send(sender, PORT, "DONE")
- modem.close()
- computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement