mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 08:23:13 +09:00
Add 2.0 support to research_sync
This commit is contained in:
1 file changed
+43
-24
@@ -1,8 +1,8 @@
|
|||||||
local clusterio_api = require("modules/clusterio/api")
|
local clusterio_api = require("modules/clusterio/api")
|
||||||
|
local compat = require("modules/clusterio/compat")
|
||||||
|
|
||||||
local sync = {}
|
local sync = {}
|
||||||
|
|
||||||
|
|
||||||
local function get_technology_progress(tech)
|
local function get_technology_progress(tech)
|
||||||
if tech == tech.force.current_research then
|
if tech == tech.force.current_research then
|
||||||
return tech.force.research_progress
|
return tech.force.research_progress
|
||||||
@@ -19,26 +19,39 @@ local function set_technology_progress(tech, progress)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
sync.events = {}
|
--- @param no_early_return boolean?
|
||||||
sync.events[clusterio_api.events.on_server_startup] = function(event)
|
--- @return table
|
||||||
if not global.research_sync then
|
local function get_script_data(no_early_return)
|
||||||
global.research_sync = {
|
local script_data = compat.script_data()
|
||||||
technologies = {},
|
local research_sync = script_data.research_sync
|
||||||
}
|
if research_sync and not no_early_return then
|
||||||
|
return research_sync
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Used when syncing completed technologies from the controller
|
research_sync = research_sync or {}
|
||||||
global.research_sync.ignore_research_finished = false
|
research_sync.technologies = research_sync.technologies or {}
|
||||||
|
|
||||||
|
if research_sync.ignore_research_finished == nil then
|
||||||
|
research_sync.ignore_research_finished = false
|
||||||
|
end
|
||||||
|
|
||||||
local force = game.forces["player"]
|
local force = game.forces["player"]
|
||||||
for _, tech in pairs(force.technologies) do
|
for _, tech in pairs(force.technologies) do
|
||||||
local progress = get_technology_progress(tech)
|
local progress = get_technology_progress(tech)
|
||||||
global.research_sync.technologies[tech.name] = {
|
research_sync.technologies[tech.name] = {
|
||||||
level = tech.level,
|
level = tech.level,
|
||||||
researched = tech.researched,
|
researched = tech.researched,
|
||||||
progress = progress,
|
progress = progress,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
script_data.research_sync = research_sync
|
||||||
|
return research_sync
|
||||||
|
end
|
||||||
|
|
||||||
|
sync.events = {}
|
||||||
|
sync.events[clusterio_api.events.on_server_startup] = function(event)
|
||||||
|
get_script_data(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_contribution(tech)
|
local function get_contribution(tech)
|
||||||
@@ -47,7 +60,8 @@ local function get_contribution(tech)
|
|||||||
return 0, nil
|
return 0, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local prev_tech = global.research_sync.technologies[tech.name]
|
local research_sync = get_script_data()
|
||||||
|
local prev_tech = research_sync.technologies[tech.name]
|
||||||
if prev_tech.progress and prev_tech.level == tech.level then
|
if prev_tech.progress and prev_tech.level == tech.level then
|
||||||
return progress - prev_tech.progress, progress
|
return progress - prev_tech.progress, progress
|
||||||
else
|
else
|
||||||
@@ -63,7 +77,8 @@ local function send_contribution(tech)
|
|||||||
level = tech.level,
|
level = tech.level,
|
||||||
contribution = contribution,
|
contribution = contribution,
|
||||||
})
|
})
|
||||||
global.research_sync.technologies[tech.name].progress = progress
|
local research_sync = get_script_data()
|
||||||
|
research_sync.technologies[tech.name].progress = progress
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -75,12 +90,13 @@ sync.events[defines.events.on_research_started] = function(event)
|
|||||||
end
|
end
|
||||||
|
|
||||||
sync.events[defines.events.on_research_finished] = function(event)
|
sync.events[defines.events.on_research_finished] = function(event)
|
||||||
if global.research_sync.ignore_research_finished then
|
local research_sync = get_script_data()
|
||||||
|
if research_sync.ignore_research_finished then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local tech = event.research
|
local tech = event.research
|
||||||
global.research_sync.technologies[tech.name] = {
|
research_sync.technologies[tech.name] = {
|
||||||
level = tech.level,
|
level = tech.level,
|
||||||
researched = tech.researched,
|
researched = tech.researched,
|
||||||
}
|
}
|
||||||
@@ -121,7 +137,7 @@ function research_sync.dump_technologies()
|
|||||||
if #techs == 0 then
|
if #techs == 0 then
|
||||||
rcon.print("[]")
|
rcon.print("[]")
|
||||||
else
|
else
|
||||||
rcon.print(game.table_to_json(techs))
|
rcon.print(compat.table_to_json(techs))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -133,8 +149,9 @@ function research_sync.sync_technologies(data)
|
|||||||
local progressIndex = 3
|
local progressIndex = 3
|
||||||
local researchedIndex = 4
|
local researchedIndex = 4
|
||||||
|
|
||||||
global.research_sync.ignore_research_finished = true
|
local research_sync = get_script_data()
|
||||||
for _, tech_data in pairs(game.json_to_table(data)) do
|
research_sync.ignore_research_finished = true
|
||||||
|
for _, tech_data in pairs(compat.json_to_table(data) --[[@as table]]) do
|
||||||
local tech = force.technologies[tech_data[nameIndex]]
|
local tech = force.technologies[tech_data[nameIndex]]
|
||||||
if tech and tech.level <= tech_data[levelIndex] then
|
if tech and tech.level <= tech_data[levelIndex] then
|
||||||
local new_level = math.min(tech_data[levelIndex], tech.prototype.max_level)
|
local new_level = math.min(tech_data[levelIndex], tech.prototype.max_level)
|
||||||
@@ -162,18 +179,19 @@ function research_sync.sync_technologies(data)
|
|||||||
progress = get_technology_progress(tech)
|
progress = get_technology_progress(tech)
|
||||||
end
|
end
|
||||||
|
|
||||||
global.research_sync.technologies[tech.name] = {
|
research_sync.technologies[tech.name] = {
|
||||||
level = tech.level,
|
level = tech.level,
|
||||||
researched = tech.researched,
|
researched = tech.researched,
|
||||||
progress = progress,
|
progress = progress,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
global.research_sync.ignore_research_finished = false
|
research_sync.ignore_research_finished = false
|
||||||
end
|
end
|
||||||
|
|
||||||
function research_sync.update_progress(data)
|
function research_sync.update_progress(data)
|
||||||
local techs = game.json_to_table(data)
|
local research_sync = get_script_data()
|
||||||
|
local techs = compat.json_to_table(data) --[[@as table]]
|
||||||
local force = game.forces["player"]
|
local force = game.forces["player"]
|
||||||
|
|
||||||
for _, controllerTech in ipairs(techs) do
|
for _, controllerTech in ipairs(techs) do
|
||||||
@@ -181,7 +199,7 @@ function research_sync.update_progress(data)
|
|||||||
if tech and tech.level == controllerTech.level then
|
if tech and tech.level == controllerTech.level then
|
||||||
send_contribution(tech)
|
send_contribution(tech)
|
||||||
set_technology_progress(tech, controllerTech.progress)
|
set_technology_progress(tech, controllerTech.progress)
|
||||||
global.research_sync.technologies[tech.name] = {
|
research_sync.technologies[tech.name] = {
|
||||||
level = tech.level,
|
level = tech.level,
|
||||||
progress = controllerTech.progress
|
progress = controllerTech.progress
|
||||||
}
|
}
|
||||||
@@ -200,7 +218,8 @@ function research_sync.research_technology(name, level)
|
|||||||
level = tech.prototype.max_level
|
level = tech.prototype.max_level
|
||||||
end
|
end
|
||||||
|
|
||||||
global.research_sync.ignore_research_finished = true
|
local research_sync = get_script_data()
|
||||||
|
research_sync.ignore_research_finished = true
|
||||||
if tech == force.current_research and tech.level == level then
|
if tech == force.current_research and tech.level == level then
|
||||||
force.research_progress = 1
|
force.research_progress = 1
|
||||||
|
|
||||||
@@ -215,9 +234,9 @@ function research_sync.research_technology(name, level)
|
|||||||
end
|
end
|
||||||
game.play_sound { path = "utility/research_completed" }
|
game.play_sound { path = "utility/research_completed" }
|
||||||
end
|
end
|
||||||
global.research_sync.ignore_research_finished = false
|
research_sync.ignore_research_finished = false
|
||||||
|
|
||||||
global.research_sync.technologies[tech.name] = {
|
research_sync.technologies[tech.name] = {
|
||||||
level = tech.level,
|
level = tech.level,
|
||||||
researched = tech.researched,
|
researched = tech.researched,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user