Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rewrite of factorio mod turrets-range 1.1.1
require "defines"
global.active_turret_range_entities = {}
global.nearby_turret_map = {}
local function draw_turret_range(turret_type, sx, sy, r)
-- what is this voodoo?
local function round(x)
return x + 0.5 - (x + 0.5) % 1
end
local x = 0
local y = 0
local angle = 0
local drawmap = {}
local turret_range_entities = {}
sx = round(sx) - 1
sy = round(sy) - 1
-- would it be better to use the range instead of the type? (considering 3rd party mods)
if turret_type == "electric-turret" then
turret_type = "laser-turret-range-overlay"
else
turret_type = "gun-turret-range-overlay"
end
while angle < math.pi * 2 do
angle = angle + 0.03
x = round(math.cos(angle) * r + sx)
y = round(math.sin(angle) * r + sy)
if drawmap[x .. "x" .. y] == nil then
drawmap[x .. "x" .. y] = true
local indicator = game.get_surface(1).create_entity {
name = turret_type,
position = { x = x, y = y },
}
table.insert(turret_range_entities, indicator)
end
end
return turret_range_entities
end
local turret_whitelist = {
["electric-turret"] = true,
["ammo-turret"] = true,
-- ["turret"] = true,
}
local function turret_key(turret)
return turret.position.x .. "x" .. turret.position.y
end
-- adds turrets to the table `map`
local function map_turrets_arround_player(map, player, range)
range = range or 5
local area = {
{ player.position.x-range, player.position.y-range },
{ player.position.x+range, player.position.y+range }
}
-- if turret is selected, add it
if player.selected and turret_whitelist[player.selected.type] then
map[turret_key(player.selected)] = player.selected
end
-- find all turrets in a square of raidus `range` centered on the player
local surface = game.get_surface(1)
for name, _ in pairs(turret_whitelist) do
local turrets = surface.find_entities_filtered { area = area, type = name }
for i, turret in ipairs(turrets) do map[turret_key(turret)] = turret end -- use ipairs?
end
end
local function update_range_indicators()
local map = {}
local prev_map = global.nearby_turret_map or {}
-- create a `Set` of turrets near all players
for _, player in ipairs(game.players) do
map_turrets_arround_player(map, player)
end
-- draw all new turrets
for key, turret in pairs(map) do
if not prev_map[key] then
local entities = draw_turret_range(turret.type, turret.position.x, turret.position.y, turret.prototype.turret_range)
global.active_turret_range_entities[key] = entities
end
end
-- remove old turrets not in new map
for key, turret in pairs(prev_map) do
if not map[key] then
for _, entity in ipairs(global.active_turret_range_entities[key]) do
entity.destroy()
end
global.active_turret_range_entities[key] = nil
end
end
-- update map
global.nearby_turret_map = map
end
-- wire it up
game.on_event(defines.events.on_tick, function(event)
if event.tick % 10 == 0 then
-- game.player.print("TICK "..event.tick)
update_range_indicators()
end
end)
game.on_load(function()
-- reset old plugin's data
for i = 1, #global.turret_range_entities do
global.turret_range_entities[i].destroy()
end
global.turret_range_entities = {}
-- init?
if not global.active_turret_range_entities then
global.active_turret_range_entities = {}
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.