Advertisement
bamik

LaserTonnel.lua

Oct 6th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | None | 0 0
  1. local robot=require("robot")
  2. local component=require("component")
  3. local computer=require("computer")
  4. local ic = component.inventory_controller
  5. local a = {...}
  6. local xLength=tonumber(a[1]) or 1024
  7. local zLength=tonumber(a[2]) or 20
  8. local zDepth=tonumber(a[3]) or 2
  9. local dLayer=tonumber(a[4]) or 3
  10. local nStart=5
  11. local tDumpster ={
  12. "minecraft:cobblestone",
  13. "minecraft:dirt",
  14. "minecraft:gravel",
  15. "chisel:granite",
  16. "chisel:limestone",
  17. "chisel:andesite",
  18. "chisel:diorite"
  19.                  }
  20. -- 1 слот - кирка
  21. local nKir=1
  22. -- 2 слот - рычаг
  23. local nRyc=2
  24. -- 3 слот - зарядник
  25. local nZar=3
  26. -- 4 слот - трансивер
  27. local nTra=4
  28. local function goForward()
  29. -- Для прохождения обсидиана, приходится жертвовать быстродействием. При встрече непреодолимых блоков, требуется вернуться назад и закончить работу.
  30. local k=0
  31. -- Если произведено 10 попытки пройти, то блок непроходимый. Возвращаемся назад. Если будут ложные срабатывания, то увеличить попытки.
  32. while not robot.forward() do k=k+1 robot.select(nKir) ic.equip() robot.swing() ic.equip() robot.use()  if k>9 then return false end end
  33. robot.suck()
  34. return true
  35. end
  36. local function goUp()
  37. while not robot.up() do robot.useUp() robot.swingUp() end
  38. end
  39. local function goDown()
  40. while not robot.down() do robot.select(nKir) ic.equip() robot.swingDown() ic.equip() robot.useDown() end
  41. end
  42. local function goBack()
  43. if not robot.back() then robot.turnAround() goForward() robot.turnAround() end
  44. end
  45. local function doPlace()
  46. if not robot.place() then goForward() goBack() robot.place() end
  47. end
  48. local function suckLaser()
  49. ll=false
  50. for jj=9,16 do if ic.getStackInSlot(3,jj) ~= nil then if ic.getStackInSlot(3,jj).label=="Mining Laser" then ic.suckFromSlot(3,jj) ll=true end end end
  51. return ll
  52. end
  53. -- Проверка на мусор
  54. local function dumpster(sNN)
  55. local log=false
  56. local i,j=1,#tDumpster
  57. repeat
  58. if sNN==tDumpster[i] then log=true end
  59. i=i+1
  60. until log or i>j
  61. return log
  62. end
  63. -- Выгрузка инвентаря в указанную сторону. Если нет сундука, то выброс мусора и уплотнение.
  64. -- Слоты до указанного не трогаются.
  65. local function drop(nSd, nSt)
  66.   if ic.getInventorySize(nSd) ~= nil then
  67.     for i = nSt, robot.inventorySize() do
  68.       robot.select(i)
  69. --      while getStackInSlot(nSd,1) ~= nil do os.sleep(0.5) end
  70. --    ic.dropIntoSlot(nSd,1)
  71.       robot.drop()
  72.     end
  73.   else
  74.     local i,j=nSt, robot.inventorySize()
  75.     while i ~= j do
  76.     robot.select(j)
  77.     local t = ic.getStackInInternalSlot(j)
  78.     if t ~= nil then
  79.     local sN = t.name
  80.       if dumpster(sN)   then robot.drop() j=j-1
  81.       else robot.transferTo(i) i=i+1
  82.       end
  83.     else
  84.       j=j-1
  85.     end
  86.     end
  87.   end
  88.   robot.select(nStart)
  89. end
  90. local function zaryad()
  91. goForward()
  92. -- Устанавливаем трансивер
  93. robot.select(nTra) doPlace()
  94. -- Заряжаем лазер или бур
  95. robot.select(nKir) ic.equip() robot.drop() drop(3,nStart) robot.select(nKir) while not suckLaser() do os.sleep(15) end ic.equip() robot.select(nStart)
  96. -- Устанавливаем зарядник
  97. goUp() robot.select(nZar) doPlace() robot.select(nStart)
  98. -- Устанавливаем рычаг и включаем его
  99. goUp() robot.select(nRyc) robot.placeDown(3) ic.equip() robot.useDown(3) ic.equip() robot.select(nStart)
  100. -- Встаем на зарядку
  101. goForward() while computer.energy()<0.99* computer.maxEnergy() do os.sleep(15) end robot.select(1)
  102.  
  103. -- Снимаем
  104. goBack() goDown() goForward()
  105. goBack() goDown() goForward()
  106. goBack() goBack() robot.select(nStart)
  107. end
  108.  
  109. -- Тело программы
  110. -- Индикатор проходимости
  111. lL=true
  112. -- Копаем dLayer тоннелей
  113. for dL=1,dLayer do
  114. -- Копаем тоннель высотой zDepth длиной xLength с уступами, чтобы не превысить максимальную высоту полета робота.
  115. for zD=0,zDepth-1 do
  116. for x=1,xLength-zD do lL=goForward()
  117. -- Если блок непроходимый, то запоминаем текущую позицицию Х.
  118. if not lL then xLength=x break end
  119. end
  120. -- Если был непроходимый блок, то завершаем раскопки.
  121. if not lL then break end
  122. goDown() robot.turnAround() end
  123. -- Если был непроходимый блок, то завершаем раскопки и запоминаем количество подъемов.
  124. if not lL then dLayer=dL break end
  125. -- Поднимаемся на высоту следующего тоннеля
  126. for zL=1,zLength+zDepth do goUp() end
  127. -- Если робот или инструмент разрядились более чем на 50%, то заряжаемся.
  128. robot.select(1) ic.equip() tL=ic.getStackInInternalSlot(1) ic.equip()
  129. local robEn=computer.energy()/computer.maxEnergy()
  130. local lasDm=tL.damage/tL.maxDamage
  131. print(robEn, lasDm)
  132. if (robEn<0.5) or (lasDm>0.5) then zaryad() end
  133. end
  134. if lL then
  135. for h=1,(zDepth+zLength)*dLayer do goDown() end
  136. else
  137. for h=1,xLength do goBack() end
  138. for h=1,(zDepth+zLength)*(dLayer-1) do goDown() end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement