Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- @file slavecode.lua
- --
- -------------------------------------------------------------------------------
- -- inventory slots
- local INVENTORY_FUEL_CHEST = 1
- local INVENTORY_ITEM_CHEST = 2
- local INVENTORY_BUCKET = 3
- local INVENTORY_STORAGE = 4
- local INVENTORY_IGNORE_MAP = {
- "minecraft:stone",
- "minecraft:cobblestone",
- "minecraft:grass_block",
- "minecraft:dirt",
- "minecraft:grass",
- "minecraft:snow",
- }
- -- directions
- local DIRECTION_NORTH = 1
- local DIRECTION_EAST = 2
- local DIRECTION_SOUTH = 3
- local DIRECTION_WEST = 4
- local DIRECTION_UP = 5
- local DIRECTION_DOWN = 6
- -- state and position handlers
- local state = "setting" -- "mining" "moving" "waiting" "setting"
- local facing = DIRECTION_EAST
- local x = 0
- local z = 0
- local y = 0
- local movex = nil
- local movey = nil
- -- file handler
- local file = "state.cc"
- -- wireless channels
- local initialChannel = 100
- local myChannel = nil
- -------------------------------------------------------------------------------
- -- Function declarations ------------------------------------------------------
- local message
- local unloadInventory
- local checkInventory
- local saveState
- local loadState
- local forward
- local nforward
- local up
- local down
- local left
- local right
- local refuel
- local updateLocation
- local updateHeight
- local turnTo
- local setting
- local getNextSpot
- local move
- local excavate
- -------------------------------------------------------------------------------
- -------------------------------------------------------------------------------
- message = function(str)
- print(str)
- --modem.transmit( initialChannel , myChannel , "- "..str )
- end
- -------------------------------------------------------------------------------
- unloadInventory = function()
- turtle.select(INVENTORY_ITEM_CHEST)
- turtle.placeUp()
- for i = INVENTORY_STORAGE , 16 do
- turtle.select(i)
- turtle.dropUp()
- end
- turtle.select(INVENTORY_ITEM_CHEST)
- turtle.digUp()
- end
- -------------------------------------------------------------------------------
- checkInventory = function()
- for i = INVENTORY_STORAGE , 16 do
- turtle.select(i)
- item = turtle.getItemCount(i)
- if item == 0 then
- return false
- end
- end
- unloadInventory()
- return true
- end
- -------------------------------------------------------------------------------
- quickCheckInventory = function()
- local item = turtle.getItemCount(16)
- if item == 0 then
- return false
- end
- unloadInventory()
- return true
- end
- -------------------------------------------------------------------------------
- saveState = function()
- f = fs.open(file, "w")
- f.writeLine(state)
- f.writeLine(facing)
- f.writeLine(tostring(x))
- f.writeLine(tostring(z))
- f.writeLine(tostring(y))
- f.writeLine(tostring(movex))
- f.writeLine(tostring(movey))
- f.close()
- end
- -------------------------------------------------------------------------------
- loadState = function()
- if not fs.exists(file) then
- return false
- end
- f = fs.open( file , "r" )
- state = f.readLine()
- facing = f.readLine()
- x = tonumber(f.readLine())
- z = tonumber(f.readLine())
- y = tonumber(f.readLine())
- movex = tonumber(f.readLine())
- movey = tonumber(f.readLine())
- f.close()
- return true
- end
- -------------------------------------------------------------------------------
- forward = function()
- turtle.select(INVENTORY_STORAGE)
- while not turtle.forward() do
- if peripheral.isPresent("front") then
- left()
- forward()
- right()
- sleep(math.random(1, 7))
- forward()
- right()
- forward()
- left()
- elseif not turtle.dig() then
- message("Unbreakable block reached - forward()")
- return false
- elseif turtle.attack() then
- message("Mob on my way. Die! - forward()")
- elseif turtle.getFuelLevel() == 0 then
- message("Low fuel - forward()")
- refuel()
- message("Refueled. Ready to go!")
- end
- quickCheckInventory()
- end
- updateLocation()
- saveState()
- return true
- end
- -------------------------------------------------------------------------------
- nforward = function(num)
- if num < 0 then
- message("Invalid number - nforward(num) =" .. num)
- return false
- elseif num == 0 then
- return true
- end
- -- If moving a lot forward, lets travel one level above.
- if num > 6 then
- forward()
- up()
- -- Move
- for i = 2 , num - 1 do
- forward()
- end
- down()
- forward()
- else
- for i = 1 , num do
- forward()
- end
- end
- return true
- end
- -------------------------------------------------------------------------------
- down = function()
- turtle.select(INVENTORY_STORAGE)
- while not turtle.down() do
- if peripheral.isPresent("bottom") then
- left()
- left()
- forward()
- left()
- left()
- sleep(math.random(1, 7))
- forward()
- elseif not turtle.digDown() then
- message("Bedrock reached - down()")
- return false
- elseif turtle.attackDown() then
- message("Mob on the way. Die! - down()")
- elseif turtle.getFuelLevel() == 0 then
- message("Low fuel - down() ")
- refuel()
- message("Refueled. Ready to go!")
- end
- quickCheckInventory()
- end
- updateHeight(DIRECTION_DOWN)
- return true
- end
- -------------------------------------------------------------------------------
- up = function()
- turtle.select(INVENTORY_STORAGE)
- while not turtle.up() do
- if peripheral.isPresent("top") then
- left()
- left()
- forward()
- left()
- left()
- sleep(math.random(1, 7))
- forward()
- elseif not turtle.digUp() then
- message("Bedrock reached - up()")
- return false
- elseif turtle.attackUp() then
- message("Mob on the way. Die! - up()")
- elseif turtle.getFuelLevel() == 0 then
- message("Low fuel - up() ")
- refuel()
- message("Refueled. Ready to go!")
- end
- quickCheckInventory()
- end
- updateHeight(DIRECTION_UP)
- return true
- end
- -------------------------------------------------------------------------------
- left = function()
- --print("turnLeft() facing=", facing," direction")
- turtle.turnLeft()
- if facing == DIRECTION_NORTH then
- facing = DIRECTION_WEST
- elseif facing == DIRECTION_SOUTH then
- facing = DIRECTION_EAST
- elseif facing == DIRECTION_EAST then
- facing = DIRECTION_NORTH
- elseif facing == DIRECTION_WEST then
- facing = DIRECTION_SOUTH
- else
- message("Invalid facing direction - left() ", facing)
- end
- saveState();
- end
- -------------------------------------------------------------------------------
- right = function()
- --print("turnRight() facing=", facing," direction")
- turtle.turnRight()
- if facing == DIRECTION_NORTH then
- facing = DIRECTION_EAST
- elseif facing == DIRECTION_SOUTH then
- facing = DIRECTION_WEST
- elseif facing == DIRECTION_EAST then
- facing = DIRECTION_SOUTH
- elseif facing == DIRECTION_WEST then
- facing = DIRECTION_NORTH
- else
- message("Invalid facing direction - right() " .. facing)
- end
- saveState();
- end
- -------------------------------------------------------------------------------
- refuel = function()
- turtle.select(INVENTORY_FUEL_CHEST)
- while not turtle.place() do
- if y<0 then
- turtle.dig()
- else
- right()
- end
- end
- turtle.suck()
- turtle.refuel(64)
- message("Fuel level: "..turtle.getFuelLevel() )
- turtle.select(INVENTORY_FUEL_CHEST)
- turtle.dig()
- end
- -------------------------------------------------------------------------------
- updateLocation = function()
- if facing == DIRECTION_NORTH then
- y = y - 1
- elseif facing == DIRECTION_SOUTH then
- y = y + 1
- elseif facing == DIRECTION_EAST then
- x = x + 1
- elseif facing == DIRECTION_WEST then
- x = x - 1
- else
- message("Invalid facing position - updateLocation()")
- end
- end
- -------------------------------------------------------------------------------
- updateHeight = function(str)
- if str == DIRECTION_DOWN then
- z = z - 1
- elseif str == DIRECTION_UP then
- z = z + 1
- else
- message("Invalid moving down/up position - updateHeight()")
- end
- end
- -------------------------------------------------------------------------------
- turnTo = function(dir)
- if dir == DIRECTION_NORTH then
- if facing == DIRECTION_NORTH then
- return true
- elseif facing == DIRECTION_SOUTH then
- right()
- right()
- elseif facing == DIRECTION_EAST then
- left()
- elseif facing == DIRECTION_WEST then
- right()
- else
- message("Invalid facing direction - turnTo(dir) " .. facing)
- end
- elseif dir == DIRECTION_SOUTH then
- if facing == DIRECTION_SOUTH then
- return true
- elseif facing == DIRECTION_NORTH then
- right()
- right()
- elseif facing == DIRECTION_WEST then
- left()
- elseif facing == DIRECTION_EAST then
- right()
- else
- message("Invalid facing direction - turnTo(dir) " .. facing)
- end
- elseif dir == DIRECTION_WEST then
- if facing == DIRECTION_WEST then
- return true
- elseif facing == DIRECTION_EAST then
- right()
- right()
- elseif facing == DIRECTION_NORTH then
- left()
- elseif facing == DIRECTION_SOUTH then
- right()
- else
- message("Invalid facing direction - turnTo(dir) " .. facing)
- end
- elseif dir == DIRECTION_EAST then
- if facing == DIRECTION_EAST then
- return true
- elseif facing == DIRECTION_WEST then
- right()
- right()
- elseif facing == DIRECTION_SOUTH then
- left()
- elseif facing == DIRECTION_NORTH then
- right()
- else
- message("Invalid facing direction - turnTo(dir) " .. facing)
- end
- else
- message("Invalid goto direction - turnTo(dir) " .. dir)
- end
- end
- -------------------------------------------------------------------------------
- setting = function()
- message("call setting()")
- local modem = peripheral.find("modem")
- myChannel = os.getComputerID()
- modem.open(myChannel)
- sleep(0.5)
- modem.transmit(initialChannel, myChannel, "ready")
- local event, side, freq , reply , msg , dist = os.pullEvent("modem_message")
- message(msg .. " - " .. myChannel)
- while tostring(msg) ~= tostring(myChannel) do
- message("Waiting for 'set' message on channel " .. myChannel)
- event, side, freq , reply , msg , dist = os.pullEvent("modem_message")
- end
- message("Channel/ID set to " .. myChannel)
- if turtle.getFuelLevel() == 0 then
- refuel()
- end
- modem.close(myChannel)
- end
- -------------------------------------------------------------------------------
- getNextSpot = function()
- message("call getNextSpot()")
- movex = 0
- movey = 0
- local modem = peripheral.find("modem")
- modem.open(myChannel)
- sleep(0.5)
- local count = 0
- while true do
- message("Sending Request CH:" .. myChannel)
- modem.transmit(myChannel, myChannel, "request")
- os.startTimer(20)
- event = { os.pullEvent() }
- if event[1] == "modem_message" then
- movex = event[4]
- movey = tonumber(event[5])
- message("Request attended " .. movex .. "-" .. movey)
- break
- elseif event[1] == "timer" then
- count = count + 1
- message("Timeout getNextSpot() " .. count)
- end
- end
- modem.close(myChannel)
- end
- -------------------------------------------------------------------------------
- move = function()
- if movex == nil or movey == nil then
- message("Moving position equals to nil - move()")
- return false
- end
- aux_x = movex - x
- aux_y = movey - y
- message("-- move() where to x=" .. movex .. " y=" .. movey)
- message("-- move() origin x=" .. x .. " y=" .. y)
- message("-- move() how much x=" .. aux_x .. " y=" .. aux_y)
- if aux_x > 0 then
- turnTo(DIRECTION_EAST)
- elseif aux_x < 0 then
- turnTo(DIRECTION_WEST)
- end
- nforward(math.abs(aux_x))
- if aux_y > 0 then
- turnTo(DIRECTION_SOUTH)
- elseif aux_y < 0 then
- turnTo(DIRECTION_NORTH)
- end
- nforward(math.abs(aux_y))
- return true
- end
- -------------------------------------------------------------------------------
- excavate = function()
- local ignore
- state = "mining"
- while down() do
- for i = 1 , 4 do
- ignore = checkIgnore()
- if not ignore then
- turtle.dig()
- checkInventory()
- end
- if i ~= 4 then
- right()
- end
- end
- end
- while z ~= 0 do
- up()
- end
- end
- -------------------------------------------------------------------------------
- checkIgnore = function()
- local index, ignored_block
- local success, data = turtle.inspect()
- if not success then
- -- message("turtle.inspect() failed. Probably air.")
- return true
- end
- if data.name == 'minecraft:lava' then
- turtle.select(INVENTORY_BUCKET)
- turtle.place()
- turtle.refuel()
- message("Lava found and used for fuel.")
- turtle.select(INVENTORY_STORAGE)
- return true
- end
- for index, ignored_block in pairs(INVENTORY_IGNORE_MAP) do
- if data.name == ignored_block then
- return true
- end
- end
- -- Do not ignore if past all checks.
- return false
- end
- -------------------------------------------------------------------------------
- function rawread()
- print("Press enter to continue")
- while true do
- local sEvent, param = os.pullEvent("key")
- if sEvent == "key" then
- if param == 28 then
- print("Enter detected")
- break
- end
- end
- end
- end
- -------------------------------------------------------------------------------
- -- Main -----------------------------------------------------------------------
- local function main()
- myChannel = os.getComputerID()
- if loadState() then
- message("State loaded. Resuming task.")
- sleep(10)
- else
- message("Slave started. Ready to mine!")
- state = "setting"
- end
- while true do
- if state == "setting" then
- message("--> Setting")
- setting()
- state = "waiting"
- elseif state == "waiting" then
- message("--> Waiting")
- getNextSpot()
- state = "moving"
- elseif state == "moving" then
- message("--> Moving")
- move()
- state = "mining"
- elseif state == "mining" then
- -- rawread()
- message("--> Mining")
- excavate()
- state = "waiting"
- else
- message("Invalid state - main() " .. state)
- end
- end
- end
- main()
Add Comment
Please, Sign In to add comment