Merge pull request #16 from shamoanjac/master

fixes + powercap
This commit is contained in:
agrecascino 2016-08-15 12:12:40 -04:00 committed by GitHub
commit e1dbcbf473
3 changed files with 72 additions and 11 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
})
@ -479,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.")
@ -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
@ -127,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
@ -169,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
@ -178,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
@ -330,6 +350,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 +523,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)
@ -505,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
@ -548,15 +579,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 +623,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 +654,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)

View File

@ -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 ...")