/*
	PostNuke Wasteland Skillsystem
	Author: NeoPhoenix
	Date: 19.08.2012
*/

#if defined included_pnwl_skill
	#endinput
#else
	#define included_pnwl_skill
#endif
#include <a_samp>

#define skill:: skill_
#define MAX_TMP_SKILLS	(5)
#define COUNT_SKILLS	(12)
/*
	Functions:
	native skill::ChangePlayerSkill(playerid,sID,Float:value,bool:_add=true)
	native Float:skill::GetPlayerSkill(playerid,sID)
	native Float:skill::GetPlayerTmpSkill(playerid,sID)
	native Float:skill::GetPlayerJobBonus(playerid,sID)
	
	Callbacks:
	native skill::OnPlayerChangeSkill(playerid,sID,Float:oldvalue,Float:newvalue,bool:_ap=false)
*/

enum (+=1)
{
	//Aktiv Skills
	SkillHandwerk,
	SkillGeschick,
	SkillMedizin,
	SkillWissenschaft,
	//Passiv Skills
	SkillKraft,
	SkillStoffwechsel,
	SkillGlueck,
	SkillStrahlung,
	//Neutrale Skills
	SkillKarma,
	SkillWortgewandt
};
new Float:PlayerSkill[MAX_PLAYERS][COUNT_SKILLS],
	SkillName[][] =
	{
		"Handwerk",
		"Geschick",
		"Medizin",
		"Wissenschaft",
		"Kraft",
		"Stoffwechsel",
		"Glück",
		"Stahlung",
		"Karma",
		"Wortgewandt"
	};


stock skill::ChangePlayerSkill(playerid,sID,Float:value,bool:_add=true,bool:_message=true)
{
	if(sID < 0 || sID >= COUNT_SKILLS) return 0;
	new Float:oldvalue = PlayerSkill[playerid][sID];
	PlayerSkill[playerid][sID] = _add?(PlayerSkill[playerid][sID]+value):(value);	
	if(PlayerSkill[playerid][sID] < 0.0)
	{
		PlayerSkill[playerid][sID] = 0.0;
		if(_message) skill::OnPlayerChangeSkill(playerid,sID,oldvalue,0.0);
		return 1;
	}
	if(PlayerSkill[playerid][sID] > 100.0)	PlayerSkill[playerid][sID] = 100.0;
	if(_message) skill::OnPlayerChangeSkill(playerid,sID,oldvalue,PlayerSkill[playerid][sID]);
	if(sID >= SkillKarma) return 1;//Neutrale Skills
	new Float:skill_gesamt,start_sid,end_sid;
	if(sID >= SkillKraft) start_sid = SkillKraft, end_sid = SkillStrahlung;
	else start_sid = 0, end_sid = SkillWissenschaft;
	for(new i=start_sid; i<=end_sid; i++) skill_gesamt += PlayerSkill[playerid][i];
	if(skill_gesamt > 100.0)
	{
		new Float:skill_remove 			= (skill_gesamt-100.0),
			Float:rest_skill			= (skill_gesamt-PlayerSkill[playerid][sID]),
			Float:remove_per_skill		= (skill_remove/(rest_skill/100.0)),
			Float:skill_multiplikator	= (1.0-(remove_per_skill/100.0));
		for(new i=start_sid; i<=end_sid; i++)
		{
			if(i == sID) continue;
			oldvalue = PlayerSkill[playerid][i];
			PlayerSkill[playerid][i] *= skill_multiplikator;
			if(_message) skill::OnPlayerChangeSkill(playerid,sID,oldvalue,PlayerSkill[playerid][i],true);
		}
	}
	return 1;
}

forward Float:skill::GetPlayerSkill(playerid,sID);
stock Float:skill::GetPlayerSkill(playerid,sID)
{
	new Float:total_skill = PlayerSkill[playerid][sID];
	total_skill += skill::GetPlayerTmpSkill(playerid,sID);
	total_skill += skill::GetPlayerJobBonus(playerid,sID);
	if(PlayerPremium[playerid][DoubleSkillTime]) total_skill *= 1.5;
	return total_skill;
}

forward Float:skill::GetPlayerJobBonus(playerid,sID);
stock Float:skill::GetPlayerJobBonus(playerid,sID)
{
	new Float:job_skill = 0.0;
	switch(PlayerInfo[playerid][Job])
	{
		case 0:
		{
			switch(sID)
			{
				case 0:
				{
					job_skill += 5.0;
				}
			}
		}
	}
	return job_skill;
}

forward Float:skill::GetPlayerTmpSkill(playerid,sID);
stock Float:skill::GetPlayerTmpSkill(playerid,sID)
{
	new Float:tmp_skill = 0.0,
		skill_id		= sID,
		tmp_str[25];
	for(new sSlot; sSlot<MAX_TMP_SKILLS; sSlot++)
	{
		format(tmp_str,25,"tmp_skill_%d_sID",sSlot);
		if(GetPVarInt(playerid,tmp_str) == skill_id)
		{
			format(tmp_str,25,"tmp_skill_%d_bonus",sSlot);
			tmp_skill += GetPVarInt(playerid,tmp_str);
		}
	}
	return tmp_skill;
}

enum (<<=1)
{//Medical Skills
	CanCureDisease = 1,	//1
	CanHeal,			//2
	CanRevive,			//4
}

//Callbacks
stock skill::OnPlayerChangeSkill(playerid,sID,Float:oldvalue,Float:newvalue,bool:_ap=false)
{
	if(oldvalue == newvalue) return 0;
	if(_ap) return 1;//Meldung nicht anzeigen
	if(newvalue > oldvalue)
	{
		format(string,sizeof(string),"SKILL: %s um %0.1f Punkte erhöht. Level %0.1f%%",SkillName[sID],(newvalue-oldvalue),PlayerSkill[playerid][sID]);
		SendClientMessage(playerid,GRUEN,string);
	}
	else
	{
		format(string,sizeof(string),"SKILL: %s um %0.1f Punkte verkümmert. Level %0.1f%%",SkillName[sID],(oldvalue-newvalue),PlayerSkill[playerid][sID]);
		SendClientMessage(playerid,ROT,string);
	}
	return 1;
}