Skip to content

Commit

Permalink
Fixed player table issue, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetabite committed Oct 13, 2020
1 parent 4c6ef57 commit aae3b8f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 119 deletions.
11 changes: 10 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nauvis-melange",
"version": "0.8.1",
"version": "0.8.3",
"title": "Nauvis Melange",
"author": "Zetabite",
"factorio_version": "0.18",
Expand Down
39 changes: 39 additions & 0 deletions scripts/config.lua
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions scripts/control/forces.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
71 changes: 38 additions & 33 deletions scripts/control/players.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -137,19 +148,18 @@ 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
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
Expand Down Expand Up @@ -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 = {}

Expand All @@ -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 = {
Expand Down
93 changes: 11 additions & 82 deletions scripts/control/setup.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -143,6 +71,7 @@ function players_table_configuration_changed()
end
end
end
players[player_index] = entry
end
global.players = players
else
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aae3b8f

Please sign in to comment.