Advertisement
ZiegelMan

digger

Aug 16th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.91 KB | None | 0 0
  1. ----------------------------------------------
  2. --          Gemakson Alex 08.2019           --
  3. ----------------------------------------------
  4. local r = require("robot")
  5. local component = require("component")
  6. local computer = require("computer")
  7. local gen = component.generator
  8. local ic = component.inventory_controller
  9. local lvl = 0
  10. local tick = 0
  11. local fPosition = {y = 0, rotation = 1}
  12. local currentPosition = {x = 0, y = 0, z = 0, rotation = 1}
  13. local targetPosition = {x = 0, y = 0, z = 0, rotation = 1}
  14. local a = 0
  15. local b = 0
  16. local c = 0
  17. local spiralPhase = 0
  18. ----------------------------------------------
  19. local genInsertFuel_ = true
  20. local cleanInv_ = true
  21. local checkLvl_ = true
  22. local equipTool_ = true
  23. local checkEnergyLevel_ = true
  24. ----------------------------------------------
  25. local cleanInvSleep = 10
  26. local insertFuelSleep = 4
  27. local checkLvlSleep = 10
  28. ----------------------------------------------
  29. local excavationPhase = 0
  30. ----------------------------------------------
  31. local lastPosition = {x = 0, y = 0, z = 0, rotation = 1, tx = 0, ty = 0, tz = 0, trotation = 1}
  32. ----------------------------------------------
  33. function Notifications(code)
  34.     if code == 1 then print("Внимание! Не осталось угля!") end
  35.     if code == 2 then print("Уровень повышен до " .. math.floor(r.level()) .. "!") end
  36.     if code == 3 then print("Ошибка! Перемещение невозможно!") end
  37.     if code == 4 then print("Осталось " .. math.floor(computer.energy() * 100 / computer.maxEnergy()) .. " % заряда!") end
  38.     if code == 5 then print("Возвращение на стартовую точку!") end
  39.     if code == 6 then print("Текущие координаты \nX: " .. currentPosition.x .. " Z: "
  40.         .. currentPosition.z .. " Y: " .. currentPosition.y .. "\nrotation: "
  41.         .. currentPosition.rotation) end
  42. end
  43. function CheckLevel()
  44.     if checkLvl == true then
  45.         if math.floor(r.level()) > lvl then
  46.             lvl = math.floor(r.level())
  47.             Notifications(2)
  48.         end
  49.     end
  50. end
  51. function Move(dirrection)
  52.     if dirrection == 1 then
  53.         for i = 1, 20 do
  54.             if r.forward() == true then
  55.                 if currentPosition.rotation == 1 then
  56.                     currentPosition.z = currentPosition.z - 1 end
  57.                 if currentPosition.rotation == 2 then  
  58.                     currentPosition.x = currentPosition.x + 1 end  
  59.                 if currentPosition.rotation == 3 then  
  60.                     currentPosition.z = currentPosition.z + 1 end
  61.                 if currentPosition.rotation == 4 then
  62.                     currentPosition.x = currentPosition.x - 1 end
  63.                 break
  64.             end
  65.             if i == 20 then Notifications(3) end
  66.         end
  67.     end
  68.     if dirrection == 2 then
  69.         for i = 1, 20 do
  70.             if r.up() == true then  
  71.                 currentPosition.y = currentPosition.y + 1
  72.                 break
  73.             end
  74.             if i == 20 then Notifications(3) end
  75.         end
  76.     end
  77.     if dirrection == 3 then
  78.         for i = 1, 20 do
  79.             if r.down() == true then
  80.                 currentPosition.y = currentPosition.y - 1
  81.                 break
  82.             end
  83.             if i == 20 then Notifications(3) end
  84.         end
  85.     end
  86. end
  87. function Turn(mode)
  88.     if mode == 1 then
  89.         r.turnLeft()
  90.         if currentPosition.rotation > 1 then
  91.             currentPosition.rotation = currentPosition.rotation - 1
  92.         else
  93.             currentPosition.rotation = 4
  94.         end
  95.     else
  96.         r.turnRight()
  97.         if currentPosition.rotation < 4 then
  98.             currentPosition.rotation = currentPosition.rotation + 1
  99.         else
  100.             currentPosition.rotation = 1
  101.         end
  102.     end
  103. end
  104. function Swing(dirrection)
  105.     if r.durability() ~= false then
  106.         if dirrection == 1 then
  107.             if r.detect() == true then
  108.                 r.swing()
  109.             end
  110.         end
  111.         if dirrection == 2 then
  112.             if r.detectUp() == true then
  113.                 r.swingUp()
  114.             end
  115.         end
  116.         if dirrection == 3 then
  117.             if r.detectDown() == true then 
  118.                 r.swingDown()
  119.             end
  120.         end
  121.     end
  122. end
  123. function GenInsertFuel()
  124.     if genInsertFuel_ == true then
  125.         if gen.count() < 32 then
  126.             for i = 1, r.inventorySize() do
  127.                 if r.count(i) ~= 0 then
  128.                     if ic.getStackInInternalSlot(i).name == "minecraft:coal" and gen.count() < 32 then
  129.                         r.select(i)
  130.                         if r.count(i) <= 32 - gen.count() then
  131.                             gen.insert(r.count(i))
  132.                         else
  133.                             gen.insert(32 - gen.count())
  134.                         end
  135.                     end
  136.                 end
  137.             end
  138.         end
  139.     else
  140.         if gen.count() == 0 then
  141.             Notifications(1)
  142.         end
  143.     end
  144. end
  145. function CleanInv()
  146.     if cleanInv_ == true then
  147.         for i = 1, r.inventorySize() do
  148.             if r.count(i) ~= 0 then
  149.                 if ic.getStackInInternalSlot(i).name == "minecraft:cobblestone" or
  150.                 ic.getStackInInternalSlot(i).name == "minecraft:gravel" or
  151.                 ic.getStackInInternalSlot(i).name == "minecraft:dirt" then
  152.                     r.select(i)
  153.                     r.dropDown()
  154.                 end
  155.             end
  156.         end
  157.     end
  158. end
  159. function EquipTool()
  160.     if equipTool_ == true then
  161.         if r.durability() == nil then
  162.             for i = 1, r.inventorySize() do
  163.                 if r.count(i) ~= 0 then
  164.                     if ic.getStackInInternalSlot(i).name == "minecraft:iron_pickaxe" or
  165.                     ic.getStackInInternalSlot(i).name == "minecraft:diamond_pickaxe" then
  166.                         r.select(i)
  167.                         ic.equip()
  168.                         break
  169.                     end
  170.                 else
  171.                     if i == r.inventorySize() then
  172.                         print("нет кирок")
  173.                         return 1
  174.                     end
  175.                 end
  176.             end
  177.         end
  178.     end
  179.     return 0
  180. end
  181. function CheckEnergyLevel()
  182.     if checkEnergyLevel_ == true then
  183.         if math.floor(computer.energy() * 100 / computer.maxEnergy()) <= 10 then
  184.             Notifications(4)
  185.             return 1
  186.         end
  187.     end
  188.     return 0
  189. end
  190. function CheckRobotStatus(code)
  191.     print("CheckRobotStatus(code)")
  192.     if code == 2 then
  193.         tick = tick + 1
  194.         if tick == 101 then tick = 1 end
  195.         if tick % insertFuelSleep == 0 then if GenInsertFuel() == 1 then Home() end end
  196.         if tick % cleanInvSleep == 0 then CleanInv() end
  197.         if tick % checkLvlSleep == 0 then CheckLevel() end
  198.         if EquipTool() == 1 then Home() print("нет кирок, Home()") end
  199.         if CheckEnergyLevel() == 1 then Home() end
  200.     else
  201.         if GenInsertFuel() == 1 then Home() end
  202.         CleanInv()
  203.         CheckLevel()
  204.         if EquipTool() == 1 then Home() end
  205.         if CheckEnergyLevel() == 1 then Home() end
  206.     end
  207. end
  208. function EnterValues(code)
  209.     if code == 1 then
  210.         print("Введите текущее значение высоты робота:")
  211.         fPosition.y = tonumber(io.read())
  212.         print(fPosition.y)
  213.     end
  214.     if code == 2 then
  215.         print("Введите [y] если хотите продолжить:")
  216.         if io.read() ~= y then     
  217.             return 1
  218.         end
  219.     end
  220.     return 0
  221. end
  222. --устанавливает цели (координаты) для робота
  223. function TargetCreator()
  224.     if excavationPhase == 1 then
  225.         targetPosition.y = 3
  226.         if currentPosition.y == targetPosition.y then excavationPhase = 2 end
  227.     end
  228.     if excavationPhase == 2 then
  229.         if currentPosition.x == targetPosition.x and currentPosition.z == targetPosition.z and currentPosition.rotation == targetPosition.rotation then
  230.             spiralPhase = spiralPhase + 1
  231.             if currentPosition.rotation == 1 then targetPosition.z = targetPosition.z - math.floor((spiralPhase + 1) / 2) end
  232.             if currentPosition.rotation == 2 then targetPosition.x = targetPosition.x + math.floor((spiralPhase + 1) / 2) end
  233.             if currentPosition.rotation == 3 then targetPosition.z = targetPosition.z + math.floor((spiralPhase + 1) / 2) end
  234.             if currentPosition.rotation == 4 then targetPosition.x = targetPosition.x - math.floor((spiralPhase + 1) / 2) end
  235.             targetPosition.rotation = currentPosition.rotation + 1
  236.             if targetPosition.rotation == 5 then targetPosition.rotation = 1 end
  237.         end
  238.     end
  239.     if excavationPhase == 3 then
  240.         print("excavationPhase == 3")
  241.         if currentPosition.x == 0 and currentPosition.z == 0 then
  242.             targetPosition.y = fPosition.y
  243.             if currentPosition.y == fPosition.y then
  244.                 print(currentPosition.y)
  245.                 print(fPosition.y)
  246.                 print("высота набрана")
  247.                 if EnterValues(2) == 1 then
  248.                     return 1
  249.                 end
  250.                 CheckRobotStatus(1)
  251.                 Beep(2)
  252.                 excavationPhase = 4
  253.             end
  254.         else
  255.             if not(targetPosition.z == 0 and targetPosition.x == 0) then
  256.                 targetPosition.z = 0
  257.                 targetPosition.x = 0
  258.             end
  259.         end
  260.     end
  261.     if excavationPhase == 4 then
  262.         print("excavationPhase == 4")
  263.         if not(targetPosition.x == lastPosition.x and targetPosition.z == lastPosition.z
  264.             and targetPosition.y == lastPosition.y and targetPosition.rotation == lastPosition.rotation) then
  265.             targetPosition.x = lastPosition.x
  266.             targetPosition.z = lastPosition.z
  267.             targetPosition.y = lastPosition.y
  268.             targetPosition.rotation = lastPosition.rotation
  269.         else
  270.             excavationPhase = 2
  271.             targetPosition.x = lastPosition.tx
  272.             targetPosition.z = lastPosition.tz
  273.             targetPosition.y = lastPosition.ty
  274.             targetPosition.rotation = lastPosition.trotation
  275.         end
  276.     end
  277.     return 0
  278. end
  279. function Beep(code)
  280.     if code == 1 then
  281.         computer.beep(800,0.1)
  282.         computer.beep(1000,0.1)
  283.         computer.beep(1200,0.2)
  284.     end
  285.     if code == 2 then
  286.         computer.beep(1000,0.2)
  287.         computer.beep(1000,0.2)
  288.     end
  289. end
  290. function Home()
  291.     print("начало функции Home()")
  292.     excavationPhase = 3
  293.     lastPosition.x = currentPosition.x
  294.     lastPosition.z = currentPosition.z
  295.     lastPosition.y = currentPosition.y
  296.     lastPosition.rotation = currentPosition.rotation
  297.     lastPosition.tx = targetPosition.x
  298.     lastPosition.tz = targetPosition.z
  299.     lastPosition.ty = targetPosition.y
  300.     lastPosition.trotation = targetPosition.rotation
  301. end
  302. ----------------------------------------------
  303.  
  304. Beep(1)
  305. EnterValues(1)
  306. CheckRobotStatus(1)
  307. currentPosition.y = fPosition.y
  308. currentPosition.rotation = fPosition.rotation
  309. excavationPhase = 1
  310. Beep(2)
  311. --основной цикл программы 
  312. while true do
  313.     CheckRobotStatus(2)
  314.     if TargetCreator() == 1 then break end
  315.     --print(targetPosition.x .. " " .. targetPosition.z .. "   " .. currentPosition.x .. " " .. currentPosition.z)
  316.     if targetPosition.y ~= currentPosition.y then
  317.         if targetPosition.y > currentPosition.y then
  318.             Swing(2)
  319.             Move(2)
  320.         else
  321.             Swing(3)
  322.             Move(3)
  323.         end
  324.     else
  325.         if targetPosition.z < currentPosition.z then c = 1 end
  326.         if targetPosition.x > currentPosition.x then c = 2 end
  327.         if targetPosition.z > currentPosition.z then c = 3 end
  328.         if targetPosition.x < currentPosition.x then c = 4 end
  329.         if c ~= currentPosition.rotation and (currentPosition.x ~= targetPosition.x  or currentPosition.z ~= targetPosition.z) then
  330.             a = currentPosition.rotation
  331.             b = 0
  332.             while true do
  333.                 a = a + 1
  334.                 b = b + 1
  335.                 if a == 5 then a = 1 end
  336.                 if a == c then break end
  337.             end
  338.             if b > 2 then
  339.                 Turn(1)
  340.             else
  341.                 Turn(2)
  342.             end
  343.         else
  344.             if currentPosition.x ~= targetPosition.x  or currentPosition.z ~= targetPosition.z then
  345.                 Swing(1)
  346.                 if excavationPhase == 2 then
  347.                     Swing(2)
  348.                     Swing(3)
  349.                 end
  350.                 Move(1)
  351.             else
  352.                 if targetPosition.rotation ~= currentPosition.rotation then
  353.                     a = currentPosition.rotation
  354.                     b = 0
  355.                     while true do
  356.                         a = a + 1
  357.                         b = b + 1
  358.                         if a == 5 then a = 1 end
  359.                         if a == targetPosition.rotation then break end
  360.                     end
  361.                     if b > 2 then
  362.                         Turn(1)
  363.                     else
  364.                         Turn(2)
  365.                     end
  366.                     print("fPosition.y " .. fPosition.y)
  367.                 end
  368.             end
  369.         end
  370.     end
  371.     print("------------------------------------------------")
  372. end
  373. --pastebin run UzNG12tA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement