You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.7 KiB
Lua

if engine.ActiveGamemode() ~= "zombiesurvival" then return end
local settings = {
StartingSavedPoints = 100,
MinimumPoints = 0,
PointsaveInterval = 30, -- Save points every x seconds
IgnoredMaps = { -- Use $ at the end if you want to define exact map names with versions. (Otherwise it will match with all maps beginning with the given name)
"zs_obj_gauntlet",
"zs_gauntlet_reborn"
}
}
function PointSaving_shouldIgnoreMap(mapName)
for k, v in ipairs(settings.IgnoredMaps) do
if string.match(string.lower(mapName), "^"..v) then return true end
end
end
local function savePoints(ply)
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
if not ply.PointSaving_StartPoints then return end
if ply:Team() == TEAM_HUMAN then
ply:SetSavedPoints(ply:GetPoints())
end
end
hook.Add("PlayerDeath", "pointsave_death", savePoints)
hook.Add("PlayerDisconnected", "pointsave_disconnect", savePoints)
hook.Add("PostEndRound", "pointsave_postendround", function(winner)
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
for k, ply in pairs(player.GetAll()) do
savePoints(ply)
end
end)
timer.Create("pointsave_pointsavetimer", settings.PointsaveInterval, 0, function()
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
for k, ply in pairs(player.GetAll()) do
savePoints(ply)
end
end)
local function loadPoints(ply)
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
timer.Simple(0, function()
if IsValid(ply) and ply:Team() == TEAM_HUMAN then
local savedPoints = ply:GetSavedPoints()
ply:SetPoints(savedPoints)
ply.PointSaving_StartPoints = savedPoints
end
end)
end
hook.Add("PlayerSpawn", "pointsave_spawn", loadPoints)
-- #### Player meta #### TODO: Put in an extra file
local meta = FindMetaTable("Player") -- Get the meta table of player
-- Shared methods
function meta:GetSavedPoints()
if SERVER then
local points = self:GetPData("zs_savedpoints", settings.StartingSavedPoints)
self:SetNWInt("zs_savedpoints", points)
--print("### PS Loaded:", points, "for", tostring(self))
return points
end
return self:GetNWInt("zs_savedpoints", settings.StartingSavedPoints)
end
if not SERVER then return end
-- Server side methods
function meta:SetSavedPoints(amount)
if amount < settings.MinimumPoints then amount = settings.MinimumPoints end
amount = math.Round(amount)
--print("### PS Saved: ", amount, "for", tostring(self))
self:SetNWInt("zs_savedpoints", amount)
self:SetPData("zs_savedpoints", amount)
end
function meta:AddSavedPoints(amount)
local points = self:GetSavedPoints()
self:SetSavedPoints(points + amount)
end
function meta:TakeSavedPoints(amount)
self:AddSavedPoints(-amount)
end