Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------
- -- Gemakson Alex 08.2019 --
- ----------------------------------------------
- local r = require("robot")
- local component = require("component")
- local computer = require("computer")
- local gen = component.generator
- local ic = component.inventory_controller
- local lvl = 0
- local tick = 0
- local fPosition = {y = 0, rotation = 1}
- local currentPosition = {x = 0, y = 0, z = 0, rotation = 1}
- local targetPosition = {x = 0, y = 0, z = 0, rotation = 1}
- local a = 0
- local b = 0
- local c = 0
- local spiralPhase = 0
- ----------------------------------------------
- local genInsertFuel_ = true
- local cleanInv_ = true
- local checkLvl_ = true
- local equipTool_ = true
- local checkEnergyLevel_ = true
- ----------------------------------------------
- local cleanInvSleep = 10
- local insertFuelSleep = 4
- local checkLvlSleep = 10
- ----------------------------------------------
- local excavationPhase = 0
- ----------------------------------------------
- local lastPosition = {x = 0, y = 0, z = 0, rotation = 1, tx = 0, ty = 0, tz = 0, trotation = 1}
- ----------------------------------------------
- function Notifications(code)
- if code == 1 then print("Внимание! Не осталось угля!") end
- if code == 2 then print("Уровень повышен до " .. math.floor(r.level()) .. "!") end
- if code == 3 then print("Ошибка! Перемещение невозможно!") end
- if code == 4 then print("Осталось " .. math.floor(computer.energy() * 100 / computer.maxEnergy()) .. " % заряда!") end
- if code == 5 then print("Возвращение на стартовую точку!") end
- if code == 6 then print("Текущие координаты \nX: " .. currentPosition.x .. " Z: "
- .. currentPosition.z .. " Y: " .. currentPosition.y .. "\nrotation: "
- .. currentPosition.rotation) end
- end
- function CheckLevel()
- if checkLvl == true then
- if math.floor(r.level()) > lvl then
- lvl = math.floor(r.level())
- Notifications(2)
- end
- end
- end
- function Move(dirrection)
- if dirrection == 1 then
- for i = 1, 20 do
- if r.forward() == true then
- if currentPosition.rotation == 1 then
- currentPosition.z = currentPosition.z - 1 end
- if currentPosition.rotation == 2 then
- currentPosition.x = currentPosition.x + 1 end
- if currentPosition.rotation == 3 then
- currentPosition.z = currentPosition.z + 1 end
- if currentPosition.rotation == 4 then
- currentPosition.x = currentPosition.x - 1 end
- break
- end
- if i == 20 then Notifications(3) end
- end
- end
- if dirrection == 2 then
- for i = 1, 20 do
- if r.up() == true then
- currentPosition.y = currentPosition.y + 1
- break
- end
- if i == 20 then Notifications(3) end
- end
- end
- if dirrection == 3 then
- for i = 1, 20 do
- if r.down() == true then
- currentPosition.y = currentPosition.y - 1
- break
- end
- if i == 20 then Notifications(3) end
- end
- end
- end
- function Turn(mode)
- if mode == 1 then
- r.turnLeft()
- if currentPosition.rotation > 1 then
- currentPosition.rotation = currentPosition.rotation - 1
- else
- currentPosition.rotation = 4
- end
- else
- r.turnRight()
- if currentPosition.rotation < 4 then
- currentPosition.rotation = currentPosition.rotation + 1
- else
- currentPosition.rotation = 1
- end
- end
- end
- function Swing(dirrection)
- if r.durability() ~= false then
- if dirrection == 1 then
- if r.detect() == true then
- r.swing()
- end
- end
- if dirrection == 2 then
- if r.detectUp() == true then
- r.swingUp()
- end
- end
- if dirrection == 3 then
- if r.detectDown() == true then
- r.swingDown()
- end
- end
- end
- end
- function GenInsertFuel()
- if genInsertFuel_ == true then
- if gen.count() < 32 then
- for i = 1, r.inventorySize() do
- if r.count(i) ~= 0 then
- if ic.getStackInInternalSlot(i).name == "minecraft:coal" and gen.count() < 32 then
- r.select(i)
- if r.count(i) <= 32 - gen.count() then
- gen.insert(r.count(i))
- else
- gen.insert(32 - gen.count())
- end
- end
- end
- end
- end
- else
- if gen.count() == 0 then
- Notifications(1)
- end
- end
- end
- function CleanInv()
- if cleanInv_ == true then
- for i = 1, r.inventorySize() do
- if r.count(i) ~= 0 then
- if ic.getStackInInternalSlot(i).name == "minecraft:cobblestone" or
- ic.getStackInInternalSlot(i).name == "minecraft:gravel" or
- ic.getStackInInternalSlot(i).name == "minecraft:dirt" then
- r.select(i)
- r.dropDown()
- end
- end
- end
- end
- end
- function EquipTool()
- if equipTool_ == true then
- if r.durability() == nil then
- for i = 1, r.inventorySize() do
- if r.count(i) ~= 0 then
- if ic.getStackInInternalSlot(i).name == "minecraft:iron_pickaxe" or
- ic.getStackInInternalSlot(i).name == "minecraft:diamond_pickaxe" then
- r.select(i)
- ic.equip()
- break
- end
- else
- if i == r.inventorySize() then
- print("нет кирок")
- return 1
- end
- end
- end
- end
- end
- return 0
- end
- function CheckEnergyLevel()
- if checkEnergyLevel_ == true then
- if math.floor(computer.energy() * 100 / computer.maxEnergy()) <= 10 then
- Notifications(4)
- return 1
- end
- end
- return 0
- end
- function CheckRobotStatus(code)
- print("CheckRobotStatus(code)")
- if code == 2 then
- tick = tick + 1
- if tick == 101 then tick = 1 end
- if tick % insertFuelSleep == 0 then if GenInsertFuel() == 1 then Home() end end
- if tick % cleanInvSleep == 0 then CleanInv() end
- if tick % checkLvlSleep == 0 then CheckLevel() end
- if EquipTool() == 1 then Home() print("нет кирок, Home()") end
- if CheckEnergyLevel() == 1 then Home() end
- else
- if GenInsertFuel() == 1 then Home() end
- CleanInv()
- CheckLevel()
- if EquipTool() == 1 then Home() end
- if CheckEnergyLevel() == 1 then Home() end
- end
- end
- function EnterValues(code)
- if code == 1 then
- print("Введите текущее значение высоты робота:")
- fPosition.y = tonumber(io.read())
- print(fPosition.y)
- end
- if code == 2 then
- print("Введите [y] если хотите продолжить:")
- if io.read() ~= y then
- return 1
- end
- end
- return 0
- end
- --устанавливает цели (координаты) для робота
- function TargetCreator()
- if excavationPhase == 1 then
- targetPosition.y = 3
- if currentPosition.y == targetPosition.y then excavationPhase = 2 end
- end
- if excavationPhase == 2 then
- if currentPosition.x == targetPosition.x and currentPosition.z == targetPosition.z and currentPosition.rotation == targetPosition.rotation then
- spiralPhase = spiralPhase + 1
- if currentPosition.rotation == 1 then targetPosition.z = targetPosition.z - math.floor((spiralPhase + 1) / 2) end
- if currentPosition.rotation == 2 then targetPosition.x = targetPosition.x + math.floor((spiralPhase + 1) / 2) end
- if currentPosition.rotation == 3 then targetPosition.z = targetPosition.z + math.floor((spiralPhase + 1) / 2) end
- if currentPosition.rotation == 4 then targetPosition.x = targetPosition.x - math.floor((spiralPhase + 1) / 2) end
- targetPosition.rotation = currentPosition.rotation + 1
- if targetPosition.rotation == 5 then targetPosition.rotation = 1 end
- end
- end
- if excavationPhase == 3 then
- print("excavationPhase == 3")
- if currentPosition.x == 0 and currentPosition.z == 0 then
- targetPosition.y = fPosition.y
- if currentPosition.y == fPosition.y then
- print(currentPosition.y)
- print(fPosition.y)
- print("высота набрана")
- if EnterValues(2) == 1 then
- return 1
- end
- CheckRobotStatus(1)
- Beep(2)
- excavationPhase = 4
- end
- else
- if not(targetPosition.z == 0 and targetPosition.x == 0) then
- targetPosition.z = 0
- targetPosition.x = 0
- end
- end
- end
- if excavationPhase == 4 then
- print("excavationPhase == 4")
- if not(targetPosition.x == lastPosition.x and targetPosition.z == lastPosition.z
- and targetPosition.y == lastPosition.y and targetPosition.rotation == lastPosition.rotation) then
- targetPosition.x = lastPosition.x
- targetPosition.z = lastPosition.z
- targetPosition.y = lastPosition.y
- targetPosition.rotation = lastPosition.rotation
- else
- excavationPhase = 2
- targetPosition.x = lastPosition.tx
- targetPosition.z = lastPosition.tz
- targetPosition.y = lastPosition.ty
- targetPosition.rotation = lastPosition.trotation
- end
- end
- return 0
- end
- function Beep(code)
- if code == 1 then
- computer.beep(800,0.1)
- computer.beep(1000,0.1)
- computer.beep(1200,0.2)
- end
- if code == 2 then
- computer.beep(1000,0.2)
- computer.beep(1000,0.2)
- end
- end
- function Home()
- print("начало функции Home()")
- excavationPhase = 3
- lastPosition.x = currentPosition.x
- lastPosition.z = currentPosition.z
- lastPosition.y = currentPosition.y
- lastPosition.rotation = currentPosition.rotation
- lastPosition.tx = targetPosition.x
- lastPosition.tz = targetPosition.z
- lastPosition.ty = targetPosition.y
- lastPosition.trotation = targetPosition.rotation
- end
- ----------------------------------------------
- Beep(1)
- EnterValues(1)
- CheckRobotStatus(1)
- currentPosition.y = fPosition.y
- currentPosition.rotation = fPosition.rotation
- excavationPhase = 1
- Beep(2)
- --основной цикл программы
- while true do
- CheckRobotStatus(2)
- if TargetCreator() == 1 then break end
- --print(targetPosition.x .. " " .. targetPosition.z .. " " .. currentPosition.x .. " " .. currentPosition.z)
- if targetPosition.y ~= currentPosition.y then
- if targetPosition.y > currentPosition.y then
- Swing(2)
- Move(2)
- else
- Swing(3)
- Move(3)
- end
- else
- if targetPosition.z < currentPosition.z then c = 1 end
- if targetPosition.x > currentPosition.x then c = 2 end
- if targetPosition.z > currentPosition.z then c = 3 end
- if targetPosition.x < currentPosition.x then c = 4 end
- if c ~= currentPosition.rotation and (currentPosition.x ~= targetPosition.x or currentPosition.z ~= targetPosition.z) then
- a = currentPosition.rotation
- b = 0
- while true do
- a = a + 1
- b = b + 1
- if a == 5 then a = 1 end
- if a == c then break end
- end
- if b > 2 then
- Turn(1)
- else
- Turn(2)
- end
- else
- if currentPosition.x ~= targetPosition.x or currentPosition.z ~= targetPosition.z then
- Swing(1)
- if excavationPhase == 2 then
- Swing(2)
- Swing(3)
- end
- Move(1)
- else
- if targetPosition.rotation ~= currentPosition.rotation then
- a = currentPosition.rotation
- b = 0
- while true do
- a = a + 1
- b = b + 1
- if a == 5 then a = 1 end
- if a == targetPosition.rotation then break end
- end
- if b > 2 then
- Turn(1)
- else
- Turn(2)
- end
- print("fPosition.y " .. fPosition.y)
- end
- end
- end
- end
- print("------------------------------------------------")
- end
- --pastebin run UzNG12tA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement