1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-17 20:58:13 +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 this = {}
local floor = math.floor
local ceil = math.ceil
local Public = {}
local insert = table.insert
local remove = table.remove
Global.register(
this,
@ -1016,11 +1019,11 @@ local function raid_event(surf)
agent.destroy()
end
table.remove(agents, j)
remove(agents, j)
end
if #agents == 0 then
table.remove(group, i)
remove(group, i)
end
end
@ -1142,6 +1145,9 @@ local function mined_wreckage(e)
if not valid_ents[ent.name] then
return
end
e.buffer.clear()
local candidates = {}
local chance = CommonFunctions.rand_range(0, 1000)
@ -1401,6 +1407,10 @@ local function on_entity_died(e)
hostile_death(e)
character_death(e)
ClaimsFunctions.on_entity_died(e.entity)
if valid_ents[e.entity.name] then
e.entity.destroy()
end
end
local function merchant_exploit_check(ent)
@ -1454,15 +1464,15 @@ end
local function stringify_color(color)
local r, g, b = color.r, color.g, color.b
if r <= 1 then
r = math.floor(r * 255)
r = floor(r * 255)
end
if g <= 1 then
g = math.floor(g * 255)
g = floor(g * 255)
end
if b <= 1 then
b = math.floor(b * 255)
b = floor(b * 255)
end
return string.format('%d,%d,%d', r, g, b)
@ -1534,7 +1544,7 @@ local function on_research_finished(e)
local reward = {
name = 'coin',
count = math.ceil(r.research_unit_count * 3)
count = ceil(r.research_unit_count * 3)
}
local f = r.force
for _, player in pairs(f.players) do

View File

@ -1,6 +1,8 @@
local Public = {}
local CommonFunctions = require 'maps.planet_prison.mod.common'
local Public = {}
local remove = table.remove
Public.command = {
--[[
@param args nil
@ -125,7 +127,7 @@ local function _do_job_attack_objects(surf, args)
for i = #agents, 1, -1 do
agent = agents[i]
if not agent.valid then
table.remove(agents, i)
remove(agents, i)
goto continue
end

View File

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

View File

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

View File

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

View File

@ -1,10 +1,12 @@
local Public = {}
local CommonFunctions = require 'maps.planet_prison.mod.common'
local SimplexFunctions = require 'maps.planet_prison.mod.simplex_noise'
local Token = require 'utils.token'
local Global = require 'utils.global'
local Public = {}
local this = {}
local insert = table.insert
local remove = table.remove
Global.register(
this,
@ -25,7 +27,7 @@ push_chunk - Pushes chunk position into a grid for later processing.
@param chunk - ChunkPosition
--]]
Public.push_chunk = function(chunk)
table.insert(this._grid, chunk)
insert(this._grid, chunk)
end
--[[
@ -33,7 +35,7 @@ add_excluding_bounding_box - Pushes bounding box into exclusion list.
@param bb - BoundindBox.
--]]
Public.push_excluding_bounding_box = function(bb)
table.insert(this._exclusions, bb)
insert(this._exclusions, bb)
end
--[[
@ -44,7 +46,7 @@ Public.remove_excluding_bounding_box = function(bb)
for i = 1, #this._exclusions do
local box = this._exclusions[i]
if box == bb then
table.remove(this._exclusions, i)
remove(this._exclusions, i)
break
end
end
@ -70,7 +72,7 @@ Public.add_noise_layer = function(type, name, objects, elevation, resolution)
deps = nil
}
table.insert(this._layers, layer)
insert(this._layers, layer)
end
--[[
@ -175,7 +177,7 @@ local function _do_job(surf, x, y)
name = object_name,
position = point
}
table.insert(layer.cache, object)
insert(layer.cache, object)
break
::continue::
@ -192,7 +194,7 @@ Public.do_job = function(surf)
return
end
local chunk = table.remove(this._grid)
local chunk = remove(this._grid)
local x = CommonFunctions.get_axis(chunk, 'x')
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
local Public = {}
local Global = require 'utils.global'
local CommonFunctions = require 'maps.planet_prison.mod.common'
local Public = {}
local this = {}
local sqrt = math.sqrt
local floor = math.floor
Global.register(
this,
@ -309,11 +311,11 @@ Public.get = function(pos, resolution)
local x = (CommonFunctions.get_axis(pos, 'x') + this.seed) * resolution
local y = (CommonFunctions.get_axis(pos, 'y') + this.seed) * resolution
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 i = math.floor(x + s)
local j = math.floor(y + s)
local G2 = (3.0 - math.sqrt(3.0)) / 6.0
local i = floor(x + s)
local j = floor(y + s)
local G2 = (3.0 - sqrt(3.0)) / 6.0
local t = (i + j) * G2
local X0 = i - t
local Y0 = j - t