2019-10-02 21:13:14 +02:00
|
|
|
--Central to add all player modifiers together.
|
2020-01-01 19:25:14 +02:00
|
|
|
--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'
|
2020-05-09 10:04:24 +02:00
|
|
|
local Global = require 'utils.global'
|
2019-11-10 23:02:45 +02:00
|
|
|
|
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 = {}
|
|
|
|
}
|
2019-11-10 23:02:45 +02:00
|
|
|
|
2020-05-09 10:04:24 +02:00
|
|
|
Global.register(
|
|
|
|
this,
|
|
|
|
function(t)
|
|
|
|
this = t
|
|
|
|
end
|
|
|
|
)
|
2019-11-10 23:02:45 +02:00
|
|
|
|
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
|
2019-11-10 23:02:45 +02:00
|
|
|
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
|
2020-05-09 10:04:24 +02:00
|
|
|
local sum_value = 0
|
2021-05-25 22:19:20 +02:00
|
|
|
for _, value in pairs(player_modifiers[k]) do
|
2020-05-09 10:04:24 +02:00
|
|
|
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
|
2020-05-09 10:04:24 +02:00
|
|
|
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)
|
2020-05-09 10:04:24 +02:00
|
|
|
return
|
|
|
|
end
|
2020-12-29 01:02:31 +02:00
|
|
|
Public.reset_player_modifiers(player)
|
2019-10-02 21:13:14 +02:00
|
|
|
end
|
|
|
|
|
2020-05-09 10:04:24 +02:00
|
|
|
local function on_player_respawned(event)
|
|
|
|
Public.update_player_modifiers(game.players[event.player_index])
|
2020-01-01 19:25:14 +02:00
|
|
|
end
|
|
|
|
|
2021-05-24 18:20:29 +02:00
|
|
|
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
|
|
|
|
|
2019-11-10 23:02:45 +02:00
|
|
|
Event.add(defines.events.on_player_joined_game, on_player_joined_game)
|
2020-01-01 19:25:14 +02:00
|
|
|
Event.add(defines.events.on_player_respawned, on_player_respawned)
|
2021-05-24 18:20:29 +02:00
|
|
|
Event.add(defines.events.on_player_removed, on_player_removed)
|
2019-10-28 18:38:36 +02:00
|
|
|
|
2020-01-03 21:46:18 +02:00
|
|
|
return Public
|