Advertisement
bueddl

Untitled

Sep 17th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. local suppression_distance = 20
  2. local sensor_side = "top"
  3. local alarm_side = "right"
  4. local timeout = 60 * 10
  5.  
  6. local cnt_trolled = 0
  7. local cnt_suppressed = 0
  8.  
  9. local function is_player_in_range()
  10.   local prox = peripheral.wrap(sensor_side)
  11.   if prox == nil then
  12.     fatal("Sensor not found")
  13.   end
  14.  
  15.   for k,target in pairs(prox.getTargets()) do
  16.     if target.IsPlayer then
  17.       local sqrt = math.sqrt
  18.       local pow = math.pow
  19.       local player = target.Position
  20.       local distance = sqrt(
  21.         pow(player.X, 2) +
  22.         pow(player.Z, 2)
  23.       )
  24.       if distance < 20 then
  25.         return true
  26.       end
  27.     end
  28.   end
  29.   return false
  30. end
  31.  
  32. local function on()
  33.   redstone.setOutput(alarm_side, true)
  34. end
  35.  
  36. local function off()
  37.   redstone.setOutput(alarm_side, false)
  38. end
  39.  
  40. local function alarm()
  41.   on()
  42.   os.sleep(1)
  43.   off()
  44. end
  45.  
  46. local function inc_troll_cnt()
  47.   cnt_trolled = cnt_trolled + 1
  48. end
  49.  
  50. local function inc_suppr_cnt()
  51.   cnt_suppressed = cnt_suppressed + 1
  52. end
  53.  
  54. local function print_stats()
  55.   term.clear()
  56.   term.setCursorPos(1,1)
  57.   print('We trolled ' .. tostring(cnt_trolled) ..
  58.         ' times and needed to suppress ' .. tostring(cnt_suppressed) ..
  59.         ' times.')
  60. end
  61.  
  62. local function troll()
  63.   if not is_player_in_range() then
  64.     inc_troll_cnt()
  65.     alarm()
  66.   else
  67.     inc_suppr_cnt()
  68.   end
  69.   print_stats()
  70. end
  71.  
  72. while true do
  73.   troll()
  74.   os.sleep(timeout)
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement