/*
	PostNuke Wasteland EX Container Library
		@->requires 'lib_item'
	Author: NeoPhoenix
	Date: 19.11.2011
	Updates:
		23.22.2011
		 - new callback cont::OnContainerLibInit [called on initializing lib_EX_container]
		 - new function cont_DeleteContainer(containerid,bool:_save=true) returns -1 on wrong containerid or 0 on non existing containers [1 = success). optional param bool:_save [true = mysql saving]		 
		 - mysql saving
		 - bugfixing at mysql saving
		24.11.2011
		 - new name and functions for cont_DeleteContainer (its now cont::DestroyContainer)
*/
#if defined included_pnwl_container
	#endinput
#else
	#define included_pnwl_container
#endif
#define cont:: cont_
/*
	native cont::CreateContainer(Float:new_cont_x,Float:new_cont_y,Float:new_cont_z,locked=0,contbezeichnung[]=" ",bool:_save=true,give_cid=0);
	native cont::DestroyContainer(containerid,bool:_save=true);
	native cont::GetPlayerNextContainer(playerid,Float:container_range=AV_CONT_RANGE);
	native Float:cont::GetPlayerRangeToContainer(playerid,container_id);
	native cont::RemoveContainerItem(containerid,itemid,anzahl=-1,bool:_save=true);
	native cont::GetContainerItemSlot(containerid,itemid);
	native cont::AddContainerItem(containerid,itemid,anzahl,bool:_save=true);
	native cont::ShowPlayerMenu(playerid,containerid,bool:skip_lock=false,bool:_redirect=false);
	native cont::OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
	native cont::PlayerExitContainer(playerid);
	native cont::OnContainerLibInit();
*/

//Requires 'lib_item'
//@lib_item->lib_container
#include "libraries/lib_item"

//Settings
#define MAX_CONTAINERS			(10)//Anzahl aller Container
#define MAX_CONTAINER_OBJECTS	(5) //Anzahl der Items die ein Container beinhalten kann
#define AV_CONT_RANGE			(5.0)
//	@->dialogs
enum (+=1)
{
	DIALOG_CONT_MAIN = 1,
	DIALOG_CONT_TAKEALL,
	DIALOG_CONT_TAKE,
	DIALOG_CONT_TAKE_LOWP,
	DIALOG_CONT_UNLOCK,
	DIALOG_CONT_UNLOCK_SUCCESS,
	DIALOG_CONT_UNLOCK_FAIL
}

//Forwards
forward cont::OnContainerInit(containerid,contbezeichnung[],sql_id);
forward cont::OnContainerExit(containerid,sql_id);
forward cont::OnContainerLibInit();

//Structures
enum contain_main_struct
{
	cont_LowestID,
	cont_HighestID,
	cont_AllItems,
}
enum contain_struct
{
	cont_CID,
	bool:cont_Aktiv,
	cont_Bezeichnung[30],
	cont_Locked,
	cont_Objects,
	bool:cont_Opened,
	Text3D:cont_Label,
	Float:cont_pos[3],
}
enum contain_obj_struct
{
	cont_obj_ID,
	cont_Anzahl,
	bool:cont_oAktiv,
}

//Variables
new ContainerLibrary[contain_main_struct];
new ContainerObjects[MAX_CONTAINERS][MAX_CONTAINER_OBJECTS][contain_obj_struct];
new ContainerInfo[MAX_CONTAINERS][contain_struct];

//Functions
stock cont::GetPlayerNextContainer(playerid,Float:container_range=AV_CONT_RANGE)
{
	new next_container = -1, Float:lowest_range = 7000.0;
	for(new container; container<MAX_CONTAINERS; container++)
	{
		if(ContainerInfo[container][cont_Aktiv] == true)
		{
			new Float:act_range = GetPlayerDistanceFromPoint(playerid,ContainerInfo[container][cont_pos][0],ContainerInfo[container][cont_pos][1],ContainerInfo[container][cont_pos][2]);
			if(act_range <= container_range && act_range < lowest_range)
			{
				lowest_range 	= act_range;
				next_container 	= container;
			}
		}
	}
	return next_container;
}

stock cont::DestroyContainer(containerid,bool:_save=true)// -1 = falsche containerid, 0 = existiert nicht
{
	if(containerid < 0 || containerid >= MAX_CONTAINERS) return -1;
	if(ContainerInfo[containerid][cont_Aktiv] == false && ContainerInfo[containerid][cont_CID] == 0) return 0;
	
	if(_save == true)
	{
		format(string,sizeof(string),"DELETE FROM pnwl_cont_main WHERE cID='%d'",ContainerInfo[containerid][cont_CID]);
		db_query_once(SQL_DB,string);
		format(string,sizeof(string),"DELETE FROM pnwl_cont_objects WHERE containerid='%d'",ContainerInfo[containerid][cont_CID]);
		db_query_once(SQL_DB,string);
		printf("@lib_EX_container->cont::DestroyContainer = container (mysql cID %d) destroyed",ContainerInfo[containerid][cont_CID]);
	}
	
	for(new oid; oid<MAX_CONTAINER_OBJECTS; oid++)
	{
		ContainerObjects[containerid][oid][cont_obj_ID]	= 0;
		ContainerObjects[containerid][oid][cont_Anzahl]	= 0;
		ContainerObjects[containerid][oid][cont_oAktiv]	= false;
	}
	
	format(ContainerInfo[containerid][cont_Bezeichnung],30," ");
	ContainerInfo[containerid][cont_Aktiv]		= false;
	ContainerInfo[containerid][cont_Opened] 	= false;
	ContainerInfo[containerid][cont_Locked]		= 0;
	ContainerLibrary[cont_AllItems]				-= ContainerInfo[containerid][cont_Objects];
	ContainerInfo[containerid][cont_Objects]	= 0;
	ContainerInfo[containerid][cont_pos][0]		= 0.0;
	ContainerInfo[containerid][cont_pos][1]		= 0.0;
	ContainerInfo[containerid][cont_pos][2]		= 0.0;

	CallLocalFunction("cont_OnContainerExit","dd",containerid,ContainerInfo[containerid][cont_CID]);
	ContainerInfo[containerid][cont_CID]		= 0;	
	
	if(containerid == ContainerLibrary[cont_HighestID]) 
	{
		for(new i=containerid; i>=0; i--)
		{
			ContainerLibrary[cont_HighestID] = i;
			if(ContainerInfo[i][cont_Aktiv] == true) break;
		}
	}
	if(containerid == ContainerLibrary[cont_LowestID])
	{
		ContainerLibrary[cont_LowestID] = -1;
		for(new i=containerid; i<MAX_CONTAINERS; i++)
		{
			if(ContainerInfo[i][cont_Aktiv] == true)
			{
				ContainerLibrary[cont_LowestID] = i;
				break;
			}
		}
	}
	return 1;
}

stock cont::CreateContainer(Float:new_cont_x,Float:new_cont_y,Float:new_cont_z,locked=0,contbezeichnung[]=" ",bool:_save=true,give_cid=0)// -1 = keine freien containerslots mehr frei
{
	new new_cont_id = -1;
	for(new container; container<MAX_CONTAINERS; container++)
	{
		if(ContainerInfo[container][cont_Aktiv] == false)
		{
			new_cont_id = container;
			if(_save == true)
			{
				format(string,sizeof(string),"INSERT INTO pnwl_cont_main (aktiv,bezeichnung,locked,cont_x,cont_y,cont_z) VALUES ('1', '%s', '%d', '%f', '%f', '%f')",contbezeichnung,locked,new_cont_x,new_cont_y,new_cont_z);
				SQL_RES = db_query(SQL_DB,string);
			}
			ContainerInfo[new_cont_id][cont_CID]		= _save==true?mysql_insert_id():give_cid;
			ContainerInfo[new_cont_id][cont_Aktiv] 		= true;
			ContainerInfo[new_cont_id][cont_Locked] 	= locked;
			ContainerInfo[new_cont_id][cont_Objects] 	= 0;
			ContainerInfo[new_cont_id][cont_Opened]		= false;
			ContainerInfo[new_cont_id][cont_pos][0]		= new_cont_x;
			ContainerInfo[new_cont_id][cont_pos][1]		= new_cont_y;
			ContainerInfo[new_cont_id][cont_pos][2]		= new_cont_z;
			strmid(ContainerInfo[container][cont_Bezeichnung],contbezeichnung,0,strlen(contbezeichnung));
			for(new oid; oid<MAX_CONTAINER_OBJECTS; oid++)
			{
				ContainerObjects[new_cont_id][oid][cont_oAktiv] 	= false;
				ContainerObjects[new_cont_id][oid][cont_Anzahl] 	= 0;
				ContainerObjects[new_cont_id][oid][cont_obj_ID]		= 0;
			}
			CallLocalFunction("cont_OnContainerInit","dsd",new_cont_id,contbezeichnung,ContainerInfo[new_cont_id][cont_CID]);
			db_free_result(SQL_RES);
			break;
		}
	}
	if(new_cont_id >= ContainerLibrary[cont_HighestID]) ContainerLibrary[cont_HighestID] = new_cont_id;
	if(new_cont_id < ContainerLibrary[cont_LowestID] || ContainerLibrary[cont_LowestID] == -1) ContainerLibrary[cont_LowestID] = new_cont_id;
	return new_cont_id;
}

stock Float:cont::GetPlayerRangeToContainer(playerid,container_id)
{
	if(container_id < 0 || container_id >= MAX_CONTAINERS || ContainerInfo[container_id][cont_Aktiv] == false) return 10000.0;
	return GetPlayerDistanceFromPoint(playerid,ContainerInfo[container_id][cont_pos][0],ContainerInfo[container_id][cont_pos][1],ContainerInfo[container_id][cont_pos][2]);
}

stock cont_ShowPlayerDialog(playerid, dialogid, dialogstyle, header[], info[], but1[], but2[])
{
	SetPVarInt(playerid,"cont_dialog",1);
	ShowPlayerDialog(playerid,dialogid,dialogstyle,header,info,but1,but2);
	return 1;
}

stock cont::RemoveContainerItem(containerid,itemid,anzahl=-1,bool:_save=true)// -1 = falsche itemid
{
	if((itemid < 0 || itemid >= MAX_ITEMS) && anzahl != -1) return -1;
	if(ContainerObjects[containerid][itemid][cont_oAktiv] == false) return 0;
	
	if(anzahl == -1) anzahl = ContainerObjects[containerid][itemid][cont_Anzahl];
	new oanzahl = ContainerObjects[containerid][itemid][cont_Anzahl];
	if((ContainerObjects[containerid][itemid][cont_Anzahl]-=anzahl)<=0)
	{
		ContainerInfo[containerid][cont_Objects] -= oanzahl;
		ContainerLibrary[cont_AllItems]	-= oanzahl;
		if(_save == true)
		{
			format(string,sizeof(string),"DELETE FROM pnwl_cont_objects WHERE containerid='%d' AND item_id='%d'",ContainerInfo[containerid][cont_CID],ContainerObjects[containerid][itemid][cont_obj_ID]);
			db_query_once(SQL_DB,string);
		}
		
		ContainerObjects[containerid][itemid][cont_oAktiv]	= false;
		ContainerObjects[containerid][itemid][cont_Anzahl]	= 0;
		ContainerObjects[containerid][itemid][cont_obj_ID]		= 0;
		
		new bool:replaced = false;
		for(new item=(MAX_CONTAINER_OBJECTS-1); item>itemid; item--)
		{
		    if(ContainerObjects[containerid][item][cont_oAktiv] == true && ContainerObjects[containerid][item][cont_Anzahl] >= 1)
		    {
				ContainerObjects[containerid][itemid][cont_Anzahl]	= ContainerObjects[containerid][item][cont_Anzahl];
				ContainerObjects[containerid][itemid][cont_obj_ID]	= ContainerObjects[containerid][item][cont_obj_ID];
				ContainerObjects[containerid][itemid][cont_oAktiv]	= true;
				
    			ContainerObjects[containerid][item][cont_oAktiv] 	= false;
				ContainerObjects[containerid][item][cont_Anzahl] 	= 0;
				ContainerObjects[containerid][item][cont_obj_ID]		= 0;
				
				replaced = true;
				break;
		    }
		}
		if(replaced == false)
		{
			//Konnte nicht durch ein anderes Item ersetzt werden, Slot bleibt leer
		}
	}
	else
	{
		ContainerInfo[containerid][cont_Objects]-= anzahl;
		ContainerLibrary[cont_AllItems] 		-= anzahl;
		if(_save == true)
		{
			format(string,sizeof(string),"UPDATE pnwl_cont_objects SET anzahl='%d' WHERE containerid='%d' AND item_id='%d'",ContainerObjects[containerid][itemid][cont_Anzahl],ContainerInfo[containerid][cont_CID],ContainerObjects[containerid][itemid][cont_obj_ID]);
			db_query_once(SQL_DB,string);
		}	
	}
	return 1;
}

stock cont::GetContainerItemSlot(containerid,itemid)
{
	for(new slot=0; slot<MAX_CONTAINER_OBJECTS; slot++) if(ContainerObjects[containerid][slot][cont_oAktiv] == true && ContainerObjects[containerid][slot][cont_obj_ID] == itemid) return slot;
	return -1;
}

stock cont::AddContainerItem(containerid,itemid,anzahl,bool:_save=true)// -1 = falsche itemid, -2 = falsche containerid, -3 = zu wenig platz, -4 = zu wenig items
{
	if(itemid < 0 || itemid>=MAX_ITEMS) return -1;
	if(anzahl <= 0) return -4;
	else if(containerid < 0 || containerid >= MAX_CONTAINERS) return -2;

	new bool:has_item = false;
	for(new oid=0; oid<MAX_CONTAINER_OBJECTS; oid++)
	{
	    if(ContainerObjects[containerid][oid][cont_obj_ID] == itemid)
	    {
	        has_item = true;
	        if(ContainerObjects[containerid][oid][cont_oAktiv] == true) ContainerObjects[containerid][oid][cont_Anzahl] += anzahl;
			else ContainerObjects[containerid][oid][cont_Anzahl] 	= anzahl;
			ContainerObjects[containerid][oid][cont_oAktiv] 		= true;

			if(_save == true)
			{
				format(string,sizeof(string),"UPDATE pnwl_cont_objects SET anzahl='%d',aktiv='1' WHERE containerid='%d' AND item_id='%d'",ContainerObjects[containerid][oid][cont_Anzahl],ContainerInfo[containerid][cont_CID],itemid);
				db_query_once(SQL_DB,string);
			}
			break;
	    }
	}
	if(has_item == false)
	{
	    for(new oid=0; oid<MAX_CONTAINER_OBJECTS; oid++)
	    {
	        if(ContainerObjects[containerid][oid][cont_oAktiv] == false)
	        {
	            has_item = true;
	            ContainerObjects[containerid][oid][cont_obj_ID] = itemid;
	            ContainerObjects[containerid][oid][cont_Anzahl] = anzahl;
	            ContainerObjects[containerid][oid][cont_oAktiv] = true;
				if(_save == true)
				{
					format(string,sizeof(string),"INSERT INTO pnwl_cont_objects (aktiv,containerid,item_id,anzahl) VALUES ('1', '%d', '%d', '%d')",ContainerInfo[containerid][cont_CID],itemid,anzahl);
					db_query_once(SQL_DB,string);
	            }
				break;
	        }
	    }
	}
	if(has_item == false) return -3;
	ContainerInfo[containerid][cont_Objects] 	+= anzahl;
	ContainerLibrary[cont_AllItems] 			+= anzahl;
	return 1;
}

stock cont::ShowPlayerMenu(playerid,containerid,bool:skip_lock=false,bool:_redirect=false)// -1 = falsche containerid, -2 = bereits in benutzung
{
	if(containerid < 0 || containerid >= MAX_CONTAINERS) return -1;
	if(_redirect == false && ContainerInfo[containerid][cont_Opened] == true) 
	{
		format(string,sizeof(string),"Error -2, Container (ID %d) bereits geöffnet",containerid);
		SendClientMessage(playerid,GRUEN,string);
		return -2;
	}
	
	SetPVarInt(playerid,"p_cont_id",(containerid+1));
	ContainerInfo[containerid][cont_Opened] = true;
	
	format(string,sizeof(string),"");
	if(!skip_lock)
	{
		if(ContainerInfo[containerid][cont_Locked] != 0)
		{
			if(ContainerInfo[containerid][cont_Locked] >= 1)
			{
				if(floatpower(1.0,1.0) >= 1)
				{//Hat ein Lockpick
					format(string,sizeof(string),"{C85135}Achtung:\n{F1F7DC}Dieser Container ist mit einem %der Schloss verriegelt. Möchtest\nversuchen ihn mit deinem %der Lockpick zu knacken?",ContainerInfo[containerid][cont_Locked],1);
					cont_ShowPlayerDialog(playerid,DIALOG_CONT_UNLOCK,DIALOG_STYLE_MSGBOX,"{F1F7DC}Schloss knacken",string,"Ja","Nein");
				}
				else
				{//Hat nicht die notwendige Ausrüstung um das Schloss zu knacken
					ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Ausrüstung nicht vorhanden","{C85135}Achtung:\n{F1F7DC}Du hast nicht die notwendige Ausrüstung um das Schloss zu öffnen.","Zurück","");
					cont::PlayerExitContainer(playerid);
				}
			}
			else// < 0 = kann nicht geknackt werden
			{
				ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Kann nicht geöffnet werden","{C85135}Achtung:\n{F1F7DC}Dieses Schloss kann nicht geöffnet werden.","Zurück","");
				cont::PlayerExitContainer(playerid);
			}
			return 1;
		}
	}
	
	new dlop = 1, objects;
	for(new obj; obj<MAX_CONTAINER_OBJECTS; obj++)
	{
		if(ContainerObjects[containerid][obj][cont_oAktiv] == true && ContainerObjects[containerid][obj][cont_Anzahl] >= 1)
		{
			format(string,sizeof(string),"%s\n[%d] %s",string,ContainerObjects[containerid][obj][cont_Anzahl],item_ItemInfo[ContainerObjects[containerid][obj][cont_obj_ID]][iName]);
			PlayerOption[playerid][dialog][dlop] = obj;
			objects++;
		}
		else
		{
			format(string,sizeof(string),"%s\n-",string);
			PlayerOption[playerid][dialog][dlop] = -1;
		}
		dlop++;
	}
	
	if(objects == 0)
	{//Zurück
		format(string,sizeof(string),"{F1F7DC}Keine Items vorhanden -> Zurück%s",string);
		PlayerOption[playerid][dialog][0] = -2;
		cont_ShowPlayerDialog(playerid,DIALOG_CONT_MAIN,DIALOG_STYLE_LIST,"{F1F7DC}Der Container ist leer",string,"Zurück","");
	}
	else
	{//Take all
		format(string,sizeof(string),"{F1F7DC}Alle Items nehmen%s",string);
		PlayerOption[playerid][dialog][0] = -3;	
		cont_ShowPlayerDialog(playerid,DIALOG_CONT_MAIN,DIALOG_STYLE_LIST,"{F1F7DC}Container",string,"Nehmen","Zurück");
	}
	return 1;
}

//Callbacks
stock cont::OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	#pragma unused inputtext
	if(GetPVarInt(playerid,"cont_dialog") == 1)
	{
	    SetPVarInt(playerid,"cont_dialog",0);
		DeletePVar(playerid,"cont_dialog");
		
		if(dialogid == dialog_NULL) return 1;
		
		new player_container_id = (GetPVarInt(playerid,"p_cont_id")-1);
		switch(dialogid)
		{
			case dialog_NULL:
			{
				return 1;
			}
			case DIALOG_CONT_MAIN:
			{
				if(!response) 
				{
					cont::PlayerExitContainer(playerid);
					return 1;//Beenden
				}
				new object_id = GetDialogOption(playerid,listitem);
				if(object_id < 0)
				{//Methods
					if(object_id == -1)
					{//INVALID ITEM
						cont::ShowPlayerMenu(playerid,player_container_id,true,true);
					}
					if(object_id == -2)
					{//KEINE ITEMS VORHANDEN -> ZURÜCK
						cont::PlayerExitContainer(playerid);
					}
					if(object_id == -3)
					{//TAKE ALL
						new objects;
						for(new obj; obj<MAX_CONTAINER_OBJECTS; obj++)
						{
							if(ContainerObjects[player_container_id][obj][cont_oAktiv] == true && ContainerObjects[player_container_id][obj][cont_Anzahl] >= 1)
							{
								if(item_GivePlayerItem(playerid,ContainerObjects[player_container_id][obj][cont_obj_ID],ContainerObjects[player_container_id][obj][cont_Anzahl]) == 1)
								{//Nehmen erfolgreich
									objects += ContainerObjects[player_container_id][obj][cont_Anzahl];
									cont::RemoveContainerItem(player_container_id,obj,ContainerObjects[player_container_id][obj][cont_Anzahl]);									
								}
							}
						}
						format(string,sizeof(string),"{F1F7DC}Du hast insgesamt %d Items aus dem Container genommen.",objects);
						cont_ShowPlayerDialog(playerid,DIALOG_CONT_TAKEALL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) genommen",string,"Zurück","");
					}
				}
				else
				{//Spezielles Item nehmen
					new	itemid = ContainerObjects[player_container_id][object_id][cont_obj_ID],
						anzahl = ContainerObjects[player_container_id][object_id][cont_Anzahl];
					new result = item_GivePlayerItem(playerid,itemid,anzahl);
					
					format(string,sizeof(string),"result = %d, object_id = %d, itemid = %d, anzahl = %d",result,object_id,itemid,anzahl);
					SendClientMessage(playerid,GRUEN,string);					
					
					switch(result)
					{
						case 1:
						{//Nehmen erfolgreich
							cont::RemoveContainerItem(player_container_id,object_id,anzahl);
							cont::ShowPlayerMenu(playerid,player_container_id,true,true);
						}
						case -1:
						{//falsche itemid
							cont_ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Error -1","{F1F7DC}lib_EX_container error -1","Zurück","");
							cont::PlayerExitContainer(playerid);
						}
						case -2:
						{//offline
							cont_ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Error -2","{F1F7DC}lib_EX_container error -2","Zurück","");
							cont::PlayerExitContainer(playerid);
						}
						case -3:
						{//zu wenig platz
							cont_ShowPlayerDialog(playerid,DIALOG_CONT_TAKE_LOWP,DIALOG_STYLE_MSGBOX,"{F1F7DC}Zu wenig Platz","{F1F7DC}Du hast anscheinend zu wenig Platz, um das Item mitzunehmen.","Zurück","Beenden");
							cont::PlayerExitContainer(playerid);
						}
						case -4:
						{//zu wenig items							
							cont_ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Error -4","{F1F7DC}lib_EX_container error -4","Zurück","");
							cont::PlayerExitContainer(playerid);
						}
					}
				}
				return 1;
			}
			case DIALOG_CONT_TAKEALL:
			{
				cont::PlayerExitContainer(playerid);
			}
			case DIALOG_CONT_TAKE:
			{
				if(!response) 
				{
					cont::PlayerExitContainer(playerid);
					return 1;
				}
				cont::ShowPlayerMenu(playerid,player_container_id,true,true);
				return 1;
			}
			case DIALOG_CONT_TAKE_LOWP:
			{//zu wenig platz das item zu nehmen
				if(!response) 
				{
					cont::PlayerExitContainer(playerid);
					return 1;
				}
				cont::ShowPlayerMenu(playerid,player_container_id,true,true);
				return 1;
			}
			case DIALOG_CONT_UNLOCK:
			{
				if(response)
				{
					if(ContainerInfo[player_container_id][cont_Locked] == 0)
					{//Bereits offen
						cont::ShowPlayerMenu(playerid,player_container_id,true,true);
						return 1;
					}					
					if(GetPVarInt(playerid,"last_lockpick") == 0)
					{//Kann Schloss möglicherweise knacken
						if(ContainerInfo[player_container_id][cont_Locked] < 0)
						{//Kann nicht geknackt werden
							cont_ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Hinweis","{C85135}Hinweis: Dieses Schloss kann nicht geknackt werden.","","");
							cont::PlayerExitContainer(playerid);
							return 1;
						}
						new next_pick = floatround(floatrandom(200.0-PlayerSkill[playerid][SkillGlueck]*2)+400.0);
						if(floatrandom(1/float(ContainerInfo[player_container_id][cont_Locked])+(PlayerSkill[playerid][SkillGlueck]+PlayerSkill[playerid][SkillGeschick])/200) >= 1.0)
						{//Erfolg
							ContainerInfo[player_container_id][cont_Locked] = 0;//Schloss öffnen
							format(string,sizeof(string),"{F1F7DC}ERFOLG! Du hast das Schloss geknackt. Das nnächste Schloss\n kannst du in %d Sekunden knacken",next_pick);
							cont_ShowPlayerDialog(playerid,DIALOG_CONT_UNLOCK_SUCCESS,DIALOG_STYLE_MSGBOX,"{F1F7DC}Schloss geknackt",string,"Öffnen","Zurück");
						}
						else
						{//Misserfolg
							format(string,sizeof(string),"{C85135}ACHTUNG: Du konntest das Schloss nicht knacken. Du kannst das\nnächste Schloss in %d Sekunden knacken",next_pick);
							cont_ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Schloss geknackt",string,"Zurück","");
							cont::PlayerExitContainer(playerid);
						}
						SetPVarInt(playerid,"last_lockpick",next_pick);
					}
				}
				else return 1;
			}
			case DIALOG_CONT_UNLOCK_SUCCESS:
			{
				if(!response) cont::PlayerExitContainer(playerid);
				else
				{
					cont::ShowPlayerMenu(playerid,player_container_id,true,true);
				}
				return 1;
			}
			default: 
			{
				return 1;
			}
		}
		return 1;
	}
	return 0;
}

stock cont::PlayerExitContainer(playerid)
{
	new contid = GetPVarInt(playerid,"p_cont_id");
	if(contid >= 1)
	{
		ContainerInfo[(contid-1)][cont_Opened] = false;
		SetPVarInt(playerid,"p_cont_id",0);
		DeletePVar(playerid,"p_cont_id");
		return (contid-1);
	}
	return -1;
}

//Publics
public cont::OnContainerLibInit()
{
	format(string,sizeof(string),"SELECT * FROM pnwl_cont_main LIMIT %d",(MAX_CONTAINERS-1));
	SQL_RES = db_query(SQL_DB,string);
	if(db_num_rows(SQL_RES) >= 1)
	{
		new ccontid,cID,aktiv,locked,Float:p[3],loop=1;
		do
		{
			db_get_field_assoc(SQL_RES,"cID",string,sizeof(string));
			cID = strval(string);
			db_get_field_assoc(SQL_RES,"aktiv",string,sizeof(string));
			aktiv = strval(string);
			db_get_field_assoc(SQL_RES,"locked",string,sizeof(string));
			locked = strval(string);
			db_get_field_assoc(SQL_RES,"cont_x",string,sizeof(string));
			p[0] = floatstr(string);
			db_get_field_assoc(SQL_RES,"cont_y",string,sizeof(string));
			p[1] = floatstr(string);
			db_get_field_assoc(SQL_RES,"cont_z",string,sizeof(string));
			p[2] = floatstr(string);
			
			db_get_field_assoc(SQL_RES,"bezeichnung",string,sizeof(string));
			ccontid = cont::CreateContainer(p[0],p[1],p[2],locked,string,false,cID);
			if(ccontid == -1)
			{
				printf("@lib_EX_container => THROW ERROR (loading containers failed at loop %d)",loop);
				break;
			}
			else ContainerInfo[ccontid][cont_Aktiv] = (aktiv==0?(false):(true));
			loop++;
		}
		while(db_next_row(SQL_RES));
		printf("@lib_EX_container => %d containers created",(loop-1));
	}
	db_free_result(SQL_RES);
	
	new tmp_info[2];
	for(new containerid; containerid<=ContainerLibrary[cont_HighestID]; containerid++)
	{
		format(string,sizeof(string),"SELECT * FROM pnwl_cont_objects WHERE containerid='%d' LIMIT %d",containerid,MAX_CONTAINER_OBJECTS);
		SQL_RES = db_query(SQL_DB,string);
		if(db_num_rows(SQL_RES) > 0)
		{
			do
			{
				db_get_field_assoc(SQL_RES,"item_id",string,sizeof(string));
				tmp_info[0] = strval(string);//ItemID
				db_get_field_assoc(SQL_RES,"anzahl",string,sizeof(string));
				tmp_info[1] = strval(string);//Anzahl
				cont::AddContainerItem(containerid,tmp_info[0],tmp_info[1],false);
			}
			while(db_next_row(SQL_RES));
		}
		db_free_result(SQL_RES);
	}
	return 1;
}