I'm building a mobspawner.
Turtles would do the killing part but I can't seem to tackle an issue.
I just can't figure how to get the turtle to keep swinging his sword until an event occures.
I could probably resume the program by using another very fast timer to skip the waiting, but that's just doesn't seem like a proper way to do.
Could someone help me out with a more elegant solution?
So far this is what I got: (in pastebin)
Spoiler
inputPos = "bottom" --redstone input
-- event = 1
delayTime = 10 --Delay in seconds
opStatus = {["on"] = false, --turtle should attack when true
["off"] = false, --turtle should NOT attack when true
["delayedOff"] = false --turtle should attack when true
}
function attack()
if opStatus.on or opStatus.delayedOff then
turtle.attack()
end
end
--[[
TODO: dropAll is too slow!!!
]]--
function dropAll()
for i=1,16 do
turtle.select(i)
turtle.drop()
end
end
--Displays statuses and warning.
function display()
term.clear()
term.setCursorPos(2,2)
print("I'm operating.")
term.setCursorPos(2,4)
for status,state in pairs (opStatus) do
if state then
print ("I'm in '".. status .."' state.")
if not status == "off" then
term.setCursorPos(2,5)
print ("Don't go in front of me!")
end
end
end
end
--Main starts here
--[[Filling up opStatus table.
If redstone is active (mobspawner is off) upon load the turtle
stays on for delayTime seconds since it cannot be known if there
are any mobs in front of it.
--]]
if rs.getInput(inputPos) then
os.startTimer(delayTime)
opStatus.on = false
opStatus.off = false
opStatus.delayedOff = true
else
opStatus.on = true
opStatus.off = false
opStatus.delayedOff = false
end
while true do
display()
attack()
event = os.pullEvent()
if event == redstone then
if rs.getInput(inputPos) then
--[[when rs input is active: delayedOff and
start the delayTimer
]]--
os.startTimer(delayTime)
opStatus.on = false
opStatus.off = false
opStatus.delayedOff = true
else
--when rs input is inactive: turn on
opStatus.on = true
opStatus.off = false
opStatus.delayedOff = false
end
elseif event == timer then
--when delayTimer is up: turn off
opStatus.on = false
opStatus.off = true
opStatus.delayedOff = false
end
dropAll()
end
The code is not perfection and probably still got some (many?) flaws.
Mainly I'm focused on the
attack()
event = os.pullEvent()
part.Thanks the help in advance!
Cheers! :-)