mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-07-13 01:20:19 +02:00
Fixed desync issues due to global state
This commit is contained in:
@ -4,14 +4,21 @@
|
|||||||
]]
|
]]
|
||||||
|
|
||||||
-- dependencies
|
-- dependencies
|
||||||
|
local Global = require 'utils.global'
|
||||||
|
|
||||||
-- this
|
-- this
|
||||||
local AlienEvolutionProgress = {}
|
local AlienEvolutionProgress = {}
|
||||||
|
|
||||||
global.alien_spawner_cache = {
|
local biter_cache = {}
|
||||||
biters = {},
|
local spitter_cache = {}
|
||||||
spitters = {},
|
|
||||||
}
|
Global.register({
|
||||||
|
biter_cache = biter_cache,
|
||||||
|
spitter_cache_slots = spitter_cache,
|
||||||
|
}, function(tbl)
|
||||||
|
biter_cache = tbl.biter_cache
|
||||||
|
spitter_cache = tbl.spitter_cache
|
||||||
|
end)
|
||||||
|
|
||||||
-- values are in the form {evolution, weight}
|
-- values are in the form {evolution, weight}
|
||||||
local biters = {
|
local biters = {
|
||||||
@ -89,20 +96,22 @@ end
|
|||||||
|
|
||||||
function AlienEvolutionProgress.getBiterValues(evolution)
|
function AlienEvolutionProgress.getBiterValues(evolution)
|
||||||
local evolution_cache_key = evolution * 100
|
local evolution_cache_key = evolution * 100
|
||||||
if (nil == global.alien_spawner_cache.biters[evolution_cache_key]) then
|
|
||||||
global.alien_spawner_cache.biters[evolution_cache_key] = get_values(biters, evolution)
|
if (nil == biter_cache[evolution_cache_key]) then
|
||||||
|
biter_cache[evolution_cache_key] = get_values(biters, evolution)
|
||||||
end
|
end
|
||||||
|
|
||||||
return global.alien_spawner_cache.biters[evolution_cache_key]
|
return biter_cache[evolution_cache_key]
|
||||||
end
|
end
|
||||||
|
|
||||||
function AlienEvolutionProgress.getSpitterValues(evolution)
|
function AlienEvolutionProgress.getSpitterValues(evolution)
|
||||||
local evolution_cache_key = evolution * 100
|
local evolution_cache_key = evolution * 100
|
||||||
if (nil == global.alien_spawner_cache.spitters[evolution_cache_key]) then
|
|
||||||
global.alien_spawner_cache.biters[evolution_cache_key] = get_values(spitters, evolution)
|
if (nil == spitter_cache[evolution_cache_key]) then
|
||||||
|
spitter_cache[evolution_cache_key] = get_values(spitters, evolution)
|
||||||
end
|
end
|
||||||
|
|
||||||
return global.alien_spawner_cache.biters[evolution_cache_key]
|
return spitter_cache[evolution_cache_key]
|
||||||
end
|
end
|
||||||
|
|
||||||
function AlienEvolutionProgress.getBitersByEvolution(total_biters, evolution)
|
function AlienEvolutionProgress.getBitersByEvolution(total_biters, evolution)
|
||||||
@ -110,11 +119,11 @@ function AlienEvolutionProgress.getBitersByEvolution(total_biters, evolution)
|
|||||||
local map = AlienEvolutionProgress.getBiterValues(evolution)
|
local map = AlienEvolutionProgress.getBiterValues(evolution)
|
||||||
|
|
||||||
for i = 1, total_biters do
|
for i = 1, total_biters do
|
||||||
local random = get_name_by_random(map)
|
local name = get_name_by_random(map)
|
||||||
if (nil == biters_calculated[random]) then
|
if (nil == biters_calculated[name]) then
|
||||||
biters_calculated[random] = 1
|
biters_calculated[name] = 1
|
||||||
else
|
else
|
||||||
biters_calculated[random] = biters_calculated[random] + 1
|
biters_calculated[name] = biters_calculated[name] + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -126,11 +135,11 @@ function AlienEvolutionProgress.getSpittersByEvolution(total_spitters, evolution
|
|||||||
local map = AlienEvolutionProgress.getSpitterValues(evolution)
|
local map = AlienEvolutionProgress.getSpitterValues(evolution)
|
||||||
|
|
||||||
for i = 1, total_spitters do
|
for i = 1, total_spitters do
|
||||||
local random = get_name_by_random(map)
|
local name = get_name_by_random(map)
|
||||||
if (nil == spitters_calculated[random]) then
|
if (nil == spitters_calculated[name]) then
|
||||||
spitters_calculated[random] = 1
|
spitters_calculated[name] = 1
|
||||||
else
|
else
|
||||||
spitters_calculated[random] = spitters_calculated[random] + 1
|
spitters_calculated[name] = spitters_calculated[name] + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -16,9 +16,6 @@ local Config = {
|
|||||||
|
|
||||||
-- initial starting position size, values higher than 30 might break
|
-- initial starting position size, values higher than 30 might break
|
||||||
starting_size = 8,
|
starting_size = 8,
|
||||||
|
|
||||||
-- the daytime value used for cave lighting
|
|
||||||
daytime = 0.5,
|
|
||||||
},
|
},
|
||||||
SetupPlayer = {
|
SetupPlayer = {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
|
@ -9,6 +9,7 @@ local Template = require 'map_gen.Diggy.Template'
|
|||||||
local Debug = require 'map_gen.Diggy.Debug'
|
local Debug = require 'map_gen.Diggy.Debug'
|
||||||
local Task = require 'utils.Task'
|
local Task = require 'utils.Task'
|
||||||
local Token = require 'utils.global_token'
|
local Token = require 'utils.global_token'
|
||||||
|
local Global = require 'utils.global'
|
||||||
|
|
||||||
-- this
|
-- this
|
||||||
local DiggyCaveCollapse = {}
|
local DiggyCaveCollapse = {}
|
||||||
@ -32,22 +33,27 @@ local disc_value = 0
|
|||||||
local ring_value = 0
|
local ring_value = 0
|
||||||
|
|
||||||
local enable_stress_grid = 0
|
local enable_stress_grid = 0
|
||||||
local stress_map_blur_add = nil
|
local stress_map_blur_add
|
||||||
local mask_disc_blur = nil
|
local mask_disc_blur
|
||||||
local stress_map_check_stress_in_threshold = nil
|
local stress_map_check_stress_in_threshold
|
||||||
local support_beam_entities = nil
|
local support_beam_entities
|
||||||
local on_surface_created = nil
|
local on_surface_created
|
||||||
|
|
||||||
local stress_threshold_causing_collapse = 0.9
|
local stress_threshold_causing_collapse = 0.9
|
||||||
|
|
||||||
global.deconstruction_alert_message_shown = {}
|
local deconstruction_alert_message_shown = {}
|
||||||
local deconstruction_alert_message_shown = global.deconstruction_alert_message_shown
|
local stress_map_storage = {}
|
||||||
|
local new_tile_map = {}
|
||||||
|
|
||||||
global.stress_map_storage = {}
|
Global.register({
|
||||||
local stress_map_storage = global.stress_map_storage
|
new_tile_map = new_tile_map,
|
||||||
|
stress_map_storage = stress_map_storage,
|
||||||
global.new_tile_map = {}
|
deconstruction_alert_message_shown = deconstruction_alert_message_shown,
|
||||||
local new_tile_map = global.new_tile_map
|
}, function(tbl)
|
||||||
|
new_tile_map = tbl.new_tile_map
|
||||||
|
stress_map_storage = tbl.stress_map_storage
|
||||||
|
deconstruction_alert_message_shown = tbl.deconstruction_alert_message_shown
|
||||||
|
end)
|
||||||
|
|
||||||
local defaultValue = 0
|
local defaultValue = 0
|
||||||
|
|
||||||
|
@ -9,28 +9,38 @@ local Task = require 'utils.Task'
|
|||||||
local Gui = require 'utils.gui'
|
local Gui = require 'utils.gui'
|
||||||
local Debug = require 'map_gen.Diggy.Debug'
|
local Debug = require 'map_gen.Diggy.Debug'
|
||||||
local Template = require 'map_gen.Diggy.Template'
|
local Template = require 'map_gen.Diggy.Template'
|
||||||
|
local Global = require 'utils.global'
|
||||||
|
|
||||||
-- this
|
-- this
|
||||||
local MarketExchange = {}
|
local MarketExchange = {}
|
||||||
|
|
||||||
local config
|
local config = {}
|
||||||
global.MarketExchange = {
|
|
||||||
first_time_market_item = {},
|
local stone_tracker = {
|
||||||
|
first_time_market_item = nil,
|
||||||
stone_sent_to_surface = 0,
|
stone_sent_to_surface = 0,
|
||||||
previous_stone_sent_to_surface = 0,
|
previous_stone_sent_to_surface = 0,
|
||||||
mining_efficiency = {
|
|
||||||
active_modifier = 0,
|
|
||||||
research_modifier = 0,
|
|
||||||
market_modifier = 0,
|
|
||||||
},
|
|
||||||
inventory_slots = {
|
|
||||||
active_modifier = 0,
|
|
||||||
research_modifier = 0,
|
|
||||||
market_modifier = 0,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
local mining_efficiency = global.MarketExchange.mining_efficiency
|
local mining_efficiency = {
|
||||||
local inventory_slots = global.MarketExchange.inventory_slots
|
active_modifier = 0,
|
||||||
|
research_modifier = 0,
|
||||||
|
market_modifier = 0,
|
||||||
|
}
|
||||||
|
local inventory_slots = {
|
||||||
|
active_modifier = 0,
|
||||||
|
research_modifier = 0,
|
||||||
|
market_modifier = 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
Global.register({
|
||||||
|
stone_tracker = stone_tracker,
|
||||||
|
mining_efficiency = mining_efficiency,
|
||||||
|
inventory_slots = inventory_slots,
|
||||||
|
}, function(tbl)
|
||||||
|
stone_tracker = tbl.stone_tracker
|
||||||
|
mining_efficiency = tbl.mining_efficiency
|
||||||
|
inventory_slots = tbl.inventory_slots
|
||||||
|
end)
|
||||||
|
|
||||||
local on_market_timeout_finished = Token.register(function(params)
|
local on_market_timeout_finished = Token.register(function(params)
|
||||||
Template.market(params.surface, params.position, params.player_force, params.currency_item, {})
|
Template.market(params.surface, params.position, params.player_force, params.currency_item, {})
|
||||||
@ -68,17 +78,16 @@ local function update_inventory_slots(force)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function update_market_contents(market)
|
local function update_market_contents(market)
|
||||||
local market_exchange = global.MarketExchange
|
|
||||||
local should_update_mining_speed = false
|
local should_update_mining_speed = false
|
||||||
local should_update_inventory_slots = false
|
local should_update_inventory_slots = false
|
||||||
|
|
||||||
if (nil ~= market_exchange.first_time_market_item) then
|
if (nil ~= stone_tracker.first_time_market_item) then
|
||||||
market.add_market_item(market_exchange.first_time_market_item)
|
market.add_market_item(stone_tracker.first_time_market_item)
|
||||||
market_exchange.first_time_market_item = nil
|
stone_tracker.first_time_market_item = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, unlockable in pairs(config.unlockables) do
|
for _, unlockable in pairs(config.unlockables) do
|
||||||
local is_in_range = unlockable.stone > market_exchange.previous_stone_sent_to_surface and unlockable.stone <= market_exchange.stone_sent_to_surface
|
local is_in_range = unlockable.stone > stone_tracker.previous_stone_sent_to_surface and unlockable.stone <= stone_tracker.stone_sent_to_surface
|
||||||
|
|
||||||
-- only add the item to the market if it's between the old and new stone range
|
-- only add the item to the market if it's between the old and new stone range
|
||||||
if (is_in_range and unlockable.type == 'market') then
|
if (is_in_range and unlockable.type == 'market') then
|
||||||
@ -130,9 +139,8 @@ local function on_market_item_purchased(event)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local market_exchange = global.MarketExchange
|
stone_tracker.previous_stone_sent_to_surface = stone_tracker.stone_sent_to_surface
|
||||||
market_exchange.previous_stone_sent_to_surface = market_exchange.stone_sent_to_surface
|
stone_tracker.stone_sent_to_surface = stone_tracker.stone_sent_to_surface + (config.stone_to_surface_amount * event.count)
|
||||||
market_exchange.stone_sent_to_surface = market_exchange.stone_sent_to_surface + (config.stone_to_surface_amount * event.count)
|
|
||||||
|
|
||||||
update_market_contents(event.market)
|
update_market_contents(event.market)
|
||||||
end
|
end
|
||||||
@ -151,7 +159,7 @@ end
|
|||||||
function MarketExchange.register(cfg)
|
function MarketExchange.register(cfg)
|
||||||
config = cfg
|
config = cfg
|
||||||
|
|
||||||
global.MarketExchange.first_time_market_item = {
|
stone_tracker.first_time_market_item = {
|
||||||
price = {{config.currency_item, 50}},
|
price = {{config.currency_item, 50}},
|
||||||
offer = {type = 'nothing', effect_description = 'Send ' .. config.stone_to_surface_amount .. ' stone to the surface'}
|
offer = {type = 'nothing', effect_description = 'Send ' .. config.stone_to_surface_amount .. ' stone to the surface'}
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,6 @@ local Template = require 'map_gen.Diggy.Template'
|
|||||||
-- this
|
-- this
|
||||||
local ScatteredResources = {}
|
local ScatteredResources = {}
|
||||||
|
|
||||||
global.ScatteredResources = {
|
|
||||||
can_spawn_resources = false
|
|
||||||
}
|
|
||||||
|
|
||||||
local function get_name_by_random(collection)
|
local function get_name_by_random(collection)
|
||||||
local random = math.random()
|
local random = math.random()
|
||||||
local current = 0
|
local current = 0
|
||||||
@ -71,10 +67,6 @@ function ScatteredResources.register(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
Event.add(Template.events.on_void_removed, function(event)
|
Event.add(Template.events.on_void_removed, function(event)
|
||||||
if (not global.ScatteredResources.can_spawn_resources) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local x = event.old_tile.position.x
|
local x = event.old_tile.position.x
|
||||||
local y = event.old_tile.position.y
|
local y = event.old_tile.position.y
|
||||||
|
|
||||||
@ -90,8 +82,6 @@ function ScatteredResources.register(config)
|
|||||||
spawn_resource(config, event.surface, x, y, distance)
|
spawn_resource(config, event.surface, x, y, distance)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
global.ScatteredResources.can_spawn_resources = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function ScatteredResources.get_extra_map_info(config)
|
function ScatteredResources.get_extra_map_info(config)
|
||||||
|
@ -11,8 +11,6 @@ local DiggyCaveCollapse = require 'map_gen.Diggy.Feature.DiggyCaveCollapse'
|
|||||||
-- this
|
-- this
|
||||||
local StartingZone = {}
|
local StartingZone = {}
|
||||||
|
|
||||||
local daytime
|
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
Registers all event handlers.
|
Registers all event handlers.
|
||||||
]]
|
]]
|
||||||
@ -67,14 +65,12 @@ function StartingZone.register(config)
|
|||||||
callback_token = Token.register(on_chunk_generated)
|
callback_token = Token.register(on_chunk_generated)
|
||||||
|
|
||||||
Event.add_removable(defines.events.on_chunk_generated, callback_token)
|
Event.add_removable(defines.events.on_chunk_generated, callback_token)
|
||||||
|
|
||||||
daytime = config.daytime
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function StartingZone.on_init()
|
function StartingZone.on_init()
|
||||||
local surface = game.surfaces.nauvis
|
local surface = game.surfaces.nauvis
|
||||||
|
|
||||||
surface.daytime = daytime
|
surface.daytime = 0.5
|
||||||
surface.freeze_daytime = 1
|
surface.freeze_daytime = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ require 'utils.utils'
|
|||||||
local Scenario = {}
|
local Scenario = {}
|
||||||
|
|
||||||
-- private state
|
-- private state
|
||||||
local scenario_registered = false
|
global.diggy_scenario_registered = false
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
Allows calling a callback for each enabled feature.
|
Allows calling a callback for each enabled feature.
|
||||||
@ -41,8 +41,8 @@ end
|
|||||||
Register the events required to initialize the scenario.
|
Register the events required to initialize the scenario.
|
||||||
]]
|
]]
|
||||||
function Scenario.register(debug)
|
function Scenario.register(debug)
|
||||||
if scenario_registered then
|
if global.diggy_scenario_registered then
|
||||||
error('Cannot register the scenario multiple times.')
|
error('Cannot register the Diggy scenario multiple times.')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ function Scenario.register(debug)
|
|||||||
ScenarioInfo.set_map_description('Dig your way through!')
|
ScenarioInfo.set_map_description('Dig your way through!')
|
||||||
ScenarioInfo.set_map_extra_info(extra_map_info)
|
ScenarioInfo.set_map_extra_info(extra_map_info)
|
||||||
|
|
||||||
scenario_registered = true
|
global.diggy_scenario_registered = true
|
||||||
end
|
end
|
||||||
|
|
||||||
return Scenario
|
return Scenario
|
||||||
|
Reference in New Issue
Block a user