/*
	PostNuke Wasteland Questsystem
	Author: NeoPhoenix
*/

#if defined pnwl_quest_included
	#endinput
#else
	#define pnwl_quest_included
	#define quest:: quest_
#endif
#include <a_samp>
/*
	native quest::ShowPlayerList(playerid);
	native quest::PlayerQuestSlotByQuestID(playerid,questid);
	native quest::QuestMenu(playerid,questid);
	native quest::Manage(playerid,questid);
	native quest::AddPlayerQuest(playerid,questid,step=0,bool:_show=true,bool:_save=true);
	native quest::RemovePlayerQuest(playerid,qslot,bool:_save=true);
	native quest::LoadPlayerQuests(playerid);
	native quest::HasPlayerQuest(playerid,questid);
	native quest::QuestIdByName(qname[]);
	native quest::CountPlayerQuests(playerid);
	native quest::OnRemoveQuest(playerid,qslot);
	native quest::OnNewQuest(playerid,questid,step);
	native quest::OnQuestUpdate(playerid,questid,step);
	native quest::OnQuestFinish(playerid,questid);	
*/

//			Questsys
//Definitions
#define MAX_QUESTS_ACTIVE 			(5)
#define MAX_QUESTS          		(6)
#define MAX_QUEST_BELOHNUNG			(3)
//Dialoge
enum (+=1)
{
	dialog_QUEST_LISTRE = 1,
	dialog_QUEST_LIST,
	dialog_QUEST_MAIN,
	dialog_QUEST_MAINRE,
	dialog_QUEST_MARK_AS_ACTIVE,
	dialog_QUEST_DONATOR,
	dialog_QUEST_BELOHNUNG,
	dialog_QUEST_REMOVE,
}
	
#define qShowPlayerDialog(%0,%1) SetPVarInt(playerid,"q_dialog",1); ShowPlayerDialog(%0,%1)

enum q_quest
{
	qName[60],//Der Name der Quest (Titel)
	qDifficulty,//Schwierigkeitsgrad
	qQuestgeberName[24],//Der Name des Questgebers
	qQuestgeberID,//SystemID des Questgebers
	bool:qWdh,//Ob die Quest wiederholbar ist
	bool:qRemovable,//Ob die Quest entfernt werden kann
}
new QuestInfo[MAX_QUESTS][q_quest] = {
{"Tutorial",			0,	"System",	-1,			false,	false},	//0
{"Jobsuche",			0,	"System",	-1,			false,	false},	//1
{"Leichen verbrennen",	1,	"Mike",		NPC_MIKE,	true,	true},	//2
{"Oelproblem",			1,	"N Jude",	-1,			false,	true},	//3
{"Obdachlos",			0,	"Penis",	-1,			false,	true},	//4
{"Tanklaster",			1,	"Albert",	-1,			true,	true}	//5
};
enum pq_info { qID,qStep }
new PlayerQuest[MAX_PLAYERS][MAX_QUESTS_ACTIVE][pq_info];

enum pq_cache { qID,qAktiv }
new PlayerQuestCache[MAX_PLAYERS][pq_cache];

new QuestDifficulty[][] =
{
	"sehr einfach",
	"einfach",
	"mittel",
	"schwer",
	"sehr schwer"
};

quest::QuestIdByName(qname[])
{
	for(new qidx; qidx<MAX_QUESTS; qidx++) if(!strcmp(qname,QuestInfo[qidx][qName])) return qidx;
	return -1;
}

quest::PlayerQuestSlotByQuestID(playerid,questid)
{
	for(new qidx; qidx<MAX_QUESTS_ACTIVE; qidx++) if(questid == PlayerQuest[playerid][qidx][qID]) return qidx;
	return -1;
}

quest::ShowOptions(playerid,questid)
{
	if(questid < 0 || questid >= MAX_QUESTS) return 1;
	//Body
	if(questid == PlayerQuestCache[playerid][qAktiv]) format(string,sizeof(string),"{F1F7DC}Demarkieren");
	else format(string,sizeof(string),"{F1F7DC}Quest als aktiv kennzeichnen");			
	format(string,sizeof(string),"%s\nQuestgeber: %s",string,QuestInfo[questid][qQuestgeberName]);
	format(string,sizeof(string),"%s\nBelohnung(en)",string);
	format(string,sizeof(string),"%s\nZiel(e)",string);	
	if(QuestInfo[questid][qRemovable]) format(string,sizeof(string),"%s\n{C85135}Quest entfernen",string);
	
	//Header
	format(query,sizeof(query),"{F1F7DC}Quest verwalten: %s (ID %d)",QuestInfo[questid][qName],questid);
	qShowPlayerDialog(playerid,dialog_QUEST_MAIN,DIALOG_STYLE_LIST,query,string,"Weiter","Zurück");
	return 1;
}

quest::ShowPlayerList(playerid)
{
	switch(PlayerEvent[playerid][TutorialStep][0])
	{
	    case -1:
	    {//Hat das Tutorial bereits beendet
			new dlop = 0;
			for(new questid; questid<MAX_QUESTS_ACTIVE; questid++)
			{
			    if(PlayerQuest[playerid][questid][qID] == 0) continue;
				PlayerOption[playerid][dialog][dlop] = PlayerQuest[playerid][questid][qID];
				new tmp_qid = PlayerQuest[playerid][questid][qID];
				if(dlop > 0)
				{
					if(PlayerQuestCache[playerid][qAktiv] == tmp_qid) format(string,sizeof(string),"%s\n{FFECAA}",string);
					else format(string,sizeof(string),"%s\n{878375}",string);						
				}
				else
				{
					if(PlayerQuestCache[playerid][qAktiv] == PlayerQuest[playerid][questid][qID]) format(string,sizeof(string),"{FFECAA}");
					else format(string,sizeof(string),"{878375}");					
				}
				switch(QuestInfo[tmp_qid][qDifficulty])
				{
					case 1: 	format(string,sizeof(string),"%s%s\t\t- %s",string,QuestDifficulty[QuestInfo[tmp_qid][qDifficulty]],QuestInfo[tmp_qid][qName]);
					case 2: 	format(string,sizeof(string),"%s%s\t\t- t%s",string,QuestDifficulty[QuestInfo[tmp_qid][qDifficulty]],QuestInfo[tmp_qid][qName]);
					case 3: 	format(string,sizeof(string),"%s%s\t\t- %s",string,QuestDifficulty[QuestInfo[tmp_qid][qDifficulty]],QuestInfo[tmp_qid][qName]);
					case 4: 	format(string,sizeof(string),"%s%s\t- %s",string,QuestDifficulty[QuestInfo[tmp_qid][qDifficulty]],QuestInfo[tmp_qid][qName]);
					default:	format(string,sizeof(string),"%s%s\t- %s",string,QuestDifficulty[QuestInfo[tmp_qid][qDifficulty]],QuestInfo[tmp_qid][qName]);
				}
				dlop++;
			}
			if(dlop >= 1) 
			{
				qShowPlayerDialog(playerid,dialog_QUEST_LIST,DIALOG_STYLE_LIST,"{F1F7DC}Liste der aktiven Quests",string,"Auswahl","Beenden");			
			}
			else 
			{
				qShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Liste der aktiven Quests","{878375}Du hast zur Zeit keine Quests angenommen","Zurück","");
			}	
		}
		case 1..5:
		{//Ist noch im Tutorial
			PlayerOption[playerid][dialog][0] = 0;
		    qShowPlayerDialog(playerid,dialog_QUEST_LIST,DIALOG_STYLE_LIST,"{F1F7DC}Liste der aktiven Quests","{FF6347}Tutorial","Auswahl","Beenden");
		    return 1;
		}
	}
	return 1;
}

qOnDialogResponse(playerid, dialogid, response, listitem)
{
	if(GetPVarInt(playerid,"q_dialog") != 1) return 0;
	SetPVarInt(playerid,"q_dialog",0);
	DeletePVar(playerid,"q_dialog");
	switch(dialogid)
	{
		case dialog_QUEST_LIST:
		{
			if(!response) return 1;
			new questid = GetDialogOption(playerid,listitem);
			if(questid < 0) return 1;
			PlayerQuestCache[playerid][qID] = questid;
			quest::ShowOptions(playerid,questid);
			return 1;
		}
		case dialog_QUEST_LISTRE:
		{
			quest::ShowPlayerList(playerid);
			return 1;
		}
		case dialog_QUEST_MAINRE:
		{
			quest::ShowOptions(playerid,PlayerQuestCache[playerid][qID]);
			return 1;
		}
		case dialog_QUEST_MAIN:
		{
			if(!response)
			{//Zurück
				quest::ShowPlayerList(playerid);
				return 1;
			}
			new questid 	= PlayerQuestCache[playerid][qID],
				questslot	= quest::PlayerQuestSlotByQuestID(playerid,questid);
			switch(listitem)
			{
				case 0:
				{//Aktivieren
					if(questid == PlayerQuestCache[playerid][qAktiv])
					{//Demarkieren
						PlayerQuestCache[playerid][qAktiv] = 0;
						qShowPlayerDialog(playerid,dialog_QUEST_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Verwaltung - Demarkieren","{F1F7DC}INFO: Du hast diese Quest demarkiert. Über mögliche Fortschritte\nwirst du nun nicht mehr benachrichtigt.","Zurück","");
						DisablePlayerMissionCP(playerid);
					}
					else
					{//Markieren
						PlayerQuestCache[playerid][qAktiv] = questid;
						qShowPlayerDialog(playerid,dialog_QUEST_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Verwaltung - Markieren","{F1F7DC}INFO: Du hast die Quest markiert. Machst du Fortschritte\nin der Queststory, so wirst du benachrichtigt.","Zurück","");
						quest::OnQuestUpdate(playerid,questid,PlayerQuest[playerid][questslot][qStep],true);
						mysql_SetIntByInt("pnwl_user_event","currentquest",questid,"userid",PlayerInfo[playerid][PLAYER_ID]);
					}
					return 1;
				}
				case 1:
				{//Questgeber
					format(string,sizeof(string),"{F1F7DC}Questgeber: \"{FFECAA}%s{F1F7DC}\"",QuestInfo[questid][qQuestgeberName]);
					switch(QuestInfo[questid][qQuestgeberID])
					{
						case -1: /*System*/format(string,sizeof(string),"%s{F1F7DC}\n\n	Das System verteilt Quests je nach Fortschritt der Story oder bei besonderen Events/Ereignissen. Man kann diese Quests nicht manuell\nannehmen, sie werden einem automatisch zugewiesen.",string);
						case 0: format(string,sizeof(string),"%s{F1F7DC}\n\nJohn ist ein Tutorial-NPC, sprich du erhältst nur Quests\nvon ihm, wenn du dich im Tutorial befindest.",string);
						default:
						{//Andere IDs
							format(string,sizeof(string),"%s{F1F7DC}\n\nDer Auftraggeber ist unbekannt.",string);
						}
					}
					qShowPlayerDialog(playerid,dialog_QUEST_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Verwaltung - Questgeber",string,"Zurück","");
					return 1;
				}
				case 2:
				{//Belohnung
					//Noch nicht fertig
					qShowPlayerDialog(playerid,dialog_QUEST_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Quest - Belohnungen","Noch nicht fertig","Zurück","");
					return 1;
				}
				case 3:
				{//Ziele
					//Noch nicht fertig
					new count_ziele=0,step=PlayerQuest[playerid][questslot][qStep];
					format(string,sizeof(string),"{F1F7DC}Ziele der Quest \"{FFECAA}%s{F1F7DC}\"\n",QuestInfo[questid][qName]);
					switch(questid)
					{
						case 0:
						{//Quest: Tutorial
							new tstep[2];
							tstep[0] = PlayerEvent[playerid][TutorialStep][0];
							tstep[1] = PlayerEvent[playerid][TutorialStep][1];					
							if(PlayerEvent[playerid][TutorialStep][0] >= 3)
							{
								if(PlayerEvent[playerid][TutorialStep][1] >= 8) { format(string,sizeof(string),"%s\n{%s}Ziel: Begib dich zum Haus, von dem Alan Wake gesprochen hat.",string,(tstep[0]==3&&tstep[1]==8)?("FFECAA"):("878375")); count_ziele++; }//Zum Haus
								if(PlayerEvent[playerid][TutorialStep][1] >= 9) { format(string,sizeof(string),"%s\n{%s}Ziel: Untersuche den toten Einheimischen.",string,(tstep[0]==3&&tstep[1]==9)?("FFECAA"):("878375")); count_ziele++; }//Zur Leiche
							}
							if(PlayerEvent[playerid][TutorialStep][0] >= 4)
							{
								if(PlayerEvent[playerid][TutorialStep][1] >= 1) { format(string,sizeof(string),"%s\n{%s}Ziel: Finde einen Weg in den Keller",string,(tstep[0]==4&&tstep[1]==1)?("FFECAA"):("878375")); count_ziele++; }//Finde einen Weg in den Keller
								if(PlayerEvent[playerid][TutorialStep][1] >= 2) { format(string,sizeof(string),"%s\n{%s}Ziel: Suche den Lagerort der Medizin",string,(tstep[0]==4&&tstep[1]==2)?("FFECAA"):("878375")); count_ziele++; }//Zum Tor im Keller
								if(PlayerEvent[playerid][TutorialStep][1] >= 3) { format(string,sizeof(string),"%s\n{%s}Ziel: Benutze den Schalter um das Tor zu öffnen",string,(tstep[0]==4&&tstep[1]==3)?("FFECAA"):("878375")); count_ziele++; }//Schalter umlegen
								if(PlayerEvent[playerid][TutorialStep][1] >= 4) { format(string,sizeof(string),"%s\n{%s}Ziel: Nehme das Päckchen in der die Medizin ist",string,(tstep[0]==4&&tstep[1]==4)?("FFECAA"):("878375")); count_ziele++; }//Medizin nehmen
								if(PlayerEvent[playerid][TutorialStep][1] >= 5) { format(string,sizeof(string),"%s\n{%s}Ziel: Konsumiere die milit. Medizin",string,(tstep[0]==4&&tstep[1]==5)?("FFECAA"):("878375")); count_ziele++; }//Medizin konsumieren
								if(PlayerEvent[playerid][TutorialStep][1] >= 6) { format(string,sizeof(string),"%s\n{%s}Ziel: Verlasse den verseuchten Bunker",string,(tstep[0]==4&&tstep[1]==6)?("FFECAA"):("878375")); count_ziele++; }//ausm bunker abhauen
							}
						}//Quest: Tutorial ende
						case 1:
						{//Jobsuche
							format(string,sizeof(string),"%s\nZiel: \"Begib dich zum Bürgermeister und suche dir einen Job\"",string);
							count_ziele++;
						}
						case 2:
						{//Leichen verbrennen
							format(string,sizeof(string),"%s\nZiel: \"Bringe Mike brennbare Items\"",string);
							count_ziele++;
						}
						case 3:
						{//Ölproblem
							count_ziele = 1;
							if(step >= 0 && step <= 4) 	format(string,sizeof(string),"%s\n{FFECAA}Ziel: Aktiviere 5 defekte Ölpumpen (%d übrig)",string,(5-step));
							else						format(string,sizeof(string),"%s\n{878375}Ziel: Aktiviere 5 defekte Ölpumpen (0 übrig)",string);
							if(step == -1) {			format(string,sizeof(string),"%s\n{FFECAA}Ziel: Aktiviere den Zentralcomputer in der Basis",string); count_ziele++; }
						}
						case 4:
						{//Obdachlos
							count_ziele = 1;
							format(string,sizeof(string),"%s\n{FFECAA}Ziel: Plündere 5 Mülltonnen (%d übrig)",string,(5-step));
						}
						case 5:
						{//Tanklaster							
							new veh 			= GetPlayerVehicleID(playerid),
								veh_model       = GetVehicleModel(veh),
								trailer			= GetVehicleTrailer(veh),
								trailer_model	= GetVehicleModel(trailer);

							count_ziele = 1;
							if(step == 0) 															format(string,sizeof(string),"%s\n{FFECAA}Ziel: Begib dich zur Truckerbasis",string);
							else if(step > 0)
							{
								format(string,sizeof(string),"%s\n{878375}Ziel: Begib dich zur Truckerbasis",string);
								if(!car_CanPullTrailer(veh_model)) 									format(string,sizeof(string),"%s\n{FFECAA}Ziel: Besorge dir einen LKW, der Anhänger ziehen kann",string);
								else																format(string,sizeof(string),"%s\n{878375}Ziel: Besorge dir einen LKW, der Anhänger ziehen kann",string);
								count_ziele++;								
								if(step == 1) 														format(string,sizeof(string),"%s\n{FFECAA}Ziel: Fahre den LKW zum Öldepot",string);
								else if(step > 1)													format(string,sizeof(string),"%s\n{878375}Ziel: Fahre den LKW zum Öldepot",string);
								count_ziele++;								
								if(step >= 1)
								{
									if(!car_CanPullTrailer(veh_model) || trailer_model != 584)		format(string,sizeof(string),"%s\n{FFECAA}Ziel: Kuppel den Tanker an den LKW",string);
									else															format(string,sizeof(string),"%s\n{878375}Ziel: Kuppel den Tanker an den LKW",string);
									count_ziele++;
								}								
								if(step >= 2)
								{								
									count_ziele++;
									switch(step)
									{
										case 2: format(string,sizeof(string),"%s\n{FFECAA}Ziel: Liefer den Tanker in der kaputten Stadt ab",string);
										case 3: format(string,sizeof(string),"%s\n{FFECAA}Ziel: Liefer den Tanker in El Quebadros",string);
										case 4: format(string,sizeof(string),"%s\n{FFECAA}Ziel: Liefer den Tanker bei der Tankstelle ab",string);
									}
								}
							}
						}
					}//Ziele Ende
					
					if(count_ziele == 0) format(string,sizeof(string),"%s{C85135}\nEs sind keine Ziele vorhanden",string);
					qShowPlayerDialog(playerid,dialog_QUEST_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Quest - Ziele",string,"Zurück","");
					return 1;
				}
				case 4:
				{//Quest entfernen
					if(!QuestInfo[questid][qRemovable])
					{
						quest::ShowOptions(playerid,PlayerQuestCache[playerid][qID]);
						return 1;
					}
					format(string,sizeof(string),"{F1F7DC}Möchtest du die Quest \"{FFECAA}%s{F1F7DC}\" wirklich {C85135}entfernen{F1F7DC}?",QuestInfo[questid][qName]);
					qShowPlayerDialog(playerid,dialog_QUEST_REMOVE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Quest entfernen",string,"Ja","Nein");
					return 1;
				}
			}
			#pragma unused questslot
			return 1;
		}
		case dialog_QUEST_REMOVE:
		{
			new questid 	= PlayerQuestCache[playerid][qID],
				questslot	= quest::PlayerQuestSlotByQuestID(playerid,questid);
			if(!response || !QuestInfo[questid][qRemovable])
			{
				quest::ShowOptions(playerid,PlayerQuestCache[playerid][qID]);
				return 1;
			}
			quest::RemovePlayerQuest(playerid,questslot);
			format(string,sizeof(string),"{F1F7DC}Du hast die Quest \"{FFECAA}%s{F1F7DC}\" {C85135}entfernt{F1F7DC}.",QuestInfo[questid][qName]);
			qShowPlayerDialog(playerid,dialog_QUEST_LISTRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Quest entfernt",string,"Zurück","");
			return 1;
		}
	}
	return 1;
}

quest::CountPlayerQuests(playerid)
{
	new count = 0;
	for(new slot; slot<MAX_QUESTS_ACTIVE; slot++) if(PlayerQuest[playerid][slot][qID] != 0) count++;
	return count;
}

quest::AddPlayerQuest(playerid,questid,step=0,bool:_show=true,bool:_save=true)
{
	if(questid < 0 || questid >= MAX_QUESTS) return -1;
	if(quest::PlayerQuestSlotByQuestID(playerid,questid) != -1) return -1;
	for(new slot; slot<MAX_QUESTS_ACTIVE; slot++)
	{
		if(PlayerQuest[playerid][slot][qID] == 0)
		{//Slot ist frei
			PlayerQuest[playerid][slot][qID] 	= questid;
			PlayerQuest[playerid][slot][qStep]	= step;
			if(_show) quest::OnNewQuest(playerid,questid,step);
			if(_save)
			{
				format(string,sizeof(string),"SELECT * FROM pnwl_user_quests WHERE userid='%d' AND questid='%d'",PlayerInfo[playerid][PLAYER_ID],questid);
				mysql_query(string);
				mysql_store_result();
				if(mysql_num_rows() > 0)
				{//Hat die Quest schon
					mysql_free_result();
					format(string,sizeof(string),"UPDATE pnwl_user_quests SET inventar='1',step='%d' WHERE userid='%d' AND questid='%d'",step,PlayerInfo[playerid][PLAYER_ID],questid);
					mysql_query(string);
					mysql_free_result();
				}
				else
				{//Hatte die Quest noch nicht
					mysql_free_result();
					format(string,sizeof(string),"INSERT INTO pnwl_user_quests (userid,questid,step,inventar) VALUES ('%d','%d','%d','1')",PlayerInfo[playerid][PLAYER_ID],questid,step);
					mysql_query(string);
					mysql_free_result();
				}
			}
			if(quest::CountPlayerQuests(playerid) == 0) PlayerQuestCache[playerid][qAktiv] = questid;
			return slot;
		}
		else
		{
			if(PlayerQuest[playerid][slot][qID] == questid) break;//Hat die Quest schon
		}
	}
	return -2;
}

quest::RemovePlayerQuest(playerid,qslot,bool:_save=true)
{
	if(qslot < 0 || qslot >= MAX_QUESTS_ACTIVE) return -1;
	quest::OnRemoveQuest(playerid,qslot);
	if(_save)
	{
		new questid = PlayerQuest[playerid][qslot][qID];
		format(string,sizeof(string),"UPDATE pnwl_user_quests SET inventar='0' WHERE userid='%d' AND questid='%d'",PlayerInfo[playerid][PLAYER_ID],questid);
		mysql_query_once(string);
	}
	PlayerQuest[playerid][qslot][qID] 	= 0;
	PlayerQuest[playerid][qslot][qStep] = 0;
	//Searching new slot
	for(new slot=(MAX_QUESTS_ACTIVE-1); slot>qslot; slot--)
	{
		if(PlayerQuest[playerid][slot][qID] != 0)
		{
			PlayerQuest[playerid][qslot][qID] 	= PlayerQuest[playerid][slot][qID];
			PlayerQuest[playerid][qslot][qStep]	= PlayerQuest[playerid][slot][qStep];	
			return 1;
		}
	}
	return 0;
}

quest::LoadPlayerQuests(playerid)
{
	format(query,sizeof(query),"SELECT * FROM pnwl_user_quests WHERE userid='%d' LIMIT %d",PlayerInfo[playerid][PLAYER_ID],MAX_QUESTS_ACTIVE);
	mysql_query(query);
	mysql_store_result();
	
	new questid,
		step,
		finished_times,
		bool:inventar;
	if(mysql_num_rows() >= 1)
	{
		while(mysql_retrieve_row())
		{
			mysql_fetch_field_row(string,"questid");
			questid = strval(string);
			mysql_fetch_field_row(string,"step");
			step = strval(string);
			mysql_fetch_field_row(string,"finished_times");
			finished_times = strval(string);
			mysql_fetch_field_row(string,"inventar");
			inventar = strval(string)==1?true:false;
			
			if(inventar) quest::AddPlayerQuest(playerid,questid,step,false,false);
			if(finished_times >= 1)
			{
				format(string,sizeof(string),"quest_%d_done",questid);
				SetPVarInt(playerid,string,finished_times);
			}
		}
	}
	mysql_free_result();
	
	PlayerQuestCache[playerid][qAktiv] = mysql_GetIntByInt("pnwl_user_event","currentquest","userid",PlayerInfo[playerid][PLAYER_ID]);
	if(!quest::HasPlayerQuest(playerid,PlayerQuestCache[playerid][qAktiv])) PlayerQuestCache[playerid][qAktiv] = 0;
	return 1;
}

forward bool:quest::HasPlayerFinishedQuest(playerid,questid);
stock bool:quest::HasPlayerFinishedQuest(playerid,questid)
{
	format(query,sizeof(query),"quest_%d_done",questid);
	if(GetPVarInt(playerid,query) >= 1) return 1;
	return 0;
}

quest::HasPlayerQuest(playerid,questid)
{
	for(new i; i<MAX_QUESTS_ACTIVE; i++) if(PlayerQuest[playerid][i][qID] == questid) return 1;
	return 0;
}

quest::OnQuestFinish(playerid,questid)
{//Beim Beenden der Quest
	format(string,sizeof(string),"INFO: Du hast die Quest \"%s\" beendet.",QuestInfo[questid][qName]);
	SendClientMessage(playerid,GRUEN,string);
	format(string,sizeof(string),"~w~Du hast die Quest \"~g~%s~w~\" beendet.",QuestInfo[questid][qName]);
	DisplayInfobox(playerid,"~w~Quest ~g~beendet",string,4);
	
	new qslot = quest::PlayerQuestSlotByQuestID(playerid,questid);
	
	new timestamp=gettime(_,_,_);
	format(string,sizeof(string),"UPDATE pnwl_user_quests SET finished='%d',finished_times=finished_times+1,step='0' WHERE userid='%d' AND questid='%d'",timestamp,PlayerInfo[playerid][PLAYER_ID],questid);
	mysql_query_once(string);
	
	DisablePlayerMissionCP(playerid);
	PlayerQuest[playerid][qslot][qStep] = 0;
	PlayerQuestCache[playerid][qAktiv] 	= 0;
	format(query,sizeof(query),"quest_%d_done",questid);
	SetPVarInt(playerid,query,1);
	
	mysql_SetIntByInt("pnwl_user_event","currentquest",0,"userid",PlayerInfo[playerid][PLAYER_ID]);
	new am_id = __amCache[amStoryline];
	if(am_id != -1) achievement::SetPlayerAchievementData(playerid,am_id,1,true);

						
	if(QuestInfo[questid][qWdh])
	{//Wiederholung möglich
		format(string,sizeof(string),"INFO: Du kannst die Quest \"%s\" beliebig oft wiederholen.",QuestInfo[questid][qName]);
		SendClientMessage(playerid,GRUEN,string);
	}
	else 
	{
		if(questid == PlayerQuestCache[playerid][qAktiv])
		{
		}
		quest::RemovePlayerQuest(playerid,qslot);
	}
	return 1;
}

quest::OnNewQuest(playerid,questid,step)
{//Beim erhalten einer neuen quest
	format(string,sizeof(string),"INFO: Du hast die Quest \"%s\" erhalten. Mehr Informationen findest du mit /quest",QuestInfo[questid][qName]);
	SendClientMessage(playerid,GRUEN,string);
	#pragma unused step
	return 1;
}

quest::OnRemoveQuest(playerid,qslot)
{//Beim entfernen einer quest
	new questid = PlayerQuest[playerid][qslot][qID];
	format(string,sizeof(string),"INFO: Die Quest \"%s\" wurde entfernt.",QuestInfo[questid][qName]);
	SendClientMessage(playerid,GRUEN,string);
	return 1;
}

quest::OnPlayerTradeItem(playerid,give_id,item,anzahl)
{//Item wurde schon entfernt
	if(anzahl <= 0) return 0;
	new itemid = item_PlayerItem[playerid][item][iID],questid = -1;
	switch(give_id)
	{
		case NPC_MIKE:
		{
			questid = quest::QuestIdByName("Leichen verbrennen");
			if(quest::HasPlayerQuest(playerid,questid) && item_ItemInfo[itemid][iProperties]&iBurns)
			{
				for(new i; i<anzahl; i++) quest::OnQuestFinish(playerid,questid);
				return anzahl;
			}
		}
	}
	return 0;
}

quest::OnQuestUpdate(playerid,questid,step,bool:refresh=false)
{//Beim updaten der quest
	new qslot = quest::PlayerQuestSlotByQuestID(playerid,questid),bool:is_active=false;
	PlayerQuest[playerid][qslot][qStep] = step;

	if(PlayerQuestCache[playerid][qAktiv] == questid) is_active = true;
	if(refresh)
	{
		format(string,sizeof(string),"INFO: Aktuelle Quest: \"%s\"",QuestInfo[questid][qName]);
		SendClientMessage(playerid,GRUEN,string);
	}
	else if(is_active)
	{
		format(string,sizeof(string),"INFO: Fortschritt bei der Quest \"%s\".",QuestInfo[questid][qName]);
		SendClientMessage(playerid,GRUEN,string);
	}

	switch(questid)
	{
		case 1:
		{//Jobsuche
		}
		case 2:
		{//Leichen verbrennen
		}
		case 3:
		{//Oelproblem
			switch(step)
			{
				case 0..4:
				{//Ölpumpen aktivieren
					if(is_active)
					{
						format(string,sizeof(string),"Ziel: Du musst noch %d Pumpen aktivieren",(5-step));
						SendClientMessage(playerid,GRUEN,string);						
						format(string,sizeof(string),"~w~Du musst noch ~g~%d~w~ weitere Anlagen aktivieren",(5-step));
						DisplayInfobox(playerid,"~w~Pumpe ~g~aktiviert",string,4);
					}
				}
				case 5:
				{//Letzte Ölpumpe aktiviert
					quest::OnQuestUpdate(playerid,questid,-1,is_active);
					return 1;
				}
				case -1:
				{//Zentralcomputer aktivieren
					if(is_active)
					{
						SendClientMessage(playerid,GRUEN,"Ziel: Aktiviere den Zentralcomputer in der Ölbasis");
						DisplayInfobox(playerid,"~w~Letzte Pumpe ~g~aktiviert","~w~Suche nun einen Weg zum Zentralcomputer um die~n~Oelzufuhr freizuschalten",3);
						SetPlayerMissionCP(playerid,150.000,1426.5000,111.000);
					}
				}
				case -2:
				{//Quest beendet
					DisablePlayerMissionCP(playerid);
					quest::OnQuestFinish(playerid,questid);
					w_SetPlayerPos(playerid,219.9660,1431.6633,10.5859,_,1);
					for(new i; i<sizeof(OilKeypad); i++)
					{
						new tmpstr[30];
						format(tmpstr,sizeof(tmpstr),"tmp_quest_oil_%d",i);
						SetPVarInt(playerid,tmpstr,0);
						DeletePVar(playerid,tmpstr);
					}
					return 1;
				}
			}
		}
		case 4:
		{//Obdachlos
			if(is_active)
			{
				format(string,sizeof(string),"Ziel: Du musst noch %d Mülltonnen durchsuchen",(5-step));
				SendClientMessage(playerid,GRUEN,string);

				format(string,sizeof(string),"~g~Ziel: ~w~Du musst noch %d Mülltonnen durchsuchen",(5-step));				
				DisplayInfobox(playerid,"~w~Quest \"Obdachlos\"",string,4);
			}
			if(step == 5)
			{//5 Mülltonnen durchsucht
				quest::OnQuestFinish(playerid,4);
				return 1;
			}
		}
		case 5:
		{//Tanklaster
			switch(step)
			{
				case 0:
				{
					if(is_active)
					{
						DisplayInfobox(playerid,"~w~Quest \"Tanklaster\"","~g~Ziel: ~w~Begib dich zur Truckerbasis",4);
						SendClientMessage(playerid,GRUEN,"Ziel: Begib dich zur Truckerbasis");
						SetPlayerMissionCP(playerid,519.9377,1644.2632,11.9215,4.0);
					}
				}
				case 1:
				{
					if(is_active)
					{
						DisplayInfobox(playerid,"~w~Quest \"Tanklaster\"","~g~Ziel: ~w~Suche dir einen LKW, der Anhaenger ziehen kann~n~~g~Ziel: ~w~Fahre den LKW zum Oeldepot und kuppel einen Tanker an",4);
						SendClientMessage(playerid,GRUEN,"Ziel: Suche dir einen LKW, der Anhänger ziehen kann");
						SendClientMessage(playerid,GRUEN,"Ziel: Fahre mit dem LKW zum Öldepot und kuppel einen Tanker an");
						SetPlayerMissionCP(playerid,262.6633,1380.6387,10.5859,4.0);
					}
				}				
				case 2..4:
				{
					if(is_active)
					{
						DisablePlayerMissionCP(playerid);
						switch(step)
						{
							case 2: SetPlayerMissionCP(playerid,-2068.8638,2099.0884,20.0570,4.0);
							case 3: SetPlayerMissionCP(playerid,-1316.5934,2696.6807,50.0625,4.0);
							case 4: SetPlayerMissionCP(playerid,-1470.5184,1864.0330,32.6328,4.0);
						}
						DisplayInfobox(playerid,"~w~Quest \"Tanklaster\"","~g~Ziel: ~w~Fahre die Ladung zu deinem Ziel (Kartenmarkierung)",4);
						SendClientMessage(playerid,GRUEN,"Ziel: Fahre die Ladung nun zu deinem Ziel (ist auf der Karte markiert)");
					}
				}
				case 5:
				{
					quest::OnQuestFinish(playerid,5);
					return 1;
				}
			}
		}
	}

	if(!refresh)
	{
		format(string,sizeof(string),"UPDATE pnwl_user_quests SET inventar='1',step='%d' WHERE userid='%d' AND questid='%d'",step,PlayerInfo[playerid][PLAYER_ID],questid);
		mysql_query(string);
		mysql_free_result();
	}
	return 1;
}