mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2024-12-28 23:06:38 +02:00
planet prison fix
removed over 60 global functions removed obsolete entities
This commit is contained in:
parent
c069a2ae68
commit
fa8c72fb46
@ -107,6 +107,9 @@ require 'modules.autostash'
|
||||
--![[East VS West Survival PVP, where you breed biters with science flasks]]--
|
||||
--require 'maps.biter_hatchery.main'
|
||||
|
||||
--![[Fight in a world where everyone are prisoners]]
|
||||
--require 'maps.planet_prison'
|
||||
|
||||
--![[Chop trees to gain resources]]--
|
||||
--require 'maps.choppy'
|
||||
--require 'maps.choppy_dx'
|
||||
@ -201,7 +204,6 @@ require 'modules.autostash'
|
||||
--require 'maps.wave_defense'
|
||||
--require 'maps.crossing'
|
||||
--require 'maps.anarchy'
|
||||
--require 'maps.planet_prison'
|
||||
--require 'maps.blue_beach'
|
||||
--require 'maps.nightfall'
|
||||
--require 'maps.pitch_black.main'
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -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
@ -1,187 +1,182 @@
|
||||
local public = {}
|
||||
local _common = require(".common")
|
||||
local _common = require('.common')
|
||||
|
||||
public.command = {
|
||||
--[[
|
||||
--[[
|
||||
@param args nil
|
||||
--]]
|
||||
noop = 0,
|
||||
|
||||
--[[
|
||||
noop = 0,
|
||||
--[[
|
||||
@param args nil
|
||||
--]]
|
||||
seek_and_destroy_player = 1,
|
||||
|
||||
--[[
|
||||
seek_and_destroy_player = 1,
|
||||
--[[
|
||||
@param args = {
|
||||
agents, // All movable agents
|
||||
positions, // Table of positions to attack
|
||||
}
|
||||
--]]
|
||||
attack_objects = 2
|
||||
attack_objects = 2
|
||||
}
|
||||
|
||||
--[[
|
||||
init - Initialize the module.
|
||||
--]]
|
||||
public.init = function()
|
||||
if global.this == nil then
|
||||
global.this = {}
|
||||
end
|
||||
end
|
||||
|
||||
local function _get_direction(src, dest)
|
||||
local src_x = _common.get_axis(src, "x")
|
||||
local src_y = _common.get_axis(src, "y")
|
||||
local dest_x = _common.get_axis(dest, "x")
|
||||
local dest_y = _common.get_axis(dest, "y")
|
||||
local src_x = _common.get_axis(src, 'x')
|
||||
local src_y = _common.get_axis(src, 'y')
|
||||
local dest_x = _common.get_axis(dest, 'x')
|
||||
local dest_y = _common.get_axis(dest, 'y')
|
||||
|
||||
local step = {
|
||||
x = nil,
|
||||
y = nil
|
||||
}
|
||||
local step = {
|
||||
x = nil,
|
||||
y = nil
|
||||
}
|
||||
|
||||
local precision = _common.rand_range(1, 10)
|
||||
if dest_x - precision > src_x then
|
||||
step.x = 1
|
||||
elseif dest_x < src_x - precision then
|
||||
step.x = -1
|
||||
else
|
||||
step.x = 0
|
||||
end
|
||||
local precision = _common.rand_range(1, 10)
|
||||
if dest_x - precision > src_x then
|
||||
step.x = 1
|
||||
elseif dest_x < src_x - precision then
|
||||
step.x = -1
|
||||
else
|
||||
step.x = 0
|
||||
end
|
||||
|
||||
if dest_y - precision > src_y then
|
||||
step.y = 1
|
||||
elseif dest_y < src_y - precision then
|
||||
step.y = -1
|
||||
else
|
||||
step.y = 0
|
||||
end
|
||||
if dest_y - precision > src_y then
|
||||
step.y = 1
|
||||
elseif dest_y < src_y - precision then
|
||||
step.y = -1
|
||||
else
|
||||
step.y = 0
|
||||
end
|
||||
|
||||
return _common.direction_lookup[step.x][step.y]
|
||||
return _common.direction_lookup[step.x][step.y]
|
||||
end
|
||||
|
||||
local function _move_to(ent, trgt, min_distance)
|
||||
local state = {
|
||||
walking = false,
|
||||
}
|
||||
local state = {
|
||||
walking = false
|
||||
}
|
||||
|
||||
local distance = _common.get_distance(trgt.position, ent.position)
|
||||
if min_distance < distance then
|
||||
local dir = _get_direction(ent.position, trgt.position)
|
||||
if dir then
|
||||
state = {
|
||||
walking = true,
|
||||
direction = dir
|
||||
}
|
||||
end
|
||||
end
|
||||
local distance = _common.get_distance(trgt.position, ent.position)
|
||||
if min_distance < distance then
|
||||
local dir = _get_direction(ent.position, trgt.position)
|
||||
if dir then
|
||||
state = {
|
||||
walking = true,
|
||||
direction = dir
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
ent.walking_state = state
|
||||
return state.walking
|
||||
ent.walking_state = state
|
||||
return state.walking
|
||||
end
|
||||
|
||||
local function _shoot_at(ent, trgt)
|
||||
ent.shooting_state = {
|
||||
state = defines.shooting.shooting_enemies,
|
||||
position = trgt.position
|
||||
}
|
||||
ent.shooting_state = {
|
||||
state = defines.shooting.shooting_enemies,
|
||||
position = trgt.position
|
||||
}
|
||||
end
|
||||
|
||||
local function _shoot_stop(ent)
|
||||
ent.shooting_state = {
|
||||
state = defines.shooting.not_shooting,
|
||||
position = {0, 0}
|
||||
}
|
||||
ent.shooting_state = {
|
||||
state = defines.shooting.not_shooting,
|
||||
position = {0, 0}
|
||||
}
|
||||
end
|
||||
|
||||
local function _do_job_seek_and_destroy_player(surf)
|
||||
for _, player in pairs(game.players) do
|
||||
if player.character == nil then
|
||||
goto continue
|
||||
end
|
||||
for _, player in pairs(game.players) do
|
||||
if player.character == nil then
|
||||
goto continue
|
||||
end
|
||||
|
||||
local search_info = {
|
||||
name = "character",
|
||||
position = player.character.position,
|
||||
radius = 20,
|
||||
force = "enemy",
|
||||
}
|
||||
local search_info = {
|
||||
name = 'character',
|
||||
position = player.character.position,
|
||||
radius = 20,
|
||||
force = 'enemy'
|
||||
}
|
||||
|
||||
local ents = surf.find_entities_filtered(search_info)
|
||||
if not ents or #ents == 0 then
|
||||
goto continue
|
||||
end
|
||||
local ents = surf.find_entities_filtered(search_info)
|
||||
if not ents or #ents == 0 then
|
||||
goto continue
|
||||
end
|
||||
|
||||
for _, e in pairs(ents) do
|
||||
if not _move_to(e, player.character, _common.rand_range(5, 10)) then
|
||||
_shoot_at(e, player.character)
|
||||
else
|
||||
_shoot_stop(e)
|
||||
end
|
||||
end
|
||||
for _, e in pairs(ents) do
|
||||
if not _move_to(e, player.character, _common.rand_range(5, 10)) then
|
||||
_shoot_at(e, player.character)
|
||||
else
|
||||
_shoot_stop(e)
|
||||
end
|
||||
end
|
||||
|
||||
::continue::
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
local function _do_job_attack_objects(surf, args)
|
||||
local agents = args.agents
|
||||
if #agents == 0 then
|
||||
return
|
||||
end
|
||||
local agents = args.agents
|
||||
if #agents == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local objects = args.objects
|
||||
local target, closest, agent, query
|
||||
for i = #agents, 1, -1 do
|
||||
agent = agents[i]
|
||||
if not agent.valid then
|
||||
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)
|
||||
local objects = args.objects
|
||||
local target, closest, agent, query
|
||||
for i = #agents, 1, -1 do
|
||||
agent = agents[i]
|
||||
if not agent.valid then
|
||||
table.remove(agents, i)
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
|
||||
target = _common.get_closest_neighbour(agent.position, objects)
|
||||
end
|
||||
if game.tick % i ~= 0 then
|
||||
goto continue
|
||||
end
|
||||
|
||||
if target == nil or not target.valid 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
|
||||
end
|
||||
|
||||
if not _move_to(agent, target, _common.rand_range(5, 15)) then
|
||||
_shoot_at(agent, target)
|
||||
else
|
||||
_shoot_stop(agent)
|
||||
end
|
||||
target = _common.get_closest_neighbour(agent.position, objects)
|
||||
end
|
||||
|
||||
::continue::
|
||||
end
|
||||
if target == nil or not target.valid then
|
||||
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
|
||||
|
||||
--[[
|
||||
@ -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.
|
||||
--]]
|
||||
public.do_job = function(surf, command, args)
|
||||
if args == nil then
|
||||
args = {}
|
||||
end
|
||||
if args == nil then
|
||||
args = {}
|
||||
end
|
||||
|
||||
if command == public.command.seek_and_destroy_player then
|
||||
_do_job_seek_and_destroy_player(surf)
|
||||
elseif command == public.command.attack_objects then
|
||||
_do_job_attack_objects(surf, args)
|
||||
end
|
||||
if command == public.command.seek_and_destroy_player then
|
||||
_do_job_seek_and_destroy_player(surf)
|
||||
elseif command == public.command.attack_objects then
|
||||
_do_job_attack_objects(surf, args)
|
||||
end
|
||||
end
|
||||
|
||||
return public
|
||||
|
@ -1,12 +1,19 @@
|
||||
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()
|
||||
if global.this == nil then
|
||||
global.this = {}
|
||||
end
|
||||
|
||||
global.this._bps = {}
|
||||
this._bps = {}
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -15,12 +22,12 @@ push_blueprint - Pushes blueprint into a list.
|
||||
@param bp - Blueprint in JSON format.
|
||||
--]]
|
||||
public.push_blueprint = function(name, bp)
|
||||
local entry = {
|
||||
bp = game.json_to_table(bp).blueprint,
|
||||
hook = nil,
|
||||
refs = {}
|
||||
}
|
||||
global.this._bps[name] = entry
|
||||
local entry = {
|
||||
bp = game.json_to_table(bp).blueprint,
|
||||
hook = nil,
|
||||
refs = {}
|
||||
}
|
||||
this._bps[name] = entry
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -29,17 +36,19 @@ set_blueprint_hook - Set callback to a blueprint.
|
||||
@param hook - Callback that will be called after blueprint is placed.
|
||||
--]]
|
||||
public.set_blueprint_hook = function(name, hook)
|
||||
if name == nil then
|
||||
log("bp.set_blueprint_hook: name is nil")
|
||||
return
|
||||
end
|
||||
if name == nil then
|
||||
log('bp.set_blueprint_hook: name is nil')
|
||||
return
|
||||
end
|
||||
|
||||
if global.this._bps[name] == nil then
|
||||
log("bp.set_blueprint_hook: unrecognized blueprint")
|
||||
return
|
||||
end
|
||||
if this._bps[name] == nil then
|
||||
log('bp.set_blueprint_hook: unrecognized blueprint')
|
||||
return
|
||||
end
|
||||
|
||||
global.this._bps[name].hook = hook
|
||||
local token = Token.register(hook)
|
||||
|
||||
this._bps[name].hook = token
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -47,18 +56,18 @@ get_references - Get all references of the blueprint on the map.
|
||||
@param name - Blueprint handle.
|
||||
--]]
|
||||
public.get_references = function(name)
|
||||
if name == nil then
|
||||
log("bp.get_references: name is nil")
|
||||
return {}
|
||||
end
|
||||
if name == nil then
|
||||
log('bp.get_references: name is nil')
|
||||
return {}
|
||||
end
|
||||
|
||||
local object = global.this._bps[name]
|
||||
if object == nil then
|
||||
log("bp.get_references: unrecognized blueprint")
|
||||
return {}
|
||||
end
|
||||
local object = this._bps[name]
|
||||
if object == nil then
|
||||
log('bp.get_references: unrecognized blueprint')
|
||||
return {}
|
||||
end
|
||||
|
||||
return object.refs
|
||||
return object.refs
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -66,18 +75,18 @@ get_references - Gets opaque object representing bp references.
|
||||
@param name - Blueprint handle.
|
||||
--]]
|
||||
public.get_references = function(name)
|
||||
if name == nil then
|
||||
log("bp.get_references: name is nil")
|
||||
return
|
||||
end
|
||||
if name == nil then
|
||||
log('bp.get_references: name is nil')
|
||||
return
|
||||
end
|
||||
|
||||
local object = global.this._bps[name]
|
||||
if object == nil then
|
||||
log("bp.get_references: unrecognized blueprint")
|
||||
return
|
||||
end
|
||||
local object = this._bps[name]
|
||||
if object == nil then
|
||||
log('bp.get_references: unrecognized blueprint')
|
||||
return
|
||||
end
|
||||
|
||||
return object.refs
|
||||
return object.refs
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -85,7 +94,7 @@ reference_get_bounding_box - Return bounding box from the reference.
|
||||
@param reference - Valid reference object fetched from get_references.
|
||||
--]]
|
||||
public.reference_get_bounding_box = function(reference)
|
||||
return reference.bb
|
||||
return reference.bb
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -93,7 +102,7 @@ reference_get_entities - Return references to entities.
|
||||
@param reference - Valid reference object fetched from get_references.
|
||||
--]]
|
||||
public.reference_get_entities = function(reference)
|
||||
return reference.entities
|
||||
return reference.entities
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -101,10 +110,9 @@ reference_get_timestamp - Return timestamp of a reference
|
||||
@param reference - Valid reference object fetched from get_references.
|
||||
--]]
|
||||
public.reference_get_timestamp = function(reference)
|
||||
return reference.timestamp
|
||||
return reference.timestamp
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
unlink_references_filtered - Unlinks all references of blueprint on the map if they
|
||||
meet the query rules.
|
||||
@ -115,32 +123,32 @@ unlinked.
|
||||
@return An array of unlinked references.
|
||||
--]]
|
||||
public.unlink_references_filtered = function(name, query)
|
||||
if name == nil then
|
||||
log("bp.get_references: name is nil")
|
||||
return
|
||||
end
|
||||
if name == nil then
|
||||
log('bp.get_references: name is nil')
|
||||
return
|
||||
end
|
||||
|
||||
local object = global.this._bps[name]
|
||||
if object == nil then
|
||||
log("bp.get_references: unrecognized blueprint")
|
||||
return
|
||||
end
|
||||
local object = this._bps[name]
|
||||
if object == nil then
|
||||
log('bp.get_references: unrecognized blueprint')
|
||||
return
|
||||
end
|
||||
|
||||
local refs = {}
|
||||
for i = #object.refs, 1, -1 do
|
||||
local ref = object.refs[i]
|
||||
if query and query.timestamp then
|
||||
if ref.timestamp > query.timestamp then
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
local refs = {}
|
||||
for i = #object.refs, 1, -1 do
|
||||
local ref = object.refs[i]
|
||||
if query and query.timestamp then
|
||||
if ref.timestamp > query.timestamp then
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(refs, ref)
|
||||
table.remove(object.refs, i)
|
||||
::continue::
|
||||
end
|
||||
table.insert(refs, ref)
|
||||
table.remove(object.refs, i)
|
||||
::continue::
|
||||
end
|
||||
|
||||
return refs
|
||||
return refs
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -153,42 +161,42 @@ meet the query rules.
|
||||
removed.
|
||||
--]]
|
||||
public.destroy_references_filtered = function(surf, name, query)
|
||||
if name == nil then
|
||||
log("bp.get_references: name is nil")
|
||||
return
|
||||
end
|
||||
if name == nil then
|
||||
log('bp.get_references: name is nil')
|
||||
return
|
||||
end
|
||||
|
||||
local object = global.this._bps[name]
|
||||
if object == nil then
|
||||
log("bp.get_references: unrecognized blueprint")
|
||||
return
|
||||
end
|
||||
local object = this._bps[name]
|
||||
if object == nil then
|
||||
log('bp.get_references: unrecognized blueprint')
|
||||
return
|
||||
end
|
||||
|
||||
for i = 1, #object.refs do
|
||||
local ref = object.refs[i]
|
||||
if query and query.timestamp then
|
||||
if ref.timestamp > query.timestamp then
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
for i = 1, #object.refs do
|
||||
local ref = object.refs[i]
|
||||
if query and query.timestamp then
|
||||
if ref.timestamp > query.timestamp then
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
|
||||
for _, ent in pairs(ref.entities) do
|
||||
if ent.valid then
|
||||
ent.destroy()
|
||||
end
|
||||
end
|
||||
for _, ent in pairs(ref.entities) do
|
||||
if ent.valid then
|
||||
ent.destroy()
|
||||
end
|
||||
end
|
||||
|
||||
local tiles = {}
|
||||
for _, tile in pairs(ref.tiles) do
|
||||
tile.name = "concrete"
|
||||
table.insert(tiles, tile)
|
||||
end
|
||||
local tiles = {}
|
||||
for _, tile in pairs(ref.tiles) do
|
||||
tile.name = 'concrete'
|
||||
table.insert(tiles, tile)
|
||||
end
|
||||
|
||||
surf.set_tiles(tiles)
|
||||
surf.set_tiles(tiles)
|
||||
|
||||
table.remove(object.refs, i)
|
||||
::continue::
|
||||
end
|
||||
table.remove(object.refs, i)
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -197,28 +205,28 @@ destroy_references - Destroys all references of blueprint on the map
|
||||
@param name - Blueprint handle.
|
||||
--]]
|
||||
public.destroy_references = function(surf, name)
|
||||
public.destroy_references_filtered(surf, name, {})
|
||||
public.destroy_references_filtered(surf, name, {})
|
||||
end
|
||||
|
||||
global._bp_destroy_reference = function(surf, ref)
|
||||
for _, ent in pairs(ref.entities) do
|
||||
if ent.valid then
|
||||
ent.destroy()
|
||||
end
|
||||
end
|
||||
local _bp_destroy_reference = function(surf, ref)
|
||||
for _, ent in pairs(ref.entities) do
|
||||
if ent.valid then
|
||||
ent.destroy()
|
||||
end
|
||||
end
|
||||
|
||||
local tiles = {}
|
||||
for _, tile in pairs(ref.tiles) do
|
||||
if tile.valid then
|
||||
goto continue
|
||||
end
|
||||
local tiles = {}
|
||||
for _, tile in pairs(ref.tiles) do
|
||||
if tile.valid then
|
||||
goto continue
|
||||
end
|
||||
|
||||
tile.name = "concrete"
|
||||
table.insert(tiles, tile)
|
||||
::continue::
|
||||
end
|
||||
tile.name = 'concrete'
|
||||
table.insert(tiles, tile)
|
||||
::continue::
|
||||
end
|
||||
|
||||
surf.set_tiles(tiles)
|
||||
surf.set_tiles(tiles)
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -227,64 +235,64 @@ destroy_reference - Destroys reference of a blueprint at given surface.
|
||||
@param reference - Any valid reference.
|
||||
--]]
|
||||
public.destroy_reference = function(surf, reference)
|
||||
for _, meta in pairs(global.this._bps) do
|
||||
for i = 1, #meta.refs do
|
||||
local ref = meta.refs[i]
|
||||
if reference.id == ref.id then
|
||||
global._bp_destroy_reference(surf, ref)
|
||||
table.remove(meta.refs, i)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, meta in pairs(this._bps) do
|
||||
for i = 1, #meta.refs do
|
||||
local ref = meta.refs[i]
|
||||
if reference.id == ref.id then
|
||||
_bp_destroy_reference(surf, ref)
|
||||
table.remove(meta.refs, i)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function _build_tiles(surf, point, tiles)
|
||||
local _tiles = {}
|
||||
local _tiles = {}
|
||||
|
||||
local get_axis = _common.get_axis
|
||||
for _, tile in pairs(tiles) do
|
||||
local _tile = {
|
||||
name = tile.name,
|
||||
position = {
|
||||
x = get_axis(tile.position, "x") + get_axis(point, "x"),
|
||||
y = get_axis(tile.position, "y") + get_axis(point, "y")
|
||||
}
|
||||
}
|
||||
table.insert(_tiles, _tile)
|
||||
end
|
||||
local get_axis = _common.get_axis
|
||||
for _, tile in pairs(tiles) do
|
||||
local _tile = {
|
||||
name = tile.name,
|
||||
position = {
|
||||
x = get_axis(tile.position, 'x') + get_axis(point, 'x'),
|
||||
y = get_axis(tile.position, 'y') + get_axis(point, 'y')
|
||||
}
|
||||
}
|
||||
table.insert(_tiles, _tile)
|
||||
end
|
||||
|
||||
surf.set_tiles(_tiles)
|
||||
return _tiles
|
||||
surf.set_tiles(_tiles)
|
||||
return _tiles
|
||||
end
|
||||
|
||||
|
||||
local function _build_entities(surf, point, entities, hook, args)
|
||||
local _entities = {}
|
||||
local _entities = {}
|
||||
|
||||
local get_axis = _common.get_axis
|
||||
for _, ent in pairs(entities) do
|
||||
local ent_info = {
|
||||
position = {
|
||||
x = get_axis(ent.position, "x") + get_axis(point, "x"),
|
||||
y = get_axis(ent.position, "y") + get_axis(point, "y")
|
||||
},
|
||||
name = ent.name,
|
||||
}
|
||||
local e = surf.create_entity(ent_info)
|
||||
if not e or not e.valid then
|
||||
goto continue
|
||||
end
|
||||
local get_axis = _common.get_axis
|
||||
for _, ent in pairs(entities) do
|
||||
local ent_info = {
|
||||
position = {
|
||||
x = get_axis(ent.position, 'x') + get_axis(point, 'x'),
|
||||
y = get_axis(ent.position, 'y') + get_axis(point, 'y')
|
||||
},
|
||||
name = ent.name
|
||||
}
|
||||
local e = surf.create_entity(ent_info)
|
||||
if not e or not e.valid then
|
||||
goto continue
|
||||
end
|
||||
|
||||
if hook then
|
||||
hook(e, args)
|
||||
end
|
||||
if hook then
|
||||
local token = Token.get(hook)
|
||||
token(e, args)
|
||||
end
|
||||
|
||||
table.insert(_entities, e)
|
||||
::continue::
|
||||
end
|
||||
table.insert(_entities, e)
|
||||
::continue::
|
||||
end
|
||||
|
||||
return _entities
|
||||
return _entities
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -295,79 +303,79 @@ build - Place blueprint at given point.
|
||||
@param args - If hook was set, this will be argument passed.
|
||||
--]]
|
||||
public.build = function(surf, name, point, args)
|
||||
if surf == nil then
|
||||
log("bp.build: surf is nil")
|
||||
return
|
||||
end
|
||||
if surf == nil then
|
||||
log('bp.build: surf is nil')
|
||||
return
|
||||
end
|
||||
|
||||
if name == nil then
|
||||
log("bp.build: name is nil")
|
||||
return
|
||||
end
|
||||
if name == nil then
|
||||
log('bp.build: name is nil')
|
||||
return
|
||||
end
|
||||
|
||||
local object = global.this._bps[name]
|
||||
if object == nil then
|
||||
log("bp.set_blueprint_hook: unrecognized blueprint")
|
||||
return
|
||||
end
|
||||
local object = this._bps[name]
|
||||
if object == nil then
|
||||
log('bp.set_blueprint_hook: unrecognized blueprint')
|
||||
return
|
||||
end
|
||||
|
||||
local instance = {
|
||||
entities = {},
|
||||
tiles = {},
|
||||
bb = nil,
|
||||
timestamp = game.tick,
|
||||
}
|
||||
local bbs = {}
|
||||
local tiles = object.bp.tiles
|
||||
if tiles and #tiles > 0 then
|
||||
instance.tiles = _build_tiles(surf, point, tiles)
|
||||
local bb = _common.create_bounding_box_by_points(instance.tiles)
|
||||
table.insert(bbs, bb)
|
||||
local instance = {
|
||||
entities = {},
|
||||
tiles = {},
|
||||
bb = nil,
|
||||
timestamp = game.tick
|
||||
}
|
||||
local bbs = {}
|
||||
local tiles = object.bp.tiles
|
||||
if tiles and #tiles > 0 then
|
||||
instance.tiles = _build_tiles(surf, point, tiles)
|
||||
local bb = _common.create_bounding_box_by_points(instance.tiles)
|
||||
table.insert(bbs, bb)
|
||||
|
||||
local query = {
|
||||
name = "character",
|
||||
area = bb,
|
||||
invert = true,
|
||||
}
|
||||
for _, ent in pairs(surf.find_entities_filtered(query)) do
|
||||
if ent.valid then
|
||||
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
|
||||
local query = {
|
||||
name = 'character',
|
||||
area = bb,
|
||||
invert = true
|
||||
}
|
||||
for _, ent in pairs(surf.find_entities_filtered(query)) do
|
||||
if ent.valid then
|
||||
ent.destroy()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ent_found.die()
|
||||
::continue::
|
||||
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)
|
||||
|
||||
instance.bb = _common.merge_bounding_boxes(bbs)
|
||||
instance.id = game.tick
|
||||
table.insert(object.refs, instance)
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
return public
|
||||
|
@ -1,5 +1,15 @@
|
||||
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.
|
||||
@ -7,89 +17,85 @@ init - Initialize claim system.
|
||||
@param max_distance - Maximal distance allowed between markers
|
||||
--]]
|
||||
public.init = function(names, max_distance)
|
||||
if global.this == nil then
|
||||
global.this = {}
|
||||
end
|
||||
if type(names) ~= 'table' then
|
||||
names = {names}
|
||||
end
|
||||
|
||||
if type(names) ~= "table" then
|
||||
names = { names }
|
||||
end
|
||||
|
||||
global.this._claims_info = {}
|
||||
global.this._claims_visible_to = {}
|
||||
global.this._claim_markers = names
|
||||
global.this._claim_max_dist = max_distance
|
||||
this._claims_info = {}
|
||||
this._claims_visible_to = {}
|
||||
this._claim_markers = names
|
||||
this._claim_max_dist = max_distance
|
||||
end
|
||||
|
||||
local function claim_new_claim(ent, deps)
|
||||
local comm = deps.common
|
||||
local point = {
|
||||
{
|
||||
x = comm.get_axis(ent.position, "x"),
|
||||
y = comm.get_axis(ent.position, "y"),
|
||||
}
|
||||
}
|
||||
local comm = deps.common
|
||||
local point = {
|
||||
{
|
||||
x = comm.get_axis(ent.position, 'x'),
|
||||
y = comm.get_axis(ent.position, 'y')
|
||||
}
|
||||
}
|
||||
|
||||
local claims = global.this._claims_info
|
||||
if claims[ent.force.name] == nil then
|
||||
claims[ent.force.name] = {}
|
||||
claims[ent.force.name].polygons = {}
|
||||
claims[ent.force.name].claims = {}
|
||||
claims[ent.force.name].collections = {}
|
||||
end
|
||||
local claims = this._claims_info
|
||||
if claims[ent.force.name] == nil then
|
||||
claims[ent.force.name] = {}
|
||||
claims[ent.force.name].polygons = {}
|
||||
claims[ent.force.name].claims = {}
|
||||
claims[ent.force.name].collections = {}
|
||||
end
|
||||
|
||||
table.insert(claims[ent.force.name].collections, point)
|
||||
table.insert(claims[ent.force.name].collections, point)
|
||||
end
|
||||
|
||||
local function claim_on_build_entity(ent, deps)
|
||||
local max_dist = global.this._claim_max_dist
|
||||
local force = ent.force.name
|
||||
local comm = deps.common
|
||||
local data = global.this._claims_info[force]
|
||||
local max_dist = this._claim_max_dist
|
||||
local force = ent.force.name
|
||||
local comm = deps.common
|
||||
local data = this._claims_info[force]
|
||||
|
||||
if data == nil then
|
||||
claim_new_claim(ent, deps)
|
||||
return
|
||||
end
|
||||
if data == nil then
|
||||
claim_new_claim(ent, deps)
|
||||
return
|
||||
end
|
||||
|
||||
local in_range = false
|
||||
local collections = data.collections
|
||||
for i = 1, #collections do
|
||||
local points = collections[i]
|
||||
local in_range = false
|
||||
local collections = data.collections
|
||||
for i = 1, #collections do
|
||||
local points = collections[i]
|
||||
|
||||
for _, point in pairs(points) do
|
||||
point = point
|
||||
local dist = comm.get_distance(point, ent.position)
|
||||
if max_dist < dist then
|
||||
goto continue
|
||||
end
|
||||
for _, point in pairs(points) do
|
||||
point = point
|
||||
local dist = comm.get_distance(point, ent.position)
|
||||
if max_dist < dist then
|
||||
goto continue
|
||||
end
|
||||
|
||||
in_range = true
|
||||
point = {
|
||||
x = comm.get_axis(ent.position, "x"),
|
||||
y = comm.get_axis(ent.position, "y"),
|
||||
}
|
||||
table.insert(points, point)
|
||||
data.claims[i] = comm.get_convex_hull(points)
|
||||
in_range = true
|
||||
point = {
|
||||
x = comm.get_axis(ent.position, 'x'),
|
||||
y = comm.get_axis(ent.position, 'y')
|
||||
}
|
||||
table.insert(points, point)
|
||||
data.claims[i] = comm.get_convex_hull(points)
|
||||
|
||||
break
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
break
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
if not in_range then
|
||||
claim_new_claim(ent, deps)
|
||||
end
|
||||
if not in_range then
|
||||
claim_new_claim(ent, deps)
|
||||
end
|
||||
end
|
||||
|
||||
local function claims_in_markers(name)
|
||||
for _, marker in pairs(global.this._claim_markers) do
|
||||
if name == marker then
|
||||
return true
|
||||
end
|
||||
end
|
||||
for _, marker in pairs(this._claim_markers) do
|
||||
if name == marker then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
return false
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -97,47 +103,47 @@ on_build_entity - Event processing function.
|
||||
@param ent - Entity
|
||||
--]]
|
||||
public.on_built_entity = function(ent)
|
||||
if not claims_in_markers(ent.name) then
|
||||
return
|
||||
end
|
||||
if not claims_in_markers(ent.name) then
|
||||
return
|
||||
end
|
||||
|
||||
local deps = {
|
||||
common = common,
|
||||
}
|
||||
claim_on_build_entity(ent, deps)
|
||||
local deps = {
|
||||
common = common
|
||||
}
|
||||
claim_on_build_entity(ent, deps)
|
||||
end
|
||||
|
||||
local function claim_on_entity_died(ent, deps)
|
||||
local comm = deps.common
|
||||
local force = ent.force.name
|
||||
local data = global.this._claims_info[force]
|
||||
if data == nil then
|
||||
return
|
||||
end
|
||||
local comm = deps.common
|
||||
local force = ent.force.name
|
||||
local data = this._claims_info[force]
|
||||
if data == nil then
|
||||
return
|
||||
end
|
||||
|
||||
for i = 1, #data.collections do
|
||||
local points = data.collections[i]
|
||||
for i = 1, #data.collections do
|
||||
local points = data.collections[i]
|
||||
|
||||
for j = 1, #points do
|
||||
local point = points[j]
|
||||
if comm.positions_equal(point, ent.position) then
|
||||
table.remove(points, j)
|
||||
for j = 1, #points do
|
||||
local point = points[j]
|
||||
if comm.positions_equal(point, ent.position) then
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if #points == 0 then
|
||||
table.remove(data.claims, i)
|
||||
table.remove(data.collections, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if #data.claims == 0 then
|
||||
global.this._claims_info[force] = nil
|
||||
end
|
||||
if #data.claims == 0 then
|
||||
this._claims_info[force] = nil
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -145,14 +151,14 @@ on_entity_died - Event processing function.
|
||||
@param ent - Entity
|
||||
--]]
|
||||
public.on_entity_died = function(ent)
|
||||
if not claims_in_markers(ent.name) then
|
||||
return
|
||||
end
|
||||
if not claims_in_markers(ent.name) then
|
||||
return
|
||||
end
|
||||
|
||||
local deps = {
|
||||
common = common,
|
||||
}
|
||||
claim_on_entity_died(ent, deps)
|
||||
local deps = {
|
||||
common = common
|
||||
}
|
||||
claim_on_entity_died(ent, deps)
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -160,7 +166,7 @@ on_player_mined_entity - Event processing function.
|
||||
@param ent - Entity
|
||||
--]]
|
||||
public.on_player_mined_entity = function(ent)
|
||||
public.on_entity_died(ent)
|
||||
public.on_entity_died(ent)
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -168,7 +174,7 @@ on_player_died - Event processing function
|
||||
@param player - Player
|
||||
--]]
|
||||
public.on_player_died = function(player)
|
||||
global.this._claims_info[player.name] = nil
|
||||
this._claims_info[player.name] = nil
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -176,33 +182,33 @@ get_claims - Get all claims data points for given force.
|
||||
@param f_name - Force name.
|
||||
--]]
|
||||
public.get_claims = function(f_name)
|
||||
if global.this._claims_info[f_name] == nil then
|
||||
return {}
|
||||
end
|
||||
if this._claims_info[f_name] == nil then
|
||||
return {}
|
||||
end
|
||||
|
||||
return global.this._claims_info[f_name].claims
|
||||
return this._claims_info[f_name].claims
|
||||
end
|
||||
|
||||
local function claims_update_visiblity()
|
||||
if #global.this._claims_visible_to == 0 then
|
||||
for _, info in pairs(global.this._claims_info) do
|
||||
for _, id in pairs(info.polygons) do
|
||||
if rendering.is_valid(id) then
|
||||
rendering.set_visible(id, false)
|
||||
if #this._claims_visible_to == 0 then
|
||||
for _, info in pairs(this._claims_info) do
|
||||
for _, id in pairs(info.polygons) do
|
||||
if rendering.is_valid(id) then
|
||||
rendering.set_visible(id, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
for _, info in pairs(global.this._claims_info) do
|
||||
for _, id in pairs(info.polygons) do
|
||||
if rendering.is_valid(id) then
|
||||
rendering.set_visible(id, true)
|
||||
rendering.set_players(id, global.this._claims_visible_to)
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, info in pairs(this._claims_info) do
|
||||
for _, id in pairs(info.polygons) do
|
||||
if rendering.is_valid(id) then
|
||||
rendering.set_visible(id, true)
|
||||
rendering.set_players(id, this._claims_visible_to)
|
||||
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.
|
||||
--]]
|
||||
public.set_visibility_to = function(name)
|
||||
for _, p in pairs(global.this._claims_visible_to) do
|
||||
if p == name then
|
||||
return
|
||||
end
|
||||
end
|
||||
for _, p in pairs(this._claims_visible_to) do
|
||||
if p == name then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(global.this._claims_visible_to, name)
|
||||
claims_update_visiblity()
|
||||
table.insert(this._claims_visible_to, name)
|
||||
claims_update_visiblity()
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -225,14 +231,14 @@ remove_visibility_from - Remove the claim visibility from the player.
|
||||
@param name - Name of a player.
|
||||
--]]
|
||||
public.remove_visibility_from = function(name)
|
||||
for i = 1, #global.this._claims_visible_to do
|
||||
local p = global.this._claims_visible_to[i]
|
||||
if p == name then
|
||||
table.remove(global.this._claims_visible_to, i)
|
||||
claims_update_visiblity()
|
||||
break
|
||||
end
|
||||
end
|
||||
for i = 1, #this._claims_visible_to do
|
||||
local p = this._claims_visible_to[i]
|
||||
if p == name then
|
||||
table.remove(this._claims_visible_to, i)
|
||||
claims_update_visiblity()
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return public
|
||||
|
@ -1,8 +1,18 @@
|
||||
local public = {}
|
||||
local Global = require 'utils.global'
|
||||
|
||||
local this = {}
|
||||
|
||||
Global.register(
|
||||
this,
|
||||
function(tbl)
|
||||
this = tbl
|
||||
end
|
||||
)
|
||||
|
||||
public.init = function()
|
||||
if global.this == nil then
|
||||
global.this = {}
|
||||
if this == nil then
|
||||
this = {}
|
||||
end
|
||||
end
|
||||
|
||||
@ -12,11 +22,11 @@ rand_range - Return random integer within the range.
|
||||
@param stop - Stop range.
|
||||
--]]
|
||||
public.rand_range = function(start, stop)
|
||||
if not global.this.rng then
|
||||
global.this.rng = game.create_random_generator()
|
||||
if not this.rng then
|
||||
this.rng = game.create_random_generator()
|
||||
end
|
||||
|
||||
return global.this.rng(start, stop)
|
||||
return this.rng(start, stop)
|
||||
end
|
||||
|
||||
--[[
|
||||
|
@ -1,24 +1,31 @@
|
||||
local public = {}
|
||||
local _common = require(".common")
|
||||
local _simplex = require(".simplex_noise")
|
||||
local _common = require('.common')
|
||||
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()
|
||||
if global.this == nil then
|
||||
global.this = {}
|
||||
end
|
||||
|
||||
global.this._grid = {}
|
||||
global.this._exclusions = {}
|
||||
global.this._layers = {}
|
||||
global.this._collision_mask = {}
|
||||
_simplex.init()
|
||||
this._grid = {}
|
||||
this._exclusions = {}
|
||||
this._layers = {}
|
||||
this._collision_mask = {}
|
||||
_simplex.init()
|
||||
end
|
||||
--[[
|
||||
push_chunk - Pushes chunk position into a grid for later processing.
|
||||
@param chunk - ChunkPosition
|
||||
--]]
|
||||
public.push_chunk = function(chunk)
|
||||
table.insert(global.this._grid, chunk)
|
||||
table.insert(this._grid, chunk)
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -26,7 +33,7 @@ add_excluding_bounding_box - Pushes bounding box into exclusion list.
|
||||
@param bb - BoundindBox.
|
||||
--]]
|
||||
public.push_excluding_bounding_box = function(bb)
|
||||
table.insert(global.this._exclusions, bb)
|
||||
table.insert(this._exclusions, bb)
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -34,13 +41,13 @@ remove_ecluding_bounding_box - Removes bounding box from exclusion list.
|
||||
@param bb - BoundingBox to get rid of.
|
||||
--]]
|
||||
public.remove_excluding_bounding_box = function(bb)
|
||||
for i = 1, #global.this._exclusions do
|
||||
local box = global.this._exclusions[i]
|
||||
if box == bb then
|
||||
table.remove(global.this._exclusions, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
for i = 1, #this._exclusions do
|
||||
local box = this._exclusions[i]
|
||||
if box == bb then
|
||||
table.remove(this._exclusions, i)
|
||||
break
|
||||
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)
|
||||
--]]
|
||||
public.add_noise_layer = function(type, name, objects, elevation, resolution)
|
||||
local layer = {
|
||||
type = type,
|
||||
name = name,
|
||||
objects = objects,
|
||||
elevation = elevation,
|
||||
resolution = resolution,
|
||||
cache = {},
|
||||
hook = nil,
|
||||
deps = nil,
|
||||
}
|
||||
local layer = {
|
||||
type = type,
|
||||
name = name,
|
||||
objects = objects,
|
||||
elevation = elevation,
|
||||
resolution = resolution,
|
||||
cache = {},
|
||||
hook = nil,
|
||||
deps = nil
|
||||
}
|
||||
|
||||
table.insert(global.this._layers, layer)
|
||||
table.insert(this._layers, layer)
|
||||
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.
|
||||
--]]
|
||||
public.add_noise_layer_hook = function(name, hook)
|
||||
for _, layer in pairs(global.this._layers) do
|
||||
if layer.name == name then
|
||||
layer.hook = hook
|
||||
break
|
||||
end
|
||||
end
|
||||
for _, layer in pairs(this._layers) do
|
||||
if layer.name == name then
|
||||
layer.hook = hook
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -86,12 +93,12 @@ lua variable. This dependency then is injected into hook.
|
||||
@param deps - Dependencies, any variable.
|
||||
--]]
|
||||
public.add_noise_layer_dependency = function(name, deps)
|
||||
for _, layer in pairs(global.this._layers) do
|
||||
if layer.name == name then
|
||||
layer.deps = deps
|
||||
break
|
||||
end
|
||||
end
|
||||
for _, layer in pairs(this._layers) do
|
||||
if layer.name == name then
|
||||
layer.deps = deps
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -99,74 +106,89 @@ set_collision_mask - Set which tiles should be ignored.
|
||||
@param mask - Table of collision masks.
|
||||
--]]
|
||||
public.set_collision_mask = function(mask)
|
||||
global.this._collision_mask = mask
|
||||
this._collision_mask = mask
|
||||
end
|
||||
|
||||
local function _do_job_tile(surf, layer)
|
||||
surf.set_tiles(layer.cache)
|
||||
surf.set_tiles(layer.cache)
|
||||
end
|
||||
|
||||
local function _do_job_entity(surf, layer)
|
||||
local hook = layer.hook
|
||||
local deps = layer.deps
|
||||
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
|
||||
local hook = layer.hook
|
||||
if not hook then
|
||||
return
|
||||
end
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local ent = surf.create_entity(object)
|
||||
if not ent or not ent.valid then
|
||||
goto continue
|
||||
end
|
||||
if hook then
|
||||
func(ent, func2)
|
||||
end
|
||||
|
||||
if hook then
|
||||
hook(ent, deps)
|
||||
end
|
||||
|
||||
::continue::
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
local function _do_job(surf, x, y)
|
||||
local point = {
|
||||
x = x,
|
||||
y = y,
|
||||
}
|
||||
local point = {
|
||||
x = x,
|
||||
y = y
|
||||
}
|
||||
|
||||
for _, exclusion in pairs(global.this._exclusions) do
|
||||
if _common.point_in_bounding_box(point, exclusion) then
|
||||
return
|
||||
end
|
||||
end
|
||||
for _, exclusion in pairs(this._exclusions) do
|
||||
if _common.point_in_bounding_box(point, exclusion) then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
for _, layer in pairs(global.this._layers) do
|
||||
local ret = _simplex.get(point, layer.resolution)
|
||||
if ret >= layer.elevation then
|
||||
local tile = surf.get_tile(point)
|
||||
for _, mask in pairs(global.this._collision_mask) do
|
||||
if tile.collides_with(mask) then
|
||||
goto continue
|
||||
for _, layer in pairs(this._layers) do
|
||||
local ret = _simplex.get(point, layer.resolution)
|
||||
if ret >= layer.elevation then
|
||||
local tile = surf.get_tile(point)
|
||||
for _, mask in pairs(this._collision_mask) do
|
||||
if tile.collides_with(mask) then
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local object_name = layer.objects[1]
|
||||
if #layer.objects > 1 then
|
||||
local index = _common.rand_range(1, #layer.objects)
|
||||
object_name = layer.objects[index]
|
||||
end
|
||||
local object_name = layer.objects[1]
|
||||
if #layer.objects > 1 then
|
||||
local index = _common.rand_range(1, #layer.objects)
|
||||
object_name = layer.objects[index]
|
||||
end
|
||||
|
||||
local object = {
|
||||
name = object_name,
|
||||
position = point,
|
||||
}
|
||||
table.insert(layer.cache, object)
|
||||
local object = {
|
||||
name = object_name,
|
||||
position = point
|
||||
}
|
||||
table.insert(layer.cache, object)
|
||||
|
||||
break
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
break
|
||||
::continue::
|
||||
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.
|
||||
--]]
|
||||
public.do_job = function(surf)
|
||||
if #global.this._grid <= 0 then
|
||||
return
|
||||
end
|
||||
if #this._grid <= 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local chunk = table.remove(global.this._grid)
|
||||
local x = _common.get_axis(chunk, "x")
|
||||
local y = _common.get_axis(chunk, "y")
|
||||
local chunk = table.remove(this._grid)
|
||||
local x = _common.get_axis(chunk, 'x')
|
||||
local y = _common.get_axis(chunk, 'y')
|
||||
|
||||
chunk = {
|
||||
left_top = {
|
||||
x = x * 32,
|
||||
y = y * 32
|
||||
},
|
||||
right_bottom = {
|
||||
x = (x * 32) + 32,
|
||||
y = (y * 32) + 32,
|
||||
}
|
||||
}
|
||||
chunk = {
|
||||
left_top = {
|
||||
x = x * 32,
|
||||
y = y * 32
|
||||
},
|
||||
right_bottom = {
|
||||
x = (x * 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
|
||||
local cache = layer.cache
|
||||
if #cache >= 1 then
|
||||
if layer.type == "LuaTile" then
|
||||
_do_job_tile(surf, layer)
|
||||
elseif layer.type == "LuaEntity" then
|
||||
_do_job_entity(surf, layer)
|
||||
end
|
||||
for _, layer in pairs(this._layers) do
|
||||
local cache = layer.cache
|
||||
if #cache >= 1 then
|
||||
if layer.type == 'LuaTile' then
|
||||
_do_job_tile(surf, layer)
|
||||
elseif layer.type == 'LuaEntity' then
|
||||
_do_job_entity(surf, layer)
|
||||
end
|
||||
|
||||
layer.cache = {}
|
||||
end
|
||||
end
|
||||
layer.cache = {}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return public
|
||||
|
@ -1,12 +1,18 @@
|
||||
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()
|
||||
if global.this == nil then
|
||||
global.this = {}
|
||||
end
|
||||
|
||||
global.this.timers = {}
|
||||
this.timers = {}
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -15,18 +21,20 @@ set_timer - Sets a timer.
|
||||
@param hook - Action executed after timer is elapsed.
|
||||
--]]
|
||||
public.set_timer = function(left, hook)
|
||||
local id = game.tick
|
||||
local entry = {
|
||||
left = left,
|
||||
hook_finish = hook,
|
||||
hook_update = nil,
|
||||
deps = nil,
|
||||
running = false,
|
||||
last_update = 0,
|
||||
}
|
||||
local id = game.tick
|
||||
local token = Token.register(hook)
|
||||
local token2 = Token.register(left)
|
||||
local entry = {
|
||||
left = token2,
|
||||
hook_finish = token,
|
||||
hook_update = nil,
|
||||
deps = nil,
|
||||
running = false,
|
||||
last_update = 0
|
||||
}
|
||||
|
||||
global.this.timers[id] = entry
|
||||
return id
|
||||
this.timers[id] = entry
|
||||
return id
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -36,7 +44,8 @@ timers is updated.
|
||||
@param hook - Hook that will be executed per update.
|
||||
--]]
|
||||
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
|
||||
|
||||
--[[
|
||||
@ -45,7 +54,8 @@ set_timer_dependency - Adds dependency into user callback.
|
||||
@param deps - Dependency of timer to add.
|
||||
--]]
|
||||
public.set_timer_dependency = function(id, deps)
|
||||
global.this.timers[id].deps = deps
|
||||
local token = Token.register(deps)
|
||||
this.timers[id].deps = token
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -53,8 +63,8 @@ set_timer_start - Sets the timer to run.
|
||||
@param id - Id of a timer.
|
||||
--]]
|
||||
public.set_timer_start = function(id)
|
||||
global.this.timers[id].running = true
|
||||
global.this.timers[id].last_update = game.tick
|
||||
this.timers[id].running = true
|
||||
this.timers[id].last_update = game.tick
|
||||
end
|
||||
|
||||
--[[
|
||||
@ -62,37 +72,37 @@ kill_timer - Effectivly kills the timer.
|
||||
@param id - Timer id.
|
||||
--]]
|
||||
public.kill_timer = function(id)
|
||||
global.this.timers[id] = nil
|
||||
this.timers[id] = nil
|
||||
end
|
||||
|
||||
--[[
|
||||
do_job - Execute timer logic within a tick.
|
||||
--]]
|
||||
public.do_job = function()
|
||||
for id, entry in pairs(global.this.timers) do
|
||||
if entry.running == false then
|
||||
goto continue
|
||||
end
|
||||
for id, entry in pairs(this.timers) do
|
||||
if entry.running == false then
|
||||
goto continue
|
||||
end
|
||||
|
||||
entry.left = entry.left - (game.tick - entry.last_update)
|
||||
if entry.left > 0 then
|
||||
entry.last_update = game.tick
|
||||
entry.left = entry.left - (game.tick - entry.last_update)
|
||||
if entry.left > 0 then
|
||||
entry.last_update = game.tick
|
||||
|
||||
if entry.hook_update ~= nil then
|
||||
if not entry.hook_update(entry.left, entry.deps) then
|
||||
goto premature_finish
|
||||
if entry.hook_update ~= nil then
|
||||
if not entry.hook_update(entry.left, entry.deps) then
|
||||
goto premature_finish
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
goto continue
|
||||
end
|
||||
goto continue
|
||||
end
|
||||
|
||||
::premature_finish::
|
||||
entry.hook_finish(entry.deps)
|
||||
global.this.timers[id] = nil
|
||||
::premature_finish::
|
||||
entry.hook_finish(entry.deps)
|
||||
this.timers[id] = nil
|
||||
|
||||
::continue::
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
return public
|
||||
|
Loading…
Reference in New Issue
Block a user