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
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief factions core file
|
|
|
|
--! @copyright Sapier, agrecascino, shamoanjac
|
|
|
|
--! @author Sapier, agrecascino, shamoanjac
|
|
|
|
--! @date 2016-08-12
|
2016-07-30 07:38:52 +02:00
|
|
|
--
|
|
|
|
-- 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 = {}
|
2016-08-14 15:53:40 +02:00
|
|
|
factions.parcels = {}
|
2016-08-07 19:31:11 +02:00
|
|
|
factions.players = {}
|
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-15 00:24:30 +02:00
|
|
|
factions.protection_max_depth = -512
|
2016-08-14 15:53:40 +02:00
|
|
|
factions.power_per_parcel = .5
|
2016-08-15 09:54:28 +02:00
|
|
|
factions.power_per_death = .25
|
|
|
|
factions.power_per_tick = .125
|
|
|
|
factions.tick_time = 60.
|
2016-08-15 18:31:11 +02:00
|
|
|
factions.power_per_attack = 2.
|
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-12 11:16:43 +02:00
|
|
|
--! @param name String containing the faction's name
|
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
|
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
|
|
|
|
factions.Faction = {
|
|
|
|
}
|
|
|
|
|
2016-08-09 23:00:53 +02:00
|
|
|
util = {
|
|
|
|
coords3D_string = function(coords)
|
|
|
|
return coords.x..", "..coords.y..", "..coords.z
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
factions.Faction.__index = factions.Faction
|
|
|
|
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
-- Faction permissions:
|
|
|
|
--
|
|
|
|
-- disband: disband the faction
|
2016-08-14 15:53:40 +02:00
|
|
|
-- claim: (un)claim parcels
|
2016-08-12 11:16:43 +02:00
|
|
|
-- playerslist: invite/kick players and open/close the faction
|
|
|
|
-- build: dig and place nodes
|
|
|
|
-- description: set the faction's description
|
|
|
|
-- ranks: create and delete ranks
|
|
|
|
-- spawn: set the faction's spawn
|
|
|
|
-- banner: set the faction's banner
|
|
|
|
-- promote: set a player's rank
|
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction:new(faction)
|
|
|
|
faction = {
|
2016-08-14 15:53:40 +02:00
|
|
|
--! @brief power of a faction (needed for parcel claiming)
|
2016-08-07 02:11:17 +02:00
|
|
|
power = 0.,
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief maximum power of a faction
|
|
|
|
maxpower = 0.,
|
2016-08-16 15:44:19 +02:00
|
|
|
--! @brief power currently in use
|
|
|
|
usedpower = 0.,
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief list of player names
|
2016-08-07 02:11:17 +02:00
|
|
|
players = {},
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief table of ranks/permissions
|
2016-08-08 23:25:31 +02:00
|
|
|
ranks = {["leader"] = {"disband", "claim", "playerslist", "build", "description", "ranks", "spawn", "banner", "promote"},
|
2016-08-08 16:31:11 +02:00
|
|
|
["moderator"] = {"claim", "playerslist", "build", "spawn"},
|
2016-08-07 02:11:17 +02:00
|
|
|
["member"] = {"build"}
|
|
|
|
},
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief name of the leader
|
2016-08-07 02:11:17 +02:00
|
|
|
leader = nil,
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief default joining rank for new members
|
2016-08-07 02:11:17 +02:00
|
|
|
default_rank = "member",
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief default rank assigned to the leader
|
2016-08-07 02:11:17 +02:00
|
|
|
default_leader_rank = "leader",
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief faction's description string
|
2016-08-07 02:11:17 +02:00
|
|
|
description = "Default faction description.",
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief list of players currently invited (can join with /f join)
|
2016-08-07 02:11:17 +02:00
|
|
|
invited_players = {},
|
2016-08-14 15:53:40 +02:00
|
|
|
--! @brief table of claimed parcels (keys are parcelpos strings)
|
2016-08-07 02:11:17 +02:00
|
|
|
land = {},
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief table of allies
|
2016-08-07 02:11:17 +02:00
|
|
|
allies = {},
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief table of enemies
|
2016-08-07 02:11:17 +02:00
|
|
|
enemies = {},
|
2016-08-14 15:53:40 +02:00
|
|
|
--! @brief table of parcels/factions that are under attack
|
|
|
|
attacked_parcels = {},
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief whether faction is closed or open (boolean)
|
2016-08-07 19:22:31 +02:00
|
|
|
join_free = false,
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief banner texture string
|
2016-08-08 19:23:39 +02:00
|
|
|
banner = "bg_white.png",
|
2016-08-08 03:30:29 +02:00
|
|
|
} or faction
|
|
|
|
setmetatable(faction, self)
|
|
|
|
return faction
|
|
|
|
end
|
2016-08-07 02:11:17 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief create a new empty faction
|
2016-08-08 03:30:29 +02:00
|
|
|
factions.new_faction = function(name)
|
|
|
|
local faction = factions.Faction:new(nil)
|
|
|
|
faction.name = name
|
2016-08-07 19:36:56 +02:00
|
|
|
factions.factions[name] = faction
|
2016-08-15 00:47:31 +02:00
|
|
|
faction:on_create()
|
2016-08-08 01:10:47 +02:00
|
|
|
factions.save()
|
2016-08-07 02:11:17 +02:00
|
|
|
return faction
|
|
|
|
end
|
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.increase_power(self, power)
|
|
|
|
self.power = self.power + power
|
2016-08-16 15:44:19 +02:00
|
|
|
if self.power > self.maxpower - self.usedpower then
|
|
|
|
self.power = self.maxpower - self.usedpower
|
2016-08-15 09:54:28 +02:00
|
|
|
end
|
2016-08-08 03:30:29 +02:00
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
|
|
|
|
function factions.Faction.decrease_power(self, power)
|
|
|
|
self.power = self.power - power
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
|
2016-08-15 10:16:12 +02:00
|
|
|
function factions.Faction.increase_maxpower(self, power)
|
|
|
|
self.maxpower = self.maxpower + power
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
|
|
|
|
function factions.Faction.decrease_maxpower(self, power)
|
|
|
|
self.maxpower = self.maxpower - power
|
|
|
|
if self.maxpower < 0. then -- should not happen
|
|
|
|
self.maxpower = 0.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-16 15:44:19 +02:00
|
|
|
function factions.Faction.increase_usedpower(self, power)
|
|
|
|
self.usedpower = self.usedpower + power
|
|
|
|
end
|
|
|
|
|
|
|
|
function factions.Faction.decrease_usedpower(self, power)
|
|
|
|
self.usedpower = self.usedpower - power
|
|
|
|
if self.usedpower < 0. then
|
|
|
|
self.usedpower = 0.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function factions.Faction.count_land(self)
|
|
|
|
local count = 0.
|
|
|
|
for k, v in pairs(self.land) do
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.add_player(self, player, rank)
|
2016-08-10 02:46:54 +02:00
|
|
|
self:on_player_join(player)
|
2016-08-08 03:30:29 +02:00
|
|
|
self.players[player] = rank or self.default_rank
|
|
|
|
factions.players[player] = self.name
|
|
|
|
self.invited_players[player] = nil
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
|
|
|
|
function factions.Faction.remove_player(self, player)
|
|
|
|
self.players[player] = nil
|
|
|
|
factions.players[player] = nil
|
|
|
|
self:on_player_leave(player)
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
--! @param parcelpos position of the wanted parcel
|
|
|
|
--! @return whether this faction can claim a parcelpos
|
|
|
|
function factions.Faction.can_claim_parcel(self, parcelpos)
|
|
|
|
local fac = factions.parcels[parcelpos]
|
2016-08-09 04:39:40 +02:00
|
|
|
if fac then
|
2016-08-14 15:53:40 +02:00
|
|
|
if factions.factions[fac].power < 0. and self.power >= factions.power_per_parcel then
|
2016-08-09 04:39:40 +02:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2016-08-14 15:53:40 +02:00
|
|
|
elseif self.power < factions.power_per_parcel then
|
2016-08-08 16:31:11 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
--! @brief claim a parcel, update power and update global parcels table
|
|
|
|
function factions.Faction.claim_parcel(self, parcelpos)
|
2016-08-09 04:39:40 +02:00
|
|
|
-- check if claiming over other faction's territory
|
2016-08-14 15:53:40 +02:00
|
|
|
local otherfac = factions.parcels[parcelpos]
|
2016-08-09 04:39:40 +02:00
|
|
|
if otherfac then
|
|
|
|
local faction = factions.factions[otherfac]
|
2016-08-14 15:53:40 +02:00
|
|
|
faction:unclaim_parcel(parcelpos)
|
2016-08-09 04:39:40 +02:00
|
|
|
end
|
2016-08-14 15:53:40 +02:00
|
|
|
factions.parcels[parcelpos] = self.name
|
|
|
|
self.land[parcelpos] = true
|
|
|
|
self:decrease_power(factions.power_per_parcel)
|
2016-08-16 15:44:19 +02:00
|
|
|
self:increase_usedpower(factions.power_per_parcel)
|
2016-08-14 15:53:40 +02:00
|
|
|
self:on_claim_parcel(parcelpos)
|
2016-08-08 03:30:29 +02:00
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
--! @brief claim a parcel, update power and update global parcels table
|
|
|
|
function factions.Faction.unclaim_parcel(self, parcelpos)
|
|
|
|
factions.parcels[parcelpos] = nil
|
|
|
|
self.land[parcelpos] = nil
|
|
|
|
self:increase_power(factions.power_per_parcel)
|
2016-08-16 15:44:19 +02:00
|
|
|
self:decrease_usedpower(factions.power_per_parcel)
|
2016-08-14 15:53:40 +02:00
|
|
|
self:on_unclaim_parcel(parcelpos)
|
2016-08-08 03:30:29 +02:00
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
--! @brief disband faction, updates global players and parcels table
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.disband(self)
|
2016-08-08 11:48:37 +02:00
|
|
|
for k, _ in pairs(self.players) do -- remove players affiliation
|
|
|
|
factions.players[k] = nil
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:53:40 +02:00
|
|
|
for k, v in pairs(self.land) do -- remove parcel claims
|
|
|
|
factions.parcels[k] = nil
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
|
|
|
self:on_disband()
|
|
|
|
factions.factions[self.name] = nil
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @brief change the faction leader
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.set_leader(self, player)
|
|
|
|
self.leader = player
|
|
|
|
self.players[player] = self.default_leader_rank
|
|
|
|
self:on_new_leader()
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @brief check permissions for a given player
|
|
|
|
--! @return boolean indicating permissions. Players not in faction always receive false
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.has_permission(self, player, permission)
|
|
|
|
local p = self.players[player]
|
|
|
|
if not p then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local perms = self.ranks[p]
|
|
|
|
for i in ipairs(perms) do
|
|
|
|
if perms[i] == permission then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
function factions.Faction.set_description(self, new)
|
|
|
|
self.description = new
|
|
|
|
self:on_change_description()
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @brief places player in invite list
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.invite_player(self, player)
|
|
|
|
self.invited_players[player] = true
|
|
|
|
self:on_player_invited(player)
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @brief removes player from invite list (can no longer join via /f join)
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.revoke_invite(self, player)
|
|
|
|
self.invited_players[player] = nil
|
|
|
|
self:on_revoke_invite(player)
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief set faction openness
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.toggle_join_free(self, bool)
|
|
|
|
self.join_free = bool
|
|
|
|
self:on_toggle_join_free()
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @return true if a player can use /f join, false otherwise
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.can_join(self, player)
|
|
|
|
return self.join_free or self.invited_players[player]
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.new_alliance(self, faction)
|
|
|
|
self.allies[faction] = true
|
|
|
|
self:on_new_alliance(faction)
|
|
|
|
if self.enemies[faction] then
|
|
|
|
self:end_enemy(faction)
|
|
|
|
end
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
function factions.Faction.end_alliance(self, faction)
|
|
|
|
self.allies[faction] = nil
|
|
|
|
self:on_end_alliance(faction)
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
function factions.Faction.new_enemy(self, faction)
|
|
|
|
self.enemies[faction] = true
|
|
|
|
self:on_new_enemy(faction)
|
|
|
|
if self.allies[faction] then
|
|
|
|
self:end_alliance(faction)
|
|
|
|
end
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
function factions.Faction.end_enemy(self, faction)
|
|
|
|
self.enemies[faction] = nil
|
|
|
|
self:on_end_enemy(faction)
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @brief faction's member will now spawn in a new place
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.set_spawn(self, pos)
|
2016-08-08 16:31:11 +02:00
|
|
|
self.spawn = {x=pos.x, y=pos.y, z=pos.z}
|
2016-08-15 01:50:13 +02:00
|
|
|
self:on_set_spawn()
|
2016-08-08 03:30:29 +02:00
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @brief create a new rank with permissions
|
|
|
|
--! @param rank the name of the new rank
|
|
|
|
--! @param rank a list with the permissions of the new rank
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.add_rank(self, rank, perms)
|
|
|
|
self.ranks[rank] = perms
|
2016-08-08 16:31:11 +02:00
|
|
|
self:on_add_rank(rank)
|
2016-08-08 03:30:29 +02:00
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-12 11:16:43 +02:00
|
|
|
|
|
|
|
--! @brief delete a rank and replace it
|
|
|
|
--! @param rank the name of the rank to be deleted
|
|
|
|
--! @param newrank the rank given to players who were previously "rank"
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.delete_rank(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)
|
|
|
|
factions.save()
|
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @param newbanner texture string of the new banner
|
2016-08-08 21:09:10 +02:00
|
|
|
function factions.Faction.set_banner(self, newbanner)
|
|
|
|
self.banner = newbanner
|
|
|
|
self:on_new_banner()
|
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief set a player's rank
|
2016-08-08 23:25:31 +02:00
|
|
|
function factions.Faction.promote(self, member, rank)
|
|
|
|
self.players[member] = rank
|
|
|
|
self:on_promote(member)
|
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-12 11:16:43 +02:00
|
|
|
--! @brief send a message to all members
|
2016-08-09 23:00:53 +02:00
|
|
|
function factions.Faction.broadcast(self, msg, sender)
|
2016-08-10 01:54:18 +02:00
|
|
|
local message = self.name.."> "..msg
|
2016-08-09 23:00:53 +02:00
|
|
|
if sender then
|
|
|
|
message = sender.."@"..message
|
|
|
|
end
|
2016-08-10 01:54:18 +02:00
|
|
|
message = "Faction<"..message
|
2016-08-09 23:00:53 +02:00
|
|
|
for k, _ in pairs(self.players) do
|
|
|
|
minetest.chat_send_player(k, message)
|
|
|
|
end
|
|
|
|
end
|
2016-08-08 21:09:10 +02:00
|
|
|
|
2016-08-15 09:54:28 +02:00
|
|
|
--! @brief checks whether a faction has at least one connected player
|
|
|
|
function factions.Faction.is_online(self)
|
|
|
|
for playername, _ in pairs(self.players) do
|
|
|
|
if minetest.get_player_by_name(playername) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-08-15 18:31:11 +02:00
|
|
|
function factions.Faction.attack_parcel(self, parcelpos)
|
|
|
|
local attacked_faction = factions.parcels[parcelpos]
|
|
|
|
if attacked_faction then
|
|
|
|
attacked_faction = factions.factions[attacked_faction]
|
|
|
|
self.power = self.power - factions.power_per_attack
|
|
|
|
if attacked_faction.attacked_parcels[parcelpos] then
|
|
|
|
attacked_faction.attacked_parcels[parcelpos][self.name] = true
|
|
|
|
else
|
|
|
|
attacked_faction.attacked_parcels[parcelpos] = {[self.name] = true}
|
|
|
|
end
|
|
|
|
attacked_faction:broadcast("Parcel ("..parcelpos..") is being attacked by "..self.name.."!!")
|
|
|
|
if self.power < 0. then -- punish memers
|
|
|
|
minetest.chat_send_all("Faction "..self.name.." has attacked too much and has now negative power!")
|
|
|
|
end
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function factions.Faction.stop_attack(self, parcelpos)
|
|
|
|
local attacked_faction = factions.parcels[parcelpos]
|
|
|
|
if attacked_faction then
|
|
|
|
attacked_faction = factions.factions[attacked_faction]
|
|
|
|
if attacked_faction.attacked_parcels[parcelpos] then
|
|
|
|
attacked_faction.attacked_parcels[parcelpos][self.name] = nil
|
2016-08-15 23:52:12 +02:00
|
|
|
attacked_faction:broadcast("Parcel ("..parcelpos..") is no longer under attack from "..self.name..".")
|
|
|
|
self:broadcast("Parcel ("..parcelpos..") has been reconquered by "..attacked_faction.name..".")
|
2016-08-15 18:31:11 +02:00
|
|
|
end
|
|
|
|
factions.save()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
--------------------------
|
|
|
|
-- callbacks for events --
|
|
|
|
function factions.Faction.on_create(self) --! @brief called when the faction is added to the global faction list
|
2016-08-10 02:46:54 +02:00
|
|
|
minetest.chat_send_all("Faction "..self.name.." has been created.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_player_leave(self, player)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast(player.." has left this faction.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_player_join(self, player)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast(player.." has joined this faction.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
function factions.Faction.on_claim_parcel(self, pos)
|
2016-08-14 23:27:51 +02:00
|
|
|
self:broadcast("Parcel ("..pos..") has been claimed.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
function factions.Faction.on_unclaim_parcel(self, pos)
|
2016-08-14 23:27:51 +02:00
|
|
|
self:broadcast("Parcel ("..pos..") has been unclaimed.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_disband(self, pos)
|
2016-08-10 02:46:54 +02:00
|
|
|
minetest.chat_send_all("Faction "..self.name.." has been disbanded.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_new_leader(self)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast(self.leader.." is now the leader of this faction.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_change_description(self)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast("Faction description has been modified to: "..self.description)
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_player_invited(self, player)
|
2016-08-09 23:00:53 +02:00
|
|
|
minetest.chat_send_player(player, "You have been invited to faction "..self.name)
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_toggle_join_free(self, player)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast("This faction is now invite-free.")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_new_alliance(self, faction)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast("This faction is now allied with "..faction)
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_end_alliance(self, faction)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast("This faction is no longer allied with "..faction.."!")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_set_spawn(self)
|
2016-08-15 01:50:13 +02:00
|
|
|
self:broadcast("The faction spawn has been set to ("..util.coords3D_string(self.spawn)..").")
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_add_rank(self, rank)
|
2016-08-10 02:43:31 +02:00
|
|
|
self:broadcast("The rank "..rank.." has been created with privileges: "..table.concat(self.ranks[rank], ", "))
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 03:30:29 +02:00
|
|
|
function factions.Faction.on_delete_rank(self, rank, newrank)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast("The rank "..rank.." has been deleted and replaced by "..newrank)
|
2016-08-08 03:30:29 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 21:09:10 +02:00
|
|
|
function factions.Faction.on_new_banner(self)
|
2016-08-09 23:00:53 +02:00
|
|
|
self:broadcast("A new banner has been set.")
|
2016-08-08 21:09:10 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-08 23:25:31 +02:00
|
|
|
function factions.Faction.on_promote(self, member)
|
2016-08-15 00:24:30 +02:00
|
|
|
minetest.chat_send_player(member, "You have been promoted to "..self.players[member])
|
2016-08-09 23:00:53 +02:00
|
|
|
end
|
2016-08-14 15:43:53 +02:00
|
|
|
|
2016-08-09 23:00:53 +02:00
|
|
|
function factions.Faction.on_revoke_invite(self, player)
|
|
|
|
minetest.chat_send_player(player, "You are no longer invited to faction "..self.name)
|
2016-08-08 23:25:31 +02:00
|
|
|
end
|
2016-08-08 03:30:29 +02:00
|
|
|
|
2016-08-07 02:11:17 +02:00
|
|
|
--??????????????
|
2016-07-30 07:38:52 +02:00
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
function factions.get_parcel_pos(pos)
|
2016-08-08 01:10:47 +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-08 01:10:47 +02:00
|
|
|
local file,error = io.open(factions_worldid .. "/" .. "factions.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-08 01:10:47 +02:00
|
|
|
minetest.log("error","MOD factions: unable to save factions 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()
|
2016-08-08 01:10:47 +02:00
|
|
|
local file,error = io.open(factions_worldid .. "/" .. "factions.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-08 01:10:47 +02:00
|
|
|
minetest.log("action", facname..","..faction.name)
|
|
|
|
for player, rank in pairs(faction.players) do
|
|
|
|
minetest.log("action", player..","..rank)
|
|
|
|
factions.players[player] = facname
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
2016-08-14 15:53:40 +02:00
|
|
|
for parcelpos, val in pairs(faction.land) do
|
|
|
|
factions.parcels[parcelpos] = facname
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
2016-08-08 03:30:29 +02:00
|
|
|
setmetatable(faction, factions.Faction)
|
2016-08-15 00:37:16 +02:00
|
|
|
-- compatiblity and later additions
|
2016-08-15 18:31:11 +02:00
|
|
|
if not faction.maxpower or faction.maxpower <= 0. then
|
2016-08-15 15:00:57 +02:00
|
|
|
faction.maxpower = faction.power
|
2016-08-15 18:31:11 +02:00
|
|
|
if faction.power < 0. then
|
|
|
|
faction.maxpower = 0.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not faction.attacked_parcels then
|
|
|
|
faction.attacked_parcels = {}
|
2016-08-15 15:00:57 +02:00
|
|
|
end
|
2016-08-07 02:11:17 +02:00
|
|
|
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-08 18:51:17 +02:00
|
|
|
|
|
|
|
function factions.convert(filename)
|
|
|
|
local file, error = io.open(factions_worldid .. "/" .. filename, "r")
|
|
|
|
if not file then
|
|
|
|
minetest.chat_send_all("Cannot load file "..filename..". "..error)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local raw_data = file:read("*a")
|
|
|
|
local data = minetest.deserialize(raw_data)
|
|
|
|
local factionsmod = data.factionsmod
|
|
|
|
local objects = data.objects
|
|
|
|
for faction, attrs in pairs(factionsmod) do
|
|
|
|
local newfac = factions.new_faction(faction)
|
|
|
|
newfac:add_player(attrs.owner, "leader")
|
|
|
|
for player, _ in pairs(attrs.adminlist) do
|
|
|
|
if not newfac.players[player] then
|
|
|
|
newfac:add_player(player, "moderator")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for player, _ in pairs(attrs.invitations) do
|
|
|
|
newfac:invite_player(player)
|
|
|
|
end
|
2016-08-14 15:53:40 +02:00
|
|
|
for i in ipairs(attrs.parcel) do
|
|
|
|
local parcelpos = table.concat(attrs.parcel[i],",")
|
|
|
|
newfac:claim_parcel(parcelpos)
|
2016-08-08 18:51:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
for player, attrs in pairs(objects) do
|
|
|
|
local facname = attrs.factionsmod
|
|
|
|
local faction = factions.factions[facname]
|
|
|
|
if faction then
|
|
|
|
faction:add_player(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2016-08-07 02:11:17 +02:00
|
|
|
|
|
|
|
minetest.register_on_dieplayer(
|
|
|
|
function(player)
|
2016-08-15 09:54:28 +02:00
|
|
|
local faction = factions.players[player:get_player_name()]
|
|
|
|
if not faction then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
faction = factions.factions[faction]
|
|
|
|
faction:decrease_power(factions.power_per_death)
|
|
|
|
return true
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2016-08-15 09:54:28 +02:00
|
|
|
|
|
|
|
factions.faction_tick = function()
|
|
|
|
for facname, faction in pairs(factions.factions) do
|
|
|
|
if faction:is_online() then
|
|
|
|
faction:increase_power(factions.power_per_tick)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local hudUpdate = 0.
|
|
|
|
local factionUpdate = 0.
|
|
|
|
|
2016-08-08 17:14:39 +02:00
|
|
|
|
2016-08-07 02:11:17 +02:00
|
|
|
minetest.register_globalstep(
|
|
|
|
function(dtime)
|
2016-08-15 09:54:28 +02:00
|
|
|
hudUpdate = hudUpdate + dtime
|
|
|
|
factionUpdate = factionUpdate + dtime
|
|
|
|
if hudUpdate > .5 then
|
2016-08-08 17:14:39 +02:00
|
|
|
local playerslist = minetest.get_connected_players()
|
|
|
|
for i in pairs(playerslist) do
|
|
|
|
local player = playerslist[i]
|
2016-08-14 15:53:40 +02:00
|
|
|
local parcelpos = factions.get_parcel_pos(player:getpos())
|
|
|
|
local faction = factions.parcels[parcelpos]
|
2016-08-08 17:14:39 +02:00
|
|
|
player:hud_remove("factionLand")
|
|
|
|
player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
name = "factionLand",
|
|
|
|
number = 0xFFFFFF,
|
|
|
|
position = {x=0.1, y = .98},
|
|
|
|
text = faction or "Wilderness",
|
|
|
|
scale = {x=1, y=1},
|
|
|
|
alignment = {x=0, y=0},
|
|
|
|
})
|
|
|
|
end
|
2016-08-15 09:54:28 +02:00
|
|
|
hudUpdate = 0.
|
|
|
|
end
|
|
|
|
if factionUpdate > factions.tick_time then
|
|
|
|
factions.faction_tick()
|
|
|
|
factionUpdate = 0.
|
2016-08-08 17:14:39 +02:00
|
|
|
end
|
2016-08-07 02:11:17 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
minetest.register_on_joinplayer(
|
|
|
|
function(player)
|
|
|
|
end
|
|
|
|
)
|
2016-08-08 16:31:11 +02:00
|
|
|
minetest.register_on_respawnplayer(
|
|
|
|
function(player)
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
local faction = factions.players[playername]
|
|
|
|
if not faction then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
faction = factions.factions[faction]
|
|
|
|
if not faction.spawn then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
player:setpos(faction.spawn)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
2016-08-04 07:21:22 +02:00
|
|
|
|
2016-08-08 01:10:47 +02:00
|
|
|
|
2016-08-15 09:54:28 +02:00
|
|
|
|
2016-08-08 01:10:47 +02:00
|
|
|
local default_is_protected = minetest.is_protected
|
|
|
|
minetest.is_protected = function(pos, player)
|
2016-08-15 23:52:12 +02:00
|
|
|
if pos.y < factions.protection_max_depth then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if factions.disallow_edit_nofac and not player_faction then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-08-14 15:53:40 +02:00
|
|
|
local parcelpos = factions.get_parcel_pos(pos)
|
2016-08-15 00:24:30 +02:00
|
|
|
local parcel_faction = factions.parcels[parcelpos]
|
|
|
|
local player_faction = factions.players[player]
|
2016-08-15 18:31:11 +02:00
|
|
|
-- check if wielding death banner
|
|
|
|
local player_info = minetest.get_player_by_name(player)
|
2016-08-15 22:03:22 +02:00
|
|
|
if not player_info then
|
|
|
|
if parcel_faction then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2016-08-15 18:31:11 +02:00
|
|
|
local player_wield = player_info:get_wielded_item()
|
|
|
|
if player_wield:get_name() == "banners:death_banner" and player_faction then --todo: check for allies, maybe for permissions
|
2016-08-16 15:44:19 +02:00
|
|
|
player_faction = factions.factions[player_faction]
|
|
|
|
return not player_faction:has_permission(player, "claim") and player_faction.power > 0.
|
2016-08-15 18:31:11 +02:00
|
|
|
end
|
2016-08-15 23:52:12 +02:00
|
|
|
-- no faction
|
2016-08-15 00:24:30 +02:00
|
|
|
if not parcel_faction then
|
2016-08-08 01:10:47 +02:00
|
|
|
return default_is_protected(pos, player)
|
|
|
|
else
|
2016-08-15 00:37:16 +02:00
|
|
|
parcel_faction = factions.factions[parcel_faction]
|
2016-08-15 23:52:12 +02:00
|
|
|
if parcel_faction.name == player_faction then
|
|
|
|
return not parcel_faction:has_permission(player, "build")
|
|
|
|
elseif parcel_faction.attacked_parcels[parcelpos] then -- chunk is being attacked
|
2016-08-15 00:24:30 +02:00
|
|
|
if parcel_faction.attacked_parcels[parcelpos][player_faction] then -- attacked by the player's faction
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2016-08-08 01:10:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|