SpaceRanger4321

Notification

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