Research GUI (#302)

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua

* Update research.lua
This commit is contained in:
2024-07-23 03:24:42 +09:00
committed by GitHub
parent 02d6a23c2d
commit ed88770314
3 changed files with 302 additions and 482 deletions

View File

@@ -1,108 +1,44 @@
local Event = require 'utils.event' --- @dep utils.event
local Common = require 'expcore.common' --- @dep utils.event
local Global = require 'utils.global' --- @dep utils.global
local config = require 'config.research' --- @dep config.research
local config_bonus = Common.opt_require 'config.bonus' --- @dep config.bonus
local Event = require 'utils.event' --- @dep utils.event
local Commands = require 'expcore.commands' --- @dep expcore.commands
local format_time = _C.format_time --- @dep expcore.common
local config = require 'config.research' --- @dep config.research
local research = {}
Global.register(research, function(tbl)
research = tbl
end)
local research_time_format = {hours=true, minutes=true, seconds=true, time=true, string=true}
research.res_queue_enable = false
local base_rate = 0
if config.bonus.enabled then
for k, _ in pairs(config_bonus.force_bonus) do
if config_bonus.force_bonus[k].name == config.bonus.name then
base_rate = config_bonus.force_bonus[k].max
end
end
end
local function research_notification(event)
local is_inf_res = false
for i=1, #config.inf_res do
if (event.research.name == config.inf_res[i].name) and (event.research.level >= config.inf_res[i].level) then
is_inf_res = true
end
end
if config.bonus_inventory.enabled then
if (event.research.force.mining_drill_productivity_bonus * 10) <= (config.bonus_inventory.limit / config.bonus_inventory.rate) then
if event.research.force.technologies['toolbelt'].researched then
event.research.force[config.bonus_inventory.name] = (math.floor(event.research.force.mining_drill_productivity_bonus * 10) * config.bonus_inventory.rate) + 10
else
event.research.force[config.bonus_inventory.name] = math.floor(event.research.force.mining_drill_productivity_bonus * 10) * config.bonus_inventory.rate
end
end
end
if is_inf_res then
if event.research.name == 'mining-productivity-4' and event.research.level > 4 then
if config.bonus.enabled then
event.research.force[config.bonus.name] = base_rate + event.research.level * config.bonus.rate
end
if config.pollution_ageing_by_research then
game.map_settings.pollution.ageing = math.min(10, event.research.level / 5)
end
end
if not (event.by_script) then
game.print{'expcom-res.inf', format_time(game.tick, research_time_format), event.research.name, event.research.level - 1}
end
else
if not (event.by_script) then
game.print{'expcom-res.msg', format_time(game.tick, research_time_format), event.research.name}
end
end
end
local function res_queue(event)
if event.research.force.rockets_launched == 0 or event.research.force.technologies['mining-productivity-4'].level <= 4 then
return
end
local res_q = event.research.research_queue
local function res_queue(force, res, by_script)
local res_q = force.research_queue
if #res_q < config.queue_amount then
for i=1, config.queue_amount - #res_q do
event.research.force.add_research(event.research.force.technologies['mining-productivity-4'])
force.add_research(force.technologies['mining-productivity-4'])
if not (event.by_script) then
game.print{'expcom-res.inf-q', event.research.name, event.research.level + i}
if not (by_script) then
game.print{'expcom-res.inf-q', res.name, res.level + i}
end
end
end
end
local function research_queue_logic(event)
research_notification(event)
if research.res_queue_enable then
res_queue(event)
end
end
Event.add(defines.events.on_research_finished, research_queue_logic)
Event.add(defines.events.on_research_cancelled, research_queue_logic)
Commands.new_command('auto-research', 'Automatically queue up research')
:add_alias('ares')
:register(function(player)
research.res_queue_enable = not research.res_queue_enable
if research.res_queue_enable then
res_queue(player.force)
res_queue(player.force, nil, true)
end
game.print{'expcom-res.res', player.name, research.res_queue_enable}
return Commands.success
end)
Event.add(defines.events.on_research_finished, function(event)
if research.res_queue_enable then
if event.research.force.rockets_launched > 0 and event.research.force.technologies['mining-productivity-4'].level > 4 then
res_queue(event.research.force, {name=event.research.name, level=event.research.level}, event.by_script)
end
end
end)

View File

@@ -1,4 +1,4 @@
--- research milestone gui
--- research gui
-- @gui Research
local Gui = require 'expcore.gui' --- @dep expcore.gui
@@ -14,11 +14,57 @@ Global.register(research, function(tbl)
end)
research.time = {}
local res = {}
local res_i = {}
research.res_queue_enable = false
local research_time_format = {
hours=true,
minutes=true,
seconds=true,
time=true,
string=true
}
local empty_time = format_time(0, {
hours=true,
minutes=true,
seconds=true,
time=true,
string=true,
null=true
})
local res = {
['lookup_name'] = {},
['disp'] = {}
}
local res_total = 0
local research_time_format = {hours=true, minutes=true, seconds=true, time=true, string=true}
local empty_time = format_time(0, {hours=true, minutes=true, seconds=true, time=true, string=true, null=true})
local mi = 1
for k, v in pairs(config.milestone) do
research.time[mi] = 0
res['lookup_name'][k] = mi
res_total = res_total + v * 60
res['disp'][mi] = {
name = '[technology=' .. k .. '] ' .. k:gsub('-', ' '),
raw_name = k,
prev = res_total,
prev_disp = format_time(res_total, research_time_format),
}
mi = mi + 1
end
local function add_log()
local result_data = {}
for i=1, #research.time, 1 do
result_data[res['disp'][i]['raw_name']] = research.time[i]
end
game.write_file(config.file_name, game.table_to_json(result_data) .. '\n', true, 0)
end
local function research_res_n(res_)
local res_n = 1
@@ -38,111 +84,160 @@ local function research_res_n(res_)
if res_n < 3 then
res_n = 3
elseif res_n > (#research.time - 5) then
res_n = #research.time - 5
end
return res_n
end
for i=1, #config.milestone do
res_total = res_total + config.milestone[i].time * 60
res_i[config.milestone[i].name] = i
research.time[i] = 0
res[i] = {
name = '[technology=' .. config.milestone[i].name .. '] ' .. config.milestone[i].name:gsub('-', ' '),
prev = res_total,
prev_disp = format_time(res_total, research_time_format),
}
local function research_notification(event)
local is_inf_res = false
if config.inf_res[event.research.name] then
if event.research.name == 'mining-productivity-4' and event.research.level == 5 then
-- Add run result to log
add_log()
end
if event.research.level >= config.inf_res[event.research.name] then
is_inf_res = true
end
end
if is_inf_res then
if event.research.name == 'mining-productivity-4' then
if config.bonus_inventory.enabled then
if (event.research.level - 1) <= math.ceil(config.bonus_inventory.limit / config.bonus_inventory.rate) then
event.research.force[config.bonus_inventory.name] = math.max((event.research.level - 1) * config.bonus_inventory.rate, config.bonus_inventory.limit)
end
end
if config.pollution_ageing_by_research then
game.map_settings.pollution.ageing = math.min(10, event.research.level / 5)
end
end
if not (event.by_script) then
game.print{'expcom-res.inf', format_time(game.tick, research_time_format), event.research.name, event.research.level - 1}
end
else
if not (event.by_script) then
game.print{'expcom-res.msg', format_time(game.tick, research_time_format), event.research.name}
end
if event.research.name == 'mining-productivity-1' or event.research.name == 'mining-productivity-2' or event.research.name == 'mining-productivity-3' then
if config.bonus_inventory.enabled then
event.research.force[config.bonus_inventory.name] = event.research.level * config.bonus_inventory.rate
end
end
end
end
--- Display label for the clock display
-- @element research_gui_clock_display
local research_gui_clock =
Gui.element{
type = 'label',
name = Gui.unique_static_name,
caption = empty_time,
style = 'heading_1_label'
}
--- A vertical flow containing the clock
-- @element research_clock_set
local research_clock_set =
Gui.element(function(_, parent, name)
local research_set = parent.add{type='flow', direction='vertical', name=name}
local disp = Gui.scroll_table(research_set, 360, 1, 'disp')
research_gui_clock(disp)
return research_set
end)
--- A vertical flow containing the data
-- @element research_data_set
local research_data_set =
Gui.element(function(_, parent, name)
local research_set = parent.add{type='flow', direction='vertical', name=name}
local disp = Gui.scroll_table(research_set, 360, 4, 'disp')
for i=1, 8, 1 do
disp.add{
type = 'label',
name = 'research_display_n_' .. i,
caption = '',
style = 'heading_1_label'
}
disp.add{
type = 'label',
name = 'research_display_d_' .. i,
caption = empty_time,
style = 'heading_1_label'
}
disp.add{
type = 'label',
name = 'research_display_p_' .. i,
caption = '',
style = 'heading_1_label'
}
disp.add{
type = 'label',
name = 'research_display_t_' .. i,
caption = empty_time,
style = 'heading_1_label'
}
end
local res_n = research_res_n(res['disp'])
for j=1, 8, 1 do
local res_j = res_n + j - 3
if res['disp'][res_j] then
local res_r = res['disp'][res_j]
disp['research_display_n_' .. j].caption = res_r.name
if research.time[res_j] == 0 then
disp['research_display_d_' .. j].caption = empty_time
disp['research_display_p_' .. j].caption = res_r.prev_disp
disp['research_display_t_' .. j].caption = empty_time
else
if research.time[res_j] < res['disp'][res_j].prev then
disp['research_display_d_' .. j].caption = '-' .. format_time(res['disp'][res_j].prev - research.time[res_j], research_time_format)
else
disp['research_display_d_' .. j].caption = format_time(research.time[res_j] - res['disp'][res_j].prev, research_time_format)
end
disp['research_display_p_' .. j].caption = res_r.prev_disp
disp['research_display_t_' .. j].caption = format_time(research.time[res_j], research_time_format)
end
else
disp['research_display_n_' .. j].caption = ''
disp['research_display_d_' .. j].caption = ''
disp['research_display_p_' .. j].caption = ''
disp['research_display_t_' .. j].caption = ''
end
end
return research_set
end)
local research_container =
Gui.element(function(definition, parent)
local container = Gui.container(parent, definition.name, 200)
local scroll_table = Gui.scroll_table(container, 400, 4)
local container = Gui.container(parent, definition.name, 320)
scroll_table.add{
name = 'clock_text',
caption = 'Time:',
type = 'label',
style = 'heading_1_label'
}
scroll_table.add{
name = 'clock_text_2',
caption = '',
type = 'label',
style = 'heading_1_label'
}
scroll_table.add{
name = 'clock_text_3',
caption = '',
type = 'label',
style = 'heading_1_label'
}
scroll_table.add{
name = 'clock_display',
caption = empty_time,
type = 'label',
style = 'heading_1_label'
}
for i=1, 8 do
scroll_table.add{
name = 'research_display_n_' .. i,
caption = '',
type = 'label',
style = 'heading_1_label'
}
scroll_table.add{
name = 'research_display_d_' .. i,
caption = empty_time,
type = 'label',
style = 'heading_1_label'
}
scroll_table.add{
name = 'research_display_p_' .. i,
caption = '',
type = 'label',
style = 'heading_1_label'
}
scroll_table.add{
name = 'research_display_t_' .. i,
caption = empty_time,
type = 'label',
style = 'heading_1_label'
}
end
local res_n = research_res_n(res)
for j=1, 8 do
local res_j = res_n + j - 3
if res[res_j] ~= nil then
local res_r = res[res_j]
scroll_table['research_display_n_' .. j].caption = res_r.name
if research.time[res_j] < res[res_j].prev then
scroll_table['research_display_d_' .. j].caption = '-' .. format_time(res[res_j].prev - research.time[res_j], research_time_format)
else
scroll_table['research_display_d_' .. j].caption = format_time(research.time[res_j] - res[res_j].prev, research_time_format)
end
scroll_table['research_display_p_' .. j].caption = res_r.prev_disp
scroll_table['research_display_t_' .. j].caption = format_time(research.time[res_j], research_time_format)
else
scroll_table['research_display_n_' .. j].caption = ''
scroll_table['research_display_d_' .. j].caption = ''
scroll_table['research_display_p_' .. j].caption = ''
scroll_table['research_display_t_' .. j].caption = ''
end
end
research_clock_set(container, 'research_st_1')
research_data_set(container, 'research_st_2')
return container.parent
end)
@@ -154,35 +249,42 @@ Gui.left_toolbar_button('item/space-science-pack', {'expcom-res.main-tooltip'},
end)
Event.add(defines.events.on_research_finished, function(event)
if event.research.name == nil then
return
elseif res_i[event.research.name] == nil then
research_notification(event)
if res['lookup_name'][event.research.name] == nil then
return
end
local n_i = res_i[event.research.name]
local n_i = res['lookup_name'][event.research.name]
research.time[n_i] = game.tick
local res_n = research_res_n(res)
local res_n = research_res_n(res['disp'])
local res_disp = {}
for j=1, 8 do
for j=1, 8, 1 do
local res_j = res_n + j - 3
res_disp[j] = {}
if res[res_j] ~= nil then
local res_r = res[res_j]
if res['disp'][res_j] then
local res_r = res['disp'][res_j]
res_disp[j]['n'] = res_r.name
if research.time[res_j] < res[res_j].prev then
res_disp[j]['d'] = '-' .. format_time(res[res_j].prev - research.time[res_j], research_time_format)
if research.time[res_j] == 0 then
res_disp[j]['d'] = empty_time
res_disp[j]['p']= res_r.prev_disp
res_disp[j]['t'] = empty_time
else
res_disp[j]['d'] = format_time(research.time[res_j] - res[res_j].prev, research_time_format)
end
if research.time[res_j] < res['disp'][res_j].prev then
res_disp[j]['d'] = '-' .. format_time(res['disp'][res_j].prev - research.time[res_j], research_time_format)
res_disp[j]['p'] = res_r.prev_disp
res_disp[j]['t'] = format_time(research.time[res_j], research_time_format)
else
res_disp[j]['d'] = format_time(research.time[res_j] - res['disp'][res_j].prev, research_time_format)
end
res_disp[j]['p'] = res_r.prev_disp
res_disp[j]['t'] = format_time(research.time[res_j], research_time_format)
end
else
res_disp[j]['n'] = ''
@@ -194,12 +296,13 @@ Event.add(defines.events.on_research_finished, function(event)
for _, player in pairs(game.connected_players) do
local frame = Gui.get_left_element(player, research_container)
local disp = frame.container['research_st_2'].disp.table
for j=1, 8 do
frame.container.scroll.table['research_display_n_' .. j].caption = res_disp[j]['n']
frame.container.scroll.table['research_display_d_' .. j].caption = res_disp[j]['d']
frame.container.scroll.table['research_display_p_' .. j].caption = res_disp[j]['p']
frame.container.scroll.table['research_display_t_' .. j].caption = res_disp[j]['t']
for j=1, 8, 1 do
disp['research_display_n_' .. j].caption = res_disp[j]['n']
disp['research_display_d_' .. j].caption = res_disp[j]['d']
disp['research_display_p_' .. j].caption = res_disp[j]['p']
disp['research_display_t_' .. j].caption = res_disp[j]['t']
end
end
end)
@@ -209,6 +312,7 @@ Event.on_nth_tick(60, function()
for _, player in pairs(game.connected_players) do
local frame = Gui.get_left_element(player, research_container)
frame.container.scroll.table['clock_display'].caption = current_time
local disp = frame.container['research_st_1'].disp.table
disp[research_gui_clock.name].caption = current_time
end
end)