/*
	PostNuke Wasteland House System
	Date: 17.10.2011
	Author: NeoPhoenix	
*/

#if defined lib_ex_house_included
	#endinput
#else
	#define lib_ex_house_included
#endif
/*
	native OnHouseInit();
	native house_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
	native OnHouseTryEnter(playerid,hid,bool:retour=false);
	native OnHouseEnterDialog(playerid,hid);
	native OnEnterHouse(playerid,hid,bool:_enter=true);
	native LoadPlayerHouseSpawnSettings(playerid,col_type[]);
	native OnHouseExit(playerid,hid);
	native ShowHouseMainDialog(playerid,hid);
*/

#include <a_samp>
#include "libraries/lib_CORE"
#define MAX_HOUSES 4
#define MAX_HOUSE_UPGRADES 5
new Float:h_exit_pos[MAX_HOUSES][4] = {
{797.1956,-2734.96337,100.878715,359.0},//0 = default
{797.1956,-2734.96337,100.878715,359.0},//1 and default
{1.0,2.0,3.0},
{1.0,2.0,3.0}
};
new Float:m_reg[4/*Bett upgrades*/] = {30.0,55.0,90.0,125.0};


#define house_ShowPlayerDialog(%0,%1,%2,%3,%4,%5,%6);\
SetPVarInt(%0,"player_haus_dialog",1);\
ShowPlayerDialog(%0,%1,%2,%3,%4,%5,%6);

enum h_info
{
	hAktiv,
	hID,
	hOwner,
	hUpgrade[6],
	hLocked,
	bool:hOpen,
	hGangOpen,
	hInterior,
	Float:hPos[3],
	hBewohner,
	hMaxBewohner,
	bool:hMietbar,
	hPickup,
	Text3D:hLabel
}
new HouseInfo[MAX_HOUSES][h_info];

OnHouseInit()
{
	format(query,sizeof(query),"SELECT * FROM pnwl_houses WHERE cvar='1' LIMIT %d",MAX_HOUSES);
	mysql_query(query);
	mysql_store_result();
	printf("LOADING HOUSES. COUNT: %d",mysql_num_rows());
	new hid=0,h_cache[5];
	if( mysql_num_rows() >= 1 )
	{
	    while(mysql_retrieve_row())
	    {
	        mysql_fetch_field_row(string,"id");
	        h_cache[0] = strval(string);
			
			HouseInfo[hid][hAktiv] = 1;
	        HouseInfo[hid][hID] = h_cache[0];
	        
			mysql_fetch_field_row(string,"owner");
	        HouseInfo[hid][hOwner] = strval(string);
			mysql_fetch_field_row(string,"interior");
	        HouseInfo[hid][hInterior] = strval(string);
			mysql_fetch_field_row(string,"bewohner");
	        HouseInfo[hid][hBewohner] = strval(string);
			mysql_fetch_field_row(string,"maxbewohner");
	        HouseInfo[hid][hMaxBewohner] = strval(string);
			
			mysql_fetch_field_row(string,"locked");
	        HouseInfo[hid][hLocked] = strval(string);
			mysql_fetch_field_row(string,"mietbar");
	        HouseInfo[hid][hMietbar] = strval(string)>=1?true:false;
			mysql_fetch_field_row(string,"open");
	        HouseInfo[hid][hOpen] = strval(string)==1?true:false;
			mysql_fetch_field_row(string,"gangopen");
	        HouseInfo[hid][hGangOpen] = strval(string);
	        
	        mysql_fetch_field_row(string,"u0");
	        HouseInfo[hid][hUpgrade][0] = strval(string);
	        mysql_fetch_field_row(string,"u1");
	        HouseInfo[hid][hUpgrade][1] = strval(string);
	        mysql_fetch_field_row(string,"u2");
	        HouseInfo[hid][hUpgrade][2] = strval(string);
	        mysql_fetch_field_row(string,"u3");
	        HouseInfo[hid][hUpgrade][3] = strval(string);
	        mysql_fetch_field_row(string,"u4");
	        HouseInfo[hid][hUpgrade][4] = strval(string);
	        mysql_fetch_field_row(string,"u5");
	        HouseInfo[hid][hUpgrade][5] = strval(string);
	        
	        mysql_fetch_field_row(string,"px");
	        HouseInfo[hid][hPos][0] = floatstr(string);
	        mysql_fetch_field_row(string,"py");
	        HouseInfo[hid][hPos][1] = floatstr(string);
	        mysql_fetch_field_row(string,"pz");
	        HouseInfo[hid][hPos][2] = floatstr(string);
	        
	        if(++hid >= MAX_HOUSES) break;
		}
	}
	mysql_free_result();
	
	hid = 0;
	while(__SERVER[TRUE])
	{
	    if(HouseInfo[hid][hID] == 0) break;
	    if(HouseInfo[hid][hOwner] != 0) format(string,sizeof(string),"Haus %d\nBesitzer: %s\nStatus %s",HouseInfo[hid][hID],mysql_GetStringByInt("pnwl_user","username","userid",HouseInfo[hid][hOwner]),HouseInfo[hid][hOpen]==false?("Abgeschlossen"):("Offen"));
	    else format(string,sizeof(string),"Haus %d\nUnbewohnt\nStatus %s",HouseInfo[hid][hID],HouseInfo[hid][hOpen]==false?("Abgeschlossen"):("Offen"));
		HouseInfo[hid][hLabel] = Create3DTextLabel(string,0x878375FF,HouseInfo[hid][hPos][0],HouseInfo[hid][hPos][1],HouseInfo[hid][hPos][2],30.0,0,1);
		if(HouseInfo[hid][hOwner] != 0) HouseInfo[hid][hPickup] = CreatePickup(1272,1,HouseInfo[hid][hPos][0],HouseInfo[hid][hPos][1],HouseInfo[hid][hPos][2],-1);
		else HouseInfo[hid][hPickup] = CreatePickup(1273,1,HouseInfo[hid][hPos][0],HouseInfo[hid][hPos][1],HouseInfo[hid][hPos][2],-1);
		hid++;
	}
	for(new hpid; hpid<sizeof(h_exit_pos[]); hpid++) CreatePickup(1318,1,h_exit_pos[hpid][0],h_exit_pos[hpid][1],h_exit_pos[hpid][2],-1);
	return 1;
}

enum (+=1)
{
	dialog_HOUSE_MAINRE = 1,
	dialog_HOUSE_MAIN,
	dialog_HOUSE_ENTER,
	dialog_HOUSE_BUY,
	dialog_HOUSE_BETT_MAIN,
	dialog_HOUSE_BETT_MAINRE,
	dialog_HOUSE_BETT_RENT,
	dialog_HOUSE_BETT_UNRENT,
	dialog_HOUSE_EINBRUCH,
	dialog_HOUSE_INFO,
}

house_OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
	#pragma unused newkeys
	if(oldkeys == KEY_SECONDARY_ATTACK)
	{
		new hid = PlayerEvent[playerid][InteractHouse];
	    if(hid != -1)
	    {
			new int_id = HouseInfo[hid][hInterior];
	        if(IsPlayerInRangeOfPoint(playerid,1.0,h_exit_pos[int_id][0],h_exit_pos[int_id][1],h_exit_pos[int_id][2]))
	        {
				if(HouseInfo[hid][hOwner] == PlayerInfo[playerid][PLAYER_ID]) 
				{
					OnHouseEnterDialog(playerid,hid);
					return 1;
				}
				OnHouseExit(playerid,hid);
				return 1;
			}
	        else
	        {//Upgrade injection
	            switch(HouseInfo[hid][hInterior])
	            {
			        //case 0 = default
			        case 2:
			        {
			        }
			        default:
			        {//CASE 1: (oder alle anderen invaliden)
						if(HouseInfo[hid][hUpgrade][0] != 0 && IsPlayerInRangeOfPoint(playerid,2.0,795.300781,-2723.40087,101.0799))
						{//Bett
							ShowHouseMainDialog(playerid,hid);
							return 1;
						}
						if(HouseInfo[hid][hUpgrade][1] != 0 && IsPlayerInRangeOfPoint(playerid,2.0,801.87042236,-2723.5883,100.88622))
						{//Schrank
						    SendClientMessage(playerid,GRUEN,"INFO: Das ist der Schrank");
							return 1;
						}
						if(HouseInfo[hid][hUpgrade][2] >= 1 && IsPlayerInRangeOfPoint(playerid,2.0,794.812316,-2718.0825,100.8787))
						{//Funk
						    SendClientMessage(playerid,GRUEN,"INFO: Das ist das Funkgerät");
							return 1;
						}
						if(HouseInfo[hid][hUpgrade][3] >= 1 && IsPlayerInRangeOfPoint(playerid,3.0,807.60809,-2722.68090,100.87871))
						{//Crafting
							craft_DisplayCraftDialog(playerid);
							return 1;
						}
						if(HouseInfo[hid][hUpgrade][4] >= 1 && IsPlayerInRangeOfPoint(playerid,3.0,801.40643, -2718.34912, 100.484703))
						{//Ofen
							craft_DisplayFurnaceDialog(playerid);
							return 1;
						}
						if(HouseInfo[hid][hUpgrade][5] >= 1 && IsPlayerInRangeOfPoint(playerid,3.0,801,-2727.6999,100.9000))
						{//Tresor
							OnDialogResponse(playerid,dialog_ITEMSTORE_MAINRE,1,0,"");
							return 1;
						}
			        }
	            }
	        }
	        return 1;
	    }
	    else
		{
			hid = 0;
			while(__SERVER[TRUE])
			{
			    if(HouseInfo[hid][hID] == 0) break;
				if(IsPlayerInRangeOfPoint(playerid,1.5,HouseInfo[hid][hPos][0],HouseInfo[hid][hPos][1],HouseInfo[hid][hPos][2]))
				{
					PlayerEvent[playerid][InteractHouse] = hid;
					OnHouseEnterDialog(playerid,hid);
				    break;
				}
				hid++;
			}
		}
	}
	return 1;
}

haus_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	#pragma unused inputtext
	if(GetPVarInt(playerid,"player_haus_dialog") != 1) return 0;
	SetPVarInt(playerid,"player_haus_dialog",0);
	if(dialogid == dialog_NULL) return 1;
	if(IsPlayerNPC(playerid)) return 1;
	new hid = PlayerEvent[playerid][InteractHouse];
	switch(dialogid)
	{
		case dialog_HOUSE_BETT_MAINRE:
		{//ShowHouseMainDialog
			house_OnPlayerKeyStateChange(playerid,0,KEY_SECONDARY_ATTACK);
			return 1;
		}
		case dialog_HOUSE_BETT_UNRENT:
		{
			if(response)
			{//Nicht mehr mieten
				if(PlayerOption[playerid][HouseSelect] != HouseInfo[hid][hID]) return house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten","{C85135}Du übernachtest hier nicht!","Zurück","");
				format(string,sizeof(string),"{F1F7DC}Du übernachtest nun nicht mehr in dem Haus %d.",PlayerOption[playerid][HouseSelect]);
				house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten",string,"Zurück","");
				if(--HouseInfo[hid][hBewohner] <= 0)
				{
					HouseInfo[hid][hBewohner] = 0;
					format(string,sizeof(string),"UPDATE pnwl_houses SET bewohner=0 WHERE id='%d'",HouseInfo[hid][hID]);
				}
				else format(string,sizeof(string),"UPDATE pnwl_houses SET bewohner=bewohner-1 WHERE id=%d",HouseInfo[hid][hID]);
				mysql_query(string);
				mysql_free_result();
				PlayerOption[playerid][HouseSelect] = -1;
			}
			else
			{//Abbruch
				house_OnPlayerKeyStateChange(playerid,0,KEY_SECONDARY_ATTACK);
			}
			return 1;
		}
		case dialog_HOUSE_BETT_RENT:
		{//Will haus mieten
			if(response)
			{
				if(HouseInfo[hid][hBewohner] >= HouseInfo[hid][hMaxBewohner]) return house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten","{C85135}Es sind alle Schlafplätze belegt, versuche es später erneut!","Zurück","");
				new db_hid = HouseInfo[hid][hID];
				if(PlayerOption[playerid][HouseSelect] == db_hid || HouseInfo[hid][hBewohner] >= HouseInfo[hid][hMaxBewohner]) return house_OnPlayerKeyStateChange(playerid,0,KEY_SECONDARY_ATTACK);
				PlayerOption[playerid][HouseSelect] = db_hid;
				PlayerOption[playerid][SelectedSpawn] = 2;
				HouseInfo[hid][hBewohner]++;
				format(string,sizeof(string),"{F1F7DC}Du übernachtest nun in dem Haus %d.",PlayerOption[playerid][HouseSelect]);
				house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten",string,"Zurück","");
				format(string,sizeof(string),"UPDATE pnwl_houses SET bewohner=bewohner+1 WHERE id='%d'",HouseInfo[hid][hID]);
				mysql_query_once(string);
				format(query,sizeof(query),"UPDATE pnwl_user_option SET selectedspawn='2', houseselect='%d' WHERE userid='%d'",db_hid,PlayerInfo[playerid][PLAYER_ID]);
				mysql_query_once(query);
			}
			else
			{//Zurück ins Bettmenü
				house_OnPlayerKeyStateChange(playerid,0,KEY_SECONDARY_ATTACK);
			}
			return 1;
		}
		case dialog_HOUSE_BETT_MAIN:
		{
			if(!response) return 1;
			new option = GetDialogOption(playerid,listitem);
			switch(option)
			{
				case 0:
				{//Informationen
					format(string,sizeof(string),"{F1F7DC}Stufe: %d\nMüdigkeitsregeneration: %f/Stunde\nBewohner: %d/%d\n",HouseInfo[hid][hUpgrade][0],m_reg[HouseInfo[hid][hUpgrade][0]],HouseInfo[hid][hBewohner],HouseInfo[hid][hMaxBewohner]);
					house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"Bett",string,"Zurück","");
				}
				case 1:
				{//Übernachten
					if(PlayerOption[playerid][HouseSelect] == HouseInfo[hid][hID])
					{//Nicht mehr übernachten
						house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_UNRENT,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten","{F1F7DC}Bist du dir sicher, dass du hier nicht mehr\nübernachten willst?","Ja","Zurück");
					}
					else
					{//Will übernachten
						if(HouseInfo[hid][hBewohner] >= HouseInfo[hid][hMaxBewohner]) return house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten","{C85135}Es sind alle Schlafplätze belegt, versuche es später erneut!","Zurück","");
						if(PlayerInfo[playerid][PLAYER_ID] == HouseInfo[hid][hOwner])
						{//Ist der Besitzer
							if(PlayerOption[playerid][HouseSelect] != -1) format(string,sizeof(string),"{F1F7DC}Möchtest du hier übernachten?\n{C85135}HINWEIS: Du kannst dann vorerst nicht mehr in deinem anderen\nHaus (ID %d) spawnen!",PlayerOption[playerid][HouseSelect]);
							else format(string,sizeof(string),"{F1F7DC}Möchtest du hier übernachten?");
							house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_RENT,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten",string,"Ja","Zurück");
							return 1;
						}
						else
						{//Ein fremder
							if(PlayerOption[playerid][HouseSelect] != -1) format(string,sizeof(string),"{F1F7DC}Möchtest du wirklich in einem fremden Bett übernachten?\n{C85135}HINWEIS: Du kannst dann vorerst nicht mehr in deinem anderen\nHaus (ID %d) spawnen!",PlayerOption[playerid][HouseSelect]);
							else format(string,sizeof(string),"{F1F7DC}Möchtest du wirklich in einem fremden Bett übernachten?");
							house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_RENT,DIALOG_STYLE_MSGBOX,"{F1F7DC}Bett - Übernachten",string,"Ja","Zurück");
							return 1;
						}
					}
				}
				case 3:
				{
					HouseUnrentAll(hid);
					house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Haus - Bewohner rausgeworfen","{F1F7DC}Du hast alle Bewohner des Hauses verwiesen","Zurück","");
					return 1;
				}
				case 4:
				{
					HouseInfo[hid][hMietbar] = false;
					house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Haus - Bett deaktiviert","{F1F7DC}Du hast das Bett für weitere Bewohner gesperrt. Hier können nun\nkeine weiteren Personen übernachten.","Zurück","");
					format(query,sizeof(query),"UPDATE pnwl_houses SET mietbar='0' WHERE id='%d'",HouseInfo[hid][hID]);
					mysql_query_once(query);
					return 1;
				}
				case 5:
				{
					HouseInfo[hid][hMietbar] = true;
					house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Haus - Bett aktiviert","{F1F7DC}Du hast das Bett für Bewohner aktiviert. Hier kann nun\njeder übernachten.","Zurück","");
					format(query,sizeof(query),"UPDATE pnwl_houses SET mietbar='1' WHERE id='%d'",HouseInfo[hid][hID]);
					mysql_query_once(query);
				}
			}
			return 1;
		}
		case dialog_HOUSE_MAINRE: return OnHouseEnterDialog(playerid,hid);
		case dialog_HOUSE_MAIN:
		{
			if(!response) 
			{
				if(PlayerOption[playerid][HouseCache][0] == -1)
				{
					PlayerOption[playerid][HouseCache][0] = -1;
					PlayerEvent[playerid][InteractHouse] = -1;
				}
				return 1;
			}
			switch(GetDialogOption(playerid,listitem))
			{
				case -1:
				{//Haus verlassen (erscheint nur von innen)
					OnHouseExit(playerid,hid);
					return 1;
				}
				case 0:
				{//Informationen
					if(HouseInfo[hid][hOpen] == true) format(string,sizeof(string),"{77AE35}Offen{F1F7DC}");
					else if(HouseInfo[hid][hGangOpen] != 0 && PlayerInfo[playerid][pGang] == HouseInfo[hid][hGangOpen]) format(string,sizeof(string),"Für Gangmitglieder {77AE35}offen{F1F7DC}");
					else format(string,sizeof(string),"{C85135}Geschlossen [%d]{F1F7DC}",HouseInfo[hid][hLocked]);
			
					if(HouseInfo[hid][hOwner] >= 1) format(string,sizeof(string),"{F1F7DC}Besitzer: %s\nStatus: %s",mysql_GetStringByInt("pnwl_user","username","userid",HouseInfo[hid][hOwner]),string);
					else format(string,sizeof(string),"{F1F7DC}Besitzer: Unbewohnt\nStatus: %s",string);
					format(query,sizeof(query),"{F1F7DC}Haus %d",HouseInfo[hid][hID]);
					house_ShowPlayerDialog(playerid,dialog_HOUSE_INFO,DIALOG_STYLE_MSGBOX,query,string,"Zurück","");
				}
				case 1:
				{//Betreten
					OnHouseTryEnter(playerid,hid);
				}
				case 2:
				{//Bewohnbar machen
					house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}In Arbeit","{F1F7DC}Dieses Feature ist noch nicht verfügbar.","Zurück","");
				}
				case 3:
				{//Haustür öffnen/abschließen
					if(HouseInfo[hid][hOpen] == true) format(string,sizeof(string),"{F1F7DC}Du hast die Tür mit einem %der Schloss {C85135}abgeschlossen{F1F7DC}.",HouseInfo[hid][hLocked]);
					else format(string,sizeof(string),"{F1F7DC}Du hast die Tür {77AE35}geöffnet{F1F7DC}.");
					HouseInfo[hid][hOpen] = HouseInfo[hid][hOpen]==true?false:true;
					house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Haus",string,"Zurück","");
					mysql_SetIntByInt("pnwl_houses","open",HouseInfo[hid][hOpen]==true?1:0,"id",HouseInfo[hid][hID]);
				}
				case 4:
				{//Haus für Gangmember öffnen/schließen
					if(PlayerInfo[playerid][pGang] == 0)
					{
						house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Du bist in keiner Gang","{F1F7DC}Fehler: Du musst in einer aktiven Gang sein um die Haustür für\ndeine Gangkollegen zu öffnen/schließen.","Zurück","");
						return 1;
					}
					if(HouseInfo[hid][hGangOpen] == 0) format(string,sizeof(string),"{F1F7DC}Du hast die Tür für Gangmitglieder {77AE35}geöffnet{F1F7DC}.");
					else format(string,sizeof(string),"{F1F7DC}Du hast die Tür für Gangmitglieder {C85135}geschlossen{F1F7DC}.");
					HouseInfo[hid][hGangOpen] = HouseInfo[hid][hGangOpen]==0?PlayerInfo[playerid][pGang]:0;
					house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Haus",string,"Zurück","");
					mysql_SetIntByInt("pnwl_houses","open",HouseInfo[hid][hGangOpen],"id",HouseInfo[hid][hID]);
				}
			}
			return 1;
		}
		case dialog_HOUSE_INFO:
		{
			OnHouseEnterDialog(playerid,hid);
			return 1;
		}
		case dialog_HOUSE_ENTER:
		{
			if(response) OnHouseTryEnter(playerid,hid,true);//Betreten
			else OnHouseEnterDialog(playerid,hid);//Nicht betreten
			return 1;
		}
		case dialog_HOUSE_EINBRUCH:
		{		
			if(response)
			{//Nicht einbrechen
				OnHouseEnterDialog(playerid,hid);
			}
			else
			{//Einbrechen
				if(HouseInfo[hid][hOpen] == true)
				{
					house_ShowPlayerDialog(playerid,dialog_HOUSE_ENTER,DIALOG_STYLE_MSGBOX,"{F1F7DC}Tür ist bereits offen","{F1F7DC}Einbruch gescheitert, die Türen sind bereits geöffnet.\nMöchtest du das Haus betreten?","Ja","Nein");
					return 1;
				}
				else
				{
					if(GetPVarInt(playerid,"next_lockpick") <= 0)
					{//Kann Schloss möglicherweise knacken
						new next_pick = floatround(floatrandom(200.0-PlayerSkill[playerid][SkillGlueck]*2)+400.0);
						if(floatrandom((1/float(HouseInfo[hid][hLocked])+(PlayerSkill[playerid][SkillGlueck]+PlayerSkill[playerid][SkillGeschick])/200)) >= 1.0)
						{//Erfolg
							format(string,sizeof(string),"{F1F7DC}Einbruch erfolgreich, du kannst den nächsten Einbruch\nin %d Sekunden wieder begehen.",next_pick);
							house_ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Einbruch erfolgreich",string,"Zurück","");
							OnEnterHouse(playerid,hid);
						}
						else
						{//Gescheitert
							format(string,sizeof(string),"{F1F7DC}Einbruch gescheitert, du kannst es in %d Sekunden wieder versuchen.",next_pick);
							house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Einbruch gescheitert",string,"Zurück","");
						}
						SetPVarInt(playerid,"next_lockpick",next_pick);
						return 1;
					}
					else
					{//Muss noch warten
						format(string,sizeof(string),"{F1F7DC}Diese Tür ist abgeschlossen und du kannst dein Equipment erst wieder\nin %d Sekunden einsetzen.",GetPVarInt(playerid,"next_lockpick"));
						house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Einbruch gescheitert",string,"Zurück","");
						return 1;
					}
				}
			}
		}
	}
	return 1;
}

OnHouseTryEnter(playerid,hid,bool:retour=false)
{
	if(HouseInfo[hid][hOwner] == PlayerInfo[playerid][PLAYER_ID])
	{
		OnEnterHouse(playerid,hid);
		return 1;
	}
	if(HouseInfo[hid][hOpen] != true && PlayerOption[playerid][HouseSelect] != HouseInfo[hid][hID] && !(HouseInfo[hid][hGangOpen] != 0 && PlayerInfo[playerid][pGang] == HouseInfo[hid][hGangOpen]))
	{//Ist abgeschlossen
		if(1 >= HouseInfo[hid][hLocked])
		{//Chance das Schloss zu knacken
			if(GetPVarInt(playerid,"next_lockpick") <= 0)
			{//Kann Schloss möglicherweise knacken
				format(string,sizeof(string),"{F1F7DC}Diese Tür ist mit einem %der Schloss verrigelt. Möchtest du es mit\ndeinem %der Einbruchsequipment versuchen zu knacken?",HouseInfo[hid][hLocked],1);
				house_ShowPlayerDialog(playerid,dialog_HOUSE_EINBRUCH,DIALOG_STYLE_MSGBOX,"{F1F7DC}Einbrechen?",string,"Nein","Ja");
				return 1;
			}
			else
			{//Muss noch warten
				format(string,sizeof(string),"{F1F7DC}Diese Tür ist abgeschlossen und du kannst dein Equipment erst wieder\nin %d Sekunden einsetzen.",GetPVarInt(playerid,"next_lockpick"));
				house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Einbruch gescheitert",string,"Zurück","");
				return 1;
			}
		}
		else
		{
			house_ShowPlayerDialog(playerid,dialog_HOUSE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Haus ist abgeschlossen","{F1F7DC}Die Türen dieses Hauses sind verrigelt und du besitzt nicht das notwendige\nWerkzeug diese zu öffnen. Frage entweder den Besitzer des Hauses ob er dir die Tür\nöffnet oder besorge dir gutes Einbrecherequipment.","Zurück","");
			return 1;
		}
	}
	else
	{//Ist offen
		if(retour == true) 
		{
			OnEnterHouse(playerid,hid);
			return 1;
		}
		house_ShowPlayerDialog(playerid,dialog_HOUSE_ENTER,DIALOG_STYLE_MSGBOX,"{F1F7DC}Fremdes Haus betreten","{F1F7DC}Du bist dabei ein fremdes Haus zu betreten. Möchtest du\ndieses Haus wirklich betreten?","Ja","Nein");
	}
	return 1;
}

OnHouseEnterDialog(playerid,hid)
{
    if(HouseInfo[hid][hOwner] != 0)
    {//Ist bewohnt
		new menu_header[120];
		format(menu_header,sizeof(menu_header),"{F1F7DC}Haus von %s",mysql_GetStringByInt("pnwl_user","username","userid",HouseInfo[hid][hOwner]));

		new dlop = 0;
		
		if(PlayerOption[playerid][HouseCache][0] != -1)
		{//Ist im Haus		
			if(HouseInfo[hid][hOwner] == PlayerInfo[playerid][PLAYER_ID])
			{
				hid = PlayerEvent[playerid][InteractHouse];
				dlop = 0;
				
				format(string,sizeof(string),"Haus verlassen");
				PlayerOption[playerid][dialog][dlop] = -1;
				dlop++;

				format(string,sizeof(string),"%s\nInformationen",string);
				PlayerOption[playerid][dialog][dlop] = 0;
				dlop++;

				if(HouseInfo[hid][hOpen] == false) format(string,sizeof(string),"%s\nHaustür {77AE35}öffnen{F1F7DC}",string);
				else format(string,sizeof(string),"%s\nHaustür {C85135}schließen{F1F7DC}",string);
				PlayerOption[playerid][dialog][dlop] = 3;
				dlop++;

				if(PlayerInfo[playerid][pGang] != 0)
				{
					if(HouseInfo[hid][hGangOpen] == 0) format(string,sizeof(string),"%s\nHaustür für Gangmitglieder {77AE35}öffnen{F1F7DC}",string);
					else format(string,sizeof(string),"%s\nHaustür für Gangmitglieder {C85135}schließen{F1F7DC}",string);
					PlayerOption[playerid][dialog][dlop] = 4;
					dlop++;
				}
				format(string,sizeof(string),"{F1F7DC}%s",string);
				house_ShowPlayerDialog(playerid,dialog_HOUSE_MAIN,DIALOG_STYLE_LIST,menu_header,string,"Ok","Zurück");				
				return 1;
			}
		}

        format(string,sizeof(string),"Informationen");
        PlayerOption[playerid][dialog][dlop] = 0;
		dlop++;
		
		if(HouseInfo[hid][hOpen] == true) format(query,sizeof(query),"{77AE35}Offen{F1F7DC}");
        else if(HouseInfo[hid][hGangOpen] != 0 && PlayerInfo[playerid][pGang] == HouseInfo[hid][hGangOpen]) format(query,sizeof(query),"Für Gangmitglieder {77AE35}offen{F1F7DC}");
		else format(query,sizeof(query),"{C85135}Geschlossen [%d]{F1F7DC}",HouseInfo[hid][hLocked]);
		format(string,sizeof(string),"%s\nHaus betreten (%s)",string,query);
		PlayerOption[playerid][dialog][dlop] = 1;
		dlop++;
	
		if(PlayerInfo[playerid][PLAYER_ID] == HouseInfo[hid][hOwner])
		{
			if(HouseInfo[hid][hOpen] == false) format(string,sizeof(string),"%s\nHaustür {77AE35}öffnen{F1F7DC}",string);
			else format(string,sizeof(string),"%s\nHaustür {C85135}schließen{F1F7DC}",string);
			PlayerOption[playerid][dialog][dlop] = 3;
			dlop++;

			if(PlayerInfo[playerid][pGang] != 0)
			{
				if(HouseInfo[hid][hGangOpen] == 0) format(string,sizeof(string),"%s\nHaustür für Gangmitglieder {77AE35}öffnen{F1F7DC}",string);
				else format(string,sizeof(string),"%s\nHaustür für Gangmitglieder {C85135}schließen{F1F7DC}",string);
				PlayerOption[playerid][dialog][dlop] = 4;
				dlop++;
			}
		}
		else
		{
		}

        format(string,sizeof(string),"{F1F7DC}%s",string);
        house_ShowPlayerDialog(playerid,dialog_HOUSE_MAIN,DIALOG_STYLE_LIST,menu_header,string,"Ok","Zurück");
    }
    else
    {//Ist nicht bewohnt
        format(string,sizeof(string),"Informationen");
        PlayerOption[playerid][dialog][0] = 0;

		if(HouseInfo[hid][hOpen] == false) format(query,sizeof(query),"{C85135}Geschlossen [%d]{F1F7DC}",HouseInfo[hid][hLocked]);
        else format(query,sizeof(query),"{77AE35}Offen{F1F7DC}");
		format(string,sizeof(string),"%s\nHaus betreten (%s)",string,query);
		PlayerOption[playerid][dialog][1] = 1;

        format(string,sizeof(string),"%s\nHaus bewohnbar machen",string);
        PlayerOption[playerid][dialog][2] = 2;

        format(string,sizeof(string),"{F1F7DC}%s",string);
        house_ShowPlayerDialog(playerid,dialog_HOUSE_MAIN,DIALOG_STYLE_LIST,"{F1F7DC}Unbewohntes Haus",string,"Ok","Zurück");
    }
	return 1;
}

OnEnterHouse(playerid,hid,bool:_enter=true)
{
	if(hid == -1) 
	{
		format(string,sizeof(string),"OnEnterHouse Error (PARAMS %d/_%d_/%s)",playerid,hid,_enter?("TRUE"):("FALSE"));
		SendClientMessage(playerid,GRUEN,string);
		return 0;
	}
	PlayerOption[playerid][HouseCache][0] = HouseInfo[hid][hID];
	PlayerEvent[playerid][InteractHouse] = hid;
	if(PlayerEvent[playerid][IstTot] == 0) SetPlayerVirtualWorld(playerid,(hid<<1)+1);
	else SetPlayerVirtualWorld(playerid,(hid<<1)+2);
	SetPlayerInterior(playerid,0);
    switch(HouseInfo[hid][hInterior])
    {
        //case 0 = default
        case 2:
        {
        }
        default:
        {//CASE 1: (oder alle anderen invaliden)
			CreatePlayerSpecial3DText(playerid,GRUEN,"Ausgang",797.1956,-2734.96337,100.878715,_,TEXT3D_TYPE_HOUSE);
            CreatePlayerSpecialObject(playerid,15046,797.14788818,-2741.18408203,101.12871552,0.00000000,0.00000000,0.00000000,_,OBJECT_TYPE_HOUSE);
            CreatePlayerSpecialObject(playerid,850,797.95349121,-2733.30004883,99.99021149,0.00000000,0.00000000,318.00000000,_,OBJECT_TYPE_HOUSE);
            CreatePlayerSpecialObject(playerid,850,796.13702393,-2732.79296875,99.99021149,0.00000000,0.00000000,187.99926758,_,OBJECT_TYPE_HOUSE);
			if(HouseInfo[hid][hUpgrade][0] >= 1)
			{
            	format(string,sizeof(string),"Bett (Stufe %d)",HouseInfo[hid][hUpgrade][0]);
            	CreatePlayerSpecial3DText(playerid,GRUEN,string,795.300781,-2723.40087,101.0799,_,TEXT3D_TYPE_HOUSE);
				switch(HouseInfo[hid][hUpgrade][0])
				{//Bett
					case 1: CreatePlayerSpecialObject(playerid,1771,793.73822021,-2722.82617188,100.28736877,0.00000000,0.00000000,0.00000000,_,OBJECT_TYPE_HOUSE);
					case 2: CreatePlayerSpecialObject(playerid,1763,793.96417236,-2723.50122070,99.87871552,0.00000000,0.00000000,90.25000000,_,OBJECT_TYPE_HOUSE);
					case 3: CreatePlayerSpecialObject(playerid,1757,793.96966553,-2723.91308594,99.79626465,0.00000000,0.00000000,90.00000000,_,OBJECT_TYPE_HOUSE);
					case 4: CreatePlayerSpecialObject(playerid,14861,794.39495850,-2724.09106445,100.32574463,0.00000000,0.00000000,0.00000000,_,OBJECT_TYPE_HOUSE);
				}
            }
			if(HouseInfo[hid][hUpgrade][1] >= 1)
			{
            	format(string,sizeof(string),"Schrank (Stufe %d)",HouseInfo[hid][hUpgrade][1]);
            	CreatePlayerSpecial3DText(playerid,GRUEN,string,801.87042236,-2723.5883,100.88622,_,TEXT3D_TYPE_HOUSE);
				switch(HouseInfo[hid][hUpgrade][1])
				{//Schrank
					case 1: CreatePlayerSpecialObject(playerid,2046,802.84307861,-2723.36157227,101.42513275,0.00000000,0.00000000,270.00000000,_,OBJECT_TYPE_HOUSE);
					case 2: CreatePlayerSpecialObject(playerid,2142,802.81933594,-2723.34936523,99.88622284,0.00000000,0.00000000,270.00000000,_,OBJECT_TYPE_HOUSE);
					case 3: CreatePlayerSpecialObject(playerid,2576,802.65832520,-2721.89453125,99.87871552,0.00000000,0.00000000,268.25000000,_,OBJECT_TYPE_HOUSE);
					case 4: CreatePlayerSpecialObject(playerid,2204,803.02569580,-2722.51171875,99.87871552,0.00000000,0.00000000,268.00000000,_,OBJECT_TYPE_HOUSE);
				}
            }
			if(HouseInfo[hid][hUpgrade][2] >= 1)
			{
            	format(string,sizeof(string),"Funkgerät (Stufe %d)",HouseInfo[hid][hUpgrade][2]);
            	CreatePlayerSpecial3DText(playerid,GRUEN,string,794.812316,-2718.0825,100.8787,_,TEXT3D_TYPE_HOUSE);
				if(HouseInfo[hid][hUpgrade][2] >= 1) CreatePlayerSpecialObject(playerid,3350,794.27294922,-2718.70068359,99.62870026,0.00000000,0.00000000,52.00000000,_,OBJECT_TYPE_HOUSE);
				if(HouseInfo[hid][hUpgrade][2] >= 2) CreatePlayerSpecialObject(playerid,3389,793.16235352,-2718.56079102,98.47863007,0.00000000,0.00000000,0.00000000,_,OBJECT_TYPE_HOUSE);
				if(HouseInfo[hid][hUpgrade][2] >= 3) CreatePlayerSpecialObject(playerid,3386,794.07360840,-2717.69433594,98.90370178,0.00000000,0.00000000,90.00000000,_,OBJECT_TYPE_HOUSE);
            }
			if(HouseInfo[hid][hUpgrade][3] >= 1)
			{
            	format(string,sizeof(string),"Crafting (Stufe %d)",HouseInfo[hid][hUpgrade][3]);
            	CreatePlayerSpecial3DText(playerid,GRUEN,string,807.60809,-2722.68090,100.87871,_,TEXT3D_TYPE_HOUSE);
				switch(HouseInfo[hid][hUpgrade][3])
				{//crafting
					case 1: CreatePlayerSpecialObject(playerid,920,808.91455078,-2722.52612305,100.33852386,0.00000000,0.00000000,270.00000000,_,OBJECT_TYPE_HOUSE);
					case 2: CreatePlayerSpecialObject(playerid,943,808.81292725,-2722.66992188,100.64448547,0.00000000,0.00000000,0.00000000,_,OBJECT_TYPE_HOUSE);
					case 3: CreatePlayerSpecialObject(playerid,929,808.52563477,-2723.13330078,100.71479034,0.00000000,0.00000000,0.00000000,_,OBJECT_TYPE_HOUSE);
				}
            }
			if(HouseInfo[hid][hUpgrade][4] >= 1)
			{
            	format(string,sizeof(string),"Ofen (Stufe %d)",HouseInfo[hid][hUpgrade][4]);
            	CreatePlayerSpecial3DText(playerid,GRUEN,string,801.40643,-2718.34912,100.484703,_,TEXT3D_TYPE_HOUSE);
				switch(HouseInfo[hid][hUpgrade][4])
				{//ofen
					case 1: CreatePlayerSpecialObject(playerid,1362,801.40643,-2718.34912,100.484706,0.00000000,0.00000000,0.00000000,_,OBJECT_TYPE_HOUSE);
				}
			}
			if(HouseInfo[hid][hUpgrade][5] >= 1)
			{
            	format(string,sizeof(string),"Tresor (Stufe %d)",HouseInfo[hid][hUpgrade][5]);
            	CreatePlayerSpecial3DText(playerid,GRUEN,string,801.00,-2727.6999,100.9000,_,TEXT3D_TYPE_HOUSE);
				CreatePlayerSpecialObject(playerid,2332,801.0,-2726.5,100.3000,0.0000,0.0000,0.000,_,OBJECT_TYPE_HOUSE);
            }
        }
    }
	if(_enter == true) w_SetPlayerPos(playerid,h_exit_pos[HouseInfo[hid][hInterior]][0],h_exit_pos[HouseInfo[hid][hInterior]][1],h_exit_pos[HouseInfo[hid][hInterior]][2],h_exit_pos[HouseInfo[hid][hInterior]][3],3);
	return 1;
}

LoadPlayerHouseSpawnSettings(playerid,col_type[])
{
	new hid=-1,rid=mysql_GetIntByInt("pnwl_user_pos",col_type,"userid",PlayerInfo[playerid][PLAYER_ID]);
	if(rid == -1)
	{
		PlayerEvent[playerid][InteractHouse]	= -1;
		PlayerOption[playerid][HouseCache][0]	= -1;
		return 1;
	}
	for(new h_id; h_id<MAX_HOUSES; h_id++)
	{
		if(HouseInfo[h_id][hAktiv] != 1) break;
		if(HouseInfo[h_id][hID] == rid)
		{
			hid = h_id;
			break;
		}
	}
	PlayerEvent[playerid][InteractHouse] = hid;
	if(hid != -1) PlayerOption[playerid][HouseCache][0] = HouseInfo[hid][hID];
	else PlayerOption[playerid][HouseCache][0] = -1;
	return hid;
}

OnHouseExit(playerid,hid)
{
	w_SetPlayerPos(playerid,HouseInfo[hid][hPos][0],HouseInfo[hid][hPos][1],HouseInfo[hid][hPos][2],_,2);
	DeletePlayerSpecial3DText(playerid,TEXT3D_ALL,TEXT3D_TYPE_HOUSE);
	DeletePlayerSpecialObject(playerid,OBJECTS_ALL,OBJECT_TYPE_HOUSE);
	PlayerOption[playerid][HouseCache][0] = -1;
	PlayerEvent[playerid][InteractHouse] = -1;
	if(PlayerEvent[playerid][IstTot] >= 1) SetPlayerVirtualWorld(playerid,1234);
	else SetPlayerVirtualWorld(playerid,0);
	CheckPlayerNoDM(playerid);
	return 1;
}

ShowHouseMainDialog(playerid,hid)
{
	new dlop=0;
	format(string,sizeof(string),"Informationen");
	PlayerOption[playerid][dialog][dlop] = 0;
	dlop++;

	if(PlayerOption[playerid][HouseSelect] == HouseInfo[hid][hID]) 
	{
		format(string,sizeof(string),"%s\nNicht länger hier übernachten",string);
		PlayerOption[playerid][dialog][dlop] = 1;
		dlop++;
	}
	else if(HouseInfo[hid][hMietbar] && HouseInfo[hid][hBewohner] < HouseInfo[hid][hMaxBewohner])
	{
		format(string,sizeof(string),"%s\nHier übernachten (%d/%d belegt)",string,HouseInfo[hid][hBewohner],HouseInfo[hid][hMaxBewohner]);
		PlayerOption[playerid][dialog][dlop] = 1;
		dlop++;
	}
	
	if(HouseInfo[hid][hOwner] == PlayerInfo[playerid][PLAYER_ID])
	{//Owner einstellungen
		format(string,sizeof(string),"%s\nUpgrades",string);
		PlayerOption[playerid][dialog][dlop] = 2;
		dlop++;							
		
		if(HouseInfo[hid][hMietbar])
		{
			if(HouseInfo[hid][hBewohner] >= 1)
			{
				format(string,sizeof(string),"%s\nBewohner (%d) rauswerfen",string,HouseInfo[hid][hBewohner]);
				PlayerOption[playerid][dialog][dlop] = 3;
				dlop++;
			}
			format(string,sizeof(string),"%s\nBett für weitere Bewohner sperren (derzeit %d/%d Bewohner)",string,HouseInfo[hid][hBewohner],HouseInfo[hid][hMaxBewohner]);
			PlayerOption[playerid][dialog][dlop] = 4;
			dlop++;
		}
		else
		{
			format(string,sizeof(string),"%s\nBett für Bewohner freigeben",string,HouseInfo[hid][hBewohner]);
			PlayerOption[playerid][dialog][dlop] = 5;
			dlop++;
		}
	}
	
	format(string,sizeof(string),"{F1F7DC}%s",string);
	format(query,sizeof(query),"{F1F7DC}Bett Stufe %d",HouseInfo[hid][hUpgrade][0]);
	house_ShowPlayerDialog(playerid,dialog_HOUSE_BETT_MAIN,DIALOG_STYLE_LIST,query,string,"Weiter","Zurück");
	return 1;
}

stock HouseUnrentAll(hid)
{
	HouseInfo[hid][hBewohner] = 0;
	format(query,sizeof(query),"UPDATE pnwl_houses SET bewohner='0' WHERE id='%d'",HouseInfo[hid][hID]);
	mysql_query_once(query);
	format(query,sizeof(query),"UPDATE pnwl_user_pos SET log_house='-1',logpos1='%0.2f',logpos2='0.2f',logpos3='%0.2f' WHERE log_house='%d'",HouseInfo[hid][hPos][0],HouseInfo[hid][hPos][1],HouseInfo[hid][hPos][2],HouseInfo[hid][hID]);
	mysql_query_once(query);
	format(query,sizeof(query),"UPDATE pnwl_user_option SET selectedspawn='1',houseselect='-1' WHERE houseselect='%d'",HouseInfo[hid][hID]);
	mysql_query_once(query);
	for(new i; i<=__SERVER[HIGHEST_PLAYER_ID]; i++)
	{
		if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
		if(PlayerOption[i][HouseSelect] == HouseInfo[hid][hID])
		{
			PlayerOption[i][HouseSelect]	= -1;
			PlayerOption[i][SelectedSpawn]	=  1;
			if(PlayerInfo[i][PLAYER_ID] == HouseInfo[hid][hOwner]) continue;
			SendClientMessage(i,GRUEN,#FORMAT_CHAT_INFO"Du wurdest aus dem Haus geworfen!");
		}
	}
	return 1;
}




