Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- GPS Node Setup Program for ComputerCraft: Tweaked
- This program will:
- 1. Navigate to build height
- 2. Place 4 computers with ender modems in a GPS constellation
- 3. Use a disk drive to install the GPS host program on each computer
- 4. Activate all GPS nodes
- Required inventory:
- - Slot 1: 4x Computer
- - Slot 2: 4x Ender Modem
- - Slot 3: 1x Disk Drive
- - Slot 4: 1x Floppy Disk
- - Slot 5: Fuel (coal/charcoal/etc) - optional but recommended
- ]]
- -- Configuration
- local BUILD_HEIGHT = 0 -- Height to build GPS constellation
- local GPS_SPACING = 6 -- Distance between GPS nodes
- -- GPS host program to write to satellites
- local GPS_HOST_PROGRAM = [[
- -- GPS Host Program
- local x, y, z = gps.locate(2, false)
- if not x then
- -- If we can't get GPS, ask the user for coordinates
- print("GPS not available. Please enter coordinates:")
- print("X coordinate:")
- x = tonumber(read())
- print("Y coordinate:")
- y = tonumber(read())
- print("Z coordinate:")
- z = tonumber(read())
- if not x or not y or not z then
- print("ERROR: Invalid coordinates")
- return
- end
- end
- print("GPS Host at: " .. x .. ", " .. y .. ", " .. z)
- rednet.open("back")
- gps.host(x, y, z)
- ]]
- -- Utility Functions
- local function refuel()
- -- Try to refuel from slot 5
- if turtle.getFuelLevel() == "unlimited" then
- return true
- end
- turtle.select(5)
- if turtle.getFuelLevel() < 1000 and turtle.refuel(1) then
- print("Refueled. Fuel level: " .. turtle.getFuelLevel())
- return true
- end
- return turtle.getFuelLevel() > 100
- end
- local function ensureFuel(needed)
- if turtle.getFuelLevel() == "unlimited" then
- return true
- end
- while turtle.getFuelLevel() < needed do
- print("Need fuel. Current: " .. turtle.getFuelLevel() .. " Need: " .. needed)
- if not refuel() then
- print("Out of fuel! Add fuel to slot 5 and press any key...")
- os.pullEvent("key")
- end
- end
- return true
- end
- local function safeUp()
- while not turtle.up() do
- if turtle.detectUp() then
- turtle.digUp()
- else
- turtle.attackUp()
- end
- sleep(0.5)
- end
- end
- local function safeDown()
- while not turtle.down() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- sleep(0.5)
- end
- end
- local function safeForward()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- else
- turtle.attack()
- end
- sleep(0.5)
- end
- end
- -- Navigate to build height
- local function navigateToBuildHeight()
- print("Navigating to build height: " .. BUILD_HEIGHT)
- local startY = 0
- -- Try to get current position via GPS
- local x, y, z = gps.locate(2, false)
- if y then
- startY = y
- print("Current Y: " .. y)
- else
- print("Warning: GPS not available, assuming ground level")
- end
- local blocksToClimb = BUILD_HEIGHT - startY
- if blocksToClimb < 0 then
- print("Already above build height!")
- return true
- end
- ensureFuel(blocksToClimb + 100)
- for i = 1, blocksToClimb do
- safeUp()
- if i % 10 == 0 then
- print("Height: " .. (startY + i))
- end
- end
- print("Reached build height!")
- return true
- end
- -- Place a computer with ender modem
- local function placeComputer(slot)
- turtle.select(1) -- Computer slot
- if not turtle.placeDown() then
- print("ERROR: Failed to place computer")
- return false
- end
- -- Attach ender modem
- turtle.select(2) -- Ender modem slot
- turtle.dropDown(1)
- return true
- end
- -- Install GPS program on computer via disk drive
- local function installGPSProgram(x, y, z)
- -- Place disk drive in front
- turtle.select(3) -- Disk drive slot
- if not turtle.place() then
- print("ERROR: Failed to place disk drive")
- return false
- end
- sleep(0.5)
- -- Insert disk
- turtle.select(4) -- Disk slot
- turtle.drop(1)
- sleep(1)
- -- Find the disk drive peripheral
- local drive = peripheral.find("drive")
- if not drive then
- print("ERROR: Could not find disk drive")
- return false
- end
- local mountPath = drive.getMountPath()
- if not mountPath then
- print("ERROR: No disk in drive")
- return false
- end
- -- Write GPS host program to disk
- local file = fs.open(fs.combine(mountPath, "startup.lua"), "w")
- file.write(GPS_HOST_PROGRAM)
- file.close()
- sleep(0.5)
- -- Retrieve disk
- turtle.select(4)
- turtle.suck(1)
- -- Remove disk drive
- turtle.select(3)
- turtle.dig()
- return true
- end
- -- Turn on the computer below
- local function activateComputer()
- -- Find the computer
- local comp = peripheral.wrap("bottom")
- if comp and comp.turnOn then
- comp.turnOn()
- print("Computer activated")
- sleep(1)
- return true
- else
- print("Warning: Could not activate computer automatically")
- return false
- end
- end
- -- Setup a single GPS node
- local function setupGPSNode(nodeNum, x, y, z)
- print("Setting up GPS Node " .. nodeNum .. " at " .. x .. ", " .. y .. ", " .. z)
- -- Place computer with modem
- if not placeComputer(nodeNum) then
- return false
- end
- -- Install GPS program
- if not installGPSProgram(x, y, z) then
- return false
- end
- -- Activate computer
- activateComputer()
- print("GPS Node " .. nodeNum .. " complete!")
- return true
- end
- -- Main setup function
- local function setupGPSConstellation()
- print("=== GPS Constellation Setup ===")
- print("This will set up 4 GPS nodes in a square pattern")
- print("")
- -- Navigate to build height
- navigateToBuildHeight()
- -- Get starting position
- local startX, startY, startZ = gps.locate(5, false)
- if not startX then
- print("Warning: Could not get GPS position")
- print("Using relative coordinates from build height")
- startX, startY, startZ = 0, BUILD_HEIGHT, 0
- end
- print("Starting position: " .. startX .. ", " .. startY .. ", " .. startZ)
- -- GPS node positions (square pattern)
- local positions = {
- {x = startX, y = startY, z = startZ},
- {x = startX + GPS_SPACING, y = startY, z = startZ},
- {x = startX + GPS_SPACING, y = startY, z = startZ + GPS_SPACING},
- {x = startX, y = startY, z = startZ + GPS_SPACING}
- }
- -- Setup each GPS node
- for i, pos in ipairs(positions) do
- if i > 1 then
- -- Navigate to next position
- if i == 2 then
- -- Move +X
- for j = 1, GPS_SPACING do
- safeForward()
- end
- elseif i == 3 then
- -- Turn right and move +Z
- turtle.turnRight()
- for j = 1, GPS_SPACING do
- safeForward()
- end
- elseif i == 4 then
- -- Turn right and move -X
- turtle.turnRight()
- for j = 1, GPS_SPACING do
- safeForward()
- end
- end
- end
- -- Setup the node
- setupGPSNode(i, pos.x, pos.y, pos.z)
- sleep(1)
- end
- print("")
- print("=== GPS Constellation Setup Complete! ===")
- print("All 4 GPS nodes are now running")
- print("Test with: gps.locate()")
- -- Return to start position
- print("Returning to start position...")
- turtle.turnRight()
- for j = 1, GPS_SPACING do
- safeForward()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Check inventory
- local function checkInventory()
- print("Checking inventory...")
- local items = {
- {slot = 1, name = "Computer", needed = 4},
- {slot = 2, name = "Ender Modem", needed = 4},
- {slot = 3, name = "Disk Drive", needed = 1},
- {slot = 4, name = "Floppy Disk", needed = 1}
- }
- for _, item in ipairs(items) do
- turtle.select(item.slot)
- local detail = turtle.getItemDetail()
- if not detail or detail.count < item.needed then
- print("ERROR: Need " .. item.needed .. "x " .. item.name .. " in slot " .. item.slot)
- return false
- end
- end
- print("Inventory check passed!")
- return true
- end
- -- Main program
- local function main()
- print("GPS Node Setup Program")
- print("======================")
- print("")
- if not checkInventory() then
- print("")
- print("Please add the required items:")
- print("Slot 1: 4x Computer")
- print("Slot 2: 4x Ender Modem")
- print("Slot 3: 1x Disk Drive")
- print("Slot 4: 1x Floppy Disk")
- print("Slot 5: Fuel (optional)")
- return
- end
- print("")
- print("Press any key to begin setup..." )
- os.pullEvent("key")
- setupGPSConstellation()
- end
- -- Run the program
- main()
Advertisement
Add Comment
Please, Sign In to add comment