Advertisement
osmarks

Traffic Lights v2

Jan 27th, 2019
1,503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. if turtle then
  2.     turtle.equipLeft()
  3.     turtle.equipLeft()
  4.     turtle.equipRight()
  5.     turtle.equipRight()
  6. end
  7.  
  8. local function fetch(u)
  9.     local h = http.get(u)
  10.     local c = h.readAll()
  11.     h.close()
  12.     return c
  13. end
  14.  
  15. local function update()
  16.     local data = fetch "https://pastebin.com/raw/ZfhJz6n4"
  17.     if data then
  18.         local f = fs.open("startup", "w")
  19.         f.write(data)
  20.         f.close()
  21.     end
  22. end
  23.  
  24. if process then process.signal("signd", process.signals.KILL) end
  25.  
  26. local ni = peripheral.find "neuralInterface"
  27. local sign = peripheral.find "minecraft:sign"
  28. local sensor, laser
  29. if not ni then
  30.     sensor = peripheral.find "plethora:sensor"
  31.     laser = peripheral.find "plethora:laser"
  32. else
  33.     sensor = ni
  34.     laser = ni
  35. end
  36. local speaker = peripheral.find "speaker"
  37.  
  38. local function get_direction(entity)
  39.     local x, y, z = entity.x, entity.y, entity.z
  40.     local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
  41.     local yaw = math.atan2(-x, z)
  42.  
  43.     print(yaw, pitch)
  44.  
  45.     return math.deg(yaw), math.deg(pitch)
  46. end
  47.  
  48. local function shoot(entity)
  49.     local dn = entity.displayName
  50.     if dn:find "gollark" or dn:find "laser" or dn:find "Arrow" then return end
  51.     local y, p = get_direction(entity)
  52.     if speaker then speaker.playSound "entity.lightning.impact" end
  53.     laser.fire(y, p, 5)
  54. end
  55.  
  56. local banned = {
  57. }
  58.  
  59. local function update_banned()
  60.     while true do
  61.         local l = textutils.unserialise(fetch(settings.get "traffic.ban_list" or "https://pastebin.com/raw/phbWuSXd"))
  62.         if type(l) == "table" then
  63.             banned = {}
  64.             for _, v in pairs(l) do
  65.                 print("Banned:", v)
  66.                 banned[v] = true
  67.             end
  68.         else
  69.             printError "Invalid Banned List."
  70.         end
  71.         sleep(15)
  72.     end
  73. end
  74.  
  75. local function main()
  76.     local incidents = 0
  77.     while true do
  78.         local entities = sensor.sense()
  79.         for _, entity in pairs(entities) do
  80.             --if entity.displayName:find "item." then shoot(entity) end
  81.             local velocity = vector.new(entity.motionX, entity.motionY + 0.0784, entity.motionZ) -- compensate for gravity
  82.             local position = vector.new(entity.x, entity.y, entity.z)
  83.             local distance = position:length()
  84.             local speed = velocity:length()
  85.             if (speed > 0.8) and not settings.get "traffic.ignore_speed" then
  86.                 incidents = incidents + 1
  87.                 shoot(entity)
  88.             end
  89.             if (distance < 1.5) and not settings.get "traffic.ignore_near" then
  90.                 shoot(entity)
  91.             end
  92.             if speed > 1e-5 then
  93.                 print(entity.displayName, "V", speed, "D", distance)
  94.             end
  95.             if banned[entity.displayName] then shoot(entity) end
  96.         end
  97.         if sign then sign.setSignText("Speed Limit", "Violations:", tostring(incidents)) end
  98.         sleep(1)
  99.     end
  100. end
  101.  
  102. parallel.waitForAll(update, main, update_banned, function()
  103.     while true do local _, k = os.pullEvent "key" if k == keys.u then break end end
  104.     update()
  105.     os.reboot()
  106. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement