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()
print("[CaptureTheFlag] Initialising...")
@ -6,7 +15,6 @@ function ctf.init()
ctf.teams = {}
ctf.players = {}
ctf.claimed = {}
ctf.diplo = {diplo = {}}
-- Settings: Feature enabling
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("team_gui_initial", "news") -- [news/flags/diplo/admin] - the starting tab
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
ctf.diplo.diplo = table.diplo
return
end
end
ctf.load()
end
-- Set settings
-- Set default setting value
function ctf._set(setting,default)
ctf._defsettings[setting] = default
end
@ -63,16 +62,42 @@ function ctf.setting(name)
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()
print("[CaptureTheFlag] Saving data...")
local file = io.open(minetest.get_worldpath().."/ctf.txt", "w")
if file then
file:write(minetest.serialize({
local out = {
teams = ctf.teams,
players = ctf.players,
diplo = ctf.diplo.diplo
}))
players = ctf.players
}
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()
end
end

View File

@ -1,5 +1,15 @@
-- 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)
if not ctf.diplo.diplo then

View File

@ -1,8 +1,7 @@
ctf.gui = {}
if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are enabled
-- Get tab buttons
function ctf.gui.tabs(name,team)
-- Get tab buttons
function ctf.gui.tabs(name,team)
local result = ""
local id = 1
local function addtab(name,text)
@ -20,10 +19,14 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
end
addtab("admin","Settings")
return result
end
-- Team interface
function ctf.gui.team_board(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
-- Team interface
function ctf.gui.team_board(name,team)
local result = ""
local data = ctf.teams[team].log
@ -81,10 +84,14 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
ctf.gui.tabs(name,team)..
result
)
end
-- Team interface
function ctf.gui.team_flags(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
-- Team interface
function ctf.gui.team_flags(name,team)
local result = ""
local t = ctf.team(team)
@ -137,10 +144,14 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
ctf.gui.tabs(name,team)..
result
)
end
-- Team interface
function ctf.gui.team_dip(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
-- Team interface
function ctf.gui.team_dip(name,team)
local result = ""
local data = {}
@ -195,10 +206,14 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
ctf.gui.tabs(name,team)..
result
)
end
-- Team interface
function ctf.gui.team_settings(name,team)
if not ctf.setting("team_gui") or not ctf.setting("gui") then
return
end
-- Team interface
function ctf.gui.team_settings(name,team)
if not team or not ctf.team(team) then
return
end
@ -222,8 +237,8 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
ctf.gui.tabs(name,team)..
result
)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if formname=="ctf:board" or formname=="ctf:flags" or formname=="ctf:dip" or formname=="ctf:team_settings" then
if fields.flags then
@ -274,9 +289,9 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
return true
end
end
end)
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if formname=="ctf:board" then
for key, field in pairs(fields) do
@ -298,9 +313,9 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
end
end
end
end)
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if formname=="ctf:flags" then
for key, field in pairs(fields) do
@ -311,9 +326,9 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
end
end
end
end)
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if formname=="ctf:dip" then
for key, field in pairs(fields) do
@ -380,8 +395,7 @@ if ctf.setting("team_gui") and ctf.setting("gui") then -- check if team guis are
end
end
end
end)
end -- end of check if team guis are enabled
end)
-- Flag interface
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}
end
-- Load the core
dofile(minetest.get_modpath("ctf").."/core.lua")
ctf.init()
-- Modules
dofile(minetest.get_modpath("ctf").."/core.lua")
dofile(minetest.get_modpath("ctf").."/diplomacy.lua")
dofile(minetest.get_modpath("ctf").."/area.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")
-- Init
ctf.init()
ctf.clean_player_lists()
ctf.collect_claimed()