Advertisement
SpaceRanger4321

Stuff

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