#include "stdafx.h"
#include "scripts.h"
#include "CTF.h"

/***** Purchase hook stuff *****/
bool LoadedOnce = false;
// Patch memory
void PatchData(void *buf, unsigned long size, unsigned long addr){
	unsigned long PID = GetCurrentProcessId();
	HANDLE PHandle = OpenProcess(PROCESS_ALL_ACCESS, false, PID);
	WriteProcessMemory(PHandle, (void *)addr, buf, size, NULL);
	CloseHandle(PHandle);
}
void Log_Invalid_PT_Request(GameObject *obj, unsigned int PresetID, float Distance) {
	char Item[128];//, msg[128];
	if (PresetID == -1) strcpy(Item,"Refill");
	else strncpy(Item,Get_Definition_Name(PresetID),sizeof(Item));
	const char *player = Get_Player_Name(obj);
	//sprintf(msg,"%s %s %f",player,Item,Distance);
	//FDSMessage(msg,"_INVALIDPT");
}
// Distance check
bool PT_Distance_Check(GameObject *obj, unsigned int PresetID) {
	//GameObject *ClosestPT = Find_Closest_PT(obj,Commands->Get_Player_Type(obj));
	int PTH = getProfileInt("JBServ","PTHProtect",0,"CTF_Settings.ini");
	if (PTH == 1) {
		int playerid5 = Get_Player_ID(obj);
		int team = Commands->Get_Player_Type(Get_GameObj(playerid5));
		Vector3 p = Commands->Get_Position(Get_GameObj(playerid5));
		Vector3 p2;
		p2 = Commands->Get_Position(Find_Closest_Object_To_Pos_By_Preset(p,"pct_zone_nod"));
		if (team == 0) p2 = Commands->Get_Position(Find_Closest_Object_To_Pos_By_Preset(p,"pct_zone_nod"));
		else if (team == 1) p2 = Commands->Get_Position(Find_Closest_Object_To_Pos_By_Preset(p,"pct_zone_gdi"));
		int distance;
		float distance4 = 0;
		float distance1 = 0;
		float distance2 = 0;
		if (p.X >= p2.X) { distance1 = p.X - p2.X; }
		if (p.X < p2.X) { distance1 = p2.X - p.X; }
		if (p.Y >= p2.Y) { distance2 = p.Y - p2.Y; }
		if (p.Y < p2.Y) { distance2 = p2.Y - p.Y; }
		distance4 = distance1 + distance2;
		distance = (int)distance4;
        int S = getProfileInt("JBServ","PTHProtectLeeway",0,"CTF_Settings.ini");
		int P = getProfileInt("JBServ","PTHProtectpunishment",0,"CTF_Settings.ini");
		if (P < 1 || P > 3) { P = 2; }
		if (S < 1 || S > 5) { S = 2; }
		const char *action;
		action = "justincase";
		if (P == 1) { action = "Announce."; }
		if (P == 2) { action = "Kick."; }
		if (P == 3) { action = "Ban."; }
		if (Get_Vehicle(Get_GameObj(playerid5)) && distance > 15) {
			char message6[512];
			sprintf(message6, "_Cheat Warning PT Hook detected by %s, PT activated while user was in a vehicle. Action: %s\n", Get_Player_Name(obj), action);
			Console_Output(message6);
		}
		else if (S == 1 && distance > 10) { 
			char message1[512];
			sprintf(message1, "_Cheat Warning PT Hook detected by %s, PT activated %d space(s) away from the max distance specified. Action: %s\n", Get_Player_Name(obj), distance - 10, action);
			Console_Output(message1);
		}
		else if (S == 2 && distance > 11) { 
			char message2[512];
			sprintf(message2, "_Cheat Warning PT Hook detected by %s, PT activated %d space(s) away from the max distance specified. Action: %s\n", Get_Player_Name(obj), distance - 11, action);
			Console_Output(message2);
		}
		else if (S == 3 && distance > 12) { 
			char message3[512];
			sprintf(message3, "_Cheat Warning PT Hook detected by %s, PT activated %d space(s) away from the max distance specified. Action: %s\n", Get_Player_Name(obj), distance - 12, action);
			Console_Output(message3);
		}
		else if (S == 4 && distance > 13) { 
			char message4[512];
			sprintf(message4, "_Cheat Warning PT Hook detected by %s, PT activated %d space(s) away from the max distance specified. Action: %s\n", Get_Player_Name(obj), distance - 13, action);
			Console_Output(message4);
		}
		else if (S == 5 && distance > 15) { 
			char message5[512];
			sprintf(message5, "_Cheat Warning PT Hook detected by %s, PT activated %d space(s) away from the max distance specified. Action: %s\n", Get_Player_Name(obj), distance - 15, action);
			Console_Output(message5);
		}
	}
	//float Distance = Commands->Get_Distance(Commands->Get_Position(obj),Commands->Get_Position(ClosestPT));
	/*if (Commands->Get_ID(ClosestPT) && Distance > 10.0f) {
		Log_Invalid_PT_Request(obj,PresetID,Distance);
		return true;
	}
	*/
	return false;
}
// Character purchase hook
int CPH (BaseControllerClass *base, GameObject *purchaser, unsigned int cost, unsigned int preset,const char *data) {
	if (PT_Distance_Check(purchaser,preset)) return 1;
	if (Get_Fly_Mode(purchaser)) { 
		Toggle_Fly_Mode(purchaser); 
	}
	Commands->Send_Custom_Event(purchaser,purchaser,02474,0,0);
	int Purchase = getProfileInt("JBServ","Purchase",0,"CTF_Settings.ini");
	if (Purchase == 1) {
		Commands->Give_Money(purchaser,(float)Get_Character_Price(Commands->Get_Preset_Name(purchaser)),false);
	}
	Commands->Send_Custom_Event(purchaser,purchaser,5829,0,0);
	return -1;
}
// Powerup purchase hook
int PPH(BaseControllerClass *base, GameObject *purchaser, unsigned int cost, unsigned int preset,const char *data) {
	if (PT_Distance_Check(purchaser,preset)) return 1;
	return -1;
	if (Get_Fly_Mode(purchaser)) { 
		Toggle_Fly_Mode(purchaser); 
	}
}
// Vehicle purchase hook
int VPH(BaseControllerClass *base, GameObject *purchaser, unsigned int cost, unsigned int preset,const char *data) {
	if (Get_Fly_Mode(purchaser)) return 1;
	if (PT_Distance_Check(purchaser,preset)) return 1;
	return -1;
}
int __cdecl Refill_Hook(GameObject *obj) {
	if (PT_Distance_Check(obj,(unsigned int)-1)) return 1;
	int Refill = getProfileInt("JBServ","DisableRefills",0,"CTF_Settings.ini");
	if (Refill == 1) { return 1; }
	Grant_Refill(obj);
	return 0;
}
void FDSMessage(const char* Message) {
	currdate.refreshdate();
	char FDSlogname[100], messageprint[512], hours[5], mins[5], secs[5];
	sprintf(FDSlogname, "renlog_%s.txt",currdate.get_date_string());
	FILE *FP;
	if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
	time_t rawtime;
	struct tm *timeinfo;
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	int hour = timeinfo->tm_hour;
	int min = timeinfo->tm_min;
	int sec = timeinfo->tm_sec;
	if (hour < 10) sprintf(hours,"0%d",hour);
	else sprintf(hours,"%d",hour);
	if (min < 10) sprintf(mins,"0%d",min);
	else sprintf(mins,"%d",min);
	if (sec < 10) sprintf(secs,"0%d",sec);
	else sprintf(secs,"%d",sec);
	printf("%s\n",Message);
	sprintf(messageprint,"[%s:%s:%s] %s\n",hours,mins,secs,Message);
	fputs( messageprint, FP );
	fclose( FP );

	/*std::ofstream FDSLog (FDSlogname,ios::app);
	if (FDSLog.is_open()) {
		printf("%s\n",Message);
		sprintf(messageprint,"[%s:%s:%s] %s\n",hours,mins,secs,Message);
		FDSLog << messageprint;
		FDSLog.close();
	}
 */
}

/* ************************************************************************************************************* */

void Chat(int ID,int Type,const wchar_t *Msg);



//Handles all CTF messages
void CTF_Message (int message, const char *Event_Name1, const char *Event_Name2) {
	currdate.refreshdate();
	char FDSlogname[100], messageprint[512], hours[5], mins[5], secs[5], readline[250], msgline[256];
	sprintf(FDSlogname, "ctflog_%s.txt",currdate.get_date_string());
	time_t rawtime;
	struct tm *timeinfo;
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	int hour = timeinfo->tm_hour;
	int min = timeinfo->tm_min;
	int sec = timeinfo->tm_sec;
	if (hour < 10) sprintf(hours,"0%d",hour);
	else sprintf(hours,"%d",hour);
	if (min < 10) sprintf(mins,"0%d",min);
	else sprintf(mins,"%d",min);
	if (sec < 10) sprintf(secs,"0%d",sec);
	else sprintf(secs,"%d",sec);

	std::string Name1(Event_Name1),
				Name2(Event_Name2);

	if (message == 1) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: GDIFlag Stolen %s\n",hours,mins,secs,Event_Name1);
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFGDISTOLEN","GDI's flag has been stolen.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		printf(Message.c_str());
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0001I1EVAG_SND.wav");
		Commands->Create_2D_WAV_Sound("pickupflag.wav");
	}
	else if (message == 2) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: NodFlag Stolen %s\n",hours,mins,secs,Event_Name1);
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFNODSTOLEN","Nod's flag has been stolen.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		printf(Message.c_str());
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0011I1EVAN_SND.wav");
		Commands->Create_2D_WAV_Sound("pickupflag.wav");
	}

	else if (message == 3) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: GDIFlag Dropped %s %s\n",hours,mins,secs,Event_Name1,Event_Name2);
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFGDIDROP","GDI's flag has been dropped.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		printf(Message.c_str());
		Commands->Create_2D_WAV_Sound("dropflag.wav");
	}
	else if (message == 4) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: NodFlag Dropped %s %s\n",hours,mins,secs,Event_Name1,Event_Name2);
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFNODDROP","Nod's flag has been dropped.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		printf(Message.c_str());
		Commands->Create_2D_WAV_Sound("dropflag.wav");
	}

	else if (message == 5) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: GDIFlag PickedUp %s\n",hours,mins,secs,Event_Name1);
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFGDIPICKUP","GDI's flag has been picked up.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		Commands->Create_2D_WAV_Sound("pickupflag.wav");
	}
	else if (message == 6) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: NodFlag PickedUp %s\n",hours,mins,secs,Event_Name1);;
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFNODPICKUP","Nod's flag has been picked up.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		printf(Message.c_str());
		Commands->Create_2D_WAV_Sound("pickupflag.wav");
	}

	else if (message == 7) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: GDIFlag Returned %s\n",hours,mins,secs,Event_Name1);
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFGDIRETURN","GDI's flag has been returned.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		printf(Message.c_str());
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0014I1EVAG_SND.wav");
		Commands->Create_2D_WAV_Sound("levelchange.wav");
	}
	else if (message == 8) {
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: NodFlag Returned %s\n",hours,mins,secs,Event_Name1);
		fputs( messageprint, FP );
		fclose( FP );
		getProfileString("StringTable","CTFNODRETURN","Nod's flag has been returned.",readline,250,"CTF_Settings.ini");
		std::string Message(readline);
		int n;
		while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
		while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
		sprintf(msgline,"msg %s",Message.c_str());
		ConsoleCommand(msgline);
		printf(Message.c_str());
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0044I1EVAN_SND.wav");
		Commands->Create_2D_WAV_Sound("levelchange.wav");
	}

	else if (message == 9) {
		if (EnableCapLimit == 1) {
			FILE *FP;
			if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
			sprintf(messageprint,"[%s:%s:%s] CTF: GDIFlag Captured %s %dCapLeft\n",hours,mins,secs,Event_Name1,(TotalCaptures - NodCaps));
			fputs( messageprint, FP );
			fclose( FP );
			getProfileString("StringTable","CTFGDICAPTURE","GDI's flag has been captured.",readline,250,"CTF_Settings.ini");
			std::string Message(readline);
			int n;
			while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
			while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
			sprintf(msgline,"msg %s",Message.c_str());
			ConsoleCommand(msgline);
			printf(Message.c_str());
		}
		else if (EnableCapLimit == 0) {
			FILE *FP;
			if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
			sprintf(messageprint,"[%s:%s:%s] CTF: GDIFlag Captured %s 0\n",hours,mins,secs,Event_Name1);
			fputs( messageprint, FP );
			fclose( FP );
			getProfileString("StringTable","CTFGDICAPTURE","GDI's flag has been captured.",readline,250,"CTF_Settings.ini");
			std::string Message(readline);
			int n;
			while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
			while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
			sprintf(msgline,"msg %s\nNod needs to capture GDI's flag %d more time(s) to win.",Message.c_str(),(TotalCaptures - NodCaps));
			ConsoleCommand(msgline);
			printf(Message.c_str());
		}
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0042I1EVAN_SND.wav");
		Commands->Create_2D_WAV_Sound("capflag.wav");
	}
	else if (message == 10) {
		if (EnableCapLimit == 1) {
			FILE *FP;
			if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
			sprintf(messageprint,"[%s:%s:%s] CTF: NodFlag Captured %s %dCapLeft\n",hours,mins,secs,Event_Name1,(TotalCaptures - GDICaps));
			fputs( messageprint, FP );
			fclose( FP );
			getProfileString("StringTable","CTFNODCAPTURE","Nod's flag has been captured.",readline,250,"CTF_Settings.ini");
			std::string Message(readline);
			int n;
			while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
			while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
			sprintf(msgline,"msg %s\nGDI needs to capture Nod's flag %d more time(s) to win.",Message.c_str(),(TotalCaptures - GDICaps));
			ConsoleCommand(msgline);
			printf(Message.c_str());
		}
		else if (EnableCapLimit == 0) {
			FILE *FP;
			if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
			sprintf(messageprint,"[%s:%s:%s] CTF: NodFlag Captured %s 0\n",hours,mins,secs,Event_Name1);
			fputs( messageprint, FP );
			fclose( FP );
			getProfileString("StringTable","CTFNODCAPTURE","Nod's flag has been captured.",readline,250,"CTF_Settings.ini");
			std::string Message(readline);
			int n;
			while((n = Message.find("<Name1>")) != std::string::npos) Message.replace(n,7,Name1);
			while((n = Message.find("<Name2>")) != std::string::npos) Message.replace(n,7,Name2);
			sprintf(msgline,"msg %s",Message.c_str());
			ConsoleCommand(msgline);
			printf(Message.c_str());
		}
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0012I1EVAG_SND.wav");
		Commands->Create_2D_WAV_Sound("capflag.wav");
	}

	else if (message == 11) {
		FDSMessage("GDI has captured the flag enough times to win.\n");
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: GDI Wins\n",hours,mins,secs);
		fputs( messageprint, FP );
		fclose( FP );
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0047I1EVAG_SND.wav");
		Commands->Create_2D_WAV_Sound("evicted.wav");
	}
	else if (message == 12) {
		FDSMessage("Nod has captured the flag enough times to win.\n");
		FILE *FP;
		if ( (FP = fopen( FDSlogname, "a")) == NULL ) return;
		sprintf(messageprint,"[%s:%s:%s] CTF: Nod Wins\n",hours,mins,secs);
		fputs( messageprint, FP );
		fclose( FP );
		Commands->Create_2D_WAV_Sound("MXXDSGN_DSGN0043I1EVAN_SND.wav");
		Commands->Create_2D_WAV_Sound("evicted.wav");
	}
}

//Creates the flag at the Ped's location
GameObject *Create_CTF_Flag (int Team, Vector3 PedPosition) {
	Vector3 FlagLoc;
	GameObject *Flag;
	char scriptPar[256];

	FlagLoc = PedPosition;
	FlagLoc.X -= getProfileFloat("Flag","Flag_X",500.000,"Data\\CTF.dat");
	FlagLoc.Y -= getProfileFloat("Flag","Flag_Y",500.000,"Data\\CTF.dat");
	FlagLoc.Z -= getProfileFloat("Flag","Flag_Z",-500.000,"Data\\CTF.dat");
	Flag = Commands->Create_Object(FlagPreset,FlagLoc);
	Commands->Set_Player_Type(Flag,Team);

	sprintf(scriptPar,"%d,%d",Team,Commands->Get_ID(Flag));
	Commands->Attach_Script(Flag,"KAK_CTF_Flag",scriptPar);

	if (Team == 0) {
		NodFlagHolder = 0;
		NodFlagAttached = 0;
	}
	else if (Team == 1) {
		GDIFlagHolder = 0;
		GDIFlagAttached = 0;
	}

	return Flag;
}

//Creates the flag at position X, used for dead flag holder
GameObject *Create_CTF_DropFlag (int Team, Vector3 Position) {
	Vector3 FlagLoc;
	GameObject *Flag;
	char scriptPar[256];

	FlagLoc = Position;
	FlagLoc.X -= getProfileFloat("Flag","Flag_X",500.000,"Data\\CTF.dat");
	FlagLoc.Y -= getProfileFloat("Flag","Flag_Y",500.000,"Data\\CTF.dat");
	FlagLoc.Z -= getProfileFloat("Flag","Flag_Z",-500.000,"Data\\CTF.dat");
	Flag = Commands->Create_Object(FlagPreset,FlagLoc);
	Commands->Set_Player_Type(Flag,Team);

	sprintf(scriptPar,"%d,%d",Team,Commands->Get_ID(Flag));
	Commands->Attach_Script(Flag,"KAK_CTF_DropFlag",scriptPar);

	if (Team == 0) {
		NodFlagHolder = 0;
		NodFlagAttached = Commands->Get_ID(Flag);
	}
	else if (Team == 1) {
		GDIFlagHolder = 0;
		GDIFlagAttached = Commands->Get_ID(Flag);
	}

	return Flag;
}

//Creates the flag at Team's Ped
GameObject *Create_CTF_PedFlag (int Team) {
	Vector3 FlagLoc, PedPosition;
	GameObject *Flag;
	char scriptPar[256], map[256], IDs[100];
	sprintf(IDs,"%d",MapID);
	getProfileString("Map",IDs,"NULL",map,256,"Data\\CTF.dat");

	if (Team == 0) {
		PedPosition.X = getProfileFloat(map,"NodFlag_X",500.000,"Data\\CTF.dat");
		PedPosition.Y = getProfileFloat(map,"NodFlag_Y",500.000,"Data\\CTF.dat");
		PedPosition.Z = getProfileFloat(map,"NodFlag_Z",-500.000,"Data\\CTF.dat");
	}
	else if (Team == 1) {
		PedPosition.X = getProfileFloat(map,"GDIFlag_X",500.000,"Data\\CTF.dat");
		PedPosition.Y = getProfileFloat(map,"GDIFlag_Y",500.000,"Data\\CTF.dat");
		PedPosition.Z = getProfileFloat(map,"GDIFlag_Z",-500.000,"Data\\CTF.dat");
	}

	FlagLoc = PedPosition;
	FlagLoc.X -= getProfileFloat("Flag","Flag_X",500.000,"Data\\CTF.dat");
	FlagLoc.Y -= getProfileFloat("Flag","Flag_Y",500.000,"Data\\CTF.dat");
	FlagLoc.Z -= getProfileFloat("Flag","Flag_Z",-500.000,"Data\\CTF.dat");
	Flag = Commands->Create_Object(FlagPreset,FlagLoc);
	Commands->Set_Player_Type(Flag,Team);

	sprintf(scriptPar,"%d,%d",Team,Commands->Get_ID(Flag));
	Commands->Attach_Script(Flag,"KAK_CTF_Flag",scriptPar);

	if (Team == 0) {
		NodFlagHolder = 0;
		NodFlagAttached = 0;
	}
	else if (Team == 1) {
		GDIFlagHolder = 0;
		GDIFlagAttached = 0;
	}

	return Flag;
}

//Creates Team's CTF Ped
void Create_CTF_Pedestal (int Team, int ID) {
	Vector3 PedLoc;
	GameObject *Ped, *Flag;
    char map[256], IDs[100];
	sprintf(IDs,"%d",ID);
	getProfileString("Map",IDs,"NULL",map,256,"Data\\CTF.dat");

	if (Team == 0) {
		PedLoc.X = getProfileFloat(map,"NodFlag_X",500.000,"Data\\CTF.dat");
		PedLoc.Y = getProfileFloat(map,"NodFlag_Y",500.000,"Data\\CTF.dat");
		PedLoc.Z = getProfileFloat(map,"NodFlag_Z",-500.000,"Data\\CTF.dat");
		Ped = Commands->Create_Object(NodPedPreset,PedLoc);
	}
	else if (Team == 1) {
		PedLoc.X = getProfileFloat(map,"GDIFlag_X",500.000,"Data\\CTF.dat");
		PedLoc.Y = getProfileFloat(map,"GDIFlag_Y",500.000,"Data\\CTF.dat");
		PedLoc.Z = getProfileFloat(map,"GDIFlag_Z",-500.000,"Data\\CTF.dat");
		Ped = Commands->Create_Object(GDIPedPreset,PedLoc);
	}

	Commands->Attach_Script(Ped,"KAK_Prevent_Kill","");
	Flag = Create_CTF_Flag(Team,PedLoc);
}

/* ************************************************************************************************************* */

//Core of CTF, controls the flag that rests on the Ped
void KAK_CTF_Flag::Custom(GameObject *obj, int message, int param, GameObject *sender) {
	if (message == CUSTOM_EVENT_POWERUP && Get_Int_Parameter("Team") != Commands->Get_Player_Type(sender)) {		//Enemy picked up the flag
		//Destroy flag on Ped
		Commands->Destroy_Object(Commands->Find_Object(Get_Int_Parameter("FlagID")));

		//Put flag on person
		GameObject *AttachedFlag = Commands->Create_Object_At_Bone(sender,FlagBackPreset,"bone for bag");
		Commands->Attach_To_Object_Bone(AttachedFlag, sender, "bone for bag");
		Commands->Disable_All_Collisions(AttachedFlag);
		Commands->Set_Player_Type(AttachedFlag,Get_Int_Parameter("Team"));
		
		if (WeaponForHolder == 0) {
			Commands->Give_Powerup(sender,"POW_Uplink",true);
			Commands->Attach_Script(sender,"KAK_Select_Flag","");
		}
		if (RegenForHolder == 1) {
			Commands->Attach_Script(sender,"KAK_FlagHolder_Regen","");
		}
		Commands->Attach_Script(sender,"KAK_FlagHolder_Appear","");

		if (SpoilsofWar == 1) {
			//Spoils of War for picking up flag
			Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","StealPointsTeam",0.0f,"CTF_Settings.ini"),true);
			Commands->Give_Points(sender,(getProfileFloat("SpoilsOfWar","StealPointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
			Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","StealPointsInd",0.0f,"CTF_Settings.ini"),false);
			Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","StealPointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
			Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","StealPointsInd",0.0f,"CTF_Settings.ini") * -1),false);
			Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","StealCreditsTeam",0.0f,"CTF_Settings.ini"),true);
			Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","StealCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
			Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","StealCreditsInd",0.0f,"CTF_Settings.ini"),false);
		}

		//Set information for later use
		if (Get_Int_Parameter("Team") == 0) {
			NodFlagHolder = Commands->Get_ID(sender);
			NodFlagAttached = Commands->Get_ID(AttachedFlag);
			CTF_Message(2,PlayerName(sender),"");
			NodFlagLoc = 2;
		}
		else if (Get_Int_Parameter("Team") == 1) {
			GDIFlagHolder = Commands->Get_ID(sender);
			GDIFlagAttached = Commands->Get_ID(AttachedFlag);
			CTF_Message(1,PlayerName(sender),"");
			GDIFlagLoc = 2;
		}
	}
	else if (message == CUSTOM_EVENT_POWERUP && Get_Int_Parameter("Team") == Commands->Get_Player_Type(sender)) {	//Friendly picked up the flag (Maybe capture, maybe idiot)
		if (Get_Int_Parameter("Team") == 0 && GDIFlagHolder == Commands->Get_ID(sender)) {		//If Nod captured GDI's Flag
			GDIFlagLoc = 1;
			//Destroy flag on person
			Commands->Destroy_Object(Commands->Find_Object(GDIFlagAttached));
			Commands->Attach_Script(sender,"KAK_Capture_Animation","");
			Commands->Send_Custom_Event(obj,sender,24156,0,0);

			if (SpoilsofWar == 1) {
				//Spoils of War for capturing flag
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","CapturePointsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Points(sender,(getProfileFloat("SpoilsOfWar","CapturePointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","CapturePointsInd",0.0f,"CTF_Settings.ini"),false);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","CapturePointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","CapturePointsInd",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","CaptureCreditsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","CaptureCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","CaptureCreditsInd",0.0f,"CTF_Settings.ini"),false);
			}
			
			//Recreate flag on ped, update information
			GDIFlagHolder = 0;
			GDIFlagAttached = 0;
			NodCaps++;

			if (NodCaps >= TotalCaptures && EnableCapLimit == 1) {	//If Nod reached the cap limit, end game
				CTF_Message(11,"","");
				AllowKillBuilding = true;
				char map[256], IDs[100];
				sprintf(IDs,"%d",MapID);
				getProfileString("Map",IDs,"NULL",map,256,"Data\\CTF.dat");
				int BuildingAmount = getProfileInt(map,"GDIBuildings",0,"Data\\CTF.dat");
				int i = 1;
				while (i <= BuildingAmount) {
					char BuildingIDTxt[256];
					sprintf(BuildingIDTxt,"GDIBuilding%d",i);
					Commands->Apply_Damage(Commands->Find_Object(getProfileInt(map,BuildingIDTxt,0,"Data\\CTF.dat")),99999,"Explosive",false);
					i++;
				}
			}
			else {
				CTF_Message(9,PlayerName(sender),"");
				Create_CTF_PedFlag(0);
				Create_CTF_PedFlag(1);
			}
		}
		else if (Get_Int_Parameter("Team") == 1 && NodFlagHolder == Commands->Get_ID(sender)) {		//Else If Nod captured GDI's Flag
			NodFlagLoc = 1;
			//Destroy flag on person
			Commands->Destroy_Object(Commands->Find_Object(NodFlagAttached));
			Commands->Attach_Script(sender,"KAK_Capture_Animation","");
			Commands->Send_Custom_Event(obj,sender,24156,0,0);
			
			if (SpoilsofWar == 1) {
				//Spoils of War for capturing flag
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","CapturePointsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Points(sender,(getProfileFloat("SpoilsOfWar","CapturePointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","CapturePointsInd",0.0f,"CTF_Settings.ini"),false);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","CapturePointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","CapturePointsInd",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","CaptureCreditsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","CaptureCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","CaptureCreditsInd",0.0f,"CTF_Settings.ini"),false);
			}
			
			//Recreate flag on Ped
			NodFlagHolder = 0;
			NodFlagAttached = 0;
			GDICaps++;

			if (GDICaps >= TotalCaptures  && EnableCapLimit == 1) {			//If GDI reached the cap limit, end game
				CTF_Message(12,"","");
				AllowKillBuilding = true;
				char map[256], IDs[100];
				sprintf(IDs,"%d",MapID);
				getProfileString("Map",IDs,"NULL",map,256,"Data\\CTF.dat");
				int BuildingAmount = getProfileInt(map,"NodBuildings",0,"Data\\CTF.dat");
				int i = 1;
				while (i <= BuildingAmount) {
					char BuildingIDTxt[256];
					sprintf(BuildingIDTxt,"NodBuilding%d",i);
					Commands->Apply_Damage(Commands->Find_Object(getProfileInt(map,BuildingIDTxt,0,"Data\\CTF.dat")),99999,"Explosive",false);
					i++;
				}
			}
			else {
				CTF_Message(10,PlayerName(sender),"");
				Create_CTF_PedFlag(0);
				Create_CTF_PedFlag(1);
			}
		}
		else Create_CTF_PedFlag(Commands->Get_Player_Type(obj));
	}
}

/* ************************************************************************************************************* */

//Controls the flag that drops from the dead flag holder
void KAK_CTF_DropFlag::Custom(GameObject *obj, int message, int param, GameObject *sender) {
	if (message == CUSTOM_EVENT_POWERUP && Get_Int_Parameter("Team") != Commands->Get_Player_Type(sender)) {		//Enemy picked up the friendly's fallen flag
		//Put flag on new holder
		GameObject *AttachedFlag = Commands->Create_Object_At_Bone(sender,FlagBackPreset,"bone for bag");
		Commands->Attach_To_Object_Bone(AttachedFlag, sender, "bone for bag");
		Commands->Disable_All_Collisions(AttachedFlag);
		Commands->Set_Player_Type(AttachedFlag,Get_Int_Parameter("Team"));
		
		if (WeaponForHolder == 0) {
			Commands->Give_Powerup(sender,"POW_Uplink",true);
			Commands->Attach_Script(sender,"KAK_Select_Flag","");
		}
		if (RegenForHolder == 1) {
			Commands->Attach_Script(sender,"KAK_FlagHolder_Regen","");
		}
		Commands->Attach_Script(sender,"KAK_FlagHolder_Appear","");

		if (SpoilsofWar == 1) {
			//Spoils of War for picking up fallen flag
			Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","PickupPointsTeam",0.0f,"CTF_Settings.ini"),true);
			Commands->Give_Points(sender,(getProfileFloat("SpoilsOfWar","PickupPointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
			Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","PickupPointsInd",0.0f,"CTF_Settings.ini"),false);
			Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","PickupPointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
			Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","PickupPointsInd",0.0f,"CTF_Settings.ini") * -1),false);
			Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","PickupCreditsTeam",0.0f,"CTF_Settings.ini"),true);
			Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","PickupCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
			Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","PickupCreditsInd",0.0f,"CTF_Settings.ini"),false);
		}

		//Setup new information
		if (Get_Int_Parameter("Team") == 0) {
			Commands->Destroy_Object(Commands->Find_Object(NodFlagAttached));
			NodFlagHolder = Commands->Get_ID(sender);
			NodFlagAttached = Commands->Get_ID(AttachedFlag);
			CTF_Message(6,PlayerName(sender),"");
			NodFlagLoc = 2;
		}
		else if (Get_Int_Parameter("Team") == 1) {
			Commands->Destroy_Object(Commands->Find_Object(GDIFlagAttached));
			GDIFlagHolder = Commands->Get_ID(sender);
			GDIFlagAttached = Commands->Get_ID(AttachedFlag);
			CTF_Message(5,PlayerName(sender),"");
			GDIFlagLoc = 2;
		}
	}
	else if (message == CUSTOM_EVENT_POWERUP && Get_Int_Parameter("Team") == Commands->Get_Player_Type(sender)) {		//Friendly picked up the friendly's fallen flag
		if (Get_Int_Parameter("Team") == 0) {		//If Player is on Nod
			//Reput flag on Nod Ped
			Create_CTF_PedFlag(0);
			Commands->Destroy_Object(Commands->Find_Object(Get_Int_Parameter("FlagID")));

			if (SpoilsofWar == 1) {
				//Spoils of War for returning flag
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","ReturnPointsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Points(sender,(getProfileFloat("SpoilsOfWar","ReturnPointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","ReturnPointsInd",0.0f,"CTF_Settings.ini"),false);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","ReturnPointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","ReturnPointsInd",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","ReturnCreditsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","ReturnCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","ReturnCreditsInd",0.0f,"CTF_Settings.ini"),false);
			}

			CTF_Message(8,PlayerName(sender),"");
			NodFlagLoc = 1;
		}
		else if (Get_Int_Parameter("Team") == 1) {	//Else if Player is on GDI
			//Reput flag on GDI Ped
			Create_CTF_PedFlag(1);
			Commands->Destroy_Object(Commands->Find_Object(Get_Int_Parameter("FlagID")));

			if (SpoilsofWar == 1) {
				//Spoils of War for returning flag
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","ReturnPointsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Points(sender,(getProfileFloat("SpoilsOfWar","ReturnPointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Points(sender,getProfileFloat("SpoilsOfWar","ReturnPointsInd",0.0f,"CTF_Settings.ini"),false);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","ReturnPointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","ReturnPointsInd",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","ReturnCreditsTeam",0.0f,"CTF_Settings.ini"),true);
				Commands->Give_Money(sender,(getProfileFloat("SpoilsOfWar","ReturnCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
				Commands->Give_Money(sender,getProfileFloat("SpoilsOfWar","ReturnCreditsInd",0.0f,"CTF_Settings.ini"),false);
			}

			CTF_Message(7,PlayerName(sender),"");
			GDIFlagLoc = 1;
		}
	}
}

void KAK_CTF_DropFlag::Created(GameObject *obj) {
	if (getProfileFloat("Settings","ReturnDropFlagTimer",0.0f,"CTF_Settings.ini") > 0.0f) Commands->Start_Timer(obj,this,getProfileFloat("Settings","ReturnDropFlagTimer",0.0f,"CTF_Settings.ini"),1);
}

void KAK_CTF_DropFlag::Timer_Expired(GameObject *obj, int number) {
	if (number == 1) {
		if (Get_Int_Parameter("Team") == 0) {
			Create_CTF_PedFlag(0);
			Commands->Destroy_Object(Commands->Find_Object(Get_Int_Parameter("FlagID")));
			CTF_Message(8,"the Server","");
			NodFlagLoc = 1;
		}
		else if (Get_Int_Parameter("Team") == 1) {
			Create_CTF_PedFlag(1);
			Commands->Destroy_Object(Commands->Find_Object(Get_Int_Parameter("FlagID")));
			CTF_Message(7,"the Server","");
			GDIFlagLoc = 1;
		}
	}
}
float Get_Character_Price (const char* Preset)
{
	float Price = 0;
	if (stricmp(Preset,"CnC_GDI_Engineer_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_GDI_Engineer_2SF") == 0) Price = 350;
	else if (stricmp(Preset,"CnC_GDI_Grenadier_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_GDI_Grenadier_2SF") == 0) Price = 450;
	else if (strstr(Preset,"CnC_Ignatio_Mobius")) Price = 1000;
	else if (stricmp(Preset,"CnC_GDI_MiniGunner_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_GDI_MiniGunner_1Off") == 0) Price = 175;
	else if (stricmp(Preset,"CnC_GDI_MiniGunner_2SF") == 0) Price = 500;
	else if (strstr(Preset,"CnC_GDI_MiniGunner_3Boss")) Price = 1000;
	else if (strstr(Preset,"CnC_GDI_MiniGunner_3Boss_ALT2")) Price = 1000;
	else if (strstr(Preset,"CnC_GDI_MiniGunner_3Boss_ALT3")) Price = 1000;
	else if (strstr(Preset,"CnC_GDI_MiniGunner_3Boss_ALT4")) Price = 1000;
	else if (stricmp(Preset,"CnC_GDI_RocketSoldier_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_GDI_RocketSoldier_1Off") == 0) Price = 225;
	else if (stricmp(Preset,"CnC_GDI_RocketSoldier_2SF") == 0) Price = 400;
	else if (stricmp(Preset,"CnC_Nod_Engineer_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_Nod_FlameThrower_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_Nod_FlameThrower_1Off") == 0) Price = 150;
	else if (stricmp(Preset,"CnC_Nod_FlameThrower_2SF") == 0) Price = 400;
	else if (strstr(Preset,"CnC_Nod_FlameThrower_3Boss")) Price = 1000;
	else if (stricmp(Preset,"CnC_Nod_Minigunner_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_Nod_Minigunner_1Off") == 0) Price = 175;
	else if (stricmp(Preset,"CnC_Nod_Minigunner_2SF") == 0) Price = 500;
	else if (strstr(Preset,"CnC_Nod_Minigunner_3Boss")) Price = 1000;
	else if (strstr(Preset,"CnC_Nod_MiniGunner_3Boss")) Price = 1000;
	else if (stricmp(Preset,"CnC_Nod_RocketSoldier_0") == 0) Price = 0;
	else if (stricmp(Preset,"CnC_Nod_RocketSoldier_1Off") == 0) Price = 225;
	else if (stricmp(Preset,"CnC_Nod_RocketSoldier_2SF") == 0) Price = 450;
	else if (strstr(Preset,"CnC_Nod_RocketSoldier_3Boss")) Price = 1000;
	else if (stricmp(Preset,"CnC_Nod_Technician_0") == 0) Price = 350;
	else if (strstr(Preset,"CnC_Sydney_PowerSuit")) Price = 1000;
	else if (strstr(Preset,"CnC_Sydney_PowerSuit_ALT2")) Price = 1000;
	else if (stricmp(Preset,"CnC_Sydney") == 0) Price = 150;
	else if (strcmp(Preset,"CnC_Ignatio_Mobius_ALT2") == 0) Price = 1000;
	else if (strcmp(Preset,"CnC_Nod_MiniGunner_3Boss_ALT2") == 0) Price = 1000;
	else if (strcmp(Preset,"CnC_Nod_RocketSoldier_3Boss_ALT2") == 0) Price = 1000;
	else if (strcmp(Preset,"CnC_Nod_FlameThrower_3Boss") == 0) Price = 1000;
	return Price;
}
/* ************************************************************************************************************* */

//Map Load Crud
void RecognizeMap(int ID,bool IsLoad) {
	char IDKey[20], MapName[30];
	sprintf(IDKey,"%d",ID);
	getProfileString("Map",IDKey,"NULL",MapName,30,"Data\\CTF.dat");
	if (IDDebug == 1) {
		char DebugMsg[100];
		sprintf(DebugMsg,"MapID_Debug: %d would work to identify this map in the CTF.dat file.",ID);
		FDSMessage(DebugMsg);
	}
	if (strcmp(MapName,"NULL") != 0) {
		if (IsLoad == true) {
			Vector3 position5820;
			position5820.X = 0.000000f;
			position5820.Y = 0.000000f;
			position5820.Z = 10.000000f;
			GameObject *obj5820 = Commands->Create_Object("Invisible_Object", position5820);
			Commands->Set_Is_Visible(obj5820,false);
			Set_Max_Health(obj5820,99999);
			Set_Max_Shield_Strength(obj5820,99999);
			Commands->Set_Shield_Type(obj5820,"Blamo");
			Commands->Set_Health(obj5820,99999);
			Commands->Set_Shield_Strength(obj5820,99999);
			Commands->Set_Model(obj5820,"NULL");
			Commands->Disable_All_Collisions(obj5820);
			Commands->Disable_Physical_Collisions(obj5820);
			Commands->Attach_Script(obj5820,"Purchase","");
			Commands->Attach_Script(obj5820,"Destroy_Bots","");
			int RGH = getProfileInt("JBServ","RGHProtect",0,"CTF_Settings.ini");
			if (RGH == 1) {
				char RGHmessage[512];
				sprintf(RGHmessage, "message (JBServ): RGH Protection script loaded!");
				Console_Input(RGHmessage);
			}
			int PTH = getProfileInt("JBServ","PTHProtect",0,"CTF_Settings.ini");
			if (PTH == 1) {
				char PTHmessage[512];
				sprintf(PTHmessage, "message (JBServ): PTH Protection script loaded!");
				Console_Input(PTHmessage);
			}
			AllowKillBuilding = false;
			MapID = ID;
			if (!LoadedOnce) { // Prevent the hook loaded more than once.
				AddCharacterPurchaseHook(CPH,"1");
				AddPowerupPurchaseHook(PPH,"1");
				AddVehiclePurchaseHook(VPH,"1");
				AddChatHook(Chat);
				// Refill hook thanks to StealthEye.
				int buf = (int)&Refill_Hook - 0x004701BE -5;
				PatchData((void *)&buf, 4, 0x004701BF);
				unsigned char NopsPatchBuffer[] = { 0x8B, 0xE8 };
				PatchData((void *)&NopsPatchBuffer, 2, 0x004701C6);
				LoadedOnce = true;
			}
			char map[256], IDs[256];
			sprintf(IDs,"%d",MapID);
			getProfileString("Map",IDs,"NULL",map,256,"Data\\CTF.dat");
			if (EnableCapLimit == 1) TotalCaptures = getProfileInt("CapLimit",map,5,"CTF_Settings.ini");

			Create_CTF_Pedestal(0,MapID);
			Create_CTF_Pedestal(1,MapID);
			GDIFlagLoc = 1;
			NodFlagLoc = 1;
			Commands->Attach_Script(Commands->Find_Object(ID),"KAK_Flag_Icon","0");
			Commands->Attach_Script(Commands->Find_Object(ID),"KAK_Flag_Icon","1");

			rename("data\\objects.ddb","data\\bjects.ddb");
			strcpy(CurrMap,MapName);

			int PSet_SvsT = SvSKillMsgTotal;
			SvSKillMsgTotal = 0;
			for (int a = 1; a <= getProfileInt("SvSKillMsg","Total",0,"CTF_Settings.ini"); a++) {
				char key[5],value[256];
				sprintf(key,"Str%d",a);
				getProfileString("SvSKillMsg",key,"Error_BadKillMsg",value,256,"CTF_Settings.ini");
				SvSKillMsg.push_back(value);
				SvSKillMsgTotal++;
			}

			if (FirstGame) {
				FirstGame = false;
				FDSMessage("First map loaded, auto gameovering now..");
				ConsoleCommand("gameover");
			}
			else {
				if (ShowAnnouncement == true) {
					ShowAnnouncement = false;
					if (EnableCTF == 1) {
						char mapname[100];
						sprintf(mapname,"-> Map \"%s\" detected, loading settings..",MapName);
						FDSMessage(mapname);
						printf("Running in CTF Mode.\n");
						if (WeaponForHolder == 1) printf("Flag holder may use a weapon.\n");
						else if (WeaponForHolder == 0) printf("Flag holder may NOT use a weapon.\n");
						if (getProfileFloat("Settings","RegenAmount",2.0f,"CTF_Settings.ini") > 0.0f && getProfileFloat("Settings","RegenAmount",2.0f,"CTF_Settings.ini") <= 5.0f) RegenAmount = getProfileFloat("Settings","RegenAmount",2.0f,"CTF_Settings.ini");
						else RegenAmount = 2.0f;
						if (RegenForHolder == 1) printf("Flag holder will have %f health regenerated every second.\n",RegenAmount);
						else if (RegenForHolder == 0) printf("Flag holder will not have health regenerated.\n");
						if (DropWeapons == 1) printf("Weapons will drop from fallen soldiers.\n");
						else if (DropWeapons == 0) printf("The greedy dead people will keep their guns.\n");
						if (SpoilsofWar == 1) printf("Spoils of War are turned on.\n");
						else if (SpoilsofWar == 0) printf("Spoils of War are turned off.\n");
						if (EnableCapLimit == 1) printf("Following Cap Limit rules.  This round's limit is %d.\n",TotalCaptures);
						else if (EnableCapLimit == 0) printf("Unlimited captures allowed.\n");

						GameObject *AnnouncementThing = Commands->Create_Object("Invisible_Object",Commands->Get_Position(Commands->Find_Object(ID)));
						Commands->Attach_Script(AnnouncementThing,"KAK_Limit_Announcement","");
					}
				}
			}
			Vector3 loc; loc.X = 0.0f; loc.Y = 0.0f; loc.Z = 0.0f;

			if (HaveWeather == 1) {
				char params[20];
				sprintf(params,"%d",ID);
				GameObject *temp = Commands->Create_Object("Invisible_Object",loc);
				Commands->Attach_Script(temp,"KAK_Random_Weather",params);
			}
		}
		else {
			rename("data\\bjects.ddb","data\\objects.ddb");
			SvSKillMsg.erase(SvSKillMsg.begin(),SvSKillMsg.end());
			GDIFlagHolder = 0;
			NodFlagHolder = 0;
			GDIFlagAttached = 0;
			NodFlagAttached = 0;
			MapID = 0;
			GDICaps = 0;
			NodCaps = 0;
			AllowKillBuilding = false;

			ShowAnnouncement = true;
			EnableCTF = getProfileInt("Settings","EnableCTFMode",1,"CTF_Settings.ini");
			WeaponForHolder= getProfileInt("Settings","AllowWeaponForHolder",0,"CTF_Settings.ini");
			DropWeapons = getProfileInt("Settings","AllowDropWeapons",1,"CTF_Settings.ini");
			SpoilsofWar = getProfileInt("Settings","AllowSpoilsofWar",1,"CTF_Settings.ini");
			EnableCapLimit = getProfileInt("Settings","AllowCapLimit",1,"CTF_Settings.ini");
			RegenForHolder = getProfileInt("Settings","AllowRegenForHolder",0,"CTF_Settings.ini");
			getProfileString("Settings","FlagPresetName","POW_Data_Disc",FlagPreset,100,"CTF_Settings.ini");
			getProfileString("Settings","GDIPedPresetName","CtfPedestalGold",GDIPedPreset,100,"CTF_Settings.ini");
			getProfileString("Settings","NodPedPresetName","CtfPedestalRed",NodPedPreset,100,"CTF_Settings.ini");
			getProfileString("Settings","FlagBackPresetName","CtfFlag",FlagBackPreset,100,"CTF_Settings.ini");
			ShowPlayerKillMessage = getProfileInt("Settings","ShowKillMessages",1,"CTF_Settings.ini");
			IDDebug = getProfileInt("Settings","DebugIDMode",1,"CTF_Settings.ini");
			HaveWeather = getProfileInt("Settings","EnableWeather",1,"CTF_Settings.ini");
			//Perform Cleanup Code Here
		}
	}
}
const char *WideCharToChar2(const wchar_t *wcs)
{
	int length = wcslen(wcs);
	char *text = new char[length+1];
	wcstombs(text,wcs,length+1);
	return text;
}
void Chat(int ID, int Type, const wchar_t *Msg) {
	if (Type == 2) return;
	//if (Settings->Gamelog && Settings->NewGamelog) Gamelog_Chat_Hook(ID,Type,Msg);
	if (Type == 1)
	{
		const char *Msg2 = WideCharToChar2(Msg);
		if (stricmp(Msg2,"!status") == 0) {
			int JB = getProfileInt("JBServ","AllowUpgrades",0,"CTF_Settings.ini");
			GameObject *obj = Get_GameObj(ID);
			int playerid = Get_Player_ID(obj);
			char m[512];
			if (JB != 1) { 
				sprintf(m,"ppage %d Upgrades have been disabled on this server.",playerid);
			}
			else {
				int l4s = getProfileInt("JBServ","level4score",0,"CTF_Settings.ini");
				int l3s = getProfileInt("JBServ","level3score",0,"CTF_Settings.ini");
				int l2s = getProfileInt("JBServ","level2score",0,"CTF_Settings.ini");
				int l1s = getProfileInt("JBServ","level1score",0,"CTF_Settings.ini");
				int l4k = getProfileInt("JBServ","level4kills",0,"CTF_Settings.ini");
				int l3k = getProfileInt("JBServ","level3kills",0,"CTF_Settings.ini");
				int l2k = getProfileInt("JBServ","level2kills",0,"CTF_Settings.ini");
				int l1k = getProfileInt("JBServ","level1kills",0,"CTF_Settings.ini");
				if (Commands->Get_Points(obj) > l1s && Get_Kills(Get_Player_ID(obj)) >= l1k) {
					sprintf(m,"ppage %d Your current status level is Level 1. This is the highest level you can achieve.",playerid);
					Console_Input(m);
					sprintf(m,"ppage %d Your rewards for being Level 1: +50 Max Health, +50 Max Shield, +8 Health/Shield & Credits every second.",playerid);
					Console_Input(m);
				}
				else if (Commands->Get_Points(obj) > l2s && Get_Kills(Get_Player_ID(obj)) >= l2k) {
					sprintf(m,"ppage %d Your current status level is Level 2. You must have %d points and %d kills to reach Level 1.",playerid,l1s,l1k);
					Console_Input(m);
					sprintf(m,"ppage %d Your rewards for being Level 2: +30 Max Health, +30 Max Shield, +5 Health/Shield & Credits every second.",playerid);
					Console_Input(m);
				}
				else if (Commands->Get_Points(obj) > l3s && Get_Kills(Get_Player_ID(obj)) >= l3k) {
					sprintf(m,"ppage %d Your current status level is Level 3. You must have %d points and %d kills to reach Level 2.",playerid,l2s,l2k);
					Console_Input(m);
					sprintf(m,"ppage %d Your rewards for being Level 3: +20 Max Health, +20 Max Shield, +3 Health/Shield & Credits every second.",playerid);
					Console_Input(m);
				}
				else if (Commands->Get_Points(obj) > l4s && Get_Kills(Get_Player_ID(obj)) >= l4k) {
					sprintf(m,"ppage %d Your current status level is Level 4. You must have %d points and %d kills to reach Level 3.",playerid,l3s,l3k);
					Console_Input(m);
					sprintf(m,"ppage %d Your rewards for being Level 4: +10 Max Health, +10 Max Shield, +2 Health/Shield & Credits every second.",playerid);
						Console_Input(m);
				}
				else {
					sprintf(m,"ppage %d Your current status level is Level 5. You must have %d points and %d kills to reach Level 4.",playerid,l4s,l4k);
					Console_Input(m);
					sprintf(m,"ppage %d This is the lowest possible status level, so you do not recieve any rewards.",playerid);
					Console_Input(m);
				}
			}
		}
	}
}
/* ************************************************************************************************************* */

//Loop fake gun so Player with Flag can not pull out weapon
void KAK_Select_Flag::Created(GameObject *obj) {
	Commands->Start_Timer(obj,this,0.1f,1);
}

void KAK_Select_Flag::Timer_Expired(GameObject *obj, int number) {
	if (number == 1) {
		Commands->Select_Weapon(obj,"Weapon_MineTimed_Player");
		Commands->Start_Timer(obj,this,0.1f,1);
	}
}

void KAK_Select_Flag::Custom(GameObject *obj, int message, int param, GameObject *sender) {
	if (message == 24156) {
		Destroy_Script();
	}
}

/* ************************************************************************************************************* */

//Loop regen health for flag holder
void KAK_FlagHolder_Regen::Created(GameObject *obj) {
	Commands->Start_Timer(obj,this,1.0f,1);
	IsAlive = true;
}

void KAK_FlagHolder_Regen::Timer_Expired(GameObject *obj, int number) {
	if (number == 1 && IsAlive) {
		if (Commands->Get_Health(obj) == Commands->Get_Max_Health(obj)) {
			if (Commands->Get_Shield_Strength(obj) != Commands->Get_Max_Shield_Strength(obj)) {
				if (IsAlive) Commands->Set_Shield_Strength(obj,(Commands->Get_Shield_Strength(obj) + RegenAmount));
			}
		}
		else {
			if (IsAlive) Commands->Set_Health(obj,(Commands->Get_Health(obj) + RegenAmount));
		}
		Commands->Start_Timer(obj,this,1.0f,1);
	}
}

void KAK_FlagHolder_Regen::Damaged(GameObject *obj, GameObject *damager, float damage) {
	if (Commands->Get_Health(obj) == 0 && Commands->Get_Shield_Strength(obj) == 0) IsAlive = false;
}

void KAK_FlagHolder_Regen::Killed(GameObject *obj, GameObject *shooter) {
	IsAlive = false;
}

void KAK_FlagHolder_Regen::Custom(GameObject *obj, int message, int param, GameObject *sender) {
	if (message == 24156) {
		Destroy_Script();
	}
}

/* ************************************************************************************************************* */

//Loop to make flag holder appear (SBH Fix)
void KAK_FlagHolder_Appear::Created(GameObject *obj) {
	Commands->Start_Timer(obj,this,0.01f,1);
}

void KAK_FlagHolder_Appear::Timer_Expired(GameObject *obj, int number) {
	if (number == 1) {
		Commands->Apply_Damage(obj,0,"Harmless",false);
		Commands->Start_Timer(obj,this,0.01f,1);
	}
}

void KAK_FlagHolder_Appear::Killed(GameObject *obj, GameObject *shooter) {
	Destroy_Script();
}

void KAK_FlagHolder_Appear::Custom(GameObject *obj, int message, int param, GameObject *sender) {
	if (message == 24156) {
		Destroy_Script();
	}
}

/* ************************************************************************************************************* */

void KAK_Limit_Announcement::Created(GameObject *obj) {
	Commands->Start_Timer(obj,this,30.0f,1);
}

void KAK_Limit_Announcement::Timer_Expired(GameObject *obj, int number) {
	if (number == 1) {
		char msgline[256];
		ConsoleCommand("msg Running in CTF mode, created by Vloktboky and the people at Black-Cell.net.");
		if (EnableCapLimit == 1) {
			sprintf(msgline,"msg This round's capture limit is %d.",TotalCaptures);
			ConsoleCommand(msgline);
			Commands->Start_Timer(obj,this,180.0f,2);
		}
	}
	else if (number == 2) {
		char msgline[256];
		sprintf(msgline,"msg GDI needs to capture Nod's flag %d more time(s) to win.  Nod needs to capture GDI's flag %d more time(s) to win.",(TotalCaptures - GDICaps),(TotalCaptures - NodCaps));
		ConsoleCommand(msgline);
		Commands->Start_Timer(obj,this,180.0f,2);
	}
}

/* ************************************************************************************************************* */

void M00_BUILDING_EXPLODE_NO_DAMAGE_DAK::Created(GameObject *obj) {
	if (EnableCTF == 1) {
		if (ShowStartMessage == false) {
			ShowStartMessage = true;
		}

		RecognizeMap(Commands->Get_ID(obj),true);
		
		/*if (strstr(Commands->Get_Preset_Name(obj),"mp_GDI_Barracks")) {
			rename("Data\\objects.ddb","Data\\bjects.ddb");
			AllowKillBuilding = false;
			MapID = Commands->Get_ID(obj);

			char map[256], IDs[256];
			sprintf(IDs,"%d",MapID);
			getProfileString("Map",IDs,"NULL",map,256,"Data\\CTF.dat");
			if (EnableCapLimit == 1) TotalCaptures = getProfileInt("CapLimit",map,5,"CTF_Settings.ini");

			Create_CTF_Pedestal(0,MapID);
			Create_CTF_Pedestal(1,MapID);
			GDIFlagLoc = 1;
			NodFlagLoc = 1;
			Commands->Attach_Script(obj,"KAK_Flag_Icon","0");
			Commands->Attach_Script(obj,"KAK_Flag_Icon","1");

			if (FirstGame == true) {
				FirstGame = false;
				ConsoleCommand("gameover");
			}
			else if (ShowAnnouncement == true) {
				ShowAnnouncement = false;
				if (EnableCTF == 1) {
					printf("Running in CTF Mode.\n");
					if (WeaponForHolder == 1) printf("Flag holder may use a weapon.\n");
					else if (WeaponForHolder == 0) printf("Flag holder may NOT use a weapon.\n");
					if (getProfileFloat("Settings","RegenAmount",2.0f,"CTF_Settings.ini") > 0.0f && getProfileFloat("Settings","RegenAmount",2.0f,"CTF_Settings.ini") <= 5.0f) RegenAmount = getProfileFloat("Settings","RegenAmount",2.0f,"CTF_Settings.ini");
					else RegenAmount = 2.0f;
					if (RegenForHolder == 1) printf("Flag holder will have %f health regenerated every second.\n",RegenAmount);
					else if (RegenForHolder == 0) printf("Flag holder will not have health regenerated.\n");
					if (DropWeapons == 1) printf("Weapons will drop from fallen soldiers.\n");
					else if (DropWeapons == 0) printf("The greedy dead people will keep their guns.\n");
					if (SpoilsofWar == 1) printf("Spoils of War are turned on.\n");
					else if (SpoilsofWar == 0) printf("Spoils of War are turned off.\n");
					if (EnableCapLimit == 1) printf("Following Cap Limit rules.  This round's limit is %d.\n",TotalCaptures);
					else if (EnableCapLimit == 0) printf("Unlimited captures allowed.\n");

					GameObject *AnnouncementThing = Commands->Create_Object("Invisible_Object",Commands->Get_Position(obj));
					Commands->Attach_Script(AnnouncementThing,"KAK_Limit_Announcement","");
				}
			}
		}*/
	}
}

void M00_BUILDING_EXPLODE_NO_DAMAGE_DAK::Damaged(GameObject *obj, GameObject *damager, float damage) {
	if (EnableCTF == 1 && AllowKillBuilding == false) {
		if (Commands->Get_Player_Type(damager) == 1 && strstr(Commands->Get_Preset_Name(obj),"GDI")) {
			Commands->Give_Points(damager,damage,false);
			Commands->Give_Money(damager,(damage * (float)-1),false);
		}
		else if (Commands->Get_Player_Type(damager) == 1 && !strstr(Commands->Get_Preset_Name(obj),"GDI")) {
			Commands->Give_Points(damager,(damage * (float)-1),false);
			Commands->Give_Money(damager,(damage * (float)-1),false);
		}
		else if (Commands->Get_Player_Type(damager) == 0 && strstr(Commands->Get_Preset_Name(obj),"Nod")) {
			Commands->Give_Points(damager,damage,false);
			Commands->Give_Money(damager,(damage * (float)-1),false);
		}
		else if (Commands->Get_Player_Type(damager) == 0 && !strstr(Commands->Get_Preset_Name(obj),"Nod")) {
			Commands->Give_Points(damager,(damage * (float)-1),false);
			Commands->Give_Money(damager,(damage * (float)-1),false);
		}


		if (damage >= 500) {
			if (Commands->Get_Player_Type(damager) == 1 && strstr(Commands->Get_Preset_Name(obj),"GDI")) {
				Commands->Give_Points(damager,(float)250,false);
				Commands->Give_Money(damager,(float)-250,false);
			}
			else if (Commands->Get_Player_Type(damager) == 1 && !strstr(Commands->Get_Preset_Name(obj),"GDI")) {
				Commands->Give_Points(damager,(float)-250,false);
				Commands->Give_Money(damager,(float)-250,false);
			}
			else if (Commands->Get_Player_Type(damager) == 0 && strstr(Commands->Get_Preset_Name(obj),"Nod")) {
				Commands->Give_Points(damager,(float)250,false);
				Commands->Give_Money(damager,(float)-250,false);
			}
			else if (Commands->Get_Player_Type(damager) == 0 && !strstr(Commands->Get_Preset_Name(obj),"Nod")) {
				Commands->Give_Points(damager,(float)-250,false);
				Commands->Give_Money(damager,(float)-250,false);
			}
		}

		Commands->Set_Health(obj,Commands->Get_Max_Health(obj));
	}
}

void M00_BUILDING_EXPLODE_NO_DAMAGE_DAK::Destroyed(GameObject *obj) {
	if (EnableCTF == 1) {
		RecognizeMap(Commands->Get_ID(obj),false);

		/*if (strstr(Commands->Get_Preset_Name(obj),"mp_GDI_Barracks")) {
			rename("Data\\bjects.ddb","Data\\objects.ddb");
			GDIFlagHolder = 0;
			NodFlagHolder = 0;
			GDIFlagAttached = 0;
			NodFlagAttached = 0;
			MapID = 0;
			GDICaps = 0;
			NodCaps = 0;
			AllowKillBuilding = false;

			ShowAnnouncement = true;
			EnableCTF = getProfileInt("Settings","EnableCTFMode",1,"CTF_Settings.ini");
			WeaponForHolder= getProfileInt("Settings","AllowWeaponForHolder",0,"CTF_Settings.ini");
			DropWeapons = getProfileInt("Settings","AllowDropWeapons",1,"CTF_Settings.ini");
			SpoilsofWar = getProfileInt("Settings","AllowSpoilsofWar",1,"CTF_Settings.ini");
			EnableCapLimit = getProfileInt("Settings","AllowCapLimit",1,"CTF_Settings.ini");
			RegenForHolder = getProfileInt("Settings","AllowRegenForHolder",0,"CTF_Settings.ini");
			getProfileString("Settings","FlagPresetName","POW_Data_Disc",FlagPreset,100,"CTF_Settings.ini");
			getProfileString("Settings","GDIPedPresetName","CtfPedestalGold",GDIPedPreset,100,"CTF_Settings.ini");
			getProfileString("Settings","NodPedPresetName","CtfPedestalRed",NodPedPreset,100,"CTF_Settings.ini");
			getProfileString("Settings","FlagBackPresetName","CtfFlag",FlagBackPreset,100,"CTF_Settings.ini");
		}*/
	}
}

/* ************************************************************************************************************* */

void M00_Disable_Transition::Created(GameObject *obj) {
	if (EnableCTF == 1) {
		if (strstr(Commands->Get_Preset_Name(obj),"CnC_Nod_Harvester")) { }
		else if (strstr(Commands->Get_Preset_Name(obj),"CnC_GDI_Harvester")) { }
		else Commands->Destroy_Object(obj);
	}
}

void M00_Disable_Transition::Damaged(GameObject *obj, GameObject *damager, float damage) {
	if (EnableCTF == 1) {
		if (strstr(Commands->Get_Preset_Name(obj),"CnC_GDI_Harvester") || strstr(Commands->Get_Preset_Name(obj),"CnC_Nod_Harvester")) {
			Commands->Set_Health(obj,Commands->Get_Max_Health(obj));
			Commands->Set_Shield_Strength(obj,Commands->Get_Max_Shield_Strength(obj));
		}
	}
}

/* ************************************************************************************************************* */

void M00_GrantPowerup_Created::Created(GameObject *obj) {
	if (strstr(Get_Parameter("WeaponDef"),"POW_Pistol_Player")) {
		char PurchaseMsg[512];
		sprintf(PurchaseMsg,"_Purchase %s purchased a %s\n",Get_Player_Name(obj),Translate_Preset(Get_Vehicle_Return(obj)));
		Console_Output(PurchaseMsg);
        int weps = getProfileInt("Settings","AllowDropWeapons",0,"CTF_Settings.ini");
		//if (!Is_Script_Attached(obj,"Weapons") && weps == 1) Commands->Attach_Script(obj,"Weapons","");
		if (stricmp(The_Game()->MapName,"C&C_Field.mix") == 0) {
			Commands->Attach_Script(obj,"Prevent","");
		}
		int RGHP = getProfileInt("JBServ","RGHProtect",0,"CTF_Settings.ini");
		if (RGHP == 1) {
			int R = getProfileInt("JBServ","RGHType",0,"CTF_Settings.ini");
			if (R == 1) {
				Commands->Attach_Script(obj,"RGHProtect","");
			}
			if (R == 2) {
				char RGH[512];
				sprintf(RGH,"RGH%s",Get_Player_Name(obj));
				if (getProfileInt("JBServ",RGH,0,"CTF_Settings.ini") == 1) {
					Commands->Attach_Script(obj,"RGHProtect","");
				}
				sprintf(RGH,"RGH%s",Get_IP_Address(Get_Player_ID(obj)));
				if (getProfileInt("JBServ",RGH,0,"CTF_Settings.ini") == 1) {
					Commands->Attach_Script(obj,"RGHProtect","");
				}
			}
		}
		int JB = getProfileInt("JBServ","AllowUpgrades",0,"CTF_Settings.ini");
		if (JB == 1) { 
			Commands->Attach_Script(obj,"Upgraded","");
		}
		int JB1 = getProfileInt("JBServ","Purchase",0,"CTF_Settings.ini");
		if (JB1 == 1) { 
			int playerid = Get_Player_ID(obj);
			GameObject *obj5820;
			Vector3 position5820;
			position5820.X = -0.000000f;
			position5820.Y = 0.000000f;
			position5820.Z = 10.000000f;
			obj5820 = Find_Closest_Object_To_Pos_By_Preset(position5820,"Invisible_Object");
			Commands->Send_Custom_Event(Get_GameObj(playerid),obj5820,5820,playerid,0);
			Commands->Send_Custom_Event(Get_GameObj(playerid),obj5820,5821,playerid,0);
		}
	}
	if (EnableCTF == 1) {
		Commands->Set_Is_Visible(obj,false);
		Dropper = "the Server";		
		IsDestroyed = false;
		if (GDIFlagHolder == Commands->Get_ID(obj) || NodFlagHolder == Commands->Get_ID(obj)) {
		if (WeaponForHolder == 0) {
			Commands->Give_Powerup(obj,"POW_Uplink",true);
			Commands->Attach_Script(obj,"KAK_Select_Flag","");
		}
		if (RegenForHolder == 1) {
			Commands->Attach_Script(obj,"KAK_FlagHolder_Regen","");
		}
		Commands->Attach_Script(obj,"KAK_FlagHolder_Appear","");
	}
	IsAlive = true;
	Commands->Create_Sound("SFX.Spawn",Commands->Get_Position(obj),0);
	}
	Commands->Give_Powerup(obj,Get_Parameter("WeaponDef"),true);
}

void M00_GrantPowerup_Created::Timer_Expired(GameObject *obj, int number) {
	if (EnableCTF == 1) {
		if (number == 5) {
			//Commands->Give_Money(obj,1,false);
			Commands->Start_Timer(obj,this,1,5);
		}
	}
}

void M00_GrantPowerup_Created::Destroyed(GameObject *obj) {
	if (strstr(Get_Parameter("WeaponDef"),"POW_Pistol_Player")) {
		if (EnableCTF == 1) {
			if (IsDestroyed == false) {
				IsDestroyed = true;
				Vector3 DeathPlace = Commands->Get_Position(obj);

				if (Commands->Get_Player_Type(obj) == 0 && Commands->Get_ID(obj) == GDIFlagHolder) {
					if (Commands->Find_Object(GDIFlagAttached) != 0) Commands->Destroy_Object(Commands->Find_Object(GDIFlagAttached));
					Create_CTF_DropFlag(1,DeathPlace);
					CTF_Message(3,PlayerName(obj),Dropper.c_str());
					GDIFlagLoc = 3;
				}
				else if (Commands->Get_Player_Type(obj) == 1 && Commands->Get_ID(obj) == NodFlagHolder) {
					if (Commands->Find_Object(NodFlagAttached) != 0) Commands->Destroy_Object(Commands->Find_Object(NodFlagAttached));
					Create_CTF_DropFlag(0,DeathPlace);
					CTF_Message(4,PlayerName(obj),Dropper.c_str());
					NodFlagLoc = 3;
				}
			}

			/*if (strstr(Get_Parameter("WeaponDef"),"POW_Pistol_Player")) {
				char IDFile[100];
				sprintf(IDFile,"del PL\\PL%d",Commands->Get_ID(obj));
				system(IDFile);
			}*/
		}
	}
}

void M00_GrantPowerup_Created::Killed(GameObject *obj, GameObject *shooter) {
	if (IsAlive && strstr(Get_Parameter("WeaponDef"),"POW_Pistol_Player")) {
		IsAlive = false;
		if (EnableCTF == 1) {
			if (strstr(Get_Parameter("WeaponDef"),"POW_Pistol_Player")) {
				if (DropWeapons == 1) {
					Vector3 DeathPlace = Commands->Get_Position(obj);
					GameObject *BackpackObject = Commands->Create_Object("Invisible_Object",DeathPlace);
					Commands->Attach_Script(BackpackObject,"KAK_Backpack_Object",Commands->Get_Preset_Name(obj));
				}

				if (Commands->Get_Player_Type(obj) == 0 && Commands->Get_ID(obj) == GDIFlagHolder) {
					if (SpoilsofWar == 1) {
						//Spoils of War for killing holder
						Commands->Give_Points(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderPointsTeam",0.0f,"CTF_Settings.ini"),true);
						Commands->Give_Points(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderPointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
						Commands->Give_Points(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderPointsInd",0.0f,"CTF_Settings.ini"),false);
						Commands->Give_Money(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderPointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
						Commands->Give_Money(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderPointsInd",0.0f,"CTF_Settings.ini") * -1),false);
						Commands->Give_Money(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderCreditsTeam",0.0f,"CTF_Settings.ini"),true);
						Commands->Give_Money(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
						Commands->Give_Money(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderCreditsInd",0.0f,"CTF_Settings.ini"),false);
					}
					if (Commands->Get_ID(shooter) == 0) Dropper = "the Server";
					else Dropper = PlayerName(shooter);
				}
				else if (Commands->Get_Player_Type(obj) == 1 && Commands->Get_ID(obj) == NodFlagHolder) {
					if (SpoilsofWar == 1) {
						Commands->Give_Points(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderPointsTeam",0.0f,"CTF_Settings.ini"),true);
						Commands->Give_Points(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderPointsTeam",0.0f,"CTF_Settings.ini") * -1),false);
						Commands->Give_Points(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderPointsInd",0.0f,"CTF_Settings.ini"),false);
						Commands->Give_Money(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderPointsTeam",0.0f,"CTF_Settings.ini") * -1),true);
						Commands->Give_Money(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderPointsInd",0.0f,"CTF_Settings.ini") * -1),false);
						Commands->Give_Money(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderCreditsTeam",0.0f,"CTF_Settings.ini"),true);
						Commands->Give_Money(shooter,(getProfileFloat("SpoilsOfWar","KillFlagHolderCreditsTeam",0.0f,"CTF_Settings.ini") * -1),false);
						Commands->Give_Money(shooter,getProfileFloat("SpoilsOfWar","KillFlagHolderCreditsInd",0.0f,"CTF_Settings.ini"),false);
					}
					if (Commands->Get_ID(shooter) == 0) Dropper = "the Server";
					else Dropper = PlayerName(shooter);
				}
			}
		}

		std::string SoundName = "Death_01";
		if (IsFemale(obj)) {
			int DS = Commands->Get_Random_Int(1,3);
			if (DS == 1) SoundName = "SFX.Female_Scream_01";
			else if (DS == 2) SoundName = "SFX.Female_Scream_02";
		}
		else {
			int DS = Commands->Get_Random_Int(1,15);
			if (DS == 1) SoundName = "Death_01";
			else if (DS == 2) SoundName = "Death_02";
			else if (DS == 3) SoundName = "Death_03";
			else if (DS == 4) SoundName = "Death_04";
			else if (DS == 5) SoundName = "Death_05";
			else if (DS == 6) SoundName = "Death_06";
			else if (DS == 7) SoundName = "Death_07";
			else if (DS == 8) SoundName = "Death_08";
			else if (DS == 9) SoundName = "Death_09";
			else if (DS == 10) SoundName = "Death_10";
			else if (DS == 11) SoundName = "Death_11";
			else if (DS == 12) SoundName = "Death_12";
			else if (DS == 13) SoundName = "Death_13";
			else if (DS == 14) SoundName = "Death_15";
			else if (DS == 15) SoundName = "Death_16";
		}
		Commands->Create_Sound(SoundName.c_str(),Commands->Get_Position(obj),0);

		if (ShowPlayerKillMessage == 1) {
			if (Commands->Get_ID(shooter) == 0) {
				std::string killer(PlayerName(obj)),
							killerpreset(Translate_Preset(IsPlayerVehicle(obj)));
				char KillMsg[512];
				sprintf(KillMsg,"_Playerkill %s was killed (%s)",killer.c_str(),killerpreset.c_str());
				FDSMessage(KillMsg);
			}
			else if (Commands->Get_ID(obj) == Commands->Get_ID(shooter)) {
				std::string killer(PlayerName(obj)),
							killerpreset(Translate_Preset(IsPlayerVehicle(obj)));
				char KillMsg[512];
				sprintf(KillMsg,"_Playerkill %s killed himself (%s)",killer.c_str(),killerpreset.c_str());
				FDSMessage(KillMsg);
			}
			else {
				std::string killer(PlayerName(shooter)),
							killed(PlayerName(obj)),
							killerpreset(Translate_Preset(IsPlayerVehicle(shooter))),
							killedpreset(Translate_Preset(IsPlayerVehicle(obj)));
				if (strcmp(killer.c_str(),"ERROR_NotPlayer") == 0) {
					char KillMsg[512];
					sprintf(KillMsg,"_Playerkill The %s killed %s (%s)",Translate_Preset(shooter),killed.c_str(),killedpreset.c_str());
					FDSMessage(KillMsg);
				}
				else {
					std::string KillMsg(SvSKillMsg[Commands->Get_Random_Int(0,SvSKillMsgTotal)]);
					int n;
					while((n = KillMsg.find("<killer>")) != std::string::npos) KillMsg.replace(n,8,killer);
					while((n = KillMsg.find("<killed>")) != std::string::npos) KillMsg.replace(n,8,killed);
					while((n = KillMsg.find("<killerpreset>")) != std::string::npos) KillMsg.replace(n,14,killerpreset);
					while((n = KillMsg.find("<killedpreset>")) != std::string::npos) KillMsg.replace(n,14,killedpreset);
					FDSMessage(KillMsg.c_str());
				}
			}
		}
	}
}

void M00_GrantPowerup_Created::Register_Auto_Save_Variables() {
	if (EnableCTF == 1) Auto_Save_Variable(1,1,&IsDestroyed);
}

/* ************************************************************************************************************* */

void KAK_Backpack_Object::Created(GameObject *obj) {
	Commands->Start_Timer(obj,this,5,1);
}

void KAK_Backpack_Object::Timer_Expired(GameObject *obj, int number) {
	if (number == 1) {
		DeathPlace = Commands->Get_Position(obj);
		//Commands->Create_Sound("SFX.Weapon_Spawn",DeathPlace,0); - Old Weapon Drop Sound

		int RandomNum = Commands->Get_Random_Int(1,4);

		if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_MiniGunner_0")) {
			BackpackScript = Commands->Create_Object("POW_AutoRifle_Player",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_Minigunner_0")) {
			BackpackScript = Commands->Create_Object("POW_AutoRifle_Player_Nod",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_MiniGunner_1Off")) {
			if (RandomNum == 1 || RandomNum == 2 || RandomNum == 3) {
				BackpackScript = Commands->Create_Object("POW_Chaingun_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_MiniGunner_1Off")) {
			if (RandomNum == 1 || RandomNum == 2 || RandomNum == 3) {
				BackpackScript = Commands->Create_Object("POW_Chaingun_Player_Nod",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_FlameThrower_1Off")) {
			if (RandomNum == 1 || RandomNum == 2 || RandomNum == 3) {
				BackpackScript = Commands->Create_Object("POW_ChemSprayer_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_FlameThrower_0")) {
			BackpackScript = Commands->Create_Object("POW_Flamethrower_Player",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_Grenadier_0")) {
			BackpackScript = Commands->Create_Object("POW_GrenadeLauncher_Player",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_RocketSoldier_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_LaserChaingun_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_FlameThrower_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_LaserRifle_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_Technician_0")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_RepairGun_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else if (RandomNum == 3) {
				BackpackScript = Commands->Create_Object("CnC_POW_MineRemote_02",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}
		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_Engineer_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_RepairGun_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else if (RandomNum == 3) {
				BackpackScript = Commands->Create_Object("CnC_POW_MineRemote_02",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_Engineer_0")) {
			BackpackScript = Commands->Create_Object("CnC_POW_MineRemote_02",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}
		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_Engineer_0")) {
			BackpackScript = Commands->Create_Object("CnC_POW_MineRemote_02",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Sydney_PowerSuit")) {
			if (RandomNum == 1) {
				BackpackScript = Commands->Create_Object("POW_PersonalIonCannon_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_050",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_RocketSoldier_3Boss")) {
			if (RandomNum == 1) {
				BackpackScript = Commands->Create_Object("POW_Railgun_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_050",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_Minigunner_3Boss")) {
			if (RandomNum == 1) {
				BackpackScript = Commands->Create_Object("POW_RamjetRifle_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_050",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}
		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_MiniGunner_3Boss")) {
			if (RandomNum == 1) {
				BackpackScript = Commands->Create_Object("POW_RamjetRifle_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_050",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_RocketSoldier_1Off")) {
			if (RandomNum == 1 || RandomNum == 2 || RandomNum == 3) {
				BackpackScript = Commands->Create_Object("POW_RocketLauncher_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}
		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_RocketSoldier_1Off")) {
			if (RandomNum == 1 || RandomNum == 2 || RandomNum == 3) {
				BackpackScript = Commands->Create_Object("POW_RocketLauncher_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_RocketSoldier_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("CnC_POW_RocketLauncher_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_RocketSoldier_0")) {
			BackpackScript = Commands->Create_Object("POW_Shotgun_Player",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}
		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_RocketSoldier_0")) {
			BackpackScript = Commands->Create_Object("POW_Shotgun_Player",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_MiniGunner_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_SniperRifle_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_Minigunner_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_SniperRifle_Player_Nod",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Sydney")) {
			if (RandomNum == 1 || RandomNum == 2 || RandomNum == 3) {
				BackpackScript = Commands->Create_Object("POW_TiberiumAutoRifle_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_Minigunner_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_SniperRifle_Player_Nod",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_GDI_Grenadier_2SF")) {
			if (RandomNum == 1 || RandomNum == 2) {
				BackpackScript = Commands->Create_Object("POW_TiberiumFlechetteGun_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Ignatio_Mobius")) {
			if (RandomNum == 1) {
				BackpackScript = Commands->Create_Object("POW_VoltAutoRifle_Player",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_050",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else if (strstr(Get_Parameter("CharacterType"),"CnC_Nod_FlameThrower_3Boss")) {
			if (RandomNum == 1) {
				BackpackScript = Commands->Create_Object("CnC_POW_VoltAutoRifle_Player_Nod",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
			else {
				BackpackScript = Commands->Create_Object("POW_Armor_050",DeathPlace);
				Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
			}
		}

		else {
			BackpackScript = Commands->Create_Object("POW_Armor_025",DeathPlace);
			Commands->Attach_Script(BackpackScript,"M00_Powerup_Destroy","");
		}

		Commands->Send_Custom_Event(obj,BackpackScript,6000,Commands->Get_ID(obj),0);
	}
}

/* ************************************************************************************************************* */

void KAK_Flag_Icon::Created(GameObject *obj) {
	if (FirstGame == false) {
		char map[512], IDs[200];
		sprintf(IDs,"%d",MapID);
		getProfileString("Map",IDs,"NULL",map,256,"Data\\CTF.dat");
		int Team = Get_Int_Parameter("Team");
		Vector3 PedPosition;
		if (Team == 0) {
			PedPosition.X = getProfileFloat(map,"NodFlag_X",500.000,"Data\\CTF.dat");
			PedPosition.Y = getProfileFloat(map,"NodFlag_Y",500.000,"Data\\CTF.dat");
			PedPosition.Z = getProfileFloat(map,"NodFlag_Z",-500.000,"Data\\CTF.dat");
		}
		else if (Team == 1) {
			PedPosition.X = getProfileFloat(map,"GDIFlag_X",500.000,"Data\\CTF.dat");
			PedPosition.Y = getProfileFloat(map,"GDIFlag_Y",500.000,"Data\\CTF.dat");
			PedPosition.Z = getProfileFloat(map,"GDIFlag_Z",-500.000,"Data\\CTF.dat");
		}
		IconPos = PedPosition;
		IconPos.X -= getProfileFloat("Flag","Flag_X",500.000,"Data\\CTF.dat");
		IconPos.Y -= getProfileFloat("Flag","Flag_Y",500.000,"Data\\CTF.dat");
		IconPos.Z -= getProfileFloat("Flag","Flag_Z",-500.000,"Data\\CTF.dat");
		IconPos.Z += 1.25f;
		Icon = Commands->Create_Object("Invisible_Object",IconPos);
		Commands->Start_Timer(obj,this,0.1f,Get_Int_Parameter("Team"));
	}
}

void KAK_Flag_Icon::Timer_Expired(GameObject *obj, int number) {
	if (number == 0) {
		if (NodFlagLoc == 1 && LastLoc != 1) {
			if (Icon != 0) Commands->Destroy_Object(Icon);
			Icon = Commands->Create_Object("Artillery_Loc",IconPos);
			Commands->Set_Animation(Icon,"O_EM_CHEVRON.O_EM_CHEVRON",true,0,0.0f,-1.0f,false);
			LastLoc = 1;
		}
		else if (NodFlagLoc == 2 && LastLoc != 2) {
			if (Icon != 0) Commands->Destroy_Object(Icon);
			Icon = Commands->Create_Object("Stealth_Tank_Loc",IconPos);
			Commands->Set_Animation(Icon,"O_EM_REDARR.O_EM_REDARR",true,0,0.0f,-1.0f,false);
			LastLoc = 2;
		}
		else if (NodFlagLoc == 3 && LastLoc != 3) {
			if (Icon != 0) Commands->Destroy_Object(Icon);
			Icon = Commands->Create_Object("M06_Servant_Loc",IconPos);
			Commands->Set_Animation(Icon,"O_EM_GRNARR.O_EM_GRNARR",true,0,0.0f,-1.0f,false);
			LastLoc = 3;
		}
		Commands->Start_Timer(obj,this,0.1f,0);
	}
	else if (number == 1) {
		if (GDIFlagLoc == 1 && LastLoc != 1) {
			Commands->Destroy_Object(Icon);
			Icon = Commands->Create_Object("Artillery_Loc",IconPos);
			Commands->Set_Animation(Icon,"O_EM_CHEVRON.O_EM_CHEVRON",true,0,0.0f,-1.0f,false);
			LastLoc = 1;
		}
		else if (GDIFlagLoc == 2 && LastLoc != 2) {
			Commands->Destroy_Object(Icon);
			Icon = Commands->Create_Object("Stealth_Tank_Loc",IconPos);
			Commands->Set_Animation(Icon,"O_EM_REDARR.O_EM_REDARR",true,0,0.0f,-1.0f,false);
			LastLoc = 2;
		}
		else if (GDIFlagLoc == 3 && LastLoc != 3) {
			Commands->Destroy_Object(Icon);
			Icon = Commands->Create_Object("M06_Servant_Loc",IconPos);
			Commands->Set_Animation(Icon,"O_EM_GRNARR.O_EM_GRNARR",true,0,0.0f,-1.0f,false);
			LastLoc = 3;
		}
		Commands->Start_Timer(obj,this,0.1f,1);
	}
}

/* ************************************************************************************************************* */

void KAK_Capture_Animation::Created(GameObject *obj) {
	Commands->Start_Timer(obj,this,1.0f,1);
}

void KAK_Capture_Animation::Timer_Expired(GameObject *obj, int number) {
	if (number == 1) {
		int Animation2Play = Commands->Get_Random_Int(1,13);
		if (Animation2Play == 1) Commands->Set_Animation(obj,"H_A_SIDEKICK",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 2) Commands->Set_Animation(obj,"H_A_PUNCHCOMBO",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 3) Commands->Set_Animation(obj,"H_A_A0A0_L02",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 4) Commands->Set_Animation(obj,"H_A_J22C",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 5) Commands->Set_Animation(obj,"H_A_A0A0_L23",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 6) Commands->Set_Animation(obj,"H_A_A0A0_L58",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 7) Commands->Set_Animation(obj,"H_A_B0A0_L05",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 8) Commands->Set_Animation(obj,"H_A_A0A0_L12",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 9) Commands->Set_Animation(obj,"H_A_J14C",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 10) Commands->Set_Animation(obj,"H_A_X33C",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 11) Commands->Set_Animation(obj,"H_A_A0A0_L22",false,0,0.0f,-1.0f,false);
		else if (Animation2Play == 12) Commands->Set_Animation(obj,"H_A_A0A0_L24",false,0,0.0f,-1.0f,false);
	}
}

void KAK_Capture_Animation::Animation_Complete(GameObject *obj, const char *anim) {
	Destroy_Script();
}

/* ************************************************************************************************************* */

void KAK_Random_Weather::Created(GameObject *obj) {
	getProfileString("WeatherType",CurrMap,"Rain",WeatherType,256,"CTF_Settings.ini");

	Range = Commands->Get_Random(0,1);
	CurrentClouds = Commands->Get_Random(0,1);
	Commands->Set_Clouds(CurrentClouds,CurrentClouds,true);
	Commands->Start_Timer(obj,this,0.5f,0);
	CurrentWind = 0.0f;
}

void KAK_Random_Weather::Timer_Expired(GameObject *obj, int number) {
	if (number == 0) {
		if (CurrentClouds < Range) {
			if ((CurrentClouds + 0.001f) >= Range) CurrentClouds = Range;
			else CurrentClouds += 0.001f;
		}
		else if (CurrentClouds > Range) {
			if ((CurrentClouds - 0.001f) <= Range) CurrentClouds = Range;
			else CurrentClouds -= 0.001f;
		}

		//Start Making Weather
		Commands->Set_Clouds(CurrentClouds,CurrentClouds,true);

		CurrentWind = 10 * CurrentClouds;
		Commands->Set_Wind(0.0f,CurrentWind,1.0f,true);

		FogEnd = ((-350 * CurrentClouds) + 500);
		Commands->Set_Fog_Enable(true);
		Commands->Set_Fog_Range(5.0f,FogEnd,1.0f);

		if (strstr(MapName,"C&C_Under.mix") || strstr(MapName,"C&C_Conquest_Winter.mix")) {
			if (CurrentClouds >= 0.625f) {
				CurrentSnow = (((8 * CurrentClouds) - 5) / 3);
				Commands->Set_Snow(CurrentSnow,CurrentSnow,true);
			}
			else Commands->Set_Snow(0.0f,0.0f,true);
		}
		else if (strstr(MapName,"C&C_Volcano.mix")) {
			if (CurrentClouds >= 0.625f) {
				CurrentAsh = (((8 * CurrentClouds) - 5) / 3);
				Commands->Set_Ash(CurrentAsh,CurrentAsh,true);
			}
			else Commands->Set_Ash(0.0f,0.0f,true);
		}
		else {
			if (CurrentClouds >= 0.625f) {
				CurrentRain = ((1.875f * CurrentClouds) - 0.125f);
				Commands->Set_Rain(CurrentRain,CurrentRain,true);
			}
			else Commands->Set_Rain(0.0f,0.0f,true);

			if (CurrentClouds >= 0.875f) {
				CurrentLightning = (8 * (CurrentClouds + 0.125f)) - 8;
				Commands->Set_Lightning(CurrentLightning,0.0f,1.0f,0.0f,1.0f,true);
			}
			else Commands->Set_Lightning(0.0f,0.0f,1.0f,0.0f,1.0f,true);
		}
		//Stop Making Weather

		if (CurrentClouds == Range) {
			Range = Commands->Get_Random(0,1);
			Commands->Start_Timer(obj,this,0.5f,0);
		}
		else Commands->Start_Timer(obj,this,0.5f,0);
	}
}

void M00_PCT_Pokable_DAK::Damaged(GameObject *obj, GameObject *damager, float damage) {
	Commands->Set_Health(obj,Commands->Get_Max_Health(obj));
	Commands->Set_Shield_Strength(obj,Commands->Get_Max_Shield_Strength(obj));
}

void M00_PCT_Pokable_DAK::Killed(GameObject *obj, GameObject *shooter) {
	//Just in case..
	Commands->Create_Object(Commands->Get_Preset_Name(obj),Commands->Get_Position(obj));
}

void M00_PCT_Pokable_DAK::Poked(GameObject *obj, GameObject *poker) {
	if (strstr(Commands->Get_Preset_Name(obj),"gdi")) Commands->Display_GDI_Player_Terminal();
	else if (strstr(Commands->Get_Preset_Name(obj),"nod")) Commands->Display_NOD_Player_Terminal();
}

/* ************************************************************************************************************* */
ScriptRegistrant<M00_BUILDING_EXPLODE_NO_DAMAGE_DAK> M00_BUILDING_EXPLODE_NO_DAMAGE_DAK_Registrant("M00_BUILDING_EXPLODE_NO_DAMAGE_DAK","");
ScriptRegistrant<M00_Disable_Transition> M00_Disable_Transition_Registrant("M00_Disable_Transition","");
ScriptRegistrant<M00_GrantPowerup_Created> M00_GrantPowerup_Created_Registrant("M00_GrantPowerup_Created","WeaponDef:string");
ScriptRegistrant<KAK_Backpack_Object> KAK_Backpack_Object_Registrant("KAK_Backpack_Object","CharacterType:string");
ScriptRegistrant<KAK_CTF_Flag> KAK_CTF_Flag_Registrant("KAK_CTF_Flag","Team:int,FlagID:int");
ScriptRegistrant<KAK_CTF_DropFlag> KAK_CTF_DropFlag_Registrant("KAK_CTF_DropFlag","Team:int,FlagID:int");
ScriptRegistrant<KAK_Select_Flag> KAK_Select_Flag_Registrant("KAK_Select_Flag","");
ScriptRegistrant<KAK_Limit_Announcement> KAK_Limit_Announcement_Registrant("KAK_Limit_Announcement","");
ScriptRegistrant<KAK_FlagHolder_Regen> KAK_FlagHolder_Regen_Registrant("KAK_FlagHolder_Regen","");
ScriptRegistrant<KAK_FlagHolder_Appear> KAK_FlagHolder_Appear_Registrant("KAK_FlagHolder_Appear","");
ScriptRegistrant<KAK_Capture_Animation> KAK_Capture_Animation_Registrant("KAK_Capture_Animation","");
ScriptRegistrant<KAK_Flag_Icon> KAK_Flag_Icon_Registrant("KAK_Flag_Icon","Team:int");
ScriptRegistrant<KAK_Random_Weather> KAK_Random_Weather_Registrant("KAK_Random_Weather","BarrID:int");
ScriptRegistrant<M00_PCT_Pokable_DAK> M00_PCT_Pokable_DAK_Registrant("M00_PCT_Pokable_DAK","");