Advertisement
kyle1320

Untitled

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