Advertisement
bamik

SnailExcavate

Sep 24th, 2015
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.63 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 48
  7. local yLength=tonumber(a[2]) or 48
  8. local zDepth=tonumber(a[3]) or 7
  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. if component.isAvailable("chunkloader")
  21.     then
  22.     component.chunkloader.setActive(true)
  23. end
  24. -- Добыча 1 столба
  25. local function prey()
  26. while not robot.forward() do robot.swing() os.sleep(0,5) end
  27. while robot.swingUp() do os.sleep(0,5) end
  28. robot.swingDown()
  29. end
  30. -- Спуск на заданную глубину
  31. local function preyDown(dLL)
  32. for dLLL=1,dLL do
  33. while not robot.down() do robot.swingDown() end
  34. end
  35. end
  36. -- Добыча 1 траншеи
  37. local function trench(ll)
  38. for lll=1,ll do
  39. prey()
  40. -- while robot.durability()*1000000<100 do os.sleep(180) end
  41. end
  42. end
  43. -- Перемещение на заданное расстояние
  44. local function moveLine(ll)
  45. while ll>1 do while not robot.forward() do robot.swing() os.sleep(0,5) end ll=ll-1 end
  46. end
  47. -- 1 слот - кирка
  48. local nKir=1
  49. -- 2 слот - рычаг
  50. local nRyc=2
  51. -- 3 слот - зарядник
  52. local nZar=3
  53. -- 4 слот - трансивер
  54. local nTra=4
  55. local function goForward()
  56. while not robot.forward() do robot.swing() os.sleep(0,5) end
  57. robot.suck()
  58. return true
  59. end
  60. local function goUp()
  61. while not robot.up() do robot.swingUp() end
  62. end
  63. local function goDown()
  64. while not robot.down() do robot.swingDown() end
  65. end
  66. local function goBack()
  67. if not robot.back() then robot.turnAround() goForward() robot.turnAround() end
  68. end
  69. local function doPlace(nSlot)
  70. robot.select(nSlot)
  71. while not robot.place() do robot.select(nStart) goForward() goBack() robot.select(nSlot) end
  72. robot.select(nStart)
  73. end
  74. local function suckDrill()
  75. ll=false
  76. for jj=9,16 do if ic.getStackInSlot(3,jj) ~= nil then if ic.getStackInSlot(3,jj).label=="bamik_drill" then ic.suckFromSlot(3,jj) ll=true end end end
  77. return ll
  78. end
  79. -- Проверка на мусор
  80. local function dumpster(sNN)
  81. local log=false
  82. local i,j=1,#tDumpster
  83. repeat
  84. if sNN==tDumpster[i] then log=true end
  85. i=i+1
  86. until log or i>j
  87. return log
  88. end
  89. -- Выгрузка инвентаря в указанную сторону. Если нет сундука, то выброс мусора и уплотнение.
  90. -- Слоты до указанного не трогаются.
  91. local function drop()
  92.   if ic.getInventorySize(3) ~= nil then
  93.     for i = robot.inventorySize(), nStart, -1 do
  94.     robot.select(i)
  95.     while robot.count()>0 do robot.drop() end
  96.     end
  97.   else
  98.     local i,j=nStart, robot.inventorySize()
  99.     while i ~= j do
  100.     robot.select(j)
  101.     local t = ic.getStackInInternalSlot(j)
  102.     if t ~= nil then
  103.     local sN = t.name
  104.       if dumpster(sN)   then robot.drop() j=j-1
  105.       else robot.transferTo(i) i=i+1
  106.       end
  107.     else
  108.       j=j-1
  109.     end
  110.     end
  111.   end
  112. end
  113. local function zaryad()
  114. goForward()
  115. -- Устанавливаем трансивер
  116. doPlace(nTra)
  117. -- Заряжаем лазер или бур
  118. robot.select(nKir) ic.equip() robot.drop() drop() robot.select(nKir) while not suckDrill() do os.sleep(10) end ic.equip() robot.select(nStart)
  119. -- Устанавливаем зарядник
  120. goUp() doPlace(nZar)
  121. -- Устанавливаем рычаг и включаем его
  122. goUp() robot.select(nRyc) robot.placeDown(3) ic.equip() robot.useDown(3) ic.equip() robot.select(nStart)
  123. -- Встаем на зарядку
  124. goForward() while computer.energy()<0.99* computer.maxEnergy() do os.sleep(15) end
  125. -- Снимаем
  126. robot.select(nKir)
  127. goBack() goDown() goForward()
  128. goBack() goDown() goForward()
  129. goBack() goBack() robot.select(nStart)
  130. end
  131. local function servicing()
  132. -- Если инвентарь заполнился, то выброс мусора.
  133. -- Свободного места до переполнения, должно хватить от проверки до проверки. Оставляем 5 слотов.
  134. -- Для добычи в ваниле, можно оставить 2.
  135. robot.select(1) ic.equip()
  136. local tL=ic.getStackInInternalSlot(1)
  137. ic.equip()
  138. local robEn=computer.energy()/computer.maxEnergy()
  139. local lasDm=tL.damage/tL.maxDamage
  140. -- Если разрядился робот, бур или заполнился инвентарь, то проводим обслуживание.
  141. if (robot.count(robot.inventorySize()-5)>0) or (robEn<0.1) or (lasDm>0.9) then zaryad() end
  142. end
  143. -- Тело программы
  144. -- Текущая глубина копания
  145. local zD=zDepth
  146. -- Текущая толщина слоя копания
  147. local dL=dLayer
  148. repeat
  149. print(zD,dL)  
  150. xL=xLength
  151. yL=yLength-1
  152. repeat
  153. trench(xL)
  154. xL=xL-1
  155. servicing()
  156. robot.turnRight()
  157. trench(yL)
  158. yL=yL-1
  159. servicing()
  160. robot.turnRight()
  161. until xL<1 or yL<0
  162. -- Очищаемся от мусора перед возвратом в исходную позицию.
  163. -- drop()
  164. -- Вычисление длины "обратного" хода
  165. local xyBack=math.ceil((math.min(xLength, yLength)-1)/2)+1
  166. if math.min(xLength, yLength)%2 == 0 then
  167. yBack=xyBack
  168. xBack=yBack
  169. else
  170. robot.turnAround()
  171. if xLength-yLength<=0 then
  172. yBack=yLength-xyBack+1
  173. xBack=xyBack+1
  174. else
  175. xBack=xLength-xyBack+2
  176. yBack=xyBack
  177. end
  178. end
  179. robot.turnLeft()
  180. moveLine(yBack)
  181. robot.turnLeft()
  182. moveLine(xBack)
  183. robot.turnAround()
  184.  
  185. zD=zD-dL
  186. dL=math.min(zD,dL)
  187. preyDown(dL)
  188. until dL<1
  189. for k=1,zDepth do goUp() end
  190. zaryad()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement