Grauly

CC_IEC-OS turtle v1

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