#include <a_samp>
#include <mxINI>

new bool:SpawnedPlayer[MAX_PLAYERS];
new SpawnTimer[MAX_PLAYERS];
new bool:KickSpawn = false;//false - Позиция после КИКА не сохраняется / true - Позиция после КИКА сохраняется
new bool:ExitSpawn = true;//false - Позиция после ВЫХОДА не сохраняется / true - Позиция после ВЫХОДА сохраняется
new bool:NoLinkSpawn = true;//false - Позиция после ПОТЕРЕ СВЯЗИ не сохраняется / true - Позиция после ПОТЕРИ СВЯЗИ сохраняется

public OnPlayerDisconnect(playerid, reason)
{
	if(SpawnedPlayer[playerid]) DisconnectPlayer(playerid,reason);
	KillTimer(SpawnTimer[playerid]);
	return 1;
}

public OnPlayerSpawn(playerid)
{
	if(!SpawnedPlayer[playerid]) SpawnTimer[playerid] = SetTimerEx("ConnectPlayer",500,0,"i",playerid);
	return 1;
}

stock DisconnectPlayer(playerid,reason)
{
	new
		Name[24],
		File,
		string[64],
		Float:X,
		Float:Y,
		Float:Z,
		Float:A,
		GunID,
		Ammo,
		Float:Health,
		Float:Armour;
	GetPlayerPos(playerid,X,Y,Z);
	GetPlayerFacingAngle(playerid,A);
	GetPlayerHealth(playerid,Health);
	GetPlayerArmour(playerid,Armour);
	GetPlayerName(playerid,Name,sizeof(Name));
	format(string,sizeof(string),"UsersInfo/%s.ini",Name);
	if(fexist(string)) File = ini_openFile(string);
	else File = ini_createFile(string);
	ini_setInteger(File,"Exit_Reason",reason);
	ini_setFloat(File,"Pos_X",X);
	ini_setFloat(File,"Pos_Y",Y);
	ini_setFloat(File,"Pos_Z",Z);
	ini_setFloat(File,"Pos_A",A);
	ini_setFloat(File,"Health",Health);
	ini_setFloat(File,"Armour",Armour);
	ini_setInteger(File,"Interior",GetPlayerInterior(playerid));
	ini_setInteger(File,"World",GetPlayerVirtualWorld(playerid));
	for(new i = 0;i < 13;i ++)
	{
	    GetPlayerWeaponData(playerid,i,GunID,Ammo);
	    format(string,sizeof(string),"GunID_%d",i);
		ini_setInteger(File,string,GunID);
		format(string,sizeof(string),"Ammo_%d",i);
		ini_setInteger(File,string,Ammo);
	}
	ini_closeFile(File);
	SpawnedPlayer[playerid] = false;
	return true;
}

forward ConnectPlayer(playerid);
public ConnectPlayer(playerid)
{
	new
		Name[24],
		File,
		string[64],
		Float:X,
		Float:Y,
		Float:Z,
		Float:A,
		GunID,
		Ammo,
		Float:Health,
		Float:Armour,
		Interior,
		World,
		reason;
	GetPlayerName(playerid,Name,sizeof(Name));
	format(string,sizeof(string),"UsersInfo/%s.ini",Name);
	if(fexist(string))
	{
		File = ini_openFile(string);
		ini_getInteger(File,"Exit_Reason",reason);
		if((reason == 0 && NoLinkSpawn) || (reason == 1 && ExitSpawn) || (reason == 2 && KickSpawn))
		{
			ini_getFloat(File,"Pos_X",X);
			ini_getFloat(File,"Pos_Y",Y);
			ini_getFloat(File,"Pos_Z",Z);
			ini_getFloat(File,"Pos_A",A);
			ini_getFloat(File,"Health",Health);
			ini_getFloat(File,"Armour",Armour);
			ini_getInteger(File,"Interior",Interior);
			ini_getInteger(File,"World",World);
			for(new i = 0;i < 13;i ++)
			{
			    format(string,sizeof(string),"GunID_%d",i);
				ini_getInteger(File,string,GunID);
				format(string,sizeof(string),"Ammo_%d",i);
				ini_getInteger(File,string,Ammo);
				GivePlayerWeapon(playerid,GunID,Ammo);
			}
			ini_closeFile(File);
			SetPlayerHealth(playerid,Health);
			SetPlayerArmour(playerid,Armour);
			SetPlayerInterior(playerid,Interior);
			SetPlayerVirtualWorld(playerid,World);
			SetPlayerPos(playerid,X,Y,Z);
			SetPlayerFacingAngle(playerid,A);
		}
	}
	else
	{
	    File = ini_createFile(string);
	    ini_setFloat(File,"Pos_X",0.0);
		ini_setFloat(File,"Pos_Y",0.0);
		ini_setFloat(File,"Pos_Z",0.0);
		ini_setFloat(File,"Pos_A",0.0);
		ini_setFloat(File,"Health",0);
		ini_setFloat(File,"Armour",0);
		ini_setInteger(File,"Interior",0);
		ini_setInteger(File,"World",0);
		for(new i = 0;i < 13;i ++)
		{
		    format(string,sizeof(string),"GunID_%d",i);
			ini_setInteger(File,string,0);
			format(string,sizeof(string),"Ammo_%d",i);
			ini_setInteger(File,string,0);
		}
		ini_closeFile(File);
	}
	SpawnedPlayer[playerid] = true;
	return true;
}
