1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-04-23 11:58:39 +02:00

Merge pull request #2 from ComfyFactory/planet_prison_fix

planet prison fix
This commit is contained in:
Gerkiz 2021-03-21 23:33:45 +01:00 committed by GitHub
commit 77c4a13783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 2616 additions and 2570 deletions

View File

@ -107,6 +107,9 @@ require 'modules.autostash'
--![[East VS West Survival PVP, where you breed biters with science flasks]]-- --![[East VS West Survival PVP, where you breed biters with science flasks]]--
--require 'maps.biter_hatchery.main' --require 'maps.biter_hatchery.main'
--![[Fight in a world where everyone are prisoners]]
--require 'maps.planet_prison'
--![[Chop trees to gain resources]]-- --![[Chop trees to gain resources]]--
--require 'maps.choppy' --require 'maps.choppy'
--require 'maps.choppy_dx' --require 'maps.choppy_dx'
@ -201,7 +204,6 @@ require 'modules.autostash'
--require 'maps.wave_defense' --require 'maps.wave_defense'
--require 'maps.crossing' --require 'maps.crossing'
--require 'maps.anarchy' --require 'maps.anarchy'
--require 'maps.planet_prison'
--require 'maps.blue_beach' --require 'maps.blue_beach'
--require 'maps.nightfall' --require 'maps.nightfall'
--require 'maps.pitch_black.main' --require 'maps.pitch_black.main'

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
return '{"blueprint":{"icons":[{"signal":{"type":"item","name":"crash-site-lab-repaired"},"index":1},{"signal":{"type":"item","name":"crash-site-generator"},"index":2}],"entities":[{"entity_number":1,"name":"crash-site-generator","position":{"x":3,"y":-3.5}},{"entity_number":2,"name":"crash-site-lab-repaired","position":{"x":-2,"y":-1}},{"entity_number":3,"name":"crash-site-chest-1","position":{"x":4,"y":-1}},{"entity_number":4,"name":"crash-site-chest-2","position":{"x":-3,"y":3}},{"entity_number":5,"name":"crash-site-assembling-machine-1-repaired","position":{"x":-2,"y":-5}}],"item":"blueprint","version":73019621376}}' return '{"blueprint":{"icons":[{"signal":{"type":"item","name":"crash-site-lab-repaired"},"index":1}],"entities":[{"entity_number":2,"name":"crash-site-chest-1","position":{"x":4,"y":-1}},{"entity_number":3,"name":"crash-site-chest-2","position":{"x":-3,"y":3}}],"item":"blueprint","version":73019621376}}'

File diff suppressed because it is too large Load Diff

View File

@ -1,187 +1,182 @@
local public = {} local public = {}
local _common = require(".common") local _common = require('.common')
public.command = { public.command = {
--[[ --[[
@param args nil @param args nil
--]] --]]
noop = 0, noop = 0,
--[[
--[[
@param args nil @param args nil
--]] --]]
seek_and_destroy_player = 1, seek_and_destroy_player = 1,
--[[
--[[
@param args = { @param args = {
agents, // All movable agents agents, // All movable agents
positions, // Table of positions to attack positions, // Table of positions to attack
} }
--]] --]]
attack_objects = 2 attack_objects = 2
} }
--[[ --[[
init - Initialize the module. init - Initialize the module.
--]] --]]
public.init = function() public.init = function()
if global.this == nil then
global.this = {}
end
end end
local function _get_direction(src, dest) local function _get_direction(src, dest)
local src_x = _common.get_axis(src, "x") local src_x = _common.get_axis(src, 'x')
local src_y = _common.get_axis(src, "y") local src_y = _common.get_axis(src, 'y')
local dest_x = _common.get_axis(dest, "x") local dest_x = _common.get_axis(dest, 'x')
local dest_y = _common.get_axis(dest, "y") local dest_y = _common.get_axis(dest, 'y')
local step = { local step = {
x = nil, x = nil,
y = nil y = nil
} }
local precision = _common.rand_range(1, 10) local precision = _common.rand_range(1, 10)
if dest_x - precision > src_x then if dest_x - precision > src_x then
step.x = 1 step.x = 1
elseif dest_x < src_x - precision then elseif dest_x < src_x - precision then
step.x = -1 step.x = -1
else else
step.x = 0 step.x = 0
end end
if dest_y - precision > src_y then if dest_y - precision > src_y then
step.y = 1 step.y = 1
elseif dest_y < src_y - precision then elseif dest_y < src_y - precision then
step.y = -1 step.y = -1
else else
step.y = 0 step.y = 0
end end
return _common.direction_lookup[step.x][step.y] return _common.direction_lookup[step.x][step.y]
end end
local function _move_to(ent, trgt, min_distance) local function _move_to(ent, trgt, min_distance)
local state = { local state = {
walking = false, walking = false
} }
local distance = _common.get_distance(trgt.position, ent.position) local distance = _common.get_distance(trgt.position, ent.position)
if min_distance < distance then if min_distance < distance then
local dir = _get_direction(ent.position, trgt.position) local dir = _get_direction(ent.position, trgt.position)
if dir then if dir then
state = { state = {
walking = true, walking = true,
direction = dir direction = dir
} }
end end
end end
ent.walking_state = state ent.walking_state = state
return state.walking return state.walking
end end
local function _shoot_at(ent, trgt) local function _shoot_at(ent, trgt)
ent.shooting_state = { ent.shooting_state = {
state = defines.shooting.shooting_enemies, state = defines.shooting.shooting_enemies,
position = trgt.position position = trgt.position
} }
end end
local function _shoot_stop(ent) local function _shoot_stop(ent)
ent.shooting_state = { ent.shooting_state = {
state = defines.shooting.not_shooting, state = defines.shooting.not_shooting,
position = {0, 0} position = {0, 0}
} }
end end
local function _do_job_seek_and_destroy_player(surf) local function _do_job_seek_and_destroy_player(surf)
for _, player in pairs(game.players) do for _, player in pairs(game.players) do
if player.character == nil then if player.character == nil then
goto continue goto continue
end end
local search_info = { local search_info = {
name = "character", name = 'character',
position = player.character.position, position = player.character.position,
radius = 20, radius = 20,
force = "enemy", force = 'enemy'
} }
local ents = surf.find_entities_filtered(search_info) local ents = surf.find_entities_filtered(search_info)
if not ents or #ents == 0 then if not ents or #ents == 0 then
goto continue goto continue
end end
for _, e in pairs(ents) do for _, e in pairs(ents) do
if not _move_to(e, player.character, _common.rand_range(5, 10)) then if not _move_to(e, player.character, _common.rand_range(5, 10)) then
_shoot_at(e, player.character) _shoot_at(e, player.character)
else else
_shoot_stop(e) _shoot_stop(e)
end end
end end
::continue:: ::continue::
end end
end end
local function _do_job_attack_objects(surf, args) local function _do_job_attack_objects(surf, args)
local agents = args.agents local agents = args.agents
if #agents == 0 then if #agents == 0 then
return return
end end
local objects = args.objects local objects = args.objects
local target, closest, agent, query local target, closest, agent, query
for i = #agents, 1, -1 do for i = #agents, 1, -1 do
agent = agents[i] agent = agents[i]
if not agent.valid then if not agent.valid then
table.remove(agents, i) table.remove(agents, i)
goto continue
end
if game.tick % i ~= 0 then
goto continue
end
query = {
position = agent.position,
radius = 15,
type = {
"projectile",
"beam"
},
force = {
"enemy",
"player",
"neutral",
},
invert = true
}
closest = surf.find_entities_filtered(query)
if #closest ~= 0 then
target = _common.get_closest_neighbour(agent.position, closest)
else
if #objects == 0 then
_shoot_stop(agent)
goto continue goto continue
end end
target = _common.get_closest_neighbour(agent.position, objects) if game.tick % i ~= 0 then
end goto continue
end
if target == nil or not target.valid then query = {
goto continue position = agent.position,
end radius = 15,
type = {
'projectile',
'beam'
},
force = {
'enemy',
'player',
'neutral'
},
invert = true
}
closest = surf.find_entities_filtered(query)
if #closest ~= 0 then
target = _common.get_closest_neighbour(agent.position, closest)
else
if #objects == 0 then
_shoot_stop(agent)
goto continue
end
if not _move_to(agent, target, _common.rand_range(5, 15)) then target = _common.get_closest_neighbour(agent.position, objects)
_shoot_at(agent, target) end
else
_shoot_stop(agent)
end
::continue:: if target == nil or not target.valid then
end goto continue
end
if not _move_to(agent, target, _common.rand_range(5, 15)) then
_shoot_at(agent, target)
else
_shoot_stop(agent)
end
::continue::
end
end end
--[[ --[[
@ -190,15 +185,15 @@ do_job - Perform non-stateful operation on all enemy "character" entities.
@param command - Command to perform on all non-player controllable characters. @param command - Command to perform on all non-player controllable characters.
--]] --]]
public.do_job = function(surf, command, args) public.do_job = function(surf, command, args)
if args == nil then if args == nil then
args = {} args = {}
end end
if command == public.command.seek_and_destroy_player then if command == public.command.seek_and_destroy_player then
_do_job_seek_and_destroy_player(surf) _do_job_seek_and_destroy_player(surf)
elseif command == public.command.attack_objects then elseif command == public.command.attack_objects then
_do_job_attack_objects(surf, args) _do_job_attack_objects(surf, args)
end end
end end
return public return public

View File

@ -1,12 +1,19 @@
local public = {} local public = {}
local _common = require(".common") local _common = require('.common')
local Global = require 'utils.global'
local Token = require 'utils.token'
local this = {}
Global.register(
this,
function(tbl)
this = tbl
end
)
public.init = function() public.init = function()
if global.this == nil then this._bps = {}
global.this = {}
end
global.this._bps = {}
end end
--[[ --[[
@ -15,12 +22,12 @@ push_blueprint - Pushes blueprint into a list.
@param bp - Blueprint in JSON format. @param bp - Blueprint in JSON format.
--]] --]]
public.push_blueprint = function(name, bp) public.push_blueprint = function(name, bp)
local entry = { local entry = {
bp = game.json_to_table(bp).blueprint, bp = game.json_to_table(bp).blueprint,
hook = nil, hook = nil,
refs = {} refs = {}
} }
global.this._bps[name] = entry this._bps[name] = entry
end end
--[[ --[[
@ -29,17 +36,19 @@ set_blueprint_hook - Set callback to a blueprint.
@param hook - Callback that will be called after blueprint is placed. @param hook - Callback that will be called after blueprint is placed.
--]] --]]
public.set_blueprint_hook = function(name, hook) public.set_blueprint_hook = function(name, hook)
if name == nil then if name == nil then
log("bp.set_blueprint_hook: name is nil") log('bp.set_blueprint_hook: name is nil')
return return
end end
if global.this._bps[name] == nil then if this._bps[name] == nil then
log("bp.set_blueprint_hook: unrecognized blueprint") log('bp.set_blueprint_hook: unrecognized blueprint')
return return
end end
global.this._bps[name].hook = hook local token = Token.register(hook)
this._bps[name].hook = token
end end
--[[ --[[
@ -47,18 +56,18 @@ get_references - Get all references of the blueprint on the map.
@param name - Blueprint handle. @param name - Blueprint handle.
--]] --]]
public.get_references = function(name) public.get_references = function(name)
if name == nil then if name == nil then
log("bp.get_references: name is nil") log('bp.get_references: name is nil')
return {} return {}
end end
local object = global.this._bps[name] local object = this._bps[name]
if object == nil then if object == nil then
log("bp.get_references: unrecognized blueprint") log('bp.get_references: unrecognized blueprint')
return {} return {}
end end
return object.refs return object.refs
end end
--[[ --[[
@ -66,18 +75,18 @@ get_references - Gets opaque object representing bp references.
@param name - Blueprint handle. @param name - Blueprint handle.
--]] --]]
public.get_references = function(name) public.get_references = function(name)
if name == nil then if name == nil then
log("bp.get_references: name is nil") log('bp.get_references: name is nil')
return return
end end
local object = global.this._bps[name] local object = this._bps[name]
if object == nil then if object == nil then
log("bp.get_references: unrecognized blueprint") log('bp.get_references: unrecognized blueprint')
return return
end end
return object.refs return object.refs
end end
--[[ --[[
@ -85,7 +94,7 @@ reference_get_bounding_box - Return bounding box from the reference.
@param reference - Valid reference object fetched from get_references. @param reference - Valid reference object fetched from get_references.
--]] --]]
public.reference_get_bounding_box = function(reference) public.reference_get_bounding_box = function(reference)
return reference.bb return reference.bb
end end
--[[ --[[
@ -93,7 +102,7 @@ reference_get_entities - Return references to entities.
@param reference - Valid reference object fetched from get_references. @param reference - Valid reference object fetched from get_references.
--]] --]]
public.reference_get_entities = function(reference) public.reference_get_entities = function(reference)
return reference.entities return reference.entities
end end
--[[ --[[
@ -101,10 +110,9 @@ reference_get_timestamp - Return timestamp of a reference
@param reference - Valid reference object fetched from get_references. @param reference - Valid reference object fetched from get_references.
--]] --]]
public.reference_get_timestamp = function(reference) public.reference_get_timestamp = function(reference)
return reference.timestamp return reference.timestamp
end end
--[[ --[[
unlink_references_filtered - Unlinks all references of blueprint on the map if they unlink_references_filtered - Unlinks all references of blueprint on the map if they
meet the query rules. meet the query rules.
@ -115,32 +123,32 @@ unlinked.
@return An array of unlinked references. @return An array of unlinked references.
--]] --]]
public.unlink_references_filtered = function(name, query) public.unlink_references_filtered = function(name, query)
if name == nil then if name == nil then
log("bp.get_references: name is nil") log('bp.get_references: name is nil')
return return
end end
local object = global.this._bps[name] local object = this._bps[name]
if object == nil then if object == nil then
log("bp.get_references: unrecognized blueprint") log('bp.get_references: unrecognized blueprint')
return return
end end
local refs = {} local refs = {}
for i = #object.refs, 1, -1 do for i = #object.refs, 1, -1 do
local ref = object.refs[i] local ref = object.refs[i]
if query and query.timestamp then if query and query.timestamp then
if ref.timestamp > query.timestamp then if ref.timestamp > query.timestamp then
goto continue goto continue
end end
end end
table.insert(refs, ref) table.insert(refs, ref)
table.remove(object.refs, i) table.remove(object.refs, i)
::continue:: ::continue::
end end
return refs return refs
end end
--[[ --[[
@ -153,42 +161,42 @@ meet the query rules.
removed. removed.
--]] --]]
public.destroy_references_filtered = function(surf, name, query) public.destroy_references_filtered = function(surf, name, query)
if name == nil then if name == nil then
log("bp.get_references: name is nil") log('bp.get_references: name is nil')
return return
end end
local object = global.this._bps[name] local object = this._bps[name]
if object == nil then if object == nil then
log("bp.get_references: unrecognized blueprint") log('bp.get_references: unrecognized blueprint')
return return
end end
for i = 1, #object.refs do for i = 1, #object.refs do
local ref = object.refs[i] local ref = object.refs[i]
if query and query.timestamp then if query and query.timestamp then
if ref.timestamp > query.timestamp then if ref.timestamp > query.timestamp then
goto continue goto continue
end end
end end
for _, ent in pairs(ref.entities) do for _, ent in pairs(ref.entities) do
if ent.valid then if ent.valid then
ent.destroy() ent.destroy()
end end
end end
local tiles = {} local tiles = {}
for _, tile in pairs(ref.tiles) do for _, tile in pairs(ref.tiles) do
tile.name = "concrete" tile.name = 'concrete'
table.insert(tiles, tile) table.insert(tiles, tile)
end end
surf.set_tiles(tiles) surf.set_tiles(tiles)
table.remove(object.refs, i) table.remove(object.refs, i)
::continue:: ::continue::
end end
end end
--[[ --[[
@ -197,28 +205,28 @@ destroy_references - Destroys all references of blueprint on the map
@param name - Blueprint handle. @param name - Blueprint handle.
--]] --]]
public.destroy_references = function(surf, name) public.destroy_references = function(surf, name)
public.destroy_references_filtered(surf, name, {}) public.destroy_references_filtered(surf, name, {})
end end
global._bp_destroy_reference = function(surf, ref) local _bp_destroy_reference = function(surf, ref)
for _, ent in pairs(ref.entities) do for _, ent in pairs(ref.entities) do
if ent.valid then if ent.valid then
ent.destroy() ent.destroy()
end end
end end
local tiles = {} local tiles = {}
for _, tile in pairs(ref.tiles) do for _, tile in pairs(ref.tiles) do
if tile.valid then if tile.valid then
goto continue goto continue
end end
tile.name = "concrete" tile.name = 'concrete'
table.insert(tiles, tile) table.insert(tiles, tile)
::continue:: ::continue::
end end
surf.set_tiles(tiles) surf.set_tiles(tiles)
end end
--[[ --[[
@ -227,64 +235,64 @@ destroy_reference - Destroys reference of a blueprint at given surface.
@param reference - Any valid reference. @param reference - Any valid reference.
--]] --]]
public.destroy_reference = function(surf, reference) public.destroy_reference = function(surf, reference)
for _, meta in pairs(global.this._bps) do for _, meta in pairs(this._bps) do
for i = 1, #meta.refs do for i = 1, #meta.refs do
local ref = meta.refs[i] local ref = meta.refs[i]
if reference.id == ref.id then if reference.id == ref.id then
global._bp_destroy_reference(surf, ref) _bp_destroy_reference(surf, ref)
table.remove(meta.refs, i) table.remove(meta.refs, i)
return return
end end
end end
end end
end end
local function _build_tiles(surf, point, tiles) local function _build_tiles(surf, point, tiles)
local _tiles = {} local _tiles = {}
local get_axis = _common.get_axis local get_axis = _common.get_axis
for _, tile in pairs(tiles) do for _, tile in pairs(tiles) do
local _tile = { local _tile = {
name = tile.name, name = tile.name,
position = { position = {
x = get_axis(tile.position, "x") + get_axis(point, "x"), x = get_axis(tile.position, 'x') + get_axis(point, 'x'),
y = get_axis(tile.position, "y") + get_axis(point, "y") y = get_axis(tile.position, 'y') + get_axis(point, 'y')
} }
} }
table.insert(_tiles, _tile) table.insert(_tiles, _tile)
end end
surf.set_tiles(_tiles) surf.set_tiles(_tiles)
return _tiles return _tiles
end end
local function _build_entities(surf, point, entities, hook, args) local function _build_entities(surf, point, entities, hook, args)
local _entities = {} local _entities = {}
local get_axis = _common.get_axis local get_axis = _common.get_axis
for _, ent in pairs(entities) do for _, ent in pairs(entities) do
local ent_info = { local ent_info = {
position = { position = {
x = get_axis(ent.position, "x") + get_axis(point, "x"), x = get_axis(ent.position, 'x') + get_axis(point, 'x'),
y = get_axis(ent.position, "y") + get_axis(point, "y") y = get_axis(ent.position, 'y') + get_axis(point, 'y')
}, },
name = ent.name, name = ent.name
} }
local e = surf.create_entity(ent_info) local e = surf.create_entity(ent_info)
if not e or not e.valid then if not e or not e.valid then
goto continue goto continue
end end
if hook then if hook then
hook(e, args) local token = Token.get(hook)
end token(e, args)
end
table.insert(_entities, e) table.insert(_entities, e)
::continue:: ::continue::
end end
return _entities return _entities
end end
--[[ --[[
@ -295,79 +303,79 @@ build - Place blueprint at given point.
@param args - If hook was set, this will be argument passed. @param args - If hook was set, this will be argument passed.
--]] --]]
public.build = function(surf, name, point, args) public.build = function(surf, name, point, args)
if surf == nil then if surf == nil then
log("bp.build: surf is nil") log('bp.build: surf is nil')
return return
end end
if name == nil then if name == nil then
log("bp.build: name is nil") log('bp.build: name is nil')
return return
end end
local object = global.this._bps[name] local object = this._bps[name]
if object == nil then if object == nil then
log("bp.set_blueprint_hook: unrecognized blueprint") log('bp.set_blueprint_hook: unrecognized blueprint')
return return
end end
local instance = { local instance = {
entities = {}, entities = {},
tiles = {}, tiles = {},
bb = nil, bb = nil,
timestamp = game.tick, timestamp = game.tick
} }
local bbs = {} local bbs = {}
local tiles = object.bp.tiles local tiles = object.bp.tiles
if tiles and #tiles > 0 then if tiles and #tiles > 0 then
instance.tiles = _build_tiles(surf, point, tiles) instance.tiles = _build_tiles(surf, point, tiles)
local bb = _common.create_bounding_box_by_points(instance.tiles) local bb = _common.create_bounding_box_by_points(instance.tiles)
table.insert(bbs, bb) table.insert(bbs, bb)
local query = { local query = {
name = "character", name = 'character',
area = bb, area = bb,
invert = true, invert = true
} }
for _, ent in pairs(surf.find_entities_filtered(query)) do for _, ent in pairs(surf.find_entities_filtered(query)) do
if ent.valid then if ent.valid then
ent.destroy() ent.destroy()
end
end
end
local entities = object.bp.entities
if entities and #entities > 0 then
instance.entities = _build_entities(surf, point, entities, object.hook, args)
local bb = _common.create_bounding_box_by_points(instance.entities)
table.insert(bbs, bb)
local query = {
name = "character",
area = bb,
invert = true,
}
for _, ent_found in pairs(surf.find_entities_filtered(query)) do
if not ent_found.valid then
goto continue
end
for _, ent_spawned in pairs(instance.entities) do
if ent_found == ent_spawned then
goto continue
end end
end end
end
ent_found.die() local entities = object.bp.entities
::continue:: if entities and #entities > 0 then
end instance.entities = _build_entities(surf, point, entities, object.hook, args)
end local bb = _common.create_bounding_box_by_points(instance.entities)
table.insert(bbs, bb)
instance.bb = _common.merge_bounding_boxes(bbs) local query = {
instance.id = game.tick name = 'character',
table.insert(object.refs, instance) area = bb,
invert = true
}
for _, ent_found in pairs(surf.find_entities_filtered(query)) do
if not ent_found.valid then
goto continue
end
return instance for _, ent_spawned in pairs(instance.entities) do
if ent_found == ent_spawned then
goto continue
end
end
ent_found.die()
::continue::
end
end
instance.bb = _common.merge_bounding_boxes(bbs)
instance.id = game.tick
table.insert(object.refs, instance)
return instance
end end
return public return public

View File

@ -1,5 +1,15 @@
local public = {} local public = {}
local common = require(".common") local common = require('.common')
local Global = require 'utils.global'
local this = {}
Global.register(
this,
function(tbl)
this = tbl
end
)
--[[ --[[
init - Initialize claim system. init - Initialize claim system.
@ -7,89 +17,85 @@ init - Initialize claim system.
@param max_distance - Maximal distance allowed between markers @param max_distance - Maximal distance allowed between markers
--]] --]]
public.init = function(names, max_distance) public.init = function(names, max_distance)
if global.this == nil then if type(names) ~= 'table' then
global.this = {} names = {names}
end end
if type(names) ~= "table" then this._claims_info = {}
names = { names } this._claims_visible_to = {}
end this._claim_markers = names
this._claim_max_dist = max_distance
global.this._claims_info = {}
global.this._claims_visible_to = {}
global.this._claim_markers = names
global.this._claim_max_dist = max_distance
end end
local function claim_new_claim(ent, deps) local function claim_new_claim(ent, deps)
local comm = deps.common local comm = deps.common
local point = { local point = {
{ {
x = comm.get_axis(ent.position, "x"), x = comm.get_axis(ent.position, 'x'),
y = comm.get_axis(ent.position, "y"), y = comm.get_axis(ent.position, 'y')
} }
} }
local claims = global.this._claims_info local claims = this._claims_info
if claims[ent.force.name] == nil then if claims[ent.force.name] == nil then
claims[ent.force.name] = {} claims[ent.force.name] = {}
claims[ent.force.name].polygons = {} claims[ent.force.name].polygons = {}
claims[ent.force.name].claims = {} claims[ent.force.name].claims = {}
claims[ent.force.name].collections = {} claims[ent.force.name].collections = {}
end end
table.insert(claims[ent.force.name].collections, point) table.insert(claims[ent.force.name].collections, point)
end end
local function claim_on_build_entity(ent, deps) local function claim_on_build_entity(ent, deps)
local max_dist = global.this._claim_max_dist local max_dist = this._claim_max_dist
local force = ent.force.name local force = ent.force.name
local comm = deps.common local comm = deps.common
local data = global.this._claims_info[force] local data = this._claims_info[force]
if data == nil then if data == nil then
claim_new_claim(ent, deps) claim_new_claim(ent, deps)
return return
end end
local in_range = false local in_range = false
local collections = data.collections local collections = data.collections
for i = 1, #collections do for i = 1, #collections do
local points = collections[i] local points = collections[i]
for _, point in pairs(points) do for _, point in pairs(points) do
point = point point = point
local dist = comm.get_distance(point, ent.position) local dist = comm.get_distance(point, ent.position)
if max_dist < dist then if max_dist < dist then
goto continue goto continue
end end
in_range = true in_range = true
point = { point = {
x = comm.get_axis(ent.position, "x"), x = comm.get_axis(ent.position, 'x'),
y = comm.get_axis(ent.position, "y"), y = comm.get_axis(ent.position, 'y')
} }
table.insert(points, point) table.insert(points, point)
data.claims[i] = comm.get_convex_hull(points) data.claims[i] = comm.get_convex_hull(points)
break break
::continue:: ::continue::
end end
end end
if not in_range then if not in_range then
claim_new_claim(ent, deps) claim_new_claim(ent, deps)
end end
end end
local function claims_in_markers(name) local function claims_in_markers(name)
for _, marker in pairs(global.this._claim_markers) do for _, marker in pairs(this._claim_markers) do
if name == marker then if name == marker then
return true return true
end end
end end
return false return false
end end
--[[ --[[
@ -97,47 +103,47 @@ on_build_entity - Event processing function.
@param ent - Entity @param ent - Entity
--]] --]]
public.on_built_entity = function(ent) public.on_built_entity = function(ent)
if not claims_in_markers(ent.name) then if not claims_in_markers(ent.name) then
return return
end end
local deps = { local deps = {
common = common, common = common
} }
claim_on_build_entity(ent, deps) claim_on_build_entity(ent, deps)
end end
local function claim_on_entity_died(ent, deps) local function claim_on_entity_died(ent, deps)
local comm = deps.common local comm = deps.common
local force = ent.force.name local force = ent.force.name
local data = global.this._claims_info[force] local data = this._claims_info[force]
if data == nil then if data == nil then
return return
end end
for i = 1, #data.collections do for i = 1, #data.collections do
local points = data.collections[i] local points = data.collections[i]
for j = 1, #points do for j = 1, #points do
local point = points[j] local point = points[j]
if comm.positions_equal(point, ent.position) then if comm.positions_equal(point, ent.position) then
table.remove(points, j) table.remove(points, j)
data.claims[i] = comm.get_convex_hull(points) data.claims[i] = comm.get_convex_hull(points)
break
end
end
if #points == 0 then
table.remove(data.claims, i)
table.remove(data.collections, i)
break break
end end
end end
if #points == 0 then if #data.claims == 0 then
table.remove(data.claims, i) this._claims_info[force] = nil
table.remove(data.collections, i) end
break
end
end
if #data.claims == 0 then
global.this._claims_info[force] = nil
end
end end
--[[ --[[
@ -145,14 +151,14 @@ on_entity_died - Event processing function.
@param ent - Entity @param ent - Entity
--]] --]]
public.on_entity_died = function(ent) public.on_entity_died = function(ent)
if not claims_in_markers(ent.name) then if not claims_in_markers(ent.name) then
return return
end end
local deps = { local deps = {
common = common, common = common
} }
claim_on_entity_died(ent, deps) claim_on_entity_died(ent, deps)
end end
--[[ --[[
@ -160,7 +166,7 @@ on_player_mined_entity - Event processing function.
@param ent - Entity @param ent - Entity
--]] --]]
public.on_player_mined_entity = function(ent) public.on_player_mined_entity = function(ent)
public.on_entity_died(ent) public.on_entity_died(ent)
end end
--[[ --[[
@ -168,7 +174,7 @@ on_player_died - Event processing function
@param player - Player @param player - Player
--]] --]]
public.on_player_died = function(player) public.on_player_died = function(player)
global.this._claims_info[player.name] = nil this._claims_info[player.name] = nil
end end
--[[ --[[
@ -176,33 +182,33 @@ get_claims - Get all claims data points for given force.
@param f_name - Force name. @param f_name - Force name.
--]] --]]
public.get_claims = function(f_name) public.get_claims = function(f_name)
if global.this._claims_info[f_name] == nil then if this._claims_info[f_name] == nil then
return {} return {}
end end
return global.this._claims_info[f_name].claims return this._claims_info[f_name].claims
end end
local function claims_update_visiblity() local function claims_update_visiblity()
if #global.this._claims_visible_to == 0 then if #this._claims_visible_to == 0 then
for _, info in pairs(global.this._claims_info) do for _, info in pairs(this._claims_info) do
for _, id in pairs(info.polygons) do for _, id in pairs(info.polygons) do
if rendering.is_valid(id) then if rendering.is_valid(id) then
rendering.set_visible(id, false) rendering.set_visible(id, false)
end
end end
end end
end return
return end
end
for _, info in pairs(global.this._claims_info) do for _, info in pairs(this._claims_info) do
for _, id in pairs(info.polygons) do for _, id in pairs(info.polygons) do
if rendering.is_valid(id) then if rendering.is_valid(id) then
rendering.set_visible(id, true) rendering.set_visible(id, true)
rendering.set_players(id, global.this._claims_visible_to) rendering.set_players(id, this._claims_visible_to)
end end
end end
end end
end end
--[[ --[[
@ -210,14 +216,14 @@ set_visibility_to - Specifies who can see the claims and redraws.
@param name - Name of a player. @param name - Name of a player.
--]] --]]
public.set_visibility_to = function(name) public.set_visibility_to = function(name)
for _, p in pairs(global.this._claims_visible_to) do for _, p in pairs(this._claims_visible_to) do
if p == name then if p == name then
return return
end end
end end
table.insert(global.this._claims_visible_to, name) table.insert(this._claims_visible_to, name)
claims_update_visiblity() claims_update_visiblity()
end end
--[[ --[[
@ -225,14 +231,14 @@ remove_visibility_from - Remove the claim visibility from the player.
@param name - Name of a player. @param name - Name of a player.
--]] --]]
public.remove_visibility_from = function(name) public.remove_visibility_from = function(name)
for i = 1, #global.this._claims_visible_to do for i = 1, #this._claims_visible_to do
local p = global.this._claims_visible_to[i] local p = this._claims_visible_to[i]
if p == name then if p == name then
table.remove(global.this._claims_visible_to, i) table.remove(this._claims_visible_to, i)
claims_update_visiblity() claims_update_visiblity()
break break
end end
end end
end end
return public return public

View File

@ -1,8 +1,18 @@
local public = {} local public = {}
local Global = require 'utils.global'
local this = {}
Global.register(
this,
function(tbl)
this = tbl
end
)
public.init = function() public.init = function()
if global.this == nil then if this == nil then
global.this = {} this = {}
end end
end end
@ -12,11 +22,11 @@ rand_range - Return random integer within the range.
@param stop - Stop range. @param stop - Stop range.
--]] --]]
public.rand_range = function(start, stop) public.rand_range = function(start, stop)
if not global.this.rng then if not this.rng then
global.this.rng = game.create_random_generator() this.rng = game.create_random_generator()
end end
return global.this.rng(start, stop) return this.rng(start, stop)
end end
--[[ --[[

View File

@ -1,24 +1,31 @@
local public = {} local public = {}
local _common = require(".common") local _common = require('.common')
local _simplex = require(".simplex_noise") local _simplex = require('.simplex_noise')
local Token = require 'utils.token'
local Global = require 'utils.global'
local this = {}
Global.register(
this,
function(tbl)
this = tbl
end
)
public.init = function() public.init = function()
if global.this == nil then this._grid = {}
global.this = {} this._exclusions = {}
end this._layers = {}
this._collision_mask = {}
global.this._grid = {} _simplex.init()
global.this._exclusions = {}
global.this._layers = {}
global.this._collision_mask = {}
_simplex.init()
end end
--[[ --[[
push_chunk - Pushes chunk position into a grid for later processing. push_chunk - Pushes chunk position into a grid for later processing.
@param chunk - ChunkPosition @param chunk - ChunkPosition
--]] --]]
public.push_chunk = function(chunk) public.push_chunk = function(chunk)
table.insert(global.this._grid, chunk) table.insert(this._grid, chunk)
end end
--[[ --[[
@ -26,7 +33,7 @@ add_excluding_bounding_box - Pushes bounding box into exclusion list.
@param bb - BoundindBox. @param bb - BoundindBox.
--]] --]]
public.push_excluding_bounding_box = function(bb) public.push_excluding_bounding_box = function(bb)
table.insert(global.this._exclusions, bb) table.insert(this._exclusions, bb)
end end
--[[ --[[
@ -34,13 +41,13 @@ remove_ecluding_bounding_box - Removes bounding box from exclusion list.
@param bb - BoundingBox to get rid of. @param bb - BoundingBox to get rid of.
--]] --]]
public.remove_excluding_bounding_box = function(bb) public.remove_excluding_bounding_box = function(bb)
for i = 1, #global.this._exclusions do for i = 1, #this._exclusions do
local box = global.this._exclusions[i] local box = this._exclusions[i]
if box == bb then if box == bb then
table.remove(global.this._exclusions, i) table.remove(this._exclusions, i)
break break
end end
end end
end end
--[[ --[[
@ -52,18 +59,18 @@ add_noise_layer - Add noise layer that will be applied onto the grid.
@param elevation - Layer visibility [0f - 1f) @param elevation - Layer visibility [0f - 1f)
--]] --]]
public.add_noise_layer = function(type, name, objects, elevation, resolution) public.add_noise_layer = function(type, name, objects, elevation, resolution)
local layer = { local layer = {
type = type, type = type,
name = name, name = name,
objects = objects, objects = objects,
elevation = elevation, elevation = elevation,
resolution = resolution, resolution = resolution,
cache = {}, cache = {},
hook = nil, hook = nil,
deps = nil, deps = nil
} }
table.insert(global.this._layers, layer) table.insert(this._layers, layer)
end end
--[[ --[[
@ -72,12 +79,12 @@ add_noise_layer_hook - Execute callback on created object.
@param hook - Callback that will be called with an object argument. @param hook - Callback that will be called with an object argument.
--]] --]]
public.add_noise_layer_hook = function(name, hook) public.add_noise_layer_hook = function(name, hook)
for _, layer in pairs(global.this._layers) do for _, layer in pairs(this._layers) do
if layer.name == name then if layer.name == name then
layer.hook = hook layer.hook = hook
break break
end end
end end
end end
--[[ --[[
@ -86,12 +93,12 @@ lua variable. This dependency then is injected into hook.
@param deps - Dependencies, any variable. @param deps - Dependencies, any variable.
--]] --]]
public.add_noise_layer_dependency = function(name, deps) public.add_noise_layer_dependency = function(name, deps)
for _, layer in pairs(global.this._layers) do for _, layer in pairs(this._layers) do
if layer.name == name then if layer.name == name then
layer.deps = deps layer.deps = deps
break break
end end
end end
end end
--[[ --[[
@ -99,74 +106,89 @@ set_collision_mask - Set which tiles should be ignored.
@param mask - Table of collision masks. @param mask - Table of collision masks.
--]] --]]
public.set_collision_mask = function(mask) public.set_collision_mask = function(mask)
global.this._collision_mask = mask this._collision_mask = mask
end end
local function _do_job_tile(surf, layer) local function _do_job_tile(surf, layer)
surf.set_tiles(layer.cache) surf.set_tiles(layer.cache)
end end
local function _do_job_entity(surf, layer) local function _do_job_entity(surf, layer)
local hook = layer.hook local hook = layer.hook
local deps = layer.deps if not hook then
for _, object in pairs(layer.cache) do return
if object.name == "character" or object.name == "gun-turret" then end
if not surf.can_place_entity(object) then local func = Token.get(hook)
if not func then
return
end
func = Token.get(func)
local deps = layer.deps
if not deps then
return
end
local func2 = Token.get(deps)
if not func2 then
return
end
for _, object in pairs(layer.cache) do
if object.name == 'character' or object.name == 'gun-turret' then
if not surf.can_place_entity(object) then
goto continue
end
end
local ent = surf.create_entity(object)
if not ent or not ent.valid then
goto continue goto continue
end end
end
local ent = surf.create_entity(object) if hook then
if not ent or not ent.valid then func(ent, func2)
goto continue end
end
if hook then ::continue::
hook(ent, deps) end
end
::continue::
end
end end
local function _do_job(surf, x, y) local function _do_job(surf, x, y)
local point = { local point = {
x = x, x = x,
y = y, y = y
} }
for _, exclusion in pairs(global.this._exclusions) do for _, exclusion in pairs(this._exclusions) do
if _common.point_in_bounding_box(point, exclusion) then if _common.point_in_bounding_box(point, exclusion) then
return return
end end
end end
for _, layer in pairs(global.this._layers) do for _, layer in pairs(this._layers) do
local ret = _simplex.get(point, layer.resolution) local ret = _simplex.get(point, layer.resolution)
if ret >= layer.elevation then if ret >= layer.elevation then
local tile = surf.get_tile(point) local tile = surf.get_tile(point)
for _, mask in pairs(global.this._collision_mask) do for _, mask in pairs(this._collision_mask) do
if tile.collides_with(mask) then if tile.collides_with(mask) then
goto continue goto continue
end
end end
end
local object_name = layer.objects[1] local object_name = layer.objects[1]
if #layer.objects > 1 then if #layer.objects > 1 then
local index = _common.rand_range(1, #layer.objects) local index = _common.rand_range(1, #layer.objects)
object_name = layer.objects[index] object_name = layer.objects[index]
end end
local object = { local object = {
name = object_name, name = object_name,
position = point, position = point
} }
table.insert(layer.cache, object) table.insert(layer.cache, object)
break break
::continue:: ::continue::
end end
end end
end end
--[[ --[[
@ -174,39 +196,39 @@ do_job - Do a single step propagation of a layers.
@param surf - LuaSurface, onto which action is taken. @param surf - LuaSurface, onto which action is taken.
--]] --]]
public.do_job = function(surf) public.do_job = function(surf)
if #global.this._grid <= 0 then if #this._grid <= 0 then
return return
end end
local chunk = table.remove(global.this._grid) local chunk = table.remove(this._grid)
local x = _common.get_axis(chunk, "x") local x = _common.get_axis(chunk, 'x')
local y = _common.get_axis(chunk, "y") local y = _common.get_axis(chunk, 'y')
chunk = { chunk = {
left_top = { left_top = {
x = x * 32, x = x * 32,
y = y * 32 y = y * 32
}, },
right_bottom = { right_bottom = {
x = (x * 32) + 32, x = (x * 32) + 32,
y = (y * 32) + 32, y = (y * 32) + 32
} }
} }
_common.for_bounding_box(surf, chunk, _do_job) _common.for_bounding_box(surf, chunk, _do_job)
for _, layer in pairs(global.this._layers) do for _, layer in pairs(this._layers) do
local cache = layer.cache local cache = layer.cache
if #cache >= 1 then if #cache >= 1 then
if layer.type == "LuaTile" then if layer.type == 'LuaTile' then
_do_job_tile(surf, layer) _do_job_tile(surf, layer)
elseif layer.type == "LuaEntity" then elseif layer.type == 'LuaEntity' then
_do_job_entity(surf, layer) _do_job_entity(surf, layer)
end end
layer.cache = {} layer.cache = {}
end end
end end
end end
return public return public

View File

@ -1,12 +1,18 @@
local public = {} local public = {}
local _global = require('utils.global') local Global = require 'utils.global'
local Token = require 'utils.token'
local this = {}
Global.register(
this,
function(tbl)
this = tbl
end
)
public.init = function() public.init = function()
if global.this == nil then this.timers = {}
global.this = {}
end
global.this.timers = {}
end end
--[[ --[[
@ -15,18 +21,20 @@ set_timer - Sets a timer.
@param hook - Action executed after timer is elapsed. @param hook - Action executed after timer is elapsed.
--]] --]]
public.set_timer = function(left, hook) public.set_timer = function(left, hook)
local id = game.tick local id = game.tick
local entry = { local token = Token.register(hook)
left = left, local token2 = Token.register(left)
hook_finish = hook, local entry = {
hook_update = nil, left = token2,
deps = nil, hook_finish = token,
running = false, hook_update = nil,
last_update = 0, deps = nil,
} running = false,
last_update = 0
}
global.this.timers[id] = entry this.timers[id] = entry
return id return id
end end
--[[ --[[
@ -36,7 +44,8 @@ timers is updated.
@param hook - Hook that will be executed per update. @param hook - Hook that will be executed per update.
--]] --]]
public.set_timer_on_update = function(id, hook) public.set_timer_on_update = function(id, hook)
global.this.timers[id].hook_update = hook local token = Token.register(hook)
this.timers[id].hook_update = token
end end
--[[ --[[
@ -45,7 +54,8 @@ set_timer_dependency - Adds dependency into user callback.
@param deps - Dependency of timer to add. @param deps - Dependency of timer to add.
--]] --]]
public.set_timer_dependency = function(id, deps) public.set_timer_dependency = function(id, deps)
global.this.timers[id].deps = deps local token = Token.register(deps)
this.timers[id].deps = token
end end
--[[ --[[
@ -53,8 +63,8 @@ set_timer_start - Sets the timer to run.
@param id - Id of a timer. @param id - Id of a timer.
--]] --]]
public.set_timer_start = function(id) public.set_timer_start = function(id)
global.this.timers[id].running = true this.timers[id].running = true
global.this.timers[id].last_update = game.tick this.timers[id].last_update = game.tick
end end
--[[ --[[
@ -62,37 +72,37 @@ kill_timer - Effectivly kills the timer.
@param id - Timer id. @param id - Timer id.
--]] --]]
public.kill_timer = function(id) public.kill_timer = function(id)
global.this.timers[id] = nil this.timers[id] = nil
end end
--[[ --[[
do_job - Execute timer logic within a tick. do_job - Execute timer logic within a tick.
--]] --]]
public.do_job = function() public.do_job = function()
for id, entry in pairs(global.this.timers) do for id, entry in pairs(this.timers) do
if entry.running == false then if entry.running == false then
goto continue goto continue
end end
entry.left = entry.left - (game.tick - entry.last_update) entry.left = entry.left - (game.tick - entry.last_update)
if entry.left > 0 then if entry.left > 0 then
entry.last_update = game.tick entry.last_update = game.tick
if entry.hook_update ~= nil then if entry.hook_update ~= nil then
if not entry.hook_update(entry.left, entry.deps) then if not entry.hook_update(entry.left, entry.deps) then
goto premature_finish goto premature_finish
end
end end
end
goto continue goto continue
end end
::premature_finish:: ::premature_finish::
entry.hook_finish(entry.deps) entry.hook_finish(entry.deps)
global.this.timers[id] = nil this.timers[id] = nil
::continue:: ::continue::
end end
end end
return public return public