Small enhancements and switched from power-only system to power/maxpower

This commit is contained in:
shamoanjac 2016-08-15 09:54:28 +02:00
parent f5fd22c272
commit ba012f1c0a
2 changed files with 53 additions and 9 deletions

View File

@ -316,7 +316,6 @@ factions.register_command("close", {
description = "Make your faction invite-only.",
on_success = function(player, faction, pos, parcelpos, args)
faction:toggle_join_free(false)
--TODO: message
return true
end
})
@ -326,7 +325,6 @@ factions.register_command("open", {
description = "Allow any player to join your faction.",
on_success = function(player, faction, pos, parcelpos, args)
faction:toggle_join_free(true)
--TODO: message
return true
end
})
@ -336,7 +334,6 @@ factions.register_command("description", {
description = "Set your faction's description",
on_success = function(player, faction, pos, parcelpos, args)
faction:set_description(table.concat(args.other," "))
--TODO: message
return true
end
})
@ -491,7 +488,7 @@ factions.register_command("promote", {
factions.register_command("power", {
description = "Display your faction's power",
on_success = function(player, faction, pos, parcelpos, args)
minetest.chat_send_player(player, "Power: "..faction.power)
minetest.chat_send_player(player, "Power: "..faction.power.."/"..faction.maxpower)
return true
end
})
@ -548,6 +545,14 @@ factions.register_command("chat", {
end
})
factions.register_command("forceupdate", {
description = "Forces an update tick.",
global_privileges = {"faction_admin"},
on_success = function(player, faction, pos, parcelpos, args)
factions.faction_tick()
end
})
-------------------------------------------------------------------------------
-- name: cmdhandler(playername,parameter)
--

View File

@ -29,6 +29,9 @@ factions.factions = {}
--- settings
factions.protection_max_depth = -512
factions.power_per_parcel = .5
factions.power_per_death = .25
factions.power_per_tick = .125
factions.tick_time = 60.
---------------------
--! @brief returns whether a faction can be created or not (allows for implementation of blacklists and the like)
@ -119,6 +122,9 @@ end
function factions.Faction.increase_power(self, power)
self.power = self.power + power
if self.power > self.maxpower then
self.power = self.maxpower
end
factions.save()
end
@ -330,6 +336,16 @@ function factions.Faction.broadcast(self, msg, sender)
end
end
--! @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
--------------------------
-- callbacks for events --
function factions.Faction.on_create(self) --! @brief called when the faction is added to the global faction list
@ -493,8 +509,6 @@ function factions.load()
local raw_data = file:read("*a")
factions.factions = minetest.deserialize(raw_data)
for facname, faction in pairs(factions.factions) do
faction.attacked_parcels = {}
faction.maxpower = 0.
minetest.log("action", facname..","..faction.name)
for player, rank in pairs(faction.players) do
minetest.log("action", player..","..rank)
@ -548,15 +562,34 @@ end
minetest.register_on_dieplayer(
function(player)
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
end
)
local lastUpdate = 0.
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.
minetest.register_globalstep(
function(dtime)
lastUpdate = lastUpdate + dtime
if lastUpdate > .5 then
hudUpdate = hudUpdate + dtime
factionUpdate = factionUpdate + dtime
if hudUpdate > .5 then
local playerslist = minetest.get_connected_players()
for i in pairs(playerslist) do
local player = playerslist[i]
@ -573,6 +606,11 @@ minetest.register_globalstep(
alignment = {x=0, y=0},
})
end
hudUpdate = 0.
end
if factionUpdate > factions.tick_time then
factions.faction_tick()
factionUpdate = 0.
end
end
)
@ -599,6 +637,7 @@ minetest.register_on_respawnplayer(
)
local default_is_protected = minetest.is_protected
minetest.is_protected = function(pos, player)
local parcelpos = factions.get_parcel_pos(pos)