mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-03-11 14:49:24 +02:00
global util usage
This commit is contained in:
parent
97d991e6a0
commit
eab8641cf8
@ -15,7 +15,7 @@ require "modules.wave_defense.main"
|
||||
require "modules.biters_yield_coins"
|
||||
require "modules.no_deconstruction_of_neutral_entities"
|
||||
require "modules.shotgun_buff"
|
||||
require "modules.explosives"
|
||||
local Explosives = require "modules.explosives"
|
||||
require "modules.mineable_wreckage_yields_scrap"
|
||||
require "modules.rocks_broken_paint_tiles"
|
||||
require "modules.rocks_heal_over_time"
|
||||
@ -500,14 +500,12 @@ local function on_init()
|
||||
global.rocks_yield_ore_base_amount = 40
|
||||
global.rocks_yield_ore_distance_modifier = 0.020
|
||||
|
||||
global.explosion_cells_destructible_tiles = {
|
||||
["out-of-map"] = 1500,
|
||||
["water"] = 1000,
|
||||
["water-green"] = 1000,
|
||||
["deepwater-green"] = 1000,
|
||||
["deepwater"] = 1000,
|
||||
["water-shallow"] = 1000,
|
||||
}
|
||||
Explosives.set_destructible_tile("out-of-map", 1500)
|
||||
Explosives.set_destructible_tile("water", 1000)
|
||||
Explosives.set_destructible_tile("water-green", 1000)
|
||||
Explosives.set_destructible_tile("deepwater-green", 1000)
|
||||
Explosives.set_destructible_tile("deepwater", 1000)
|
||||
Explosives.set_destructible_tile("water-shallow", 1000)
|
||||
|
||||
Map_score.set_score_description("Wagon distance reached:")
|
||||
|
||||
|
@ -32,8 +32,16 @@ function Public.treasure_chest(surface, position, container_name)
|
||||
local container = surface.create_entity({name = container_name, position = position, force = "neutral"})
|
||||
for _, item_stack in pairs(item_stacks) do
|
||||
container.insert(item_stack)
|
||||
end
|
||||
end
|
||||
container.minable = false
|
||||
|
||||
for _ = 1, 2, 1 do
|
||||
if math_random(1, 4) == 1 then
|
||||
container.insert({name = "explosives", count = math_random(20, 50)})
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Public.treasure_chest_old(surface, position, container_name)
|
||||
|
@ -1,16 +1,19 @@
|
||||
--Cellular Automata Explosives by MewMew
|
||||
local Public = {}
|
||||
local Global = require 'utils.global'
|
||||
local explosives = {}
|
||||
Global.register(
|
||||
explosives,
|
||||
function(tbl)
|
||||
explosives = tbl
|
||||
end
|
||||
)
|
||||
|
||||
--Example: cell_birth(game.player.surface.index, {x = -0, y = -64}, game.tick, {x = -0, y = -64}, 100000) --100000 = damage
|
||||
|
||||
--1 steel chest filled with explosives = ~1 million damage
|
||||
local math_abs = math.abs
|
||||
local math_floor = math.floor
|
||||
local math_sqrt = math.sqrt
|
||||
local math_round = math.round
|
||||
local math_random = math.random
|
||||
local table_shuffle_table = table.shuffle_table
|
||||
local damage_per_explosive = 500
|
||||
local damage_decay = 10
|
||||
local shuffle_table = table.shuffle_table
|
||||
local speed = 3
|
||||
local density = 1
|
||||
local density_r = density * 0.5
|
||||
@ -35,13 +38,13 @@ local function cell_birth(surface_index, origin_position, origin_tick, position,
|
||||
local key = pos_to_key(position)
|
||||
|
||||
--Merge cells that are overlapping.
|
||||
if global.explosion_cells[key] then
|
||||
global.explosion_cells[key].health = global.explosion_cells[key].health + health
|
||||
if explosives.cells[key] then
|
||||
explosives.cells[key].health = explosives.cells[key].health + health
|
||||
return
|
||||
end
|
||||
|
||||
--Spawn new cell.
|
||||
global.explosion_cells[key] = {
|
||||
explosives.cells[key] = {
|
||||
surface_index = surface_index,
|
||||
origin_position = origin_position,
|
||||
origin_tick = origin_tick,
|
||||
@ -52,12 +55,12 @@ local function cell_birth(surface_index, origin_position, origin_tick, position,
|
||||
end
|
||||
|
||||
local function grow_cell(cell)
|
||||
table_shuffle_table(global.explosion_cells_vectors)
|
||||
shuffle_table(explosives.vectors)
|
||||
local radius = math_floor((game.tick - cell.origin_tick) / 9) + 2
|
||||
local positions = {}
|
||||
for i = 1, 4, 1 do
|
||||
local position = {x = cell.position.x + global.explosion_cells_vectors[i][1], y = cell.position.y + global.explosion_cells_vectors[i][2]}
|
||||
if not global.explosion_cells[pos_to_key(position)] then
|
||||
local position = {x = cell.position.x + explosives.vectors[i][1], y = cell.position.y + explosives.vectors[i][2]}
|
||||
if not explosives.cells[pos_to_key(position)] then
|
||||
local distance = math_sqrt((cell.origin_position.x - position.x) ^ 2 + (cell.origin_position.y - position.y) ^ 2)
|
||||
if distance < radius then
|
||||
positions[#positions + 1] = position
|
||||
@ -65,15 +68,15 @@ local function grow_cell(cell)
|
||||
end
|
||||
end
|
||||
|
||||
if #positions == 0 then positions[#positions + 1] = {x = cell.position.x + global.explosion_cells_vectors[1][1], y = cell.position.y + global.explosion_cells_vectors[1][2]} end
|
||||
if #positions == 0 then positions[#positions + 1] = {x = cell.position.x + explosives.vectors[1][1], y = cell.position.y + explosives.vectors[1][2]} end
|
||||
|
||||
local new_cell_health = math_round(cell.health / #positions, 3) - damage_decay
|
||||
local new_cell_health = math_round(cell.health / #positions, 3) - explosives.damage_decay
|
||||
|
||||
--[[
|
||||
if new_cell_health > 0 then
|
||||
global.explosion_cells_damage_dealt = global.explosion_cells_damage_dealt + damage_decay * #positions
|
||||
explosives.cells_damage_dealt = explosives.cells_damage_dealt + damage_decay * #positions
|
||||
else
|
||||
global.explosion_cells_damage_dealt = global.explosion_cells_damage_dealt + (new_cell_health + damage_decay) * #positions
|
||||
explosives.cells_damage_dealt = explosives.cells_damage_dealt + (new_cell_health + damage_decay) * #positions
|
||||
end
|
||||
]]
|
||||
|
||||
@ -84,28 +87,11 @@ local function grow_cell(cell)
|
||||
end
|
||||
end
|
||||
|
||||
local function reflect_cell(entity, cell)
|
||||
table_shuffle_table(global.explosion_cells_vectors)
|
||||
for i = 1, 4, 1 do
|
||||
local position = {x = cell.position.x + global.explosion_cells_vectors[i][1], y = cell.position.y + global.explosion_cells_vectors[i][2]}
|
||||
if global.explosion_cells[pos_to_key(position)] then
|
||||
cell_birth(cell.surface_index, cell.origin_position, cell.origin_tick, position, cell.health)
|
||||
entity.damage(global.explosion_cells_reflect[entity.name] * 0.01 * math.random(75, 125), "player", "explosion")
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function damage_entity(entity, cell)
|
||||
if not entity.valid then return true end
|
||||
if not entity.health then return true end
|
||||
if entity.health <= 0 then return true end
|
||||
if not entity.destructible then return true end
|
||||
--if not entity.minable then return true end
|
||||
--if global.explosion_cells_reflect[entity.name] then
|
||||
-- if reflect_cell(entity, cell) then return end
|
||||
--end
|
||||
|
||||
local damage_required = entity.health
|
||||
for _ = 1, 4, 1 do
|
||||
@ -136,22 +122,18 @@ local function damage_area(cell)
|
||||
end
|
||||
|
||||
local tile = surface.get_tile(cell.position)
|
||||
if global.explosion_cells_destructible_tiles[tile.name] then
|
||||
if explosives.destructible_tiles[tile.name] then
|
||||
local key = pos_to_key(tile.position)
|
||||
if not global.explosion_cells_tiles[key] then global.explosion_cells_tiles[key] = global.explosion_cells_destructible_tiles[tile.name] end
|
||||
if not explosives.tiles[key] then explosives.tiles[key] = explosives.destructible_tiles[tile.name] end
|
||||
|
||||
if cell.health > global.explosion_cells_tiles[key] then
|
||||
--global.explosion_cells_damage_dealt = global.explosion_cells_damage_dealt + global.explosion_cells_tiles[key]
|
||||
|
||||
cell.health = cell.health - global.explosion_cells_tiles[key]
|
||||
global.explosion_cells_tiles[key] = nil
|
||||
if cell.health > explosives.tiles[key] then
|
||||
cell.health = cell.health - explosives.tiles[key]
|
||||
explosives.tiles[key] = nil
|
||||
if math_abs(tile.position.y) < surface.map_gen_settings.height * 0.5 and math_abs(tile.position.x) < surface.map_gen_settings.width * 0.5 then
|
||||
surface.set_tiles({{name = "landfill", position = tile.position}}, true)
|
||||
end
|
||||
else
|
||||
--global.explosion_cells_damage_dealt = global.explosion_cells_damage_dealt + cell.health
|
||||
|
||||
global.explosion_cells_tiles[key] = global.explosion_cells_tiles[key] - cell.health
|
||||
explosives.tiles[key] = explosives.tiles[key] - cell.health
|
||||
return
|
||||
end
|
||||
end
|
||||
@ -165,13 +147,13 @@ local function life_cycle(cell)
|
||||
end
|
||||
|
||||
local function tick(event)
|
||||
for key, cell in pairs(global.explosion_cells) do
|
||||
for key, cell in pairs(explosives.cells) do
|
||||
if cell.spawn_tick < game.tick then
|
||||
life_cycle(cell)
|
||||
global.explosion_cells[key] = nil
|
||||
explosives.cells[key] = nil
|
||||
end
|
||||
end
|
||||
if game.tick % 216000 == 0 then global.explosion_cells_tiles = {} end
|
||||
if game.tick % 216000 == 0 then explosives.tiles = {} end
|
||||
end
|
||||
|
||||
local function on_entity_died(event)
|
||||
@ -187,25 +169,33 @@ local function on_entity_died(event)
|
||||
if not amount then return end
|
||||
if amount < 1 then return end
|
||||
|
||||
cell_birth(entity.surface.index, {x = entity.position.x, y = entity.position.y}, game.tick, {x = entity.position.x, y = entity.position.y}, amount * damage_per_explosive)
|
||||
cell_birth(entity.surface.index, {x = entity.position.x, y = entity.position.y}, game.tick, {x = entity.position.x, y = entity.position.y}, amount * explosives.damage_per_explosive)
|
||||
end
|
||||
|
||||
function Public.reset()
|
||||
explosives.cells = {}
|
||||
explosives.tiles = {}
|
||||
if not explosives.vectors then explosives.vectors = {{density, 0}, {density * -1, 0}, {0, density}, {0, density * -1}} end
|
||||
if not explosives.damage_per_explosive then explosives.damage_per_explosive = 500 end
|
||||
if not explosives.damage_decay then explosives.damage_decay = 10 end
|
||||
if not explosives.destructible_tiles then explosives.destructible_tiles = {} end
|
||||
end
|
||||
|
||||
function Public.set_destructible_tile(tile_name, health)
|
||||
explosives.destructible_tiles[tile_name] = health
|
||||
end
|
||||
|
||||
function Public.get_table()
|
||||
return explosives
|
||||
end
|
||||
|
||||
local function on_init()
|
||||
global.explosion_cells = {}
|
||||
global.explosion_cells_vectors = {{density, 0}, {density * -1, 0}, {0, density}, {0, density * -1}}
|
||||
--global.explosion_cells_damage_dealt = 0
|
||||
--global.explosion_cells_reflect = {
|
||||
-- ["stone-wall"] = 25,
|
||||
--}
|
||||
global.explosion_cells_tiles = {}
|
||||
global.explosion_cells_destructible_tiles = {
|
||||
["water"] = false,
|
||||
["deepwater"] = false,
|
||||
["out-of-map"] = false,
|
||||
}
|
||||
Public.reset()
|
||||
end
|
||||
|
||||
local event = require 'utils.event'
|
||||
event.on_init(on_init)
|
||||
event.on_nth_tick(speed, tick)
|
||||
event.add(defines.events.on_entity_died, on_entity_died)
|
||||
local Event = require 'utils.event'
|
||||
Event.on_init(on_init)
|
||||
Event.on_nth_tick(speed, tick)
|
||||
Event.add(defines.events.on_entity_died, on_entity_died)
|
||||
|
||||
return Public
|
Loading…
x
Reference in New Issue
Block a user