SpaceRanger4321

NotificationStuff

Mar 27th, 2022 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 KB | None | 0 0
  1. local username = "enes18enes"
  2. local interface = peripheral.wrap("back")
  3. local ni = peripheral.wrap("back")
  4. local w, h = ni.canvas().getSize()
  5. local canvas = interface.canvas()
  6.  
  7. if _G.thing_group then
  8. pcall(_G.thing_group.remove)
  9. end
  10.  
  11. local types = {
  12.  
  13. ["ERROR"] = 0xFF0000FF,
  14. ["INFO"] = 0x196bc2FF,
  15. ["WARN"] = 0xccc100FF
  16.  
  17. }
  18.  
  19. local function notification(type, text)
  20.  
  21. local notificationBox = canvas.addRectangle(190, 15, 100, 100, 0x67C7EBFF)
  22. --local notificationText = canvas.addText( { x = 220, y = 100 }, "JARVIS 1.2" )
  23.  
  24. if type == types.ERROR then
  25.  
  26. notificationBox.setSize(115, 19)
  27. notificationBox.setAlpha(100)
  28. local typeBox = canvas.addRectangle(190, 34, 100, 100, types.ERROR)
  29. typeBox.setSize(115, 11)
  30. typeBox.setAlpha(100)
  31. local title = canvas.addText( {x = 230, y = 36}, "ERROR" )
  32. local text = canvas.addText( {192, 16}, text)
  33. sleep(1.5)
  34. notificationBox.remove()
  35. typeBox.remove()
  36. title.remove()
  37. text.remove()
  38.  
  39. elseif type == types.INFO then
  40.  
  41. notificationBox.setSize(115, 19)
  42. notificationBox.setAlpha(100)
  43. local typeBox = canvas.addRectangle(190, 34, 100, 100, types.INFO)
  44. typeBox.setSize(115, 11)
  45. typeBox.setAlpha(100)
  46. local title = canvas.addText( {x = 230, y = 36}, "INFO" )
  47. local text = canvas.addText( {192, 16}, text)
  48. sleep(1.5)
  49. notificationBox.remove()
  50. typeBox.remove()
  51. title.remove()
  52. text.remove()
  53.  
  54.  
  55. elseif type == types.WARN then
  56.  
  57. notificationBox.setSize(115, 19)
  58. notificationBox.setAlpha(100)
  59. local typeBox = canvas.addRectangle(190, 34, 100, 100, types.WARN)
  60. typeBox.setSize(115, 11)
  61. typeBox.setAlpha(100)
  62. local title = canvas.addText( {x = 230, y = 36}, "WARN" )
  63. local text = canvas.addText( {192, 16}, text)
  64. sleep(1.5)
  65. notificationBox.remove()
  66. typeBox.remove()
  67. title.remove()
  68. text.remove()
  69.  
  70.  
  71. else
  72.  
  73. notification(types.ERROR, "INVALID TYPE INPUTTED")
  74. print("Invalid notification type selected")
  75.  
  76. end
  77.  
  78. end
  79.  
  80. local colortheme = {
  81. status = 0xFFFFFFFF,
  82. notice = 0xFF880000,
  83. follow = 0xFF00FFFF,
  84. watch = 0xFFFF00FF,
  85. lase = 0xFF0000FF,
  86. entity = 0x00FFFFFF
  87. }
  88.  
  89. local group
  90. local function initialize_group_thing()
  91. if group then pcall(group.remove) end
  92. group = ni.canvas().addGroup({ w - 70, 10 })
  93. _G.thing_group = group
  94. end
  95. initialize_group_thing()
  96.  
  97. local notices = {}
  98. local function push_notice(t)
  99. table.insert(notices, { t, os.epoch "utc" })
  100. end
  101.  
  102. local function iron_hud()
  103.  
  104. canvas.clear()
  105.  
  106. local hudText = canvas.addRectangle(0, 0, 100, 100, 0x67C7EBFF)
  107. hudText.setSize(800, 15)
  108. hudText.setAlpha(124)
  109. local text = canvas.addText( { x = 220, y = 4 }, "JARVIS 1.2" )
  110. local ironLoad1 = canvas.addPolygon()
  111.  
  112.  
  113.  
  114.  
  115. end
  116.  
  117. local user_meta
  118. local addressed_lasers = {}
  119. local fast_mode_reqs = {}
  120. local status_lines = {}
  121. local flight_mode = "std"
  122. local flight_shortcodes = {
  123. o = "off",
  124. b = "brake",
  125. h = "hpower",
  126. l = "lpower",
  127. s = "std",
  128. a = "align"
  129. }
  130.  
  131. local flight_powers = {
  132. std = 1,
  133. lpower = 0.5,
  134. align = 1
  135. }
  136.  
  137. local targets = {}
  138.  
  139. local function is_target(name)
  140. for target, type in pairs(targets) do
  141. if name:lower():match(target) then return type end
  142. end
  143. end
  144.  
  145. local function bool_to_yn(b)
  146. if b == true then return "y"
  147. elseif b == false then return "n"
  148. else return "?" end
  149. end
  150.  
  151. local function scan_entities()
  152. local last_velocity
  153. local last_time
  154. while true do
  155. fast_mode_reqs.laser = false
  156. fast_mode_reqs.acting = false
  157. local entities = ni.sense()
  158. local maybe_players = {}
  159. local things = {}
  160. local notices = {}
  161. local lasers = {}
  162. user_meta = ni.getMetaByName(username)
  163. user_meta.motionY = user_meta.motionY + 0.07840001
  164. local v = vector.new(user_meta.motionX, user_meta.motionY, user_meta.motionZ)
  165. user_meta.velocity = v
  166. local time = os.epoch "utc"
  167. if last_velocity then
  168. user_meta.acceleration = (v - last_velocity) / (time - last_time) * 1000
  169. status_lines.acceleration = ("Acc: %.2f/%.2f"):format(user_meta.acceleration:length(), user_meta.acceleration.y)
  170. end
  171. last_velocity = v
  172. last_time = time
  173.  
  174. status_lines.vel = ("Vel: %.2f/%.2f"):format(v:length(), user_meta.motionY)
  175.  
  176. for _, entity in pairs(entities) do
  177. entity.s = vector.new(entity.x, entity.y, entity.z)
  178. entity.v = vector.new(entity.motionX, entity.motionY, entity.motionZ)
  179. if entity.displayName ~= username then
  180. things[entity.displayName] = (things[entity.displayName] or 0) + 1
  181. end
  182. if entity.displayName ~= username and entity.displayName == entity.name and (math.floor(entity.yaw) ~= entity.yaw and math.floor(entity.pitch) ~= entity.pitch) then -- player, quite possibly
  183. entity.v = entity.v + vector.new(0, 0.0784, 0)
  184. table.insert(maybe_players, entity)
  185. end
  186. if entity.name == "plethora:laser" and not addressed_lasers[entity.id] then
  187. local closest_approach, param = project(entity.s, entity.v - v, vector.new(0, 0, 0))
  188. if param > 0 and vector_sqlength(closest_approach) < 5 then
  189. push_notice "Laser detected"
  190. fast_mode_reqs.laser = true
  191. local time_to_impact = (entity.s:length() / (entity.v - v):length()) / 20
  192. print("got inbound laser", time_to_impact, vector_sqlength(closest_approach), param)
  193. addressed_lasers[entity.id] = true
  194. schedule(function()
  195. --print("TODO: dodge laser")
  196. push_notice "Executing dodging"
  197. local dir2d = vector.new(entity.motionX - user_meta.motionX, 0, entity.motionZ - user_meta.motionZ)
  198. local perpendicular_dir2d = vector.new(1, 0, -dir2d.x / dir2d.z)
  199. local y, p = calc_yaw_pitch(perpendicular_dir2d)
  200. if math.random(1, 2) == 1 then p = -p end
  201. ni.launch(y, p, 3)
  202. end, math.max(0, time_to_impact / 2 - 0.1) * 1000)
  203. schedule(function() addressed_lasers[entity.id] = false end, 15000)
  204. table.insert(lasers, entity)
  205. end
  206. end
  207. end
  208. for _, laser in pairs(lasers) do
  209. for _, player in pairs(maybe_players) do
  210. local closest_approach, param = project(laser.s, laser.v, player.s)
  211. print(player.displayName, closest_approach, param)
  212. if param < 0 and vector_sqlength(closest_approach - player.s) < 8 and counterattack then
  213. print("execute counterattack", player.displayName)
  214. push_notice(("Counterattack %s"):format(player.displayName))
  215. targets[player.displayName:lower()] = "laser"
  216. end
  217. end
  218. end
  219.  
  220. status_lines.counterattacks = "Counter: " .. bool_to_yn(counterattack)
  221.  
  222. local i = 0
  223. local ok, err = pcall(group.clear)
  224. if not ok then
  225. initialize_group_thing()
  226. end
  227. local time = os.epoch "utc"
  228. for _, text in pairs(status_lines) do
  229. group.addText({ 0, i * 7 }, text, colortheme.status, 0.6)
  230. i = i + 1
  231. end
  232. for ix, info in pairs(notices) do
  233. if time >= (info[2] + 1000) then notices[ix] = nil end
  234. group.addText({ 0, i * 7 }, info[1], colortheme.notice, 0.6)
  235. i = i + 1
  236. end
  237. for thing, count in pairs(things) do
  238. local text = thing
  239. if count ~= 1 then text = text .. " " .. count end
  240. group.addText({ 0, i * 7 }, text, colortheme[is_target(thing) or "entity"], 0.6)
  241. i = i + 1
  242. end
  243.  
  244. for _, entity in pairs(entities) do
  245. local action = is_target(entity.displayName)
  246. if action then
  247. if action == "laser" then
  248. schedule(function() lase(entity) end, 0, entity.id)
  249. elseif action == "watch" then
  250. schedule(function() ni.look(calc_yaw_pitch(entity.s)) end, 0, entity.id)
  251. elseif action == "follow" then
  252. schedule(function()
  253. local y, p = calc_yaw_pitch(entity.s)
  254. ni.launch(y, p, math.min(entity.s:length() / 24, 2))
  255. end, 0, entity.id)
  256. end
  257. fast_mode_reqs.acting = true
  258. end
  259. end
  260.  
  261. local fast_mode = false
  262. for _, m in pairs(fast_mode_reqs) do
  263. fast_mode = fast_mode or m
  264. end
  265.  
  266. status_lines.fast_mode = "Fast scan: " .. bool_to_yn(fast_mode)
  267.  
  268. if fast_mode then sleep() else sleep(0.2) end
  269. end
  270. end
  271.  
  272.  
  273.  
  274. local function run_flight()
  275.  
  276. while true do
  277. while not user_meta do sleep() end
  278. local disp = flight_mode
  279. if (user_meta.acceleration and (user_meta.motionY < -0.3 and user_meta.acceleration.y < -0.1)) or (not user_meta.acceleration and user_meta.motionY < -0.3) then
  280. ni.launch(0, 270, 0.4)
  281. fast_mode_reqs.flying = true
  282. disp = disp .. " F"
  283. end
  284. fast_mode_reqs.flying = false
  285. if flight_mode == "std" or flight_mode == "hpower" or flight_mode == "lpower" or flight_mode == "align" then
  286. if user_meta.isElytraFlying or user_meta.isSneaking then
  287. fast_mode_reqs.flying = true
  288. end
  289. if user_meta.isElytraFlying ~= user_meta.isSneaking then
  290. if flight_mode == "align" then
  291. ni.look(user_meta.yaw, 0)
  292. end
  293. ni.launch(user_meta.yaw, (flight_mode ~= "align" and user_meta.pitch) or 10, flight_powers[flight_mode])
  294. end
  295. elseif flight_mode == "brake" then
  296. local y, p = calc_yaw_pitch(-user.velocity)
  297. ni.launch(y, p, math.min(user.velocity:length(), 1))
  298. fast_mode_reqs.flying = true
  299. end
  300. status_lines.flight = "Flight: " .. disp
  301. sleep(0.1)
  302. end
  303.  
  304. end
  305.  
  306. iron_hud()
  307. sleep(1)
  308. notification(types.ERROR, "TEST")
  309.  
  310. while true do
  311. local ok, err = pcall(parallel.waitForAny, scan_entities, run_flight)
  312. if err == "Terminated" then break end
  313. printError(err)
  314. sleep(0.2)
  315. iron_hud()
  316. end
  317.  
Add Comment
Please, Sign In to add comment