1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-11-25 22:32:18 +02:00
Files
ComfyFactorio/modules/rpg/table.lua

600 lines
16 KiB
Lua
Raw Normal View History

2020-07-25 17:22:04 +02:00
-- one table to rule them all!
local Global = require 'utils.global'
2025-06-10 23:15:50 +02:00
local Task = require 'utils.task_token'
2020-07-25 17:22:04 +02:00
local Event = require 'utils.event'
local Gui = require 'utils.gui'
2025-06-10 23:15:50 +02:00
local this =
{
2020-07-25 17:22:04 +02:00
rpg_extra = {},
rpg_t = {}
2020-07-25 17:22:04 +02:00
}
--! Gui Frames
local settings_frame_name = Gui.uid_name()
2022-04-18 01:18:25 +02:00
local settings_tooltip_frame = Gui.uid_name()
local close_settings_tooltip_frame = Gui.uid_name()
local settings_tooltip_name = Gui.uid_name()
2020-07-25 17:22:04 +02:00
local save_button_name = Gui.uid_name()
local discard_button_name = Gui.uid_name()
local draw_main_frame_name = Gui.uid_name()
2022-04-05 19:28:08 +02:00
local close_main_frame_name = Gui.uid_name()
2020-07-25 17:22:04 +02:00
local main_frame_name = Gui.uid_name()
local settings_button_name = Gui.uid_name()
local spell_gui_button_name = Gui.uid_name()
local spell_gui_frame_name = Gui.uid_name()
2021-07-02 10:08:35 +02:00
local enable_spawning_frame_name = Gui.uid_name()
local spell1_button_name = Gui.uid_name()
local spell2_button_name = Gui.uid_name()
local spell3_button_name = Gui.uid_name()
2024-05-29 19:57:58 +02:00
local cooldown_indicator_name = Gui.uid_name()
2020-07-25 17:22:04 +02:00
Global.register(
this,
2024-11-05 20:04:00 +01:00
function (tbl)
2020-07-25 17:22:04 +02:00
this = tbl
end
)
local Public = {}
2025-06-10 23:15:50 +02:00
Public.events =
{
2024-05-27 20:30:03 +02:00
on_spell_cast_success = Event.generate_event_name('on_spell_cast_success'),
on_spell_cast_failure = Event.generate_event_name('on_spell_cast_failure')
}
2020-07-25 17:22:04 +02:00
Public.points_per_level = 5
2024-11-05 20:04:00 +01:00
Public.experience_levels = { 0 }
2021-11-14 21:01:01 +01:00
for a = 1, 4999, 1 do -- max level
2020-11-15 19:23:54 +01:00
Public.experience_levels[#Public.experience_levels + 1] = Public.experience_levels[#Public.experience_levels] + a * 8
2020-07-25 17:22:04 +02:00
end
2025-06-10 23:15:50 +02:00
Public.gui_settings_levels =
{
2021-05-25 22:19:20 +02:00
['reset_text_label'] = 50,
['stone_path_label'] = 20,
2022-04-05 20:02:13 +02:00
['aoe_punch_label'] = 30,
2021-05-25 22:19:20 +02:00
['explosive_bullets_label'] = 50
}
2025-06-10 23:15:50 +02:00
Public.die_cause =
{
2020-07-25 17:22:04 +02:00
['ammo-turret'] = true,
['electric-turret'] = true,
['fluid-turret'] = true
}
Public.nth_tick = 18001
Public.visuals_delay = 1800
2024-11-05 20:04:00 +01:00
Public.xp_floating_text_color = { 157, 157, 157 }
2020-07-25 17:22:04 +02:00
2025-06-10 23:15:50 +02:00
Public.enemy_types =
{
2020-07-25 17:22:04 +02:00
['unit'] = true,
['unit-spawner'] = true,
['turret'] = true
}
2025-06-10 23:15:50 +02:00
Public.classes =
{
2025-06-01 15:06:27 +08:00
['engineer'] = { "rpg_gui.role_engineer" },
['strength'] = { "rpg_gui.role_strength" },
['magicka'] = { "rpg_gui.role_magic" },
['dexterity'] = { "rpg_gui.role_dexterity" },
['vitality'] = { "rpg_gui.role_vitality" },
2020-07-25 17:22:04 +02:00
}
2025-06-10 23:15:50 +02:00
Public.auto_allocate_nodes =
{
2024-11-05 20:04:00 +01:00
{ 'allocations.deactivated' },
{ 'allocations.str' },
{ 'allocations.mag' },
{ 'allocations.dex' },
{ 'allocations.vit' }
2020-08-20 11:35:52 +02:00
}
2025-06-10 23:15:50 +02:00
Public.auto_allocate_nodes_func =
{
2020-12-29 13:46:42 +01:00
'Deactivated',
'Strength',
'Magicka',
'Dexterity',
'Vitality'
}
2022-07-06 18:26:28 +02:00
function Public.reset_table(migrate)
2020-07-25 17:22:04 +02:00
this.rpg_extra.debug = false
this.rpg_extra.breached_walls = 1
this.rpg_extra.reward_new_players = 0
this.rpg_extra.level_limit_enabled = false
this.rpg_extra.global_pool = 0
2021-11-08 19:20:58 +01:00
this.rpg_extra.heal_modifier = 2
2024-11-05 20:04:00 +01:00
this.rpg_extra.mana_modifier = 2
2020-07-25 17:22:04 +02:00
this.rpg_extra.personal_tax_rate = 0.3
this.rpg_extra.leftover_pool = 0
this.rpg_extra.turret_kills_to_global_pool = true
this.rpg_extra.difficulty = false
this.rpg_extra.surface_name = 'nauvis'
2021-07-02 10:10:44 +02:00
this.rpg_extra.enable_health_and_mana_bars = false
this.rpg_extra.enable_mana = false
2021-11-22 20:16:33 +01:00
this.rpg_extra.mana_limit = 100000
2021-07-02 10:10:44 +02:00
this.rpg_extra.enable_wave_defense = false
this.rpg_extra.enable_explosive_bullets = false
this.rpg_extra.enable_explosive_bullets_globally = false
this.rpg_extra.enable_range_buffs = false
2020-07-25 17:22:04 +02:00
this.rpg_extra.mana_per_tick = 0.1
2021-11-23 20:28:23 +01:00
this.rpg_extra.xp_modifier_when_mining = 0.0045
2021-07-02 10:10:44 +02:00
this.rpg_extra.force_mana_per_tick = false
this.rpg_extra.enable_stone_path = false
this.rpg_extra.enable_auto_allocate = false
2022-04-05 20:02:13 +02:00
this.rpg_extra.enable_aoe_punch = true
2023-10-15 00:22:00 +02:00
this.rpg_extra.grant_xp_level = nil
2025-02-20 01:31:24 +01:00
this.rpg_extra.check_x_position = nil
2022-04-05 20:02:13 +02:00
this.rpg_extra.enable_aoe_punch_globally = false
this.rpg_extra.disable_get_heal_modifier_from_using_fish = false
2025-06-10 23:15:50 +02:00
this.rpg_extra.tweaked_crafting_items =
{
['stone-furnace'] = true,
['wooden-chest'] = true,
['copper-cable'] = true,
['iron-stick'] = true,
['iron-gear-wheel'] = true,
['pipe'] = true
}
this.tweaked_crafting_items_enabled = false
2022-07-06 18:26:28 +02:00
if not migrate then
this.rpg_t = {}
end
2025-06-10 23:15:50 +02:00
this.rpg_extra.rpg_xp_yield =
{
2020-07-25 17:22:04 +02:00
['behemoth-biter'] = 16,
['behemoth-spitter'] = 16,
['behemoth-worm-turret'] = 64,
['big-biter'] = 8,
['big-spitter'] = 8,
['big-worm-turret'] = 48,
['biter-spawner'] = 64,
['character'] = 16,
['gun-turret'] = 8,
['laser-turret'] = 16,
['medium-biter'] = 4,
['medium-spitter'] = 4,
['medium-worm-turret'] = 32,
['small-biter'] = 1,
['small-spitter'] = 1,
['small-worm-turret'] = 16,
['spitter-spawner'] = 64
}
end
--- Gets value from table
---@param key string
2020-07-25 17:22:04 +02:00
function Public.get(key)
if key then
return this[key]
else
return this
end
end
2024-11-05 20:04:00 +01:00
--- Gets value from table
---@param key string
function Public.get_extra(key)
if key then
return this.rpg_extra[key]
else
return this.rpg_extra
end
end
2020-11-17 12:45:27 +01:00
--- Gets value from player rpg_t table
---@param key string|integer
2022-07-10 19:42:07 +02:00
---@param value string|nil
2020-11-17 12:45:27 +01:00
function Public.get_value_from_player(key, value)
if key and value then
if (this.rpg_t[key] and this.rpg_t[key][value]) then
return this.rpg_t[key][value]
end
return false
end
if key then
if this.rpg_t[key] then
return this.rpg_t[key]
end
return false
end
return false
end
2020-11-17 12:45:27 +01:00
--- Sets value to player rpg_t table
---@param key string
2022-07-10 19:42:07 +02:00
---@param value string|boolean|number|nil
---@param setter string|boolean|number|nil
2020-11-17 12:45:27 +01:00
function Public.set_value_to_player(key, value, setter)
if key and value then
if (this.rpg_t[key] and this.rpg_t[key][value]) then
this.rpg_t[key][value] = setter or false
elseif (this.rpg_t[key] and not this.rpg_t[key][value]) then
this.rpg_t[key][value] = setter or false
end
end
end
2021-05-25 22:19:20 +02:00
--- Sets a new table to rpg_t table
---@param key string
2022-08-14 21:56:11 +02:00
---@param tbl table
2021-05-25 22:19:20 +02:00
function Public.set_new_player_tbl(key, tbl)
if key and tbl then
if type(tbl) ~= 'table' then
return error('Given parameter is not a table.')
end
this.rpg_t[key] = tbl
return this.rpg_t[key]
end
end
--- Removes a player from rpg_t table
---@param index number
2021-05-25 22:19:20 +02:00
function Public.remove_player(index)
if index then
if this.rpg_t[index] then
this.rpg_t[index] = nil
end
end
end
2020-07-25 17:22:04 +02:00
--- Sets value to table
---@param key string
2023-06-22 22:57:07 +02:00
function Public.set(key, value)
if key and (value or value == false) then
this[key] = value
return this[key]
elseif key then
2020-07-25 17:22:04 +02:00
return this[key]
else
return this
end
end
2024-11-05 20:04:00 +01:00
--- Sets value to table
---@param key string
function Public.set_extra(key, value)
if key and (value or value == false) then
this.rpg_extra[key] = value
return this.rpg_extra[key]
elseif key then
return this.rpg_extra[key]
else
return this.rpg_extra
end
end
2025-02-20 01:31:24 +01:00
-- Checks if the player is in the correct position to continue with the function
function Public.set_x_position(value)
this.rpg_extra.check_x_position = value or nil
return this.rpg_extra.check_x_position
end
-- Checks if the player is in the correct position to continue with the function
function Public.get_x_position()
return this.rpg_extra.check_x_position
end
2020-07-25 17:22:04 +02:00
--- Toggle debug - when you need to troubleshoot.
2022-07-06 18:26:28 +02:00
function Public.toggle_debug(value)
this.rpg_extra.debug = value or false
2020-07-26 22:06:19 +02:00
return this.rpg_extra.debug
2020-07-25 17:22:04 +02:00
end
2021-11-14 13:02:00 +01:00
--- Toggle debug - when you need to troubleshoot.
2022-04-05 20:02:13 +02:00
function Public.toggle_debug_aoe_punch()
if this.rpg_extra.debug_aoe_punch then
this.rpg_extra.debug_aoe_punch = false
2021-11-14 13:02:00 +01:00
else
2022-04-05 20:02:13 +02:00
this.rpg_extra.debug_aoe_punch = true
2021-11-14 13:02:00 +01:00
end
2022-04-05 20:02:13 +02:00
return this.rpg_extra.debug_aoe_punch
2021-11-14 13:02:00 +01:00
end
2020-07-25 17:22:04 +02:00
--- Debug only - when you need to troubleshoot.
---@param str string
2020-07-25 17:22:04 +02:00
function Public.debug_log(str)
if not this.rpg_extra.debug then
return
end
print(str)
end
--- Sets surface name for rpg_v2 to use
2023-08-10 14:29:05 +02:00
---@param name string|table
2020-07-25 17:22:04 +02:00
function Public.set_surface_name(name)
2023-08-10 14:29:05 +02:00
if name and type(name) == 'string' then
this.rpg_extra.surface_name = name
elseif name and type(name) == 'table' then
2020-07-25 17:22:04 +02:00
this.rpg_extra.surface_name = name
else
return error('No surface name given.', 2)
end
2020-07-26 22:06:19 +02:00
return this.rpg_extra.surface_name
2020-07-25 17:22:04 +02:00
end
--- Enables the bars that shows above the player character.
--- If you disable mana but enable <enable_health_and_mana_bars> then only health will be shown
---@param value boolean
2020-07-25 17:22:04 +02:00
function Public.enable_health_and_mana_bars(value)
this.rpg_extra.enable_health_and_mana_bars = value or false
2020-07-26 22:06:19 +02:00
return this.rpg_extra.enable_health_and_mana_bars
2020-07-25 17:22:04 +02:00
end
2022-07-06 18:26:28 +02:00
--- Toggles the mod gui state.
---@param value boolean
---@param read boolean
function Public.enable_mod_gui(value, read)
if not read then
Gui.set_mod_gui_top_frame(value or false)
end
if Gui.get_mod_gui_top_frame() then
local players = game.connected_players
for i = 1, #players do
local player = players[i]
local top = player.gui.top
if top.mod_gui_top_frame and top.mod_gui_top_frame.valid then
top.mod_gui_top_frame.visible = true
end
for _, child in pairs(top.children) do
if child.caption == '[RPG]' then
child.destroy()
Public.draw_gui_char_button(player)
end
end
end
else
local players = game.connected_players
local count = 0
for i = 1, #players do
local player = players[i]
local top = Gui.get_button_flow(player)
for _, child in pairs(top.children) do
count = count + 1
if child.caption == '[RPG]' then
child.destroy()
Public.draw_gui_char_button(player)
end
end
if count == 0 then
if player.gui.top.mod_gui_top_frame and player.gui.top.mod_gui_top_frame.valid then
player.gui.top.mod_gui_top_frame.visible = false
end
else
if player.gui.top.mod_gui_top_frame and player.gui.top.mod_gui_top_frame.valid then
player.gui.top.mod_gui_top_frame.visible = true
end
end
end
end
end
2020-07-25 17:22:04 +02:00
--- Enables the mana feature that allows players to spawn entities.
---@param value boolean
2020-07-25 17:22:04 +02:00
function Public.enable_mana(value)
this.rpg_extra.enable_mana = value or false
2020-07-26 22:06:19 +02:00
return this.rpg_extra.enable_mana
2020-07-25 17:22:04 +02:00
end
--- This should only be enabled if wave_defense is enabled.
--- It boosts the amount of xp the players get after x amount of waves.
---@param value boolean
2020-07-25 17:22:04 +02:00
function Public.enable_wave_defense(value)
this.rpg_extra.enable_wave_defense = value or false
2020-07-26 22:06:19 +02:00
return this.rpg_extra.enable_wave_defense
2020-07-25 17:22:04 +02:00
end
2021-02-25 23:59:33 +01:00
--- Enables/disabled explosive bullets globally.
---@param value boolean
2021-02-25 23:59:33 +01:00
function Public.enable_explosive_bullets_globally(value)
this.rpg_extra.enable_explosive_bullets_globally = value or false
2021-02-25 23:59:33 +01:00
return this.rpg_extra.enable_explosive_bullets_globally
end
--- Fetches if the explosive bullets module is activated globally.
function Public.get_explosive_bullets_globally()
2021-02-25 23:59:33 +01:00
return this.rpg_extra.enable_explosive_bullets_globally
end
--- Fetches if the explosive bullets module is activated.
function Public.get_explosive_bullets()
return this.rpg_extra.enable_explosive_bullets
end
2021-02-25 23:59:33 +01:00
--- Enables/disabled explosive bullets.
---@param value boolean
2021-02-25 23:59:33 +01:00
function Public.enable_explosive_bullets(value)
this.rpg_extra.enable_explosive_bullets = value or false
2021-02-25 23:59:33 +01:00
return this.rpg_extra.enable_explosive_bullets
end
--- Fetches if the range buffs module is activated.
function Public.get_range_buffs()
return this.rpg_extra.enable_range_buffs
end
--- Enables/disabled range buffs.
---@param value boolean
function Public.enable_range_buffs(value)
this.rpg_extra.enable_range_buffs = value or false
return this.rpg_extra.enable_range_buffs
2021-02-25 23:59:33 +01:00
end
2020-07-25 17:22:04 +02:00
--- Enables/disabled personal tax.
2023-08-10 14:29:05 +02:00
---@param value number
2020-07-25 17:22:04 +02:00
function Public.personal_tax_rate(value)
2023-08-12 10:12:45 +02:00
this.rpg_extra.personal_tax_rate = value or 0
2020-07-26 22:06:19 +02:00
return this.rpg_extra.personal_tax_rate
2020-07-25 17:22:04 +02:00
end
--- Enables/disabled stone-path-tile creation on mined.
---@param value boolean
2020-07-25 17:22:04 +02:00
function Public.enable_stone_path(value)
this.rpg_extra.enable_stone_path = value or false
2020-07-26 22:06:19 +02:00
return this.rpg_extra.enable_stone_path
2020-07-25 17:22:04 +02:00
end
2020-08-20 11:35:52 +02:00
--- Enables/disabled auto-allocations of skill-points.
---@param value boolean
2020-08-20 11:35:52 +02:00
function Public.enable_auto_allocate(value)
this.rpg_extra.enable_auto_allocate = value or false
2020-08-20 11:35:52 +02:00
return this.rpg_extra.enable_auto_allocate
end
2022-04-05 20:02:13 +02:00
--- Enables/disabled aoe_punch.
---@param value boolean
2022-04-05 20:02:13 +02:00
function Public.enable_aoe_punch(value)
this.rpg_extra.enable_aoe_punch = value or false
2020-07-26 22:06:19 +02:00
2022-04-05 20:02:13 +02:00
return this.rpg_extra.enable_aoe_punch
2020-07-25 17:22:04 +02:00
end
2022-04-05 20:02:13 +02:00
--- Enables/disabled aoe_punch.
---@param value boolean
2022-04-05 20:02:13 +02:00
function Public.enable_aoe_punch_globally(value)
this.rpg_extra.enable_aoe_punch_globally = value or false
2020-07-26 22:06:19 +02:00
2022-04-05 20:02:13 +02:00
return this.rpg_extra.enable_aoe_punch_globally
2020-07-25 17:22:04 +02:00
end
2024-11-10 22:10:11 +01:00
--- Sets the amount of xp the player gets from killing a specific entity.
---@param tbl table
function Public.set_rpg_xp_yield(tbl)
if not tbl then
return error('No table given.', 2)
end
if not type(tbl) == 'table' then
return error('Given parameter is not a table.', 2)
end
this.rpg_extra.rpg_xp_yield = tbl
end
2020-11-15 19:23:54 +01:00
function Public.tweaked_crafting_items(tbl)
if not tbl then
return
end
if type(type) ~= 'table' then
return
end
2020-11-15 19:23:54 +01:00
this.tweaked_crafting_items = tbl
2020-11-15 19:23:54 +01:00
return this.tweaked_crafting_items
end
2022-07-06 18:26:28 +02:00
function Public.migrate_new_rpg_tbl(player)
local rpg_t = Public.get_value_from_player(player.index, nil)
if rpg_t then
rpg_t.flame_boots = nil
rpg_t.one_punch = nil
2022-08-29 16:35:24 +02:00
rpg_t.points_left = rpg_t.points_to_distribute or rpg_t.points_left or 0
2022-07-06 18:26:28 +02:00
rpg_t.points_to_distribute = nil
rpg_t.aoe_punch = false
2025-06-10 23:15:50 +02:00
rpg_t.auto_toggle_features =
{
2022-07-06 18:26:28 +02:00
aoe_punch = false,
stone_path = false
}
end
Public.enable_mod_gui(false, true)
local screen = player.gui.screen
for _, child in pairs(screen.children) do
if child.caption and child.caption[1] == 'rpg_settings.spell_name' then
child.destroy()
end
end
end
2025-06-10 23:15:50 +02:00
-- Sets the callback function for x_marks_the_spot
---@param token number
---@return number|nil
function Public.set_x_marks_the_spot_custom_callback(token)
if token then
this.rpg_extra.x_marks_the_spot_custom_callback = token
else
return error('No token given.', 2)
end
return this.rpg_extra.x_marks_the_spot_custom_callback
end
--- Gets the callback function for x_marks_the_spot
--- @return function|nil
function Public.get_x_marks_the_spot_custom_callback()
local token = Task.get(this.rpg_extra.x_marks_the_spot_custom_callback)
if token then
return token
end
end
2022-07-06 18:26:28 +02:00
function Public.migrate_to_new_version()
2022-08-29 16:35:24 +02:00
-- Public.reset_table(true)
2022-07-06 18:26:28 +02:00
if this.rpg_spells then
this.rpg_spells = nil
end
local players = game.players
for _, player in pairs(players) do
Public.migrate_new_rpg_tbl(player)
end
end
2020-07-25 17:22:04 +02:00
Public.settings_frame_name = settings_frame_name
2022-04-18 01:18:25 +02:00
Public.settings_tooltip_frame = settings_tooltip_frame
Public.close_settings_tooltip_frame = close_settings_tooltip_frame
Public.settings_tooltip_name = settings_tooltip_name
2020-07-25 17:22:04 +02:00
Public.save_button_name = save_button_name
Public.discard_button_name = discard_button_name
Public.draw_main_frame_name = draw_main_frame_name
2022-04-05 19:28:08 +02:00
Public.close_main_frame_name = close_main_frame_name
2020-07-25 17:22:04 +02:00
Public.main_frame_name = main_frame_name
Public.settings_button_name = settings_button_name
Public.spell_gui_button_name = spell_gui_button_name
Public.spell_gui_frame_name = spell_gui_frame_name
2021-07-02 10:08:35 +02:00
Public.enable_spawning_frame_name = enable_spawning_frame_name
Public.spell1_button_name = spell1_button_name
Public.spell2_button_name = spell2_button_name
Public.spell3_button_name = spell3_button_name
2024-05-29 19:57:58 +02:00
Public.cooldown_indicator_name = cooldown_indicator_name
2020-07-25 17:22:04 +02:00
2024-11-05 20:04:00 +01:00
local on_init = function ()
2020-07-25 17:22:04 +02:00
Public.reset_table()
end
Event.on_init(on_init)
2022-07-06 18:26:28 +02:00
Event.on_configuration_changed(
2024-11-05 20:04:00 +01:00
function ()
2022-07-06 18:26:28 +02:00
print('[RPG] Migrating to new version')
Public.migrate_to_new_version()
end
)
2020-07-25 17:22:04 +02:00
return Public