Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require("robot")
- local computer = require("computer")
- local component = require("component")
- local sides = require("sides")
- local ic = component.inventory_controller
- inventorySize = robot.inventorySize()
- toolSlots = {
- shovel = {1, 2, 3, 4},
- pickaxe = {5, 6, 7, 8}
- }
- currentTool = "none"
- currentToolSlot = -1
- dropFirstSlot = 9
- isInverted = false
- saveCoords = {x = 0, y = 0, z = 0, side = 1}
- baseCoords = {x = 0, y = 0, z = 0, side = 1}
- chardgeCoords = {x = 0, y = 0, z = 0, side = 1}
- dropCoords = {x = 0, y = 0, z = 0, side = 3}
- loadToolShovelCoords = {x = 1, y = 0, z = 0, side = 3}
- loadToolPickaxeCoords = {x = 2, y = 0, z = 0, side = 3}
- curCoords = {x = 0, y = 0, z = 0, side = 1}
- dx = {0, 1, 0, -1}
- dz = {1, 0, -1, 0}
- -- 1
- -- Z
- --
- -- 4 0 X2
- --
- --
- -- 3
- function getEnergy()
- return computer.energy() / computer.maxEnergy()
- end
- function getItemCount(slotList)
- itemCount = 0
- for i = 1,#slotList do
- itemCount = itemCount + robot.count(slotList[i])
- end
- return itemCount
- end
- function getFirstNotEmptySlot(slotList)
- for i = 1,#slotList do
- if robot.count(slotList[i]) > 0 then
- return slotList[i]
- end
- end
- return nil
- end
- function toolEquip(slot)
- t = robot.select()
- robot.select(slot)
- ic.equip()
- robot.select(t)
- end
- function equipTool(tool)
- if currentTool == "breaking" then
- toolEquip(dropFirstSlot)
- currentTool = "none"
- end
- if currentTool == "none" then
- currentToolSlot = getFirstNotEmptySlot(toolSlots[tool])
- toolEquip(currentToolSlot)
- currentTool = tool
- else
- toolEquip(currentToolSlot)
- currentToolSlot = getFirstNotEmptySlot(toolSlots[tool])
- toolEquip(currentToolSlot)
- currentTool = tool
- end
- end
- function changeTool(side)
- data = geolyzer.analyze(side)
- tool = data.harvestTool
- v, s = robot.durability()
- if v == nill or s == "no tool equipped" then
- currentTool = "none"
- end
- if v == 0 then
- currentTool = "breaking"
- end
- if tool == "axe" or tool == nil then
- equipTool("shovel")
- else
- if tool == "shovel" or tool == "pickaxe" then
- if currentTool ~= tool then
- equipTool(tool)
- end
- else
- equipTool("pickaxe")
- end
- end
- end
- function returnToBaseWithError(messageID)
- if messageID == 1 then
- message = "ERROR: Can't break block: x = " .. curCoords.x .. ", y = " .. curCoords.y .. ", z = " .. curCoords.z
- print(message)
- file = io.open("error", "w")
- io.output(file)
- io.write(message)
- io.close(file)
- end
- goTo(baseCoords)
- turnTo(1)
- while true do
- computer.shutdown(false)
- end
- end
- function dig()
- conter = 0
- while robot.detect() do
- changeTool(sides.front)
- robot.swing()
- conter = conter + 1
- if conter > 10 then
- returnToBaseWithError(1)
- end
- os.sleep(0)
- end
- end
- function digUp()
- conter = 0
- while robot.detectUp() do
- changeTool(sides.top)
- robot.swingUp()
- conter = conter + 1
- if conter > 10 then
- returnToBaseWithError(1)
- end
- os.sleep(0)
- end
- end
- function digDown()
- conter = 0
- while robot.detectDown() do
- changeTool(sides.bottom)
- robot.swingDown()
- conter = conter + 1
- if conter > 10 then
- returnToBaseWithError(1)
- end
- os.sleep(0)
- end
- end
- function forward(isDigUp, isDigDown)
- if robot.detect() then
- dig()
- end
- while not robot.forward() do
- dig()
- end
- if isDigUp then
- digUp()
- end
- if isDigDown then
- digDown()
- end
- curCoords.x = curCoords.x + dx[curCoords.side]
- curCoords.z = curCoords.z + dz[curCoords.side]
- os.sleep(0)
- end
- function up(isDigUp)
- if robot.detectUp() then
- digUp()
- end
- while not robot.up() do
- digUp()
- end
- if isDigUp then
- digUp()
- end
- curCoords.y = curCoords.y + 1
- os.sleep(0)
- end
- function down(isDigDown)
- if robot.detectDown() then
- digDown()
- end
- while not robot.down() do
- digDown()
- end
- if isDigDown then
- digDown()
- end
- curCoords.y = curCoords.y - 1
- os.sleep(0)
- end
- function turnLeft()
- robot.turnLeft()
- curCoords.side = curCoords.side - 1;
- if curCoords.side == 0 then
- curCoords.side = 4
- end
- os.sleep(0)
- end
- function turnRight()
- robot.turnRight()
- curCoords.side = curCoords.side + 1;
- if curCoords.side == 5 then
- curCoords.side = 1
- end
- os.sleep(0)
- end
- function turnTo(side)
- if curCoords.side == side then
- return
- end
- if (curCoords.side - side) % 2 == 0 then
- turnLeft()
- turnLeft()
- return
- end
- if (curCoords.side + 1) % 4 == side % 4 then
- turnRight()
- return
- end
- if (curCoords.side - 1) % 4 == side % 4 then
- turnLeft()
- return
- end
- end
- function goTo(coord, digUp, digDown)
- while curCoords.y ~= coord.y do
- if curCoords.y > coord.y then
- down(digDown)
- else
- up(digUp)
- end
- end
- if curCoords.x ~= coord.x then
- if curCoords.x > coord.x then
- turnTo(4)
- else
- turnTo(2)
- end
- while curCoords.x ~= coord.x do
- forward(digUp, digDown)
- end
- end
- if curCoords.z ~= coord.z then
- if curCoords.z > coord.z then
- turnTo(3)
- else
- turnTo(1)
- end
- while curCoords.z ~= coord.z do
- forward(digUp, digDown)
- end
- end
- end
- function checkEnergy(value)
- return getEnergy() >= value
- end
- function checkTool()
- return getItemCount(toolSlots["shovel"]) > 0 and getItemCount(toolSlots["pickaxe"]) > 0
- end
- function checkDrop()
- firstEmptySlot = inventorySize
- for i = dropFirstSlot,inventorySize do
- if robot.count(i) == 0 then
- firstEmptySlot = i
- break
- end
- end
- return firstEmptySlot + 2 <= inventorySize
- end
- function charging()
- goTo(chardgeCoords)
- while not checkEnergy(0.95) do
- os.sleep(1)
- end
- end
- function drop()
- goTo(dropCoords)
- turnTo(dropCoords.side)
- for i = dropFirstSlot,inventorySize do
- robot.select(i)
- robot.drop()
- end
- robot.select(dropFirstSlot)
- end
- function loadTool()
- if currentTool ~= "none" then
- if currentTool == "shovel" then
- toolEquip(toolSlots["shovel"][1])
- currentTool = "none"
- end
- if currentTool == "pickaxe" then
- toolEquip(toolSlots["pickaxe"][1])
- currentTool = "none"
- end
- end
- goTo(loadToolShovelCoords)
- turnTo(loadToolShovelCoords.side)
- for i = 1,#toolSlots["shovel"] do
- if robot.count(toolSlots["shovel"][i]) == 0 then
- robot.select(toolSlots["shovel"][i])
- robot.suck()
- end
- end
- goTo(loadToolPickaxeCoords)
- turnTo(loadToolPickaxeCoords.side)
- for i = 1,#toolSlots["pickaxe"] do
- if robot.count(toolSlots["pickaxe"][i]) == 0 then
- robot.select(toolSlots["pickaxe"][i])
- robot.suck()
- end
- end
- robot.select(dropFirstSlot)
- end
- function returnToBase()
- saveCoords.x = curCoords.x
- saveCoords.y = curCoords.y
- saveCoords.z = curCoords.z
- goTo(baseCoords)
- if not checkEnergy(0.95) then
- charging()
- end
- if not checkDrop() then
- drop()
- end
- if not checkTool() then
- loadTool()
- end
- goTo(baseCoords)
- turnTo(1)
- forward(true)
- goTo(saveCoords)
- turnTo(saveCoords.side)
- end
- function check()
- if not checkEnergy(0.2) or not checkTool() or not checkDrop() then
- returnToBase()
- end
- end
- function main(x, z, y, rx, rz, ry)
- if component.isAvailable("geolyzer") then
- geolyzer = component.geolyzer
- else
- geolyzer = nil
- print("Need geolyzer component")
- return
- end
- if rx ~= nil and ry ~= nil and rz ~= nil then
- isRev = true
- rx = tonumber(rx)
- ry = tonumber(ry)
- rz = tonumber(rz)
- end
- returnToBase()
- forward(true)
- coordTo = {x = 0, y = 0, z = 0, side = 1}
- robot.select(dropFirstSlot)
- for k = 0, y / 3 do
- for j = 0, x - 1 do
- for i = 1, z do
- if k % 2 == 0 then
- coordTo.x = j
- if j % 2 == 0 then
- coordTo.z = i
- else
- coordTo.z = z - i + 1
- end
- else
- coordTo.x = x - j - 1
- if (x % 2 == 0) then
- if j % 2 == 0 then
- coordTo.z = i
- else
- coordTo.z = z - i + 1
- end
- else
- if j % 2 == 0 then
- coordTo.z = z - i + 1
- else
- coordTo.z = i
- end
- end
- end
- coordTo.y = -k * 3 - 1
- if coordTo.y < -y then
- coordTo.y = -y
- end
- if isRev then
- if coordTo.x == rx and coordTo.y == ry and coordTo.z == rz then
- goTo(coordTo, coordTo.y < 0 and coordTo.z > 0, coordTo.y > -y and coordTo.z > 0)
- isRev = false
- end
- else
- goTo(coordTo, coordTo.y < 0 and coordTo.z > 0, coordTo.y > -y and coordTo.z > 0)
- check()
- end
- end
- end
- end
- goTo(baseCoords)
- drop()
- goTo(baseCoords)
- turnTo(1)
- if currentToolSlot > 0 then
- toolEquip(currentToolSlot)
- end
- end
- main(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement