Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.79 KB | None | 0 0
  1. local component = require ("component")
  2. local computer = require("computer")
  3. local robot = require("robot")
  4. local sides = require("sides")
  5.  
  6.  
  7. robodigger = {
  8. x = 0,
  9. y = 0,
  10. z = 0,
  11. f = 0,
  12. rested = true,
  13. }
  14.  
  15. path = {}
  16. actions = {}
  17.  
  18. torch_counter = 0
  19. torch_max = 7
  20.  
  21. -- funzione per ruotare il robot e fargli ricordare la sua rotazione, rotazione oraria
  22. function sureturn (rotValue)
  23. if rotValue == 90 then
  24. robot.turnRight()
  25. robodigger.f = (robodigger.f + 90) %360
  26. elseif rotValue == -90 then
  27. robot.turnLeft()
  28. robodigger.f = (robodigger.f - 90) %360
  29. elseif rotValue == 180 or -180 then
  30. robot.turnAround()
  31. robodigger.f = (robodigger.f + 180) %360
  32. end
  33. end
  34.  
  35. function raddrizza()
  36. if robodigger.f == 90 then
  37. sureturn(-90)
  38. elseif robodigger.f == 180 then
  39. sureturn(180)
  40. elseif robodigger.f == 270 then
  41. sureturn(90)
  42. end
  43. end
  44.  
  45.  
  46. -- funzione per assicurare un movimento certo, e con un'aggiornamento delle coordinate corretto
  47. -- direction dovrebbe seguire sides.api
  48. -- movimenti assolti rispetto alla partenza
  49. -- se m è TRUE, allora il robot mina quello che trova davanti e quello sopra.
  50. -- i se TRUE ignora la stanchezza e specifica che sta tornando a casa
  51.  
  52. --[[function returnmove (side) --sto controllando bene se mi conviene farla
  53. moved = false
  54. while moved == false do
  55. if side = sides.top then
  56. if robot.up() == true then
  57. robodigger.y = robodigger.y + 1
  58. moved = true
  59. else os.sleep(0.5)
  60. end
  61.  
  62. else if side == sides.down then
  63. if robot.down() == true then
  64. robodigger.y = robodigger.y -1
  65. moved = true
  66. else os.sleep(0.5)
  67. end]]
  68.  
  69.  
  70. function suremove (side, m, i)
  71. moved = false
  72. tries = 3
  73. while moved == false do
  74.  
  75. if side == sides.top then
  76. if m == true and robot.detectUp() == true then
  77. if robot.swingUp() == false then
  78. switchtodiamond(side)
  79. end
  80. end
  81. if robot.up() == true then
  82. robodigger.y = robodigger.y + 1
  83. moved = true
  84. else tries = tries -1 os.sleep(0.5)
  85. end
  86.  
  87. elseif side == sides.down then
  88. if m == true and robot.detectDown() == true then
  89. if robot.swingDown() == false then
  90. switchtodiamond(side)
  91. end
  92. end
  93. if robot.down() == true then
  94. robodigger.y = robodigger.y - 1
  95. moved = true
  96. else tries = tries -1 os.sleep(0.5)
  97. end
  98.  
  99. else
  100. if side == sides.front then
  101. if robodigger.f == 90 then
  102. sureturn(-90)
  103. elseif robodigger.f == 270 then
  104. sureturn(90)
  105. elseif robodigger.f == 180 then
  106. sureturn(180)
  107. end
  108. if m == true and robot.detect() == true then
  109. if robot.swing() == false then
  110. switchtodiamond(side)
  111. end
  112. end
  113. if robot.forward() == true then
  114. robodigger.x = robodigger.x +1
  115. moved = true
  116. component.chat.say("mosso")
  117. else tries = tries -1 os.sleep(0.5)
  118. component.chat.say("non mosso")
  119. end
  120. end
  121.  
  122. if side == sides.right then
  123. if robodigger.f == 180 then
  124. sureturn(-90)
  125. elseif robodigger.f == 0 then
  126. sureturn(90)
  127. elseif robodigger.f == 270 then
  128. sureturn(180)
  129. end
  130.  
  131. if m == true and robot.detect() == true then
  132. if robot.swing() == false then
  133. switchtodiamond(side)
  134. end
  135. end
  136. if robot.forward() == true then
  137. robodigger.z = robodigger.z +1
  138. moved = true
  139. else tries = tries -1 os.sleep(0.5)
  140. end
  141. end
  142.  
  143. if side == sides.back then
  144. if robodigger.f == 270 then
  145. sureturn(-90)
  146. elseif robodigger.f == 90 then
  147. sureturn(90)
  148. elseif robodigger.f == 0 then
  149. sureturn(180)
  150. end
  151. if m == true and robot.detect() == true then
  152. if robot.swing() == false then
  153. switchtodiamond(side)
  154. end
  155. end
  156. if robot.forward() == true then
  157. robodigger.x = robodigger.x - 1
  158. moved = true
  159. else tries = tries -1 os.sleep(0.5)
  160. end
  161. end
  162.  
  163. if side == sides.left then
  164. if robodigger.f == 0 then
  165. sureturn(-90)
  166. elseif robodigger.f == 180 then
  167. sureturn(90)
  168. elseif robodigger.f == 90 then
  169. sureturn(180)
  170. end
  171. if m == true and robot.detect() == true then
  172. if robot.swing() == false then
  173. switchtodiamond(side)
  174. end
  175. end
  176. if robot.forward() == true then
  177. robodigger.z = robodigger.z - 1
  178. moved = true
  179. else tries = tries -1 os.sleep(0.5)
  180. end
  181. end
  182.  
  183. end
  184. if i == false then
  185. checkstats()
  186. if tries == 0 or robodigger.rested == false then
  187. component.chat.say("REEEEEE")
  188. return false end
  189. end
  190. end
  191. return true end
  192.  
  193. -- funzione go to, per decidere in che coordinate rispetto all'inizio andare. r sta per "relativo", "m" passa "mining" a suremove
  194. -- non_pb controlla se pathbuilder deve essere chiamato
  195. function gogoto (tox, toy, toz, r, m, non_pb)
  196. if r == true then
  197. tox = tox + robodigger.x
  198. toy = toy + robodigger.y
  199. toz = toz + robodigger.z
  200. end
  201. while robodigger.x ~= tox do
  202. if robodigger.x < tox then
  203. if suremove(sides.forward, m, false) == false then return false end
  204. else
  205. if suremove(sides.back, m, false) == false then return false end
  206. end
  207. end
  208.  
  209. while robodigger.z ~= toz do
  210. if robodigger.z < toz then
  211. if suremove(sides.right, m, false) == false then return false end
  212. else
  213. if suremove(sides.left, m, false) == false then return false end
  214. end
  215. end
  216.  
  217. while robodigger.y ~= toy do
  218. if robodigger.y < toy then
  219. if suremove(sides.up, m, false) == false then return false end
  220. else
  221. if suremove(sides.down, m, false) == false then return false end
  222. end
  223. end
  224.  
  225. if non_pb == false then --questa parte non compare mai in invgogoto, che è intesa per tornare indietro nel pathfinding
  226. pathbuilder()
  227. end
  228. end
  229.  
  230. function invgogoto (tox, toy, toz) --molto diversa da gogoto, non usabile per muoversi in avanti
  231. while robodigger.y ~= toy do
  232. if robodigger.y < toy then
  233. if suremove(sides.up, m, true) == false then return false end
  234. else
  235. if suremove(sides.down, m, true) == false then return false end
  236. end
  237. end
  238. while robodigger.z ~= toz do
  239. if robodigger.z < toz then
  240. if suremove(sides.right, m, true) == false then return false end
  241. else
  242. if suremove(sides.left, m, true) == false then return false end
  243. end
  244. end
  245. while robodigger.x ~= tox do
  246. if robodigger.x < tox then
  247. if suremove(sides.forward, m, true) == false then return false end
  248. else
  249. if suremove(sides.back, m, true) == false then return false end
  250. end
  251. end
  252. end
  253.  
  254. -- sides obbidisce a sides.api per dare una direzione alla branch. "indietro" chiede se deve tornare alle coordinate di partenza
  255. function minebranch (lenght, height, side, indietro)
  256. branchstart = {
  257. x = robodigger.x,
  258. y = robodigger.y,
  259. z = robodigger.z
  260. }
  261. if side == sides.forward then
  262. torch_counter = 4
  263. for i=1,lenght do
  264. if gogoto(1, height, 0, true, true, true) == false then return false end
  265. gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
  266. torch_counter = torch_counter +1
  267. if torch_counter == torch_max then
  268. robot.turnRight()
  269. robot.swing()
  270. robot.place()
  271. torch_counter = 0
  272. robot.turnLeft()
  273. end
  274. end
  275. if indietro == true then
  276. gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
  277. else
  278. pathbuilder()
  279. end
  280. elseif side == sides.back then
  281. torch_counter = 4
  282. for i=1,lenght do
  283. if gogoto(-1, height, 0, true, true, true) == false then return false end
  284. gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
  285. torch_counter = torch_counter +1
  286. if torch_counter == torch_max then
  287. robot.turnRight()
  288. robot.swing()
  289. robot.place()
  290. torch_counter = 0
  291. robot.turnLeft()
  292. end
  293. end
  294. if indietro == true then
  295. gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
  296. else
  297. pathbuilder()
  298. end
  299. elseif side == sides.right then
  300. torch_counter = 4
  301. for i=1,lenght do
  302. if gogoto(0, height, 1, true, true, true) == false then return false end
  303. gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
  304. torch_counter = torch_counter +1
  305. if torch_counter == torch_max then
  306. robot.turnRight()
  307. robot.swing()
  308. robot.place()
  309. torch_counter = 0
  310. robot.turnLeft()
  311. end
  312. end
  313. if indietro == true then
  314. gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
  315. else
  316. pathbuilder()
  317. end
  318. elseif side == sides.left then
  319. torch_counter = 4
  320. for i=1,lenght do
  321. if gogoto(0, height, -1, true, true, true) == false then return false end
  322. gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
  323. component.chat.say("scendo!")
  324. torch_counter = torch_counter +1
  325. if torch_counter == torch_max then
  326. robot.turnRight()
  327. robot.swing()
  328. robot.place()
  329. torch_counter = 0
  330. robot.turnLeft()
  331. end
  332. end
  333. if indietro == true then
  334. gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
  335. else
  336. pathbuilder()
  337. end
  338. end
  339. end
  340.  
  341. function minevolume (depth, height, width, side)
  342. start = {
  343. x = robodigger.x,
  344. y = robodigger.y,
  345. z = robodigger.z
  346. }
  347. minebranch(1, height, side, false)
  348. for i=1, width-1, 2 do
  349. minebranch(depth-1, height, side, false)
  350. if side == sides.forward then
  351. minebranch(1, height, sides.left, false)
  352. minebranch(depth-1, height, sides.back, false)
  353. minebranch(1, height, sides.left, false)
  354. elseif side == sides.back then
  355. minebranch(1, height, sides.right, false)
  356. minebranch(depth-1, height, sides.forward, false)
  357. minebranch(1, height, sides.right, false)
  358. elseif side == sides.right then
  359. minebranch(1, height, sides.forward, false)
  360. minebranch(depth-1, height, sides.left, false)
  361. minebranch(1, height, sides.forward, false)
  362. elseif side == sides.left then
  363. minebranch(1, height, sides.back, false)
  364. minebranch(depth-1, height, sides.right, false)
  365. minebranch(1, height, sides.back, false)
  366. end
  367. if i == width -1 and width % 2 ~= 0 then
  368. minebranch(depth, height, side, false)
  369. end
  370. end
  371. gogoto(start.x, start.y, start.z)
  372. end
  373.  
  374. function checkstats ()
  375. if computer.maxEnergy() > computer.energy()*4 then
  376. component.chat.say("Sono stanco!")
  377. robodigger.rested = false
  378. return false
  379. end
  380. if robot.durability() == nil then
  381. component.chat.say("Tool rotto!")
  382. robodigger.rested = false
  383. return false
  384. end
  385. if component.inventory_controller.getStackInInternalSlot().label == "Torch" and component.inventory_controller.getStackInInternalSlot().size == 1 then
  386. component.chat.say("Finite torce!")
  387. robodigger.rested = false
  388. return false
  389. end
  390. if component.inventory_controller.getStackInInternalSlot(robot.inventorySize()) == true then -- controlla se c'è qualcosa nell'ultimo slot
  391. component.chat.say("inventario pieno!")
  392. robodigger.rested = false
  393. return false
  394. end
  395. end
  396.  
  397. function pathbuilder ()
  398. table.insert(path, {robodigger.x, robodigger.y, robodigger.z})
  399. print(path[#path][1])
  400. print(path[#path][2])
  401. print(path[#path][3])
  402. end
  403.  
  404. --usa pathbuilder per tornare indietro con invgogoto
  405. function ritorno()
  406. for i =#path, 1, -1 do
  407. component.chat.say(tostring(i))
  408. invgogoto(path[i][1], path[i][2], path[i][3], false, false)
  409. end
  410. raddrizza()
  411. end
  412.  
  413. -- usa pathbuilder per tornare all'ultimo passaggio valido, e a seconda della manovra fatta, prova a superare il problema
  414. function fixmove()
  415. invgogoto(path[#path][1], path[#path][2], path[#path][3], false, false, true)
  416. end
  417.  
  418. function rest()
  419. end
  420.  
  421. function switchtodiamond (side)
  422. robot.select(2)
  423. component.inventory_controller.equip()
  424. if side == sides.top then
  425. robot.swingUp()
  426. elseif side == sides.down then
  427. robot.swingDown()
  428. else
  429. robot.swing()
  430. end
  431. component.inventory_controller.equip()
  432. robot.select(1)
  433. end
  434.  
  435.  
  436.  
  437. -- main loop
  438. while true do
  439. pathbuilder()
  440. if gogoto(5,0,0, false, true, false) == false then
  441.  
  442. break
  443. end
  444. ritorno()
  445. raddrizza()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement