/*
	[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
/*
	native iman_OnLibInit();
	native iman_OnSpawnUpdate(iman_id,item_id,item_anzahl,imtimestamp=-1);
	native iman_OnSpawnDelete(iman_id,imtimestamp=-1);
	native iman_OnItemTake(playerid,iman_id);
	native iman_GetNearestSpawn(playerid,Float:range=AVERAGE_TAKE_DEST);
*/

forward iman_OnItemTake(playerid,iman_id);

#include <a_samp>
#define MAX_IMAN_POS_ITEMS  (5)//Anzahl möglicher Items pro Spawn
#define MAX_IMAN_SPAWNS  	(4)//Anzahl möglicher Spawnpunkte
#if !defined AVERAGE_TAKE_DEST
#define AVERAGE_TAKE_DEST	(5.0)//Vordefinierte /nehmen-Entfernung
#endif
#define IMAN_STATIC_POS		(0.0)

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

enum iman_pick
{
	bool:iman_Created,
	Float:iman_Pos[3],
	iman_Item,
	iman_Anzahl,
	iman_Timestamp,
	Text3D:iman_Label,
}
new iman_Pickup[MAX_IMAN_SPAWNS][iman_pick];

enum iman_spawn
{
	Float:iman_Wahrscheinlichkeit,
	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,	{-206.4428,1116.3135,19.7422},		{0.0,0.0,0.0},		{1},				{5}			},
{ 100.0,	{-206.4995,1121.8661,19.7422},		{0.0,0.0,0.0},		{1},				{2}			},
{ 100.0,	{-193.2149,1087.8866,19.7861},		{5.0,5.0,0.0},		{1,2},				{1,2}		},
{ 100.0,	{-193.1415,1108.7759,19.7944},		{5.0,5.0,0.0},		{1,2,3,4,5},		{1,2,3,4,5}	}
};

iman_OnLibInit()
{
	iman_Info[iman_LowestID] = -1;	
	new number = 0,
		tries = 0,
		timestamp = gettime(_,_,_);
	for(new i; i<MAX_IMAN_SPAWNS; i++)
	{
	    tries++;
	    new Float:wahrscheinlichkeit = ((iman_Spawn[i][iman_Wahrscheinlichkeit]/100.0)*__SERVER[OPT_DROPRATE]);
	    new Float:rand = frandom(100.0);
	    if(rand > wahrscheinlichkeit) 
		{
			if(iman_Info[iman_Init] >= 1 && iman_Pickup[i][iman_Created] == true) iman_OnSpawnDelete(i,timestamp);
			continue;
		}
		new item_id,
			item_anzahl;
	    for(new o; o<MAX_IMAN_POS_ITEMS; o++)
	    {
	        if(iman_Spawn[i][iman_PosItems][o] <= 0) break;
	        item_id = o;
	    }
	    item_id = item_id<=0?0:random(item_id);
	    if(iman_Spawn[i][iman_PosItems][item_id] >= 1)
	    {
	        item_anzahl = random(iman_Spawn[i][iman_PosItemAnzahl][item_id]);
	        if(item_anzahl <= 0) item_anzahl = 1;
	        item_id = iman_Spawn[i][iman_PosItems][item_id];
			if(item_id == 0) break;
			number += iman_OnSpawnUpdate(i,item_id,item_anzahl,timestamp);
	    }
	}
	printf("%d/%d = %0.2f%%",number,tries,float(number)/float(tries)*100.0);
	iman_Info[iman_Init]++;
	return number;
}

iman_OnSpawnUpdate(iman_id,item_id,item_anzahl,imtimestamp=-1)
{
	if(iman_id < 0 || iman_id >= MAX_IMAN_SPAWNS) return 0;
	if(iman_Pickup[iman_id][iman_Created] == true) Delete3DTextLabel(iman_Pickup[iman_id][iman_Label]);
	iman_Pickup[iman_id][iman_Created] = false;
	
	if(item_id <= 0) return 0;

	new Float:rand = frandom(iman_Spawn[iman_id][iman_Abw][0],-iman_Spawn[iman_id][iman_Abw][0]);
	iman_Pickup[iman_id][iman_Pos][0] = iman_Spawn[iman_id][iman_Pos][0] + rand;

	rand = frandom(iman_Spawn[iman_id][iman_Abw][1],-iman_Spawn[iman_id][iman_Abw][1]);
	iman_Pickup[iman_id][iman_Pos][1] = iman_Spawn[iman_id][iman_Pos][1] + rand;

	rand = frandom(iman_Spawn[iman_id][iman_Abw][2],-iman_Spawn[iman_id][iman_Abw][2]);
	iman_Pickup[iman_id][iman_Pos][2] = iman_Spawn[iman_id][iman_Pos][2] + rand;
	
	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_Timestamp] 	= imtimestamp<=0?(gettime(_,_,_)>=1?gettime(_,_,_):1):imtimestamp;
	iman_Pickup[iman_id][iman_Created] 	 	= true;
	iman_Info[iman_ActiveSpawns]++;
	if(iman_id > iman_Info[iman_HighestID]) iman_Info[iman_HighestID] 	= iman_id;
	if(iman_id < iman_Info[iman_LowestID] || iman_Info[iman_LowestID] == -1) 	iman_Info[iman_LowestID] 	= iman_id;
	
	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 1;
}

iman_OnSpawnDelete(iman_id,imtimestamp=-1)
{
    if(iman_id < 0 || iman_id >= MAX_IMAN_SPAWNS) return 0;
	if(iman_Pickup[iman_id][iman_Created] == false) return 0;
	new timestamp = gettime(_,_,_);
	
	iman_Pickup[iman_id][iman_Item]			= 0;
	iman_Pickup[iman_id][iman_Anzahl]       = 0;
	iman_Pickup[iman_id][iman_Timestamp] 	= imtimestamp<=0?(timestamp>=1?timestamp:1):imtimestamp;
	iman_Pickup[iman_id][iman_Created] 	 	= false;
	Delete3DTextLabel(iman_Pickup[iman_id][iman_Label]);
	iman_Info[iman_ActiveSpawns]--;
	
	if(iman_id == iman_Info[iman_HighestID])
	{
	    for(new i=iman_Info[iman_HighestID]; i>=0; i--)
	    {
	        iman_Info[iman_HighestID] = i;
	        if(iman_Pickup[i][iman_Created] == true) break;
	    }
	}
	if(iman_id == iman_Info[iman_LowestID])
	{
	    if(iman_Info[iman_ActiveSpawns] >= 1)
	    {
		    for(new i=iman_Info[iman_LowestID]; i<MAX_IMAN_SPAWNS; i++)
		    {
		        iman_Info[iman_LowestID] = i;
		        if(iman_Pickup[i][iman_Created] == true) break;
		    }
		}
		else
		{
		    iman_Info[iman_LowestID] = 0;
		}
	}
	return 1;
}

iman_GetNearestSpawn(playerid,Float:range=AVERAGE_TAKE_DEST)
{
	for(new i=iman_Info[iman_LowestID]; i<=iman_Info[iman_HighestID]; 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;
}