turtle_client.lua
138 lines ยท 4.4 KB
eHydra Turtle Client
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/eHydra_wip/turtle_client.lua
| 1 | -- eHydra Turtle Client |
| 2 | -- Receives commands and executes programs from eHydra deployment system |
| 3 | |
| 4 | local CLIENT_PORT = 0 |
| 5 | local SERVER_PORT = 420 |
| 6 | local EHYDRA_PROTOCOL = "ehydra" |
| 7 | |
| 8 | -- Find and open modem |
| 9 | local modem = peripheral.find("modem") |
| 10 | if not modem then |
| 11 | print("โ No modem found - wireless functionality disabled") |
| 12 | else |
| 13 | rednet.open(peripheral.getName(modem)) |
| 14 | print("๐ก Wireless modem enabled") |
| 15 | end |
| 16 | |
| 17 | -- Send deployment confirmation |
| 18 | if modem then |
| 19 | print("๐ค Sending deployment confirmation...") |
| 20 | rednet.broadcast("TURTLE_DEPLOYED", EHYDRA_PROTOCOL) |
| 21 | end |
| 22 | |
| 23 | -- Utility functions |
| 24 | local function split(inputstr, sep) |
| 25 | if sep == nil then |
| 26 | sep = "%s" |
| 27 | end |
| 28 | local t = {} |
| 29 | for str in string.gmatch(inputstr, "([^"..sep.."]+)") do |
| 30 | table.insert(t, str) |
| 31 | end |
| 32 | return t |
| 33 | end |
| 34 | |
| 35 | local function checkFuel() |
| 36 | if turtle.getFuelLevel() < 50 then |
| 37 | print("โฝ Low fuel, attempting refuel...") |
| 38 | for slot = 1, 16 do |
| 39 | turtle.select(slot) |
| 40 | if turtle.refuel() then |
| 41 | print("โ Refueled successfully") |
| 42 | return true |
| 43 | end |
| 44 | end |
| 45 | print("โ No fuel available") |
| 46 | return false |
| 47 | end |
| 48 | return true |
| 49 | end |
| 50 | |
| 51 | -- Main command processing loop |
| 52 | print("๐ข eHydra Turtle Client v1.0") |
| 53 | print("============================") |
| 54 | print("Turtle ID: " .. os.getComputerID()) |
| 55 | |
| 56 | -- Get GPS coordinates if available |
| 57 | local x, y, z = gps.locate(5, false) |
| 58 | if x then |
| 59 | print("๐ GPS Location: " .. x .. ", " .. y .. ", " .. z) |
| 60 | else |
| 61 | print("๐ GPS: Not available") |
| 62 | end |
| 63 | |
| 64 | print("โณ Listening for commands...") |
| 65 | |
| 66 | -- Command processing |
| 67 | while true do |
| 68 | if modem then |
| 69 | local senderId, message, protocol = rednet.receive(EHYDRA_PROTOCOL, 1) |
| 70 | |
| 71 | if message then |
| 72 | print("๐จ Received command from " .. senderId) |
| 73 | |
| 74 | if type(message) == "table" then |
| 75 | local command = message.command |
| 76 | |
| 77 | if command == "RUN" then |
| 78 | local program = message.program or "quarry" |
| 79 | local args = message.args or "" |
| 80 | |
| 81 | print("๐ Running program: " .. program .. " " .. args) |
| 82 | |
| 83 | if fs.exists(program) then |
| 84 | shell.run(program .. " " .. args) |
| 85 | else |
| 86 | print("โ Program not found: " .. program) |
| 87 | rednet.send(senderId, {status = "ERROR", message = "Program not found"}, EHYDRA_PROTOCOL) |
| 88 | end |
| 89 | |
| 90 | elseif command == "STATUS" then |
| 91 | local status = { |
| 92 | id = os.getComputerID(), |
| 93 | fuel = turtle.getFuelLevel(), |
| 94 | position = {x = x, y = y, z = z}, |
| 95 | inventory = {} |
| 96 | } |
| 97 | |
| 98 | for slot = 1, 16 do |
| 99 | local item = turtle.getItemDetail(slot) |
| 100 | if item then |
| 101 | status.inventory[slot] = {name = item.name, count = item.count} |
| 102 | end |
| 103 | end |
| 104 | |
| 105 | rednet.send(senderId, {status = "OK", data = status}, EHYDRA_PROTOCOL) |
| 106 | print("๐ Status sent to " .. senderId) |
| 107 | |
| 108 | elseif command == "REFUEL" then |
| 109 | if checkFuel() then |
| 110 | rednet.send(senderId, {status = "OK", fuel = turtle.getFuelLevel()}, EHYDRA_PROTOCOL) |
| 111 | else |
| 112 | rednet.send(senderId, {status = "ERROR", message = "No fuel available"}, EHYDRA_PROTOCOL) |
| 113 | end |
| 114 | |
| 115 | elseif command == "STOP" then |
| 116 | print("๐ Stop command received") |
| 117 | rednet.send(senderId, {status = "OK", message = "Stopping"}, EHYDRA_PROTOCOL) |
| 118 | break |
| 119 | |
| 120 | else |
| 121 | print("โ Unknown command: " .. tostring(command)) |
| 122 | rednet.send(senderId, {status = "ERROR", message = "Unknown command"}, EHYDRA_PROTOCOL) |
| 123 | end |
| 124 | else |
| 125 | print("๐ Text message: " .. tostring(message)) |
| 126 | end |
| 127 | end |
| 128 | end |
| 129 | |
| 130 | -- Small delay to prevent CPU overload |
| 131 | sleep(0.1) |
| 132 | end |
| 133 | |
| 134 | print("๐ข eHydra Turtle Client shutting down...") |
| 135 | if modem then |
| 136 | rednet.close() |
| 137 | end |
| 138 |