1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-08 00:39:30 +02:00
ComfyFactorio/player_modifiers.lua

210 lines
5.6 KiB
Lua
Raw Normal View History

2019-10-02 21:13:14 +02:00
--Central to add all player modifiers together.
--Will overwrite character stats from other mods.
2019-10-02 21:13:14 +02:00
2021-05-25 22:19:20 +02:00
local Event = require 'utils.event'
local Global = require 'utils.global'
2020-07-21 14:41:39 +02:00
local this = {
2021-05-23 17:03:52 +02:00
modifiers = {},
2020-07-21 14:41:39 +02:00
disabled_modifier = {}
}
Global.register(
this,
function(t)
this = t
end
)
2019-10-28 18:38:36 +02:00
local Public = {}
2021-05-25 22:19:20 +02:00
function Public.get(key)
if key then
return this[key]
else
return this
end
end
function Public.set(key, value)
if key and (value or value == false) then
this[key] = value
return this[key]
elseif key then
return this[key]
else
return this
end
end
2019-10-02 21:13:14 +02:00
local modifiers = {
2021-05-23 17:03:52 +02:00
[1] = 'character_build_distance_bonus',
[2] = 'character_crafting_speed_modifier',
[3] = 'character_health_bonus',
[4] = 'character_inventory_slots_bonus',
[5] = 'character_item_drop_distance_bonus',
[6] = 'character_item_pickup_distance_bonus',
[7] = 'character_loot_pickup_distance_bonus',
[8] = 'character_mining_speed_modifier',
[9] = 'character_reach_distance_bonus',
[10] = 'character_resource_reach_distance_bonus',
[11] = 'character_maximum_following_robot_count_bonus',
[12] = 'character_running_speed_modifier'
2019-10-02 21:13:14 +02:00
}
2019-10-28 18:38:36 +02:00
function Public.update_player_modifiers(player)
2021-05-25 22:19:20 +02:00
local player_modifiers = this.modifiers[player.index]
if not player_modifiers then
return
end
local disabled_modifiers = this.disabled_modifier[player.index]
if not disabled_modifiers then
return
end
2021-05-23 17:03:52 +02:00
for k, modifier in pairs(modifiers) do
local sum_value = 0
2021-05-25 22:19:20 +02:00
for _, value in pairs(player_modifiers[k]) do
sum_value = sum_value + value
end
if player.character then
2021-05-25 22:19:20 +02:00
if disabled_modifiers and disabled_modifiers[k] then
2020-07-21 14:41:39 +02:00
player[modifier] = 0
else
player[modifier] = sum_value
end
end
end
2019-10-02 21:13:14 +02:00
end
2021-05-23 17:03:52 +02:00
function Public.update_single_modifier(player, modifier, category, value)
2021-05-25 22:19:20 +02:00
local player_modifiers = this.modifiers[player.index]
if not player_modifiers then
2021-05-23 17:03:52 +02:00
return
end
if not modifier then
return
end
2021-05-25 22:19:20 +02:00
for k, _ in pairs(player_modifiers) do
if modifiers[k] == modifier and player_modifiers[k] then
2021-05-23 17:03:52 +02:00
if category then
2021-05-25 22:19:20 +02:00
if not player_modifiers[k][category] then
player_modifiers[k][category] = {}
2021-05-23 17:03:52 +02:00
end
2021-05-25 22:19:20 +02:00
player_modifiers[k][category] = value
2021-05-23 17:03:52 +02:00
else
2021-05-25 22:19:20 +02:00
player_modifiers[k] = value
2021-05-23 17:03:52 +02:00
end
end
end
end
function Public.disable_single_modifier(player, modifier, value)
2021-05-25 22:19:20 +02:00
local disabled_modifiers = this.disabled_modifier[player.index]
if not disabled_modifiers then
2021-05-23 17:03:52 +02:00
return
end
if not modifier then
return
end
for k, _ in pairs(modifiers) do
if modifiers[k] == modifier then
if value then
2021-05-25 22:19:20 +02:00
disabled_modifiers[k] = value
2021-05-23 17:03:52 +02:00
else
2021-05-25 22:19:20 +02:00
disabled_modifiers[k] = nil
2021-05-23 17:03:52 +02:00
end
end
end
end
function Public.get_single_modifier(player, modifier, category)
2021-05-25 22:19:20 +02:00
local player_modifiers = this.modifiers[player.index]
if not player_modifiers then
2021-05-23 17:03:52 +02:00
return
end
if not modifier then
return
end
2021-05-25 22:19:20 +02:00
for k, _ in pairs(player_modifiers) do
2021-05-23 17:03:52 +02:00
if modifiers[k] == modifier then
if category then
2021-05-25 22:19:20 +02:00
if player_modifiers[k] and player_modifiers[k][category] then
return player_modifiers[k][category]
2021-05-23 17:03:52 +02:00
end
else
2021-05-25 22:19:20 +02:00
if player_modifiers[k] then
return player_modifiers[k]
2021-05-23 17:03:52 +02:00
end
end
return false
end
end
return false
end
function Public.get_single_disabled_modifier(player, modifier, category)
2021-05-25 22:19:20 +02:00
local disabled_modifiers = this.disabled_modifier[player.index]
if not disabled_modifiers then
2021-05-23 17:03:52 +02:00
return
end
if not modifier then
return
end
2021-05-25 22:19:20 +02:00
for k, _ in pairs(disabled_modifiers) do
2021-05-23 17:03:52 +02:00
if modifiers[k] == modifier then
if category then
2021-05-25 22:19:20 +02:00
if disabled_modifiers[k] and disabled_modifiers[k][category] then
return disabled_modifiers[k][category]
2021-05-23 17:03:52 +02:00
end
else
2021-05-25 22:19:20 +02:00
if disabled_modifiers[k] then
return disabled_modifiers[k]
2021-05-23 17:03:52 +02:00
end
end
return false
end
end
return false
end
2020-12-29 01:02:31 +02:00
function Public.reset_player_modifiers(player)
if player and player.valid then
2021-05-23 17:03:52 +02:00
this.modifiers[player.index] = {}
this.disabled_modifier[player.index] = {}
for k, _ in pairs(modifiers) do
this.modifiers[player.index][k] = {}
2020-12-29 01:02:31 +02:00
end
Public.update_player_modifiers(player)
end
end
2019-10-02 21:13:14 +02:00
local function on_player_joined_game(event)
2020-12-29 01:02:31 +02:00
local player = game.get_player(event.player_index)
2021-05-23 17:03:52 +02:00
if this.modifiers[player.index] then
2020-12-29 01:02:31 +02:00
Public.update_player_modifiers(player)
return
end
2020-12-29 01:02:31 +02:00
Public.reset_player_modifiers(player)
2019-10-02 21:13:14 +02:00
end
local function on_player_respawned(event)
Public.update_player_modifiers(game.players[event.player_index])
end
local function on_player_removed(event)
if this.modifiers[event.player_index] then
this.modifiers[event.player_index] = nil
end
if this.disabled_modifier[event.player_index] then
this.disabled_modifier[event.player_index] = nil
end
end
Event.add(defines.events.on_player_joined_game, on_player_joined_game)
Event.add(defines.events.on_player_respawned, on_player_respawned)
Event.add(defines.events.on_player_removed, on_player_removed)
2019-10-28 18:38:36 +02:00
return Public