Decouple core.lua
This commit is contained in:
parent
8c7381aa24
commit
6b1555ab4b
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
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 = ""
|
||||||
local id = 1
|
local id = 1
|
||||||
local function addtab(name,text)
|
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
|
end
|
||||||
addtab("admin","Settings")
|
addtab("admin","Settings")
|
||||||
return result
|
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
|
end
|
||||||
|
|
||||||
-- Team interface
|
|
||||||
function ctf.gui.team_board(name,team)
|
|
||||||
local result = ""
|
local result = ""
|
||||||
local data = ctf.teams[team].log
|
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)..
|
ctf.gui.tabs(name,team)..
|
||||||
result
|
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
|
end
|
||||||
|
|
||||||
-- Team interface
|
|
||||||
function ctf.gui.team_flags(name,team)
|
|
||||||
local result = ""
|
local result = ""
|
||||||
local t = ctf.team(team)
|
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)..
|
ctf.gui.tabs(name,team)..
|
||||||
result
|
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
|
end
|
||||||
|
|
||||||
-- Team interface
|
|
||||||
function ctf.gui.team_dip(name,team)
|
|
||||||
local result = ""
|
local result = ""
|
||||||
local data = {}
|
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)..
|
ctf.gui.tabs(name,team)..
|
||||||
result
|
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
|
end
|
||||||
|
|
||||||
-- Team interface
|
|
||||||
function ctf.gui.team_settings(name,team)
|
|
||||||
if not team or not ctf.team(team) then
|
if not team or not ctf.team(team) then
|
||||||
return
|
return
|
||||||
end
|
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)..
|
ctf.gui.tabs(name,team)..
|
||||||
result
|
result
|
||||||
)
|
)
|
||||||
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()
|
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 formname=="ctf:board" or formname=="ctf:flags" or formname=="ctf:dip" or formname=="ctf:team_settings" then
|
||||||
if fields.flags 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
|
return true
|
||||||
end
|
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()
|
local name = player:get_player_name()
|
||||||
if formname=="ctf:board" then
|
if formname=="ctf:board" then
|
||||||
for key, field in pairs(fields) do
|
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
|
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()
|
local name = player:get_player_name()
|
||||||
if formname=="ctf:flags" then
|
if formname=="ctf:flags" then
|
||||||
for key, field in pairs(fields) do
|
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
|
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()
|
local name = player:get_player_name()
|
||||||
if formname=="ctf:dip" then
|
if formname=="ctf:dip" then
|
||||||
for key, field in pairs(fields) do
|
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
|
||||||
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)
|
||||||
|
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user