bob558

копалка от серафима

Nov 5th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.86 KB | None | 0 0
  1. --[[ opencomputers карьер by serafim update 05.11.20
  2.  
  3. Без замены инструмента для неломаемой кирки из тинкера !!!
  4.  
  5. Копает змейкой квадрат проходя три слоя блоков за один подход,
  6. пока не выкопает весь обьём до бэдрока или не упрётся в неразрушаемый блок,
  7. при этом вернётся к старту и в случае проблем сообщит о ней,
  8. также предложит продолжить копать с последней позиции.
  9.  
  10. требования:
  11. инвентарь, контроллер инвентаря, генератор, улучшение парение.
  12.  
  13. пример сборки:
  14. https://i.imgur.com/T1X13Gk.png
  15.  
  16. использование:
  17. на старте, сзади робота поставить сундук,уголь больше 20 штук.
  18. Робот может работать без сундука и всего прочего, нужна только кирка.
  19.  
  20. пример запуска: (по умолчанию один чанк)
  21. quarry 16 16 2 или quarry
  22. -- 2 опустится в низ на 2 блока от старта.
  23.  
  24. получить список предметов:
  25. https://pastebin.com/au9etcfF
  26. ]]--
  27.  
  28. local a = {...}
  29. local width = tonumber(a[1]) or 16 --ширина
  30. local length = tonumber(a[2]) or 16 --длина
  31. local down = tonumber(a[3]) or 2 --опустится в низ
  32.  
  33. --список мусора
  34. local scrap = {
  35. "minecraft:cobblestone",
  36. "minecraft:dirt",
  37. "minecraft:gravel",
  38. "minecraft:flint",
  39. "minecraft:sand",
  40. "minecraft:sandstone"
  41. }
  42.  
  43. --список инструмента
  44. local toollist = {
  45. "ickaxe",
  46. "rill",
  47. "pick",
  48. "vajra"
  49. }
  50.  
  51. local com = require('component')
  52. local computer = require("computer")
  53. local cheststart,fuelchest,fuelinv,refuel = true,true,true,true
  54. local depth,xPos,zPos,xDir,zDir = 0,0,0,0,1
  55. local ridht,bedrock,unloaded = true,false,0
  56. local x,y,z,xd,zd
  57.  
  58. if not com.isAvailable("robot") then
  59. print("только роботы могут использовать эту программу")
  60. os.exit()
  61. end
  62. local r = require("robot")
  63.  
  64. local invsize = r.inventorySize()
  65. local tools = invsize
  66. local coal = invsize-1
  67.  
  68. if not com.isAvailable("inventory_controller") then
  69. print("для работы нужен контроллер инвентаря")
  70. os.exit()
  71. end
  72. local i_c = com.inventory_controller
  73.  
  74. if not com.isAvailable("generator") then
  75. print("для работы нужен генератор")
  76. os.exit()
  77. end
  78. local gen = com.generator
  79.  
  80. --чанк лоадер
  81. local function chunkload(state)
  82. if com.isAvailable("chunkloader") then
  83. cloader = com.chunkloader
  84. if state then
  85. cloader.setActive(true)
  86. print("чанк лоадер активен")
  87. else
  88. cloader.setActive(false)
  89. print("чанк лоадер деактивирован")
  90. end
  91. print("")
  92. end
  93. end
  94.  
  95. --поиск предмета в инвентаре робота
  96. local function slotitem(slot, list)
  97. for j, name in pairs(list) do
  98. local item = i_c.getStackInInternalSlot(slot)
  99. if item and string.find(item.name,name) then
  100. if item.charge == nil or item.charge > 10000 then
  101. return true
  102. end
  103. end
  104. end
  105. for i = 1, invsize do
  106. if r.count(i) > 0 then
  107. for j, name in pairs(list) do
  108. local item = i_c.getStackInInternalSlot(i)
  109. if item and string.find(item.name,name) then
  110. if item.charge == nil or item.charge > 10000 then
  111. r.select(i)
  112. r.transferTo(slot)
  113. return true
  114. end
  115. end
  116. end
  117. end
  118. end
  119. return false
  120. end
  121.  
  122. --взять из сундука расходники
  123. local function servise(slotinv, list)
  124. local inv = i_c.getInventorySize(3)
  125. if inv then
  126. if slotitem(slotinv, list) then
  127. return true
  128. end
  129. r.select(slotinv)
  130. if r.count(slotinv) > 0 and not r.drop() then
  131. returnHome("в сундуке нет места")
  132. return false
  133. end
  134. for slot = 1, inv do
  135. for j, name in pairs(list) do
  136. local item = i_c.getStackInSlot(3,slot)
  137. if item and string.find(item.name,name) then
  138. if item.charge == nil or item.charge > 10000 then
  139. i_c.suckFromSlot(3, slot)
  140. return true
  141. end
  142. end
  143. end
  144. end
  145. return false
  146. else
  147. returnHome("нет сундука")
  148. end
  149. end
  150.  
  151. --сортируем лут
  152. local function sortlut(droplut)
  153. print("сортирую лут...")
  154. r.swingDown()
  155. for i = 1, invsize do
  156. local slot = i_c.getStackInInternalSlot(i)
  157. if slot ~= nil then
  158. for k, name in pairs(scrap) do
  159. if slot.name == name then
  160. r.select(i)
  161. r.dropDown()
  162. end
  163. end
  164. end
  165. end
  166. for i = 1, invsize-2 do
  167. if r.count(i) == 0 then
  168. for j = invsize-2, 1, -1 do
  169. if r.count(j) > 0 then
  170. if j < i then
  171. break
  172. end
  173. r.select(j)
  174. r.transferTo(i)
  175. break
  176. end
  177. end
  178. end
  179. end
  180. local unload = function()
  181. local inv = i_c.getInventorySize(3)
  182. if inv then
  183. cheststart = true
  184. for i = 1,invsize-2 do
  185. if r.count(i) > 0 then
  186. r.select(i)
  187. unloaded = unloaded + r.count(i)
  188. if not r.drop() then
  189. returnHome("в сундуке нет места")
  190. return
  191. end
  192. end
  193. end
  194. servise(tools, toollist)
  195. r.select(coal)
  196. gen.insert()
  197. servise(coal, {"coal"})
  198. elseif not cheststart then
  199. returnHome("инвентарь заполнен")
  200. else
  201. returnHome("нет сундука")
  202. end
  203. fuelchest = true
  204. print("всего добыто руды: "..unloaded)
  205. end
  206. if r.count(invsize-6) > 0 then
  207. print("возвращаюсь к сундуку сложить лут")
  208. x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  209. goTo( 0,0,0,0,-1 )
  210. unload()
  211. goTo( x,y,z,xd,zd )
  212. end
  213. if droplut and cheststart then
  214. unload()
  215. end
  216. r.select(1)
  217. end
  218.  
  219. --стоп
  220. local function stop(message)
  221. print("\n".."!!! стоп !!!".."\n"..message.."\n")
  222. r.setLightColor(0xFF0000)
  223. r.turnAround()
  224. chunkload(false)
  225. computer.beep(1000, 1)
  226. print("программа завершена")
  227. os.exit()
  228. end
  229.  
  230. --возврат к старту
  231. function returnHome(message, save)
  232. r.setLightColor(0xFF0000)
  233. if save then
  234. x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  235. end
  236. if y ~= nil then
  237. print("ширина "..width.." длина "..length.." глубина "..y)
  238. end
  239. goTo( 0,0,0,0,-1 )
  240. if save and cheststart and r.count(1) > 0 then
  241. sortlut(true)
  242. end
  243. goTo( 0,0,0,0,1 )
  244. print("\n".."!!! стоп !!!".."\n"..message.."\n")
  245. chunkload(false)
  246. computer.beep(1000, 1)
  247. io.write("продолжить копать ? [y/n] > ")
  248. oq = io.read()
  249. if oq == "n" or oq == "N" then
  250. print("программа завершена")
  251. os.exit()
  252. else
  253. r.setLightColor(0xFFFFFF)
  254. print("продолжаю копать...")
  255. chunkload(true)
  256. goTo( x,y,z,xd,zd )
  257. end
  258. end
  259.  
  260. --дозаправка углём
  261. local function checkfuel()
  262. if computer.energy() < 6000 then
  263. refuel = true
  264. end
  265. if gen.count() <= 10 and refuel then
  266. for i = 1,invsize do
  267. local item = i_c.getStackInInternalSlot(i)
  268. if item and string.find(item.name,"coal") then
  269. r.select(i)
  270. r.transferTo(coal)
  271. end
  272. end
  273. r.select(coal)
  274. if gen.insert() then
  275. print("заправился, угля в генераторе = "..gen.count())
  276. fuelinv = true
  277. elseif fuelchest and cheststart then
  278. x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  279. print("возвращаюсь к сундуку за углем")
  280. goTo( 0,0,0,0,-1 )
  281. if servise(coal, {"coal"}) then
  282. r.select(coal)
  283. gen.insert()
  284. print("заправился, угля в генераторе = "..gen.count())
  285. else
  286. fuelchest = false
  287. print("в сундуке нет угля")
  288. end
  289. goTo( x,y,z,xd,zd )
  290. else
  291. refuel = false
  292. if fuelinv then
  293. print("нет угля в инвентаре робота !!!")
  294. fuelinv = false
  295. end
  296. end
  297. r.select(1)
  298. end
  299. if computer.energy() < 5000 then
  300. if gen.count() >= 10 then
  301. print("мало энергии, заряжаюсь до 10000")
  302. r.swingUp()
  303. while true do
  304. os.sleep(10)
  305. if computer.energy() >= 10000 then
  306. print("зарядился :)")
  307. break
  308. end
  309. if gen.count() == 0 then
  310. returnHome("закончился уголь",true)
  311. break
  312. end
  313. end
  314. else
  315. returnHome("закончился уголь",true)
  316. end
  317. end
  318. end
  319.  
  320. --идём по Х в 0 координат
  321. local function x_forward()
  322. for xx = 1,width-1 do
  323. while true do
  324. if r.forward() then
  325. xPos = xPos - 1
  326. break
  327. else
  328. r.swing()
  329. end
  330. end
  331. end
  332. end
  333.  
  334. --движемся в верх
  335. local function tryUp()
  336. local tru = 0
  337. while not r.up() do
  338. r.swingUp()
  339. tru = tru + 1
  340. if tru >= 5 then
  341. tru = 0
  342. returnHome("непреодолимое препятствие",true)
  343. end
  344. end
  345. depth = depth - 1
  346. end
  347.  
  348. --копаем и движемся вперед
  349. local function digForward()
  350. if r.detectUp() then
  351. r.swingUp()
  352. end
  353. if r.detect() then
  354. r.swing()
  355. end
  356. if r.detectDown() and not r.swingDown() then
  357. if bedrock then
  358. r.swingDown()
  359. else
  360. print("низ достигнут !")
  361. bedrock = true
  362. end
  363. end
  364. if r.count(invsize-2) > 0 then
  365. sortlut()
  366. end
  367. local tru = 0
  368. while true do
  369. if r.forward() then
  370. xPos = xPos + xDir
  371. zPos = zPos + zDir
  372. break
  373. end
  374. r.swing()
  375. tru = tru + 1
  376. if tru == 5 and bedrock then
  377. tryUp()
  378. end
  379. if tru >= 10 then
  380. tru = 0
  381. returnHome("непреодолимое препятствие",true)
  382. end
  383. end
  384. end
  385.  
  386. --спускаемся отлавливая бедрок
  387. local function go_down()
  388. for zz = 1,down do
  389. while true do
  390. if r.detectDown() and not r.swingDown() then
  391. print("низ достигнут !")
  392. bedrock = true
  393. end
  394. if r.down() then
  395. depth = depth + 1
  396. break
  397. elseif bedrock then
  398. while true do
  399. r.swing()
  400. if r.detect() then
  401. tryUp()
  402. else
  403. return
  404. end
  405. end
  406. end
  407. end
  408. end
  409. end
  410.  
  411. --поворот в лево
  412. local function turnLeft()
  413. r.turnLeft()
  414. xDir, zDir = -zDir, xDir
  415. end
  416.  
  417. --поворот в право
  418. local function turnRight()
  419. r.turnRight()
  420. xDir, zDir = zDir, -xDir
  421. end
  422.  
  423. --навигация к сундуку
  424. function goTo( x, y, z, xd, zd )
  425. local Xposition = function()
  426. if xPos > x then
  427. while xDir ~= -1 do
  428. turnLeft()
  429. end
  430. while xPos > x do
  431. if r.forward() then
  432. xPos = xPos - 1
  433. else
  434. r.swing()
  435. end
  436. end
  437. elseif xPos < x then
  438. while xDir ~= 1 do
  439. turnRight()
  440. end
  441. while xPos < x do
  442. if r.forward() then
  443. xPos = xPos + 1
  444. else
  445. r.swing()
  446. end
  447. end
  448. end
  449. end
  450. local Zposition = function()
  451. if zPos > z then
  452. while zDir ~= -1 do
  453. turnLeft()
  454. end
  455. while zPos > z do
  456. if r.forward() then
  457. zPos = zPos - 1
  458. else
  459. r.swing()
  460. end
  461. end
  462. elseif zPos < z then
  463. while zDir ~= 1 do
  464. turnRight()
  465. end
  466. while zPos < z do
  467. if r.forward() then
  468. zPos = zPos + 1
  469. else
  470. r.swing()
  471. end
  472. end
  473. end
  474. end
  475. r.select(1)
  476. while depth < y do
  477. if r.down() then
  478. depth = depth + 1
  479. else
  480. r.swingDown()
  481. end
  482. end
  483. if y > 0 then
  484. Zposition()
  485. Xposition()
  486. else
  487. Xposition()
  488. Zposition()
  489. end
  490. while depth > y do
  491. if r.up() then
  492. depth = depth - 1
  493. else
  494. r.swingUp()
  495. end
  496. end
  497. while zDir ~= zd or xDir ~= xd do
  498. turnRight()
  499. end
  500. end
  501.  
  502. --копаем
  503. local function dig()
  504. os.execute("cls")
  505. print("размер карьера: ширина "..width.." длина "..length)
  506. print("угля в генераторе = "..gen.count())
  507. print("энергии = "..math.floor(100*computer.energy()/computer.maxEnergy()).." %")
  508. print("подготовка...")
  509. goTo( 0,0,0,0,-1 )
  510. for slot = 1, invsize do
  511. if r.count(slot) == 0 then
  512. r.select(slot)
  513. i_c.equip()
  514. break
  515. end
  516. if slot == invsize then
  517. stop("в инвентаре робота нет пустых слотов")
  518. end
  519. end
  520. local inv = i_c.getInventorySize(3)
  521. if inv then
  522. cheststart = true
  523. print("доступен сундук")
  524. if servise(coal, {"coal"}) then
  525. r.select(coal)
  526. if gen.insert() then
  527. print("заправился, угля в генераторе = "..gen.count())
  528. servise(coal, {"coal"})
  529. end
  530. else
  531. fuelchest = false
  532. end
  533. if servise(tools, toollist) then
  534. r.select(tools)
  535. i_c.equip()
  536. servise(tools, toollist)
  537. else
  538. stop("нет инструмента")
  539. end
  540. else
  541. cheststart = false
  542. print("сундук сзади робота не найден")
  543. checkfuel()
  544. if slotitem(tools, toollist) then
  545. r.select(tools)
  546. i_c.equip()
  547. slotitem(tools, toollist)
  548. else
  549. stop("нет инструмента")
  550. end
  551. end
  552. goTo( 0,0,0,0,1 )
  553. r.setLightColor(0xFFFFFF)
  554. chunkload(true)
  555. print("опускаюсь на "..down.." блока(ов)")
  556. for height = 1,256 do
  557. refuel = true
  558. checkfuel()
  559. go_down()
  560. down = 3
  561. for xx = 1,width do
  562. for yy = 1,length-1 do
  563. digForward()
  564. end
  565. checkfuel()
  566. if xx ~= width then
  567. if ridht then
  568. turnRight()
  569. digForward()
  570. turnRight()
  571. ridht = false
  572. else
  573. turnLeft()
  574. digForward()
  575. turnLeft()
  576. ridht = true
  577. end
  578. end
  579. end
  580. r.swingUp()
  581. r.swingDown()
  582. if ridht then
  583. turnLeft()
  584. x_forward()
  585. turnLeft()
  586. ridht = false
  587. else
  588. turnRight()
  589. x_forward()
  590. turnRight()
  591. ridht = true
  592. end
  593. if bedrock then
  594. goTo( 0,0,0,0,-1 )
  595. sortlut(true)
  596. stop("завершил копать :)")
  597. end
  598. end
  599. end
  600.  
  601. dig()
Add Comment
Please, Sign In to add comment