Feature Update (#237)

See PR for details, there are too many to be included here.
This commit is contained in:
2023-08-16 02:47:34 +09:00
committed by GitHub
parent cdd34ebaea
commit 46f6215d94
64 changed files with 4417 additions and 289 deletions

View File

@@ -12,7 +12,7 @@ end)
-- Apply an offset to a LuaPosition
local function apply_offset(position, offset)
return { x = position.x + (offset.x or offset[1]), y = position.y + (offset.y or offset[2]) }
return {x=position.x + (offset.x or offset[1]), y=position.y + (offset.y or offset[2])}
end
-- Apply the offset to the turrets default position
@@ -22,23 +22,33 @@ end
-- Get or create the force used for entities in spawn
local function get_spawn_force()
local force = game.forces['Spawn']
if force and force.valid then return force end
force = game.create_force('Spawn')
local force = game.forces['spawn']
if force and force.valid then
return force
end
force = game.create_force('spawn')
force.set_cease_fire('player', true)
game.forces['player'].set_cease_fire('Spawn', true)
-- force.set_friend('player', true)
game.forces['player'].set_cease_fire('spawn', true)
-- game.forces['player'].set_friend('spawn', true)
return force
end
-- Protects an entity and sets its force to the spawn force
-- Protects an entity
-- and sets its force to the spawn force
local function protect_entity(entity, set_force)
if entity and entity.valid then
entity.destructible = false
entity.minable = false
entity.rotatable = false
entity.operable = false
if not set_force then entity.health = 0 end
if set_force then entity.force = get_spawn_force() end
if set_force then
entity.force = get_spawn_force()
end
end
end
@@ -51,8 +61,8 @@ local function spawn_turrets()
-- Makes a new turret if it is not found
if not turret or not turret.valid then
turret = surface.create_entity{name='gun-turret', position=pos, force='Spawn'}
protect_entity(turret, true)
turret = surface.create_entity{name='gun-turret', position=pos, force='spawn'}
protect_entity(turret)
end
-- Adds ammo to the turret
@@ -68,12 +78,16 @@ local function spawn_belts(surface, position)
position = apply_offset(position, config.afk_belts.offset)
local belt_type = config.afk_belts.belt_type
local belt_details = {{-0.5, -0.5, 2}, {0.5, -0.5, 4}, {-0.5, 0.5, 0}, {0.5, 0.5, 6}} -- x, y,dir
for _, belt_set in pairs(config.afk_belts.locations) do
local set_position = apply_offset(position, belt_set)
for _, belt in pairs(belt_details) do
local pos = apply_offset(set_position, belt)
local belt_entity = surface.create_entity{name=belt_type, position=pos, force='neutral', direction=belt[3]}
if config.afk_belts.protected then protect_entity(belt_entity) end
local belt_entity = surface.create_entity{name=belt_type, position=pos, force='spawn', direction=belt[3]}
if config.afk_belts.protected then
protect_entity(belt_entity)
end
end
end
end
@@ -83,9 +97,11 @@ local function spawn_pattern(surface, position)
position = apply_offset(position, config.pattern.offset)
local tiles_to_make = {}
local pattern_tile = config.pattern.pattern_tile
for _, tile in pairs(config.pattern.locations) do
table.insert(tiles_to_make, {name=pattern_tile, position=apply_offset(position, tile)})
end
surface.set_tiles(tiles_to_make)
end
@@ -104,9 +120,13 @@ end
local function spawn_entities(surface, position)
position = apply_offset(position, config.entities.offset)
for _, entity in pairs(config.entities.locations) do
local pos = apply_offset(position, { x=entity[2], y=entity[3] })
local pos = apply_offset(position, {x=entity[2], y=entity[3]})
entity = surface.create_entity{name=entity[1], position=pos, force='neutral'}
if config.entities.protected then protect_entity(entity) end
if config.entities.protected then
protect_entity(entity)
end
entity.operable = config.entities.operable
end
end
@@ -114,7 +134,8 @@ end
-- Generates an area with no water or entities, no water area is larger
local function spawn_area(surface, position)
local dr = config.spawn_area.deconstruction_radius
local dr2 = dr^2
local tr = config.spawn_area.tile_radius
local tr2 = tr^2
local decon_tile = config.spawn_area.deconstruction_tile
local fr = config.spawn_area.landfill_radius
@@ -132,7 +153,7 @@ local function spawn_area(surface, position)
local y2 = (y+0.5)^2
local dst = x2+y2
local pos = {x=position.x+x, y=position.y+y}
if dst < dr2 then
if dst < tr2 then
-- If it is inside the decon radius always set the tile
table.insert(tiles_to_make, {name=decon_tile, position=pos})
elseif dst < fr2 and surface.get_tile(pos).collides_with('player-layer') then
@@ -144,10 +165,34 @@ local function spawn_area(surface, position)
-- Remove entities then set the tiles
local entities_to_remove = surface.find_entities_filtered{position=position, radius=dr, name='character', invert=true}
for _, entity in pairs(entities_to_remove) do entity.destroy() end
for _, entity in pairs(entities_to_remove) do
entity.destroy()
end
surface.set_tiles(tiles_to_make)
end
local function spawn_resource_tiles(surface)
for _, v in ipairs(config.resource_tiles.resources) do
if v.enabled then
for x=v.offset[1], v.offset[1] + v.size[1] do
for y=v.offset[2], v.offset[2] + v.size[2] do
surface.create_entity({name=v.name, amount=v.amount, position={x, y}})
end
end
end
end
end
local function spawn_resource_patches(surface)
for _, v in ipairs(config.resource_patches.resources) do
if v.enabled then
for i=1, v.num_patches do
surface.create_entity({name=v.name, amount=v.amount, position={v.offset[1] + v.offset_next[1] * (i - 1), v.offset[2] + v.offset_next[2] * (i - 1)}})
end
end
end
end
-- Only add a event handler if the turrets are enabled
if config.turrets.enabled then
Event.on_nth_tick(config.turrets.refill_time, function()
@@ -156,6 +201,19 @@ if config.turrets.enabled then
end)
end
if config.resource_refill_nearby.enabled then
-- could have a flag in global that early returns if true, and reset it on_tick
Event.on_nth_tick(36000, function()
if game.tick < 10 then
return
end
for _, ore in pairs(game.players[1].surface.find_entities_filtered{position=game.players[1].force.get_spawn_position(game.players[1].surface), radius=config.resource_refill_nearby.range, name=config.resource_refill_nearby.resources_name}) do
ore.amount = ore.amount + math.random(config.resource_refill_nearby.amount[1], config.resource_refill_nearby.amount[2])
end
end)
end
-- When the first player joins create the spawn area
Event.add(defines.events.on_player_created, function(event)
if event.player_index ~= 1 then return end
@@ -169,8 +227,10 @@ Event.add(defines.events.on_player_created, function(event)
if config.afk_belts.enabled then spawn_belts(s, p) end
if config.turrets.enabled then spawn_turrets() end
if config.entities.enabled then spawn_entities(s, p) end
if config.resource_tiles.enabled then spawn_resource_tiles(s) end
if config.resource_patches.enabled then spawn_resource_patches(s) end
player.teleport(p, s)
end)
-- Way to access global table
return turrets
return turrets