/*
	[EX] PostNuke Wasteland ItemManagement
	@extending <lib_item.inc>	
	Date: 11.12.2011
	Author: NeoPhoenix
*/

#if defined iman_EX_included
	#endinput
#else
	#define iman_EX_included
#endif
#define iman:: iman_

#include <a_samp>
#define MAX_IMAN_POS_ITEMS  (5)//Anzahl möglicher Items pro Spawn
#define MAX_IMAN_SPAWNS  	(3)//Anzahl möglicher Spawnpunkte
#define MAX_SPAWNS			(20)//Anzahl möglicher Spawns
#if !defined AVERAGE_TAKE_DEST
#define AVERAGE_TAKE_DEST	(5.0)//Vordefinierte /nehmen-Entfernung
#endif
#define IMAN_STATIC_POS		(0.0)
enum (+=1)
{
	SPAWN_TYPE_IMAN = -1,
	SPAWN_TYPE_NONE = 0,
	SPAWN_TYPE_NORMAL,
	SPAWN_TYPE_THROW_AWAY
}
#define SPAWN_DELETE_TIME	(3*60)//3 Minuten

enum iman_inf
{
	iman_ActiveSpawns,
	iman_Init,
}
new iman_Info[iman_inf];

enum iman_pick
{
	bool:iman_Created,
	Float:iman_Pos[3],
	iman_Item,
	iman_Anzahl,
	iman_Timeleft,
	iman_Spawntype,
	iman_PickupID,
	Text3D:iman_Label,
}
new iman_Pickup[MAX_SPAWNS][iman_pick];

enum iman_spawn
{
	Float:iman_Wahrscheinlichkeit,
	iman_Spawntime,//Sekunden
	iman_Deletetime,//Sekunden
	Float:iman_Pos[3],
	Float:iman_Abw[3],
	iman_PosItems[MAX_IMAN_POS_ITEMS],
	iman_PosItemAnzahl[MAX_IMAN_POS_ITEMS],
}
new iman_Spawn[MAX_IMAN_SPAWNS][iman_spawn] = {
	{100.0, 60, 	120,	{-206.4428,1116.3135,19.7422},		{0.0,0.0,0.0},		{2},				{2}			},
	{75.0, 	120,	240,	{-206.4995,1121.8661,19.7422},		{0.0,0.0,0.0},		{3},				{2}			},
	{500.0, 180, 	360,	{-193.2149,1087.8866,19.7861},		{5.0,5.0,0.0},		{2,3,4},			{2,2,4}		}
};

iman::OnLibInit()
{
	new number = 0,
		tries = 0;
		
	for(new i; i<MAX_SPAWNS; i++)
	{
		iman_Pickup[i][iman_PickupID] = -1;
	}
	
	for(new i; i<MAX_IMAN_SPAWNS; i++)
	{
	    tries++;
		if(iman::InitNativeSpawn(i) >= 0) number++;
		else
		{//Konnte nicht gespawnt werden
			if(iman_Pickup[i][iman_Created] == true)
			{
				printf("iman::OnLibInit critical error. IMAN ID %d TYPE %d already created",i,iman_Pickup[i][iman_Spawntype]);
				continue;
			}
			iman_Pickup[i][iman_Timeleft] 	= iman_Spawn[i][iman_Spawntime];
			iman_Pickup[i][iman_Spawntype]	= SPAWN_TYPE_IMAN;
		}
	}
	printf("iman::OnLib Init %d/%d = %0.2f%%",number,tries,float(number)/float(tries)*100.0);
	iman_Info[iman_Init]++;
	return number;
}

iman::CreateSpawn(item_id,item_anzahl,iman_type=SPAWN_TYPE_IMAN,Float:__posX,Float:__posY,Float:__posZ,destroy_time,optional_id=-1)
{
	if(item_id <= 0 || item_id >= MAX_ITEMS || item_anzahl <= 0) return -1;
	new iman_id = -1;
	if(iman_type == SPAWN_TYPE_IMAN) iman_id = optional_id;
	else
	{
		for(new iid=MAX_IMAN_SPAWNS; iid<MAX_SPAWNS; iid++)
		{
			if(iman_Pickup[iid][iman_Created] == false)
			{
				iman_id = iid;
				break;
			}
		}
	}
	if(iman_Pickup[iman_id][iman_Created] == true) 	return -1;	
	if(iman_id < 0 || iman_id >= MAX_SPAWNS) 		return -1;
	
	if(iman_Pickup[iman_id][iman_Created] == true) Delete3DTextLabel(iman_Pickup[iman_id][iman_Label]);
	iman_Pickup[iman_id][iman_Pos][0]		= __posX;
	iman_Pickup[iman_id][iman_Pos][1]		= __posY;
	iman_Pickup[iman_id][iman_Pos][2]		= __posZ;	
	iman_Pickup[iman_id][iman_Item]			= item_id;
	iman_Pickup[iman_id][iman_Anzahl]       = item_anzahl>=1?item_anzahl:1;
	iman_Pickup[iman_id][iman_Timeleft] 	= destroy_time;
	iman_Pickup[iman_id][iman_Spawntype]	= iman_type;
	iman_Pickup[iman_id][iman_PickupID]		= CreatePickup(item_ItemInfo[item_id][iObjectID],1,__posX,__posY,__posZ,0);
	iman_Pickup[iman_id][iman_Created] 	 	= true;
	iman_Info[iman_ActiveSpawns]++;
	
	format(string,sizeof(string),"%dx %s\n/nehmen",item_anzahl,item_ItemInfo[item_id][iName]);
	iman_Pickup[iman_id][iman_Label] = Create3DTextLabel(string,GRUEN,iman_Pickup[iman_id][iman_Pos][0],iman_Pickup[iman_id][iman_Pos][1],iman_Pickup[iman_id][iman_Pos][2],20.0,0,0);
	return iman_id;
}

iman::DeleteSpawn(iman_id)
{
    if(iman_id < 0 || iman_id >= MAX_SPAWNS) 		return 0;
	if(iman_Pickup[iman_id][iman_Created] == false) return 0;
	Delete3DTextLabel(iman_Pickup[iman_id][iman_Label]);
	DestroyPickup(iman_Pickup[iman_id][iman_PickupID]);
	iman_Info[iman_ActiveSpawns]--;
	iman_Pickup[iman_id][iman_Created]  =  false;
	iman_Pickup[iman_id][iman_Item]		=  0;
	iman_Pickup[iman_id][iman_Anzahl]	=  0;
	iman_Pickup[iman_id][iman_PickupID] = -1;
	if(iman_Pickup[iman_id][iman_Spawntype] == SPAWN_TYPE_IMAN)
	{
		iman_Pickup[iman_id][iman_Spawntype]	= SPAWN_TYPE_IMAN;
		iman_Pickup[iman_id][iman_Timeleft] 	= iman_Spawn[iman_id][iman_Spawntime];
	}
	else
	{
		iman_Pickup[iman_id][iman_Spawntype]	= SPAWN_TYPE_NONE;
		iman_Pickup[iman_id][iman_Timeleft]		= 0;
	}
	return 1;
}

iman::Cronjob(_time=60)
{
	for(new i; i<MAX_SPAWNS; i++)
	{
		if(iman_Pickup[i][iman_Spawntype] == SPAWN_TYPE_IMAN)
		{//Nativer Spawn
			if(iman_Pickup[i][iman_Created] == true)
			{
				if((iman_Pickup[i][iman_Timeleft] -= _time) <= 0) iman::DeleteSpawn(i);
				continue;
			}
			if((iman_Pickup[i][iman_Timeleft] -= _time) <= 0)
			{
				iman::InitNativeSpawn(i);
				continue;
			}
		}
		else
		{//Normaler Spawn
			if(iman_Pickup[i][iman_Created] == true)
			{
				if((iman_Pickup[i][iman_Timeleft] -= _time) <= 0) iman::DeleteSpawn(i);
				continue;
			}
		}
	}
	return 1;
}

iman::InitNativeSpawn(iman_id)
{
	if(iman_id < 0 || iman_id >= MAX_IMAN_SPAWNS) 	return -1;
	if(iman_Pickup[iman_id][iman_Created] == true) 	return -1;
	new Float:wahrscheinlichkeit = ((iman_Spawn[iman_id][iman_Wahrscheinlichkeit]/100.0)*__SERVER[OPT_DROPRATE]);
	new Float:rand = frandom(100.0);
	if(rand > wahrscheinlichkeit)
	{
		iman_Pickup[iman_id][iman_Timeleft] = iman_Spawn[iman_id][iman_Spawntime];
		return -1;
	}
	
	new item_id,
		item_anzahl;
	for(new o; o<MAX_IMAN_POS_ITEMS; o++)
	{
		if(iman_Spawn[iman_id][iman_PosItems][o] <= 0) break;
		item_id = o;
	}
	item_id = item_id<=0?0:random(item_id);
	if(iman_Spawn[iman_id][iman_PosItems][item_id] >= 1)
	{
		item_anzahl = random(iman_Spawn[iman_id][iman_PosItemAnzahl][item_id]);
		if(item_anzahl <= 0) item_anzahl = 1;
		item_id = iman_Spawn[iman_id][iman_PosItems][item_id];
		if(item_id == 0) return -1;
		
		rand = frandom(iman_Spawn[iman_id][iman_Abw][0],-iman_Spawn[iman_id][iman_Abw][0]);
		new Float:__posX = iman_Spawn[iman_id][iman_Pos][0] + rand;
		rand = frandom(iman_Spawn[iman_id][iman_Abw][1],-iman_Spawn[iman_id][iman_Abw][1]);
		new Float:__posY = iman_Spawn[iman_id][iman_Pos][1] + rand;
		rand = frandom(iman_Spawn[iman_id][iman_Abw][2],-iman_Spawn[iman_id][iman_Abw][2]);
		new Float:__posZ = iman_Spawn[iman_id][iman_Pos][2] + rand;
		
		return iman::CreateSpawn(item_id,item_anzahl,SPAWN_TYPE_IMAN,__posX,__posY,__posZ,iman_Spawn[iman_id][iman_Deletetime],iman_id);
	}
	return -1;
}

iman::UpdateSpawn(iman_id,newtime)
{
	if(iman_id < 0 || iman_id >= MAX_SPAWNS) 		return -1;
	if(iman_Pickup[iman_id][iman_Created] == false) return 0;
	iman_Pickup[iman_id][iman_Timeleft] = newtime;
	format(query,sizeof(query),"%dx %s\n/nehmen",iman_Pickup[iman_id][iman_Anzahl],item_ItemInfo[iman_Pickup[iman_id][iman_Item]][iName]);
	Update3DTextLabelText(iman_Pickup[iman_id][iman_Label],GRUEN,query);
	return 1;
}

iman::GetNearestSpawn(playerid,Float:range=AVERAGE_TAKE_DEST)
{
	for(new i; i<MAX_SPAWNS; i++)
	{
	    if(iman_Pickup[i][iman_Created] == false) continue;
	    if(IsPlayerInRangeOfPoint(playerid,range,iman_Pickup[i][iman_Pos][0],iman_Pickup[i][iman_Pos][1],iman_Pickup[i][iman_Pos][2])) return i;
	}
	return -1;
}