Skip to content

Instantly share code, notes, and snippets.

@jsimmons
Created July 20, 2018 18:44
Show Gist options
  • Save jsimmons/3aba36b5d0f7292871f70cac3303566e to your computer and use it in GitHub Desktop.
Save jsimmons/3aba36b5d0f7292871f70cac3303566e to your computer and use it in GitHub Desktop.
local function clear_tile_teams()
for i = 1, conf_map_num do
local tile = g_map[i]
local tile_type = tile.tile_type
if tile_type ~= 'water' and
tile_type ~= 'bridge' and
tile_type ~= 'rocks' and
tile_type ~= 'building' then
tile.team = nil
end
end
end
local flood_fill
flood_fill = function(x, y, team, touched)
local tile = get_tile(x, y)
if tile == nil then return end
local tile_type = tile.tile_type
if tile_type == 'water' or
tile_type == 'bridge' or
tile_type == 'rocks' then
return
end
if touched[tile] then return end
touched[tile] = true
if tile_type ~= 'building' then
tile.team = team
end
local flood_fill = flood_fill
flood_fill(x - 1, y - 1, team, touched)
flood_fill(x + 1, y + 1, team, touched)
flood_fill(x + 1, y - 1, team, touched)
flood_fill(x - 1, y + 1, team, touched)
flood_fill(x , y - 1, team, touched)
flood_fill(x , y + 1, team, touched)
flood_fill(x - 1, y , team, touched)
return flood_fill(x + 1, y, team, touched)
end
local function recalculate_tile_teams()
clear_tile_teams()
local touched = {}
for i, building in ipairs(g_buildings) do
if building.building_type == 'keep' then
flood_fill(building.x, building.y, building.team, touched)
end
end
touched = {}
for i = 1, #g_water_border_indexes do
local x, y = make_xy(g_water_border_indexes[i])
flood_fill(x, y, nil, touched)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment