From aae3b8fb4b1319a9b7d59fd20126ade200941102 Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 13 Oct 2020 17:41:24 +0200 Subject: [PATCH] Fixed player table issue, refactoring --- changelog.txt | 11 ++++- info.json | 2 +- scripts/config.lua | 39 ++++++++++++++++ scripts/control/forces.lua | 4 +- scripts/control/players.lua | 71 +++++++++++++++------------- scripts/control/setup.lua | 93 +++++-------------------------------- 6 files changed, 101 insertions(+), 119 deletions(-) diff --git a/changelog.txt b/changelog.txt index c11dbc1..4d04ed4 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,9 +1,18 @@ +--------------------------------------------------------------------------------------------------- +Version: 0.8.3 +Date: 13.10.2020 + Changes: + - Added default values in config.lua + Bugfixes: + - Fixed player table being empty in existing save after version change + --------------------------------------------------------------------------------------------------- Version: 0.8.2 Date: 13.10.2020 + Changes: + - Made constants only load as local Bugfixes: - Fixed on_load() crash due to global manipulation - - Made constants only load as local --------------------------------------------------------------------------------------------------- Version: 0.8.1 diff --git a/info.json b/info.json index 363c16b..13952d6 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "nauvis-melange", - "version": "0.8.1", + "version": "0.8.3", "title": "Nauvis Melange", "author": "Zetabite", "factorio_version": "0.18", diff --git a/scripts/config.lua b/scripts/config.lua index 9f716b2..a8430df 100644 --- a/scripts/config.lua +++ b/scripts/config.lua @@ -1,8 +1,47 @@ local config = {} +-- Constants config.SPICE_DURATION = 3600 config.SPICE_COOLDOWN = config.SPICE_DURATION config.OVERLAY_REFRESH = 30 config.OVERLAY_TIMER = config.OVERLAY_REFRESH + 1 config.ZOOM_FACTOR = 3.0 config.VICTORY_SPICE_AMOUNT = 10000 + +-- Defaults +config.players_default = { + addiction_level = 0, + zoom_factor = config.ZOOM_FACTOR, + under_influence = false, + radar = { + active = false, + reference = false, + remaining_ticks = 0, + }, + craft_mod = { + active = false, + remaining_ticks = 0, + }, + bad_trip = { + active = false, + remaining_ticks = 0, + } +} + +config.forces_default = { + spice_shipped = 0 +} + +config.render_default = { + spice_overlay = {} +} + +config.spice_effects_blacklist_default = { + type = { + ['locomotive'] = true, + ['artillery-wagon'] = true, + ['cargo-wagon'] = true, + ['fluid-wagon'] = true + }, + name = {} +} return config diff --git a/scripts/control/forces.lua b/scripts/control/forces.lua index 056cfb5..d2592dd 100644 --- a/scripts/control/forces.lua +++ b/scripts/control/forces.lua @@ -4,7 +4,7 @@ local VICTORY_SPICE_AMOUNT = config.VICTORY_SPICE_AMOUNT function force_created(event) local force_index = event.force.index - forces_table[force_index] = remote.call('nauvis_melange_table_defaults', 'forces_default') + forces_table[force_index] = config.forces_default end function forces_merged(event) @@ -17,7 +17,7 @@ function forces_merged(event) end function force_reset(event) - forces_table[event.force.index] = remote.call('nauvis_melange_table_defaults', 'forces_default') + forces_table[event.force.index] = config.forces_default end function checkSpiceVictory() diff --git a/scripts/control/players.lua b/scripts/control/players.lua index 75f2cfa..f2f3cfb 100644 --- a/scripts/control/players.lua +++ b/scripts/control/players.lua @@ -5,6 +5,17 @@ local SPICE_COOLDOWN = config.SPICE_COOLDOWN local OVERLAY_REFRESH = config.OVERLAY_REFRESH local OVERLAY_TIMER = config.OVERLAY_TIMER +remote.add_interface('nauvis_melange_player', { + consume_spice = function(player_index, factor, consequence, pre_consumption) + return consume_spice(player_index, factor, consequence, pre_consumption) + end, + onZoomFactorChanged = function(event) + if global.zoom_table[event.playerIndex] then + global.zoom_table[event.playerIndex] = event.zoomFactor + end + end +}) + function apply_spice_to_vehicle(player) if (player.vehicle) then local vehicle = player.vehicle @@ -53,7 +64,7 @@ end -- consequence: 'bad_trip': bad_trip should be applied and items are consumed 'no_consumption': no item is consumed 'no_effect': nothings happens, but the items are consumed - 'nil': proceeds without checking and removes items + 'nil': proceeds without checking if enough items exist and removes items --]] function consume_spice(player_index, factor, consequence, pre_consumed) local player = game.get_player(player_index) @@ -128,7 +139,7 @@ end function player_joined(event) local player_index = event.player_index if not (players_table[player_index]) then - players_table[player_index] = remote.call('nauvis_melange_table_defaults', 'players_default') + players_table[player_index] = config.players_default end end @@ -137,7 +148,7 @@ function player_died(event) if players_table[player_index].radar.reference then players_table[player_index].radar.reference.destroy() end - players_table[player_index] = remote.call('nauvis_melange_table_defaults', 'players_default') + players_table[player_index] = config.players_default if render_table.spice_overlay[player_index] then table.remove(render_table.spice_overlay, player_index) end @@ -145,11 +156,10 @@ end function remove_addiction() for _, player in pairs(game.connected_players) do - local player_index = player.index if player.character then - local addiction_level = players_table[player_index].addiction_level + local addiction_level = players_table[player.index].addiction_level if math.random(0, 100) > 75 and addiction_level > 0 then - players_table[player_index].addiction_level = addiction_level - 1 + players_table[player.index].addiction_level = addiction_level - 1 end end end @@ -241,6 +251,22 @@ function overlay_refresh() end end +-- Util stuff + +function load_globals() + players_table = global.players + spice_effects_blacklist = global.spice_effects_blacklist + render_table = global.render_table +end + +function check_and_call_kux_zooming() + if script.active_mods['Kux-Zooming'] then + if remote.interfaces['Kux-Zooming'] and remote.interfaces['Kux-Zooming']['onZoomFactorChanged'] then + remote.call('Kux-Zooming', 'onZoomFactorChanged_add', 'nauvis_melange_player', 'onZoomFactorChanged') + end + end +end + -- lib local lib = {} @@ -255,39 +281,18 @@ lib.events = { } lib.on_init = function() - players_table = global.players - spice_effects_blacklist = global.spice_effects_blacklist - render_table = global.render_table - - if script.active_mods['Kux-Zooming'] then - if remote.interfaces['Kux-Zooming'] and remote.interfaces['Kux-Zooming']['onZoomFactorChanged'] then - remote.call('Kux-Zooming', 'onZoomFactorChanged_add', 'nauvis_melange_player', 'onZoomFactorChanged') - end - end + load_globals() + check_and_call_kux_zooming() end lib.on_configuration_changed = function() - players_table = global.players - spice_effects_blacklist = global.spice_effects_blacklist - render_table = global.render_table - - if script.active_mods['Kux-Zooming'] then - if remote.interfaces['Kux-Zooming'] and remote.interfaces['Kux-Zooming']['onZoomFactorChanged'] then - remote.call('Kux-Zooming', 'onZoomFactorChanged_add', 'nauvis_melange_player', 'onZoomFactorChanged') - end - end + load_globals() + check_and_call_kux_zooming() end lib.on_load = function () - players_table = global.players - spice_effects_blacklist = global.spice_effects_blacklist - render_table = global.render_table - - if script.active_mods['Kux-Zooming'] then - if remote.interfaces['Kux-Zooming'] and remote.interfaces['Kux-Zooming']['onZoomFactorChanged'] then - remote.call('Kux-Zooming', 'onZoomFactorChanged_add', 'nauvis_melange_player', 'onZoomFactorChanged') - end - end + load_globals() + check_and_call_kux_zooming() end lib.on_nth_tick = { diff --git a/scripts/control/setup.lua b/scripts/control/setup.lua index b4438a8..fffad4e 100644 --- a/scripts/control/setup.lua +++ b/scripts/control/setup.lua @@ -1,68 +1,5 @@ local config = require('scripts.config') -remote.add_interface('nauvis_melange_table_defaults', { - players_default = function() - return { - addiction_level = 0, - zoom_factor = global.ZOOM_FACTOR, - under_influence = false, - radar = { - active = false, - reference = false, - remaining_ticks = 0, - }, - craft_mod = { - active = false, - remaining_ticks = 0, - }, - bad_trip = { - active = false, - remaining_ticks = 0, - } - } - end, - forces_default = function() - return {spice_shipped = 0} - end, - render_default = function() - return { - spice_overlay = {} - } - end, - spice_effects_blacklist_default = function() - return { - type = { - ['locomotive'] = true, - ['artillery-wagon'] = true, - ['cargo-wagon'] = true, - ['fluid-wagon'] = true - }, - name = {} - } - end -}) - -remote.add_interface('nauvis_melange_constants', { - VICTORY_SPICE_AMOUNT = function() - return config.VICTORY_SPICE_AMOUNT - end, - SPICE_COOLDOWN = function() - return config.SPICE_COOLDOWN - end, - SPICE_DURATION = function() - return config.SPICE_DURATION - end, - OVERLAY_REFRESH = function() - return config.OVERLAY_REFRESH - end, - ZOOM_FACTOR = function() - return config.ZOOM_FACTOR - end, - OVERLAY_TIMER = function() - return config.OVERLAY_TIMER - end -}) - -- Mostly for modders usage remote.add_interface('nauvis_melange_functions', { add_to_spice_effects_blacklist = function(key, value) @@ -73,27 +10,16 @@ remote.add_interface('nauvis_melange_functions', { end }) -remote.add_interface('nauvis_melange_player', { - consume_spice = function(player_index, factor, consequence, pre_consumption) - return consume_spice(player_index, factor, consequence, pre_consumption) - end, - onZoomFactorChanged = function(event) - if global.zoom_table[event.playerIndex] then - global.zoom_table[event.playerIndex] = event.zoomFactor - end - end -}) - -- Render table function render_table_init() - global.render_table = remote.call('nauvis_melange_table_defaults', 'render_default') + global.render_table = config.render_default end -- Player table function players_table_init() global.players = {} for _, player in pairs(game.connected_players) do - global.players[player.index] = remote.call('nauvis_melange_table_defaults', 'forces_default') + global.players[player.index] = config.forces_default end end @@ -104,7 +30,7 @@ function players_table_configuration_changed() local players = {} for player_name, entry in pairs(global.addiction_system) do if #entry <= 0 then - for subkey, value in pairs(remote.call('nauvis_melange_table_defaults', 'players_default')) do + for subkey, value in pairs(config.players_default) do if not entry[subkey] then entry[subkey] = value end @@ -118,8 +44,9 @@ function players_table_configuration_changed() -- For current version if global.players then local players = {} - for _, entry in pairs(global.players) do - for subkey, value in pairs(remote.call('nauvis_melange_table_defaults', 'players_default')) do + for player_index, entry in pairs(global.players) do + players[player_index] = {} + for subkey, value in pairs(config.players_default) do if not entry[subkey] then entry[subkey] = value else @@ -132,6 +59,7 @@ function players_table_configuration_changed() remaining_ticks = 0, } if radar_reference then + entry['radar'] = {} entry['radar'].active = true entry['radar'].reference = radar_reference entry['radar'].remaining_ticks = remaining_ticks @@ -143,6 +71,7 @@ function players_table_configuration_changed() end end end + players[player_index] = entry end global.players = players else @@ -154,7 +83,7 @@ end function forces_table_init() global.forces = {} for _, force in pairs(game.forces) do - global.forces[force.index] = remote.call('nauvis_melange_table_defaults', 'forces_default') + global.forces[force.index] = config.forces_default end end @@ -163,7 +92,7 @@ function forces_table_configuration_changed() local forces = {} for force_index, entry in pairs(global.forces) do if #entry <= 0 then - for subkey, value in pairs(remote.call('nauvis_melange_table_defaults', 'forces_default')) do + for subkey, value in pairs(config.forces_default) do if not entry[subkey] then entry[subkey] = value end @@ -250,7 +179,7 @@ end -- Spice effects blacklist function spice_effects_blacklist_init() - global.spice_effects_blacklist = remote.call('nauvis_melange_table_defaults', 'spice_effects_blacklist_default') + global.spice_effects_blacklist = config.spice_effects_blacklist_default end -- lib