/*
	PostNuke Wasteland NPC Synchronisation
	Author:	NeoPhoenix
	Date:	02.02.2012

	Functions
	native npc::LoadPlayer(playerid);
	native npc::SaveSync();
	native npc::LoadNPC();
	native npc::IdByName(npc_name[]);
	native npc::Connect(step_id);
*/
#define npc:: npc_

enum npc_list (+=1)
{
	NPC_NONE = 0,
	NPC_JOHN,
	NPC_MATTHEW,
	NPC_DIMITRY,
	NPC_WEZ,
	NPC_HUMUNGUS,
	NPC_JOE,
	NPC_JASON,
	NPC_RICHARD,
	NPC_INDIAN,
	NPC_TRAVIS,
	NPC_NICK,
	NPC_WILFRIED,
	NPC_MIKE,
	NPC_HENRY,
	NPC_CHUCK,
	NPC_MATHEO,
	NPC_JAMES,
}
enum npc_data
{
	npc_name[MAX_PLAYER_NAME],
	Float:npc_pos[4],
	npc_additional[2],//waffe
	npc_vw,
	npc_int,
	npc_skin
}
new NPCData[npc_list][npc_data] = {
	{"None",		{0.0,0.0,0.0,0.0},							{0},	0,		0,	0	},
	{"John",		{45.6855,-2119.5842,22.9661,0.0},			{0},	0,		2,	299	},
	{"Matthew",		{-2186.5549,605.17846,35.1640,0.0},			{0},	0,		0,	287	},
	{"Dimitry",		{-172.3654,1165.9482,87.5835,180.0171},		{0},	1234,	2,	112	},
	{"Wez",			{-223.1800,1406.0132,27.7734,91.1914},		{0},	1234,	18,	181	},
	{"Humungus",	{-218.4289,1406.5778,27.7734,127.7068},		{0},	1234,	18,	146	},
	{"Joe",			{373.7224,-117.2789,1001.4995,175.8300},	{0},	1234,	5,	209	},
	{"Jason",		{59.5830,1214.7313,18.8433,261.5},			{0},	0,		0,	133	},
	{"Richard",		{-210.8343,1116.7026,123.7506,357.9782},	{0},	1234,	2,	17 	},
	{"Kawacatoose",	{-838.3779,2762.0925,46.0000,226.9203},		{0},	0,		0,	128	},
	{"Travis",		{-677.3391,940.6191,12.9519,43.9207},		{0},	0,		0,	162	},
	{"Nick",		{2865.9231,-259.4516,52.7608,108.963},		{0},	1234,	2,	70 	},
	{"Wilfried",	{-215.9452,1113.7700,120.2511,358.6638},	{0},	1234,	2,	168	},
	{"Mike",		{-149.9183,1903.6771,43.4313,310.8037},		{0},	0,		0,	154	},
	{"Henry",		{-841.3748,436.5292,7.1421,248.3289},		{8},	0,		0,	0  	},
	{"Chuck",		{-1464.4015,2611.0276,40.6781,133.2065},	{0},	1234,	2,	81	},
	{"Matheo",		{-234.2295,2620.8293,62.9129,191.6629},		{0},	0,		0,	37	},
	{"James",		{3174.3943,2307.4609,72.0516,268.6970},		{0},	1234,	2,	126	}
};

enum pnpc_info
{
	cash,
	beliebtheit
};
enum enpc_info 
{
	laune,
	cash
};

stock npc::IdByName(insert_name[])
{
	if(!insert_name[0]) return npc_list:NPC_NONE;
	for(new npc; npc_list:npc<npc_list; npc++) if(!strcmp(insert_name,NPCData[npc_list:npc][npc_name])) return npc_list:npc;
	return npc_list:NPC_NONE;
}

new NpcOnline[npc_list],
	NpcID[npc_list],
	NPCInfo[npc_list][enpc_info],
	PlayerNPC[MAX_PLAYERS][npc_list][pnpc_info];

npc::LoadNPC()
{
	mysql_query("SELECT * FROM pnwl_server_npc");
	mysql_store_result();
	if(mysql_num_rows() >= 1)
	{
		while(mysql_retrieve_row())
		{
			mysql_fetch_field_row(query,"npcid");
			new npc_id = strval(query);
			
			if(npc_id < 0 || npc_list:npc_id >= npc_list) continue;
			mysql_fetch_field_row(query,"laune");
			NPCInfo[npc_list:npc_id][laune]	= strval(query);
			
			mysql_fetch_field_row(query,"cash");
			NPCInfo[npc_list:npc_id][cash]	= strval(query);			
		}
	}
	mysql_free_result();
	return 1;
}

npc::SaveSync()
{
	for(new npc; npc_list:npc<npc_list; npc++)
	{
		format(query,sizeof(query),"UPDATE pnwl_server_npc SET laune='%d',cash='%d' WHERE npcid='%d'",NPCInfo[npc_list:npc][laune],NPCInfo[npc_list:npc][cash],npc_list:npc);
		mysql_query(query);
	}
	return 1;
}

npc::LoadPlayer(playerid)
{
	format(string, sizeof(string), "SELECT * FROM pnwl_user_npc WHERE userid='%d'",PlayerInfo[playerid][PLAYER_ID]);
	mysql_query(string);
	mysql_store_result();
	if(mysql_num_rows() >= 1)
	{
		mysql_retrieve_row();
		for(new npc; npc_list:npc<npc_list; npc++)
		{
			format(string,sizeof(string),"cash_%d",npc_list:npc);
			mysql_fetch_field_row(string,string); PlayerNPC[playerid][npc_list:npc][cash] 		= strval(string);
			format(string,sizeof(string),"beliebtheit_%d",npc_list:npc);
			mysql_fetch_field_row(string,string); PlayerNPC[playerid][npc_list:npc][beliebtheit]= strval(string);
		}
	}
	return 1;
}

forward npc::Connect(step_id,steps,loop);
public npc::Connect(step_id,steps,loop)
{
	for(new nID=step_id; nID<(step_id+steps); nID++)
	{
		if(npc_list:nID<npc_list)
		{
			if(nID == 0) continue;
			ConnectNPC(NPCData[npc_list:nID][npc_name],"npcidle");
			continue;
		}
		SetTimer("FCNPC_InitZombies",5000,false);
		return 0;
	}
	if(loop) SetTimerEx("npc_Connect",15000,false,"ddd",(step_id+steps),steps,1);
	return 1;
}