Skip to content

Commit

Permalink
Port Towny to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerkiz committed Oct 22, 2024
1 parent 6042b6f commit fc137ca
Show file tree
Hide file tree
Showing 18 changed files with 585 additions and 627 deletions.
52 changes: 15 additions & 37 deletions maps/scrap_towny_ffa/biters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ local BiterHealthBooster = require 'modules.biter_health_booster_v2'
local Public = {}
local math_random = math.random
local math_floor = math.floor
local math_sqrt = math.sqrt
local math_round = math.round
local table_size = table.size
local table_insert = table.insert
local table_remove = table.remove
Expand All @@ -15,43 +13,17 @@ local table_shuffle = table.shuffle_table
local tick_schedule = {}
Global.register(
tick_schedule,
function(t)
function (t)
tick_schedule = t
end
)

local ScenarioTable = require 'maps.scrap_towny_ffa.table'
local Evolution = require 'maps.scrap_towny_ffa.evolution'

local function get_commmands(target, group)
local function get_commmands(target)
local commands = {}
local group_position = {x = group.position.x, y = group.position.y}
local step_length = 128

local target_position = target.position
local distance_to_target = math_floor(math_sqrt((target_position.x - group_position.x) ^ 2 + (target_position.y - group_position.y) ^ 2))
local steps = math_floor((distance_to_target - 27) / step_length) + 1
local vector = {math_round((target_position.x - group_position.x) / steps, 3), math_round((target_position.y - group_position.y) / steps, 3)}

for _ = 1, steps, 1 do
group_position.x = group_position.x + vector[1]
group_position.y = group_position.y + vector[2]
local position = group.surface.find_non_colliding_position('small-biter', group_position, step_length, 2)
if position then
commands[#commands + 1] = {
type = defines.command.go_to_location,
destination = {x = position.x, y = position.y},
radius = 16,
distraction = defines.distraction.by_damage
}
end
end

commands[#commands + 1] = {
type = defines.command.attack,
target = target,
distraction = defines.distraction.by_damage
}
commands[#commands + 1] = {
type = defines.command.attack_area,
destination = target.position,
Expand All @@ -64,6 +36,12 @@ local function get_commmands(target, group)
distraction = defines.distraction.by_damage
}

commands[#commands + 1] = {
type = defines.command.attack,
target = target,
distraction = defines.distraction.by_damage
}

return commands
end

Expand Down Expand Up @@ -199,7 +177,7 @@ function Public.swarm(town_center, radius)
end
tick_schedule[future][#tick_schedule[future] + 1] = {
callback = 'swarm',
params = {tc, r}
params = { tc, r }
}
return
end
Expand Down Expand Up @@ -248,7 +226,7 @@ function Public.swarm(town_center, radius)
if not unit_group_position then
return
end
local unit_group = surface.create_unit_group({position = unit_group_position, force = units[1].force})
local unit_group = surface.create_unit_group({ position = unit_group_position, force = units[1].force })

for key, unit in pairs(units) do
if key > count2 then
Expand All @@ -261,16 +239,16 @@ function Public.swarm(town_center, radius)
{
type = defines.command.compound,
structure_type = defines.compound_command.return_last,
commands = get_commmands(market, unit_group)
commands = get_commmands(market)
}
)
table_insert(this.swarms, {group = unit_group, timeout = game.tick + 36000})
table_insert(this.swarms, { group = unit_group, timeout = game.tick + 36000 })
end

local function on_unit_group_finished_gathering(event)
local unit_group = event.group
local position = unit_group.position
local entities = unit_group.surface.find_entities_filtered({position = position, radius = 256, name = 'market'})
local entities = unit_group.surface.find_entities_filtered({ position = position, radius = 256, name = 'market' })
local target = entities[1]
if target ~= nil then
local force = target.force
Expand All @@ -288,7 +266,7 @@ local function on_unit_group_finished_gathering(event)
{
type = defines.command.compound,
structure_type = defines.compound_command.return_last,
commands = get_commmands(target, unit_group)
commands = get_commmands(target)
}
)
end
Expand All @@ -308,7 +286,7 @@ local function on_tick()
tick_schedule[game.tick] = nil
end

local on_init = function()
local on_init = function ()
BiterHealthBooster.acid_nova(true)
BiterHealthBooster.check_on_entity_died(true)
end
Expand Down
30 changes: 15 additions & 15 deletions maps/scrap_towny_ffa/building.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ local neutral_whitelist = {
['long-handed-inserter'] = true,
['raw-fish'] = true,
['bulk-inserter'] = true,
['stack-inserter'] = true,
['steel-chest'] = true,
['tank'] = true,
['wooden-chest'] = true,
Expand Down Expand Up @@ -61,14 +60,15 @@ local function refund_item(event, item_name)
end

local function error_floaty(surface, position, msg)
surface.create_entity(
{
name = 'flying-text',
position = position,
text = msg,
color = { r = 0.77, g = 0.0, b = 0.0 }
}
)
for _, p in pairs(game.connected_players) do
if p.surface == surface then
p.create_local_flying_text({
position = position,
text = msg,
color = { r = 0.77, g = 0.0, b = 0.0 }
})
end
end
end

function Public.in_range(pos1, pos2, radius)
Expand Down Expand Up @@ -226,7 +226,7 @@ local function prevent_entity_in_restricted_zone(event)
if error == true then
if player_index ~= nil then
local player = game.get_player(player_index)
player.play_sound({ path = 'utility/cannot_build', position = player.position, volume_modifier = 0.75 })
player.play_sound({ path = 'utility/cannot_build', position = player.physical_position, volume_modifier = 0.75 })
end
error_floaty(surface, position, 'Can not build in restricted zone!')
end
Expand Down Expand Up @@ -256,7 +256,7 @@ local function prevent_landfill_in_restricted_zone(event)
if fail == true then
if player_index ~= nil then
local player = game.get_player(player_index)
player.play_sound({ path = 'utility/cannot_build', position = player.position, volume_modifier = 0.75 })
player.play_sound({ path = 'utility/cannot_build', position = player.physical_position, volume_modifier = 0.75 })
end
error_floaty(surface, position, 'Can not build in restricted zone!')
end
Expand All @@ -283,11 +283,11 @@ local function process_built_entities(event)
force = player.force
force_name = force.name

local is_another_character_near = Public.is_another_character_near(player.surface, player.position, force)
local is_another_character_near = Public.is_another_character_near(player.surface, player.physical_position, force)
if is_another_character_near then
entity.destroy()

player.play_sound({ path = 'utility/cannot_build', position = player.position, volume_modifier = 0.75 })
player.play_sound({ path = 'utility/cannot_build', position = player.physical_position, volume_modifier = 0.75 })
error_floaty(surface, position, "Can't build near other characters!")
if name ~= 'entity-ghost' then
if event.stack and event.stack.valid_for_read then
Expand Down Expand Up @@ -325,7 +325,7 @@ local function process_built_entities(event)
return
end

player.play_sound({ path = 'utility/cannot_build', position = player.position, volume_modifier = 0.75 })
player.play_sound({ path = 'utility/cannot_build', position = player.physical_position, volume_modifier = 0.75 })
end
error_floaty(surface, position, "Can't build near town!")
if name ~= 'entity-ghost' then
Expand Down Expand Up @@ -384,7 +384,7 @@ local function prevent_tiles_near_towns(event)
if fail == true then
if player_index ~= nil then
local player = game.get_player(player_index)
player.play_sound({ path = 'utility/cannot_build', position = player.position, volume_modifier = 0.75 })
player.play_sound({ path = 'utility/cannot_build', position = player.physical_position, volume_modifier = 0.75 })
end
error_floaty(surface, position, "Can't build near town!")
end
Expand Down
Loading

0 comments on commit fc137ca

Please sign in to comment.