Guest User

Builder Turtle Routines - Global Builders Project

a guest
Sep 20th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.08 KB | None | 0 0
  1. -- Construction Turtle (Builder)
  2.  
  3. local mtx, mty, mtz = -205, 72, 113
  4. local mtd = 1
  5. local mRange = 0
  6. -- mtx, mty, mtz are coordinates of
  7. -- resupply dock, and can also be used
  8. -- to assign absolute coordinates to
  9. -- turtle if no GPS system is available.
  10.  
  11. -- mtd is the direction the turtle
  12. -- should be facing on approach to the
  13. -- resupply dock, and can be used to
  14. -- initialize the turtle direction if
  15. -- no GPS system is available.
  16.  
  17. -- mRange stores turtles theoretical
  18. -- maximum modem range assuming
  19. -- clear weather
  20.  
  21. local tx, ty, tz, td = 0, 0, 0, 0
  22. -- tx, ty, tz are absolute world coords
  23. -- td represents direction turtle is facing
  24.  
  25. local bty, btx, btz, btd = 0, 1, 1, 1
  26. -- variables for turtle location within
  27. -- current construction chunk
  28.  
  29. local gpsLinked = false
  30.  
  31. local commLog = {}
  32. -- commLog table stores information from
  33. -- incoming messages to check for duplicates
  34.  
  35. local m, d = nil, nil
  36. -- handles for peripherals
  37.  
  38. local destructiveTurtle = false
  39. -- toggle to define whether or not turtle is
  40. -- allowed to break blocks
  41.  
  42. local roomForFuel = true
  43. -- toggle to determine whether or not turtle
  44. -- will reserve slot 16 for on-board fuel reserve
  45.  
  46. local cInvSlot = 1
  47. -- variable indexing current turtle inventory
  48. -- slot
  49. local maxMats = 1
  50. -- variable indicating how many different material
  51. -- types are specified in coordinate block
  52.  
  53. local cInvCatalog = {}
  54. for i = 1, 16, 1 do
  55.     cInvCatalog[i] = {0, 0}
  56. end
  57. -- table to contain index values for all
  58. -- turtle inventory slots using format:
  59. -- {itemtype, quantity}
  60. local maxFuel = 8000
  61. -- variable for upper limit on turtle fuel
  62. -- refuel function will not consume more fuel
  63. -- if fuel level is above maxFuel
  64.  
  65. local tLabel, tID = os.getComputerLabel (), os.getComputerID ()
  66. -- assigned modem channel for turtle
  67.  
  68. local North, East, South, West = 1, 2, 3, 4
  69. -- numeric values for compass directions
  70.  
  71. local workBlock = {}
  72. -- table to contain build coordinates
  73. -- received from central computer
  74.  
  75. function termStatus ()
  76.     term.clear()
  77.     term.setCursorPos(1, 1)
  78.     write (tLabel..' ('..tostring(tID)..')')
  79.     term.setCursorPos(15, 1)
  80.     write ('Fuel: '..turtle.getFuelLevel())
  81.     term.setCursorPos(1, 2)
  82.     write ('GPS: ')
  83.     term.setCursorPos(6, 2)
  84.     if gpsLinked then
  85.         write ('OK')
  86.     else write ('*NO*')
  87.     end
  88.     term.setCursorPos(12, 2)
  89.     write ('Modem: ')
  90.     term.setCursorPos(19, 2)
  91.     if m == nil then
  92.         write('*NO*')
  93.     else write ('OK')
  94.     end
  95.     term.setCursorPos (24, 2)
  96.     write ('Range: '..tostring(mRange)..'m')
  97.     term.setCursorPos (1, 3)
  98.     local tFacingStr = ""
  99.     if td == 1 then
  100.         tFacingStr = "north"
  101.     elseif td == 2 then
  102.         tFacingStr = "east"
  103.     elseif td == 3 then
  104.         tFacingStr = "south"
  105.     elseif td == 4 then
  106.         tFacingStr = "west"
  107.     end
  108.     write ('Y= '..tostring(ty)..'  X= '..tostring(tx)..'  Z= '..tostring(tz)..'  Facing: '..tFacingStr)
  109.     term.setCursorPos(1, 5)
  110.     write ('Inventory:')
  111.     for i = 1, 4, 1 do
  112.         local ix = 1 + ((i-1) * 10)
  113.         term.setCursorPos (ix, 7)
  114.         term.write (i..': ')
  115.         term.setCursorPos ((ix + 4), 7)
  116.         term.write (cInvCatalog[i][1]..':'..cInvCatalog[i][2])
  117.         term.setCursorPos (ix, 8)
  118.         term.write ((i + 4)..': ')
  119.         term.setCursorPos ((ix + 4), 8)
  120.         term.write (cInvCatalog[i+4][1]..':'..cInvCatalog[i+4][2])
  121.         term.setCursorPos (ix, 9)
  122.         term.write ((i + 8)..': ')
  123.         term.setCursorPos ((ix + 4), 9)
  124.         term.write (cInvCatalog[i+8][1]..':'..cInvCatalog[i+8][2])
  125.         term.setCursorPos (ix, 10)
  126.         term.write ((i + 12)..': ')
  127.         term.setCursorPos ((ix + 4), 10)
  128.         term.write (cInvCatalog[i+12][1]..':'..cInvCatalog[i+12][2])
  129.     end
  130.     term.setCursorPos (1, 12)
  131. end
  132.  
  133.  
  134. function refuel ()
  135.     turtle.select (16)
  136.     while (turtle.getItemCount(16) > 0) and
  137.     (maxFuel > turtle.getFuelLevel()) do
  138.         turtle.refuel(1)
  139.     end
  140. end
  141.  
  142.  
  143. function turnLeft ()
  144.     if turtle.turnLeft () then
  145.         if (td - 1) == 0 then
  146.             td = 4
  147.         else td = (td - 1)
  148.         end
  149.     else
  150.         print ("ERROR attempting to turn left!")
  151.     end
  152. end
  153.  
  154.  
  155. function turnRight ()
  156.     if turtle.turnRight () then
  157.         if (td + 1) == 5 then
  158.             td = 1
  159.         else td = (td + 1)
  160.         end
  161.     else
  162.         print ("ERROR attempting to turn right!")
  163.     end
  164. end
  165.  
  166.  
  167. function moveUp ()
  168.     if turtle.up () then
  169.         ty = ty + 1
  170.     else
  171.         print ("ERROR attempting to move up!")
  172.     end
  173. end
  174.  
  175.  
  176. function moveDown ()
  177.     if turtle.down () then
  178.         ty = ty - 1
  179.     else
  180.         print ("ERROR attempting to move down!")
  181.     end
  182. end
  183.  
  184.  
  185. function moveForward ()
  186.     if turtle.forward () then
  187.         if td == 1 then tz = tz + 1
  188.         elseif td == 2 then tx = tx + 1
  189.         elseif td == 3 then tz = tz - 1
  190.         elseif td == 4 then tx = tx - 1
  191.         end
  192.     else
  193.         print ("ERROR attempting to move forward!")
  194.     end
  195. end
  196.  
  197.  
  198. function moveBack ()
  199.     if turtle.back () then
  200.         if td == 1 then tz = tz - 1
  201.         elseif td == 2 then tx = tx - 1
  202.         elseif td == 3 then tz = tz + 1
  203.         elseif td == 4 then tx = tx + 1
  204.         end
  205.     else
  206.         print ("ERROR attempting to move back!")
  207.     end
  208. end
  209.  
  210.  
  211. function manageInv (itemType)
  212.     -- manages turtle's internal inventory, ensuring
  213.     -- 'sample' block remains in slot 1 for
  214.     -- comparison during resupply.  Function returns
  215.     -- false if inventory is empty.
  216.  
  217.     local maxSlots = 0
  218.  
  219.     if destructiveTurtle then maxSlots = 12
  220.     elseif roomForFuel then maxSlots = 15
  221.     else maxSlots = 16
  222.     end
  223.  
  224.  
  225.     for i = 1, maxSlots, 1 do
  226.         if (cInvCatalog[i][1] == itemType) and
  227.         (turtle.getItemCount (i) > 1) then
  228.             turtle.select(i)
  229.             return true
  230.         end
  231.     end
  232.     return false
  233. end
  234.  
  235.  
  236. function placeDown (blockTab)
  237.     -- places block below turtle
  238.     -- returns 0 if successful, 1 if no inventory,
  239.     -- 2 if other error
  240.  
  241.     if manageInv(blockTab[1]) then
  242.         local ok = turtle.placeDown()
  243.         if ok then return 0
  244.         else return 2
  245.         end
  246.     else return 1
  247.     end
  248. end
  249.  
  250.  
  251. function placeForward (blockTab)
  252.     -- places block in front of turtle
  253.     -- should be used for directional placements only
  254.     -- error codes as with placeDown
  255.  
  256.     if manageInv(blockTab[1]) then
  257.         local ok = turtle.place()
  258.         if ok then return 0
  259.         else return 2
  260.         end
  261.     else return 1
  262.     end
  263. end
  264.  
  265.  
  266. function faceAbs (ftd)
  267.     -- function to change turtle direction based
  268.     -- on absolute coordinates
  269.  
  270.     if ftd > td then
  271.         for i = 1, (ftd - td), 1 do
  272.             turnRight ()
  273.         end
  274.     elseif ftd < td then
  275.         for i = 1, (td - ftd), 1 do
  276.             turnLeft ()
  277.         end
  278.     end
  279. end
  280.  
  281.  
  282. function faceDir (curDir, faceTo)
  283.     -- function to change turtle direction based
  284.     -- on relative coordinates
  285.  
  286.     if (curDir == 4) and (faceTo == 1) then
  287.         turnRight()
  288.     elseif (curDir == 1) and (faceTo == 4) then
  289.         turnLeft()
  290.     elseif (faceTo == 0) then
  291.         return false
  292.     elseif (curDir > faceTo) then
  293.         for i = 1, (curDir - faceTo), 1 do
  294.             turnLeft()
  295.         end
  296.     elseif (faceTo > curDir) then
  297.         for i = 1, (faceTo - curDir), 1 do
  298.             turnRight()
  299.         end
  300.     end
  301. end
  302.  
  303.  
  304. function moveTo (yDif, xDif, zDif, cDir)
  305.    
  306.     if xDif ~= 0 then
  307.         if xDif > 0 then
  308.             faceDir (cDir, 2)
  309.             cDir = 2
  310.             for i = 1, xDif, 1 do
  311.                 moveForward()
  312.             end
  313.         elseif xDif < 0 then
  314.             faceDir (cDir, 4)
  315.             cDir = 4
  316.             for i = 1, (xDif * -1) do
  317.                 moveForward()
  318.             end
  319.         end
  320.     end
  321.     if zDif ~= 0 then
  322.         if zDif > 0 then
  323.             faceDir (cDir, 3)
  324.             cDir = 1
  325.             for i = 1, zDif, 1 do
  326.                 moveForward()
  327.             end
  328.         elseif zDif < 0 then
  329.             faceDir (cDir, 1)
  330.             cDir = 3
  331.             for i = 1, (zDif * -1) do
  332.                 moveForward()
  333.             end
  334.         end
  335.     end
  336.     if yDif ~= 0 then
  337.         if yDif > 0 then
  338.             for i = 1, yDif, 1 do
  339.                 moveUp()
  340.             end
  341.         elseif yDif < 0 then
  342.             for i = 1, (yDif * -1), 1 do
  343.                 moveDown()
  344.             end
  345.         end
  346.     end
  347.     return cDir
  348. end
  349.  
  350.  
  351. function initInvCatalog ()
  352.     -- examines turtle inventory and sets item
  353.     -- types and quantities in table
  354.  
  355.     local matched = false
  356.     local maxSlots, tItemType = 0, 1
  357.  
  358.     if destructiveTurtle then maxSlots = 12
  359.     elseif roomForFuel then maxSlots = 15
  360.     else maxSlots = 16
  361.     end
  362.  
  363.     for i = 1, 16, 1 do
  364.         cInvCatalog[i][1] = 0
  365.     end
  366.  
  367.     for i = 1, maxSlots, 1 do
  368.         cInvCatalog[i][2] = turtle.getItemCount (i)
  369.         if (cInvCatalog[i][1] == 0)  and
  370.         (not (cInvCatalog[i][2] == 0)) then
  371.             turtle.select (i)
  372.             for ii = 1, maxSlots, 1 do
  373.                 if not (i == ii) then
  374.                     if turtle.compareTo(ii) and not
  375.                     (cInvCatalog[ii][1] == 0) then
  376.                         cInvCatalog[i][1] = cInvCatalog[ii][1]
  377.                         matched = true
  378.                     end
  379.                 end
  380.             end
  381.             if not matched then
  382.                 cInvCatalog[i][1] = tItemType
  383.                 tItemType = tItemType + 1
  384.             else matched = false
  385.             end
  386.         end
  387.     end
  388. end
  389.  
  390.  
  391. function checkCommLog (msg)
  392.  
  393.     local sType = msg[2]
  394.     if commLog[sType] == nil then
  395.         commLog[sType] = {}
  396.         commLog[sType][1] = { msg[3], msg[4], msg[5], {msg[6], msg[1]} }
  397.         return true
  398.     else
  399.         for i = 1, #commLog[sType], 1 do
  400.             if (commLog[sType][i][1] == msg[3]) and
  401.             (commlog[sType][i][2] == msg[4]) and
  402.             (commLog[sType][i][3] == msg[5]) then
  403.                 local msgs = #commLog[sType][i][4]
  404.                 for ii = 1, msgs, 1 do
  405.                     if commLog[sType][i][4][ii] == {msg[6], msg[1]} then
  406.                         return false
  407.                     end
  408.                 end
  409.                 table.insert (commLog[sType][i][4], {msg[6], msg[1]})
  410.                 if msgs > 5 then
  411.                     table.remove (commLog[sType][i][4], 1)
  412.                 end
  413.                 return true
  414.             else
  415.                 table.insert (commLog[sType], {msg[3], msg[4], msg[5], {msg[6], msg[1]}})
  416.                 return true
  417.             end
  418.         end
  419.     end
  420. end
  421.  
  422.  
  423. function reqMessageID ()
  424.  
  425.     local IDStr = tostring( os.day() )..'-'..tostring( os.time () )
  426.     return IDStr
  427. end
  428.  
  429.  
  430. function listen ()
  431.  
  432.     local event, modemSide, senderChannel,
  433.     replyChannel, message, senderDistance =
  434.     os.pullEvent("modem_message")
  435.     print ('Received message on channel '..tostring(senderChannel))
  436.     local tTab = textutils.unserialize (message)
  437.     if checkCommLog (tTab) then
  438.         if tTab[1] == 1 then
  439.             print ('Device status requested.')
  440.         elseif tTab[1] == 2 then
  441.             -- insert debug prints in place of move orders
  442.             -- confirm accurate math
  443.             print ('Relocation order received.')
  444.             local newCoords = tTab[7]
  445.             local nY = newCoords[1] - ty
  446.             moveTo (nY, 0, 0, td)
  447.             nY = newCoords[2] - ty
  448.             local nX, nZ = 0, 0
  449.             if tx > 0 then
  450.                 if newCoords[3] <= 0 then
  451.                     nX = newCoords[3] + (-1 * tx)
  452.                 else
  453.                     nX = newCoords[3] - tx
  454.                 end
  455.             else
  456.                 if newCoords[3] <= 0 then
  457.                     nX = newCoords[3] + (-1 * tx)
  458.                 else
  459.                     nX = newCoords[3] + (-1 * tx)
  460.                 end
  461.             end
  462.             if tz > 0 then
  463.                 if newCoords[4] <= 0 then
  464.                     nZ = newCoords[4] + (-1 * tz)
  465.                 else
  466.                     nZ = newCoords[4] - tz
  467.                 end
  468.             else
  469.                 if newCoords[4] <= 0 then
  470.                     nZ = newCoords[4] + (-1 * tz)
  471.                 else
  472.                     nZ = newCoords[4] + (-1 * tz)
  473.                 end
  474.             end
  475.             moveTo (nY, nX, nZ, td)
  476.             print ('Yoff: '..tostring(nY)..' Xoff: '..tostring(nX)..' Zoff: '..tostring(nZ))
  477.         elseif tTab[1] == 3 then
  478.             print ('Build order received.')
  479.         end
  480.     else
  481.         print ('Error receiving message:')
  482.         print ('Duplicate indicated.')
  483.     end
  484. end
  485.  
  486. function reqChunk ()
  487.  
  488.     local tTab = {1, 5, tLabel, tID, tID, reqMessageID ()}
  489.     m.transmit (1, tID, textutils.serialize (tTab))
  490.     listen ()
  491. end
  492.  
  493.  
  494. function outOfBounds (wy, wx, wz)
  495.     if (wy < 1) or (wy > 16) then
  496.         return true
  497.     elseif (wx < 1) or (wx > 16) then
  498.         return true
  499.     elseif (wz < 1) or (wz > 16) then
  500.         return true
  501.     else return false
  502.     end
  503. end
  504.  
  505.  
  506. function advNext (cty, ctx, ctz, seekDir)
  507.     -- seekDir indicates direction to move on
  508.     -- horizontal plane
  509.     -- 0 = right to left, 1 = left to right
  510.  
  511.  
  512.     if seekDir == 0 then
  513.         if math.ceil (ctx / 2) > math.floor (ctx / 2) then
  514.             ctz = ctz + 1
  515.             if ctz > 16 then
  516.                 ctz = 16
  517.                 ctx = ctx + 1
  518.             end
  519.         else
  520.             ctz = ctz - 1
  521.             if ctz < 1 then
  522.                 ctz = 1
  523.                 ctx = ctx + 1
  524.                 if ctx > 16 then
  525.                     ctx = 16
  526.                     cty = cty + 1
  527.                 end
  528.             end
  529.         end
  530.     elseif seekDir == 1 then
  531.         if math.ceil (ctx / 2) > math.floor (ctx / 2) then
  532.             ctz = ctz - 1
  533.             if ctz < 1 then
  534.                 ctz = 1
  535.                 ctx = ctx - 1
  536.                 if ctx < 1 then
  537.                     ctx = 1
  538.                     cty = cty + 1
  539.                 end
  540.             end
  541.         else ctz = ctz + 1
  542.             if ctz > 16 then
  543.                 ctz = 16
  544.                 ctx = ctx - 1
  545.             end
  546.         end
  547.     end
  548.     if cty > 16 then return cty, ctx, ctz, {nil, nil}
  549.     else return cty, ctx, ctz, workBlock[cty][ctx][ctz]
  550.     end
  551. end
  552.  
  553.  
  554. function buildChunk ()
  555.     -- builds block defined by workBlock
  556.  
  557.     local wty, wtx, wtz, wtm = 0, 0, 0, 0
  558.  
  559.     if workBlock == nil then
  560.         print ('ERROR: Block table undefined.')
  561.         print ('Unable to build block.')
  562.         return false
  563.     else
  564.         moveUp ()
  565.         bty = 1
  566.         maxMats = 0
  567.         for i = 1, 16, 1 do
  568.             for ii = 1, 16, 1 do
  569.                 for iii = 1, 16, 1 do
  570.                     if workBlock[i][ii][iii][1] > maxMats then
  571.                         maxMats = workBlock[i][ii][iii][1]
  572.                     end
  573.                 end
  574.             end
  575.         end
  576.     end
  577.  
  578.     local nty, ntx, ntz, ntm, pDir = 1, 1, 1, {}, 0
  579.     local wty, wtx, wtz, wtm = 0, 0, 0, {}
  580.  
  581.     for i = 1, 16, 1 do
  582.         for ii = 1, maxMats, 1 do
  583.             if math.ceil (ii / 2) > math.floor (ii / 2) then
  584.                 nty, ntx, ntz = i, 1, 1
  585.                 pDir = 0
  586.             else
  587.                 nty, ntx, ntz = i, 16, 1
  588.                 pDir = 1
  589.             end
  590.             ntm = workBlock[nty][ntx][ntz]
  591.             if (nty == i) and (ntm[1] == ii) and (ntm[2] > 0) then
  592.                 wty, wtx, wtz = nty, ntx, ntz
  593.                 if ntm[2] == 1 then
  594.                     wtz = wtz + 1
  595.                 elseif ntm[2] == 2 then
  596.                     wtx = wtx + 1
  597.                 elseif ntm[2] == 3 then
  598.                     wtz = wtz - 1
  599.                 elseif ntm[2] == 4 then
  600.                     wtx = wtx - 1
  601.                 end
  602.                 if (not (outOfBounds (wty, wtx, wtz))) then
  603.                     if (workBlock[wty][wtx][wtz][1] > 0) then
  604.                         wtm = workBlock[wty][wtx][wtz]
  605.                         btd = moveTo (wty - bty, wtx - btx, wtz - btz, btd)
  606.                         bty = wty
  607.                         btx = wtx
  608.                         btz = wtz
  609.                         if not (wtm[2] == 0) then
  610.                             faceDir (btd, wtm[2])
  611.                             btd = wtm[2]
  612.                         end
  613.                         placeDown(wtm)
  614.                         workBlock[wty][wtx][wtz] = {0, 0}
  615.                         faceDir (btd, ntm[2])
  616.                         btd = ntm[2]
  617.                         moveBack()
  618.                         bty = nty
  619.                         btx = ntx
  620.                         btz = ntz
  621.                         placeDown(ntm)
  622.                         workBlock[nty][ntx][ntz] = {0, 0}
  623.                     end
  624.                 end
  625.             end
  626.             while not (nty > i) do
  627.                 nty, ntx, ntz, ntm = advNext (nty, ntx, ntz, pDir)
  628.                 if (nty == i) and (ntm[1] == ii) and (ntm[2] > 0) then
  629.                     wty, wtx, wtz = nty, ntx, ntz
  630.                     if ntm[2] == 1 then
  631.                         wtz = wtz + 1
  632.                     elseif ntm[2] == 2 then
  633.                         wtx = wtx + 1
  634.                     elseif ntm[2] == 3 then
  635.                         wtz = wtz - 1
  636.                     elseif ntm[2] == 4 then
  637.                         wtx = wtx - 1
  638.                     end
  639.                     if (not (outOfBounds (wty, wtx, wtz))) then
  640.                         if (workBlock[wty][wtx][wtz][1] > 0) then
  641.                             wtm = workBlock[wty][wtx][wtz]
  642.                             btd = moveTo ((wty - bty), (wtx - btx), (wtz - btz), btd)
  643.                             bty = wty
  644.                             btx = wtx
  645.                             btz = wtz
  646.                             if not (wtm[2] == 0) then
  647.                                 faceDir (btd, wtm[2])
  648.                                 btd = wtm[2]
  649.                             end
  650.                             placeDown(wtm)
  651.                             workBlock[wty][wtx][wtz] = {0, 0}
  652.                             faceDir (btd, ntm[2])
  653.                             btd = ntm[2]
  654.                             moveBack()
  655.                             btx = ntx
  656.                             btz = ntz
  657.                             btd = ntm[2]
  658.                             placeDown(ntm)
  659.                             workBlock[wty][ntx][ntz] = {0, 0}
  660.                         end
  661.                     end
  662.                 end
  663.             end
  664.         end
  665.  
  666.         nty, ntx, ntz = i, 1, 1
  667.         ntm = workBlock[i][1][1]
  668.         if (ntm[2] == 0) and (ntm[1] > 0) then
  669.             btd = moveTo ((nty - bty), (ntx - btx), (ntz - btz), btd)
  670.             bty = nty
  671.             btx = ntx
  672.             btz = ntz
  673.             placeDown(ntm)
  674.             workBlock[bty][btx][btz][1] = 0
  675.         end
  676.         while not (nty > i) do
  677.             nty, ntx, ntz, ntm = advNext (nty, ntx, ntz, 0)
  678.             if (nty == i) and (ntm[2] == 0) and (ntm[1] > 0) then
  679.                 btd = moveTo ((nty - bty), (ntx - btx), (ntz - btz), btd)
  680.                 bty = nty
  681.                 btx = ntx
  682.                 btz = ntz
  683.                 placeDown(ntm)
  684.                 workBlock[bty][btx][btz][1] = 0
  685.             end
  686.         end
  687.  
  688.         nty, ntx, ntz = i, 16, 1
  689.         ntm = workBlock[i][16][1]
  690.         if (ntm[2] > 0) and (ntm[1] > 0) then
  691.             btd = moveTo ((nty - bty), (ntx - btx), (ntz - btz), btd)
  692.             bty = nty
  693.             btx = ntx
  694.             btz = ntz
  695.             faceDir (btd, ntm[2])
  696.             btd = ntm[2]
  697.             placeDown(ntm)
  698.             workBlock[i][16][1][1] = 0
  699.         end
  700.         while not (nty > i) do
  701.             nty, ntx, ntz, ntm = advNext (nty, ntx, ntz, 1)
  702.             if (not (nty > bty)) and (ntm[2] > 0) and (ntm[1] > 0) then
  703.                 btd = moveTo ((nty - bty), (ntx - btx), (ntz - btz), btd)
  704.                 bty = nty
  705.                 btx = ntx
  706.                 btz = ntz
  707.                 faceDir (btd, ntm[2])
  708.                 btd = ntm[2]
  709.                 placeDown(ntm)
  710.                 workBlock[bty][btx][btz][1] = 0
  711.             end
  712.         end
  713.     end
  714.  
  715.     return true
  716. end
  717.  
  718.  
  719. function getModemRange (mY)
  720.  
  721.     local modHeight = 0
  722.     if mY <= 96 then modHeight = 0
  723.     else modHeight = (mY - 96)
  724.     end
  725.  
  726.     local tempRange = math.floor (64 + (modHeight * (320 / 159)))
  727.     print ('Current maximum range is ', tempRange, 'm')
  728.     return tempRange
  729. end
  730.  
  731.  
  732. function initTurtle ()
  733.  
  734.     local periLoc = {"top", "bottom", "left", "right", "front", "back"}
  735.     local periStr, tempStr = "", ""
  736.  
  737.     for i = 1, 6, 1 do
  738.         periStr = peripheral.getType (periLoc[i])
  739.         if periStr == "modem" then
  740.             m = peripheral.wrap (periLoc[i])
  741.             m.open (tID)
  742.         elseif periStr == "drive" then
  743.             print ('Drive detected (not connected): '..periLoc[i])
  744.         end
  745.     end
  746.     print ('Pausing for network startup.')
  747.     sleep (2)
  748.     local fx, fy, fz = gps.locate(3)
  749.     if fx == nil then
  750.         tx = mtx
  751.         ty = mty
  752.         tz = mtz
  753.         td = mtd
  754.     else
  755.         gpsLinked = true
  756.         sleep (0.5)
  757.         tx = fx
  758.         ty = fy
  759.         tz = fz
  760.         print (tx, ty, tz)
  761.         if turtle.back() then
  762.             -- account for fringe case where movement pushes
  763.             -- turtle out of gps range
  764.             fx, fy, fz = gps.locate (3)
  765.             if not (fx == nil) then
  766.                 print (fx, fy, fz)
  767.                 if fx ~= tx then
  768.                     if fx > tx then
  769.                         td = East
  770.                     else
  771.                         td = West
  772.                     end
  773.                 else
  774.                     if fz > tz then
  775.                         td = North
  776.                     else
  777.                         td = South
  778.                     end
  779.                 end
  780.             else
  781.                 turtle.forward()
  782.                 if turtle.forward() then
  783.                     fx, fy, fz = gps.locate (3)
  784.                     if not (fx == nil) then
  785.                         if fx ~= tx then
  786.                             if fx > tx then
  787.                                 td = East
  788.                             else
  789.                                 td = West
  790.                             end
  791.                         else
  792.                             if fz > tz then
  793.                                 td = North
  794.                             else
  795.                                 td = South
  796.                             end
  797.                         end
  798.                     else
  799.                         print ('Error detecting turtle direction:')
  800.                         print ('Out of range.')
  801.                     end
  802.                 else
  803.                     print ('Error detecting turtle direction:')
  804.                     print ('Forward movement restricted.')
  805.                 end
  806.             end
  807.         else
  808.             print ('Error initializing turtle facing.')
  809.             print ('Turtle could not reverse.')
  810.         end
  811.         tx = fx
  812.         ty = fy
  813.         tz = fz
  814.     end
  815.  
  816.     tempStr = "Builder_"..tostring(tID)
  817.     if (tLabel == "") or (tLabel ~= tempStr) then
  818.         os.setComputerLabel (tempStr)
  819.         tLabel = tempStr
  820.     end
  821.  
  822.     mRange = getModemRange (ty)
  823.  
  824.     local msgContainer = textutils.serialize ({0, 5, tLabel, tID, tID, reqMessageID ()})
  825.     m.transmit (1, tID, msgContainer)
  826.  
  827.     initInvCatalog()
  828.     termStatus()
  829. end
  830.  
  831.  
  832. initTurtle ()
  833. termStatus()
  834.  
  835. -- refuel ()
  836.  
  837. while true do
  838.     listen ()
  839. end
  840.  
  841. --[[
  842. refuel ()
  843. local newChunk = true
  844. local oMsgTab = {}
  845. local oMsgStr = ""
  846.  
  847. reqChunk ()
  848. while newChunk do
  849. termStatus()
  850. newChunk = buildChunk ()
  851. print ('buildChunk returned : ', newChunk)
  852. oMsgTab = {2, 5, tLabel, tID, tID, reqMessageID ()}
  853. oMsgStr = textutils.serialize (oMsgTab)
  854. m.transmit (0, tID, oMsgStr)
  855. sleep (1)
  856. reqChunk ()
  857. end
  858. m.close(tID)--]]
Advertisement
Add Comment
Please, Sign In to add comment