2016-07-30 07:38:52 +02:00
|
|
|
-------------------------------------------------------------------------------
|
2016-08-07 19:31:11 +02:00
|
|
|
-- factions Mod by Sapier
|
2016-07-30 07:38:52 +02:00
|
|
|
--
|
|
|
|
-- License WTFPL
|
|
|
|
--
|
2016-08-07 19:31:11 +02:00
|
|
|
--! @file factions.lua
|
|
|
|
--! @brief factions core file containing datastorage
|
2016-07-30 07:38:52 +02:00
|
|
|
--! @copyright Sapier
|
|
|
|
--! @author Sapier
|
|
|
|
--! @date 2013-05-08
|
|
|
|
--
|
|
|
|
-- Contact sapier a t gmx net
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
--read some basic information
|
2016-08-07 19:31:11 +02:00
|
|
|
local factions_worldid = minetest.get_worldpath()
|
2016-07-30 07:38:52 +02:00
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
--! @class factions
|
|
|
|
--! @brief main class for factions
|
|
|
|
factions = {}
|
2016-07-30 07:38:52 +02:00
|
|
|
|
|
|
|
--! @brief runtime data
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.factions = {}
|
|
|
|
factions.chunks = {}
|
|
|
|
factions.players = {}
|
2016-07-30 07:38:52 +02:00
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.print = function(text)
|
|
|
|
print("factions: " .. dump(text))
|
2016-07-30 07:38:52 +02:00
|
|
|
end
|
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.dbg_lvl1 = function() end --factionsmod.print -- errors
|
|
|
|
factions.dbg_lvl2 = function() end --factionsmod.print -- non cyclic trace
|
|
|
|
factions.dbg_lvl3 = function() end --factionsmod.print -- cyclic trace
|
2016-07-30 07:38:52 +02:00
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.factions = {}
|
2016-08-07 02:11:17 +02:00
|
|
|
--- settings
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.lower_claimable_height = -512
|
2016-08-07 02:11:17 +02:00
|
|
|
|
|
|
|
---------------------
|
|
|
|
--! @brief returns whether a faction can be created or not (allows for implementation of blacklists and the like)
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.can_create_faction = function(name)
|
|
|
|
if factions.factions[name] then
|
2016-08-07 02:11:17 +02:00
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
2016-08-07 19:36:56 +02:00
|
|
|
end
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
--! @brief create a faction object
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.new_faction = function(name)
|
2016-08-07 02:11:17 +02:00
|
|
|
local faction = {
|
|
|
|
name = name,
|
|
|
|
power = 0.,
|
|
|
|
players = {},
|
2016-08-07 19:22:31 +02:00
|
|
|
ranks = {["leader"] = {"disband", "claim", "playerlist", "build", "edit", "ranks"},
|
2016-08-07 02:11:17 +02:00
|
|
|
["member"] = {"build"}
|
|
|
|
},
|
|
|
|
leader = nil,
|
|
|
|
default_rank = "member",
|
|
|
|
default_leader_rank = "leader",
|
|
|
|
description = "Default faction description.",
|
|
|
|
invited_players = {},
|
|
|
|
land = {},
|
|
|
|
allies = {},
|
|
|
|
enemies = {},
|
2016-08-07 19:22:31 +02:00
|
|
|
join_free = false,
|
|
|
|
spawn = nil,
|
2016-08-07 02:11:17 +02:00
|
|
|
|
|
|
|
----------------------
|
|
|
|
-- methods
|
|
|
|
increase_power = function(self, power)
|
|
|
|
self.power = self.power + power
|
|
|
|
end,
|
|
|
|
decrease_power = function(self, power)
|
|
|
|
self.power = self.power - power
|
|
|
|
end,
|
|
|
|
add_player = function(self, player, rank)
|
|
|
|
self.players[player] = rank or self.default_rank
|
|
|
|
self:on_player_join(player)
|
|
|
|
self.invited_players[player] = nil
|
|
|
|
end,
|
|
|
|
remove_player = function(self, player)
|
|
|
|
self.players[player] = nil
|
|
|
|
self:on_player_leave(player)
|
|
|
|
end,
|
|
|
|
claim_chunk = function(self, chunkpos)
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.chunks[chunkpos] = self.name
|
2016-08-07 02:11:17 +02:00
|
|
|
self.land[chunkpos] = true
|
|
|
|
self:on_claim_chunk(chunkpos)
|
|
|
|
end,
|
|
|
|
unclaim_chunk = function(self, chunkpos)
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.chunks[chunkpos] = nil
|
2016-08-07 02:11:17 +02:00
|
|
|
self.land[chunkpos] = nil
|
|
|
|
self:on_unclaim_chunks(chunkpos)
|
|
|
|
end,
|
|
|
|
disband = function(self)
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.factions[self.name] = nil
|
2016-08-07 02:11:17 +02:00
|
|
|
for i in ipairs(self.players) do -- remove players affiliation
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.players[self.players[i]] = nil
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
|
|
|
for k, v in self.land do -- remove chunk claims
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.chunks[v] = nil
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
|
|
|
self:on_disband()
|
|
|
|
end,
|
|
|
|
set_leader = function(self, player)
|
|
|
|
self.leader = player
|
|
|
|
self.players[player] = self.default_leader_rank
|
|
|
|
self:on_new_leader()
|
|
|
|
end,
|
|
|
|
has_permission = function(self, player, permission)
|
|
|
|
local p = self.players[player]
|
|
|
|
if not p then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return table.contains(self.groups[p], permission)
|
|
|
|
end,
|
|
|
|
set_description = function(self, new)
|
|
|
|
self.description = new
|
|
|
|
self:on_change_description()
|
|
|
|
end,
|
|
|
|
invite_player = function(self, player)
|
|
|
|
self.invited_players[player] = true
|
|
|
|
self:on_player_invited(player)
|
|
|
|
end,
|
|
|
|
revoke_invite = function(self, player)
|
2016-08-07 19:36:56 +02:00
|
|
|
self.invited_player[player] = nil
|
2016-08-07 02:11:17 +02:00
|
|
|
self:on_revoke_invite(player)
|
|
|
|
end,
|
|
|
|
is_invited = function(self, player)
|
|
|
|
return table.contains(self.invited_players, player)
|
|
|
|
end,
|
|
|
|
toggle_join_free = function(self, bool)
|
|
|
|
self.join_free = bool
|
|
|
|
self:on_toggle_join_free()
|
|
|
|
end,
|
|
|
|
can_join = function(self, player)
|
|
|
|
return self.join_free or invited_players[player]
|
|
|
|
end,
|
|
|
|
new_alliance = function(self, faction)
|
|
|
|
self.allies[faction] = true
|
|
|
|
self:on_new_alliance(faction)
|
|
|
|
if self.enemies[faction] then
|
|
|
|
self:end_enemy(faction)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
end_alliance = function(self, faction)
|
|
|
|
self.allies[faction] = nil
|
|
|
|
self:on_end_alliance(faction)
|
|
|
|
end,
|
|
|
|
new_enemy = function(self, faction)
|
|
|
|
self.enemies[faction] = true
|
2016-08-07 19:36:56 +02:00
|
|
|
self:on_new_enemy(faction)
|
2016-08-07 02:11:17 +02:00
|
|
|
if self.allies[faction] then
|
|
|
|
self:end_alliance(faction)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
end_enemy = function(self, faction)
|
|
|
|
self.enemies[faction] = nil
|
2016-08-07 19:22:31 +02:00
|
|
|
self:on_end_enemy(faction)
|
|
|
|
end,
|
|
|
|
set_spawn = function(self, pos)
|
|
|
|
self.spawn = pos
|
|
|
|
self:on_set_spawn()
|
|
|
|
end,
|
|
|
|
add_rank = function(self, rank, perms)
|
|
|
|
self.ranks[rank] = perms
|
|
|
|
self:on_new_rank(rank)
|
|
|
|
end,
|
|
|
|
delete_rank = function(self, rank, newrank)
|
|
|
|
for player, r in pairs(self.players) do
|
|
|
|
if r == rank then
|
|
|
|
self.players[player] = newrank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.ranks[rank] = nil
|
|
|
|
self:on_delete_rank(rank, newrank)
|
2016-08-07 02:11:17 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
-----------------------
|
|
|
|
-- callbacks for events
|
|
|
|
on_create = function(self) --! @brief called when the faction is added to the global faction list
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_player_leave = function(self, player)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_player_join = function(self, player)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_claim_chunk = function(self, pos)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_unclaim_chunk = function(self, pos)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_disband = function(self, pos)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_new_leader = function(self)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_change_description = function(self)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_player_invited = function(self, player)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_toggle_join_free = function(self, player)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_new_alliance = function(self, faction)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_end_alliance = function(self, faction)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
2016-08-07 19:22:31 +02:00
|
|
|
on_set_spawn = function(self)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_add_rank = function(self, rank)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
|
|
|
on_delete_rank = function(self, rank, newrank)
|
|
|
|
--TODO: implement
|
|
|
|
end,
|
2016-08-07 02:11:17 +02:00
|
|
|
}
|
2016-08-07 19:36:56 +02:00
|
|
|
factions.factions[name] = faction
|
2016-08-07 02:11:17 +02:00
|
|
|
return faction
|
|
|
|
end
|
|
|
|
|
|
|
|
--??????????????
|
2016-08-07 19:31:11 +02:00
|
|
|
function factions.fix_powercap(name)
|
|
|
|
factions.data.factionsmod[name].powercap = #factionsmod.dynamic_data.membertable[name] + 10
|
2016-08-04 07:21:22 +02:00
|
|
|
end
|
2016-08-07 02:11:17 +02:00
|
|
|
--??????????????
|
2016-07-30 07:38:52 +02:00
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
function factions.get_chunk(pos)
|
|
|
|
return factions.chunks[factionsmod.get_chunkpos(pos)]
|
2016-07-30 07:38:52 +02:00
|
|
|
end
|
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
function factions.get_chunkpos(pos)
|
2016-08-07 02:11:17 +02:00
|
|
|
return {math.floor(pos.x / 16.), math.floor(pos.z / 16.)}
|
2016-08-07 19:36:56 +02:00
|
|
|
end
|
2016-07-30 07:38:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
2016-08-07 02:11:17 +02:00
|
|
|
-- name: add_faction(name)
|
2016-07-30 07:38:52 +02:00
|
|
|
--
|
2016-08-07 02:11:17 +02:00
|
|
|
--! @brief add a faction
|
2016-08-07 19:31:11 +02:00
|
|
|
--! @memberof factions
|
2016-07-30 07:38:52 +02:00
|
|
|
--! @public
|
|
|
|
--
|
2016-08-07 02:11:17 +02:00
|
|
|
--! @param name of faction to add
|
2016-07-30 07:38:52 +02:00
|
|
|
--!
|
2016-08-07 02:11:17 +02:00
|
|
|
--! @return faction object/false (succesfully added faction or not)
|
2016-07-30 07:38:52 +02:00
|
|
|
-------------------------------------------------------------------------------
|
2016-08-07 19:31:11 +02:00
|
|
|
function factions.add_faction(name)
|
2016-08-07 19:36:56 +02:00
|
|
|
if factions.can_create_faction(name) then
|
|
|
|
local fac = factions.new_faction(name)
|
|
|
|
fac:on_create()
|
|
|
|
return fac
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2016-07-30 07:38:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- name: get_faction_list()
|
|
|
|
--
|
2016-08-07 19:31:11 +02:00
|
|
|
--! @brief get list of factions
|
|
|
|
--! @memberof factions
|
2016-07-30 07:38:52 +02:00
|
|
|
--! @public
|
|
|
|
--!
|
2016-08-07 19:31:11 +02:00
|
|
|
--! @return list of factions
|
2016-07-30 07:38:52 +02:00
|
|
|
-------------------------------------------------------------------------------
|
2016-08-07 19:31:11 +02:00
|
|
|
function factions.get_faction_list()
|
2016-07-30 07:38:52 +02:00
|
|
|
|
|
|
|
local retval = {}
|
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
for key,value in pairs(factions.factions) do
|
2016-07-30 07:38:52 +02:00
|
|
|
table.insert(retval,key)
|
|
|
|
end
|
|
|
|
|
|
|
|
return retval
|
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- name: save()
|
|
|
|
--
|
|
|
|
--! @brief save data to file
|
2016-08-07 19:31:11 +02:00
|
|
|
--! @memberof factions
|
2016-07-30 07:38:52 +02:00
|
|
|
--! @private
|
|
|
|
-------------------------------------------------------------------------------
|
2016-08-07 19:31:11 +02:00
|
|
|
function factions.save()
|
2016-07-30 07:38:52 +02:00
|
|
|
|
|
|
|
--saving is done much more often than reading data to avoid delay
|
|
|
|
--due to figuring out which data to save and which is temporary only
|
|
|
|
--all data is saved here
|
|
|
|
--this implies data needs to be cleant up on load
|
|
|
|
|
2016-08-07 19:31:11 +02:00
|
|
|
local file,error = io.open(factions_worldid .. "/" .. "factionsmod.conf","w")
|
2016-07-30 07:38:52 +02:00
|
|
|
|
|
|
|
if file ~= nil then
|
2016-08-07 19:31:11 +02:00
|
|
|
file:write(minetest.serialize(factions.factions))
|
2016-07-30 07:38:52 +02:00
|
|
|
file:close()
|
|
|
|
else
|
2016-08-07 19:31:11 +02:00
|
|
|
minetest.log("error","MOD factions: unable to save factionsmod world specific data!: " .. error)
|
2016-07-30 07:38:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- name: load()
|
|
|
|
--
|
|
|
|
--! @brief load data from file
|
2016-08-07 19:31:11 +02:00
|
|
|
--! @memberof factions
|
2016-07-30 07:38:52 +02:00
|
|
|
--! @private
|
|
|
|
--
|
|
|
|
--! @return true/false
|
|
|
|
-------------------------------------------------------------------------------
|
2016-08-07 19:31:11 +02:00
|
|
|
function factions.load()
|
|
|
|
local file,error = io.open(factions_worldid .. "/" .. "factionsmod.conf","r")
|
2016-07-30 07:38:52 +02:00
|
|
|
|
|
|
|
if file ~= nil then
|
|
|
|
local raw_data = file:read("*a")
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.factions = minetest.deserialize(raw_data)
|
|
|
|
for facname, faction in pairs(factions.factions) do
|
2016-08-07 02:11:17 +02:00
|
|
|
for i in ipairs(faction.players) do
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.players[faction.players[i]] = facname
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
|
|
|
for chunkpos, val in pairs(faction.land) do
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.chunks[chunkpos] = val
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
|
|
|
end
|
2016-07-30 07:38:52 +02:00
|
|
|
file:close()
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
2016-07-30 22:33:10 +02:00
|
|
|
end
|
2016-08-07 02:11:17 +02:00
|
|
|
|
|
|
|
--autojoin players to faction players
|
|
|
|
minetest.register_on_dieplayer(
|
|
|
|
function(player)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
minetest.register_globalstep(
|
|
|
|
function(dtime)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
minetest.register_on_joinplayer(
|
|
|
|
function(player)
|
|
|
|
end
|
|
|
|
)
|
2016-08-04 07:21:22 +02:00
|
|
|
|