turtle_client.lua

138 lines ยท 4.4 KB

Open raw

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
4local CLIENT_PORT = 0
5local SERVER_PORT = 420
6local EHYDRA_PROTOCOL = "ehydra"
7
8-- Find and open modem
9local modem = peripheral.find("modem")
10if not modem then
11 print("โŒ No modem found - wireless functionality disabled")
12else
13 rednet.open(peripheral.getName(modem))
14 print("๐Ÿ“ก Wireless modem enabled")
15end
16
17-- Send deployment confirmation
18if modem then
19 print("๐Ÿ“ค Sending deployment confirmation...")
20 rednet.broadcast("TURTLE_DEPLOYED", EHYDRA_PROTOCOL)
21end
22
23-- Utility functions
24local 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
33end
34
35local 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
49end
50
51-- Main command processing loop
52print("๐Ÿข eHydra Turtle Client v1.0")
53print("============================")
54print("Turtle ID: " .. os.getComputerID())
55
56-- Get GPS coordinates if available
57local x, y, z = gps.locate(5, false)
58if x then
59 print("๐Ÿ“ GPS Location: " .. x .. ", " .. y .. ", " .. z)
60else
61 print("๐Ÿ“ GPS: Not available")
62end
63
64print("โณ Listening for commands...")
65
66-- Command processing
67while 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)
132end
133
134print("๐Ÿข eHydra Turtle Client shutting down...")
135if modem then
136 rednet.close()
137end
138