2018-07-15 10:07:37 +00:00
2018-07-15 10:15:12 +00:00
if engine.ActiveGamemode ( ) ~= " zombiesurvival " then return end
2018-07-15 10:07:37 +00:00
2018-07-15 13:04:12 +00:00
local settings = {
StartingSavedPoints = 100 ,
2018-07-16 14:58:18 +00:00
MinimumPoints = 0 ,
2018-07-20 13:49:38 +00:00
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)
2018-07-15 13:04:12 +00:00
" zs_obj_gauntlet " ,
" zs_gauntlet_reborn "
}
}
2018-07-16 15:02:46 +00:00
function PointSaving_shouldIgnoreMap ( mapName )
2018-07-15 13:04:12 +00:00
for k , v in ipairs ( settings.IgnoredMaps ) do
2018-07-15 14:34:14 +00:00
if string.match ( string.lower ( mapName ) , " ^ " .. v ) then return true end
2018-07-15 10:07:37 +00:00
end
2018-07-15 13:04:12 +00:00
end
2018-07-15 10:07:37 +00:00
2018-07-15 13:04:12 +00:00
local function savePoints ( ply )
2018-07-16 15:02:46 +00:00
if PointSaving_shouldIgnoreMap ( game.GetMap ( ) ) then return end
2018-07-15 13:04:12 +00:00
2018-07-24 16:15:35 +00:00
if not ply.PointSaving_StartPoints then return end
2018-07-20 13:49:38 +00:00
2018-07-15 13:04:12 +00:00
if ply : Team ( ) == TEAM_HUMAN then
ply : SetSavedPoints ( ply : GetPoints ( ) )
2018-07-15 10:07:37 +00:00
end
2018-07-15 13:04:12 +00:00
end
2018-07-16 15:48:34 +00:00
hook.Add ( " PlayerDeath " , " pointsave_death " , savePoints )
2018-07-15 13:04:12 +00:00
hook.Add ( " PlayerDisconnected " , " pointsave_disconnect " , savePoints )
2018-07-15 14:34:14 +00:00
hook.Add ( " PostEndRound " , " pointsave_postendround " , function ( winner )
2018-07-16 15:02:46 +00:00
if PointSaving_shouldIgnoreMap ( game.GetMap ( ) ) then return end
2018-07-15 14:34:14 +00:00
for k , ply in pairs ( player.GetAll ( ) ) do
savePoints ( ply )
end
end )
2018-07-20 13:49:38 +00:00
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 )
2018-07-16 13:04:18 +00:00
2018-07-15 13:04:12 +00:00
local function loadPoints ( ply )
2018-07-16 15:02:46 +00:00
if PointSaving_shouldIgnoreMap ( game.GetMap ( ) ) then return end
2018-07-15 13:04:12 +00:00
timer.Simple ( 0 , function ( )
if IsValid ( ply ) and ply : Team ( ) == TEAM_HUMAN then
2018-07-24 16:15:35 +00:00
local savedPoints = ply : GetSavedPoints ( )
ply : SetPoints ( savedPoints )
ply.PointSaving_StartPoints = savedPoints
2018-07-15 10:07:37 +00:00
end
2018-07-15 13:04:12 +00:00
end )
end
hook.Add ( " PlayerSpawn " , " pointsave_spawn " , loadPoints )
2018-07-15 10:07:37 +00:00
2018-07-15 14:34:14 +00:00
-- #### Player meta #### TODO: Put in an extra file
2018-07-15 10:07:37 +00:00
2018-07-15 13:04:12 +00:00
local meta = FindMetaTable ( " Player " ) -- Get the meta table of player
2018-07-15 10:07:37 +00:00
2018-07-15 13:04:12 +00:00
-- Shared methods
2018-07-15 10:07:37 +00:00
2018-07-15 13:04:12 +00:00
function meta : GetSavedPoints ( )
if SERVER then
local points = self : GetPData ( " zs_savedpoints " , settings.StartingSavedPoints )
self : SetNWInt ( " zs_savedpoints " , points )
2018-08-02 14:03:54 +00:00
--print("### PS Loaded:", points, "for", tostring(self))
2018-07-15 13:04:12 +00:00
return points
2018-07-15 10:07:37 +00:00
end
2018-07-15 13:04:12 +00:00
return self : GetNWInt ( " zs_savedpoints " , settings.StartingSavedPoints )
2018-07-15 10:07:37 +00:00
end
2018-07-15 13:04:12 +00:00
if not SERVER then return end
-- Server side methods
function meta : SetSavedPoints ( amount )
2018-07-16 14:58:18 +00:00
if amount < settings.MinimumPoints then amount = settings.MinimumPoints end
2018-07-16 15:48:34 +00:00
amount = math.Round ( amount )
2018-08-02 14:03:54 +00:00
--print("### PS Saved: ", amount, "for", tostring(self))
2018-07-15 13:04:12 +00:00
self : SetNWInt ( " zs_savedpoints " , amount )
self : SetPData ( " zs_savedpoints " , amount )
2018-07-15 10:07:37 +00:00
end
2018-07-15 13:04:12 +00:00
function meta : AddSavedPoints ( amount )
local points = self : GetSavedPoints ( )
self : SetSavedPoints ( points + amount )
2018-07-15 10:07:37 +00:00
end
2018-07-15 13:04:12 +00:00
function meta : TakeSavedPoints ( amount )
self : AddSavedPoints ( - amount )
2018-07-15 10:07:37 +00:00
end