From ba012f1c0a2f9399d3695144029aaf4e1c46d45c Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 15 Aug 2016 09:54:28 +0200 Subject: [PATCH 1/6] Small enhancements and switched from power-only system to power/maxpower --- chatcommands.lua | 13 +++++++++---- factions.lua | 49 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/chatcommands.lua b/chatcommands.lua index ea97712..855bbc3 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -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) -- diff --git a/factions.lua b/factions.lua index 15f97b2..8261867 100644 --- a/factions.lua +++ b/factions.lua @@ -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) From 047ae6ae7cace71c6eb913e6e7f2dc1932455e8a Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 15 Aug 2016 10:16:12 +0200 Subject: [PATCH 2/6] Added decrease_maxpower and increase_maxpower --- factions.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/factions.lua b/factions.lua index 8261867..a6b0a62 100644 --- a/factions.lua +++ b/factions.lua @@ -133,6 +133,18 @@ function factions.Faction.decrease_power(self, power) factions.save() end +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 + function factions.Faction.add_player(self, player, rank) self:on_player_join(player) self.players[player] = rank or self.default_rank From ad985c681d43eb306f347d75eb57ca7309b1c6ef Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 15 Aug 2016 12:07:32 +0200 Subject: [PATCH 3/6] Updated version --- init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 43dd156..f5708a6 100644 --- a/init.lua +++ b/init.lua @@ -12,7 +12,7 @@ -- Contact sapier a t gmx net ------------------------------------------------------------------------------- -factions_version = "0.7.0" +factions_version = "0.8.0" core.log("action", "MOD: factions (by sapier) loading ...") From 19940d92eeec732a2e3a0e95f5aec1e09195110f Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 15 Aug 2016 14:31:03 +0200 Subject: [PATCH 4/6] Claiming chunks also lowers the power cap --- factions.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/factions.lua b/factions.lua index a6b0a62..08393dd 100644 --- a/factions.lua +++ b/factions.lua @@ -187,6 +187,7 @@ function factions.Faction.claim_parcel(self, parcelpos) factions.parcels[parcelpos] = self.name self.land[parcelpos] = true self:decrease_power(factions.power_per_parcel) + self:decrease_maxpower(factions.power_per_parcel) self:on_claim_parcel(parcelpos) factions.save() end @@ -196,6 +197,7 @@ function factions.Faction.unclaim_parcel(self, parcelpos) factions.parcels[parcelpos] = nil self.land[parcelpos] = nil self:increase_power(factions.power_per_parcel) + self:increase_maxpower(factions.power_per_parcel) self:on_unclaim_parcel(parcelpos) factions.save() end From 7137f09b4c8ed1a1069e0c96d55fc1e568e738eb Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 15 Aug 2016 15:00:57 +0200 Subject: [PATCH 5/6] Prepared power and maxpower for model migration --- factions.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/factions.lua b/factions.lua index 08393dd..6164370 100644 --- a/factions.lua +++ b/factions.lua @@ -533,6 +533,9 @@ function factions.load() end setmetatable(faction, factions.Faction) -- compatiblity and later additions + if not faction.maxpower or faction.maxpower == 0. then + faction.maxpower = faction.power + end end file:close() end From f44b059afc640ae8c2d7191205dbec282c8f4bca Mon Sep 17 00:00:00 2001 From: shamoanjac Date: Mon, 15 Aug 2016 16:39:19 +0200 Subject: [PATCH 6/6] Fixed /f promote --- chatcommands.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatcommands.lua b/chatcommands.lua index 855bbc3..9a82737 100644 --- a/chatcommands.lua +++ b/chatcommands.lua @@ -476,7 +476,7 @@ factions.register_command("promote", { on_success = function(player, faction, pos, parcelpos, args) local rank = args.strings[1] if faction.ranks[rank] then - faction:promote(args.players[1], rank) + faction:promote(args.players[1]:get_player_name(), rank) return true else send_error(player, "The specified rank does not exist.")