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