/*
	PostNuke Wasteland PlayerEvents (PEV)
	Author:	NeoPhoenix
	Date:	13.07.2012
*/

forward pevent_Muelltonne(playerid,mID);
public pevent_Muelltonne(playerid,mID)
{
	if(!IsPlayerConnected(playerid)) return 1;
	SetPVarInt(playerid,"pevent_Muelltonne",0);
	DeletePVar(playerid,"pevent_Muelltonne");
	ClearAnimations(playerid,1);
	if(!IsPlayerConnected(playerid) || PlayerEvent[playerid][IstTot] != 0) return 1;
	if(Muelltonne[mID][mActive] && Muelltonne[mID][mPickup])
	{
		if(IsPlayerInRangeOfPoint(playerid,MAX_ITEM_GIVE_RANGE,Muelltonne[mID][mPos][0],Muelltonne[mID][mPos][1],Muelltonne[mID][mPos][2]))
		{
			new item_list[4],pos=0;
			for(new it; it<4; it++)
			{
				if(Muelltonne[mID][mItems][it] > 0)
				{
					item_list[pos] = Muelltonne[mID][mItems][it];
					pos++;
				}
			}
			if(pos)
			{
				new itemid 		= item_list[random(pos)],
					anzahl      = random(2)+1,
					bool:find  	= frandom(3+(skill::GetPlayerSkill(playerid,SkillGlueck)/10.0))<1.0?false:true;
					
				new questid = quest::QuestIdByName("Obdachlos");
				if(questid != -1)
				{
					new slot = quest::PlayerQuestSlotByQuestID(playerid,questid);
					if(slot != -1) quest::OnQuestUpdate(playerid,questid,(PlayerQuest[playerid][slot][qStep]+1));
				}
					
				if(!find)
				{//Leider nichts gefunden
					ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Mülltonne durchsucht","{F1F7DC}\
					Du hast die Mülltonne durchsucht und leider nichts\n\
					gefunden. Die nächste Tonne kann in 3 Minuten durchsucht\n\
					werden.","Zurück","");
					SetPVarInt(playerid,"can_mine",3*60);
					DeactivateMuelltonne(mID);
					return 1;
				}
				
				switch(item::GivePlayerItem(playerid,itemid,anzahl))
				{
					case 1:
					{
						format(string,sizeof(string),"{F1F7DC}\
						Du hast die Mülltonne durchsucht und folgende%s Item%s gefunden:\n\
						%dx %s\n\
						Die nächste Tonne kann in 3 Minuten durchsucht werden",anzahl>1?(""):("s"),anzahl>1?("s"):(""),anzahl,item_ItemInfo[itemid][iName]);
						ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Mülltonne durchsucht",string,"Zurück","");
						SetPVarInt(playerid,"can_mine",3*60);
						DeactivateMuelltonne(mID);
						
						if(anzahl)
						{
							new am_id = __amCache[amSpuernase];
							if(am_id != -1) achievement::SetPlayerAchievementData(playerid,am_id,anzahl,true);
						}
						return 1;
					}
					case -3:
					{
						ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Mülltonne durchsucht","{F1F7DC}\
						Du hast die Mülltonne durchsucht, hast aber leider keinen\n\
						Platz mehr in deinem Rucksack. Sehr ärgerlich...","Zurück","");
					}
					default:
					{
						ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Mülltonne durchsucht","{F1F7DC}\
						Ein Fehler ist aufgetreten. NeoPhoenix zu faul konkreter zu werden...","Zurück","");
					}
				}
				return 1;
			}
			DeactivateMuelltonne(mID);
		}
	}
	return 1;
}

forward pevent_DropItem(playerid);
public pevent_DropItem(playerid)
{
	if(!IsPlayerConnected(playerid)) return 0;
	DeletePVar(playerid,"pevent_ImanTake");
	new itemid = PlayerCache[playerid][holding_item];
	if(itemid <= 0) return 0;
	SetPlayerSpecialAction(playerid,SPECIAL_ACTION_NONE);
	ApplyAnimation(playerid,"BOMBER","BOM_PLANT_2IDLE",4.0,0,0,0,0,0);
	equip::RemoveSlot(playerid,ITEM_ACTION_SLOT);
	PlayerCache[playerid][holding_item] = 0;
	for(new i=MAX_IMAN_SPAWNS; i<MAX_SPAWNS; i++)
	{
		if(!iman_Pickup[i][iman_Created]) continue;
		if(iman_Pickup[i][iman_Item] == itemid)
		{
			if(IsPlayerInRangeOfPoint(playerid,(AVERAGE_TAKE_DEST*2),iman_Pickup[i][iman_Pos][0],iman_Pickup[i][iman_Pos][1],iman_Pickup[i][iman_Pos][2]))
			{
				iman_Pickup[i][iman_Anzahl]++;
				iman::UpdateSpawn(i,SPAWN_DELETE_TIME);
				return 1;
			}
		}
	}
	new Float:__posX,Float:__posY,Float:__posZ;
	GetPlayerPos(playerid,__posX,__posY,__posZ);
	iman::CreateSpawn(itemid,1,SPAWN_TYPE_THROW_AWAY,__posX,__posY,__posZ,SPAWN_DELETE_TIME);
	return 0;
}

forward pevent_ImanTake(playerid,iman_id);
public pevent_ImanTake(playerid,iman_id)
{
	print("imantake");
	if(!IsPlayerConnected(playerid)) return 0;
	if(GetPVarInt(playerid,"pevent_ImanTake") != 1) return 0;
	ClearAnimations(playerid,1);
	DeletePVar(playerid,"pevent_ImanTake");	
	if(!IsPlayerConnected(playerid) || PlayerEvent[playerid][IstTot] >= 1) return 0;	
	if(!iman_Pickup[iman_id][iman_Created]) return SendClientMessage(playerid,GRUEN,"INFO: Du kannst nichts nehmen");
	new result = item::GivePlayerItem(playerid,iman_Pickup[iman_id][iman_Item],iman_Pickup[iman_id][iman_Anzahl]);
	if(result) return iman::DeleteSpawn(iman_id),1;
	return 0;
}

forward pevent_PickupItem(playerid,iman_id,animtype);
public pevent_PickupItem(playerid,iman_id,animtype)
{
	print("on pickupitem");
	if(GetPVarInt(playerid,"pevent_ImanTake") != 1) return 1;
	DeletePVar(playerid,"pevent_ImanTake");
	ClearAnimations(playerid,1);
	print("test");
	if(!IsPlayerConnected(playerid) || PlayerEvent[playerid][IstTot] >= 1) return 0;	
	print("test2");
    if(!animtype) ApplyAnimation(playerid,"BOMBER","BOM_PLANT_2IDLE",4.0,0,0,0,0,0);
	if(!iman_Pickup[iman_id][iman_Created]) return SendClientMessage(playerid,GRUEN,"INFO: Du kannst nichts nehmen");	
	new itemid = iman_Pickup[iman_id][iman_Item];
	if(iman_Pickup[iman_id][iman_Anzahl] > 1) return print("test1"),SetPVarInt(playerid,"pevent_ImanTake",1),pevent_ImanTake(playerid,iman_id);
	else
	{
		printf("end2");
		iman::DeleteSpawn(iman_id);
		item::SetPlayerHoldingItem(playerid,itemid);
	}
	return 1;
}

forward pevent_RemoveAttachedObject(playerid,aoid);
public pevent_RemoveAttachedObject(playerid,aoid)
{
	DeletePVar(playerid,"changing_Weapon");
	if(!IsPlayerAttachedObjectSlotUsed(playerid,aoid)) return 0;
	RemovePlayerAttachedObject(playerid,aoid);
	return 1;
}

forward pevent_BoxStart(pid,playerid);
public pevent_BoxStart(pid,playerid)
{
	if(!IsPlayerConnected(playerid) || !IsPlayerConnected(pid)) return 1;
	if(!BoxTime) return 1;
	TogglePlayerControllable(pid,1);
	TogglePlayerControllable(playerid,1);
	GameTextForPlayer(pid,"~g~LOS!",2000,0);
	GameTextForPlayer(playerid,"~g~LOS!",2000,0);
	SetPlayerHealth(pid,100.0);
	SetPlayerHealth(playerid,100.0);
	return 1;
}

forward pevent_Train(playerid,train_id);
public pevent_Train(playerid,train_id)
{
	if(!IsPlayerConnected(playerid)) return 1;
	SetPVarInt(playerid,"pev_train",train_id);
	TogglePlayerControllable(playerid,0);
	switch(train_id)
	{
		case 1:
		{//Bike
			ApplyAnimation(playerid,"GYMNASIUM","gym_bike_still",4.0,1,1,1,1,0,1);
		}
		case -1:
		{//Bike absteigen
			TogglePlayerControllable(playerid,1);
			SetPlayerPos(playerid,-1476.7799,2610.5217,40.6853);
			SetPlayerFacingAngle(playerid,268.9622);
			SetCameraBehindPlayer(playerid);
			OnSportstudioExit(playerid,1);
			SetPVarInt(playerid,"pev_train",0);
		}
		case 2:
		{//Laufband
			ApplyAnimation(playerid,"GYMNASIUM","gym_tread_tired",4.0,1,1,1,1,0,1);
		}
		case -2:
		{//Laufband absteigen
			TogglePlayerControllable(playerid,1);
			SetPlayerPos(playerid,-1467.1683,2602.4785,40.6784);
			SetPlayerFacingAngle(playerid,270.6629);
			SetCameraBehindPlayer(playerid);
			OnSportstudioExit(playerid,2);
			SetPVarInt(playerid,"pev_train",0);
		}
		case 3:
		{//hanteln
			ApplyAnimation(playerid,"Freeweights","gym_free_loop",4.0,1,1,1,1,0,1);
		}
		case -3:
		{//hanteln weglegen
			TogglePlayerControllable(playerid,1);
			SetPlayerPos(playerid,-1472.2189,2602.1113,40.0524);
			SetPlayerFacingAngle(playerid,350.0);
			SetCameraBehindPlayer(playerid);
			OnSportstudioExit(playerid,3);
			SetPVarInt(playerid,"pev_train",0);
		}
	}
	return 1;
}

forward pevent_WankEnd(playerid);
public pevent_WankEnd(playerid)
{
	if(!IsPlayerConnected(playerid)) return 1;
	DeletePVar(playerid,"pev_wank");
    SetPlayerSpecialAction(playerid,68);//Pinkeln
	SetTimerEx("pevent_StopPlayerAnimation",300,0,"d",playerid);
	return 1;
}

forward pevent_StopPlayerAnimation(playerid);
public pevent_StopPlayerAnimation(playerid)
{
	if(!IsPlayerConnected(playerid)) return 1;
	SetPlayerSpecialAction(playerid,0);
	ClearAnimations(playerid);
	return 1;
}

forward pevent_Anrufen(playerid,nummer[]);
public pevent_Anrufen(playerid,nummer[])
{
	if(!IsPlayerConnected(playerid))			return 1;
	if(GetPVarInt(playerid,"pev_anrufen") != 1)	return 1;
	if(strval(nummer) >= 30000001 && strval(nummer) <= 99999999)
	{
		for(new i; i<=__SERVER[HIGHEST_PLAYER_ID]; i++)
		{
			if(!IsPlayerConnected(i))						continue;
			if(PlayerInfo[i][Phone] != strval(nummer))		continue;
			if(playerid == i)								return SendClientMessage(playerid,ROT,"eigene nummer");
			if(IsPlayerNPC(i))								return SendClientMessage(playerid,ROT,"npc nummer");
			if(GetPVarInt(i,"pev_anrufen") != 0)			return SendClientMessage(playerid,ROT,"besetzt");
			SendPlayerMe(playerid," ruft jemanden an.");
			SetPVarInt(playerid,"pev_anrufen",2);
			SetPVarInt(playerid,"pev_anrufen_id",i);
			SendPlayerMe(i,"'s Handy klingelt");
			SetPVarInt(i,"pev_anrufen",-1);
			SetPVarInt(i,"pev_anrufen_id",playerid);
			return 1;
		}
	}
	SetPlayerSpecialAction(playerid,SPECIAL_ACTION_STOPUSECELLPHONE);
	SendClientMessage(playerid,ROT,"falsche nummer");
	DeletePVar(playerid,"pev_anrufen");
	return 1;
}

forward pevent_WeaponChange(playerid);
public pevent_WeaponChange(playerid)
{
	DeletePVar(playerid,"_guns_event");
	return 1;
}

forward pevent_ReloadFinished(playerid,weaponid);
public pevent_ReloadFinished(playerid,weaponid)
{
	if(GetPVarInt(playerid,"_reloading") == 1 && GetPlayerWeapon(playerid) == weaponid) gun_UpdateAmmoDisplay(playerid,gun_GetAmmoStorage(playerid,weaponid));
	ClearAnimations(playerid);
	SetPVarInt(playerid,"_reloading",0);
	SetPVarInt(playerid,"_reloading_w",0);
	return 1;
}