Posted 30 October 2012 - 06:35 PM
I designed this for my Industrial Craft infinite UU-Matter generator, and figured others might like it as well.
it's extactly what it says in the title, there is a control terminal with a lever and a wireless modem, and that will serve as the on/off switch for as many wireless turtles as you can fit in range. Turtles will also automatically search for a new control terminal if the current one stops responding for two minutes. (By default, can be altered in the code)
Currently, the Turtle code just mines cobblestone and nothing else, for use in conjunction with Buildcraft, RedPower, or another turtle or a similar mod to pick up that cobblestone. However, there is no reason I can think of that you couldn't replace the dig command with whatever else you'd like.
If you'd like to use this code for such, I don't mind, and you don't have to give credit so long as you don't say it's yours.
To set up:
1. Place the Control Terminal (computer) where you want it. Note that this will become a dedicaed computer, and you won't be able to use it for anything else.
2. Attach a lever (or some other redstone signal) to the left side.
3. Attach a wireless modem to the right.
4. Place wireless mining turtles facing directly at your cobble generators.
5. Install the CobbleGenTerminal program onto the Terminal and run it.
6. Install the CobbleGenTurtle program onto all the turtles, and run it on all of them.
7. Turn on and off to your heart's content! (Note: You can terminate the turtle program on all attached turtles in range by pressing s. Also, the turtles will probably not turn off instantly, and will have to iterate up to 5 times; read the code's comments if you wish to change this.)
Note that this is my first lua program, and the code probably isn't perfect, especially regarding the Shut Down command, and it may cause lag from all those rednet messages being sent back and forth. If you find a bug or have a suggestion for improving the code, please post it here and I may post an updated version.
Code for the control terminal: (I'll add comments to this soon, though the print()s and messages make it fairly obvious)
Turtle Code:
it's extactly what it says in the title, there is a control terminal with a lever and a wireless modem, and that will serve as the on/off switch for as many wireless turtles as you can fit in range. Turtles will also automatically search for a new control terminal if the current one stops responding for two minutes. (By default, can be altered in the code)
Currently, the Turtle code just mines cobblestone and nothing else, for use in conjunction with Buildcraft, RedPower, or another turtle or a similar mod to pick up that cobblestone. However, there is no reason I can think of that you couldn't replace the dig command with whatever else you'd like.
If you'd like to use this code for such, I don't mind, and you don't have to give credit so long as you don't say it's yours.
To set up:
1. Place the Control Terminal (computer) where you want it. Note that this will become a dedicaed computer, and you won't be able to use it for anything else.
2. Attach a lever (or some other redstone signal) to the left side.
3. Attach a wireless modem to the right.
4. Place wireless mining turtles facing directly at your cobble generators.
5. Install the CobbleGenTerminal program onto the Terminal and run it.
6. Install the CobbleGenTurtle program onto all the turtles, and run it on all of them.
7. Turn on and off to your heart's content! (Note: You can terminate the turtle program on all attached turtles in range by pressing s. Also, the turtles will probably not turn off instantly, and will have to iterate up to 5 times; read the code's comments if you wish to change this.)
Note that this is my first lua program, and the code probably isn't perfect, especially regarding the Shut Down command, and it may cause lag from all those rednet messages being sent back and forth. If you find a bug or have a suggestion for improving the code, please post it here and I may post an updated version.
Code for the control terminal: (I'll add comments to this soon, though the print()s and messages make it fairly obvious)
Spoiler
rednet.open("right")
while true do
while redstone.getInput("left") do
print("Redstone Signal On")
event, id, message = os.pullEvent()
if event == "char" then
if id == "s" then
print("Shutting down turtles...")
rednet.broadcast("Shut Down")
end
elseif event == "redstone" then
print("Turning off turtles...")
elseif event == "rednet_message" then
if message == "New Turtle" then
print("Recieved: Turtle Here")
rednet.send(id, "Terminal here")
print("Sent: Terminal Here")
elseif message == "Mine?" then
print("Received: Mine?")
rednet.send(id, "Yes")
print("Sent: Yes")
end
end
end
while not redstone.getInput("left") do
print("Redstone Signal Off")
TurnOn = true ;
while TurnOn do
event, id, message = os.pullEvent()
if event == "char" then
if id == "s" then
print("Shutting down turtles...")
rednet.broadcast("Shut Down")
end
elseif event == "redstone" then
print("Turning on turtles...")
rednet.broadcast("Turn on")
TurnOn = false
elseif event == "rednet_message" then
if message == "New Turtle" then
print("Recieved: Turtle Here")
rednet.send(id, "Terminal here")
print("Sent: Terminal Here")
elseif message == "Mine?" then
print("Recieved: Mine?")
rednet.send(id, "No")
print("Sent: No")
elseif message == "Ping" then
print("Recieved: Ping")
rednet.send(id, "Pong")
print("Sent: Pong")
end
end
end
end
end
Turtle Code:
Spoiler
local terminalID = 0 ; --[[The ID of the linked terminal]]
local CheckForTerminal = true ; --[[Used to determine if the turtle needs to find a new terminal]]
local TerminalLag = 0 ; --[[If this goes to 12, the terminal will be considered timed out]]
local ShutDown = false ; --[[Used to determine if the turtle should shut down]]
rednet.open("right")
while not ShutDown do --[[This terminates the program if the turtle recieves a Shut Down signal]]
while CheckForTerminal and not ShutDown do --[[This searches for a new terminal for the turtle to link to, ran when the program boots or the terminal times out]]
rednet.broadcast("New Turtle")
print("Sent: New Turtle")
local id, message = rednet.receive(30) --[[Default checks every 30 seconds, you can decrease or increase this if you wish]]
if message == "Terminal here" then
print("Received: Terminal Here")
terminalID = id
CheckForTerminal = false
end
end
while not CheckForTerminal and not ShutDown and TerminalLag <= 12 do --[[Where all the magic happens. Only runs if the turtle has a terminal linked]]
rednet.send(terminalID, "Mine?") --[[Checks if the turtle should perform another iteration, only sends a message to its linked terminal]]
print("Sent: Mine?")
id, message = rednet.receive(10) --[[Altering this will increase or decrease how quickly the terminal times out]]
if id == terminalID and message == "Yes" then --[[If terminal tells the turtle yes, it will perform another iteration]]
TerminalLag = 0
print("Received: Yes")
for i=1, 5 do --[[Iterations completed before checking in with terminal, increase or decrease as much as youd like, though keep in mind that this may cause lag if you use a lot of turtles and this is a low number]]
--[[This is what the turtle will actually do. This can be modified to your heart's content, it shouldn't have any effect on the rest of the program]]
while not turtle.detect() do sleep(1)
end
turtle.dig()
--[[End of turtle actions]]
print(i)
end
elseif id == terminalID and message == "No" then --[[If the terminal tells it no, it puts the turtle on standby until it recieves a "Turn on" signal from its terminal]]
print("Received: No")
local waiting = true ;
TerminalLag = 0
while waiting do --[[Standby check]]
print("Waiting for signal...")
id, message = rednet.receive(30) --[[This determines how quickly the terminal will time out while the turtle is on standby]]
if id == terminalID and message == "Turn on" then --[[Detects turn on signal from terminal]]
print("Recieved: Turn on")
waiting = false
elseif id == terminalID and message == "Shut Down" then --[[Detects shut down signal from terminal]]
print("Recieved Shut Down from Terminal.")
waiting = false
ShutDown = true
elseif TerminalLag == 12 then --[[Detects if the terminal has timed out]]
print("Terminal Timeout, searching for new terminal...")
CheckForTerminal = true
waiting = false
else
rednet.send(terminalID, "Ping") --[[Pings the terminal]]
print("Sent: Ping")
id, message = rednet.receive(30)
if id == terminalID and message == "Pong" then
print("Recieved: Pong")
sleep(1)
elseif id == terminalID and message == "Turn On" then --[[Not sure if this and the next elseif are totally necessary, but eh, might as well for redundancys sake]]
print("Recieved: Turn on (during ping)")
waiting = false
elseif id == terminalID and message == "Shut Down" then
print("Recieved Shut Down from Terminal.")
waiting = false
ShutDown = true
elseif TerminalLag == 12 then --[[Detects if the terminal has timed out]]
print("Terminal Timeout, searching for new terminal...")
CheckForTerminal = true
waiting = false
elseif message == nil then --[[If the turtle recieves no message, then it increases the lag counter by six. You can alter that number below]
TerminalLag = TerminalLag + 6
print("Lagging ".. TerminalLag .. "0 seconds...")
end
end
end
elseif id == terminalID and message == "Shut Down" then --[[Detects a Shut Down message]]
print("Recieved Shut Down from Terminal.")
ShutDown = true
elseif message == nil then --[[If the turtle recieves no rednet messages, it will increase the lag counter by one. You can alter that number below]]
TerminalLag = TerminalLag + 1
print("Lagging " .. TerminalLag .. "0 seconds...")
end
end
if TerminalLag == 12 then --[[Detects a terminal time out]]
print("Terminal Timeout, searching for new terminal...")
CheckForTerminal = true
end
end
while ShutDown do --[[Terminates the program if the turtle has recieved a Shut Down signal]]
print("Good Bye.")
error()
end