/*
	PostNuke Wasteland Craftingengine
	Author:	NeoPhoenix
	Date:	03.02.2012
*/

//@lib_item required
#include "libraries/lib_item"

/*
	native craft_GetCraftRecipie(item_0,anzahl_0,item_1,anzahl_1);
	native craft_GetFurnaceRecipie(item_id);
	native Float:craft_GetRangeToCraft(playerid,craft_id);
	native Float:craft_GetRangeToFurnace(playerid,ofen_id);
	native craft_GetNearestCraft(playerid,Float:range=AV_CRAFT_DIST);
	native craft_GetNearestFurnace(playerid,Float:range=AV_FURNACE_DIST);
	native craft_AddItemToCraft(playerid,itemid,anzahl);
	native craft_AddItemToFurnace(playerid,itemid,anzahl);
	native craft_UpdatePlayerFurnace(playerid);
	native craft_UpdatePlayerCraft(playerid);
	native craft_DisplayFurnaceDialog(playerid);
	native craft_DisplayCraftDialog(playerid);
	native craft_GetNextFurnaceLevel(playerid,ofen);
	native craft_GetNextCraftLevel(playerid,craft);
*/

#define MAX_FURNACE_RECIPIES	(2)
#define MAX_CRAFT_RECIPIES		(2)
#define MAX_FURNACES			(1)
#define MAX_CRAFTINGS			(2)
#define AV_FURNACE_DIST			(5.0)
#define AV_CRAFT_DIST			(5.0)

enum (+=1)
{
	dialog_FURNACE_MAINRE = 1,
	dialog_FURNACE_MAIN,
	dialog_FURNACE_ITEM_SPEC,
	dialog_FURNACE_ITEM_SPEC_ANZAHL,
	dialog_FURNACE_ACTIVATE_MAIN,

	dialog_CRAFT_MAINRE,
	dialog_CRAFT_MAIN,
	dialog_CRAFT_ITEM_MAIN,
	dialog_CRAFT_ITEM_SPEC,
	dialog_CRAFT_ITEM_SPEC_ANZAHL,
	dialog_CRAFT_ACTIVATE_MAIN,
}

enum cr_data
{
	item_0_data[2],
	item_1_data[2],
	produkt_data[2],
	required_level,
}
new const craft_recipies[MAX_CRAFT_RECIPIES][cr_data] = {
{{4,2},{7,1},{5,1},0},
{{7,1},{4,2},{5,1},5}
};

enum cr_f_data
{
	base_item,
	new_item[2],
	required_level,
}
new const furnace_recipies[MAX_FURNACE_RECIPIES][cr_f_data] = {
{3,{4,1},0},
{432,{1,1},0}
};

enum furnace_data
{
	Float:f_pos[3],
	f_level,
}
new const Float:furnaces[MAX_FURNACES][furnace_data] = {
{{0.0,0.0,0.0},1}
};

enum crafting_data
{
	Float:c_pos[3],
	c_level
}
new const Float:craftings[MAX_CRAFTINGS][crafting_data] = {
{{-197.757,1114.26,19.3214},1},
{{0.0,0.0,0.0},0}
};

craft_OnCraftInit()
{
	for(new f; f<MAX_FURNACES; f++) 	Create3DTextLabel("Ofen\n/ofen",GRUEN,furnaces[f][f_pos][0],furnaces[f][f_pos][1],furnaces[f][f_pos][2],30.0,0);
	for(new c; c<MAX_CRAFTINGS; c++) 	Create3DTextLabel("Craftingmaschine\n/craft",GRUEN,craftings[c][c_pos][0],craftings[c][c_pos][1],craftings[c][c_pos][2],30.0,0);
	return 1;
}

stock craft_DisplayFurnaceDialog(playerid)
{
	SetPVarInt(playerid,"dialog_crafting",1);
	craft_OnDialogResponse(playerid,dialog_FURNACE_MAINRE,0,0,"");
	return 1;
}
stock craft_DisplayCraftDialog(playerid)
{
	SetPVarInt(playerid,"dialog_crafting",1);
	craft_OnDialogResponse(playerid,dialog_CRAFT_MAINRE,0,0,"");
	return 1;
}

#define craft_ShowPlayerDialog(%0,%1,%2,%3,%4,%5) SetPVarInt(%0,"dialog_crafting",1);\
ShowPlayerDialog(%0,%1,%2,%3,%4,%5)

Float:craft_GetRangeToCraft(playerid,craft) 	
{
	if(craft == -1) return 6000.0;
	if(craft == -2 || PlayerEvent[playerid][InteractHouse] != -1)
	{
		if(HouseInfo[PlayerEvent[playerid][InteractHouse]][hUpgrade][3] <= 0) return 6000.0;//hat kein craftingupgrade
		switch(HouseInfo[PlayerEvent[playerid][InteractHouse]][hInterior])
		{
			case 2:
			{
			}
			default: return GetPlayerDistanceFromPoint(playerid,807.60809,-2722.68090,100.87871);
		}
		return 6000.0;
	}
	if(craft >= 0)
	{
		if(craft >= MAX_CRAFTINGS) return 6000.0;
		return GetPlayerDistanceFromPoint(playerid,craftings[craft][c_pos][0],craftings[craft][c_pos][1],craftings[craft][c_pos][2]);
	}
	return 6000.0;
}

Float:craft_GetRangeToFurnace(playerid,ofen)
{
	if(ofen == -1) return 6000.0;
	if(ofen == -2 || PlayerEvent[playerid][InteractHouse] != -1)
	{
		if(HouseInfo[PlayerEvent[playerid][InteractHouse]][hUpgrade][4] <= 0) return 6000.0;//hat kein ofenupgrade
		switch(HouseInfo[PlayerEvent[playerid][InteractHouse]][hInterior])
		{
			case 2:
			{
			}
			default: return GetPlayerDistanceFromPoint(playerid,801.40643,-2718.34912,100.484703);
		}
		return 6000.0;
	}
	if(ofen >= 0)
	{
		if(ofen >= MAX_FURNACES) return 6000.0;
		return GetPlayerDistanceFromPoint(playerid,furnaces[ofen][f_pos][0],furnaces[ofen][f_pos][1],furnaces[ofen][f_pos][2]);
	}
	return 6000.0;
}

stock craft_GetNearestCraft(playerid,Float:range=AV_CRAFT_DIST)
{
	if(PlayerEvent[playerid][InteractHouse] != -1)
	{
		if(HouseInfo[PlayerEvent[playerid][InteractHouse]][hUpgrade][3] <= 0) return -1;//hat kein craftingupgrade
		switch(HouseInfo[PlayerEvent[playerid][InteractHouse]][hInterior])
		{
			case 2:
			{
			}
			default: if(IsPlayerInRangeOfPoint(playerid,range,807.60809,-2722.68090,100.87871)) return -2;//steht crafting
		}
		return -1;//Steht nicht am crafting
	}
	new Float:nearest_dist = 6000.0,
		Float:tmp_dist,
		nearest_id = -1;
	for(new c; c<MAX_CRAFTINGS; c++)
	{
		tmp_dist = GetPlayerDistanceFromPoint(playerid,craftings[c][c_pos][0],craftings[c][c_pos][1],craftings[c][c_pos][2]);
		if(tmp_dist < range && tmp_dist < nearest_dist)
		{
			nearest_dist = tmp_dist;
			nearest_id = c;
			continue;
		}
	}
	return nearest_id;
}

stock craft_GetNearestFurnace(playerid,Float:range=AV_FURNACE_DIST)
{
	if(PlayerEvent[playerid][InteractHouse] != -1)
	{
		if(HouseInfo[PlayerEvent[playerid][InteractHouse]][hUpgrade][4] <= 0) return -1;//hat kein ofenupgrade
		switch(HouseInfo[PlayerEvent[playerid][InteractHouse]][hInterior])
		{
			case 2:
			{
			}
			default: if(IsPlayerInRangeOfPoint(playerid,range,801.40643,-2718.34912,100.484703)) return -2;//steht am ofen
		}
		return -1;//Steht nicht am ofen
	}
	new Float:nearest_dist = 6000.0,
		Float:tmp_dist,
		nearest_id = -1;
	for(new c; c<MAX_FURNACES; c++)
	{
		tmp_dist = GetPlayerDistanceFromPoint(playerid,furnaces[c][f_pos][0],furnaces[c][f_pos][1],furnaces[c][f_pos][2]);
		if(tmp_dist < range && tmp_dist < nearest_dist)
		{
			nearest_dist = tmp_dist;
			nearest_id = c;
			continue;
		}
	}
	return nearest_id;
}

stock craft_AddItemToCraft(playerid,itemid,anzahl)// -1 = falsche itemid, -2 = x, -3 = zu wenig platz, -4 = zu wenig items
{
	if(itemid <= 0 || itemid >= MAX_ITEMS) return -1;
	if(GetPVarInt(playerid,"craft_craft_0_id") != 0 && GetPVarInt(playerid,"craft_craft_0_id") != itemid && GetPVarInt(playerid,"craft_craft_1_id") != 0 && GetPVarInt(playerid,"craft_craft_1_id") != itemid) return -3;
	if(anzahl <= 0) return -4;

	if(GetPVarInt(playerid,"craft_craft_0_id") == 0 || GetPVarInt(playerid,"craft_craft_0_id") == itemid)
	{
		SetPVarInt(playerid,"craft_craft_0_id",itemid);
		SetPVarInt(playerid,"craft_craft_0_anzahl",GetPVarInt(playerid,"craft_craft_0_anzahl")+anzahl);
	}
	else if(GetPVarInt(playerid,"craft_craft_1_id") == 0 || GetPVarInt(playerid,"craft_craft_1_id") == itemid)
	{
		SetPVarInt(playerid,"craft_craft_1_id",itemid);
		SetPVarInt(playerid,"craft_craft_1_anzahl",GetPVarInt(playerid,"craft_craft_1_anzahl")+anzahl);	
	}
	else return -2;
	return 1;
}

stock craft_GetNextFurnaceLevel(playerid,ofen)
{
	new hid = PlayerEvent[playerid][InteractHouse];
	if(hid == -1)
	{
		if(ofen < 0 || ofen >= MAX_FURNACES) return 0;
		return furnaces[ofen][f_level];
	}
	return HouseInfo[hid][hUpgrade][4];
}

stock craft_GetNextCraftLevel(playerid,craft)
{
	new hid = PlayerEvent[playerid][InteractHouse];
	if(hid == -1)
	{
		if(craft < 0 || craft >= MAX_CRAFTINGS) return 0;
		return craftings[craft][c_level];
	}
	return HouseInfo[hid][hUpgrade][3];
}

stock craft_AddItemToFurnace(playerid,itemid,anzahl)// -1 = falsche itemid, -2 = x, -3 = zu wenig platz, -4 = zu wenig items
{
	if(itemid <= 0 || itemid >= MAX_ITEMS) return -1;
	if(anzahl <= 0) return -4;

	SetPVarInt(playerid,"craft_furnace_id",itemid);
	if(GetPVarInt(playerid,"craft_furnace_id") == itemid) SetPVarInt(playerid,"craft_furnace_anzahl",GetPVarInt(playerid,"craft_furnace_anzahl")+anzahl);
	else SetPVarInt(playerid,"craft_furnace_anzahl",anzahl);
	
	return 1;
}

stock craft_GetFurnaceRecipie(item_id)
{
	for(new c; c<MAX_FURNACE_RECIPIES; c++) if(furnace_recipies[c][base_item] == item_id) return c;
	return -1;
}

stock craft_GetCraftRecipie(item_0,anzahl_0,item_1,anzahl_1)// -1 = rezept existiert nicht, x{0-MAX_CRAFT_RECIPIES} = rezeptid
{
	if(item_0 == 0 || anzahl_0 == 0 || item_1 == 0 || anzahl_1 == 0) return -1;
	for(new r; r<MAX_CRAFT_RECIPIES; r++)
	{
		if(craft_recipies[r][item_0_data][0] == item_0 && craft_recipies[r][item_1_data][0] == item_1)
		{//item ids passen
			if(craft_recipies[r][item_0_data][1] == anzahl_0 && craft_recipies[r][item_1_data][1] == anzahl_1)
			{//anzahl passt auch
				return r;
			}
		}
		//Umgekehrte slots
		if(craft_recipies[r][item_1_data][0] == item_0 && craft_recipies[r][item_0_data][0] == item_1)
		{//item ids passen
			if(craft_recipies[r][item_1_data][1] == anzahl_0 && craft_recipies[r][item_0_data][1] == anzahl_1)
			{//anzahl passt auch
				return r;
			}
		}		
	}
	return -1;
}

craft_UpdatePlayerFurnace(playerid)
{
	new item_id 	= GetPVarInt(playerid,"craft_furnace_id"),
		item_anzahl	= GetPVarInt(playerid,"craft_furnace_anzahl");
	if(item_id && item_anzahl <= 0 || item_anzahl > item::CountPlayerItems(playerid,item_id))
	{
		SetPVarInt(playerid,"craft_furnace_id",0);
		SetPVarInt(playerid,"craft_furnace_anzahl",0);
	}
	return ;
}

craft_UpdatePlayerCraft(playerid)
{
	new item_id 	= GetPVarInt(playerid,"craft_craft_0_id"),
		item_anzahl	= GetPVarInt(playerid,"craft_craft_0_anzahl");
	if(item_id && item_anzahl <= 0 || item_anzahl > item::CountPlayerItems(playerid,item_id))
	{
		SetPVarInt(playerid,"craft_craft_0_id",0);
		SetPVarInt(playerid,"craft_craft_0_anzahl",0);
	}
	item_id 	= GetPVarInt(playerid,"craft_craft_1_id");
	item_anzahl	= GetPVarInt(playerid,"craft_craft_1_anzahl");
	if(item_id && item_anzahl <= 0 || item_anzahl > item::CountPlayerItems(playerid,item_id))
	{
		SetPVarInt(playerid,"craft_craft_1_id",0);
		SetPVarInt(playerid,"craft_craft_1_anzahl",0);
	}
	return ;
}

craft_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(GetPVarInt(playerid,"dialog_crafting") != 1) return 0;
	SetPVarInt(playerid,"dialog_crafting",0);
	DeletePVar(playerid,"dialog_crafting");
	if(dialogid == dialog_NULL) return 1;
	switch(dialogid)
	{
		case dialog_FURNACE_MAINRE:
		{//menü öffnen
			format(string,sizeof(string),"{F1F7DC}Tutorial für den Ofen\nItems verwalten");
			
			if(GetPVarInt(playerid,"craft_furnace_anzahl") >= 1) format(string,sizeof(string),"%s ({C87E35}voll",string);
			else format(string,sizeof(string),"%s ({77AE35}leer",string);			
			format(string,sizeof(string),"%s{F1F7DC})\nOfen anheizen",string);
			
			craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAIN,DIALOG_STYLE_LIST,"{F1F7DC}Ofen",string,"Weiter","Beenden");
			return 1;
		}
		case dialog_FURNACE_MAIN:
		{
			if(!response) return 1;//menü schließen
			switch(listitem)
			{
				case 0://ofentutorial
				{				
					format(string,sizeof(string),"{F1F7DC}\
					Hallo.\n\
					Ich werde dir kurz erklären, die du den Ofen richtig und effektiv\n\
					benutzen kannst. Bevor du den Ofen anheizen kannst musst du einige\n\
					Items mit \"/item\" reinschieben - genauergesagt zwei Sorten von Items.\n\
					Als erstes das Item, das du verarbeiten möchtest (dem Ofen geben), dann");				
					format(string,sizeof(string),"%s\n\
					brennbares Item benutzen um den Ofen anzuheizen (das kennst du sicherlich aus Minecraft).\n\
					Dann kannst du unter \"Ofen anheizen\" den Prozess starten, das Item wird\n\
					eingeschmolzen und du erhältst ein neues Produkt. Easy...",string);					
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Tutorial zum Ofen",string,"Zurück","");
				}
				case 1://items verwalten
				{
					new anzahl = GetPVarInt(playerid,"craft_furnace_anzahl");
					if(anzahl == 0)
					{//slot ist leer
						craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Ofen - Item verwalten","{F1F7DC}Dieser Slot ist zur Zeit leer. Um ein Item in diesem\nSlot zu lagern verwende bitte den Command \"/item\", wähle das gewünschte\nItem aus und \"gebe\" es dem Ofen.","Zurück","");
						return 1;
					}
					format(query,sizeof(query),"{F1F7DC}Anzahl von \"%s\" ändern (derzeit %d)\nKomplett entfernen",item_ItemInfo[GetPVarInt(playerid,"craft_furnace_id")][iName],anzahl);
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_ITEM_SPEC,DIALOG_STYLE_LIST,"{F1F7DC}Ofen - Item verwalten",query,"Weiter","Zurück");
				}
				case 2://ofen anheizen
				{
					new bool:ready = false;
					if(GetPVarInt(playerid,"craft_furnace_anzahl") >= 1) 
					{
						ready = true;
						format(string,sizeof(string),"{F1F7DC}Items:  %dx %s (%s)",GetPVarInt(playerid,"craft_furnace_anzahl"),item_ItemInfo[GetPVarInt(playerid,"craft_furnace_id")][iName],item_ItemInfo[GetPVarInt(playerid,"craft_furnace_id")][iProperties]&iBurns?("{C85135}brennbar{F1F7DC}"):("nicht brennbar"));
					}
					else format(string,sizeof(string),"{F1F7DC}Items: {C85135}(leer)");					
							
					if(ready) 
					{
						format(string,sizeof(string),"%s\n\n{F1F7DC}Hinweis: Alles ist in Ordnung, du kannst den Ofen\nnun anheizen.",string);
						craft_ShowPlayerDialog(playerid,dialog_FURNACE_ACTIVATE_MAIN,DIALOG_STYLE_MSGBOX,"{F1F7DC}Ofen anheizen",string,"Anheizen","Zurück");
					}
					else 
					{
						format(string,sizeof(string),"%s\n\n{F1F7DC}Du musst Items in den Ofen schieben!",string);
						craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Ofen anheizen",string,"Zurück","");
					}
					return 1;
				}
			}
			return 1;
		}
		case dialog_FURNACE_ACTIVATE_MAIN:
		{
			new craft_item = GetPVarInt(playerid,"craft_furnace_id"),
				craft_anzahl = GetPVarInt(playerid,"craft_furnace_anzahl");
			if(craft_item != 0)
			{
				new recipie = craft_GetFurnaceRecipie(craft_item);
				if(recipie == -1)
				{//das rezept gibt es nicht
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Ofen anheizen","{C85135}Dieses Item kann nicht eingeschmolzen werden.","Zurück","");
					return 1;
				}
				new ofen = craft_GetNearestFurnace(playerid);
				if(craft_GetNextFurnaceLevel(playerid,ofen) < furnace_recipies[recipie][required_level])
				{//level des ofens ist nicht ausreichend
					format(string,sizeof(string),"{C85135}Das Level des Ofens ist nicht hoch genug.\n{F1F7DC}Benötigtes Level: {C85135}%d\n{F1F7DC}Level des Ofens: {C85135}%d",furnace_recipies[recipie][required_level],craft_GetNextFurnaceLevel(playerid,ofen));
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Ofen anheizen",string,"Zurück","");
					return 1;				
				}

				//craft_UpdatePlayerFurnace(playerid); <-- bereits in item_RemovePlayerItem enthalten
				
				new result = item_GivePlayerItem(playerid,furnace_recipies[recipie][new_item][0],furnace_recipies[recipie][new_item][1]*craft_anzahl);// -1 = falsche itemid, -2 = offline, -3 = zu wenig platz, -4 = zu wenig items
				if(result == 1)
				{//erfolg
					format(query,sizeof(query),"{F1F7DC}Erfolg: Du hast durch das Verfahren %dx %s erhalten.",craft_anzahl,item_ItemInfo[furnace_recipies[recipie][new_item][0]][iName]);
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Erfolg",query,"Zurück","");
					item_RemovePlayerItems(playerid,craft_item,craft_anzahl);
				}
				else
				{//fehler
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Ofen - Verfahren gescheitert","{C85135}Hinweis: Das Verfahren ist leider gescheitert. Eventuell hast\ndu zu wenig Platz im Rucksack.","Zurück","");
				}
			}
			return 1;
		}
		case dialog_FURNACE_ITEM_SPEC:
		{			
			if(!response)
			{//Zurück zur Itemverwaltung (MAIN)
				SetPVarInt(playerid,"dialog_crafting",1);
				craft_OnDialogResponse(playerid,dialog_FURNACE_MAIN,1,1,"");
				return 1;
			}
			switch(listitem)
			{
				case 0:
				{//itemanzahl ändern
					new anzahl = GetPVarInt(playerid,"craft_furnace_anzahl");
					new item_id = GetPVarInt(playerid,"craft_furnace_id");					
					format(query,sizeof(query),"Anzahl des Items %s (%s) ändern.\nJetzige Anzahl:\t%d\nMinimale Anzahl:\t1\nMaximale Anzahl:\t%d",item_ItemInfo[item_id][iName],item_ItemInfo[item_id][iProperties]&iBurns?("{C85135}brennbar{F1F7DC}"):("nicht brennbar"),anzahl,item::CountPlayerItems(playerid,item_id));
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_ITEM_SPEC_ANZAHL,DIALOG_STYLE_INPUT,"{F1F7DC}Ofen - Itemanzahl ändern",query,"Weiter","Zurück");
				}
				case 1:
				{//item komplett entfernen
					new item_id = GetPVarInt(playerid,"craft_furnace_id");					
					format(query,sizeof(query),"{F1F7DC}Du hast das Item %s aus dem Ofen entfernt",item_ItemInfo[item_id][iName]);
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item entfernt",query,"Zurück","");
					DeletePVar(playerid,"craft_furnace_anzahl");
					DeletePVar(playerid,"craft_furnace_id");
					return 1;
				}
			}
		}
		case dialog_FURNACE_ITEM_SPEC_ANZAHL:
		{
			if(response)
			{
				new anzahl = GetPVarInt(playerid,"craft_furnace_anzahl");
				new item_id 	= GetPVarInt(playerid,"craft_furnace_id"),
					max_anzahl	= item::CountPlayerItems(playerid,item_id),
					neu_anzahl 	= strval(inputtext);
				if(neu_anzahl <= 0 || neu_anzahl > max_anzahl)
				{//ungültige eingabe
					format(query,sizeof(query),"Anzahl des Items %s (%s) ändern.\nJetzige Anzahl:\t%d\nMinimale Anzahl:\t1\nMaximale Anzahl:\t%d",item_ItemInfo[item_id][iName],item_ItemInfo[item_id][iProperties]&iBurns?("{C85135}brennbar{F1F7DC}"):("nicht brennbar"),anzahl,max_anzahl);
					craft_ShowPlayerDialog(playerid,dialog_FURNACE_ITEM_SPEC_ANZAHL,DIALOG_STYLE_INPUT,"{F1F7DC}Ofen - Itemanzahl ändern",query,"Weiter","Zurück");
					return 1;
				}
				//eingabe korrekt
				SetPVarInt(playerid,"craft_furnace_anzahl",neu_anzahl);
			}
			//Zurück zur verwaltung des slots X
			SetPVarInt(playerid,"dialog_crafting",1);
			craft_OnDialogResponse(playerid,dialog_FURNACE_MAIN,1,1,"");
			return 1;
		}
		case dialog_CRAFT_MAINRE:
		{//menü öffnen
			format(string,sizeof(string),"{F1F7DC}Tutorial für die Craftingmaschine\nItems verwalten");
			
			new slots_used = 0;
			if(GetPVarInt(playerid,"craft_craft_0_anzahl") >= 1) slots_used++;
			if(GetPVarInt(playerid,"craft_craft_1_anzahl") >= 1) slots_used++;
			
			if(slots_used == 1) format(string,sizeof(string),"%s ({C87E35}1/2 Slots belegt",string,slots_used);
			else if(slots_used == 2) format(string,sizeof(string),"%s ({C85135}2/2 Slots belegt",string,slots_used);
			else format(string,sizeof(string),"%s ({77AE35}leer",string);			
			format(string,sizeof(string),"%s{F1F7DC})\nMaschine starten",string);
			
			craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAIN,DIALOG_STYLE_LIST,"{F1F7DC}Craftingmaschine",string,"Weiter","Beenden");
			return 1;
		}
		case dialog_CRAFT_MAIN:
		{
			if(!response) return 1;//menü schließen
			switch(listitem)
			{
				case 0://crafttutorial
				{				
					format(string,sizeof(string),"{F1F7DC}\
					Hallo.\n\
					Ich werde dir kurz erklären, die du die Craftingmaschine richtig und effektiv\n\
					benutzen kannst. Bevor du die Maschine starten kannst musst du einige\n\
					Items mit \"/item\" reinschieben - genauergesagt zwei verschiedene Items.\n\
					Dann einfach auf \"Maschine starten\" klicken und neue Items werden generiert.");				
					format(string,sizeof(string),"%s\n\
					Dabei gehen die verwendeten Items leider kaputt. Es gibt verschiedene Rezepte,\n\
					dabei ist wichtig, dass die Itemanzahl exakt mit der des Rezeptes übereinstimmt.",string);					
					craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Tutorial zum Crafting",string,"Zurück","");
				}
				case 1://items verwalten
				{
					if(GetPVarInt(playerid,"craft_craft_0_anzahl") >= 1) format(string,sizeof(string),"{F1F7DC}%dx %s",GetPVarInt(playerid,"craft_craft_0_anzahl"),item_ItemInfo[GetPVarInt(playerid,"craft_craft_0_id")][iName]);
					else format(string,sizeof(string),"{C85135}(leer)");
					
					if(GetPVarInt(playerid,"craft_craft_1_anzahl") >= 1) format(string,sizeof(string),"%s\n{F1F7DC}%dx %s",string,GetPVarInt(playerid,"craft_craft_1_anzahl"),item_ItemInfo[GetPVarInt(playerid,"craft_craft_1_id")][iName]);
					else format(string,sizeof(string),"%s\n{C85135}(leer)",string);
					
					craft_ShowPlayerDialog(playerid,dialog_CRAFT_ITEM_MAIN,DIALOG_STYLE_LIST,"{F1F7DC}Crafting - Items verwalten",string,"Weiter","Zurück");
				}
				case 2://maschine starten
				{
					new items_stores = 0, bool:ready = false;
					if(GetPVarInt(playerid,"craft_craft_0_anzahl") >= 1) 
					{
						items_stores++;
						format(string,sizeof(string),"{F1F7DC}Slot 1: %dx %s",GetPVarInt(playerid,"craft_craft_0_anzahl"),item_ItemInfo[GetPVarInt(playerid,"craft_craft_0_id")][iName]);
					}
					else format(string,sizeof(string),"{F1F7DC}Slot 1: {C85135}(leer)");
					
					if(GetPVarInt(playerid,"craft_craft_1_anzahl") >= 1) 
					{
						items_stores++;
						format(string,sizeof(string),"%s\n{F1F7DC}Slot 2: %dx %s",string,GetPVarInt(playerid,"craft_craft_1_anzahl"),item_ItemInfo[GetPVarInt(playerid,"craft_craft_1_id")][iName]);
					}
					else format(string,sizeof(string),"%s\n{F1F7DC}Slot 2: {C85135}(leer)",string);

					if(items_stores != 2)
					{//zu wenig items gelagert
						format(string,sizeof(string),"%s\n\n{C85135}INFO: Du musst beide Itemslots benutzen.",string);
					}
					else
					{//genug items gelagert
						format(string,sizeof(string),"%s\n\n{F1F7DC}Hinweis: Alles ist in Ordnung, du kannst die Maschine\n           nun starten.",string);
						ready = true;
					}					
					if(ready) 
					{
						craft_ShowPlayerDialog(playerid,dialog_CRAFT_ACTIVATE_MAIN,DIALOG_STYLE_MSGBOX,"{F1F7DC}Maschine starten",string,"Starten","Zurück");
					}
					else 
					{
						craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Maschine starten",string,"Zurück","");
					}
					return 1;
				}
			}
			return 1;
		}
		case dialog_CRAFT_ACTIVATE_MAIN:
		{
			new	item_id[2],
				item_anzahl[2];
			item_id[0] 		= GetPVarInt(playerid,"craft_craft_0_id");
			item_id[1] 		= GetPVarInt(playerid,"craft_craft_1_id");
			item_anzahl[0] 	= GetPVarInt(playerid,"craft_craft_0_anzahl");
			item_anzahl[1] 	= GetPVarInt(playerid,"craft_craft_1_anzahl");
			
			new recipie = craft_GetCraftRecipie(item_id[0],item_anzahl[0],item_id[1],item_anzahl[1]);
			if(recipie == -1)
			{//das rezept gibt es nicht
				craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Maschine starten","{C85135}Es konnte kein Rezept dafür gefunden werden.","Zurück","");
				return 1;
			}
			new craft = craft_GetNearestCraft(playerid);
			if(craft_GetNextCraftLevel(playerid,craft) < craft_recipies[recipie][required_level])
			{//level der maschine ist nicht ausreichend
				format(string,sizeof(string),"{C85135}Das Level der Maschine ist nicht hoch genug.\n{F1F7DC}Benötigtes Level: {C85135}%d\n{F1F7DC}Level der Maschine:  {C85135}%d",craft_recipies[recipie][required_level],craft_GetNextCraftLevel(playerid,craft));
				craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Maschine starten",string,"Zurück","");
				return 1;
			}
			
			SetPVarInt(playerid,"craft_craft_0_anzahl",item_anzahl[0]-craft_recipies[recipie][item_0_data][1]);
			SetPVarInt(playerid,"craft_craft_1_anzahl",item_anzahl[1]-craft_recipies[recipie][item_1_data][1]);
			item::RemovePlayerItems(playerid,item_id[0],craft_recipies[recipie][item_0_data][1]);
			item::RemovePlayerItems(playerid,item_id[1],craft_recipies[recipie][item_1_data][1]);
			//craft_UpdatePlayerCraft(playerid); <-- bereits in item_RemovePlayerItems enthalten
			
			new result = item_GivePlayerItem(playerid,craft_recipies[recipie][produkt_data][0],craft_recipies[recipie][produkt_data][1]);// -1 = falsche itemid, -2 = offline, -3 = zu wenig platz, -4 = zu wenig items
			if(result == 1)
			{//erfolg
				format(query,sizeof(query),"{F1F7DC}Erfolg: Du hast durch das Verfahren %dx %s erhalten.",craft_recipies[recipie][produkt_data][1],item_ItemInfo[craft_recipies[recipie][produkt_data][0]][iName]);
				craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Erfolg",query,"Zurück","");
			}
			else
			{//fehler
				craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Craft - Verfahren gescheitert","{C85135}Hinweis: Das Verfahren ist leider gescheitert. Eventuell hast\n         du zu wenig Platz im Rucksack.","Zurück","");
				item_GivePlayerItem(playerid,item_id[0],craft_recipies[recipie][item_0_data][1],true,false);
				item_GivePlayerItem(playerid,item_id[1],craft_recipies[recipie][item_1_data][1],true,false);
			}
			return 1;
		}
		case dialog_CRAFT_ITEM_MAIN:
		{
			if(!response) 
			{
				craft_DisplayCraftDialog(playerid);
				return 1;
			}
			format(query,sizeof(query),"craft_craft_%d_anzahl",listitem);
			SetPVarInt(playerid,"cr_managing_craft_item",listitem);
			new anzahl = GetPVarInt(playerid,query);
			if(anzahl == 0)
			{//slot ist leer
				craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Craft - Item verwalten","{F1F7DC}Dieser Slot ist zur Zeit leer. Um ein Item in diesem\nSlot zu lagern verwende bitte den Command \"/item\", wähle das gewünschte\nItem aus und \"gebe\" es in die Maschine.","Zurück","");
				return 1;
			}
			format(query,sizeof(query),"{F1F7DC}Anzahl ändern (derzeit %d)\nKomplett entfernen",anzahl);
			craft_ShowPlayerDialog(playerid,dialog_CRAFT_ITEM_SPEC,DIALOG_STYLE_LIST,"{F1F7DC}Craft - Item verwalten",query,"Weiter","Zurück");
			return 1;
		}
		case dialog_CRAFT_ITEM_SPEC:
		{			
			if(!response)
			{//Zurück zur Itemverwaltung (MAIN)
				DeletePVar(playerid,"cr_managing_craft_item");
				SetPVarInt(playerid,"dialog_crafting",1);
				craft_OnDialogResponse(playerid,dialog_CRAFT_MAIN,1,1,"");
				return 1;
			}
			new slot = GetPVarInt(playerid,"cr_managing_craft_item");
			switch(listitem)
			{
				case 0:
				{//itemanzahl ändern
					format(query,sizeof(query),"craft_craft_%d_anzahl",slot);
					new anzahl = GetPVarInt(playerid,query);
					format(query,sizeof(query),"craft_craft_%d_id",slot);
					new item_id = GetPVarInt(playerid,query);
					
					format(query,sizeof(query),"Anzahl des Items %s ändern.\nJetzige Anzahl:\t%d\nMinimale Anzahl:\t1\nMaximale Anzahl:\t %d",item_ItemInfo[item_id][iName],anzahl,item::CountPlayerItems(playerid,item_id));
					craft_ShowPlayerDialog(playerid,dialog_CRAFT_ITEM_SPEC_ANZAHL,DIALOG_STYLE_INPUT,"{F1F7DC}Crafting - Itemanzahl ändern",query,"Weiter","Zurück");
				}
				case 1:
				{//item komplett entfernen
					format(query,sizeof(query),"craft_craft_%d_id",slot);
					new item_id = GetPVarInt(playerid,query);
					
					format(query,sizeof(query),"{F1F7DC}Du hast das Item %s aus dem Slot %d entfernt",item_ItemInfo[item_id][iName],slot);
					craft_ShowPlayerDialog(playerid,dialog_CRAFT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item entfernt",query,"Zurück","");

					DeletePVar(playerid,"cr_managing_craft_item");
					format(query,sizeof(query),"craft_craft_%d_anzahl",slot);
					SetPVarInt(playerid,query,0);
					format(query,sizeof(query),"craft_craft_%d_id",slot);
					SetPVarInt(playerid,query,0);
					return 1;
				}
			}
		}
		case dialog_CRAFT_ITEM_SPEC_ANZAHL:
		{
			if(response)
			{
				new slot = GetPVarInt(playerid,"cr_managing_craft_item");	
				format(query,sizeof(query),"craft_craft_%d_anzahl",slot);
				new anzahl = GetPVarInt(playerid,query);
				format(query,sizeof(query),"craft_craft_%d_id",slot);
				new item_id = GetPVarInt(playerid,query),
					has_item_count = item::CountPlayerItems(playerid,item_id),
					neu_anzahl = strval(inputtext);
				if(neu_anzahl <= 0 || neu_anzahl > has_item_count)
				{//ungültige eingabe
					format(query,sizeof(query),"Anzahl des Items %s ändern.\nJetzige Anzahl:   %d\nMinimale Anzahl:  1\nMaximale Anzahl:  %d",item_ItemInfo[item_id][iName],anzahl,has_item_count);
					craft_ShowPlayerDialog(playerid,dialog_CRAFT_ITEM_SPEC_ANZAHL,DIALOG_STYLE_INPUT,"{F1F7DC}Crafting - Itemanzahl ändern",query,"Weiter","Zurück");
					return 1;
				}
				//eingabe korrekt
				format(query,sizeof(query),"craft_craft_%d_anzahl",slot);
				SetPVarInt(playerid,query,neu_anzahl);
			}
			//Zurück zur verwaltung des slots X
			DeletePVar(playerid,"cr_managing_craft_item");
			SetPVarInt(playerid,"dialog_crafting",1);
			craft_OnDialogResponse(playerid,dialog_CRAFT_ITEM_MAIN,1,GetPVarInt(playerid,"cr_managing_craft_item"),"");
			return 1;
		}		
	}
	return 1;
}





