Grauly

CC_IEC-OS turtle v1 _2

Apr 16th, 2021 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.79 KB | None | 0 0
  1. local fuelSlot = 1
  2. local floorWireID = "chisel:factory"
  3. local floorWires = {
  4.     2;
  5.     3;
  6.     --not needed
  7. }
  8. local floorSideID = "chisel:technicalnew"
  9. local floorSide = {
  10.     2;
  11.     3;
  12.     4;
  13.     --not needed
  14. }
  15. local floorMainID = "chisel:laboratory"
  16. local floorMain = {
  17.     2;
  18.     3;
  19.     4;
  20. }
  21. local wallsID = "chisel:laboratory"
  22. local walls = {
  23.     9;
  24.     10;
  25.     11;
  26.     12;
  27.     13;
  28.     14;
  29.     15;
  30.     16;
  31. }
  32.  
  33. local IECNET = true
  34. -- end of config
  35. local args = {...}
  36.  
  37. --IECNET setup
  38. local gpsEnabled = false
  39. local net = "Turtlenet"
  40. function iecnetSetup()
  41.     if (IECNET) then
  42.         if (peripheral.isPresent("left")) then
  43.             if (peripheral.getType("left") == "modem") then
  44.                 --peripheral is present, starting IECNET
  45.                 rednet.open("left")
  46.                 if (rednet.lookup(net,"IEC-Host") == nil) then
  47.                     print("IECNET does not have a Host or Host is Offline, broadcasts will go unheard.")
  48.                 end
  49.                 x,y,z = gps.locate()
  50.                 if (x == nil) then
  51.                     print("GPS not available, will not send Location Data")
  52.                 else
  53.                     gpsEnabled = true;
  54.                 end
  55.                 rednet.broadcast("Online",net)
  56.             else
  57.                 print("IECNET was enabled, but no modem was found.")
  58.                 IECNET = false
  59.             end
  60.         else
  61.             print("IECNET was enabled, but no modem was found.")
  62.             IECNET = false
  63.         end
  64.     end
  65. end
  66.  
  67. function getLocation()
  68.     x,y,z = gps.locate()
  69.     if (x == nil) then
  70.         gpsEnabled = false
  71.         return nil
  72.     else
  73.         if (not gpsEnabled) then
  74.             gpsEnabled = true
  75.             print("GPS now available, sending location data")
  76.         end
  77.         x = math.floor(x+0.5)
  78.         y = math.floor(y+0.5)
  79.         z = math.floor(z+0.5)
  80.         return tostring(x).." "..tostring(y).." "..tostring(z)
  81.     end
  82. end
  83.  
  84. function refuel()
  85.     if(turtle.getFuelLevel() < 5) then
  86.         turtle.select(fuelSlot)
  87.         if(turtle.refuel(1) == false) then
  88.             if (IECNET) then
  89.                 if (gpsEnabled) then
  90.                     rednet.broadcast("Out of Fuel ".."\n"..location,net)
  91.                 else
  92.                     rednet.broadcast("Out of Fuel",net)
  93.                 end
  94.             end
  95.             error("out of fuel")
  96.         end
  97.     end
  98. end
  99.  
  100. --digs the block infront
  101. function digForward()
  102.     while(turtle.dig()) do
  103.     end
  104. end
  105.  
  106. --digs the block below
  107. function digDown()
  108.     while(turtle.digDown()) do
  109.     end
  110. end
  111.  
  112. --digs the block above
  113. function digUp()
  114.     while(turtle.digUp()) do
  115.     end
  116. end
  117.  
  118. function placeWalls()
  119.     for i=1, #walls do
  120.         turtle.select(walls[i])
  121.         if(turtle.getItemCount() > 0) then
  122.             if(turtle.getItemDetail().name == wallsID) then
  123.                 while(not turtle.place()) do
  124.                     digForward()
  125.                 end
  126.                 return true
  127.             end
  128.         end
  129.     end
  130.     return false
  131. end
  132.  
  133. function placeWallsUp()
  134.     for i=1, #walls do
  135.         turtle.select(walls[i])
  136.         if(turtle.getItemCount() > 0) then
  137.             if(turtle.getItemDetail().name == wallsID) then
  138.                 while(not turtle.placeUp()) do
  139.                     digDown()
  140.                 end
  141.                 return true
  142.             end
  143.         end
  144.     end
  145.     return false
  146. end
  147.  
  148. function placeFloorWire()
  149.     for i=1, #floorWires do
  150.         turtle.select(floorWires[i])
  151.         if(turtle.getItemCount() > 0) then
  152.             if(turtle.getItemDetail().name == floorWireID) then
  153.                 while(not turtle.placeDown()) do
  154.                     digDown()
  155.                 end
  156.                 return true
  157.             end
  158.         end
  159.     end
  160.     return false
  161. end
  162.  
  163. function placeFloorSide()
  164.     for i=1, #floorSide do
  165.         turtle.select(floorSide[i])
  166.         if(turtle.getItemCount() > 0) then
  167.             if(turtle.getItemDetail().name == floorSideID) then
  168.                 while(not turtle.placeDown()) do
  169.                     digDown()
  170.                 end
  171.                 return true
  172.             end
  173.         end
  174.     end
  175.     return false
  176. end
  177.  
  178. function placeFloorMain()
  179.     for i=1, #floorMain do
  180.         turtle.select(floorMain[i])
  181.         if(turtle.getItemCount() > 0) then
  182.             if(turtle.getItemDetail().name == floorMainID) then
  183.                 while(not turtle.placeDown()) do
  184.                     digDown()
  185.                 end
  186.                 return true
  187.             end
  188.         end
  189.     end
  190.     return false
  191. end
  192.  
  193. function goForward()
  194.     digForward()
  195.     turtle.forward()
  196.     digUp()
  197.     digDown()
  198.     refuel()
  199. end
  200.  
  201. function turn()
  202.     turtle.turnRight()
  203.     turtle.turnRight()
  204. end
  205.  
  206. function move(dist)
  207.     for i=2,dist,1 do
  208.         turtle.forward()
  209.         refuel()
  210.     end
  211. end
  212.  
  213. function tunnel()
  214.     --assumes the turtle is in the middle on the floor
  215.     goForward()
  216.     placeFloorMain()
  217.     turtle.turnRight()
  218.     goForward()
  219.     placeFloorMain()
  220.     placeWalls()
  221.     turtle.up()
  222.     placeWalls()
  223.     turn()
  224.     move(2)
  225.     digForward()
  226.     turtle.forward()
  227.     placeWalls()
  228.     digDown()
  229.     turtle.down()
  230.     placeWalls()
  231.     placeFloorMain()
  232.     turn()
  233.     move(2)
  234.     turtle.turnLeft()
  235. end
  236.  
  237. --begin of program
  238. local iterations = 1
  239. if(not(args[1] == nil)) then
  240.     iterations = args[1]
  241. end
  242. iecnetSetup()
  243. print("Starting!")
  244. for i=1,iterations,1 do
  245.     if (IECNET) then
  246.         location = getLocation()
  247.         if (gpsEnabled) then
  248.             rednet.broadcast("Iteration "..i.."/"..iterations.."\n"..location,net)
  249.         else
  250.             rednet.broadcast("Iteration "..i.."/"..iterations,net)
  251.         end
  252.     end
  253.     tunnel()
  254. end
  255. if(IECNET) then
  256.     location = getLocation()
  257.     if (gpsEnabled) then
  258.         rednet.broadcast("Done!".."\n"..location,net)
  259.     else
  260.         rednet.broadcast("Done!",net)
  261.     end
  262. end
Add Comment
Please, Sign In to add comment