#include "scripts.h"
#include "profile.h"
#include "date.h"
#include "R3v.h"
#include "engine.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <sstream>
#include <stdio.h>
int stringToInt( const std::string &str )
{
std::stringstream stream;
stream << str;
int value;
stream >> value;
return value;
}

float stringToFloat( const std::string &str )
{
 std::stringstream stream;
 stream << str;
 float value;
 stream >> value;
 return value;
 }
 
void DataFromDDE(char *In)
{
	char seps[] = " ";
    char *token;
	token = strtok( In, seps );
	if (strcmp(token,"killplayer") == 0)
	{
		int playerid = stringToInt(strtok(NULL," "));
		kill_player(playerid);
	}
	if (strcmp(token,"showpos") == 0)
	{
		char Message[512];
		int playerid = stringToInt(strtok(NULL," "));
		Vector3 p = Commands->Get_Position(Get_GameObj(playerid));
		sprintf(Message, "Position of player %d: %fX - %fY - %fZ", playerid,p.X,p.Y,p.Z);
		Console_Output(Message);
	}
	if (strcmp(token,"bring") == 0)
	{
		int playerid = stringToInt(strtok(NULL," "));
		int playerid2 = stringToInt(strtok(NULL," "));
		char Message[512];
		sprintf(Message, "eject %d", playerid2);
		Console_Input(Message);
		Vector3 position = Commands->Get_Position(Get_GameObj(playerid));
		position.X = position.X + 3;
		position.Z = position.Z + 1;
		Commands->Set_Position(Get_GameObj(playerid2), position);
	}
    if (strcmp(token,"setpos") == 0)
	{
		int playerid = stringToInt(strtok(NULL," "));
	    char Message[512];
		sprintf(Message, "eject %d", playerid);
		Console_Input(Message);
		Vector3 position;
		position.X = stringToFloat(strtok(NULL," "));
		position.Y = stringToFloat(strtok(NULL," "));
		position.Z = stringToFloat(strtok(NULL," "));
		Commands->Set_Position(Get_GameObj(playerid), position);
	}
	if (strcmp(token,"goto") == 0)
	{
		int playerid = stringToInt(strtok(NULL," "));
		int playerid2 = stringToInt(strtok(NULL," "));
		char Message[512];
		sprintf(Message, "eject %d", playerid);
		Console_Input(Message);
		Vector3 pos = Commands->Get_Position(Get_GameObj(playerid2));
		pos.X = pos.X + 3;
		pos.Z = pos.Z + 1;
		Commands->Set_Position(Get_GameObj(playerid), pos);
	}
	if (strcmp(token,"givemoney") == 0)
	{
		int playerid = stringToInt(strtok(NULL," "));
		float amount = stringToFloat(strtok(NULL," "));
	    Commands->Give_Money(Get_GameObj(playerid),amount,false);
	}
}

void kill_player(int obj)
{ 
	char Message[512];
	sprintf(Message, "eject %d", obj);
	Console_Input(Message);
    Commands->Apply_Damage(Get_GameObj(obj),99999,"Death",0);
}
void RGHProtect::Destroyed(GameObject *obj) 
{
	int check = Commands->Get_ID(This);
	if (Commands->Find_Object(check)) { Commands->Destroy_Object(This); }
}
void RGHProtect::Timer_Expired(GameObject *obj, int number)
{
	if (number == 2) {
		Destroy_All_Objects_By_Preset("Nod_Engineer_0");
		Destroy_All_Objects_By_Preset("GDI_Engineer_0");
		Commands->Start_Timer(obj,this,10,2);
	}
	if (number == 1) {
		Vector3 p = Commands->Get_Position(obj);
		p.Z = p.Z + 3;
		if (Get_Vehicle(obj) != 0) { p.Z = p.Z + 2; }
		float f = Commands->Get_Facing(obj);
		if (f <= 45 && f >= -45) { p.X = p.X + 2; }
		if (f > 45 && f <= 135) { p.Y = p.Y + 2; }
		if (f > 135 && f <= 180 || f > -180 && f <= -135) { p.X = p.X - 2; }
		if (f > -135 && f < -45) { p.Y = p.Y - 2; }
		GameObject *This2 = Find_Closest_Object_To_Pos(p);
		if (strcmp(Commands->Get_Preset_Name(This2),"GDI_Engineer_0") != 0 && strcmp(Commands->Get_Preset_Name(This2),"Nod_Engineer_0") != 0)
		{
			int team = Commands->Get_Player_Type(obj);
			if (team == 0) { This = Commands->Create_Object("GDI_Engineer_0", p); }
			else if (team == 1) { This = Commands->Create_Object("Nod_Engineer_0", p); }
			Commands->Clear_Weapons(This);
			Commands->Set_Is_Visible(This,false);
			Set_Max_Health(This,99999);
			Set_Max_Shield_Strength(This,99999);
			Commands->Set_Shield_Type(This,"Blamo");
			Commands->Set_Health(This,99999);
			Commands->Set_Shield_Strength(This,99999);
			Commands->Disable_All_Collisions(This);
			Commands->Disable_Physical_Collisions(This);
			Toggle_Fly_Mode(This);
			Commands->Set_Model(This,"NULL");
		}
		else {
			if (!Get_Fly_Mode(This)) { Toggle_Fly_Mode(This); }
			Commands->Set_Model(This,"NULL");
			Commands->Disable_All_Collisions(This);
			Commands->Disable_Physical_Collisions(This);
			Commands->Set_Position(This, p);
		}
		Commands->Start_Timer(obj,this,.1f,1);	
	}
	Remove_Duplicate_Script(obj,"RGH_Protect");
}
void RGHProtect::Created(GameObject *obj)
{
	Vector3 p = Commands->Get_Position(obj);
	p.Z = p.Z + 3;
	//This = Find_Closest_Object_To_Pos(p);
	//if (strcmp(Commands->Get_Preset_Name(This),"Nod_Engineer_0") == 0) { Commands->Destroy_Object(This); }
	//if (strcmp(Commands->Get_Preset_Name(This),"GDI_Engineer_0") == 0) { Commands->Destroy_Object(This); }
	//This = Find_Closest_Object_To_Pos(p);
	//if (strcmp(Commands->Get_Preset_Name(This),"Nod_Engineer_0") == 0) { Commands->Destroy_Object(This); }
	//if (strcmp(Commands->Get_Preset_Name(This),"GDI_Engineer_0") == 0) { Commands->Destroy_Object(This); }
	int team = Commands->Get_Player_Type(obj);
	if (team == 0) { This = Commands->Create_Object("GDI_Engineer_0", p); }
	else if (team == 1) { This = Commands->Create_Object("Nod_Engineer_0", p); }
	Commands->Clear_Weapons(This);
   	Commands->Set_Is_Visible(This,false);
	Set_Max_Health(This,99999);
	Set_Max_Shield_Strength(This,99999);
	Commands->Set_Shield_Type(This,"Blamo");
	Commands->Set_Health(This,99999);
	Commands->Set_Shield_Strength(This,99999);
	Commands->Disable_All_Collisions(This);
	Commands->Disable_Physical_Collisions(This);
	Toggle_Fly_Mode(This);
	Commands->Set_Model(This,"NULL");
	Commands->Start_Timer(obj,this,.1f,1);
	Commands->Start_Timer(obj,this,10,2);
	Remove_Duplicate_Script(obj,"RGH_Protect");
}
ScriptRegistrant<RGHProtect> RGHProtect_Registrant("RGHProtect","");