Alexr360

GPS Setup

Jan 27th, 2026 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.32 KB | None | 0 0
  1. --[[
  2.     GPS Node Setup Program for ComputerCraft: Tweaked
  3.    
  4.     This program will:
  5.     1. Navigate to build height
  6.     2. Place 4 computers with ender modems in a GPS constellation
  7.     3. Use a disk drive to install the GPS host program on each computer
  8.     4. Activate all GPS nodes
  9.    
  10.     Required inventory:
  11.     - Slot 1: 4x Computer
  12.     - Slot 2: 4x Ender Modem
  13.     - Slot 3: 1x Disk Drive
  14.     - Slot 4: 1x Floppy Disk
  15.     - Slot 5: Fuel (coal/charcoal/etc) - optional but recommended
  16. ]]
  17.  
  18. -- Configuration
  19. local BUILD_HEIGHT = 0  -- Height to build GPS constellation
  20. local GPS_SPACING = 6      -- Distance between GPS nodes
  21.  
  22. -- GPS host program to write to satellites
  23. local GPS_HOST_PROGRAM = [[
  24. -- GPS Host Program
  25. local x, y, z = gps.locate(2, false)
  26. if not x then
  27.     -- If we can't get GPS, ask the user for coordinates
  28.     print("GPS not available. Please enter coordinates:")
  29.     print("X coordinate:")
  30.     x = tonumber(read())
  31.     print("Y coordinate:")
  32.     y = tonumber(read())
  33.     print("Z coordinate:")
  34.     z = tonumber(read())
  35.    
  36.     if not x or not y or not z then
  37.         print("ERROR: Invalid coordinates")
  38.         return
  39.     end
  40. end
  41.  
  42. print("GPS Host at: " .. x .. ", " .. y .. ", " .. z)
  43. rednet.open("back")
  44. gps.host(x, y, z)
  45. ]]
  46.  
  47. -- Utility Functions
  48. local function refuel()
  49.     -- Try to refuel from slot 5
  50.     if turtle.getFuelLevel() == "unlimited" then
  51.         return true
  52.     end
  53.    
  54.     turtle.select(5)
  55.     if turtle.getFuelLevel() < 1000 and turtle.refuel(1) then
  56.         print("Refueled. Fuel level: " .. turtle.getFuelLevel())
  57.         return true
  58.     end
  59.     return turtle.getFuelLevel() > 100
  60. end
  61.  
  62. local function ensureFuel(needed)
  63.     if turtle.getFuelLevel() == "unlimited" then
  64.         return true
  65.     end
  66.    
  67.     while turtle.getFuelLevel() < needed do
  68.         print("Need fuel. Current: " .. turtle.getFuelLevel() .. " Need: " .. needed)
  69.         if not refuel() then
  70.             print("Out of fuel! Add fuel to slot 5 and press any key...")
  71.             os.pullEvent("key")
  72.         end
  73.     end
  74.     return true
  75. end
  76.  
  77. local function safeUp()
  78.     while not turtle.up() do
  79.         if turtle.detectUp() then
  80.             turtle.digUp()
  81.         else
  82.             turtle.attackUp()
  83.         end
  84.         sleep(0.5)
  85.     end
  86. end
  87.  
  88. local function safeDown()
  89.     while not turtle.down() do
  90.         if turtle.detectDown() then
  91.             turtle.digDown()
  92.         else
  93.             turtle.attackDown()
  94.         end
  95.         sleep(0.5)
  96.     end
  97. end
  98.  
  99. local function safeForward()
  100.     while not turtle.forward() do
  101.         if turtle.detect() then
  102.             turtle.dig()
  103.         else
  104.             turtle.attack()
  105.         end
  106.         sleep(0.5)
  107.     end
  108. end
  109.  
  110. -- Navigate to build height
  111. local function navigateToBuildHeight()
  112.     print("Navigating to build height: " .. BUILD_HEIGHT)
  113.     local startY = 0
  114.    
  115.     -- Try to get current position via GPS
  116.     local x, y, z = gps.locate(2, false)
  117.     if y then
  118.         startY = y
  119.         print("Current Y: " .. y)
  120.     else
  121.         print("Warning: GPS not available, assuming ground level")
  122.     end
  123.    
  124.     local blocksToClimb = BUILD_HEIGHT - startY
  125.     if blocksToClimb < 0 then
  126.         print("Already above build height!")
  127.         return true
  128.     end
  129.    
  130.     ensureFuel(blocksToClimb + 100)
  131.    
  132.     for i = 1, blocksToClimb do
  133.         safeUp()
  134.         if i % 10 == 0 then
  135.             print("Height: " .. (startY + i))
  136.         end
  137.     end
  138.    
  139.     print("Reached build height!")
  140.     return true
  141. end
  142.  
  143. -- Place a computer with ender modem
  144. local function placeComputer(slot)
  145.     turtle.select(1)  -- Computer slot
  146.     if not turtle.placeDown() then
  147.         print("ERROR: Failed to place computer")
  148.         return false
  149.     end
  150.    
  151.     -- Attach ender modem
  152.     turtle.select(2)  -- Ender modem slot
  153.     turtle.dropDown(1)
  154.    
  155.     return true
  156. end
  157.  
  158. -- Install GPS program on computer via disk drive
  159. local function installGPSProgram(x, y, z)
  160.     -- Place disk drive in front
  161.     turtle.select(3)  -- Disk drive slot
  162.     if not turtle.place() then
  163.         print("ERROR: Failed to place disk drive")
  164.         return false
  165.     end
  166.    
  167.     sleep(0.5)
  168.    
  169.     -- Insert disk
  170.     turtle.select(4)  -- Disk slot
  171.     turtle.drop(1)
  172.    
  173.     sleep(1)
  174.    
  175.     -- Find the disk drive peripheral
  176.     local drive = peripheral.find("drive")
  177.     if not drive then
  178.         print("ERROR: Could not find disk drive")
  179.         return false
  180.     end
  181.    
  182.     local mountPath = drive.getMountPath()
  183.     if not mountPath then
  184.         print("ERROR: No disk in drive")
  185.         return false
  186.     end
  187.    
  188.     -- Write GPS host program to disk
  189.     local file = fs.open(fs.combine(mountPath, "startup.lua"), "w")
  190.     file.write(GPS_HOST_PROGRAM)
  191.     file.close()
  192.    
  193.     sleep(0.5)
  194.    
  195.     -- Retrieve disk
  196.     turtle.select(4)
  197.     turtle.suck(1)
  198.    
  199.     -- Remove disk drive
  200.     turtle.select(3)
  201.     turtle.dig()
  202.    
  203.     return true
  204. end
  205.  
  206. -- Turn on the computer below
  207. local function activateComputer()
  208.     -- Find the computer
  209.     local comp = peripheral.wrap("bottom")
  210.     if comp and comp.turnOn then
  211.         comp.turnOn()
  212.         print("Computer activated")
  213.         sleep(1)
  214.         return true
  215.     else
  216.         print("Warning: Could not activate computer automatically")
  217.         return false
  218.     end
  219. end
  220.  
  221. -- Setup a single GPS node
  222. local function setupGPSNode(nodeNum, x, y, z)
  223.     print("Setting up GPS Node " .. nodeNum .. " at " .. x .. ", " .. y .. ", " .. z)
  224.    
  225.     -- Place computer with modem
  226.     if not placeComputer(nodeNum) then
  227.         return false
  228.     end
  229.    
  230.     -- Install GPS program
  231.     if not installGPSProgram(x, y, z) then
  232.         return false
  233.     end
  234.    
  235.     -- Activate computer
  236.     activateComputer()
  237.    
  238.     print("GPS Node " .. nodeNum .. " complete!")
  239.     return true
  240. end
  241.  
  242. -- Main setup function
  243. local function setupGPSConstellation()
  244.     print("=== GPS Constellation Setup ===")
  245.     print("This will set up 4 GPS nodes in a square pattern")
  246.     print("")
  247.    
  248.     -- Navigate to build height
  249.     navigateToBuildHeight()
  250.    
  251.     -- Get starting position
  252.     local startX, startY, startZ = gps.locate(5, false)
  253.     if not startX then
  254.         print("Warning: Could not get GPS position")
  255.         print("Using relative coordinates from build height")
  256.         startX, startY, startZ = 0, BUILD_HEIGHT, 0
  257.     end
  258.    
  259.     print("Starting position: " .. startX .. ", " .. startY .. ", " .. startZ)
  260.    
  261.     -- GPS node positions (square pattern)
  262.     local positions = {
  263.         {x = startX, y = startY, z = startZ},
  264.         {x = startX + GPS_SPACING, y = startY, z = startZ},
  265.         {x = startX + GPS_SPACING, y = startY, z = startZ + GPS_SPACING},
  266.         {x = startX, y = startY, z = startZ + GPS_SPACING}
  267.     }
  268.    
  269.     -- Setup each GPS node
  270.     for i, pos in ipairs(positions) do
  271.         if i > 1 then
  272.             -- Navigate to next position
  273.             if i == 2 then
  274.                 -- Move +X
  275.                 for j = 1, GPS_SPACING do
  276.                     safeForward()
  277.                 end
  278.             elseif i == 3 then
  279.                 -- Turn right and move +Z
  280.                 turtle.turnRight()
  281.                 for j = 1, GPS_SPACING do
  282.                     safeForward()
  283.                 end
  284.             elseif i == 4 then
  285.                 -- Turn right and move -X
  286.                 turtle.turnRight()
  287.                 for j = 1, GPS_SPACING do
  288.                     safeForward()
  289.                 end
  290.             end
  291.         end
  292.        
  293.         -- Setup the node
  294.         setupGPSNode(i, pos.x, pos.y, pos.z)
  295.         sleep(1)
  296.     end
  297.    
  298.     print("")
  299.     print("=== GPS Constellation Setup Complete! ===")
  300.     print("All 4 GPS nodes are now running")
  301.     print("Test with: gps.locate()")
  302.    
  303.     -- Return to start position
  304.     print("Returning to start position...")
  305.     turtle.turnRight()
  306.     for j = 1, GPS_SPACING do
  307.         safeForward()
  308.     end
  309.     turtle.turnLeft()
  310.     turtle.turnLeft()
  311. end
  312.  
  313. -- Check inventory
  314. local function checkInventory()
  315.     print("Checking inventory...")
  316.     local items = {
  317.         {slot = 1, name = "Computer", needed = 4},
  318.         {slot = 2, name = "Ender Modem", needed = 4},
  319.         {slot = 3, name = "Disk Drive", needed = 1},
  320.         {slot = 4, name = "Floppy Disk", needed = 1}
  321.     }
  322.    
  323.     for _, item in ipairs(items) do
  324.         turtle.select(item.slot)
  325.         local detail = turtle.getItemDetail()
  326.         if not detail or detail.count < item.needed then
  327.             print("ERROR: Need " .. item.needed .. "x " .. item.name .. " in slot " .. item.slot)
  328.             return false
  329.         end
  330.     end
  331.    
  332.     print("Inventory check passed!")
  333.     return true
  334. end
  335.  
  336. -- Main program
  337. local function main()
  338.     print("GPS Node Setup Program")
  339.     print("======================")
  340.     print("")
  341.    
  342.     if not checkInventory() then
  343.         print("")
  344.         print("Please add the required items:")
  345.         print("Slot 1: 4x Computer")
  346.         print("Slot 2: 4x Ender Modem")
  347.         print("Slot 3: 1x Disk Drive")
  348.         print("Slot 4: 1x Floppy Disk")
  349.         print("Slot 5: Fuel (optional)")
  350.         return
  351.     end
  352.    
  353.     print("")
  354.     print("Press any key to begin setup..."  )
  355.     os.pullEvent("key")
  356.    
  357.     setupGPSConstellation()
  358. end
  359.  
  360. -- Run the program
  361. main()
Advertisement
Add Comment
Please, Sign In to add comment