2016-08-07 19:31:11 +02:00
--! @class factions
--! @brief main class for factions
factions = { }
2016-07-30 07:38:52 +02:00
2019-01-26 07:21:05 +01:00
-- Create cold databases.
factions.factions = colddb.Colddb ( " factions/factions " )
factions.parcels = colddb.Colddb ( " factions/parcels " )
factions.players = colddb.Colddb ( " factions/players " )
factions.player_ips = colddb.Colddb ( " factions/ips " )
-- Memory only storage.
factions.onlineplayers = { }
2016-07-30 07:38:52 +02:00
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 )
2018-10-18 04:04:46 +02:00
if # name > factions_config.faction_name_max_length then
2016-08-18 15:26:19 +02:00
return false
2019-01-26 07:21:05 +01:00
elseif factions.factions . get ( name ) ~= nil 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
2016-08-09 23:00:53 +02:00
util = {
coords3D_string = function ( coords )
return coords.x .. " , " .. coords.y .. " , " .. coords.z
end
}
2019-01-26 07:21:05 +01:00
starting_ranks = { [ " leader " ] = { " build " , " door " , " container " , " name " , " description " , " motd " , " invite " , " kick "
, " player_title " , " spawn " , " with_draw " , " territory " , " claim " , " access " , " disband " , " flags " , " ranks " , " promote " } ,
[ " moderator " ] = { " claim " , " door " , " build " , " spawn " , " invite " , " kick " , " promote " } ,
[ " member " ] = { " build " , " container " , " door " }
2018-10-31 22:15:08 +01:00
}
2016-08-12 11:16:43 +02:00
-- Faction permissions:
--
-- build: dig and place nodes
2018-10-31 22:15:08 +01:00
-- pain_build: dig and place nodes but take damage doing so
-- door: open/close or dig doors
-- container: be able to use containers like chest
-- name: set the faction's name
-- description: Set the faction description
-- motd: set the faction's message of the day
-- invite: (un)invite players to join the faction
-- kick: kick players off the faction
2018-11-01 05:25:13 +01:00
-- player_title: set player titles
2018-12-27 08:09:41 +01:00
-- spawn: set the faction's spawn
2018-10-31 22:15:08 +01:00
-- with_draw: withdraw money from the faction's bank
-- territory: claim or unclaim territory
-- claim: (un)claim parcels of land
-- access: manage access to territory and parcels of land to players or factions
-- disband: disband the faction
-- flags: manage faction's flags
2018-12-27 08:09:41 +01:00
-- ranks: create, edit, and delete ranks
2016-08-12 11:16:43 +02:00
-- promote: set a player's rank
2019-01-05 01:31:30 +01:00
-- diplomacy: be able to control the faction's diplomacy
2018-10-31 22:15:08 +01:00
2019-01-26 07:21:05 +01:00
factions.permissions = { " build " , " pain_build " , " door " , " container " , " name " , " description " , " motd " , " invite " , " kick "
, " player_title " , " spawn " , " with_draw " , " territory " , " claim " , " access " , " disband " , " flags " , " ranks " , " promote " }
factions.permissions_desc = { " dig and place nodes " , " dig and place nodes but take damage doing so " , " open/close or dig faction doors " , " be able to use containers like chest " , " set the faction's name "
, " Set the faction description " , " set the faction's message of the day " , " (un)invite players to join the faction " , " kick players off the faction " , " set player titles " , " set the faction's spawn "
, " withdraw money from the faction's bank " , " claim or unclaim territory " , " (un)claim parcels of land " , " manage access to territory and parcels of land to players or factions "
, " disband the faction " , " manage faction's flags " , " create, edit, and delete ranks " , " set a player's rank " }
2018-10-31 22:15:08 +01:00
-- open: can the faction be joined without an invite?
-- monsters: can monsters spawn on your land?
-- tax_kick: will players be kicked for not paying tax?
-- animals: can animals spawn on your land?
2019-01-05 01:31:30 +01:00
factions.flags = { " open " , " monsters " , " tax_kick " , " animals " }
2019-01-26 07:21:05 +01:00
factions.flags_desc = { " can the faction be joined without an invite? " , " can monsters spawn on your land?(unused) " , " will players be kicked for not paying tax?(unused) " , " can animals spawn on your land?(unused) " }
2018-10-31 22:15:08 +01:00
if factions_config.faction_diplomacy == true then
2019-01-26 07:21:05 +01:00
table.insert ( factions.permissions , " diplomacy " )
2018-10-31 22:15:08 +01:00
2019-01-26 07:21:05 +01:00
table.insert ( factions.permissions_desc , " be able to control the faction's diplomacy " )
2018-10-31 22:15:08 +01:00
local lt = starting_ranks [ " leader " ]
2019-01-26 07:21:05 +01:00
table.insert ( lt , " diplomacy " )
2018-10-31 22:15:08 +01:00
starting_ranks [ " leader " ] = lt
end
2018-10-15 17:24:58 +02:00
2019-01-26 07:21:05 +01:00
function factions . new ( )
return {
2018-10-31 22:15:08 +01:00
name = " " ,
2016-08-14 15:53:40 +02:00
--! @brief power of a faction (needed for parcel claiming)
2018-10-18 04:04:46 +02:00
power = factions_config.power ,
2016-08-12 11:16:43 +02:00
--! @brief maximum power of a faction
2018-10-18 04:04:46 +02:00
maxpower = factions_config.maxpower ,
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
2018-10-31 22:15:08 +01:00
ranks = starting_ranks ,
2016-08-12 11:16:43 +02:00
--! @brief name of the leader
2016-08-07 02:11:17 +02:00
leader = nil ,
2018-10-31 22:15:08 +01:00
--! @brief spawn of the faction
spawn = { x = 0 , y = 0 , z = 0 } ,
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. " ,
2018-10-25 07:21:42 +02:00
--! @brief faction's message of the day.
message_of_the_day = " " ,
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 = { } ,
2018-10-15 17:24:58 +02:00
--
request_inbox = { } ,
2016-08-12 11:16:43 +02:00
--! @brief table of enemies
2016-08-07 02:11:17 +02:00
enemies = { } ,
2018-10-15 17:24:58 +02:00
--!
2018-10-18 07:15:26 +02:00
neutral = { } ,
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)
2018-10-31 22:15:08 +01:00
join_free = false ,
2016-08-17 22:28:12 +02:00
--! @brief gives certain privileges
2016-08-20 00:12:40 +02:00
is_admin = false ,
--! @brief last time anyone logged on
last_logon = os.time ( ) ,
2018-10-15 17:24:58 +02:00
--! @brief how long this has been without parcels
no_parcel = os.time ( ) ,
2019-01-26 07:21:05 +01:00
}
2016-08-08 03:30:29 +02:00
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
2019-01-26 07:21:05 +01:00
function factions . new_faction ( name )
local faction = factions.new ( )
2016-08-08 03:30:29 +02:00
faction.name = name
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
factions.on_create ( name )
2018-10-18 04:31:07 +02:00
minetest.after ( 1 ,
2019-01-26 07:21:05 +01:00
function ( name )
factions.on_no_parcel ( name )
end , name )
factions.onlineplayers [ name ] = { }
return faction
2016-08-07 02:11:17 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . start_diplomacy ( name , faction )
for l , i in pairs ( factions.get_faction_list ( ) ) do
local fac = factions.factions . get ( i )
2018-10-23 05:21:32 +02:00
if i ~= name and not ( faction.neutral [ i ] or faction.allies [ i ] or faction.enemies [ i ] ) then
2018-12-27 08:16:11 +01:00
if factions_config.faction_diplomacy == true then
2019-01-26 07:21:05 +01:00
factions.new_neutral ( name , i )
factions.new_neutral ( i , name )
2018-12-27 08:16:11 +01:00
else
2019-01-26 07:21:05 +01:00
factions.new_enemy ( name , i )
factions.new_enemy ( i , name )
2018-12-27 08:16:11 +01:00
end
2018-10-23 05:21:32 +02:00
end
end
end
2019-01-26 07:21:05 +01:00
function factions . set_name ( oldname , name )
local faction = factions.factions . get ( oldname )
faction.name = name
for i , v in pairs ( factions.get_faction_list ( ) ) do
if v ~= oldname then
local fac = factions.factions . get ( v )
2018-10-31 22:15:08 +01:00
if fac.neutral [ oldname ] then
fac.neutral [ oldname ] = nil
fac.neutral [ name ] = true
end
2019-01-26 07:21:05 +01:00
2018-10-31 22:15:08 +01:00
if fac.allies [ oldname ] then
fac.allies [ oldname ] = nil
fac.allies [ name ] = true
end
2019-01-26 07:21:05 +01:00
2018-10-31 22:15:08 +01:00
if fac.enemies [ oldname ] then
fac.enemies [ oldname ] = nil
fac.enemies [ name ] = true
end
2019-01-26 07:21:05 +01:00
if fac.request_inbox [ oldname ] then
local value = fac.request_inbox [ oldname ]
fac.request_inbox [ oldname ] = nil
fac.request_inbox [ name ] = value
end
factions.factions . set ( v , fac )
2018-10-31 22:15:08 +01:00
end
end
2019-01-26 07:21:05 +01:00
for parcel in pairs ( faction.land ) do
factions.parcels . set ( parcel , name )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
for playername in pairs ( faction.players ) do
factions.players . set ( playername , name )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
for playername in pairs ( factions.onlineplayers [ oldname ] ) do
updateFactionName ( playername , name )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
factions.onlineplayers [ name ] = factions.onlineplayers [ oldname ]
factions.onlineplayers [ oldname ] = nil
factions.factions . remove ( oldname )
factions.factions . set ( name , faction )
factions.on_set_name ( name , oldname )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . increase_power ( name , power )
local faction = factions.factions . get ( name )
faction.power = faction.power + power
if faction.power > faction.maxpower - faction.usedpower then
faction.power = faction.maxpower - faction.usedpower
2016-08-15 09:54:28 +02:00
end
2019-01-26 07:21:05 +01:00
for i in pairs ( factions.onlineplayers [ name ] ) do
updateHudPower ( minetest.get_player_by_name ( i ) , faction )
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . decrease_power ( name , power )
local faction = factions.factions . get ( name )
faction.power = faction.power - power
for i in pairs ( factions.onlineplayers [ name ] ) do
updateHudPower ( minetest.get_player_by_name ( i ) , faction )
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . increase_maxpower ( name , power )
local faction = factions.factions . get ( name )
faction.maxpower = faction.maxpower + power
for i in pairs ( factions.onlineplayers [ name ] ) do
updateHudPower ( minetest.get_player_by_name ( i ) , faction )
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2016-08-15 10:16:12 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . decrease_maxpower ( name , power )
local faction = factions.factions . get ( name )
faction.maxpower = faction.maxpower - power
if faction.maxpower < 0. then -- should not happen
faction.maxpower = 0.
2016-08-15 10:16:12 +02:00
end
2019-01-26 07:21:05 +01:00
for i in pairs ( factions.onlineplayers [ name ] ) do
updateHudPower ( minetest.get_player_by_name ( i ) , faction )
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2016-08-15 10:16:12 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . increase_usedpower ( name , power )
local faction = factions.factions . get ( name )
faction.usedpower = faction.usedpower + power
for i in pairs ( factions.onlineplayers [ name ] ) do
updateHudPower ( minetest.get_player_by_name ( i ) , faction )
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2016-08-16 15:44:19 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . decrease_usedpower ( name , power )
local faction = factions.factions . get ( name )
faction.usedpower = faction.usedpower - power
if faction.usedpower < 0. then
faction.usedpower = 0.
2016-08-16 15:44:19 +02:00
end
2019-01-26 07:21:05 +01:00
for i in pairs ( factions.onlineplayers [ name ] ) do
updateHudPower ( minetest.get_player_by_name ( i ) , faction )
2018-10-23 05:21:32 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2018-10-23 05:21:32 +02:00
end
2016-08-16 15:44:19 +02:00
2019-01-26 07:21:05 +01:00
function factions . count_land ( name )
2016-08-16 15:44:19 +02:00
local count = 0.
2019-01-26 07:21:05 +01:00
for k , v in pairs ( factions.factions . get ( name ) . land ) do
2016-08-16 15:44:19 +02:00
count = count + 1
end
return count
end
2018-10-15 17:24:58 +02:00
minetest.register_on_prejoinplayer ( function ( name , ip )
2019-01-26 07:21:05 +01:00
factions.player_ips . set ( name , ip )
2018-10-15 17:24:58 +02:00
end )
2019-01-26 07:21:05 +01:00
function factions . add_player ( name , player , rank )
local faction = factions.factions . get ( name )
if factions.onlineplayers [ name ] == nil then
factions.onlineplayers [ name ] = { }
end
factions.onlineplayers [ name ] [ player ] = true
factions.on_player_join ( name , player )
2018-10-18 04:04:46 +02:00
if factions_config.enable_power_per_player then
2019-01-26 07:21:05 +01:00
local ip = factions.player_ips . get ( player )
2018-10-15 17:24:58 +02:00
local notsame = true
2019-01-26 07:21:05 +01:00
for i , k in pairs ( faction.players ) do
local other_ip = factions.player_ips . get ( i )
2018-10-15 17:24:58 +02:00
if other_ip == ip then
notsame = false
break
end
end
if notsame then
2019-01-26 07:21:05 +01:00
factions.increase_maxpower ( name , factions_config.powermax_per_player )
2018-10-15 17:24:58 +02:00
end
end
2019-01-26 07:21:05 +01:00
faction.players [ player ] = rank or faction.default_rank
factions.players . set ( player , name )
faction.invited_players [ player ] = nil
2018-10-17 04:58:15 +02:00
local pdata = minetest.get_player_by_name ( player )
local ipc = pdata : is_player_connected ( player )
if ipc then
2019-01-26 07:21:05 +01:00
createHudFactionName ( pdata , name )
createHudPower ( pdata , faction )
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . check_players_in_faction ( name )
for i , k in pairs ( factions.factions . get ( name ) . players ) do
2018-10-27 17:51:39 +02:00
return true
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.disband ( name , " Zero players on faction. " )
2018-10-27 17:51:39 +02:00
return false
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . remove_player ( name , player )
local faction = factions.factions . get ( name )
if factions.onlineplayers [ name ] == nil then
factions.onlineplayers [ name ] = { }
end
factions.onlineplayers [ name ] [ player ] = nil
faction.players [ player ] = nil
factions.factions . set ( name , faction )
factions.players . remove ( player )
factions.on_player_leave ( name , player )
2018-10-18 04:04:46 +02:00
if factions_config.enable_power_per_player then
2019-01-26 07:21:05 +01:00
local ip = factions.player_ips . get ( player )
2018-10-15 17:24:58 +02:00
local notsame = true
2019-01-26 07:21:05 +01:00
for i , k in pairs ( faction.players ) do
local other_ip = factions.player_ips . get ( i )
2018-10-15 17:24:58 +02:00
if other_ip == ip then
notsame = false
break
end
end
if notsame then
2019-01-26 07:21:05 +01:00
factions.decrease_maxpower ( name , factions_config.powermax_per_player )
2018-10-15 17:24:58 +02:00
end
end
2019-01-26 07:21:05 +01:00
2018-10-17 04:58:15 +02:00
local pdata = minetest.get_player_by_name ( player )
local ipc = pdata : is_player_connected ( player )
2019-01-26 07:21:05 +01:00
2018-10-17 04:58:15 +02:00
if ipc then
removeHud ( pdata , " factionName " )
removeHud ( pdata , " powerWatch " )
2018-10-12 19:41:55 +02:00
end
2019-01-26 07:21:05 +01:00
factions.check_players_in_faction ( name )
2016-08-08 03:30:29 +02:00
end
2019-01-26 07:21:05 +01:00
2019-01-22 23:16:56 +01:00
local parcel_size = factions_config.parcel_size
2019-01-26 07:21:05 +01:00
2016-08-14 15:53:40 +02:00
--! @param parcelpos position of the wanted parcel
--! @return whether this faction can claim a parcelpos
2019-01-26 07:21:05 +01:00
function factions . can_claim_parcel ( name , parcelpos )
local fn = factions.parcels . get ( parcelpos )
if fn == nil then
return true
end
local faction = factions.factions . get ( name )
if fn then
local fac = factions.factions . get ( fn )
if fac.power < 0. and faction.power >= factions_config.power_per_parcel and not faction.allies [ fn ] and not faction.neutral [ fn ] then
2016-08-09 04:39:40 +02:00
return true
else
return false
end
2019-01-26 07:21:05 +01:00
elseif faction.power < factions_config.power_per_parcel then
2016-08-08 16:31:11 +02:00
return false
end
2019-01-26 07:21:05 +01:00
2016-08-08 16:31:11 +02:00
return true
end
2016-08-14 15:53:40 +02:00
--! @brief claim a parcel, update power and update global parcels table
2019-01-26 07:21:05 +01:00
function factions . claim_parcel ( name , parcelpos )
2016-08-09 04:39:40 +02:00
-- check if claiming over other faction's territory
2019-01-26 07:21:05 +01:00
local otherfac = factions.parcels . get ( parcelpos )
2016-08-09 04:39:40 +02:00
if otherfac then
2019-01-26 07:21:05 +01:00
factions.unclaim_parcel ( otherfac , parcelpos )
factions.parcelless_check ( otherfac )
2016-08-09 04:39:40 +02:00
end
2019-01-26 07:21:05 +01:00
factions.parcels . set ( parcelpos , name )
local faction = factions.factions . get ( name )
faction.land [ parcelpos ] = true
factions.factions . set ( name , faction )
factions.decrease_power ( name , factions_config.power_per_parcel )
factions.increase_usedpower ( name , factions_config.power_per_parcel )
factions.on_claim_parcel ( name , parcelpos )
factions.parcelless_check ( name )
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
--! @brief claim a parcel, update power and update global parcels table
2019-01-26 07:21:05 +01:00
function factions . unclaim_parcel ( name , parcelpos )
factions.parcels . remove ( parcelpos )
local faction = factions.factions . get ( name )
faction.land [ parcelpos ] = nil
factions.factions . set ( name , faction )
factions.increase_power ( name , factions_config.power_per_parcel )
factions.decrease_usedpower ( name , factions_config.power_per_parcel )
factions.on_unclaim_parcel ( name , parcelpos )
factions.parcelless_check ( name )
end
function factions . parcelless_check ( name )
local faction = factions.factions . get ( name )
if faction.land then
2018-10-15 17:24:58 +02:00
local count = 0
2019-01-26 07:21:05 +01:00
for index , value in pairs ( faction.land ) do
2018-10-15 17:24:58 +02:00
count = count + 1
2018-10-23 06:03:11 +02:00
break
2018-10-15 17:24:58 +02:00
end
if count > 0 then
2019-01-26 07:21:05 +01:00
if faction.no_parcel ~= - 1 then
factions.broadcast ( name , " Faction " .. name .. " will not be disbanded because it now has parcels. " )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
faction.no_parcel = - 1
2018-10-15 17:24:58 +02:00
else
2019-01-26 07:21:05 +01:00
faction.no_parcel = os.time ( )
factions.on_no_parcel ( name )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2018-10-15 17:24:58 +02:00
end
end
2016-08-14 15:53:40 +02:00
--! @brief disband faction, updates global players and parcels table
2019-01-26 07:21:05 +01:00
function factions . disband ( name , reason )
local faction = factions.factions . get ( name )
if not faction.is_admin then
for i , v in pairs ( factions.get_faction_list ( ) ) do
local fac = factions.factions . get ( v )
if fac.name ~= name then
if fac.enemies [ name ] then
factions.end_enemy ( fac.name , name )
end
if fac.allies [ name ] then
factions.end_alliance ( fac.name , name )
2018-10-28 05:14:52 +01:00
end
2019-01-26 07:21:05 +01:00
if fac.neutral [ name ] then
factions.end_neutral ( fac.name , name )
2018-10-28 05:14:52 +01:00
end
2019-01-26 07:21:05 +01:00
if fac.request_inbox [ name ] then
fac.request_inbox [ name ] = nil
2018-10-28 05:14:52 +01:00
end
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( v , fac )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
for k , _ in pairs ( faction.players ) do -- remove players affiliation
factions.players . remove ( k )
2018-10-28 05:14:52 +01:00
end
2019-01-26 07:21:05 +01:00
for k , v in pairs ( faction.land ) do -- remove parcel claims
factions.parcels . remove ( k )
2018-10-28 05:14:52 +01:00
end
2019-01-26 07:21:05 +01:00
factions.on_disband ( name , reason )
for i , l in pairs ( factions.onlineplayers [ name ] ) do
removeHud ( i , " factionName " )
removeHud ( i , " powerWatch " )
2018-10-28 05:14:52 +01:00
end
2019-01-26 07:21:05 +01:00
factions.onlineplayers [ name ] = nil
factions.factions . remove ( name )
2018-10-15 17:24:58 +02:00
end
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
--! @brief change the faction leader
2019-01-26 07:21:05 +01:00
function factions . set_leader ( name , player )
local faction = factions.factions . get ( name )
if faction.leader then
faction.players [ faction.leader ] = faction.default_rank
2016-08-27 22:34:09 +02:00
end
2019-01-26 07:21:05 +01:00
faction.leader = player
faction.players [ player ] = faction.default_leader_rank
factions.on_new_leader ( )
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
2019-01-26 07:21:05 +01:00
function factions . set_message_of_the_day ( name , text )
local faction = factions.factions . get ( name )
faction.message_of_the_day = text
factions.factions . set ( name , faction )
2018-10-25 07:21:42 +02:00
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
2019-01-26 07:21:05 +01:00
function factions . has_permission ( name , player , permission )
local faction = factions.factions . get ( name )
local p = faction.players [ player ]
2016-08-08 03:30:29 +02:00
if not p then
return false
end
2019-01-26 07:21:05 +01:00
local perms = faction.ranks [ p ]
2018-10-27 21:01:49 +02:00
if perms then
for i in ipairs ( perms ) do
if perms [ i ] == permission then
return true
end
end
2018-10-30 01:36:46 +01:00
else
2019-01-26 07:21:05 +01:00
return false
2018-10-27 21:01:49 +02:00
end
2016-08-08 03:30:29 +02:00
end
2018-10-22 21:30:11 +02:00
2019-01-26 07:21:05 +01:00
function factions . set_description ( name , new )
local faction = factions.factions . get ( name )
faction.description = new
factions.on_change_description ( name )
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
--! @brief places player in invite list
2019-01-26 07:21:05 +01:00
function factions . invite_player ( name , player )
local faction = factions.factions . get ( name )
faction.invited_players [ player ] = true
factions.on_player_invited ( name , player )
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
--! @brief removes player from invite list (can no longer join via /f join)
2019-01-26 07:21:05 +01:00
function factions . revoke_invite ( name , player )
local faction = factions.factions . get ( name )
faction.invited_players [ player ] = nil
factions.on_revoke_invite ( name , player )
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
--! @brief set faction openness
2019-01-26 07:21:05 +01:00
function factions . toggle_join_free ( name , bool )
local faction = factions.factions . get ( name )
faction.join_free = bool
factions.on_toggle_join_free ( name )
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
--! @return true if a player can use /f join, false otherwise
2019-01-26 07:21:05 +01:00
function factions . can_join ( name , player )
local faction = factions.factions . get ( name )
return faction.join_free or faction.invited_players [ player ]
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
2019-01-26 07:21:05 +01:00
function factions . new_alliance ( name , faction )
local bfaction = factions.factions . get ( name )
bfaction.allies [ faction ] = true
factions.on_new_alliance ( name , faction )
if bfaction.enemies [ faction ] then
factions.end_enemy ( name , faction )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
if bfaction.neutral [ faction ] then
factions.end_neutral ( name , faction )
2016-08-08 03:30:29 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , bfaction )
2016-08-08 03:30:29 +02:00
end
2018-10-22 21:30:11 +02:00
2019-01-26 07:21:05 +01:00
function factions . end_alliance ( name , faction )
local bfaction = factions.factions . get ( name )
bfaction.allies [ faction ] = nil
factions.on_end_alliance ( name , faction )
factions.factions . set ( name , bfaction )
2016-08-08 03:30:29 +02:00
end
2018-10-22 21:30:11 +02:00
2019-01-26 07:21:05 +01:00
function factions . new_neutral ( name , faction )
local bfaction = factions.factions . get ( name )
bfaction.neutral [ faction ] = true
factions.on_new_neutral ( name , faction )
if bfaction.allies [ faction ] then
factions.end_alliance ( name , faction )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
if bfaction.enemies [ faction ] then
factions.end_enemy ( name , faction )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , bfaction )
2018-10-15 17:24:58 +02:00
end
2018-10-22 21:30:11 +02:00
2019-01-26 07:21:05 +01:00
function factions . end_neutral ( name , faction )
local bfaction = factions.factions . get ( name )
bfaction.neutral [ faction ] = nil
factions.on_end_neutral ( name , faction )
factions.factions . set ( name , bfaction )
2018-10-15 17:24:58 +02:00
end
2018-10-22 21:30:11 +02:00
2019-01-26 07:21:05 +01:00
function factions . new_enemy ( name , faction )
local bfaction = factions.factions . get ( name )
bfaction.enemies [ faction ] = true
factions.on_new_enemy ( name , faction )
if bfaction.allies [ faction ] then
factions.end_alliance ( name , faction )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
if bfaction.neutral [ faction ] then
factions.end_neutral ( name , faction )
2016-08-08 03:30:29 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , bfaction )
2016-08-08 03:30:29 +02:00
end
2018-10-22 21:30:11 +02:00
2019-01-26 07:21:05 +01:00
function factions . end_enemy ( name , faction )
local bfaction = factions.factions . get ( name )
bfaction.enemies [ faction ] = nil
factions.on_end_enemy ( name , faction )
factions.factions . set ( name , bfaction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
--! @brief faction's member will now spawn in a new place
2019-01-26 07:21:05 +01:00
function factions . set_spawn ( name , pos )
local faction = factions.factions . get ( name )
faction.spawn = { x = pos.x , y = pos.y , z = pos.z }
factions.on_set_spawn ( name )
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
2019-01-26 07:21:05 +01:00
function factions . tp_spawn ( name , playername )
local faction = factions.factions . get ( name )
2018-10-31 22:15:08 +01:00
player = minetest.get_player_by_name ( playername )
2019-01-26 07:21:05 +01:00
2018-10-31 22:15:08 +01:00
if player then
2019-01-26 07:21:05 +01:00
player : set_pos ( faction.spawn )
2018-10-31 22:15:08 +01:00
end
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
2019-01-26 07:21:05 +01:00
function factions . add_rank ( name , rank , perms )
local faction = factions.factions . get ( name )
faction.ranks [ rank ] = perms
factions.on_add_rank ( name , rank )
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-12 11:16:43 +02:00
2018-10-31 22:15:08 +01:00
--! @brief replace an rank's permissions
--! @param rank the name of the rank to edit
--! @param add or remove permissions to the rank
2019-01-26 07:21:05 +01:00
function factions . replace_privs ( name , rank , perms )
local faction = factions.factions . get ( name )
faction.ranks [ rank ] = perms
factions.on_replace_privs ( name , rank )
factions.factions . set ( name , faction )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . remove_privs ( name , rank , perms )
local faction = factions.factions . get ( name )
2018-10-31 22:15:08 +01:00
local revoked = false
2019-01-26 07:21:05 +01:00
local p = faction.ranks [ rank ]
2018-10-31 22:15:08 +01:00
for index , perm in pairs ( p ) do
2019-01-26 07:21:05 +01:00
if table_Contains ( perms , perm ) then
2018-10-31 22:15:08 +01:00
revoked = true
2019-01-26 07:21:05 +01:00
table.remove ( p , index )
2018-10-31 22:15:08 +01:00
end
end
2019-01-26 07:21:05 +01:00
faction.ranks [ rank ] = p
2018-10-31 22:15:08 +01:00
if revoked then
2019-01-26 07:21:05 +01:00
factions.on_remove_privs ( name , rank , perms )
2018-10-31 22:15:08 +01:00
else
2019-01-26 07:21:05 +01:00
factions.broadcast ( name , " No privilege was revoked from rank " .. rank .. " . " )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . add_privs ( name , rank , perms )
local faction = factions.factions . get ( name )
2018-10-31 22:15:08 +01:00
local added = false
2019-01-26 07:21:05 +01:00
local p = faction.ranks [ rank ]
2018-10-31 22:15:08 +01:00
for index , perm in pairs ( perms ) do
2019-01-26 07:21:05 +01:00
if not table_Contains ( p , perm ) then
2018-10-31 22:15:08 +01:00
added = true
2019-01-26 07:21:05 +01:00
table.insert ( p , perm )
2018-10-31 22:15:08 +01:00
end
end
2019-01-26 07:21:05 +01:00
faction.ranks [ rank ] = p
2018-10-31 22:15:08 +01:00
if added then
2019-01-26 07:21:05 +01:00
factions.on_add_privs ( name , rank , perms )
2018-10-31 22:15:08 +01:00
else
2019-01-26 07:21:05 +01:00
factions.broadcast ( name , " The rank " .. rank .. " already has these privileges. " )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . set_rank_name ( name , oldrank , newrank )
local faction = factions.factions . get ( name )
local copyrank = faction.ranks [ oldrank ]
faction.ranks [ newrank ] = copyrank
faction.ranks [ oldrank ] = nil
for player , r in pairs ( faction.players ) do
2018-10-31 22:15:08 +01:00
if r == oldrank then
2019-01-26 07:21:05 +01:00
faction.players [ player ] = newrank
2018-10-31 22:15:08 +01:00
end
end
2019-01-26 07:21:05 +01:00
if oldrank == faction.default_leader_rank then
faction.default_leader_rank = newrank
2019-01-26 18:03:09 +01:00
factions.broadcast ( name , " The default leader rank has been set to " .. newrank )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
if oldrank == faction.default_rank then
faction.default_rank = newrank
2019-01-26 18:03:09 +01:00
factions.broadcast ( name , " The default rank given to new players is set to " .. newrank )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
factions.on_set_rank_name ( name , oldrank , newrank )
factions.factions . set ( name , faction )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . set_def_rank ( name , rank )
local faction = factions.factions . get ( name )
for player , r in pairs ( faction.players ) do
if r == rank or r == nil or not faction.ranks [ r ] then
faction.players [ player ] = rank
2018-10-30 01:36:46 +01:00
end
end
2019-01-26 07:21:05 +01:00
faction.default_rank = rank
factions.on_set_def_rank ( name , rank )
factions.factions . set ( name , faction )
end
function factions . reset_ranks ( name )
local faction = factions.factions . get ( name )
faction.ranks = starting_ranks
faction.default_rank = " member "
faction.default_leader_rank_rank = " leader "
for player , r in pairs ( faction.players ) do
if not player == leader and ( r == nil or not faction.ranks [ r ] ) then
faction.players [ player ] = faction.default_rank
2018-10-31 22:15:08 +01:00
elseif player == leader then
2019-01-26 07:21:05 +01:00
faction.players [ player ] = faction.default_leader_rank_rank
2018-10-31 22:15:08 +01:00
end
end
2019-01-26 07:21:05 +01:00
factions.on_reset_ranks ( name )
factions.factions . set ( name , faction )
2018-10-30 01:36:46 +01:00
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"
2019-01-26 07:21:05 +01:00
function factions . delete_rank ( name , rank , newrank )
local faction = factions.factions . get ( name )
for player , r in pairs ( faction.players ) do
2016-08-08 03:30:29 +02:00
if r == rank then
2019-01-26 07:21:05 +01:00
faction.players [ player ] = newrank
2016-08-08 03:30:29 +02:00
end
end
2019-01-26 07:21:05 +01:00
faction.ranks [ rank ] = nil
factions.on_delete_rank ( name , rank , newrank )
if rank == faction.default_leader_rank then
faction.default_leader_rank = newrank
2019-01-26 18:03:09 +01:00
factions.broadcast ( name , " The default leader rank has been set to " .. newrank )
2018-10-27 20:40:37 +02:00
end
2019-01-26 07:21:05 +01:00
if rank == faction.default_rank then
faction.default_rank = newrank
2019-01-26 18:03:09 +01:00
factions.broadcast ( name , " The default rank given to new players is set to " .. newrank )
2018-10-27 20:40:37 +02:00
end
2019-01-26 07:21:05 +01:00
factions.factions . set ( name , faction )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2016-08-12 11:16:43 +02:00
--! @brief set a player's rank
2019-01-26 07:21:05 +01:00
function factions . promote ( name , member , rank )
local faction = factions.factions . get ( name )
faction.players [ member ] = rank
factions.on_promote ( name , member )
factions.factions . set ( name , faction )
2016-08-08 23:25:31 +02:00
end
2016-08-14 15:43:53 +02:00
2016-08-12 11:16:43 +02:00
--! @brief send a message to all members
2019-01-26 07:21:05 +01:00
function factions . broadcast ( name , msg , sender )
if factions.onlineplayers [ name ] == nil then
factions.onlineplayers [ name ] = { }
end
local message = name .. " > " .. msg
2016-08-09 23:00:53 +02:00
if sender then
2019-01-26 07:21:05 +01:00
message = sender .. " @ " .. message
2016-08-09 23:00:53 +02:00
end
2019-01-26 07:21:05 +01:00
message = " Faction< " .. message
for k , _ in pairs ( factions.onlineplayers [ name ] ) do
2016-08-09 23:00:53 +02:00
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
2019-01-26 07:21:05 +01:00
function factions . is_online ( name )
if factions.onlineplayers [ name ] == nil then
factions.onlineplayers [ name ] = { }
end
for playername , _ in pairs ( factions.onlineplayers [ name ] ) do
2018-11-02 19:37:51 +01:00
return true
2016-08-15 09:54:28 +02:00
end
return false
end
2016-08-08 03:30:29 +02:00
--------------------------
-- callbacks for events --
2019-01-26 07:21:05 +01:00
function factions . on_create ( name ) --! @brief called when the faction is added to the global faction list
minetest.chat_send_all ( " Faction " .. name .. " has been created. " )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_set_name ( name , oldname )
minetest.chat_send_all ( " Faction " .. oldname .. " has been changed its name to " .. name .. " . " )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . on_no_parcel ( name )
local faction = factions.factions . get ( name )
local now = os.time ( ) - faction.no_parcel
2018-10-18 04:31:07 +02:00
local l = factions_config.maximum_parcelless_faction_time
2019-01-26 07:21:05 +01:00
factions.broadcast ( name , " This faction will disband in " .. l - now .. " seconds, because it has no parcels. " )
2018-10-18 04:31:07 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . on_player_leave ( name , player )
factions.broadcast ( name , player .. " has left this faction " )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_player_join ( name , player )
factions.broadcast ( name , player .. " has joined this faction " )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_claim_parcel ( name , pos )
factions.broadcast ( name , " Parcel ( " .. pos .. " ) has been claimed. " )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_unclaim_parcel ( name , pos )
factions.broadcast ( name , " Parcel ( " .. pos .. " ) has been unclaimed. " )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_disband ( name , reason )
local msg = " Faction " .. name .. " has been disbanded. "
2016-08-20 00:15:45 +02:00
if reason then
2019-01-26 07:21:05 +01:00
msg = msg .. " ( " .. reason .. " ) "
2016-08-20 00:15:45 +02:00
end
minetest.chat_send_all ( msg )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_new_leader ( name )
local faction = factions.factions . get ( name )
factions.broadcast ( name , faction.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
2019-01-26 07:21:05 +01:00
function factions . on_change_description ( name )
local faction = factions.factions . get ( name )
factions.broadcast ( name , " Faction description has been modified to: " .. faction.description )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_player_invited ( name , player )
local faction = factions.factions . get ( name )
minetest.chat_send_player ( player , " You have been invited to faction " .. faction.name )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_toggle_join_free ( name , player )
local faction = factions.factions . get ( name )
if faction.join_free then
factions.broadcast ( name , " This faction is now invite-free. " )
2016-08-29 01:41:23 +02:00
else
2019-01-26 07:21:05 +01:00
factions.broadcast ( name , " This faction is no longer invite-free. " )
2016-08-29 01:41:23 +02:00
end
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_new_alliance ( name , faction )
factions.broadcast ( name , " This faction is now allied with " .. faction )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_end_alliance ( name , faction )
factions.broadcast ( name , " 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
2019-01-26 07:21:05 +01:00
function factions . on_new_neutral ( name , faction )
factions.broadcast ( name , " This faction is now neutral with " .. faction )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . on_end_neutral ( name , faction )
factions.broadcast ( name , " This faction is no longer neutral with " .. faction .. " ! " )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . on_new_enemy ( name , faction )
factions.broadcast ( name , " This faction is now at war with " .. faction )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . on_end_enemy ( name , faction )
factions.broadcast ( name , " This faction is no longer at war with " .. faction .. " ! " )
2018-10-15 17:24:58 +02:00
end
2019-01-26 07:21:05 +01:00
function factions . on_set_spawn ( name )
local faction = factions.factions . get ( name )
factions.broadcast ( name , " The faction spawn has been set to ( " .. util.coords3D_string ( faction.spawn ) .. " ). " )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_add_rank ( name , rank )
local faction = factions.factions . get ( name )
factions.broadcast ( name , " The rank " .. rank .. " has been created with privileges: " .. table.concat ( faction.ranks [ rank ] , " , " ) )
2016-08-08 03:30:29 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_replace_privs ( name , rank )
local faction = factions.factions . get ( name )
factions.broadcast ( name , " The privileges in rank " .. rank .. " have been delete and changed to: " .. table.concat ( faction.ranks [ rank ] , " , " ) )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . on_remove_privs ( name , rank , privs )
factions.broadcast ( name , " The privileges in rank " .. rank .. " have been revoked: " .. table.concat ( privs , " , " ) )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . on_add_privs ( name , rank , privs )
factions.broadcast ( name , " The privileges in rank " .. rank .. " have been added: " .. table.concat ( privs , " , " ) )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . on_set_rank_name ( name , rank , newrank )
factions.broadcast ( name , " The name of rank " .. rank .. " has been changed to " .. newrank )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . on_delete_rank ( name , rank , newrank )
factions.broadcast ( name , " 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
2019-01-26 07:21:05 +01:00
function factions . on_set_def_rank ( name , rank )
factions.broadcast ( name , " The default rank given to new players has been changed to " .. rank )
2018-10-30 01:36:46 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . on_reset_ranks ( name )
factions.broadcast ( name , " All of the faction's ranks have been reset to the default ones. " )
2018-10-31 22:15:08 +01:00
end
2019-01-26 07:21:05 +01:00
function factions . on_promote ( name , member )
local faction = factions.factions . get ( name )
minetest.chat_send_player ( member , " You have been promoted to " .. faction.players [ member ] )
2016-08-09 23:00:53 +02:00
end
2016-08-14 15:43:53 +02:00
2019-01-26 07:21:05 +01:00
function factions . on_revoke_invite ( name , player )
minetest.chat_send_player ( player , " You are no longer invited to faction " .. name )
2016-08-08 23:25:31 +02:00
end
2016-08-08 03:30:29 +02:00
2016-08-14 15:53:40 +02:00
function factions . get_parcel_pos ( pos )
2018-10-18 04:40:00 +02:00
if factions_config.protection_style == " 2d " then
2018-12-27 08:22:46 +01:00
return math.floor ( pos.x / parcel_size ) * parcel_size .. " , " .. math.floor ( pos.z / parcel_size ) * parcel_size
2018-10-18 04:40:00 +02:00
elseif factions_config.protection_style == " 3d " then
2018-12-27 08:22:46 +01:00
return math.floor ( pos.x / parcel_size ) * parcel_size .. " , " .. math.floor ( pos.y / parcel_size ) * parcel_size .. " , " .. math.floor ( pos.z / parcel_size ) * parcel_size
2018-10-17 04:58:15 +02:00
end
2016-08-07 19:36:56 +02:00
end
2016-07-30 07:38:52 +02:00
2016-08-18 12:46:21 +02:00
function factions . get_player_faction ( playername )
2019-01-26 07:21:05 +01:00
local facname = factions.players . get ( playername )
2016-08-17 22:28:12 +02:00
if facname then
2019-01-26 07:21:05 +01:00
local faction = factions.factions . get ( facname )
return faction , facname
2016-08-17 22:28:12 +02:00
end
return nil
end
function factions . get_parcel_faction ( parcelpos )
2019-01-26 07:21:05 +01:00
local facname = factions.parcels . get ( parcelpos )
2016-08-17 22:28:12 +02:00
if facname then
2019-01-26 07:21:05 +01:00
local faction = factions.factions . get ( facname )
return faction , facname
2016-08-17 22:28:12 +02:00
end
return nil
end
function factions . get_faction ( facname )
2019-01-26 07:21:05 +01:00
return factions.factions . get ( facname )
2016-08-17 22:28:12 +02:00
end
function factions . get_faction_at ( pos )
2018-10-17 04:58:15 +02:00
local y = pos.y
2018-10-18 04:04:46 +02:00
if factions_config.protection_depth_height_limit and ( pos.y < factions_config.protection_max_depth or pos.y > factions_config.protection_max_height ) then
2018-10-17 04:58:15 +02:00
return nil
end
2016-08-17 22:28:12 +02:00
local parcelpos = factions.get_parcel_pos ( pos )
return factions.get_parcel_faction ( parcelpos )
end
2016-08-07 19:31:11 +02:00
function factions . get_faction_list ( )
2016-07-30 07:38:52 +02:00
2019-01-26 07:21:05 +01:00
local names = { }
local directory = string.format ( " %s/factions/factions " , minetest.get_worldpath ( ) )
local nameslist = minetest.get_dir_list ( directory )
for k , v in pairs ( nameslist ) do
names [ # names + 1 ] = v : sub ( 0 , v : len ( ) - 5 )
2018-11-01 17:09:29 +01:00
end
2016-07-30 07:38:52 +02:00
2019-01-26 07:21:05 +01:00
return names
2016-08-08 18:51:17 +02:00
end
2016-08-18 15:21:59 +02:00
2016-08-07 02:11:17 +02:00
minetest.register_on_dieplayer (
2016-08-18 15:21:59 +02:00
function ( player )
2019-01-26 07:21:05 +01:00
local faction , name = factions.get_player_faction ( player : get_player_name ( ) )
2016-08-18 15:21:59 +02:00
if not faction then
2016-08-15 09:54:28 +02:00
return true
2016-08-07 02:11:17 +02:00
end
2019-01-26 07:21:05 +01:00
factions.decrease_power ( name , factions_config.power_per_death )
2016-08-18 15:21:59 +02:00
return true
end
2016-08-07 02:11:17 +02:00
)
2018-10-17 04:58:15 +02:00
function factions . faction_tick ( )
2016-08-20 00:12:40 +02:00
local now = os.time ( )
2019-01-26 07:21:05 +01:00
for i , facname in pairs ( factions.get_faction_list ( ) ) do
local faction = factions.factions . get ( facname )
if factions.is_online ( facname ) then
2018-10-18 04:04:46 +02:00
if factions_config.enable_power_per_player then
2018-10-17 04:58:15 +02:00
local count = 0
2019-01-26 07:21:05 +01:00
for _ in pairs ( factions.onlineplayers [ facname ] ) do count = count + 1 end
factions.increase_power ( facname , factions_config.power_per_player * count )
2018-10-17 04:58:15 +02:00
else
2019-01-26 07:21:05 +01:00
factions.increase_power ( facname , factions_config.power_per_tick )
2018-10-15 17:24:58 +02:00
end
2016-08-15 09:54:28 +02:00
end
2018-10-18 04:04:46 +02:00
if now - faction.last_logon > factions_config.maximum_faction_inactivity or ( faction.no_parcel ~= - 1 and now - faction.no_parcel > factions_config.maximum_parcelless_faction_time ) then
2019-01-26 07:21:05 +01:00
local r = " "
if now - faction.last_logon > factions_config.maximum_faction_inactivity then
r = " inactivity "
else
r = " no parcel claims "
end
factions.disband ( facname , r )
2016-08-20 00:12:40 +02:00
end
2016-08-15 09:54:28 +02:00
end
end
2018-10-12 19:41:55 +02:00
2016-08-07 02:11:17 +02:00
minetest.register_on_joinplayer (
2016-08-18 15:21:59 +02:00
function ( player )
2018-10-12 19:41:55 +02:00
local name = player : get_player_name ( )
2019-01-26 07:21:05 +01:00
minetest.after ( 5 , createHudfactionLand , player )
local faction , facname = factions.get_player_faction ( name )
2016-08-21 01:15:41 +02:00
if faction then
2019-01-26 07:21:05 +01:00
if factions.onlineplayers [ facname ] == nil then
factions.onlineplayers [ facname ] = { }
end
factions.onlineplayers [ facname ] [ name ] = true
2016-08-21 01:15:41 +02:00
faction.last_logon = os.time ( )
2019-01-26 07:21:05 +01:00
factions.factions . set ( facname , faction )
minetest.after ( 5 , createHudFactionName , player , facname )
minetest.after ( 5 , createHudPower , player , faction )
2018-10-18 04:31:07 +02:00
if faction.no_parcel ~= - 1 then
local now = os.time ( ) - faction.no_parcel
local l = factions_config.maximum_parcelless_faction_time
2019-01-26 07:21:05 +01:00
minetest.chat_send_player ( name , " This faction will disband in " .. l - now .. " seconds, because it has no parcels. " )
2018-10-18 07:20:05 +02:00
end
2019-01-26 07:21:05 +01:00
if factions.has_permission ( facname , name , " diplomacy " ) then
for _ in pairs ( faction.request_inbox ) do minetest.chat_send_player ( name , " You have diplomatic requests in the inbox. " ) break end
2018-10-30 01:36:46 +01:00
end
2019-01-26 07:21:05 +01:00
2018-10-29 04:03:55 +01:00
if faction.message_of_the_day and ( faction.message_of_the_day ~= " " or faction.message_of_the_day ~= " " ) then
2019-01-26 07:21:05 +01:00
minetest.chat_send_player ( name , faction.message_of_the_day )
2018-10-25 07:21:42 +02:00
end
2016-08-21 01:15:41 +02:00
end
2019-01-26 07:21:05 +01:00
2016-08-18 15:21:59 +02:00
end
2016-08-07 02:11:17 +02:00
)
2018-10-12 19:41:55 +02:00
minetest.register_on_leaveplayer (
function ( player )
2018-10-17 04:58:15 +02:00
local name = player : get_player_name ( )
2019-01-26 07:21:05 +01:00
local faction , facname = factions.get_player_faction ( name )
2018-10-28 02:06:48 +01:00
local id_name1 = name .. " factionLand "
2019-01-26 07:21:05 +01:00
2018-10-28 02:06:48 +01:00
if hud_ids [ id_name1 ] then
hud_ids [ id_name1 ] = nil
end
2019-01-26 07:21:05 +01:00
2018-10-17 04:58:15 +02:00
if faction then
2019-01-26 07:21:05 +01:00
factions.onlineplayers [ facname ] [ name ] = nil
2018-10-28 02:06:48 +01:00
local id_name2 = name .. " factionName "
local id_name3 = name .. " powerWatch "
if hud_ids [ id_name2 ] then
hud_ids [ id_name2 ] = nil
end
if hud_ids [ id_name3 ] then
hud_ids [ id_name3 ] = nil
end
2018-11-01 17:09:29 +01:00
end
2018-10-12 19:41:55 +02:00
end
)
2016-08-08 16:31:11 +02:00
minetest.register_on_respawnplayer (
function ( player )
2019-01-26 07:21:05 +01:00
local faction , facname = factions.get_player_faction ( player : get_player_name ( ) )
if not faction then
2016-08-08 16:31:11 +02:00
return false
else
if not faction.spawn then
return false
else
2019-01-26 07:21:05 +01:00
player : set_pos ( faction.spawn )
2016-08-08 16:31:11 +02:00
return true
end
end
end
)
2016-08-04 07:21:22 +02:00
2016-08-08 01:10:47 +02:00
local default_is_protected = minetest.is_protected
minetest.is_protected = function ( pos , player )
2018-10-17 04:58:15 +02:00
local y = pos.y
2019-01-26 07:21:05 +01:00
2018-10-18 04:04:46 +02:00
if factions_config.protection_depth_height_limit and ( pos.y < factions_config.protection_max_depth or pos.y > factions_config.protection_max_height ) then
2016-08-15 23:52:12 +02:00
return false
end
2016-08-14 15:53:40 +02:00
local parcelpos = factions.get_parcel_pos ( pos )
2019-01-26 07:21:05 +01:00
local parcel_faction , parcel_fac_name = factions.get_parcel_faction ( parcelpos )
local player_faction , player_fac_name = factions.get_player_faction ( player )
2016-08-15 23:52:12 +02:00
-- no faction
2016-08-19 23:10:01 +02:00
if not parcel_faction then
2016-08-08 01:10:47 +02:00
return default_is_protected ( pos , player )
2016-08-19 23:10:01 +02:00
elseif player_faction then
2019-01-26 07:21:05 +01:00
if parcel_name == player_name then
if factions.has_permission ( parcel_fac_name , player , " pain_build " ) then
2018-10-31 22:15:08 +01:00
local p = minetest.get_player_by_name ( player )
p : set_hp ( p : get_hp ( ) - 0.5 )
end
2019-01-26 07:21:05 +01:00
return not ( factions.has_permission ( parcel_fac_name , player , " build " ) or factions.has_permission ( parcel_fac_name , player , " pain_build " ) )
elseif parcel_allies [ player_name ] then
if factions.has_permission ( player_fac_name , player , " pain_build " ) then
2018-10-31 22:15:08 +01:00
local p = minetest.get_player_by_name ( player )
p : set_hp ( p : get_hp ( ) - 0.5 )
end
2019-01-26 07:21:05 +01:00
return not ( factions.has_permission ( player_fac_name , player , " build " ) or factions.has_permission ( player_fac_name , player , " pain_build " ) )
2018-10-15 17:24:58 +02:00
else
2019-01-26 07:21:05 +01:00
return true
2016-08-15 00:24:30 +02:00
end
2016-08-19 23:10:01 +02:00
else
return true
2016-08-08 01:10:47 +02:00
end
end
2018-10-17 04:58:15 +02:00
function factionUpdate ( )
2018-10-27 22:40:17 +02:00
factions.faction_tick ( )
2019-01-26 07:21:05 +01:00
minetest.after ( factions_config.tick_time , factionUpdate )
end