
local function AdminReset(ply, cmd, args)
	if (ply:IsAdmin()) then
		GAMEMODE:ResetGame();
	end
end
concommand.Add("chimerahunt_reset", AdminReset);

local meta = FindMetaTable("Player");


function meta:IsChimera()
	return self:GetNWBool("IsChimera");
end

function meta:RemoveChimera()
	if (ValidEntity(self.Chimera)) then
		self.Chimera:Remove();
	end
end

function meta:SetupChimera()
	
	if (!self:IsChimera()) then
		return;
	end
	
	self:RemoveChimera();
	
	local ch = ents.Create("chimera_model");
	ch:SetPlayer(self);
	ch:SetPos(ch:GetPlayer():GetPos());
	ch:Spawn();
	ch:Activate();
	
	ch:SetPos(self:GetPos());
	ch:SetAngles(self:GetAngles());
	
	ch:SetParent(self);
	
	self.Chimera = ch;
	
	
end


function meta:SetScared(b)
	self:SetNWBool("UCScared", b);
end

function meta:IsScared()
	return self:GetNWBool("UCScared");
end


function meta:SetSprint(int)
	self:SetNWInt("UCSprint", int);
end

function meta:GetSprint()
	return self:GetNWInt("UCSprint");
end

function meta:SetSpeed(spd)
	self:SetWalkSpeed(spd);
	self:SetRunSpeed(spd);
end

function meta:MakeSprintTimer()

	local str = (tostring(self:UniqueID()) .. "UCSprint");
	self:DestroySprintTimer();
	
	timer.Create(str, GAMEMODE.plySprintCooldown, 1, function()
		if (ValidEntity(self)) then
			self:SetSprint(.01);
			self.SprintRegen = true;
			umsg.Start("UCStopSprint", self)
			umsg.End()
		end
	end);
	
end

function meta:DestroySprintTimer()
	local str = (tostring(self:UniqueID()) .. "UCSprint");
	if (timer.IsTimer(str)) then
		timer.Destroy(str);
	end
end

function meta:IsSprintTimer()
	return timer.IsTimer(tostring(self:UniqueID()) .. "UCSprint");
end

function meta:SendRestMsg(b)
	if (self.RestMsg != b) then
		self.RestMsg = b;
		umsg.Start("UCSprintRest", self)
			umsg.Bool(b);
		umsg.End()
	end
end


function meta:LastAlive()
	local tbl = {};
	for k, v in ipairs(player.GetAll()) do
		if (v:Alive() && !v:IsChimera()) then
			table.insert(tbl, v);
		end
	end
	return (#tbl == 1);
end


function meta:SetTeamAuto()
	if (self:IsChimera()) then
		self:SetTeam(GAMEMODE.TEAM_CHIMERA);
	else
		if (self:Alive()) then
			self:SetTeam(GAMEMODE.TEAM_PIGMASKS);
		else
			self:SetTeam(GAMEMODE.TEAM_DEADPIGS);
		end
	end
end


function meta:ExcludeMeInTable()
	local tbl = {};
	for k, v in ipairs(GAMEMODE:AlivePlayers()) do
		if (v != self) then
			table.insert(tbl, v);
		end
	end
	return tbl;
end

function meta:SpecNextPerson()
	
	local cur = self:GetNWEntity("UCSpec");
	local num = self.SpecN;
	
	if (!ValidEntity(player.GetAll()[num])) then //player no longer exists
		num = math.random(1, #self:ExcludeMeInTable()); //choose a new one
		self.SpecN = num;
	end
	
	local ent = self:ExcludeMeInTable()[self.SpecN];
	self:SetNWEntity("UCSpec", ent);
	self:SpectateEntity(ent);
	
end

function meta:AdvanceSpecMode()
	local s = self:GetNWInt("SpecMode");
	if (s == 1) then
		self:SetNWInt("SpecMode", 2);
		self:Spectate(OBS_MODE_ROAMING);
	elseif (s == 2) then
		self:SetNWInt("SpecMode", 1);
		self:Spectate(OBS_MODE_CHASE);
	end
end


function meta:CanPressButton()
	
	local c = GAMEMODE:GetChimera();
	
	if (c == nil || !c:Alive() || self:IsScared()) then
		return false;
	end
	
	local atch = c.Chimera:LookupAttachment("Button");
	atch = c.Chimera:GetAttachment(atch);
	local pos = atch.Pos;
	pos = (pos + (c:GetForward() * -6));
	
	local tr = self:GetEyeTrace();
	if (!ValidEntity(tr.Entity)) then
		return false;
	end
	
	if (((self:GetPos():Distance(pos) <= GAMEMODE.BtnPressDistance && !table.HasValue(c:FindEnts(player.GetAll(), GAMEMODE.BtnPressDis, -1), self)) || self:GetGroundEntity() == c) && tr.Entity == c) then
		return true;
	end
	
	return false;
	
end


function meta:BeAPiggy()

	self.PigSnort = (self.PigSnort || 0);

	if (!self:Alive()) then
		return;
	end
	
	local snort = self.PigSnort;
	
	if (CurTime() >= snort) then
		local t = math.random(unpack(GAMEMODE.SnortTime));
		if (self:IsScared() || self.UCSprinting) then
			t = (t * .125);
		end
		self.PigSnort = (CurTime() + t);
		self:EmitSound("chimerahunt/pigs/snort" .. tostring(math.random(1, 4)) .. ".wav", 75, math.random(90, 105));
	end
	
end


//function credits go to cringerpants http://www.facepunch.com/showpost.php?p=4575979&postcount=21
function meta:FindEnts(tbl, dis, num)
	
	local pos = self:GetPos()
	local forward = (self:GetForward() * num)
	
	local entitiesInFront = {}
	for i, e in ipairs(tbl) do
		//player.GetByID(1):ChatPrint(tostring(forward:Dot((pos - e:GetPos()):GetNormalized())));
		if (e != self and forward:Dot((e:GetPos() - pos):GetNormalized()) <= dis) then
			table.insert(entitiesInFront, e)
		end
	end
	return entitiesInFront;
	
end
