Advertisement
Guest User

Navigacia

a guest
Jul 20th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. -- b = peripheral.wrap("left") Iba pri compass turtle
  3.  
  4. --Začiatok:
  5. term.clear()
  6. term.setCursorPos (1,1)
  7. print "Postav Turtlu na osu X smerom tak, aby ked isla dopredu pridala +1 na osi x."
  8. print " "
  9. print "Pre potvrdenie stlac klavesu 'y'"
  10. event, p1 = os.pullEvent("char")
  11.  
  12. if event == "char" and p1 == "y" then
  13.  
  14. term.clear()
  15. term.setCursorPos (1,1)
  16. print "Napis spravu ktora sa zobrazi ked dojde turtla do ciela:"
  17. print ("Sprava: ")
  18. term.setCursorPos (9,3)
  19. final = tostring(read())
  20.  
  21. term.clear()
  22. term.setCursorPos (1,1)
  23. print "Napis suradnice turtly:"
  24. print ("x:")
  25. term.setCursorPos (4,2)
  26. x = tonumber(read())
  27. print ("y:")
  28. term.setCursorPos (4,3)
  29. y = tonumber(read())
  30. print ("z:")
  31. term.setCursorPos (4,4)
  32. z = tonumber(read())
  33.  
  34. term.clear()
  35. term.setCursorPos (1,1)
  36. print "Napis suradnice miesta:"
  37. print ("x:")
  38. term.setCursorPos (4,2)
  39. gox = tonumber(read())
  40. print ("y:")
  41. term.setCursorPos (4,3)
  42. goy = tonumber(read())
  43. print ("z:")
  44. term.setCursorPos (4,4)
  45. goz = tonumber(read())
  46.  
  47. --Kontrola paliva:
  48. tx = x
  49. tgox = gox
  50. tz = z
  51. tgoz = goz
  52. if goy > y then zostatoky = goy - y else zostatoky = y - goy end
  53. if tgox > tx then zostatokx = tgox - tx else zostatokx = tx - tgox end
  54. if tgoz > tz then zostatokz = tgoz - tz else zostatokz = tz - tgoz end
  55. math.abs(zostatokz)
  56. math.abs(zostatokx)
  57. totalfuel = zostatokz + zostatokx + zostatoky
  58. totaltotal = totalfuel + turtle.getFuelLevel()
  59. totalcoal = math.floor(totalfuel/80)
  60. totalcoal = totalcoal + 2
  61. if totalfuel < turtle.getFuelLevel() then
  62. else
  63. totalfuel = totalfuel - turtle.getFuelLevel()
  64. if totalcoal <= 64 then
  65. ter = false
  66. else
  67. ter = true
  68. coal = 0
  69. while totalcoal >= 64 do
  70. totalcoal = totalcoal - 64
  71. coal = coal + 1
  72. end
  73. end
  74.  
  75. term.clear()
  76. term.setCursorPos (1,1)
  77. if ter == true then
  78. if totalcoal == 0 then
  79. print ("Turtla potrebuje "..coal.."x64 uhlia. Vloz uhlie do inventara turtlicky!")
  80. else
  81. print ("Turtla potrebuje "..coal.."x64 + "..totalcoal.." uhlia. Vloz uhlie do inventara turtlicky!")
  82. end
  83. else
  84. print ("Turtla potrebuje "..totalcoal.." uhlia. Vloz uhlie do inventara turtlicky!")
  85. end
  86. print ("Potrebuje: "..totaltotal.." paliva")
  87. while turtle.getFuelLevel() < totaltotal do
  88. for l = 1,16 do
  89. turtle.select(l)
  90. turtle.refuel(64)
  91. term.setCursorPos (1,4)
  92. print ("Palivo: "..turtle.getFuelLevel().."")
  93. if turtle.getFuelLevel() > totaltotal then
  94. turtle.select(1)
  95. end
  96. end
  97. end
  98. end
  99.  
  100. --Timeout Turtly:
  101. for i=1,5 do
  102. term.clear()
  103. term.setCursorPos (1,1)
  104. print ("Turtla vyrazi za 5 sekund!")
  105. term.setCursorPos (1,2)
  106. print (i)
  107. sleep(1)
  108. end
  109.  
  110. --Zápis pozícii:
  111. term.clear()
  112. term.setCursorPos (1,1)
  113. print ("Idem z pozicie: ")
  114. print ("x:"..x)
  115. print ("y:"..y)
  116. print ("z:"..z)
  117. term.setCursorPos (21,1)
  118. print ("Na poziciu: ")
  119. term.setCursorPos (21,2)
  120. print ("x:"..gox)
  121. term.setCursorPos (21,3)
  122. print ("y:"..goy)
  123. term.setCursorPos (21,4)
  124. print ("z:"..goz)
  125. end
  126.  
  127. function goto(gox, goy, goz, x, y, z)
  128.  
  129. --Preberanie premenných:
  130. testgox = tonumber(gox)
  131. testgoz = tonumber(goz)
  132.  
  133. testx = tonumber(x)
  134. testz = tonumber(z)
  135.  
  136. tx = x
  137. tgox = gox
  138. tz = z
  139. tgoz = goz
  140.  
  141. --Zápis zostatku do cieľa:
  142. if goy > y then zostatoky = goy - y else zostatoky = y - goy end
  143. if tgox > tx then zostatokx = tgox - tx else zostatokx = tx - tgox end
  144. if tgoz > tz then zostatokz = tgoz - tz else zostatokz = tz - tgoz end
  145.  
  146. math.abs(zostatokx)
  147. math.abs(zostatokz)
  148.  
  149. xp = 1
  150. xm = 2
  151. zp = 3
  152. zm = 4
  153. curOrientation = xp
  154.  
  155. --Funkcie ktoré potrebuje na správne orientovanie:
  156. function go()
  157. if turtle.forward() == false then
  158. up()
  159. return false
  160. else
  161. if turtle.detectDown() == false then down() end
  162. if curOrientation == xp then x = x+1
  163. elseif curOrientation == xm then x = x-1
  164. elseif curOrientation == zp then z = z+1
  165. elseif curOrientation == zm then z = z-1 end
  166. return true
  167. end
  168. end
  169. function left()
  170. turtle.turnLeft()
  171. if curOrientation == xp then curOrientation = zm
  172. elseif curOrientation == xm then curOrientation = zp
  173. elseif curOrientation == zp then curOrientation = xp
  174. elseif curOrientation == zm then curOrientation = xm end
  175. end
  176. function right()
  177. turtle.turnRight()
  178. if curOrientation == xp then curOrientation = zp
  179. elseif curOrientation == xm then curOrientation = zm
  180. elseif curOrientation == zp then curOrientation = xm
  181. elseif curOrientation == zm then curOrientation = xp end
  182. end
  183. function back()
  184. if turtle.forward() == false then
  185. turtle.dig()
  186. return false
  187. else
  188. if curOrientation == xp then x = x-1
  189. elseif curOrientation == xm then x = x+1
  190. elseif curOrientation == zp then x = z-1
  191. elseif curOrientation == zm then x = z+1 end
  192. end
  193. end
  194. function up()
  195. if turtle.up() == true then
  196. y = y+1
  197. return true
  198. else
  199. turtle.digUp()
  200. return false
  201. end
  202. end
  203. function down()
  204. if turtle.down() == true then
  205. y = y-1
  206. return true
  207. else
  208. turtle.digDown()
  209. return false
  210. end
  211. end
  212.  
  213. --Funkcia ktora otočí turtlu na správnu pozíciu:
  214. function Smer()
  215. xsmer = false
  216. zsmer = false
  217.  
  218. if testgox > testx then xsmer = true end
  219. if testgoz > testz then zsmer = true end
  220.  
  221. if xsmer == false and zsmer == false then
  222. left()
  223. left()
  224. elseif xsmer == false and zsmer == true then
  225. right()
  226. elseif xsmer == true and zsmer == false then
  227. left()
  228. elseif xsmer == true and zsmer == true then
  229. end
  230. end
  231.  
  232. --Zistenie či je väčšia hodnota y a goy
  233. function ud()
  234. if goy > y then
  235. return true
  236. else
  237. return false
  238. end
  239. end
  240.  
  241. --Zapisovanie dát kde aktualne je a koľko má paliva:
  242. function current(x, y, z)
  243. term.clearLine(1,7)
  244. term.clearLine(1,8)
  245. term.clearLine(1,9)
  246. term.setCursorPos(1,6)
  247. print "Aktualne som na:"
  248. print ("x: "..x)
  249. print ("y: "..y)
  250. print ("z: "..z)
  251. term.setCursorPos(21,6)
  252. print ("Do ciela zostava:")
  253. term.setCursorPos(21,7)
  254. totalfuel = zostatokz + zostatokx + zostatoky
  255. print (totalfuel.." blocks")
  256. term.setCursorPos(21,8)
  257. print ("Palivo turtli:")
  258. term.setCursorPos(21,9)
  259. print (turtle.getFuelLevel())
  260. end
  261.  
  262. --Základ turtly tzv. jadro:
  263. Smer()
  264. while true do
  265. if curOrientation == xp or curOrientation == xm then
  266. while zostatokx > 0 do
  267. if go() == true then zostatokx = zostatokx - 1 else end
  268. current(x, y, z)
  269. end
  270. right()
  271. while zostatokz > 0 do
  272. if go() == true then zostatokz = zostatokz - 1 else end
  273. current(x, y, z)
  274. end
  275. else
  276. while zostatokz > 0 do
  277. if go() == true then zostatokz = zostatokz - 1 else end
  278. current(x, y, z)
  279. end
  280. right()
  281. while zostatokx > 0 do
  282. if go() == true then zostatokx = zostatokx - 1 else end
  283. current(x, y, z)
  284. end
  285. end
  286. if goy > y then zostatoky = goy - y else zostatoky = y - goy end
  287. if ud() == true then
  288. while zostatoky > 0 do
  289. zostatoky = zostatoky - 1
  290. up()
  291. current(x, y, z)
  292. end
  293. else
  294. while zostatoky > 0 do
  295. zostatoky = zostatoky - 1
  296. down()
  297. current(x, y, z)
  298. end
  299. break
  300. end
  301. end
  302.  
  303. --Na konci sa turtla otočí na stranu XP:
  304. while curOrientation ~= xp do
  305. right()
  306. current(x, y, z)
  307. end
  308.  
  309. --Koniec kódu - Turtla zisťuje či dorazila na správne miesto:
  310. term.clear()
  311. if zostatokx == 0 and zostatokz == 0 and zostatoky == 0 then
  312. term.setCursorPos(1,1)
  313. print("Turtla uspesne dorazila na miesto so spravou:")
  314. print(final)
  315. else
  316. term.setCursorPos(1,1)
  317. print("Nastala necakana chyba!")
  318. print("Turtla nedorazila spravne do ciela!")
  319. end
  320. end
  321.  
  322. --Spustenie celej funkcie:
  323. goto(gox, goy, goz, x, y, z)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement