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-15 13:04:12 +00:00
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 "
}
}
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
if ply : Team ( ) == TEAM_HUMAN then
2018-07-15 14:34:14 +00:00
--print("### PS Saved: "..ply:GetPoints())
2018-07-15 13:04:12 +00:00
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:12:55 +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-16 13:04:18 +00:00
-- TODO: Save points every minute or so
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-15 14:34:14 +00:00
--print("### Loaded: "..ply:GetSavedPoints())
2018-07-15 13:04:12 +00:00
ply : SetPoints ( ply : GetSavedPoints ( ) )
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 )
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-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