Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Turtle v1.2
- https://pastebin.com/e93GxVeT
- --for testing
- s = 1
- --/for testing
- inputPos = "bottom" --redstone input
- 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
- }
- displayTimerStart = true
- displayRefreshRate = 1
- delayTimerStart = true
- function attack()
- while true do
- if opStatus.on or opStatus.delayedOff then turtle.attack() end
- sleep(0) --no idea why it is needed but it do o.O
- end
- end
- function dropAll()
- while true do
- if opStatus.on or opStatus.delayedOff then
- for i=1,16 do
- turtle.select(i)
- turtle.drop()
- end
- end
- sleep(0) --no idea why it is needed but it do o.O
- end
- end
- --Displays statuses
- 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
- --for testing
- term.setCursorPos(2,11)
- term.write(tostring(s))
- s = s+1
- --/for testing
- end
- --Main starts here
- function main()
- --[[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
- delayTimerID = os.startTimer(delayTime)
- opStatus.on = false
- opStatus.off = false
- opStatus.delayedOff = true
- --for testing
- term.setCursorPos(2,10)
- term.clearLine()
- print("delayedOff ol")
- --/for testing
- else
- opStatus.on = true
- opStatus.off = false
- opStatus.delayedOff = false
- --for testing
- term.setCursorPos(2,10)
- term.clearLine()
- print("On ol")
- --/for testing
- end
- while true do
- if displayTimerStart then
- displayTimerStart = false
- displayTimerID = os.startTimer(displayRefreshRate)
- end
- event, p1 = os.pullEvent()
- if event == "redstone" then
- if rs.getInput(inputPos) then
- --[[when rs input is active: delayedOff and
- start the delayTimer
- ]]--
- delayTimerID = os.startTimer(delayTime)
- opStatus.on = false
- opStatus.off = false
- opStatus.delayedOff = true
- --for testing
- term.setCursorPos(2,10)
- term.clearLine()
- print("delayedOff il")
- --/for testing
- else
- --when rs input is inactive: turn on
- opStatus.on = true
- opStatus.off = false
- opStatus.delayedOff = false
- --for testing
- term.setCursorPos(2,10)
- term.clearLine()
- print("on il")
- --/for testing
- end
- elseif event == "timer" and p1 == delayTimerID and opStatus.delayedOff then
- --when delayTimer is up and in delayOff state: turn off
- opStatus.on = false
- opStatus.off = true
- opStatus.delayedOff = false
- --for testing
- term.setCursorPos(2,10)
- term.clearLine()
- print("Off il")
- --/for testing
- elseif event == "timer" and p1 == displayTimerID then
- display()
- displayTimerStart = true
- end
- end
- end
- parallel.waitForAny(main,attack,dropAll)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement