Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local username = "enes18enes"
- local interface = peripheral.wrap("back")
- local ni = peripheral.wrap("back")
- local w, h = ni.canvas().getSize()
- local canvas = interface.canvas()
- if _G.thing_group then
- pcall(_G.thing_group.remove)
- end
- local types {
- ["ERROR"] = "ERRORCOLOR",
- ["INFO"] = "INFOCOLOR",
- ["WARN"] = "WARNCOLOR"
- }
- local function notification(type, text)
- if type == types.ERROR then
- local notificationBox = canvas.addRectangle().getDocs()
- print(type)
- print(types.ERROR.. " color")
- print("notification text: " text)
- elseif type == types.INFO then
- local notificationBox = canvas.addRectangle(types.INFO)
- elseif type == types.WARN then
- local notificationBox = canvas.addRectangle(types.WARN)
- else
- notification(types.ERROR, "INVALID TYPE")
- end
- end
- local colortheme = {
- status = 0xFFFFFFFF,
- notice = 0xFF880000,
- follow = 0xFF00FFFF,
- watch = 0xFFFF00FF,
- lase = 0xFF0000FF,
- entity = 0x00FFFFFF
- }
- local group
- local function initialize_group_thing()
- if group then pcall(group.remove) end
- group = ni.canvas().addGroup({ w - 70, 10 })
- _G.thing_group = group
- end
- initialize_group_thing()
- local notices = {}
- local function push_notice(t)
- table.insert(notices, { t, os.epoch "utc" })
- end
- local function iron_hud()
- canvas.clear()
- local hudText = canvas.addRectangle(0, 0, 100, 100, 0x67C7EBFF)
- hudText.setSize(800, 15)
- hudText.setAlpha(124)
- local text = canvas.addText( { x = 220, y = 4 }, "JARVIS 1.2" )
- local ironLoad1 = canvas.addPolygon()
- end
- local user_meta
- local addressed_lasers = {}
- local fast_mode_reqs = {}
- local status_lines = {}
- local flight_mode = "std"
- local flight_shortcodes = {
- o = "off",
- b = "brake",
- h = "hpower",
- l = "lpower",
- s = "std",
- a = "align"
- }
- local flight_powers = {
- std = 1,
- lpower = 0.5,
- align = 1
- }
- local targets = {}
- local function is_target(name)
- for target, type in pairs(targets) do
- if name:lower():match(target) then return type end
- end
- end
- local function bool_to_yn(b)
- if b == true then return "y"
- elseif b == false then return "n"
- else return "?" end
- end
- local function scan_entities()
- local last_velocity
- local last_time
- while true do
- fast_mode_reqs.laser = false
- fast_mode_reqs.acting = false
- local entities = ni.sense()
- local maybe_players = {}
- local things = {}
- local notices = {}
- local lasers = {}
- user_meta = ni.getMetaByName(username)
- user_meta.motionY = user_meta.motionY + 0.07840001
- local v = vector.new(user_meta.motionX, user_meta.motionY, user_meta.motionZ)
- user_meta.velocity = v
- local time = os.epoch "utc"
- if last_velocity then
- user_meta.acceleration = (v - last_velocity) / (time - last_time) * 1000
- status_lines.acceleration = ("Acc: %.2f/%.2f"):format(user_meta.acceleration:length(), user_meta.acceleration.y)
- end
- last_velocity = v
- last_time = time
- status_lines.vel = ("Vel: %.2f/%.2f"):format(v:length(), user_meta.motionY)
- for _, entity in pairs(entities) do
- entity.s = vector.new(entity.x, entity.y, entity.z)
- entity.v = vector.new(entity.motionX, entity.motionY, entity.motionZ)
- if entity.displayName ~= username then
- things[entity.displayName] = (things[entity.displayName] or 0) + 1
- end
- 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
- entity.v = entity.v + vector.new(0, 0.0784, 0)
- table.insert(maybe_players, entity)
- end
- if entity.name == "plethora:laser" and not addressed_lasers[entity.id] then
- local closest_approach, param = project(entity.s, entity.v - v, vector.new(0, 0, 0))
- if param > 0 and vector_sqlength(closest_approach) < 5 then
- push_notice "Laser detected"
- fast_mode_reqs.laser = true
- local time_to_impact = (entity.s:length() / (entity.v - v):length()) / 20
- print("got inbound laser", time_to_impact, vector_sqlength(closest_approach), param)
- addressed_lasers[entity.id] = true
- schedule(function()
- --print("TODO: dodge laser")
- push_notice "Executing dodging"
- local dir2d = vector.new(entity.motionX - user_meta.motionX, 0, entity.motionZ - user_meta.motionZ)
- local perpendicular_dir2d = vector.new(1, 0, -dir2d.x / dir2d.z)
- local y, p = calc_yaw_pitch(perpendicular_dir2d)
- if math.random(1, 2) == 1 then p = -p end
- ni.launch(y, p, 3)
- end, math.max(0, time_to_impact / 2 - 0.1) * 1000)
- schedule(function() addressed_lasers[entity.id] = false end, 15000)
- table.insert(lasers, entity)
- end
- end
- end
- for _, laser in pairs(lasers) do
- for _, player in pairs(maybe_players) do
- local closest_approach, param = project(laser.s, laser.v, player.s)
- print(player.displayName, closest_approach, param)
- if param < 0 and vector_sqlength(closest_approach - player.s) < 8 and counterattack then
- print("execute counterattack", player.displayName)
- push_notice(("Counterattack %s"):format(player.displayName))
- targets[player.displayName:lower()] = "laser"
- end
- end
- end
- status_lines.counterattacks = "Counter: " .. bool_to_yn(counterattack)
- local i = 0
- local ok, err = pcall(group.clear)
- if not ok then
- initialize_group_thing()
- end
- local time = os.epoch "utc"
- for _, text in pairs(status_lines) do
- group.addText({ 0, i * 7 }, text, colortheme.status, 0.6)
- i = i + 1
- end
- for ix, info in pairs(notices) do
- if time >= (info[2] + 1000) then notices[ix] = nil end
- group.addText({ 0, i * 7 }, info[1], colortheme.notice, 0.6)
- i = i + 1
- end
- for thing, count in pairs(things) do
- local text = thing
- if count ~= 1 then text = text .. " " .. count end
- group.addText({ 0, i * 7 }, text, colortheme[is_target(thing) or "entity"], 0.6)
- i = i + 1
- end
- for _, entity in pairs(entities) do
- local action = is_target(entity.displayName)
- if action then
- if action == "laser" then
- schedule(function() lase(entity) end, 0, entity.id)
- elseif action == "watch" then
- schedule(function() ni.look(calc_yaw_pitch(entity.s)) end, 0, entity.id)
- elseif action == "follow" then
- schedule(function()
- local y, p = calc_yaw_pitch(entity.s)
- ni.launch(y, p, math.min(entity.s:length() / 24, 2))
- end, 0, entity.id)
- end
- fast_mode_reqs.acting = true
- end
- end
- local fast_mode = false
- for _, m in pairs(fast_mode_reqs) do
- fast_mode = fast_mode or m
- end
- status_lines.fast_mode = "Fast scan: " .. bool_to_yn(fast_mode)
- if fast_mode then sleep() else sleep(0.2) end
- end
- end
- local function run_flight()
- while true do
- while not user_meta do sleep() end
- local disp = flight_mode
- 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
- ni.launch(0, 270, 0.4)
- fast_mode_reqs.flying = true
- disp = disp .. " F"
- end
- fast_mode_reqs.flying = false
- if flight_mode == "std" or flight_mode == "hpower" or flight_mode == "lpower" or flight_mode == "align" then
- if user_meta.isElytraFlying or user_meta.isSneaking then
- fast_mode_reqs.flying = true
- end
- if user_meta.isElytraFlying ~= user_meta.isSneaking then
- if flight_mode == "align" then
- ni.look(user_meta.yaw, 0)
- end
- ni.launch(user_meta.yaw, (flight_mode ~= "align" and user_meta.pitch) or 10, flight_powers[flight_mode])
- end
- elseif flight_mode == "brake" then
- local y, p = calc_yaw_pitch(-user.velocity)
- ni.launch(y, p, math.min(user.velocity:length(), 1))
- fast_mode_reqs.flying = true
- end
- status_lines.flight = "Flight: " .. disp
- sleep(0.1)
- end
- end
- iron_hud()
- sleep(5)
- notification(types.ERROR, "TEST")
- while true do
- local ok, err = pcall(parallel.waitForAny, scan_entities, run_flight)
- if err == "Terminated" then break end
- printError(err)
- sleep(0.2)
- iron_hud()
- end
Add Comment
Please, Sign In to add comment