SHOW:
|
|
- or go back to the newest paste.
1 | -- command by KnightMiner version 1.0 | |
2 | -- to be used alongside voice | |
3 | ||
4 | -- find a modem | |
5 | local opened, found | |
6 | for _, side in ipairs( peripheral.getNames() ) do | |
7 | if peripheral.getType( side ) == "modem" then | |
8 | if not rednet.isOpen( side ) then | |
9 | rednet.open( side ) | |
10 | opened = side | |
11 | end | |
12 | found = true | |
13 | break | |
14 | end | |
15 | end | |
16 | if not found then | |
17 | print( "No modem found" ) | |
18 | return | |
19 | end | |
20 | ||
21 | -- load config file | |
22 | local config = {} | |
23 | if fs.exists( "command.cfg" ) then | |
24 | local configFile = fs.open( "command.cfg", "r" ) | |
25 | config = textutils.unserialize( configFile.readAll() ) or {} | |
26 | configFile.close() | |
27 | end | |
28 | local turtles = config.turtles or {} | |
29 | local timeout = config.timeout or 5 | |
30 | ||
31 | -- default ID | |
32 | local linkedID | |
33 | if config.link then | |
34 | linkedID = tonumber( config.link ) or turtles[config.link] | |
35 | end | |
36 | ||
37 | -- run a single command | |
38 | local loop | |
39 | function sendCommand( data ) | |
40 | local input = data:gsub( "^([^ ]*).*$", "%1" ) | |
41 | local command = data:gsub( "^[^ ]*%s*", "" ) | |
42 | local num = tonumber( input ) or turtles[input] | |
43 | ||
44 | -- exit the loop | |
45 | if loop and input == "exit" then | |
46 | return true | |
47 | ||
48 | -- link a new turtle | |
49 | elseif loop and input == "link" then | |
50 | if command == "" then | |
51 | linkedID = nil | |
52 | print( "Unlinked from turtle" ) | |
53 | else | |
54 | linkedID = tonumber( command ) or turtles[command] | |
55 | if linkedID then | |
56 | print( "Now linked to '" .. command .. "'" ) | |
57 | else | |
58 | print( "Turtle name '" .. command .. "' is invalid" ) | |
59 | end | |
60 | end | |
61 | ||
62 | -- standard command | |
63 | elseif linkedID or ( num and command ~= "" ) then | |
64 | local sentTo | |
65 | if linkedID then | |
66 | sentTo = linkedID | |
67 | rednet.send( linkedID, data, "voice-command" ) | |
68 | else | |
69 | sentTo = num | |
70 | rednet.send( num, command, "voice-command" ) | |
71 | end | |
72 | if timeout then | |
73 | os.startTimer( timeout ) | |
74 | end | |
75 | while true do | |
76 | local event = { os.pullEvent() } | |
77 | if event[1] == "key" and event[2] == keys["end"] or event[1] == "timer" then | |
78 | break | |
79 | elseif event[1] == "rednet_message" then | |
80 | if event[2] == sentTo and event[4] == "voice-messages" then | |
81 | local messages = event[3] | |
82 | for _, msg in ipairs( messages ) do | |
83 | print( msg ) | |
84 | end | |
85 | break | |
86 | end | |
87 | end | |
88 | end | |
89 | else | |
90 | print( "ERROR: Turtle '" .. input .. "' does not exist" ) | |
91 | end | |
92 | end | |
93 | ||
94 | -- single command | |
95 | local args = { ... } | |
96 | if args[1] then | |
97 | sendCommand( table.concat( args, " " ) ) | |
98 | ||
99 | -- standalone disabled | |
100 | elseif config.disableLoop then | |
101 | print( "Usage: command <command>" ) | |
102 | ||
103 | -- standalone | |
104 | else | |
105 | ||
106 | print( "Running command by KnightMiner" ) | |
107 | print( "Type 'exit' to exit" ) | |
108 | ||
109 | -- main loop | |
110 | loop = true | |
111 | local history = {} | |
112 | local done | |
113 | while not done do | |
114 | write( "cmd" .. ( linkedID and ":" .. linkedID or "" ) .. "> " ) | |
115 | local data = read( nil, history ) | |
116 | table.insert( history, data ) | |
117 | if data ~= '' then | |
118 | done = sendCommand( data ) | |
119 | end | |
120 | end | |
121 | end | |
122 | ||
123 | -- shutdown tasks | |
124 | if opened then | |
125 | rednet.close( opened ) | |
126 | end |