Decouple core.lua

This commit is contained in:
rubenwardy 2014-12-28 15:57:14 +00:00
parent 8c7381aa24
commit 6b1555ab4b
4 changed files with 456 additions and 409 deletions

View File

@ -1,3 +1,12 @@
ctf.registered_on_load = {}
function ctf.register_on_load(func)
table.insert(ctf.registered_on_load, func)
end
ctf.registered_on_save = {}
function ctf.register_on_save(func)
table.insert(ctf.registered_on_save, func)
end
function ctf.init() function ctf.init()
print("[CaptureTheFlag] Initialising...") print("[CaptureTheFlag] Initialising...")
@ -6,7 +15,6 @@ function ctf.init()
ctf.teams = {} ctf.teams = {}
ctf.players = {} ctf.players = {}
ctf.claimed = {} ctf.claimed = {}
ctf.diplo = {diplo = {}}
-- Settings: Feature enabling -- Settings: Feature enabling
ctf._set("node_ownership",true) ctf._set("node_ownership",true)
@ -34,19 +42,10 @@ function ctf.init()
ctf._set("flag_protect_distance", 25) -- how far do flags protect? ctf._set("flag_protect_distance", 25) -- how far do flags protect?
ctf._set("team_gui_initial", "news") -- [news/flags/diplo/admin] - the starting tab ctf._set("team_gui_initial", "news") -- [news/flags/diplo/admin] - the starting tab
local file = io.open(minetest.get_worldpath().."/ctf.txt", "r") ctf.load()
if file then
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
ctf.teams = table.teams
ctf.players = table.players
ctf.diplo.diplo = table.diplo
return
end
end
end end
-- Set settings -- Set default setting value
function ctf._set(setting,default) function ctf._set(setting,default)
ctf._defsettings[setting] = default ctf._defsettings[setting] = default
end end
@ -63,16 +62,42 @@ function ctf.setting(name)
end end
end end
-- Save game function ctf.load()
local file = io.open(minetest.get_worldpath().."/ctf.txt", "r")
if file then
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
ctf.teams = table.teams
ctf.players = table.players
for i = 1, #ctf.registered_on_load do
ctf.registered_on_load[i](table)
end
return
end
end
end
function ctf.save() function ctf.save()
print("[CaptureTheFlag] Saving data...") print("[CaptureTheFlag] Saving data...")
local file = io.open(minetest.get_worldpath().."/ctf.txt", "w") local file = io.open(minetest.get_worldpath().."/ctf.txt", "w")
if file then if file then
file:write(minetest.serialize({ local out = {
teams = ctf.teams, teams = ctf.teams,
players = ctf.players, players = ctf.players
diplo = ctf.diplo.diplo }
}))
for i = 1, #ctf.registered_on_save do
local res = ctf.registered_on_save[i]()
if res then
for key, value in pairs(res) do
out[key] = value
end
end
end
file:write(minetest.serialize(out))
file:close() file:close()
end end
end end

View File

@ -1,5 +1,15 @@
-- diplo states: war, peace, alliance -- diplo states: war, peace, alliance
ctf.diplo = {} ctf.diplo = {
diplo = {}
}
ctf.register_on_load(function(table)
ctf.diplo.diplo = table.diplo
end)
ctf.register_on_save(function()
return { diplo = ctf.diplo.diplo }
end)
function ctf.diplo.get(one,two) function ctf.diplo.get(one,two)
if not ctf.diplo.diplo then if not ctf.diplo.diplo then

View File

@ -1,6 +1,5 @@
ctf.gui = {} ctf.gui = {}
if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are enabled
-- Get tab buttons -- Get tab buttons
function ctf.gui.tabs(name,team) function ctf.gui.tabs(name,team)
local result = "" local result = ""
@ -24,6 +23,10 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
-- Team interface -- Team interface
function ctf.gui.team_board(name,team) function ctf.gui.team_board(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
local result = "" local result = ""
local data = ctf.teams[team].log local data = ctf.teams[team].log
@ -85,6 +88,10 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
-- Team interface -- Team interface
function ctf.gui.team_flags(name,team) function ctf.gui.team_flags(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
local result = "" local result = ""
local t = ctf.team(team) local t = ctf.team(team)
@ -141,6 +148,10 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
-- Team interface -- Team interface
function ctf.gui.team_dip(name,team) function ctf.gui.team_dip(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
local result = "" local result = ""
local data = {} local data = {}
@ -199,6 +210,10 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
-- Team interface -- Team interface
function ctf.gui.team_settings(name,team) function ctf.gui.team_settings(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
if not team or not ctf.team(team) then if not team or not ctf.team(team) then
return return
end end
@ -381,7 +396,6 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
end end
end end
end) end)
end -- end of check if team guis are enabled
-- Flag interface -- Flag interface
function ctf.gui.flag_board(name,pos) function ctf.gui.flag_board(name,pos)

View File

@ -58,11 +58,8 @@ function v3.get_direction(pos1,pos2)
return {x=x_raw,y=y_raw,z=z_raw} return {x=x_raw,y=y_raw,z=z_raw}
end end
-- Load the core
dofile(minetest.get_modpath("ctf").."/core.lua")
ctf.init()
-- Modules -- Modules
dofile(minetest.get_modpath("ctf").."/core.lua")
dofile(minetest.get_modpath("ctf").."/diplomacy.lua") dofile(minetest.get_modpath("ctf").."/diplomacy.lua")
dofile(minetest.get_modpath("ctf").."/area.lua") dofile(minetest.get_modpath("ctf").."/area.lua")
dofile(minetest.get_modpath("ctf").."/gui.lua") dofile(minetest.get_modpath("ctf").."/gui.lua")
@ -70,5 +67,6 @@ dofile(minetest.get_modpath("ctf").."/cli.lua")
dofile(minetest.get_modpath("ctf").."/flag.lua") dofile(minetest.get_modpath("ctf").."/flag.lua")
-- Init -- Init
ctf.init()
ctf.clean_player_lists() ctf.clean_player_lists()
ctf.collect_claimed() ctf.collect_claimed()