Advertisement
Asioron

Untitled

Jul 16th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. local c = require("component")
  2. local fs = require("filesystem")
  3. local term = require('term')
  4. local r = require("robot")
  5. local srl = require("serialization")
  6. local ic = c.inventory_controller
  7. local red = c.redstone
  8. local craftItem = {}
  9.  
  10. local function rob(np,n,rz) --движение робота (направление, повтор, не разрушать) (0;вниз 1;вверх 2:назад 3:вперед 4:налево 5:направо 6:кругом)
  11. local err = 0
  12. local errStop = 10
  13. if not n then
  14. n = 1
  15. end
  16. for i=1,n do
  17. if np == 1 then --вверх
  18. while not r.up() do
  19. if not rz then
  20. r.swingUp()
  21. else
  22. os.sleep(0)
  23. if err > errStop then
  24. break
  25. else
  26. err = err + 1
  27. end
  28. end
  29. end
  30. elseif np == 0 then --вниз
  31. while not r.down() do
  32. if not rz then
  33. r.swingDown()
  34. else
  35. os.sleep(0)
  36. if err > errStop then
  37. break
  38. else
  39. err = err + 1
  40. end
  41. end
  42. end
  43. elseif np == 3 then --вперед
  44. while not r.forward() do
  45. if not rz then
  46. r.swing()
  47. else
  48. os.sleep(0)
  49. if err > errStop then
  50. break
  51. else
  52. err = err + 1
  53. end
  54. end
  55. end
  56. elseif np == 4 then --налево
  57. r.turnLeft()
  58. elseif np == 5 then --направо
  59. r.turnRight()
  60. elseif np == 6 then --кругом
  61. r.turnAround()
  62. elseif np == 2 then --назад
  63. while not r.back() do
  64. if not rz then
  65. rob(6)
  66. rob(3)
  67. rob(6)
  68. else
  69. os.sleep(0)
  70. if err > errStop then
  71. break
  72. else
  73. err = err + 1
  74. end
  75. end
  76. end
  77. end
  78. end
  79. if err > errStop then
  80. return false
  81. else
  82. return true
  83. end
  84. end
  85.  
  86. local function build()
  87. rob(3,3)
  88. r.select(1)
  89. for z = 1, 3 do
  90. for y = 1, 3 do
  91. for x = 1, 3 do
  92. if z == 2 and y == 2 and x == 2 then
  93. r.select(2)
  94. r.placeDown(0)
  95. r.select(1)
  96. else
  97. r.placeDown(0)
  98. end
  99. if x ~= 3 then
  100. rob(3,1)
  101. end
  102. end
  103. if math.fmod(y, 2) == 1 then
  104. if y ~= 3 then
  105. rob(4)
  106. rob(3,1)
  107. rob(4)
  108. end
  109. else
  110. rob(5)
  111. rob(3,1)
  112. rob(5)
  113. end
  114. end
  115. rob(5)
  116. rob(3,2)
  117. rob(5)
  118. rob(3,2)
  119. rob(6)
  120. rob(1)
  121. end
  122. rob(3)
  123. rob(4)
  124. rob(3)
  125. r.select(3)
  126. r.dropDown(1)
  127. end
  128.  
  129. local function seachLabel(label, size) --поиск в сундуке
  130. for i=1, ic.getInventorySize(1) do
  131. vr = ic.getStackInSlot(1, i)
  132. if vr and vr.label == label and vr.size >= size then
  133. return i
  134. end
  135. end
  136. return false
  137. end
  138.  
  139. local function loadCraft()
  140. print('Загрузка базы крафта')
  141. local f = io.open("/usr/bin/craft.tbl", "r")
  142. if f then
  143. craftItem = srl.unserialize(f:read("*a"))
  144. f:close()
  145. print('Успешно!')
  146. else
  147. print('Ошибка!')
  148. print('Необходимо закрепить ресурсы крафта')
  149. for i = 1, 2 do
  150. print('Разложите ресурсы для #'..i..' крафта')
  151. print('Пожалуйста положите: \n 1 слот образец стенки\n 2 слот центральный блок \n 3 слот предмет активации \nПо окончании нажмите enter')
  152. io.read()
  153. craftItem[i] = {ic.getStackInInternalSlot(1).label, ic.getStackInInternalSlot(2).label, ic.getStackInInternalSlot(3).label}
  154. print('Крафт #'..i..' записан')
  155. end
  156. if not fs.exists('/usr/bin/') then
  157. fs.makeDirectory('/usr/bin/')
  158. end
  159. f = io.open("/usr/bin/craft.tbl", "w")
  160. f:write(srl.serialize(craftItem))
  161. f:close()
  162. end
  163. end
  164.  
  165. local function seachItem()
  166. local tmp = {}
  167. local size = 1
  168. for i = 1, 2 do
  169. for j = 1, 3 do
  170. if j == 1 then
  171. size = 26
  172. else
  173. size = 1
  174. end
  175. print('Поиск '..craftItem[i][j])
  176. if seachLabel(craftItem[i][j], size) then
  177. tmp[j] = true
  178. else
  179. tmp[j] = false
  180. end
  181. end
  182. if tmp[1] and tmp[2] and tmp[3] then
  183. return i
  184. end
  185. end
  186. return false
  187. end
  188.  
  189. local function export()
  190. for i = 1, r.inventorySize() do
  191. if r.count(i) > 0 then
  192. r.select(i)
  193. r.dropUp(64)
  194. end
  195. end
  196. r.select(1)
  197. end
  198.  
  199. local function importItem(craft)
  200. local tmp, size = 0, 0
  201. for i = 1, 3 do
  202. r.select(i)
  203. if i == 1 then
  204. size = 26
  205. else
  206. size = 1
  207. end
  208. tmp = seachLabel(craftItem[craft][i], size)
  209. if tmp then
  210. if ic.suckFromSlot(1, tmp, size) ~= size then
  211. print('Ошибка, не смог собрать ресурсы')
  212. export()
  213. return false
  214. end
  215. else
  216. print('Ошибка, нехватает ресурсов')
  217. export()
  218. return false
  219. end
  220. end
  221. return true
  222. end
  223.  
  224. local function goHome()
  225. rob(4)
  226. rob(3,3)
  227. rob(4)
  228. rob(3,1)
  229. rob(0,3)
  230. rob(5)
  231. rob(3,1)
  232. rob(6)
  233. end
  234.  
  235. local function sorting()
  236. print('Выполняется объединение стаков')
  237. for i = 1, ic.getInventorySize(1) do
  238. r.suckUp(64)
  239. end
  240. export()
  241. end
  242.  
  243. loadCraft()
  244. sorting()
  245. term.clear()
  246. while true do
  247. if red.getInput(4) > 0 then
  248. print('Поиск ресурсов для крафта')
  249. tmp = seachItem()
  250. if tmp then
  251. print('Ресурсы для крафта #'..tmp..' найдены')
  252. if importItem(tmp) then
  253. print('Выполняется строительство крафта')
  254. build()
  255. export()
  256. print('Возврат на базу')
  257. goHome()
  258. sorting()
  259. print('Ожидаю завершения крафта')
  260. os.sleep(20)
  261. else
  262. term.clear()
  263. print('Ошибка при получении ресурсов!')
  264. os.sleep(10)
  265. end
  266. else
  267. sorting()
  268. term.clear()
  269. print('Ресурсы не найдены!')
  270. os.sleep(10)
  271. end
  272. else
  273. term.clear()
  274. print('Ожидаю сигнала редстоуна')
  275. os.sleep(10)
  276. end
  277. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement