Advertisement
ea-st-ar

melee_turtlev12

Jul 11th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. --Turtle v1.2
  2. https://pastebin.com/e93GxVeT
  3.  
  4. --for testing
  5. s = 1
  6. --/for testing
  7.  
  8. inputPos = "bottom" --redstone input
  9. delayTime = 10 --Delay in seconds
  10. opStatus = {
  11.             ["on"] = false, --turtle should attack when true
  12.             ["off"] = false, --turtle should NOT attack when true
  13.             ["delayedOff"] = false --turtle should attack when true
  14.            }
  15. displayTimerStart = true
  16. displayRefreshRate = 1
  17. delayTimerStart = true
  18.  
  19.  
  20.  
  21. function attack()
  22.  while true do
  23.   if opStatus.on or opStatus.delayedOff then turtle.attack() end
  24.   sleep(0) --no idea why it is needed but it do o.O
  25.  end
  26. end
  27.  
  28.  
  29. function dropAll()
  30.  while true do
  31.   if opStatus.on or opStatus.delayedOff then
  32.    for i=1,16 do
  33.     turtle.select(i)
  34.     turtle.drop()
  35.    end
  36.   end
  37.  sleep(0) --no idea why it is needed but it do o.O
  38.  end
  39. end
  40.  
  41. --Displays statuses
  42. function display()
  43.  term.clear()
  44.  term.setCursorPos(2,2)
  45.  print("I'm operating.")
  46.  term.setCursorPos(2,4)
  47.  for status,state in pairs (opStatus) do
  48.   if state then
  49.    print ("I'm in '".. status .."' state.")
  50.    if not (status == "off") then
  51.     term.setCursorPos(2,5)
  52.     print ("Don't go in front of me!")
  53.    end
  54.   end
  55.  end
  56. --for testing
  57.  term.setCursorPos(2,11)
  58.  term.write(tostring(s))
  59.  s = s+1
  60. --/for testing
  61. end
  62.  
  63. --Main starts here
  64.  
  65. function main()
  66.  
  67. --[[Filling up opStatus table.
  68. If redstone is active (mobspawner is off) upon load the turtle
  69. stays on for delayTime seconds since it cannot be known if there
  70. are any mobs in front of it.
  71. --]]
  72.  if rs.getInput(inputPos) then
  73.    delayTimerID = os.startTimer(delayTime)
  74.    opStatus.on = false
  75.    opStatus.off = false
  76.    opStatus.delayedOff = true
  77. --for testing
  78.    term.setCursorPos(2,10)
  79.    term.clearLine()
  80.    print("delayedOff ol")
  81. --/for testing
  82.  else
  83.   opStatus.on = true
  84.   opStatus.off = false
  85.   opStatus.delayedOff = false
  86. --for testing
  87.   term.setCursorPos(2,10)
  88.   term.clearLine()
  89.   print("On ol")
  90. --/for testing
  91.  end
  92.  
  93.  
  94.  while true do
  95.   if displayTimerStart then
  96.    displayTimerStart = false
  97.    displayTimerID = os.startTimer(displayRefreshRate)
  98.   end
  99.  
  100.   event, p1 = os.pullEvent()
  101.   if event == "redstone" then
  102.    if rs.getInput(inputPos) then
  103.  --[[when rs input is active: delayedOff and
  104.      start the delayTimer
  105.  ]]--
  106.     delayTimerID = os.startTimer(delayTime)
  107.     opStatus.on = false
  108.     opStatus.off = false
  109.     opStatus.delayedOff = true
  110. --for testing
  111.     term.setCursorPos(2,10)
  112.     term.clearLine()
  113.     print("delayedOff il")
  114. --/for testing
  115.    else
  116.  --when rs input is inactive: turn on
  117.     opStatus.on = true
  118.     opStatus.off = false
  119.     opStatus.delayedOff = false
  120. --for testing    
  121.     term.setCursorPos(2,10)
  122.     term.clearLine()
  123.     print("on il")
  124. --/for testing
  125.    end
  126.   elseif event == "timer" and p1 == delayTimerID and opStatus.delayedOff then
  127.  --when delayTimer is up and in delayOff state: turn off
  128.    opStatus.on = false
  129.    opStatus.off = true
  130.    opStatus.delayedOff = false
  131. --for testing
  132.    term.setCursorPos(2,10)
  133.    term.clearLine()
  134.    print("Off il")
  135. --/for testing
  136.   elseif event == "timer" and p1 == displayTimerID then
  137.    display()
  138.    displayTimerStart = true
  139.   end
  140.  end
  141. end
  142.  
  143. parallel.waitForAny(main,attack,dropAll)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement