startup.lua
90 lines · 2.5 KB
Startup script for newly deployed turtles
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/Maengorn/startup.lua
| 1 | -- Startup script for newly deployed turtles |
| 2 | |
| 3 | -- Try to equip wireless modem if available |
| 4 | function equipModem() |
| 5 | print("Checking for wireless modem...") |
| 6 | |
| 7 | -- Check if already equipped |
| 8 | if peripheral.find("modem") then |
| 9 | print("Modem already equipped") |
| 10 | return true |
| 11 | end |
| 12 | |
| 13 | -- Try to find modem in inventory |
| 14 | for slot = 1, 16 do |
| 15 | local item = turtle.getItemDetail(slot) |
| 16 | if item then |
| 17 | if item.name:find("wireless_modem") then |
| 18 | print("Found modem in slot " .. slot .. ", equipping...") |
| 19 | turtle.select(slot) |
| 20 | turtle.equipLeft() |
| 21 | if peripheral.find("modem") then |
| 22 | print("Modem equipped on left") |
| 23 | return true |
| 24 | end |
| 25 | turtle.equipRight() |
| 26 | if peripheral.find("modem") then |
| 27 | print("Modem equipped on right") |
| 28 | return true |
| 29 | end |
| 30 | end |
| 31 | end |
| 32 | end |
| 33 | |
| 34 | print("WARNING: No wireless modem found!") |
| 35 | return false |
| 36 | end |
| 37 | |
| 38 | -- Try to equip modem first |
| 39 | equipModem() |
| 40 | |
| 41 | function findDiskDrive() |
| 42 | -- Check all sides for a disk drive |
| 43 | local sides = {"left", "right", "front", "back", "top", "bottom"} |
| 44 | for _, side in ipairs(sides) do |
| 45 | if peripheral.getType(side) == "drive" then |
| 46 | if peripheral.call(side, "isDiskPresent") then |
| 47 | print("Found disk on " .. side) |
| 48 | return side |
| 49 | end |
| 50 | end |
| 51 | end |
| 52 | return nil |
| 53 | end |
| 54 | |
| 55 | if not fs.exists("/clientdig") then |
| 56 | -- First boot - look for disk drive |
| 57 | print("First boot - searching for disk drive...") |
| 58 | local diskSide = findDiskDrive() |
| 59 | |
| 60 | if not diskSide then |
| 61 | print("ERROR: No disk drive found!") |
| 62 | print("Please place a disk drive with files adjacent to turtle") |
| 63 | os.sleep(5) |
| 64 | os.reboot() |
| 65 | return |
| 66 | end |
| 67 | |
| 68 | -- Copy files from disk |
| 69 | if fs.exists("disk/clientdig") then |
| 70 | fs.copy("disk/clientdig", "/clientdig") |
| 71 | print("Copied clientdig from disk") |
| 72 | else |
| 73 | print("ERROR: No clientdig found on disk!") |
| 74 | os.sleep(5) |
| 75 | return |
| 76 | end |
| 77 | |
| 78 | if fs.exists("disk/startup") then |
| 79 | fs.copy("disk/startup", "/startup") |
| 80 | print("Copied startup from disk") |
| 81 | end |
| 82 | |
| 83 | print("Setup complete, rebooting...") |
| 84 | os.sleep(1) |
| 85 | os.reboot() |
| 86 | else |
| 87 | -- Files already installed, run the client |
| 88 | print("Starting client...") |
| 89 | shell.run("clientdig") |
| 90 | end |