----------------------------
-- CakeScript (March 29, 2008)
-- by LuaBanana
--
-- Alpha Version
-- util.lua
----------------------------

include("player_util.lua");

function CAKE.NilFix(val, default)

	if(val == nil) then
	
		return default;
	
	else
	
		return val;
		
	end
	
end

-- CAKE.SearchForPlayer
-- If more than one player is found, it will give the string value "err"
-- If no player is found, it will return nil.
-- Partial names and STEAMIDs are acceptable.

function CAKE.SearchForPlayer(infotype, data)

	local ply = nil;
	local count = 0;
	
Msg("Searching for player: " .. data .. "\n");
	
	if(infotype == 1) then -- Name
	
		for k, v in pairs(player.GetAll()) do
		
			if(string.match(v:Nick(), data) != nil) then
			
				ply = v;
				count = count + 1;
			
			-- in case IC name == OOC name
			elseif(string.match(v:Name(), data) != nil) then
			
				ply = v;
				count = count + 1;
				
			end
			
		end
		
	elseif(infotype == 2) then -- SteamID
	
		for k, v in pairs(player.GetAll()) do
		
			if(string.match(v:SteamID(), data) != nil) then
			
				ply = v;
				count = count + 1;
				
			end
			
		end
		
	end

	if(count > 1) then

		return "err"; -- Multiple players found
		
	end
	
	return ply;
	
end

function CAKE.InitTime() -- Load the time from a text file or default value, this occurs on gamemode initialization.

	local clumpedtime = "1 1 2012 1"
	
	if(file.Exists("CakeScript/time.txt")) then
	
		clumpedtime = file.Read("CakeScript/time.txt")
		
	else
	
		file.Write("CakeScript/time.txt", "1 1 2012 1")
		
	end
	
	local unclumped = string.Explode(" ", clumpedtime)
	timeday = tonumber(unclumped[1])
	timemonth = tonumber(unclumped[2])
	timeyear = tonumber(unclumped[3])
	timeminutes = tonumber(unclumped[4])
	
	SetGlobalString("time", "Loading..")
	
end

function CAKE.SaveTime() -- Save the time to a text file, this occurs every 120 seconds.

	local clumpedtime = timeday .. " " .. timemonth .. " " .. timeyear .. " " .. timeminutes
	file.Write("CakeScript/time.txt", clumpedtime)
	
end

function CAKE.SendTime() -- Send the string containing the date & time to clients, this occurs every 1 second.

	local nHours = string.format("%02.f", math.floor(timeminutes / 60));
	local nMins = string.format("%02.f", math.floor(timeminutes - (nHours*60)));
	
	if(tonumber(nHours) > 12) then 
	
		nHours = nHours - 12
		timez = "PM";
		
	else
	
		timez = "AM";
		
	end
	
	if(tonumber(nHours) == 0) then
	
		nHours = 12
		
	end
	
	SetGlobalString("time", timemonth .. "." .. timeday .. "." .. timeyear .. " - " .. nHours .. ":" .. nMins .. timez)
	
end

function CAKE.CreateTimers() -- Create the timers on gamemode initialization.

	-- moved to an internal of the load props function call, so we don't accidentally overwrite before we've loaded
	--timer.Create("propsave", 20, 0, CAKE.SaveProps);
	timer.Create("timesave", 120, 0, CAKE.SaveTime);
	timer.Create("sendtime", 10, 0, CAKE.SendTime);
	
	timer.Create("OOCdynamic", 30, 0, CAKE.CalcNewDelay );
	
	/*
	timer.Create("givemoney", 3600, 0, function()
		for k, v in pairs(player.GetAll()) do
			if(Teams[v:Team()] != nil) then
				v:SetField("money", tonumber(v:GetField("money")) + Teams[v:Team()]["salary"]);
				CAKE.Notify(v, "Paycheck! " .. Teams[v:Team()]["salary"] .. " credits earned.");
			end
		end 
	end)
	*/
	timer.Create("regen", 1, 0, function()
		for k, v in pairs(player.GetAll()) do
			v:SetNWFloat("conscious", math.Clamp(v:GetNWFloat("conscious") + 2, 0, 100))
		end
	end)
	
	-- New regens regen
	timer.Create("hpRegen", 1, 0, function()
	
		for k, v in pairs( player.GetAll() ) do
	
			-- might cause bugs if not checking this, there for safety
			if( v:GetNWInt("charactercreate") == 0 ) then
		
				-- make sure they don't regen while running either.
				if( v:KeyDown( IN_SPEED ) ) then
					v:SetNWInt("lastjump", CurTime() );
				end
		
				-- Sprint regen
				if( CurTime() - v:GetNWInt("lastjump") > 1 ) then
	
					if( v:GetNWFloat("sprint") < 100 ) then
		
						v:SetNWFloat("sprint", math.Clamp( v:GetNWFloat("sprint") + 10, 0, 100 ) );
		
					end
	
				end
	
				-- Health regen
				if( CurTime() - v:GetNWInt("lasthurt") > 60 ) then
	
					if( v:Health() < 5 and v:GetNWInt("healthRegen") == 0 ) then
		
						v:SetHealth( v:Health()+ 10 );
		
					end
	
				end
		
			end
	
		end
	
	end)

end

function CAKE.CombineCCArgs(args)

	local str = ""
	
	for k, v in pairs(args) do
	
		str = str .. v
		
		if(v != table.getn(args) - 1) then
		
			str = str .. " ";
			
		end
		
	end
	
	-- Get rid of that last space that gets thrown in there for fuck knows why
	str = string.sub(str, 1, string.len(str) - 1);
	return str
	
end

function CAKE.AddModels(mdls)
	local rf = RecipientFilter()
	rf:AddAllPlayers()
	
	if(type(mdls) == "table") then
	
		for k, v in pairs(mdls) do
		
			table.insert(models, v)
			
		end
		
	else
	
		table.insert(models, mdls)
		
	end
	
end

function CAKE.DayLog(text)
	
	local d = os.date("!*t");
	local logfile = d.month .. "-" .. d.day .. "-" .. d.year .. ".txt";
	local path = "CakeScript/logs/" .. logfile;

	local log = file.Read(path) or "";

	local time = "";

	if(d.hour < 10) then
		time = time .. "0" .. d.hour;
	elseif(d.hour >= 10) then
		time = time .. d.hour;
	end

	time = time .. ":";

	if(d.min < 10) then
		time = time .. "0" .. d.min;
	elseif(d.min >= 10) then
		time = time .. d.min;
	end
	
	time = time .. ":";

	if(d.sec < 10) then
		time = time .. "0" .. d.sec;
	elseif(d.sec >= 10) then
		time = time .. d.sec;
	end

	log = log .. "\n[" .. time .. "] " .. text;
	
	file.Write(path, log);

end

function CAKE.AdminLog(text)
	
	local d = os.date("!*t");
	local logfile = d.month .. "-" .. d.day .. "-" .. d.year .. ".txt";
	local path = "CakeScript/logs/admin/" .. logfile;

	local log = file.Read(path) or "";

	local time = "";

	if(d.hour < 10) then
		time = time .. "0" .. d.hour;
	elseif(d.hour >= 10) then
		time = time .. d.hour;
	end

	time = time .. ":";

	if(d.min < 10) then
		time = time .. "0" .. d.min;
	elseif(d.min >= 10) then
		time = time .. d.min;
	end
	
	time = time .. ":";

	if(d.sec < 10) then
		time = time .. "0" .. d.sec;
	elseif(d.sec >= 10) then
		time = time .. d.sec;
	end

	log = log .. "\n[" .. time .. "] " .. text;
	
	file.Write(path, log);

end

function CAKE.FileLog(file, text)

	local path = "CakeScript/logs/" .. file .. ".txt";

	if(file.Exists(path)) then
		local log = file.Read(path);
	else
		local log = "";
	end
	
	log = log .. "\n" .. text;

	file.Write(path, log);

end

local BannedModels =
{
	
	"models\props_c17\oildrum001_explosive.mdl",
	"models\props_junk\gascan001a.mdl",
	"models\props_junk\propanecanister001a.mdl",
	"models\props_junk\propane_tank001a.mdl",
	"models\extras\info_speech.mdl",
	"models\props_explosive\explosive_butane_can02.mdl",
	"models\props_explosive\explosive_butane_can.mdl"

} -- List of banned props. exact path is required. 

function IsBannedModel( mdl )

	local mdl = string.gsub( mdl, "/", "\\" );

	for k, v in pairs( BannedModels ) do

		if( v == string.gsub( mdl, "\\", "" ) ) then
			return true;
		end
	
	end
	
	return false;

end -- Function for determining if a prop is banned.

--[[function CAKE.IncludeResourcesInFolder( dir )

	local files = file.FindInLua( "cakescript/content/" .. dir .. "*" );

	for k, v in pairs( files ) do

		if( string.find( v, ".vmt" ) or string.find( v, ".vtf" ) or string.find( v, ".mdl" ) ) then
		
			resource.AddFile( dir .. v );
		
		end
	
	end

end]]--  --Utility command to aid in adding files to resource lists