Advertisement
pedrosgali

Turtle move 2.0

Feb 19th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. North = 0
  2. East = 1
  3. South = 2
  4. West = 3
  5. minFuel = 80
  6. minSafeZone = 99
  7. minEmptySlot = 1
  8. lastReport = ""
  9.  
  10. data = {
  11.   pos = {x = 15, y = 64, z = 51, h = 0},
  12.   target = {x = 0, y = 0, z = 0, h = 0},
  13.   home = {x = 15, y = 64, z = 51, h = 0},
  14.   slot = {
  15.     current = 1,
  16.     fuel = 1,
  17.     last = 1,
  18.     },
  19.   locs = {},
  20.   safeZone = minSafeZone + os.getComputerID()
  21.   }
  22.  
  23. --UTILS--
  24.  
  25. function report(msg, clear)
  26.   if clear then
  27.     term.clear()
  28.     term.setCursorPos(1, 1)
  29.   end
  30.   if msg ~= lastReport then
  31.     print(msg)
  32.   end
  33.   lastReport = msg
  34.   local x, y = term.getCursorPos()
  35.   if y > 10 then
  36.     y = 1
  37.     term.clear()
  38.   end
  39.   term.setCursorPos(1, 12)
  40.   print("X: "..data.pos.x.." Y: "..data.pos.y.." Z: "..data.pos.z.." H: "..data.pos.h)
  41.   term.setCursorPos(1, y)
  42. end
  43.  
  44. function selectSlot(s)
  45.   turtle.select(s)
  46.   local info = turtle.getItemDetail()
  47.   local count = turtle.getItemCount()
  48.   data.slot.last = data.slot.current
  49.   data.slot.current = s
  50.   if info then
  51.     report("Item Data:")
  52.     report("Item Name: "..info.name)
  53.     report("Amount   : "..count)
  54.   end
  55. end
  56.  
  57. function selectLast()
  58.   selectSlot(data.slot.last)
  59. end
  60.  
  61. function fillSlot(id, count)
  62.   count = count or 64
  63.   selectSlot(id)
  64.   repeat
  65.     turtle.suck(1)
  66.   until turtle.getItemCount(id) == count
  67. end
  68.  
  69. function checkFuel()
  70.   local fuelCount = turtle.getFuelLevel()
  71.   if fuelCount < minFuel then
  72.     report("Refuelling...", true)
  73.     selectSlot(data.slot.fuel)
  74.     while fuelCount < minFuel do
  75.       fuelCount = turtle.getFuelLevel()
  76.       turtle.refuel(1)
  77.       report("Fuel level: "..fuelCount..".")
  78.     end
  79.   end
  80. end
  81.  
  82. function emptyAll()
  83.   for i = minEmptySlot, 16 do
  84.     if i ~= data.slot.fuel then
  85.       turtle.select(i)
  86.       turtle.drop()
  87.     end
  88.   end
  89. end
  90.  
  91. --MOVEMENT--
  92.  
  93. function fd(dist, dig)
  94.   dist = dist or 1
  95.   while dist > 0 do
  96.     if dig then
  97.       turtle.dig()
  98.     end
  99.     checkFuel()
  100.     if turtle.forward() then
  101.       dist = dist - 1
  102.       if data.pos.h == North then
  103.         data.pos.z = data.pos.z - 1
  104.       elseif data.pos.h == East then
  105.         data.pos.x = data.pos.x + 1
  106.       elseif data.pos.h == South then
  107.         data.pos.z = data.pos.z + 1
  108.       else
  109.         data.pos.x = data.pos.x - 1
  110.       end
  111.       report("Moving forward.")
  112.     else
  113.       report("I'm stuck!")
  114.     end
  115.   end
  116. end
  117.  
  118. function bk(dist)
  119.   dist = dist or 1
  120.   while dist > 0 do
  121.     checkFuel()
  122.     if turtle.back() then
  123.       dist = dist - 1
  124.       if data.pos.h == North then
  125.         data.pos.z = data.pos.z + 1
  126.       elseif data.pos.h == East then
  127.         data.pos.x = data.pos.x - 1
  128.       elseif data.pos.h == South then
  129.         data.pos.z = data.pos.z - 1
  130.       else
  131.         data.pos.x = data.pos.x + 1
  132.       end
  133.       report("Moving backward.")
  134.     else
  135.       report("I'm stuck!")
  136.     end
  137.   end
  138. end
  139.  
  140. function up(dist, dig)
  141.   dist = dist or 1
  142.   while dist > 0 do
  143.     if dig then
  144.       turtle.digUp()
  145.     end
  146.     checkFuel()
  147.     if turtle.up() then
  148.       dist = dist - 1
  149.       data.pos.y = data.pos.y + 1
  150.       report("Moving up.")
  151.     else
  152.       report("I'm stuck!")
  153.     end
  154.   end
  155. end
  156.  
  157. function dn(dist, dig)
  158.   dist = dist or 1
  159.   while dist > 0 do
  160.     if dig then
  161.       turtle.digDown()
  162.     end
  163.     checkFuel()
  164.     if turtle.down() then
  165.       dist = dist - 1
  166.       data.pos.y = data.pos.y - 1
  167.       report("Moving down.")
  168.     else
  169.       report("I'm stuck!")
  170.     end
  171.   end
  172. end
  173.  
  174. function rt(int)
  175.   for i = 1, int do
  176.     turtle.turnRight()
  177.     data.pos.h = (data.pos.h + 1) % 4
  178.   end
  179. end
  180.  
  181. function lt(int)
  182.   for i = 1, int do
  183.     turtle.turnLeft()
  184.     data.pos.h = (data.pos.h - 1) % 4
  185.   end
  186. end
  187.  
  188. function face(dir)
  189.   while data.pos.h ~= dir do
  190.     turtle.turnRight()
  191.     data.pos.h = (data.pos.h + 1) % 4
  192.     report("Heading: "..data.pos.h)
  193.   end
  194. end
  195.  
  196. function findX(t, dig)
  197.   if t < data.pos.x then
  198.     face(West)
  199.   elseif t > data.pos.x then
  200.     face(East)
  201.   end
  202.   while t ~= data.pos.x do
  203.     fd(1, dig)
  204.   end
  205. end
  206.  
  207. function findZ(t, dig)
  208.   if t < data.pos.z then
  209.     face(North)
  210.   elseif t > data.pos.z then
  211.     face(South)
  212.   end
  213.   while t ~= data.pos.z do
  214.     fd(1, dig)
  215.   end
  216. end
  217.  
  218. function findY(t, dig)
  219.   if t > data.pos.y then
  220.     while t ~= data.pos.y do
  221.       up(1, dig)
  222.     end
  223.   elseif t < data.pos.y then
  224.     while t ~= data.pos.y do
  225.       dn(1, dig)
  226.     end
  227.   end
  228. end
  229.  
  230. function to(x, y, z, h)
  231.   findY(data.safeZone)
  232.   findX(x)
  233.   findZ(z)
  234.   findY(y, true)
  235.   face(h)
  236. end
  237.  
  238. function toNoSafety(x, y, z, h)
  239.   findX(x, true)
  240.   findZ(z, true)
  241.   findY(y, true)
  242.   face(h)
  243. end
  244.  
  245. function from(x, y, z, h)
  246.   findY(data.safeZone)
  247.   findZ(z)
  248.   findX(x)
  249.   findY(y, true)
  250.   face(h)
  251. end
  252.  
  253. function setHome(x, y, z, h)
  254.   data.home = {
  255.     x = x,
  256.     y = y,
  257.     z = z,
  258.     h = h,
  259.     }
  260. end
  261.  
  262. function setReturnPoint()
  263.   data.target = {
  264.   x = data.pos.x,
  265.   y = data.pos.y,
  266.   z = data.pos.z,
  267.   h = data.pos.h,
  268.   }
  269. end
  270.  
  271. function getTarget()
  272.   return data.target.x, data.target.y, data.target.z, data.target.h
  273. end
  274.  
  275. function unload(ret)
  276.   if ret then
  277.     setReturnPoint()
  278.   end
  279.   from(data.home.x, data.home.y, data.home.z, data.home.h)
  280.   rt(1)
  281.   emptyAll()
  282.   rt(2)
  283.   fillSlot(data.slot.fuel)
  284.   rt(1)
  285.   if ret then
  286.     to(data.target.x, data.target.y, data.target.z, data.target.h)
  287.   end
  288. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement