Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local username = "gollark"
- local ni = peripheral.wrap "back"
- local w, h = ni.canvas().getSize()
- if _G.thing_group then
- pcall(_G.thing_group.remove)
- end
- 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 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 vector_sqlength(self)
- return self.x * self.x + self.y * self.y + self.z * self.z
- end
- local function project(line_start, line_dir, point)
- local t = (point - line_start):dot(line_dir) / vector_sqlength(line_dir)
- return line_start + line_dir * t, t
- end
- local function calc_yaw_pitch(v)
- local x, y, z = v.x, v.y, v.z
- local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
- local yaw = math.atan2(-x, z)
- return math.deg(yaw), math.deg(pitch)
- end
- local counterattack = false
- local addressed_lasers = {}
- local timers = {}
- local function bool_to_yn(b)
- if b == true then return "y"
- elseif b == false then return "n"
- else return "?" end
- end
- local laser_power = 5
- local status_lines = {}
- local notices = {}
- local function push_notice(t)
- table.insert(notices, { t, os.epoch "utc" })
- end
- local function lase(entity)
- local target_location = entity.s
- for i = 1, 5 do
- target_location = entity.s + entity.v * (target_location:length() / 1.5)
- end
- local y, p = calc_yaw_pitch(target_location)
- ni.fire(y, p, laser_power)
- end
- local user_meta
- local fast_mode_reqs = {}
- local function scan_entities()
- 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)
- 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
- timers[os.startTimer(math.max(0, time_to_impact / 2 - 0.1))] = { "dodge", entity }
- timers[os.startTimer(15)] = { "clear", entity.id }
- 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))
- --lase(player, 5)
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement