1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-19 21:10:19 +02:00

make functions local where we can

This commit is contained in:
Gerkiz 2021-03-22 12:57:58 +01:00
parent 9e08248edf
commit b3e938ddf9
7 changed files with 90 additions and 63 deletions

View File

@ -16,8 +16,11 @@ local MapConfig = require 'maps.planet_prison.config'
local Token = require 'utils.token' local Token = require 'utils.token'
local this = {} local this = {}
local floor = math.floor
local ceil = math.ceil
local Public = {} local Public = {}
local insert = table.insert local insert = table.insert
local remove = table.remove
Global.register( Global.register(
this, this,
@ -1016,11 +1019,11 @@ local function raid_event(surf)
agent.destroy() agent.destroy()
end end
table.remove(agents, j) remove(agents, j)
end end
if #agents == 0 then if #agents == 0 then
table.remove(group, i) remove(group, i)
end end
end end
@ -1142,6 +1145,9 @@ local function mined_wreckage(e)
if not valid_ents[ent.name] then if not valid_ents[ent.name] then
return return
end end
e.buffer.clear()
local candidates = {} local candidates = {}
local chance = CommonFunctions.rand_range(0, 1000) local chance = CommonFunctions.rand_range(0, 1000)
@ -1401,6 +1407,10 @@ local function on_entity_died(e)
hostile_death(e) hostile_death(e)
character_death(e) character_death(e)
ClaimsFunctions.on_entity_died(e.entity) ClaimsFunctions.on_entity_died(e.entity)
if valid_ents[e.entity.name] then
e.entity.destroy()
end
end end
local function merchant_exploit_check(ent) local function merchant_exploit_check(ent)
@ -1454,15 +1464,15 @@ end
local function stringify_color(color) local function stringify_color(color)
local r, g, b = color.r, color.g, color.b local r, g, b = color.r, color.g, color.b
if r <= 1 then if r <= 1 then
r = math.floor(r * 255) r = floor(r * 255)
end end
if g <= 1 then if g <= 1 then
g = math.floor(g * 255) g = floor(g * 255)
end end
if b <= 1 then if b <= 1 then
b = math.floor(b * 255) b = floor(b * 255)
end end
return string.format('%d,%d,%d', r, g, b) return string.format('%d,%d,%d', r, g, b)
@ -1534,7 +1544,7 @@ local function on_research_finished(e)
local reward = { local reward = {
name = 'coin', name = 'coin',
count = math.ceil(r.research_unit_count * 3) count = ceil(r.research_unit_count * 3)
} }
local f = r.force local f = r.force
for _, player in pairs(f.players) do for _, player in pairs(f.players) do

View File

@ -1,6 +1,8 @@
local Public = {}
local CommonFunctions = require 'maps.planet_prison.mod.common' local CommonFunctions = require 'maps.planet_prison.mod.common'
local Public = {}
local remove = table.remove
Public.command = { Public.command = {
--[[ --[[
@param args nil @param args nil
@ -125,7 +127,7 @@ local function _do_job_attack_objects(surf, args)
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) remove(agents, i)
goto continue goto continue
end end

View File

@ -1,4 +1,3 @@
local Public = {}
local CommonFunctions = require 'maps.planet_prison.mod.common' local CommonFunctions = require 'maps.planet_prison.mod.common'
local Global = require 'utils.global' local Global = require 'utils.global'
local Token = require 'utils.token' local Token = require 'utils.token'
@ -7,6 +6,10 @@ local this = {
_bps = {} _bps = {}
} }
local Public = {}
local insert = table.insert
local remove = table.remove
Global.register( Global.register(
this, this,
function(tbl) function(tbl)
@ -144,8 +147,8 @@ Public.unlink_references_filtered = function(name, query)
end end
end end
table.insert(refs, ref) insert(refs, ref)
table.remove(object.refs, i) remove(object.refs, i)
::continue:: ::continue::
end end
@ -190,12 +193,12 @@ Public.destroy_references_filtered = function(surf, name, query)
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) insert(tiles, tile)
end end
surf.set_tiles(tiles) surf.set_tiles(tiles)
table.remove(object.refs, i) remove(object.refs, i)
::continue:: ::continue::
end end
end end
@ -223,7 +226,7 @@ local _bp_destroy_reference = function(surf, ref)
end end
tile.name = 'concrete' tile.name = 'concrete'
table.insert(tiles, tile) insert(tiles, tile)
::continue:: ::continue::
end end
@ -241,7 +244,7 @@ Public.destroy_reference = function(surf, reference)
local ref = meta.refs[i] local ref = meta.refs[i]
if reference.id == ref.id then if reference.id == ref.id then
_bp_destroy_reference(surf, ref) _bp_destroy_reference(surf, ref)
table.remove(meta.refs, i) remove(meta.refs, i)
return return
end end
end end
@ -252,7 +255,7 @@ local function _build_tiles(surf, point, tiles)
local _tiles = {} local _tiles = {}
local get_axis = CommonFunctions.get_axis local get_axis = CommonFunctions.get_axis
fmaps.planet_prison.modor _, ile in pairs(tiles) do for _, tile in pairs(tiles) do
local _tile = { local _tile = {
name = tile.name, name = tile.name,
position = { position = {
@ -260,7 +263,7 @@ local function _build_tiles(surf, point, tiles)
y = get_axis(tile.position, 'y') + get_axis(point, 'y') y = get_axis(tile.position, 'y') + get_axis(point, 'y')
} }
} }
table.insert(_tiles, _tile) insert(_tiles, _tile)
end end
surf.set_tiles(_tiles) surf.set_tiles(_tiles)
@ -271,7 +274,7 @@ local function _build_entities(surf, point, entities, hook, args)
local _entities = {} local _entities = {}
local get_axis = CommonFunctions.get_axis local get_axis = CommonFunctions.get_axis
fmaps.planet_prison.modor _, nt 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'),
@ -300,7 +303,7 @@ local function _build_entities(surf, point, entities, hook, args)
end end
end end
table.insert(_entities, e) insert(_entities, e)
::continue:: ::continue::
end end
@ -341,8 +344,8 @@ Public.build = function(surf, name, point, args)
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 = CommonFunctions.create_bounding_box_by_points(imaps.planet_prison.modnstance tiles) local bb = CommonFunctions.create_bounding_box_by_points(instance.tiles)
table.insert(bbs, bb) insert(bbs, bb)
local query = { local query = {
name = 'character', name = 'character',
@ -359,8 +362,8 @@ Public.build = function(surf, name, point, args)
local entities = object.bp.entities local entities = object.bp.entities
if entities and #entities > 0 then if entities and #entities > 0 then
instance.entities = _build_entities(surf, point, entities, object.hook, args) instance.entities = _build_entities(surf, point, entities, object.hook, args)
local bb = CommonFunctions.create_bounding_box_by_points(imaps.planet_prison.modnstance entities) local bb = CommonFunctions.create_bounding_box_by_points(instance.entities)
table.insert(bbs, bb) insert(bbs, bb)
local query = { local query = {
name = 'character', name = 'character',
@ -383,9 +386,9 @@ Public.build = function(surf, name, point, args)
end end
end end
instance.bb = CommonFunctions.merge_bounding_boxes(bbs instance.bb = CommonFunctions.merge_bounding_boxes(bbs)
imaps.planet_prison.modnstance.id game.tick instance.id = game.tick
table.insert(object.refs, instance) insert(object.refs, instance)
return instance return instance
end end

View File

@ -1,8 +1,10 @@
local Public = {}
local CommonFunctions = require 'maps.planet_prison.mod.common' local CommonFunctions = require 'maps.planet_prison.mod.common'
local Global = require 'utils.global' local Global = require 'utils.global'
local Public = {}
local this = {} local this = {}
local insert = table.insert
local remove = table.remove
Global.register( Global.register(
this, this,
@ -43,7 +45,7 @@ local function claim_new_claim(ent)
claims[ent.force.name].collections = {} claims[ent.force.name].collections = {}
end end
table.insert(claims[ent.force.name].collections, point) insert(claims[ent.force.name].collections, point)
end end
local function claim_on_build_entity(ent) local function claim_on_build_entity(ent)
@ -73,7 +75,7 @@ local function claim_on_build_entity(ent)
x = CommonFunctions.get_axis(ent.position, 'x'), x = CommonFunctions.get_axis(ent.position, 'x'),
y = CommonFunctions.get_axis(ent.position, 'y') y = CommonFunctions.get_axis(ent.position, 'y')
} }
table.insert(points, point) insert(points, point)
data.claims[i] = CommonFunctions.get_convex_hull(points) data.claims[i] = CommonFunctions.get_convex_hull(points)
break break
@ -124,7 +126,7 @@ local function claim_on_entity_died(ent)
for j = 1, #points do for j = 1, #points do
local point = points[j] local point = points[j]
if CommonFunctions.positions_equal(point, ent.position) then if CommonFunctions.positions_equal(point, ent.position) then
table.remove(points, j) remove(points, j)
data.claims[i] = CommonFunctions.get_convex_hull(points) data.claims[i] = CommonFunctions.get_convex_hull(points)
break break
@ -132,8 +134,8 @@ local function claim_on_entity_died(ent)
end end
if #points == 0 then if #points == 0 then
table.remove(data.claims, i) remove(data.claims, i)
table.remove(data.collections, i) remove(data.collections, i)
break break
end end
end end
@ -215,7 +217,7 @@ Public.set_visibility_to = function(name)
end end
end end
table.insert(this._claims_visible_to, name) insert(this._claims_visible_to, name)
claims_update_visiblity() claims_update_visiblity()
end end
@ -227,7 +229,7 @@ Public.remove_visibility_from = function(name)
for i = 1, #this._claims_visible_to do for i = 1, #this._claims_visible_to do
local p = this._claims_visible_to[i] local p = this._claims_visible_to[i]
if p == name then if p == name then
table.remove(this._claims_visible_to, i) remove(this._claims_visible_to, i)
claims_update_visiblity() claims_update_visiblity()
break break
end end

View File

@ -1,7 +1,13 @@
local Public = {}
local Global = require 'utils.global' local Global = require 'utils.global'
local Public = {}
local this = {} local this = {}
local insert = table.insert
local remove = table.remove
local random = math.random
local sqrt = math.sqrt
local floor = math.floor
local atan2 = math.atan2
Global.register( Global.register(
this, this,
@ -16,7 +22,7 @@ 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)
return math.random(start, stop) return random(start, stop)
end end
--[[ --[[
@ -117,7 +123,7 @@ Public.get_distance = function(a, b)
local h = (Public.get_axis(a, 'x') - Public.get_axis(b, 'x')) ^ 2 local h = (Public.get_axis(a, 'x') - Public.get_axis(b, 'x')) ^ 2
local v = (Public.get_axis(a, 'y') - Public.get_axis(b, 'y')) ^ 2 local v = (Public.get_axis(a, 'y') - Public.get_axis(b, 'y')) ^ 2
return math.sqrt(h + v) return sqrt(h + v)
end end
--[[ --[[
@ -320,9 +326,9 @@ get_time - Return strigified time of a tick.
@param ticks - Just a ticks. @param ticks - Just a ticks.
--]] --]]
Public.get_time = function(ticks) Public.get_time = function(ticks)
local seconds = math.floor((ticks / 60) % 60) local seconds = floor((ticks / 60) % 60)
local minutes = math.floor((ticks / 60 / 60) % 60) local minutes = floor((ticks / 60 / 60) % 60)
local hours = math.floor(ticks / 60 / 60 / 60) local hours = floor(ticks / 60 / 60 / 60)
local time local time
if hours > 0 then if hours > 0 then
@ -342,7 +348,7 @@ polygon_insert - Append vertex in clockwise order.
@param vertices - Tables of vertices. @param vertices - Tables of vertices.
--]] --]]
Public.polygon_append_vertex = function(vertices, vertex) Public.polygon_append_vertex = function(vertices, vertex)
table.insert(vertices, vertex) insert(vertices, vertex)
local x_avg, y_avg = 0, 0 local x_avg, y_avg = 0, 0
for _, v in pairs(vertices) do for _, v in pairs(vertices) do
@ -358,12 +364,12 @@ Public.polygon_append_vertex = function(vertices, vertex)
local v = vertices[j] local v = vertices[j]
delta_x = Public.get_axis(v, 'x') - x_avg delta_x = Public.get_axis(v, 'x') - x_avg
delta_y = Public.get_axis(v, 'y') - y_avg delta_y = Public.get_axis(v, 'y') - y_avg
rad1 = ((math.atan2(delta_x, delta_y) * (180 / 3.14)) + 360) % 360 rad1 = ((atan2(delta_x, delta_y) * (180 / 3.14)) + 360) % 360
v = vertices[j + 1] v = vertices[j + 1]
delta_x = Public.get_axis(v, 'x') - x_avg delta_x = Public.get_axis(v, 'x') - x_avg
delta_y = Public.get_axis(v, 'y') - y_avg delta_y = Public.get_axis(v, 'y') - y_avg
rad2 = ((math.atan2(delta_x, delta_y) * (180 / 3.14)) + 360) % 360 rad2 = ((atan2(delta_x, delta_y) * (180 / 3.14)) + 360) % 360
if rad1 > rad2 then if rad1 > rad2 then
vertices[j], vertices[j + 1] = vertices[j + 1], vertices[j] vertices[j], vertices[j + 1] = vertices[j + 1], vertices[j]
end end
@ -471,7 +477,7 @@ Public.get_convex_hull = function(_vertices)
end end
end end
table.remove(vertices, lowest_index) remove(vertices, lowest_index)
x1 = Public.get_axis(lowest, 'x') x1 = Public.get_axis(lowest, 'x')
y1 = Public.get_axis(lowest, 'y') y1 = Public.get_axis(lowest, 'y')
@ -484,12 +490,12 @@ Public.get_convex_hull = function(_vertices)
v = vertices[j] v = vertices[j]
x2 = Public.get_axis(v, 'x') x2 = Public.get_axis(v, 'x')
y2 = Public.get_axis(v, 'y') y2 = Public.get_axis(v, 'y')
rad1 = (math.atan2(y2 - y1, x2 - x1) * (180 / 3.14) + 320) % 360 rad1 = (atan2(y2 - y1, x2 - x1) * (180 / 3.14) + 320) % 360
v = vertices[j + 1] v = vertices[j + 1]
x2 = Public.get_axis(v, 'x') x2 = Public.get_axis(v, 'x')
y2 = Public.get_axis(v, 'y') y2 = Public.get_axis(v, 'y')
rad2 = (math.atan2(y2 - y1, x2 - x1) * (180 / 3.14) + 320) % 360 rad2 = (atan2(y2 - y1, x2 - x1) * (180 / 3.14) + 320) % 360
if rad1 > rad2 then if rad1 > rad2 then
vertices[j + 1], vertices[j] = vertices[j], vertices[j + 1] vertices[j + 1], vertices[j] = vertices[j], vertices[j + 1]
@ -497,9 +503,9 @@ Public.get_convex_hull = function(_vertices)
dist1 = Public.get_distance(lowest, vertices[j]) dist1 = Public.get_distance(lowest, vertices[j])
dist2 = Public.get_distance(lowest, vertices[j + 1]) dist2 = Public.get_distance(lowest, vertices[j + 1])
if dist1 > dist2 then if dist1 > dist2 then
table.remove(vertices, j + 1) remove(vertices, j + 1)
else else
table.remove(vertices, j) remove(vertices, j)
end end
end end
@ -524,13 +530,13 @@ Public.get_convex_hull = function(_vertices)
point = vertices[i] point = vertices[i]
while #stack > 1 and convex_hull_turn(point, rev(stack, 1), rev(stack)) >= 0 do while #stack > 1 and convex_hull_turn(point, rev(stack, 1), rev(stack)) >= 0 do
table.remove(stack) remove(stack)
end end
table.insert(stack, point) insert(stack, point)
end end
table.insert(stack, lowest) insert(stack, lowest)
return stack return stack
end end

View File

@ -1,10 +1,12 @@
local Public = {}
local CommonFunctions = require 'maps.planet_prison.mod.common' local CommonFunctions = require 'maps.planet_prison.mod.common'
local SimplexFunctions = require 'maps.planet_prison.mod.simplex_noise' local SimplexFunctions = require 'maps.planet_prison.mod.simplex_noise'
local Token = require 'utils.token' local Token = require 'utils.token'
local Global = require 'utils.global' local Global = require 'utils.global'
local Public = {}
local this = {} local this = {}
local insert = table.insert
local remove = table.remove
Global.register( Global.register(
this, this,
@ -25,7 +27,7 @@ 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(this._grid, chunk) insert(this._grid, chunk)
end end
--[[ --[[
@ -33,7 +35,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(this._exclusions, bb) insert(this._exclusions, bb)
end end
--[[ --[[
@ -44,7 +46,7 @@ Public.remove_excluding_bounding_box = function(bb)
for i = 1, #this._exclusions do for i = 1, #this._exclusions do
local box = this._exclusions[i] local box = this._exclusions[i]
if box == bb then if box == bb then
table.remove(this._exclusions, i) remove(this._exclusions, i)
break break
end end
end end
@ -70,7 +72,7 @@ Public.add_noise_layer = function(type, name, objects, elevation, resolution)
deps = nil deps = nil
} }
table.insert(this._layers, layer) insert(this._layers, layer)
end end
--[[ --[[
@ -175,7 +177,7 @@ local function _do_job(surf, x, y)
name = object_name, name = object_name,
position = point position = point
} }
table.insert(layer.cache, object) insert(layer.cache, object)
break break
::continue:: ::continue::
@ -192,7 +194,7 @@ Public.do_job = function(surf)
return return
end end
local chunk = table.remove(this._grid) local chunk = remove(this._grid)
local x = CommonFunctions.get_axis(chunk, 'x') local x = CommonFunctions.get_axis(chunk, 'x')
local y = CommonFunctions.get_axis(chunk, 'y') local y = CommonFunctions.get_axis(chunk, 'y')

View File

@ -1,10 +1,12 @@
-- Original implementation taken from here https://github.com/thenumbernine/lua-simplexnoise/blob/master/2d.lua -- Original implementation taken from here https://github.com/thenumbernine/lua-simplexnoise/blob/master/2d.lua
local Public = {}
local Global = require 'utils.global' local Global = require 'utils.global'
local CommonFunctions = require 'maps.planet_prison.mod.common' local CommonFunctions = require 'maps.planet_prison.mod.common'
local Public = {}
local this = {} local this = {}
local sqrt = math.sqrt
local floor = math.floor
Global.register( Global.register(
this, this,
@ -309,11 +311,11 @@ Public.get = function(pos, resolution)
local x = (CommonFunctions.get_axis(pos, 'x') + this.seed) * resolution local x = (CommonFunctions.get_axis(pos, 'x') + this.seed) * resolution
local y = (CommonFunctions.get_axis(pos, 'y') + this.seed) * resolution local y = (CommonFunctions.get_axis(pos, 'y') + this.seed) * resolution
local n0, n1, n2 local n0, n1, n2
local F2 = 0.5 * (math.sqrt(3.0) - 1.0) local F2 = 0.5 * (sqrt(3.0) - 1.0)
local s = (x + y) * F2 local s = (x + y) * F2
local i = math.floor(x + s) local i = floor(x + s)
local j = math.floor(y + s) local j = floor(y + s)
local G2 = (3.0 - math.sqrt(3.0)) / 6.0 local G2 = (3.0 - sqrt(3.0)) / 6.0
local t = (i + j) * G2 local t = (i + j) * G2
local X0 = i - t local X0 = i - t
local Y0 = j - t local Y0 = j - t