/*
	PostNuke Wasteland Functioncollection
	Author: NeoPhoenix
	Date: 08.01.2013
*/

PLAYER::SavePlayer(playerid,discon_reason=1)
{
	if(IsPlayerNPC(playerid) || playerid < 0 || playerid >= MAX_PLAYERS) return 0;
	job_SavePlayerJob(playerid);//Job abspeichern
	
	format(string,sizeof(string),"UPDATE pnwl_user_status SET hunger='%f',durst='%f',krankheitssumme='%f' WHERE userid='%d'",PlayerStatus[playerid][Hunger],PlayerStatus[playerid][Durst],PlayerStatus[playerid][KrankheitsSumme],PlayerInfo[playerid][PLAYER_ID]);
	mysql_query_once(string);//Status speichern
	format(string,sizeof(string),"UPDATE pnwl_user_resource SET batterien='%f',wasser='%f',benzin='%f' WHERE userid='%d'",PlayerResource[playerid][Batterien],PlayerResource[playerid][Wasser],PlayerResource[playerid][Benzin],PlayerInfo[playerid][PLAYER_ID]);
	mysql_query_once(string);//Resourcen speichern

	mysql_SetIntByInt("pnwl_user_event","payday",PlayerEvent[playerid][PayDayTime],"userid",PlayerInfo[playerid][PLAYER_ID]);
	mysql_SetIntByInt("pnwl_user_option","houseselect",PlayerOption[playerid][HouseSelect],"userid",PlayerInfo[playerid][PLAYER_ID]);
	mysql_SetIntByInt("pnwl_user","spielzeit",PlayerInfo[playerid][Spielzeit],"userid",PlayerInfo[playerid][PLAYER_ID]);

	for(new npc; npc_list:npc<npc_list; npc++)
	{
		format(query,sizeof(query),"UPDATE pnwl_user_npc SET cash_%d='%d', beliebtheit_%d='%d' WHERE userid='%d'",npc_list:npc,PlayerNPC[playerid][npc_list:npc][cash],npc_list:npc,PlayerNPC[playerid][npc_list:npc][beliebtheit],PlayerInfo[playerid][PLAYER_ID]);
		mysql_query(query);
	}
	
	if(PlayerInfo[playerid][IsInCRoom] != 1)
	{
		new Float:_logpos[3];
		GetPlayerPos(playerid,_logpos[0],_logpos[1],_logpos[2]);
		if(PlayerEvent[playerid][IstTot] > 0)
		{
			GetPlayerPos(playerid,PlayerInfo[playerid][TotPos][0],PlayerInfo[playerid][TotPos][1],PlayerInfo[playerid][TotPos][2]);
			new totvw = GetPlayerVirtualWorld(playerid);
			if(PlayerOption[playerid][HouseCache][0] == -1)
			{
				if(totvw == 0) 			totvw = 1000;
				else if(totvw == 1234) 	totvw = 1235;
			}

			format(string,sizeof(string),"UPDATE pnwl_user_pos SET totpos1='%f',totpos2='%f',totpos3='%f',totint='%d',totvw='%d' WHERE userid='%d'",PlayerInfo[playerid][TotPos][0],PlayerInfo[playerid][TotPos][1],PlayerInfo[playerid][TotPos][2],GetPlayerInterior(playerid),totvw,PlayerInfo[playerid][PLAYER_ID]);
			mysql_query_once(string);

			mysql_SetIntByInt("pnwl_user_event","isttot",PlayerEvent[playerid][IstTot],"userid",PlayerInfo[playerid][PLAYER_ID]);

			if(PlayerOption[playerid][HouseCache][0] != -1) mysql_SetIntByInt("pnwl_user_pos","tot_house",PlayerOption[playerid][HouseCache][0],"userid",PlayerInfo[playerid][PLAYER_ID]);
			else mysql_SetIntByInt("pnwl_user_pos","tot_house",-1,"userid",PlayerInfo[playerid][PLAYER_ID]);
		}
		else
		{
			if(discon_reason == 0)
			{//crash left
				format(string,sizeof(string),"UPDATE pnwl_user_option SET crashed='1',selectedspawn='4' WHERE userid='%d'",PlayerInfo[playerid][PLAYER_ID]);
				mysql_query_once(string);
				format(string,sizeof(string),"UPDATE pnwl_user_pos SET crashedpos1='%f',crashedpos2='%f',crashedpos3='%f',crashint='%d',crashvw='%d' WHERE userid='%d'",_logpos[0],_logpos[1],_logpos[2],GetPlayerInterior(playerid),GetPlayerVirtualWorld(playerid),PlayerInfo[playerid][PLAYER_ID]);
				mysql_query_once(string);
				if(PlayerOption[playerid][HouseCache][0] != -1) mysql_SetIntByInt("pnwl_user_pos","crash_house",PlayerOption[playerid][HouseCache][0],"userid",PlayerInfo[playerid][PLAYER_ID]);
				else mysql_SetIntByInt("pnwl_user_pos","crash_house",-1,"userid",PlayerInfo[playerid][PLAYER_ID]);
				
				format(string,sizeof(string),"Name: %s ID: %d Time: %d Pos(%0.2f,%0.2f,%0.2f) Int/VW(%d,%d)\n",GetName(playerid),PlayerInfo[playerid][PLAYER_ID],gettime(),_logpos[0],_logpos[1],_logpos[2],GetPlayerInterior(playerid),GetPlayerVirtualWorld(playerid));
				INI::Append(#ERROR_PATH"/user_crash.txt",string);
			}
			else
			{
				if(discon_reason == 1)/*normal left*/ mysql_SetIntByInt("pnwl_user_option","selectedspawn",1,"userid",PlayerInfo[playerid][PLAYER_ID]);//log spawn
				if(PlayerEvent[playerid][IstTot] == 0)
				{
					format(string,sizeof(string),"UPDATE pnwl_user_pos SET logpos1='%f',logpos2='%f',logpos3='%f',logint='%d',logvw='%d' WHERE userid='%d'",_logpos[0],_logpos[1],_logpos[2],GetPlayerInterior(playerid),GetPlayerVirtualWorld(playerid),PlayerInfo[playerid][PLAYER_ID]);
					mysql_query_once(string);
					if(PlayerOption[playerid][HouseCache][0] != -1) mysql_SetIntByInt("pnwl_user_pos","log_house",PlayerOption[playerid][HouseCache][0],"userid",PlayerInfo[playerid][PLAYER_ID]);
					else mysql_SetIntByInt("pnwl_user_pos","log_house",-1,"userid",PlayerInfo[playerid][PLAYER_ID]);
				}
				else
				{
				}
			}
		}
	}
	
	new wstring[15],hits,fired,time,Float:damage,kills;
	for(new weaponid; weaponid<=54; weaponid++)
	{
		format(wstring,15,"w_%d_AMMO",weaponid);
		fired = GetPVarInt(playerid,wstring);
		SetPVarInt(playerid,wstring,0);
		
		format(wstring,15,"w_%d_KILLS",weaponid);
		kills = GetPVarInt(playerid,wstring);
		SetPVarInt(playerid,wstring,0);
		
		if(fired <= 0 && kills <= 0) continue;//Hat die Waffe nicht benutzt
		format(wstring,15,"w_%d_HIT",weaponid);
		hits = GetPVarInt(playerid,wstring);
		SetPVarInt(playerid,wstring,0);
		
		format(wstring,15,"w_%d_TIME",weaponid);
		time = GetPVarInt(playerid,wstring);
		SetPVarInt(playerid,wstring,0);
		
		format(wstring,15,"w_%d_DAM",weaponid);
		damage = GetPVarFloat(playerid,wstring);
		SetPVarFloat(playerid,wstring,0.0);
		
		format(query,sizeof(query),"UPDATE pnwl_user_weapon_stats SET fired_%d=fired_%d+%d, hits_%d=hits_%d+%d, damage_%d=damage_%d+%f, time_%d=time_%d+%d, kills_%d=kills_%d+%d WHERE userid='%d'",weaponid,weaponid,fired,weaponid,weaponid,hits,weaponid,weaponid,damage,weaponid,weaponid,time,weaponid,weaponid,kills,PlayerInfo[playerid][PLAYER_ID],weaponid);
		mysql_query_once(query);
	}
	mysql_SetIntByInt("wcf0_user_option_value","userOption80",PlayerInfo[playerid][Spielzeit],"userid",PlayerInfo[playerid][PLAYER_ID]);//Spielzeit

	new wt_s[15],monat;
	getdate(_,monat,_);
	format(wt_s,sizeof(wt_s),"monat_%d",monat);
	format(query,sizeof(query),"UPDATE pnwl_user_mon_online SET mon_%d=mon_%d+%d WHERE userid='%d'",monat,monat,GetPVarInt(playerid,wt_s),PlayerInfo[playerid][PLAYER_ID]);
	mysql_query_once(query);
	format(query,sizeof(query),"UPDATE pnwl_server_mon_online SET onlinezeit=onlinezeit+%d WHERE monat='%d'",GetPVarInt(playerid,wt_s),monat);
	mysql_query_once(query);
	SetPVarInt(playerid,wt_s,0);
	
	for(new wt=1; wt<=7; wt++)
	{
		format(wt_s,sizeof(wt_s),"wochentag_%d",wt);
		if(GetPVarInt(playerid,wt_s) >= 1)
		{
			format(query,sizeof(query),"UPDATE pnwl_user_wt_online SET wt_%d=wt_%d+%d WHERE userid='%d'",wt,wt,GetPVarInt(playerid,wt_s),PlayerInfo[playerid][PLAYER_ID]);
			mysql_query_once(query);
			format(query,sizeof(query),"UPDATE pnwl_server_wt_online SET onlinezeit=onlinezeit+%d WHERE wochentag='%d'",GetPVarInt(playerid,wt_s),wt);
			mysql_query_once(query);
			SetPVarInt(playerid,wt_s,0);
		}
	}
	for(new zeit=0; zeit<=23; zeit++)
	{
		format(wt_s,sizeof(wt_s),"zeit_%d",zeit);
		if(GetPVarInt(playerid,wt_s) >= 1)
		{
			format(query,sizeof(query),"UPDATE pnwl_user_time_online SET zeit_%d=zeit_%d+%d WHERE userid='%d'",zeit,zeit,GetPVarInt(playerid,wt_s),PlayerInfo[playerid][PLAYER_ID]);
			mysql_query_once(query);
			format(query,sizeof(query),"UPDATE pnwl_server_time_online SET onlinezeit=onlinezeit+%d WHERE zeit='%d'",GetPVarInt(playerid,wt_s),zeit);
			mysql_query_once(query);
			SetPVarInt(playerid,wt_s,0);
		}
	}
	return 1;
}

stock GetPlayerLanding(playerid,Float:max_dist)
{
	for(new lp; lp<sizeof(land_point); lp++) if(IsPlayerInRangeOfPoint(playerid,max_dist,land_point[lp][1][0],land_point[lp][1][1],land_point[lp][1][2])) return lp;
	return -1;
}

stock EndMedicalTreatment(playerid)
{
	switch(PlayerEvent[playerid][MedicalEvent][0])
	{
		case 1:
		{
			OnPlayerHealPlayer(playerid,PlayerEvent[playerid][MedicalEvent][1],0);
			return 1;
		}
	}
	return 0;
}

stock ScriptedMoveObject(objectid,Float:tpx,Float:tpy,Float:tpz,Float:Speed,callback[],dparam=0)
{
	if(Speed <= 0.0) return 0;
	/*new Float:npos[3];
	GetObjectPos(objectid,npos[0],npos[1],npos[2]);
	new Float:dist = floatsqroot( floatpower(tpx-npos[0],2)+floatpower(tpy-npos[1],2)+floatpower(tpz-npos[2],2) ),
		time = floatround((dist/Speed)*1000);*/
	//printf("ScriptedMoveObject From(%0.2f,%0.2f) To(%0.2f,%0.2f) Dist(%0.2f) Speed(%0.2f) Time(%d)",npos[0],npos[1],tpx,tpy,dist,Speed,time);
	if(IsObjectMoving(objectid)) StopObject(objectid);
	new time = MoveObject(objectid,tpx,tpy,tpz,Speed);
	SetTimerEx(callback,time,0,"dd",objectid,dparam);
	return 1;
}

stock jaegers_MoveTarget(targetid,Float:veclen,moved=0)
{
	new Float:pos[3],
	    Float:npos[3],
		oid = JaegersObject[targetid];
	if(oid < 0) return 0;
	GetObjectPos(oid,pos[0],pos[1],pos[2]);
	if(In2dArea(pos[0],pos[1],JaegersArea[0][0],JaegersArea[0][1],JaegersArea[1][0], JaegersArea[1][1]))
	{//Im Gebiet
	    npos[0] = (pos[0]-veclen)+frandom(2*veclen);
	    if(npos[0] < JaegersArea[0][0]) npos[0] = JaegersArea[0][0];
	    if(npos[0] > JaegersArea[1][0]) npos[0] = JaegersArea[1][0];
	    npos[1] = (pos[1]-veclen)+frandom(2*veclen);
	    if(npos[1] < JaegersArea[0][1]) npos[1] = JaegersArea[0][1];
	    if(npos[1] > JaegersArea[1][1]) npos[1] = JaegersArea[1][1];
    }
    else
    {//Nicht im Gebiet
		npos[0] = (JaegersArea[0][0]+frandom(JaegersArea[1][0]-JaegersArea[0][0]));
		npos[1] = (JaegersArea[0][1]+frandom(JaegersArea[1][1]-JaegersArea[0][1]));
    }
    MapAndreas_FindZ_For2DCoord(npos[0],npos[1],npos[2]);
    npos[2] += 0.2;
    
    new Float:tri[3],Float:angle=0.0,Float:rot[3];
	tri[0] 	= (npos[0]-pos[0]);											//Dreieck X -> Gegenkathete
    tri[1] 	= (npos[1]-pos[1]);											//Dreieck Y -> Ankathete
    tri[2] 	= floatsqroot(floatpower(tri[0],2)+floatpower(tri[1],2));	//Dreieck H -> Hypothenuse
    if(tri[0] != 0.0 && tri[1] != 0.0) angle = asin(tri[0]/tri[2]);   	//Dreick Alpha
    angle = floatabs(angle);
	if(npos[0] >= pos[0] && npos[1] >= pos[1]) angle += 0.0;        //1 Quadrant     ^
	else if(npos[0] >= pos[0] && npos[1] <= pos[1]) angle += 45.0;  //4 Quadrant   2 | 1
 	else if(npos[0] <= pos[0] && npos[1] <= pos[1]) angle += 90.0;  //3 Quadrant --------->
	else if(npos[0] <= pos[0] && npos[1] >= pos[1]) angle += 135.0; //2 Quadrant   3 | 4
    GetObjectRot(oid,rot[0],rot[1],rot[2]);
	SetObjectRot(oid,rot[0],rot[1],angle);
	//printf("MoveTarget: Pos(%0.2f,%0.2f) NPos(%0.2f,%0.2f) Angle(%0.2f)",pos[0],pos[1],npos[0],npos[1],angle);
 	ScriptedMoveObject(oid,npos[0],npos[1],npos[2],frandom(8.0)+0.1,"jaegers_OnFinishMovement",(moved+1));
	return 1;
}

stock jaegers_InRangeOfTarget(playerid,Float:range=5.0)
{
	if(!IsPlayerInArea(playerid,JaegersArea[0][0],JaegersArea[0][1],JaegersArea[1][0], JaegersArea[1][1])) return -1;
	for(new i; i<MAX_JAEGER_POS; i++)
	{
		if(JaegersObject[i] >= 0)
		{
		    new Float:jopos[3];
		    GetObjectPos(JaegersObject[i],jopos[0],jopos[1],jopos[2]);
			if(IsPlayerInRangeOfPoint(playerid,range,jopos[0],jopos[1],jopos[2])) return i;
		}
	}
	return -1;
}

stock miners_Create(Float:droprate)
{
	droprate = ((droprate/100.0)*__SERVER[OPT_DROPRATE]);
	new created = 0;
	for(new i; i<MAX_MINERS; i++)
	{
		miners_DestroyRock(i);
	    if(frandom(100.0) < droprate)
	    {
	        MinersObject[i] = CreateObject(906,MinersPos[i][0],MinersPos[i][1],MinersPos[i][2],MinersPos[i][3],MinersPos[i][4],MinersPos[i][5],50.0);
	        MinersHealth[i] = 1000.0+frandom(500.0);
	        created++;
	    }
	}
	return created;
}

stock miners_DestroyRock(rockid)
{
	if(MinersObject[rockid] == -1) return 0;
	DestroyObject(MinersObject[rockid]);
	MinersObject[rockid] = -1;
	MinersHealth[rockid] = 0.0;
	return 1;
}

stock miners_InRangeOfRock(playerid,Float:range=5.0)
{
	if(!IsPlayerInRangeOfPoint(playerid,150.0,787.9872,2793.7009,-51.9906)) return -1;
	for(new i; i<MAX_MINERS; i++) if(MinersObject[i] >= 0 && IsPlayerInRangeOfPoint(playerid,range,MinersPos[i][0],MinersPos[i][1],MinersPos[i][2])) return i;
	return -1;
}

stock jaegers_Create(Float:droprate)
{
	droprate = ((droprate/100.0)*__SERVER[OPT_DROPRATE]);
	new created = 0;
	for(new i; i<MAX_JAEGER_POS; i++)
	{
		jaegers_DestroyTarget(i);
	    if(frandom(100.0) <= droprate)
	    {
			new Float:cpos[3];
			cpos[0] = (JaegersArea[0][0]+frandom(JaegersArea[1][0]-JaegersArea[0][0]));
			cpos[1] = (JaegersArea[0][1]+frandom(JaegersArea[1][1]-JaegersArea[0][1]));
	        MapAndreas_FindZ_For2DCoord(cpos[0],cpos[1],cpos[2]);
	        cpos[2] += 0.2;
	        JaegersObject[i] = CreateObject(19315,cpos[0],cpos[1],cpos[2],0.0,0.0,0.0,400.0);
	        JaegersHealth[i] = 1000.0+frandom(500.0);
	        created++;
	    }
	}
	return created;
}

stock jaegers_DestroyTarget(targetid)
{
	if(JaegersObject[targetid] == -1) return 0;
	DestroyObject(JaegersObject[targetid]);
	JaegersObject[targetid] = -1;
	JaegersHealth[targetid] = 0.0;
	return 1;
}

stock CountPlayerDevComments(playerid)
{
	new count = 0;
	for(new devc=1; devc<(MAX_DEVELOPER_PICKUPS+1); devc++)	if(PlayerEvent[playerid][DevComment][0] & (1<<devc)) count++;
	return count;
}

stock UpdatePlayerNametag(playerid)
{
    new Float:player_hp,
		Float:karma = skill::GetPlayerSkill(playerid,SkillKarma);
	GetPlayerHealth(playerid,player_hp);
	format(string,sizeof(string),"%s",GetName(playerid));
	if(karma < -10.0)		strcat(string,"\nBandit");
	else if(karma > 10.0)	strcat(string,"\nSamariter");
	
	if(player_hp > 95.0)		return Update3DTextLabelText(PlayerInfo[playerid][Nametag],	COLOR_PLAYER_HP_UE_95, string);
 	else if(player_hp > 83.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_83, string);
	else if(player_hp > 75.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_75, string);
	else if(player_hp > 67.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_67, string);
	else if(player_hp > 55.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_55, string);
	else if(player_hp > 50.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_50, string);
	else if(player_hp > 45.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_45, string);
	else if(player_hp > 40.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_40, string);
	else if(player_hp > 35.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_35, string);
	else if(player_hp > 25.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_25, string);
	else if(player_hp > 15.0)	return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_15, string);
	else						return Update3DTextLabelText(PlayerInfo[playerid][Nametag], COLOR_PLAYER_HP_UE_00, string);
}

PlayerShopDrawExit(playerid)
{
	PlayerShopDrawHide(playerid);
	if(GetPVarInt(playerid,"in_shop") == 1)
	{
		fixed_PlayerTextDrawDestroy(playerid,PBarText[playerid][0]);
		fixed_PlayerTextDrawDestroy(playerid,PBarText[playerid][1]);
		fixed_PlayerTextDrawDestroy(playerid,PBarText[playerid][2]);
	}
	return 1;
}

PBarTextReload(playerid)
{
	for(new i; i<3; i++)
	{
		if(PBarText[playerid][i] != PlayerText:INVALID_TEXT_DRAW)
		{
			PlayerTextDrawHide(playerid,PBarText[playerid][i]);
			PlayerTextDrawDestroy(playerid,PBarText[playerid][i]);
			PBarText[playerid][i] = PlayerText:INVALID_TEXT_DRAW;
		}
	}
	
	PBarText[playerid][0] = CreatePlayerTextDraw(playerid,317.000000, 279.000000, "db bartext0");
	PlayerTextDrawBackgroundColor(playerid,PBarText[playerid][0], 0x352700FF);
	PlayerTextDrawFont(playerid,PBarText[playerid][0], 2);
	PlayerTextDrawLetterSize(playerid,PBarText[playerid][0], 0.430000, 2.200000);
	PlayerTextDrawColor(playerid,PBarText[playerid][0], 0x352700FF);
	PlayerTextDrawSetOutline(playerid,PBarText[playerid][0], 0);
	PlayerTextDrawSetProportional(playerid,PBarText[playerid][0], 1);
	PlayerTextDrawSetShadow(playerid,PBarText[playerid][0], 1);
	PlayerTextDrawAlignment(playerid,PBarText[playerid][0],2);

	PBarText[playerid][1] = CreatePlayerTextDraw(playerid,317.000000, 259.000000, "db bartext1");
	PlayerTextDrawBackgroundColor(playerid,PBarText[playerid][1], 0x352700FF);
	PlayerTextDrawFont(playerid,PBarText[playerid][1], 2);
	PlayerTextDrawLetterSize(playerid,PBarText[playerid][1], 0.430000, 2.200000);
	PlayerTextDrawColor(playerid,PBarText[playerid][1], 0x352700FF);
	PlayerTextDrawSetOutline(playerid,PBarText[playerid][1], 0);
	PlayerTextDrawSetProportional(playerid,PBarText[playerid][1], 1);
	PlayerTextDrawSetShadow(playerid,PBarText[playerid][1], 1);
	PlayerTextDrawAlignment(playerid,PBarText[playerid][1],2);

	PBarText[playerid][2] = CreatePlayerTextDraw(playerid,317.000000, 299.000000, "db bartext2");
	PlayerTextDrawBackgroundColor(playerid,PBarText[playerid][2], 0x352700FF);
	PlayerTextDrawFont(playerid,PBarText[playerid][2], 2);
	PlayerTextDrawLetterSize(playerid,PBarText[playerid][2], 0.430000, 2.200000);
	PlayerTextDrawColor(playerid,PBarText[playerid][2], 0x352700FF);
	PlayerTextDrawSetOutline(playerid,PBarText[playerid][2], 0);
	PlayerTextDrawSetProportional(playerid,PBarText[playerid][2], 1);
	PlayerTextDrawSetShadow(playerid,PBarText[playerid][2], 1);
	PlayerTextDrawAlignment(playerid,PBarText[playerid][2],2);
	return 1;
}

PlayerShopDrawHide(playerid)
{
	HideInfoMessage(playerid);
	fixed_TextDrawHideForPlayer(playerid,PBarRAussen[0]);
	fixed_TextDrawHideForPlayer(playerid,PBarRAussen[2]);
	fixed_TextDrawHideForPlayer(playerid,PBarRInnen[0]);
	fixed_TextDrawHideForPlayer(playerid,PBarRInnen[2]);
	fixed_PlayerTextDrawHide(playerid,PBarStatus[playerid][0]);
	fixed_PlayerTextDrawHide(playerid,PBarStatus[playerid][2]);
	fixed_PlayerTextDrawHide(playerid,PBarText[playerid][0]);
	fixed_PlayerTextDrawHide(playerid,PBarText[playerid][1]);
	fixed_PlayerTextDrawHide(playerid,PBarText[playerid][2]);
	if(GetPVarInt(playerid,"in_shop") == 1)
	{
		fixed_PlayerTextDrawDestroy(playerid,PBarStatus[playerid][0]);
		fixed_PlayerTextDrawDestroy(playerid,PBarStatus[playerid][2]);
	}
	return 1;
}

stock w_SetPlayerPos(playerid,Float:p_x,Float:p_y,Float:p_z,Float:angle=-13.37,w_time=0)
{
	if(w_time && !IsPlayerNPC(playerid))
	{
	    fixed_TextDrawShowForPlayer(playerid,ScreenHide);
	    fixed_TextDrawShowForPlayer(playerid,LoadDraw);
		TogglePlayerControllable(playerid,false);
		PlayerInfo[playerid][pScreenHide] = w_time;
		SetCameraBehindPlayer(playerid);
	}
	SetPlayerPos(playerid,p_x,p_y,p_z);
	PlayerEvent[playerid][tmpPos][0] = p_x;
	PlayerEvent[playerid][tmpPos][1] = p_y;
	PlayerEvent[playerid][tmpPos][2] = p_z;
	if(angle != -13.37) SetPlayerFacingAngle(playerid,angle);
	return 1;
}

CheckRealName(name[],source=1)// 1 = Vorname, 2 = Nachname
{
	if(strlen(name) < MIN_VORNAME_LEN 	&& source==1) 	return false;
	if(strlen(name) < MIN_NACHNAME_LEN 	&& source==2) 	return false;
	if(strlen(name) > MAX_VORNAME_LEN 	&& source==1) 	return false;
	if(strlen(name) > MAX_NACHNAME_LEN 	&& source==2) 	return false;
	new spaces;
	for(new letter; letter<strlen(name); letter++)
	{
		if(name[letter] == ' ')
		{
		    if(letter == 0) return false;
		    if(name[letter-1] == ' ') return false;
		    spaces++;
		}
		if(name[letter] >= '0' && name[letter] <= '9') return false;//Enthaelt Zahlen
		if(name[letter] >= 'A' && name[letter] <= 'Z' && letter != 0 && name[letter-1] != ' ') return false;//Neuer Name
		if(name[letter] >= 'a' && name[letter] <= 'z')//Im Namen
		{
		    if(letter == 0 || name[letter-1] == ' ')
		    {
		        return false;
		    }
		}
	}
	return 1;
}

tailor_GetNumberOfPages() return (TOTAL_ITEMS/SELECTION_ITEMS);
items_GetNumberOfPages(playerid) return (item::CountPlayerUsedSlots(playerid)/SELECTION_ITEMS);

PlayerText:mui_CreatePageTextDraw(playerid,Float:Xpos,Float:Ypos)
{
	new PlayerText:txtInit;
   	txtInit = CreatePlayerTextDraw(playerid,Xpos,Ypos,"0/0");
   	PlayerTextDrawUseBox(playerid,txtInit,0);
	PlayerTextDrawLetterSize(playerid,txtInit,0.4,1.1);
	PlayerTextDrawFont(playerid,txtInit,1);
	PlayerTextDrawSetShadow(playerid,txtInit,0);
    PlayerTextDrawSetOutline(playerid,txtInit,1);
    PlayerTextDrawColor(playerid,txtInit,0xACCBF1FF);
    PlayerTextDrawShow(playerid,txtInit);
    return txtInit;
}

PlayerText:mui_CreateItemPreviewDraw(playerid,modelindex,Float:Xpos,Float:Ypos,Float:width,Float:height,Float:fRotX=16.0,Float:fRotY=0.0,Float:fRotZ=-55.0,Float:fZoom=1.0)
{
    new PlayerText:txtPlayerSprite = CreatePlayerTextDraw(playerid,Xpos,Ypos,"");
    PlayerTextDrawFont(playerid,txtPlayerSprite,TEXT_DRAW_FONT_MODEL_PREVIEW);
    PlayerTextDrawColor(playerid,txtPlayerSprite,0xFFFFFFFF);
    PlayerTextDrawBackgroundColor(playerid,txtPlayerSprite,0xFFD6A750);
    PlayerTextDrawTextSize(playerid,txtPlayerSprite,width,height);
    PlayerTextDrawSetPreviewModel(playerid,txtPlayerSprite,modelindex);
	PlayerTextDrawSetPreviewRot(playerid,txtPlayerSprite,fRotX,fRotY,fRotZ,fZoom);
    PlayerTextDrawSetSelectable(playerid,txtPlayerSprite,1);
    PlayerTextDrawShow(playerid,txtPlayerSprite);
    return txtPlayerSprite;
}

mui_DestroyItemPreview(playerid)
{
	for(new x=0; x<SELECTION_ITEMS; x++)
	{
		if(mui_SelectionItems[playerid][x] != PlayerText:INVALID_TEXT_DRAW)
		{
			PlayerTextDrawDestroy(playerid,mui_SelectionItems[playerid][x]);
			mui_SelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
		}
		if(mui_ItemSubtitle[playerid][x] != PlayerText:INVALID_TEXT_DRAW)
		{
			PlayerTextDrawDestroy(playerid,mui_ItemSubtitle[playerid][x]);
			mui_ItemSubtitle[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
		}
	}
	return;
}

items_ShowPlayerItemPreviews(playerid)
{
	new Float:BaseX = mui_DIALOG_BASE_X,
		Float:BaseY = mui_DIALOG_BASE_Y-(mui_SPRITE_DIM_Y*0.33),
		linetracker = 0,
		itemat = (GetPVarInt(playerid,"items_mui_page")*SELECTION_ITEMS);
	mui_DestroyItemPreview(playerid);
	for(new x=0; x<SELECTION_ITEMS; x++)
	{
		if(itemat < 0 || itemat >= MAX_PLAYER_ITEMS) break;
	    if(linetracker == 0)
		{
	        BaseX = mui_DIALOG_BASE_X+25.0;
	        BaseY += mui_SPRITE_DIM_Y+1.0;
		}
		new itemid = item_PlayerItem[playerid][x][iID];
		if(!itemid) break;
  		mui_SelectionItems[playerid][x]	= mui_CreateItemPreviewDraw(playerid,item_ItemInfo[itemid][iObjectID],BaseX,BaseY,mui_SPRITE_DIM_X,mui_SPRITE_DIM_Y);
		mui_ItemSubtitle[playerid][x]	= CreatePlayerTextDraw(playerid,BaseX+29.0,BaseY+62.0,"_");
		PlayerTextDrawAlignment(playerid,mui_ItemSubtitle[playerid][x],2);
		PlayerTextDrawBackgroundColor(playerid,mui_ItemSubtitle[playerid][x],255);
		PlayerTextDrawFont(playerid,mui_ItemSubtitle[playerid][x],1);
		PlayerTextDrawLetterSize(playerid,mui_ItemSubtitle[playerid][x],0.150000,0.799999);
		PlayerTextDrawColor(playerid,mui_ItemSubtitle[playerid][x],-1);
		PlayerTextDrawSetOutline(playerid,mui_ItemSubtitle[playerid][x],0);
		PlayerTextDrawSetProportional(playerid,mui_ItemSubtitle[playerid][x],1);
		PlayerTextDrawSetShadow(playerid,mui_ItemSubtitle[playerid][x],1);
		format(string,sizeof(string),"%dx %s",item_PlayerItem[playerid][x][iAnzahl],item_ItemInfo[itemid][iName]);
		PlayerTextDrawSetString(playerid,mui_ItemSubtitle[playerid][x],string);
		PlayerTextDrawShow(playerid,mui_ItemSubtitle[playerid][x]);
  		mui_SelectionItemsTag[playerid][x]	= itemat;
		BaseX += mui_SPRITE_DIM_X+1.0;
		if(++linetracker == ITEMS_PER_LINE) linetracker = 0;
		itemat++;
	}
	return 1;
}

tailor_ShowPlayerSkinPreviews(playerid)
{
	new Float:BaseX = mui_DIALOG_BASE_X,
		Float:BaseY = mui_DIALOG_BASE_Y-(mui_SPRITE_DIM_Y*0.33),
		linetracker = 0,
		itemat = (GetPVarInt(playerid,"tailor_mui_page")*SELECTION_ITEMS);
	mui_DestroyItemPreview(playerid);
	for(new x=0; x<SELECTION_ITEMS; x++)
	{
		if(itemat >= TOTAL_ITEMS) break;
	    if(linetracker == 0)
		{
	        BaseX = mui_DIALOG_BASE_X+25.0;
	        BaseY += mui_SPRITE_DIM_Y+1.0;
		}
  		mui_SelectionItems[playerid][x]		= mui_CreateItemPreviewDraw(playerid,tailor_SkinList[itemat][0],BaseX,BaseY,mui_SPRITE_DIM_X,mui_SPRITE_DIM_Y);
  		mui_SelectionItemsTag[playerid][x]	= itemat;
		BaseX += mui_SPRITE_DIM_X+1.0;
		linetracker++;
		if(linetracker == ITEMS_PER_LINE) linetracker = 0;
		itemat++;
	}
	return;
}

items_UpdatePageTextDraw(playerid)
{
	new PageText[64+1];
	format(PageText,64,"%d/%d",GetPVarInt(playerid,"items_mui_page")+1,items_GetNumberOfPages(playerid)+1);
	PlayerTextDrawSetString(playerid,mui_CurrentPageTextDrawId[playerid],PageText);
	return;
}

tailor_UpdatePageTextDraw(playerid)
{
	new PageText[64+1];
	format(PageText,64,"%d/%d",GetPVarInt(playerid,"tailor_mui_page")+1,tailor_GetNumberOfPages()+1);
	PlayerTextDrawSetString(playerid,mui_CurrentPageTextDrawId[playerid],PageText);
	return;
}

tailor_CreateSelectionMenu(playerid)
{
	if(GetPVarInt(playerid,"tailor_mui_active") == 1) mui_DestroySelectionMenu(playerid);
	TextDrawShowForPlayer(playerid,mui_BackgroundTextDrawId);
	mui_CurrentPageTextDrawId[playerid]	= mui_CreatePageTextDraw(playerid,mui_DIALOG_WIDTH-30.0,mui_DIALOG_BASE_Y+15.0);
	TextDrawShowForPlayer(playerid,tailor_gHeaderTextDrawId);
	TextDrawShowForPlayer(playerid,mui_NextButtonTextDrawId);
	TextDrawShowForPlayer(playerid,mui_PrevButtonTextDrawId);
    tailor_ShowPlayerSkinPreviews(playerid);
    tailor_UpdatePageTextDraw(playerid);
    SetPVarInt(playerid,"tailor_mui_active",1);
	SelectTextDraw(playerid,0x8B45136450);
    return;
}

items_CreateSelectionMenu(playerid)
{
	if(GetPVarInt(playerid,"items_mui_active") == 1) mui_DestroySelectionMenu(playerid);
	TextDrawShowForPlayer(playerid,mui_BackgroundTextDrawId);
	mui_CurrentPageTextDrawId[playerid]	= mui_CreatePageTextDraw(playerid,mui_DIALOG_WIDTH-30.0,mui_DIALOG_BASE_Y+15.0);
	TextDrawShowForPlayer(playerid,items_gHeaderTextDrawId);
	TextDrawShowForPlayer(playerid,mui_NextButtonTextDrawId);
	TextDrawShowForPlayer(playerid,mui_PrevButtonTextDrawId);
    items_ShowPlayerItemPreviews(playerid);
    items_UpdatePageTextDraw(playerid);
    SetPVarInt(playerid,"items_mui_active",1);
	SelectTextDraw(playerid,0x8B45136450);
    return;
}

mui_DestroySelectionMenu(playerid)
{
	if(GetPVarInt(playerid,"tailor_mui_active") == 0 && GetPVarInt(playerid,"items_mui_active") == 0) return;	
	if(GetPVarInt(playerid,"items_mui_active") == 1)		TextDrawHideForPlayer(playerid,items_gHeaderTextDrawId);
	else if(GetPVarInt(playerid,"tailor_mui_active") == 1)	TextDrawHideForPlayer(playerid,tailor_gHeaderTextDrawId);
	mui_DestroyItemPreview(playerid);
	PlayerTextDrawDestroy(playerid,mui_CurrentPageTextDrawId[playerid]);
	mui_CurrentPageTextDrawId[playerid]	= PlayerText:INVALID_TEXT_DRAW;
	TextDrawHideForPlayer(playerid,mui_NextButtonTextDrawId);
	TextDrawHideForPlayer(playerid,mui_PrevButtonTextDrawId);
	TextDrawHideForPlayer(playerid,mui_BackgroundTextDrawId);
	CancelSelectTextDraw(playerid);
	SetPVarInt(playerid,"tailor_mui_active",0);
	SetPVarInt(playerid,"items_mui_active",0);
	return;
}

mui_PlayerInit(playerid)
{
    mui_CurrentPageTextDrawId[playerid]= PlayerText:INVALID_TEXT_DRAW;
    for(new x=0; x < SELECTION_ITEMS; x++) mui_SelectionItems[playerid][x] = PlayerText:INVALID_TEXT_DRAW;
	mui_ItemAt[playerid] = 0;
	return;
}

stock fixed_GivePlayerWeapon(playerid,w,ammo=-1)
{
	#pragma unused w
	#pragma unused ammo
	SetTimerEx("guns_EndGiveWeapon",300,false,"d",playerid);
	return 1;
}

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

stock fixed_RemovePlayerWeaponSlot(playerid,slot,bool:_remove=false)
{
	if(IsPlayerNPC(playerid)) return 0;
	if(0 <= slot < MAX_WEAPON_SLOTS)
	{
		SetPVarInt(playerid,"_guns_remove",1);
		SetPlayerArmedWeapon(playerid,0);
		new data[2],
			ammo[3],
			w = 0;
		GetPlayerWeaponData(playerid,slot,data[0],data[1]);
		new clipsize = GetWeaponClipsize(playerid,data[0]);
		w		= data[0];
		ammo[0] = 0;
		ammo[1] = PlayerWeapon[playerid][slot][1];
		ammo[2] = data[1];
		
		//Remove WeaponSlot
		new tmp_data[13][2];
		for(new i; i<13; i++) GetPlayerWeaponData(playerid,i,tmp_data[i][0],tmp_data[i][1]);
		PlayerCache[playerid][current_weapon_id] = 0;
		ResetPlayerWeapons(playerid);
		tmp_data[slot][0] = 0;
		tmp_data[slot][1] = 0;
		PlayerWeapon[playerid][slot][0] = 0;
		PlayerWeapon[playerid][slot][1] = 0;
		PlayerWeapon[playerid][slot][2] = 0;
		for(new i; i<13; i++)
		{
			if(tmp_data[i][0] != 0 && tmp_data[i][1] >= 1)
			{
				GivePlayerWeapon(playerid,tmp_data[i][0],tmp_data[i][1]);
			}
		}
		
		//Waffe speichern
		if(data[1] >= 1 && data[1] >= 1)
		{
			if(_remove)				ammo[0] = 0;
			else if(clipsize == 0)	ammo[0] = 1;//Schlagwaffe ohne Munition [immer 1 stück]
			else if(clipsize == 1)	ammo[0] = ammo[2];//Ohne Magazin [jetziges magazin]
			else if(clipsize >= 2)	ammo[0] = (ammo[1]+ammo[2]);//Waffe mit Magazin [reserve+jetziges magazin]
			
			format(query,sizeof(query),"UPDATE pnwl_user_cur_weapons SET wid_%d=0 WHERE userid=%d",slot,PlayerInfo[playerid][PLAYER_ID]);
			mysql_query_once(query);
			format(query,sizeof(query),"SELECT * FROM pnwl_user_weapons WHERE userid=%d AND weaponid=%d",PlayerInfo[playerid][PLAYER_ID],w);
			mysql_query(query);
			mysql_store_result();
			if(mysql_num_rows() >= 1)
			{
				mysql_free_result();
				format(query,sizeof(query),"UPDATE pnwl_user_weapons SET ammo=%d WHERE userid=%d AND weaponid=%d",ammo,PlayerInfo[playerid][PLAYER_ID],w);
				mysql_query_once(query);
			}
			else
			{
				mysql_free_result();
				format(query,sizeof(query),"INSERT INTO pnwl_user_weapons (userid,weaponid,ammo) VALUES ('%d','%d','%d')",PlayerInfo[playerid][PLAYER_ID],w,ammo);
				mysql_query_once(query);
			}			
			format(query,sizeof(query),"inv_has_weapon_wid_%d",w);
			SetPVarInt(playerid,query,_remove?0:1);
			format(query,sizeof(query),"inv_ammo_wid_%d",w);
			SetPVarInt(playerid,query,ammo[0]);
			DeletePVar(playerid,"_guns_remove");
			return 1;
		}
		DeletePVar(playerid,"_guns_remove");
		return 2;
	}
	return 0;
}

stock guns_RefreshCache(playerid,wid)
{
	if(!(0 <= wid < MAX_PNWL_WEAPONS)) return ;
	new data[2],
		sts[45],
		slot = GetWeaponSlot(wid);
	GetPlayerWeaponData(playerid,slot,data[0],data[1]);
	if(wid != data[0]) return ;
	format(sts,sizeof(sts),"inv_ammo_wid_%d",data[0]);
	new clipsize = GetWeaponClipsize(playerid,wid);
	if(clipsize == 0) SetPVarInt(playerid,sts,0);
	else if(clipsize == 1) SetPVarInt(playerid,sts,data[1]);
	else if(clipsize >= 2)
	{
		PlayerWeapon[playerid][slot][2] = data[1];
		SetPVarInt(playerid,sts,(PlayerWeapon[playerid][slot][1]+data[1]));
	}
	return ;
}

forward bool:guns_HasEquipped(playerid,wid);
stock bool:guns_HasEquipped(playerid,wid)
{
	if(!(0 <= wid < MAX_PNWL_WEAPONS)) return false;
	new slot = GetWeaponSlot(wid),
		data[2];
	GetPlayerWeaponData(playerid,slot,data[0],data[1]);
	if(wid == data[0]) return true;
	else return false;
}

stock ManageAusdauerDraw(playerid,bool:_show)
{
	if(_show)
	{
		if(PlayerInfo[playerid][IsInCRoom] != 0) return ManageAusdauerDraw(playerid,false);
		if(GetPVarInt(playerid,"ausdauer_show") != 1) TextDrawShowForPlayer(playerid,AusdauerDraw);
		if(PlayerAusdauerDraw[playerid] != PlayerText:INVALID_TEXT_DRAW)
		{
			PlayerTextDrawHide(playerid,PlayerAusdauerDraw[playerid]);
			PlayerTextDrawDestroy(playerid,PlayerAusdauerDraw[playerid]);
			PlayerAusdauerDraw[playerid] = PlayerText:INVALID_TEXT_DRAW;
		}
		
		if(PlayerEvent[playerid][Ausdauer] >= 100.0)	PlayerEvent[playerid][Ausdauer] = 100.0;
		if(PlayerEvent[playerid][Ausdauer] <= 0.0)		PlayerEvent[playerid][Ausdauer] = 0.0;
		
		PlayerAusdauerDraw[playerid] = CreatePlayerTextDraw(playerid, 320.000000, 440.000000, "~y~");
		PlayerTextDrawAlignment(playerid,PlayerAusdauerDraw[playerid], 2);
		PlayerTextDrawBackgroundColor(playerid,PlayerAusdauerDraw[playerid], 255);
		PlayerTextDrawFont(playerid,PlayerAusdauerDraw[playerid], 1);
		PlayerTextDrawLetterSize(playerid,PlayerAusdauerDraw[playerid], 1.000000, 0.300000);
		PlayerTextDrawColor(playerid,PlayerAusdauerDraw[playerid], -1);
		PlayerTextDrawSetOutline(playerid,PlayerAusdauerDraw[playerid], 0);
		PlayerTextDrawSetProportional(playerid,PlayerAusdauerDraw[playerid], 1);
		PlayerTextDrawSetShadow(playerid,PlayerAusdauerDraw[playerid], 1);
		PlayerTextDrawUseBox(playerid,PlayerAusdauerDraw[playerid], 1);
		PlayerTextDrawBoxColor(playerid,PlayerAusdauerDraw[playerid], RGB_Verlauf(1.0-(PlayerEvent[playerid][Ausdauer]/100.0))<<8|0xFF);
		PlayerTextDrawTextSize(playerid,PlayerAusdauerDraw[playerid], 0.000000, PlayerEvent[playerid][Ausdauer]*2.0);
		PlayerTextDrawShow(playerid,PlayerAusdauerDraw[playerid]);
		SetPVarInt(playerid,"ausdauer_show",1);
	}
	else
	{
		if(GetPVarInt(playerid,"ausdauer_show") != 1) return 1;
		TextDrawHideForPlayer(playerid,AusdauerDraw);
		if(playerid,PlayerAusdauerDraw[playerid] != PlayerText:INVALID_TEXT_DRAW)
		{
			PlayerTextDrawHide(playerid,PlayerAusdauerDraw[playerid]);
			PlayerTextDrawDestroy(playerid,PlayerAusdauerDraw[playerid]);
			PlayerAusdauerDraw[playerid] = PlayerText:INVALID_TEXT_DRAW;
		}
		SetPVarInt(playerid,"ausdauer_show",0);
	}
	return 1;
}

stock GetServerTicks() // 2012 - D0erfler
{
	new network_str[401];
	GetNetworkStats(network_str, sizeof(network_str));
	new i = 14; // string: "Server Ticks: %d ....." number is at 14
	new number[4]; // max 9999 ticks
	while(network_str[i] >= 48 && network_str[i] <= 57) // char werte für zahlen
	{
	    number[i-14] = network_str[i];
	    i++;
	}
	return strval(number);
}

forward bool:IsInArray(array[],ch,size=sizeof(array));
stock bool:IsInArray(array[],ch,size=sizeof(array))
{
	for(new i; i<size; i++) if(array[i] == ch) return true;
	return false;
}

stock UpdateStatusProgressBar(playerid,Float:percentage=-1.0)
{
	TextDrawHideForPlayer(playerid,StatusProgressBg);
	PlayerTextDrawHide(playerid,pStatusProgressBar[playerid]);
	PlayerTextDrawDestroy(playerid,pStatusProgressBar[playerid]);
	if(percentage == -1.0) return 1;//Hide
	TextDrawShowForPlayer(playerid,StatusProgressBg);
	if(percentage < 0.0)	percentage = 0.0;
	if(percentage > 100.0)	percentage = 100.0;
	pStatusProgressBar[playerid] = CreatePlayerTextDraw(playerid,279.000000,355.000000,"~s~");
	PlayerTextDrawBackgroundColor(playerid,pStatusProgressBar[playerid], 255);
	PlayerTextDrawFont(playerid,pStatusProgressBar[playerid], 1);
	PlayerTextDrawLetterSize(playerid,pStatusProgressBar[playerid], 0.500000, 0.199999);
	PlayerTextDrawColor(playerid,pStatusProgressBar[playerid], -1);
	PlayerTextDrawSetOutline(playerid,pStatusProgressBar[playerid], 0);
	PlayerTextDrawSetProportional(playerid,pStatusProgressBar[playerid], 1);
	PlayerTextDrawSetShadow(playerid,pStatusProgressBar[playerid], 1);
	PlayerTextDrawUseBox(playerid,pStatusProgressBar[playerid], 1);
	PlayerTextDrawBoxColor(playerid,pStatusProgressBar[playerid], RGB_Verlauf(1.0-percentage));
	PlayerTextDrawTextSize(playerid,pStatusProgressBar[playerid], 275.0+(0.85*percentage), 0.000000);
	PlayerTextDrawShow(playerid,pStatusProgressBar[playerid]);
	return 1;
}

stock CreateTimedObject(modelid,Float:X,Float:Y,Float:Z,Float:rX=0.0,Float:rY=0.0,Float:rZ=0.0,Float:DrawDistance=50.0,time=1000)
{
	new obj = CreateObject(modelid,X,Y,Z,rX,rY,rZ,DrawDistance);
	SetTimerEx("sevent_DestroyObject",time,false,"d",obj);
	return obj;
}

forward Float:GetAngleToPoint(Float:fPointX, Float:fPointY, Float:fDestX, Float:fDestY);
stock Float:GetAngleToPoint(Float:fPointX, Float:fPointY, Float:fDestX, Float:fDestY) return absoluteangle(-(90-(atan2((fDestY-fPointY),(fDestX-fPointX)))));

forward Float:absoluteangle(Float:angle);
stock Float:absoluteangle(Float:angle)
{
	while(angle < 0.0)	angle += 360.0;
	while(angle > 360.0)angle -= 360.0;
	return angle;
}

forward Float:VelocityToKmh(Float:velocity[],size=sizeof(velocity));
stock Float:VelocityToKmh(Float:velocity[],size=sizeof(velocity))
{
	new Float:v;
	for(new i; i<size; i++) v += floatpower(velocity[i],2.0);
	v = floatsqroot(v);
	v *= 198.0;
	return v;
}

stock RGB_Verlauf(Float:percentage_)
{
	if(percentage_ < 0.0) percentage_ = 0.0;
	if(percentage_ > 1.0) percentage_ = 1.0;
	return HSV_TO_RGB(120.0-(percentage_*120.0));
}

stock HSV_TO_RGB(Float:deg)
{
	//https://commons.wikimedia.org/wiki/File:HSV-RGB-comparison.svg
	new R,B,G;
	while(deg < 0.0)	deg += 360.0;
	while(deg > 360.0)  deg -= 360.0;
	if(deg <= 60.0 || deg >= 300.0)			R = 0xFF;
	else if(deg >= 120.0 && deg <= 240.0)	R = 0x00;
	else
	{
	    if(deg > 60.0 && deg < 120.0)		R = floatround(0xFF-((deg-60.0)/60.0)*0xFF);
	    else if(deg > 240.0 && deg < 300.0)	R = floatround(0x00+((deg-240.0)/60.0)*0xFF);
	}
	
	if(deg >= 60.0 && deg <= 180.0)	G = 0xFF;
	else if(deg >= 240.0)			G = 0x00;
	else
	{
	    if(deg > 180.0 && deg < 240.0)	G = floatround(0xFF-((deg-180.0)/60.0)*0xFF);
	    else if(deg < 60.0)				G = floatround(0x00+(deg/60.0)*0xFF);
	}
	
	if(deg >= 180.0 && deg <= 300.0)	B = 0xFF;
	else if(deg < 120.0)				B = 0x00;
	else
	{
	    if(deg > 300.0)						B = floatround(0xFF-((deg-300.0)/60.0)*0xFF);
	    else if(deg > 120.0 && deg < 180.0)	B = floatround(0x00+((deg-120.0)/60.0)*0xFF);
	}
	return (R<<16)|(G<<8)|B;
}

enum no_dm_struct
{
	dm_type,//1 = Radius, 2 = 2D Area, 3 = 3D Area
	Float:dm_x[2],
	Float:dm_y[2],
	Float:dm_z[2],
	opt_c[2],//{int,vw}
}
new Float:NoDmZones[][no_dm_struct] = {
	{3,/*X*/{-359.761,67.922},	/*Y*/{1005.5242,1304.2878},	/*Z,Z*/			{5.0,60.0}				},//Fort Carson Stadt
	{1,/*X*/{-172.365},			/*Y*/{1165.9482},			/*Z,Radius*/	{87.5835,15.0},	{2}		},//Waffenladen Dimitry
	{1,/*X*/{-218.428},			/*Y*/{1406.5778},			/*Z,Radius*/	{27.7734,15.0},	{18}	},//Kneipe Humungous
	{1,/*X*/{-210.834},			/*Y*/{1116.7026},			/*Z,Radius*/	{123.506,25.0},	{2}		},//Stadthalle Richard
	{1,/*X*/{373.7224},			/*Y*/{-117.2789},			/*Z,Radius*/	{1001.49,25.0},	{5}		},//Pizzaladen Joe
	{1,/*X*/{-174.651},			/*Y*/{1092.0603},			/*Z,Radius*/	{52.6125,25.0},	{2}		},//Bar in FC
	{1,/*X*/{2238.009},			/*Y*/{-1162.478},			/*Z,Radius*/	{1029.79,50.0},	{15}	},//Hotel in FC
	{1,/*X*/{2853.266},			/*Y*/{-262.9061},			/*Z,Radius*/	{52.7608,10.0},	{2}		}//Arztpraxis in FC
};

stock IsPlayerInNoDmZone(playerid)
{
	new Float:p[3];
	GetPlayerPos(playerid,p[0],p[1],p[2]);
	for(new dmz; dmz<sizeof(NoDmZones); dmz++)
	{
		if(NoDmZones[dmz][opt_c][0] != 0 && NoDmZones[dmz][opt_c][0] != GetPlayerInterior(playerid)) continue;
		if(NoDmZones[dmz][opt_c][1] != 0 && NoDmZones[dmz][opt_c][1] != GetPlayerVirtualWorld(playerid)) continue;
		switch(NoDmZones[dmz][dm_type])
		{
			case 1:
			{
				if(IsPlayerInRangeOfPoint(playerid,NoDmZones[dmz][dm_z][1],NoDmZones[dmz][dm_x][0],NoDmZones[dmz][dm_y][0],NoDmZones[dmz][dm_z][0])) return dmz;
				continue;
			}
			case 2,3:
			{
				if(IsPlayerInArea(playerid,NoDmZones[dmz][dm_x][0],NoDmZones[dmz][dm_x][1],NoDmZones[dmz][dm_y][0],NoDmZones[dmz][dm_y][1]))
				{
					if(NoDmZones[dmz][dm_type] == 2 || (p[2] >= NoDmZones[dmz][dm_z][0] && p[2] <= NoDmZones[dmz][dm_z][1]))
					{
						SetPlayerArmedWeapon(playerid,0);
						return dmz;
					}
				}
			}
		}
	}
	return -1;
}
forward bool:IsPlayerInArea(playerid,Float:minX,Float:maxX,Float:minY,Float:maxY);
stock bool:IsPlayerInArea(playerid,Float:minX,Float:maxX,Float:minY,Float:maxY)
{
	new Float:p[3];
	GetPlayerPos(playerid,p[0],p[1],p[2]);
	if(p[0] >= minX && p[0] <= maxX && p[1] >= minY && p[1] <= maxY) return true;
	return false;
}

stock GetNearestVehicle(playerid,Float:max_dist=10000.0)
{
	new Float:dist	= max_dist,
	    nearest     = -1;
	for(new i; i<MAX_VEHICLES; i++)
	{
	    if(!IsVehicleConnected(i)) continue;
	    new Float:p[3];
		GetVehiclePos(i,p[0],p[1],p[2]);
		if(IsPlayerInRangeOfPoint(playerid,dist,p[0],p[1],p[2])) nearest = i;
	}
	return nearest;
}

stock GetNearestPlayer(playerid,Float:max_dist=10000.0,bool:check_vwi=true)
{
	new Float:dist	= max_dist,
	    nearest     = -1;
	for(new i; i<MAX_PLAYERS; i++)
	{
	    if(!IsPlayerConnected(i) || i == playerid || IsPlayerNPC(i)) continue;
	    if(check_vwi && (GetPlayerInterior(playerid) != GetPlayerInterior(i) || GetPlayerVirtualWorld(playerid) != GetPlayerVirtualWorld(i))) continue;
	    new Float:p[3];
		GetPlayerPos(i,p[0],p[1],p[2]);
		if(IsPlayerInRangeOfPoint(playerid,dist,p[0],p[1],p[2])) nearest = i;
	}
	return nearest;
}

forward Float:DistanceBetweenPlayers(p1,p2);
stock Float:DistanceBetweenPlayers(p1,p2)
{
	if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2)) return 10000.0;
	if(p1 == p1) return 0.0;
	new Float:p[3];
	GetPlayerPos(p2,p[0],p[1],p[2]);
	return GetPlayerDistanceFromPoint(p1,p[0],p[1],p[2]);
}

CheckPlayerNoDM(playerid)
{
	new dmz = IsPlayerInNoDmZone(playerid);
	if(dmz != -1)
	{
		if(GetPVarInt(playerid,"in_no_dm_zone") == 0)
		{
			fixed_TextDrawShowForPlayer(playerid,NoDmZoneDraw);
			GameTextForPlayer(playerid,"~r~NoDm Zone betreten",1000,1);
			SetPVarInt(playerid,"in_no_dm_zone",1);
			return 2;
		}
		return 1;
	}
	else
	{
		if(GetPVarInt(playerid,"in_no_dm_zone") == 1)
		{
			fixed_TextDrawHideForPlayer(playerid,NoDmZoneDraw);
			SetPVarInt(playerid,"in_no_dm_zone",0);
			return -1;
		}
	}
	return 0;
}

stock ConvertTime(time,&weeks=-1,&days=-1,&hours=-1,&minutes=-1,&seconds=-1)
{
	new rest = time;
	if(weeks != -1 && rest >= 604800)
	{
		weeks	= (rest-(rest%604800))/604800;
		rest	-= (weeks*604800);
	}
	if(days != -1 && rest >= 86400)
	{
		days 	= (rest-(rest%86400))/86400;
		rest 	-= (days*86400);
	}
	if(hours != -1 && rest >= 3600)
	{
		hours 	= (rest-(rest%3600))/3600;
		rest    -= (hours*3600);
	}
	if(minutes != -1 && rest >= 60)
	{
		minutes = (rest-(rest%60))/60;
		rest    -= (minutes*60);
	}
	if(seconds != -1 && rest >= 1)
	{
		seconds = rest;
		rest    = 0;
	}
	return rest;
}

stock tokenize(const source[],dest[][],delim,destsize=sizeof(dest))
{
    new start,tokenID,last;
	for(new i; i<=strlen(source); i++)
    {
        if(source[i] == delim || i == strlen(source))
        {
            last = strmid(dest[tokenID],source,start,i,144);
            dest[tokenID][last] = '\0';
            start = (i+1);
            if(++tokenID >= destsize) break;
        }
    }
    return 1;
}

stock array_filter(array[],filter[],size=sizeof(array),null=0)
{
	new filtered = 0;
	for(new pos; pos<size; pos++)
	{
	    if(array[pos] == null) continue;
		if(CallLocalFunction(filter,"d",array[pos])) array[pos] = null, filtered++;
	}
	for(new pos; pos<size; pos++)
	{
	    if(array[pos] == null)
	    {
	        for(new port=(pos+1); port<size; port++)
	        {
				if(array[port] != null)
				{
				    array[pos]  = array[port];
				    array[port] = null;
				    break;
				}
	        }
	    }
	}
	return filtered;
}