Advertisement
kyle1320

Move API

Mar 29th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. os.loadAPI("file")
  2.  
  3. fuelSlot = 0
  4. defaultSlot = 0
  5. minFuel = 0
  6. x = 0
  7. y = 0
  8. z = 0
  9. dirx = 0
  10. dirz = 0
  11.  
  12. function register(slot, default, fuel, facex, facez)
  13. if not start() then
  14. fuelSlot = slot
  15. defaultSlot = default
  16. minFuel = fuel
  17. dirx = facex
  18. dirz = facez
  19. end
  20. end
  21.  
  22. function start()
  23. vars = file.read("movement")
  24.  
  25. if vars == nil then
  26. return false
  27. end
  28.  
  29. fuelSlot = vars["fuelSlot"]
  30. defaultSlot = vars["defaultSlot"]
  31. minFuel = vars["minFuel"]
  32. x = vars["x"]
  33. y = vars["y"]
  34. z = vars["z"]
  35. dirx = vars["dirx"]
  36. dirz = vars["dirz"]
  37.  
  38. return true
  39. end
  40.  
  41. function write()
  42. file.write("movement", {["fuelSlot"]=fuelSlot,
  43. ["defaultSlot"]=defaultSlot,
  44. ["minFuel"]=minFuel,
  45. ["x"]=x,
  46. ["y"]=y,
  47. ["z"]=z,
  48. ["dirx"]=dirx,
  49. ["dirz"]=dirz})
  50. end
  51.  
  52. function move_(direction)
  53. if direction=="forward" then
  54. x = x + dirx
  55. z = z + dirz
  56. elseif direction=="back" then
  57. x = x - dirx
  58. z = z - dirz
  59. elseif direction=="up" then
  60. y = y + 1
  61. elseif direction=="down" then
  62. y = y - 1
  63. else
  64. failed("MOVEDIR")
  65. end
  66. write()
  67. end
  68.  
  69. function turn(direction)
  70. if direction=="right" then
  71. if dirx == 0 then
  72. dirx = dirz
  73. dirz = 0
  74. else
  75. dirz = dirx*-1
  76. dirx = 0
  77. end
  78. elseif direction=="left" then
  79. if dirx == 0 then
  80. dirx = dirz*-1
  81. dirz = 0
  82. else
  83. dirz = dirx
  84. dirx = 0
  85. end
  86. else
  87. failed("TURNDIR")
  88. end
  89. write()
  90. end
  91.  
  92. function refuel()
  93. if turtle.getFuelLevel() < minFuel then
  94. turtle.select(fuelSlot)
  95. if not turtle.refuel(1) then
  96. failed("FUEL")
  97. end
  98. turtle.select(defaultSlot)
  99. end
  100. end
  101.  
  102. function failed(message)
  103. print("Failed with error : " .. message)
  104. write()
  105. shell.exit()
  106. end
  107.  
  108. function dig()
  109. if (not turtle.dig()) and turtle.detect() then
  110. failed("DIG")
  111. end
  112. end
  113.  
  114. function digUp()
  115. if not (not turtle.digUp()) and turtle.detectUp() then
  116. failed("DIGUP")
  117. end
  118. end
  119.  
  120. function digDown()
  121. if (not turtle.digDown()) and turtle.detectDown() then
  122. failed("DIGDN")
  123. end
  124. end
  125.  
  126. function rt()
  127. turtle.turnRight()
  128. turn("right")
  129. end
  130.  
  131. function lt()
  132. turtle.turnLeft()
  133. turn("left")
  134. end
  135.  
  136. function fwd()
  137. refuel()
  138. while not turtle.forward() do
  139. dig()
  140. end
  141. move_("forward")
  142. end
  143.  
  144. function back()
  145. refuel()
  146. while not turtle.back() do
  147. rt()
  148. rt()
  149. dig()
  150. rt()
  151. rt()
  152. end
  153. move_("back")
  154. end
  155.  
  156. function up()
  157. refuel()
  158. while not turtle.up() do
  159. digUp()
  160. end
  161. move_("up")
  162. end
  163.  
  164. function down()
  165. refuel()
  166. while not turtle.down() do
  167. digDown()
  168. end
  169. move_("down")
  170. end
  171.  
  172. function seekdir(seek, dir)
  173. if dir=="x" then
  174. if dirx ~= seek then
  175. if dirz == seek then
  176. rt()
  177. elseif dirx ~= seek*-1 then
  178. lt()
  179. else
  180. lt()
  181. lt()
  182. end
  183. end
  184. elseif dir=="z" then
  185. if dirz ~= seek then
  186. if dirx == seek then
  187. lt()
  188. elseif dirz ~= seek*-1 then
  189. rt()
  190. else
  191. rt()
  192. rt()
  193. end
  194. end
  195. else
  196. failed("SEEKDIR")
  197. end
  198. end
  199.  
  200. function seekcoords(seekx, seeky, seekz, seekdirx, seekdirz)
  201. toxd = seekx - x
  202. tox = toxd / math.abs(toxd)
  203. toxd = math.abs(toxd)
  204.  
  205. toyd = seeky - y
  206. toy = toyd / math.abs(toyd)
  207. toyd = math.abs(toyd)
  208.  
  209. tozd = seekz - z
  210. toz = tozd / math.abs(tozd)
  211. tozd = math.abs(tozd)
  212.  
  213. seekdir(tox, "x")
  214.  
  215. for i=1,toxd do
  216. fwd()
  217. end
  218.  
  219. seekdir(toz, "z")
  220.  
  221. for i=1,tozd do
  222. fwd()
  223. end
  224.  
  225. seekdir(seekdirx, "x")
  226. seekdir(seekdirz, "z")
  227.  
  228. for i=1,toyd do
  229. if toy > 0 then
  230. up()
  231. elseif toy < 0 then
  232. down()
  233. end
  234. end
  235. end
  236.  
  237. function reset(resetx, resety, resetz)
  238. if resetx then
  239. x = 0
  240. end
  241. if resety then
  242. y = 0
  243. end
  244. if resetz then
  245. z = 0
  246. end
  247. end
  248.  
  249. function getx()
  250. return x
  251. end
  252.  
  253. function gety()
  254. return y
  255. end
  256.  
  257. function getz()
  258. return z
  259. end
  260.  
  261. function getfacex()
  262. return dirx
  263. end
  264.  
  265. function getfacez()
  266. return dirz
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement