Advertisement
ea-st-ar

melee_turtlev11

Jul 9th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.10 KB | None | 0 0
  1. inputPos = "bottom" --redstone input
  2.  
  3. delayTime = 10 --Delay in seconds
  4.  
  5. opStatus = {["on"] = false, --turtle should attack when true
  6.             ["off"] = false, --turtle should NOT attack when true
  7.             ["delayedOff"] = false --turtle should attack when true
  8.            }
  9. function attack()
  10.  while true do
  11.   if opStatus.on or opStatus.delayedOff then turtle.attack() end
  12.  end
  13. end
  14. --[[
  15. TODO: dropAll is too slow!!!
  16. ]]--
  17. function dropAll()
  18.  while true do
  19.   if opStatus.on or opStatus.delayedOff then
  20.    for i=1,16 do
  21.     turtle.select(i)
  22.     turtle.drop()
  23.    end
  24.    sleep(5)
  25.   end
  26.  end
  27. end
  28.  
  29. --Displays statuses and warning.
  30. function display()
  31.  term.clear()
  32.  term.setCursorPos(2,2)
  33.  print("I'm operating.")
  34.  term.setCursorPos(2,4)
  35.  for status,state in pairs (opStatus) do
  36.   if state then
  37.    print ("I'm in '".. status .."' state.")
  38.    if not status == "off" then
  39.     term.setCursorPos(2,5)
  40.     print ("Don't go in front of me!")
  41.    end
  42.   end
  43.  end
  44. end
  45.  
  46.  function main()
  47.  --Main starts here
  48.  
  49.  --[[Filling up opStatus table.
  50.  If redstone is active (mobspawner is off) upon load the turtle
  51.  stays on for delayTime seconds since it cannot be known if there
  52.  are any mobs in front of it.
  53.  --]]
  54.  if rs.getInput(inputPos) then
  55.   os.startTimer(delayTime)
  56.   opStatus.on = false
  57.   opStatus.off = false
  58.   opStatus.delayedOff = true
  59.  else
  60.   opStatus.on = true
  61.   opStatus.off = false
  62.   opStatus.delayedOff = false
  63.  end
  64.  
  65.  
  66.  while true do
  67.   display()
  68.  
  69.   event = os.pullEvent()
  70.   if event == "redstone" then
  71.    if rs.getInput(inputPos) then
  72.  --[[when rs input is active: delayedOff and
  73.      start the delayTimer
  74.  ]]--
  75.     os.startTimer(delayTime)
  76.     opStatus.on = false
  77.     opStatus.off = false
  78.     opStatus.delayedOff = true
  79.    else
  80.  --when rs input is inactive: turn on
  81.     opStatus.on = true
  82.     opStatus.off = false
  83.     opStatus.delayedOff = false
  84.    end
  85.   elseif event == "timer" then
  86.  --when delayTimer is up: turn off
  87.     opStatus.on = false
  88.     opStatus.off = true
  89.     opStatus.delayedOff = false
  90.   end
  91.  end
  92. end
  93.  
  94. parallel.waitForAny(main,attack,dropAll)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement