Advertisement
shirkit

aware

May 15th, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. os.loadAPI("utils")
  2.  
  3. local shaftmode=false
  4. local positions = {}
  5. local current = {0,0,0,0}
  6.  
  7. local function modposition(axis, value)
  8.   if (axis == 4) then
  9.     current[axis] = ((current[axis] + value) % 4)
  10.   else
  11.     current[axis] = (current[axis] + value)
  12.   end
  13.   setposition("current", current)
  14.   --reportpositions()
  15. end
  16.  
  17. function setshaftmode(toggle)
  18.   shaftmode=toggle
  19. end
  20.  
  21. function moveforward()
  22.   if turtle.forward() then
  23.     if     (current[4] == 0) then modposition(2,1)
  24.     elseif (current[4] == 1) then modposition(1,-1)
  25.     elseif (current[4] == 2) then modposition(2,-1)
  26.     elseif (current[4] == 3) then modposition(1,1)
  27.     end
  28.     return true
  29.   else
  30.     return false
  31.   end
  32. end
  33.  
  34. function moveback()
  35.   if turtle.back() then
  36.     if     (current[4] == 0) then modposition(2,-1)
  37.     elseif (current[4] == 1) then modposition(1,1)
  38.     elseif (current[4] == 2) then modposition(2,1)
  39.     elseif (current[4] == 3) then modposition(1,-1)
  40.     end
  41.     return true
  42.   else
  43.     return false
  44.   end
  45. end
  46.  
  47. function moveup()
  48.   if turtle.up() then
  49.     modposition(3,1)
  50.     return true
  51.   else
  52.     return false
  53.   end
  54. end
  55.  
  56. function movedown()
  57.   if turtle.down() then
  58.     modposition(3,-1)
  59.     return true
  60.   else
  61.     return false
  62.   end
  63. end
  64.  
  65. function turnright()
  66.   modposition(4,1)
  67.   turtle.turnRight()
  68. end
  69.  
  70. function turnleft()
  71.   modposition(4,-1)
  72.   turtle.turnLeft()
  73. end
  74.  
  75. function turnto(direction)
  76.   if (direction == nil) then
  77.    return
  78.   end
  79.   while current[4] ~= direction do
  80.     diff = current[4] - direction
  81.     if ((diff == 1) or (diff == -3)) then
  82.       turnleft()
  83.     else
  84.       turnright()
  85.     end
  86.   end
  87. end
  88.  
  89. function digmoveforward()
  90.   if turtle.detect() then
  91.     turtle.dig()
  92.   end
  93.   if shaftmode then
  94.     if turtle.detectUp() then
  95.       turtle.digUp()
  96.     end
  97.   end
  98.   if moveforward() then
  99.     return true
  100.   else
  101.     return false
  102.   end
  103. end
  104.  
  105. function digmoveup()
  106.   if turtle.detectUp() then
  107.     turtle.digUp()
  108.   end
  109.   if shaftmode then
  110.     if turtle.detect() then
  111.       turtle.dig()
  112.     end
  113.   end
  114.   if moveup() then
  115.     return true
  116.   else
  117.     return false
  118.   end
  119. end
  120.  
  121. function digmovedown()
  122.   if turtle.detectDown() then
  123.     turtle.digDown()
  124.   end
  125.   if shaftmode then
  126.     if turtle.detect() then
  127.       turtle.dig()
  128.     end
  129.   end
  130.   if movedown() then
  131.     return true
  132.   else
  133.     return false
  134.   end
  135. end
  136.  
  137. function movetoz(tarz)
  138.   if (tarz == nil) then
  139.    return true
  140.   end
  141.   while(current[3] ~= tarz) do
  142.     if current[3] < tarz then
  143.       digmoveup()
  144.     else
  145.       digmovedown()
  146.     end
  147.   end
  148. end
  149.  
  150. function movetox(tarx)
  151.   while(current[1] ~= tarx) do
  152.     if current[1] < tarx then
  153.       turnto(3)
  154.     else
  155.       turnto(1)
  156.     end
  157.     digmoveforward()
  158.   end
  159. end
  160.  
  161. function movetoy(tary)
  162.   while(current[2] ~= tary) do
  163.     if current[2] < tary then
  164.       turnto(0)
  165.     else
  166.       turnto(2)
  167.     end
  168.     digmoveforward()
  169.   end
  170. end
  171.  
  172. function moveto(targx, targy, targz, targo)
  173.   if(targz == nil) then
  174.     movetox(targx)
  175.     movetoy(targy)
  176.   elseif(current[3] > targz) then
  177.     movetoz(targz)
  178.     movetoy(targy)
  179.     movetox(targx)
  180.   else
  181.     movetox(targx)
  182.     movetoy(targy)
  183.     movetoz(targz)
  184.   end
  185.   turnto(targo)
  186. end
  187.  
  188. function goto(destination)
  189.   utils.termwrite("going to"..textutils.serialize(destination))
  190.   moveto(destination[1], destination[2], destination[3], destination[4])
  191. end
  192.  
  193. function emptyslots()
  194.   numslots = 0
  195.   for i=1, 9 do
  196.     if turtle.getItemCount(i) == 0 then
  197.       numslots = numslots + 1
  198.     end
  199.   end
  200.   return numslots
  201. end
  202.  
  203. function dump()
  204.   while (turtle.drop()) do
  205.     sleep(.12)
  206.   end
  207. end
  208.  
  209. local function reportpositions()
  210.   pushpos((os.getComputerID()+10000),"T",current)
  211.   pushpos((os.getComputerID()+100),"H",getposition("home"))
  212. end
  213.  
  214. function pushpos(id, class, position)
  215.   fposition=textutils.serialize(position)
  216.   formatted={id,class,fposition}
  217.   rednet.broadcast(textutils.serialize(formatted))
  218. end
  219.  
  220. function getaxis(axis)
  221.   return current[axis]
  222. end
  223.  
  224. function getposition(name)
  225.   position = utils.varfromfile("aware."..name)
  226.   if ( position == nil) then
  227.     return {0, 0, 0, 0}  
  228.   else
  229.     return position
  230.   end
  231. end
  232.  
  233. function setposition(name, coordinates)
  234.   if (coordinates ~= nil) then
  235.     position = coordinates
  236.   else
  237.     position = aware.getposition("current")
  238.   end
  239.   utils.vartofile(position, "aware."..name)
  240. end
  241.  
  242. local function init()
  243.   --rednet.open("right")
  244.   current = getposition("current")
  245. end
  246.  
  247. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement