Advertisement
BombBloke

Turtle Replacer (ComputerCraft)

Feb 2nd, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. -- Turtle Replacement
  2. -- ----------------------------------------------------------
  3.  
  4. -- Configures a newly placed turtle to take up a suitable job.
  5.  
  6. -- ----------------------------------------------------------
  7.  
  8. if not turtle then
  9.     shell.run("\\startup")
  10.     error()
  11. elseif turtle.craft then
  12.     os.setComputerLabel("BBCrafter1")
  13.    
  14.     file = fs.open("\\update","w")
  15.     file.writeLine("shell.run(\"rm\",\"startup\")")
  16.     file.writeLine("shell.run(\"pastebin\",\"get\",\"kCTERg1Z\",\"startup\")")
  17.     file.close()
  18.    
  19.     shell.run("pastebin","get","kCTERg1Z","\\startup")
  20.    
  21.     while not turtle.back() do
  22.         for i=1,16 do if turtle.getItemCount(i) > 0 then
  23.             turtle.select(i)
  24.             turtle.refuel()
  25.         end end
  26.        
  27.         turtle.turnLeft()
  28.     end
  29.    
  30.     os.reboot()
  31. end
  32.  
  33. -- ----------------------------------------------------------
  34.  
  35. -- Initialise important values:
  36.  
  37. node = {{{253,435,61},
  38.     {253,435,67},
  39.     {252,435,67}},
  40.    
  41.     {{245,438,71},
  42.     {249,443,65}},
  43.    
  44.     {{238,445,77},
  45.     {228,457,65}},
  46.    
  47.     {{229,440,75},
  48.     {221,440,71},
  49.     {215,437,65}}}
  50.  
  51. local facing = {"north","east","south","west"}
  52. local direction, x, y, z
  53.  
  54. -- ----------------------------------------------------------
  55.  
  56. -- Functions and stuff:
  57.  
  58. -- ----------------------------------------------------------
  59.  
  60. -- Accepts strings representing compass-facings to turn the turtle.
  61. local function faceDirection(targetdirection)
  62.     local tardir
  63.     for i=1,4 do if targetdirection == facing[i] then
  64.         tardir = i
  65.         break
  66.     end end
  67.    
  68.     if tardir < direction then
  69.         if tardir == 1 and direction == 4 then while not turtle.turnRight() do end
  70.         else for i=1,direction-tardir do while not turtle.turnLeft() do end end end
  71.     elseif tardir > direction then
  72.         if tardir == 4 and direction == 1 then while not turtle.turnLeft() do end
  73.         else for i=1,tardir-direction do while not turtle.turnRight() do end end end
  74.     end
  75.    
  76.     direction = tardir
  77. end
  78.  
  79. -- Travel to a co-ordinate.
  80. local function goToPos(targetx,targety,targetz)
  81.     while x ~= targetx or y ~= targety or z ~= targetz do
  82.         if z > targetz then if turtle.down() then z = z - 1 end
  83.         elseif z < targetz then if turtle.up() then z = z + 1 end end
  84.        
  85.         if x > targetx then
  86.             if direction ~= 4 then faceDirection("west") end
  87.             if turtle.forward() then x = x - 1 end
  88.         elseif x < targetx then
  89.             if direction ~= 2 then faceDirection("east") end
  90.             if turtle.forward() then x = x + 1 end
  91.         end
  92.        
  93.         if y > targety then
  94.             if direction ~= 1 then faceDirection("north") end
  95.             if turtle.forward() then y = y - 1 end
  96.         elseif y < targety then
  97.             if direction ~= 3 then faceDirection("south") end
  98.             if turtle.forward() then y = y + 1 end
  99.         end
  100.     end
  101. end
  102.  
  103. -- ----------------------------------------------------------
  104.  
  105. -- Init complete, let's go!
  106.  
  107. -- ----------------------------------------------------------
  108.  
  109. turtle.select(1)
  110. turtle.refuel()
  111.  
  112. local file = fs.open("\\disk\\id","r")
  113. local myID = tonumber(file.readLine())
  114. file.close()
  115.  
  116. shell.run("cp","\\disk\\update","\\update")
  117. shell.run("cp","\\disk\\node"..myID,"\\node")
  118.  
  119. file = fs.open("\\startup","w")
  120. file.writeLine("shell.run(\"chopper\")")
  121. file.close()
  122.  
  123. shell.run("pastebin","get","waF1RFPz","\\chopper")
  124.  
  125. os.setComputerLabel("BBChopper"..myID)
  126.  
  127. do
  128.     local tempx, tempy, tempz
  129.     while tempx == nil or tempy == nil or tempz == nil do
  130.         tempx, tempy, tempz = gps.locate(5)
  131.         sleep(5)
  132.     end
  133.    
  134.     while not turtle.forward() do while not turtle.turnLeft() do end end
  135.     x,y,z = gps.locate(5)
  136.  
  137.     if x < tempx then direction = 4
  138.     elseif x > tempx then direction = 2
  139.     elseif y < tempy then direction = 1
  140.     else direction = 3 end
  141. end
  142.  
  143. for i=1,#node[1] do goToPos(node[1][i][1],node[1][i][2],node[1][i][3]) end
  144.  
  145. for i=1,#node[1+myID] do goToPos(node[1+myID][i][1],node[1+myID][i][2],node[1+myID][i][3]) end
  146.  
  147. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement