#include "time.h"

#include "scripts.h"
#include "engine.h"
#include "engine_d3d.h"
#include "engine_3dre.h"
#include "jnz.h"



bool IsInGame = 0;
clock_t UpdateTimer = 0;

Render2DTextClass*	Render2dText;


void AddTextRender(float X, float Y, unsigned int color, const char *Format, ...)
{
	char buffer[256];
	va_list va;
	_crt_va_start(va, Format);
	vsnprintf(buffer, 256, Format, va);
	va_end(va);

	RectClass *r = (RectClass *)((char *)Render2dText+0x5B8);
	r->Right = X;
	r->Left = X;
	r->Top = Y;
	r->Bottom = Y;
	Render2dText->Draw_Text(buffer,color);
}


void jnz_Load()
{
	Render2dText = CreateRender2DTextClass("font9x12.tga");	
	DWORD loc = 0;
	__asm
	{
		mov eax, cScTextObj::Act;
		mov loc, eax;
	}
	DWORD old;
	VirtualProtect((void *)0x7D63FC, 4, PAGE_EXECUTE_READWRITE, &old);
	memcpy((void *)0x7D63FC, (void *)&loc, 4);
	VirtualProtect((void *)0x7D63FC, 4, old, &old);
}

void jnz_Unload()
{
}

void jnz_GameStart()
{
	IsInGame = 1;
}

void jnz_GameEnd()
{
	IsInGame = 0;
}






void jnz_Render()
{
	Render2dText->Reset();	

	Vector2 pos((float)100, (float)100);
	static RECT textbox; SetRect(&textbox, 0, 60, 640, 480);
	static int Timed = 0;
	static int Remote = 0;
	static int Proxy = 0; 
	
	if(clock() - UpdateTimer > 100)
	{
		UpdateTimer = clock();
		int t_timed = 0, t_remote = 0, t_proxy = 0;
		for(GenericSLNode *x = BaseGameObjList->HeadNode; x; x = x->NodeNext)
		{
			GameObject *obj = (GameObject *)x->NodeData;
			if(!obj)
			{
				continue;
			}
			if(Is_C4(obj) && Get_Object_Type(obj) == Get_Object_Type(Commands->Get_The_Star()))
			{
				int Mode = Get_C4_Mode(obj);
				if(Mode == 1)
				{
					t_remote++;
				}
				else if(Mode == 2)
				{
					t_timed++;
				}
				else if(Mode == 3)
				{
					 t_proxy++;
				}
			}
		}
		Remote = t_remote;
		Timed = t_timed;
		Proxy = t_proxy;
	}
			
	const char *team = "Neutral";
	unsigned int c4_colour = 0xFFFFFFFF;
	if(Get_Object_Type(Commands->Get_The_Star()) == 0)
	{
		c4_colour = 0xFFFF0000;
		team = "Nod";
	}
	else if(Get_Object_Type(Commands->Get_The_Star()) == 1)
	{
		c4_colour = 0xFFFFFF00;
		team = "GDI";
	}
	AddTextRender(20, 160, c4_colour, "C4 for team %s\nTimed: %d\nRemote: %d\nProxy: %d", team, Timed, Remote, Proxy);
	

	AddTextRender(800, 160, 0xFFFFFFFF, "Beacon Countdown");

	int bcount = 0;
	for(GenericSLNode *x = BaseGameObjList->HeadNode; x; x = x->NodeNext)
	{
		GameObject *o = (GameObject *)x->NodeData;
		if(bcount <= 3)
		{
			if(Is_Beacon(o))
			{
				PhysicalGameObj *o2 = ((ScriptableGameObj *)o)->As_PhysicalGameObj();
				if (!o2)
				{
					continue;
				}
				BeaconGameObj *o3 = o2->As_BeaconGameObj();
				if (!o3)
				{
					continue;
				}
				bcount++;
				unsigned int Colour = 0xFFFFFFFF;
				int team = Get_Object_Type(o3->Get_Beacon_Owner());
				if(team == 1)
				{
					Colour = 0xFFFFFF00;
				}	
				else if(team == 0)
				{
					Colour = 0xFFFF0000;
				}
				if(o3->DetonateTime > 0)
				{
					const char *pName = Get_Player_Name(o3->Get_Beacon_Owner());
					AddTextRender(800, (float)(160 + (bcount * 20)), Colour, "%s %.2f", pName, o3->DetonateTime);
					delete []pName;
				}
			}
		}
	}

	GameObject *Target = *BracketObj;
	const char *Driver = 0;
	if(Target)
	{
		if(Is_Vehicle(Target))
		{
			GameObject *driver = Get_Vehicle_Driver(Target);
			if(driver)
			{
				Driver = Get_Player_Name(driver);
			}
		}
		unsigned int Colour = 0xFFFFFFFF;
		int team = Get_Object_Type(Target);
		if(team == 1)
		{
			Colour = 0xFFFFFF00;
		}	
		else if(team == 0)
		{
			Colour = 0xFFFF0000;
		}
		AddTextRender(20, 290, Colour, "Target");
		AddTextRender(20, 310, Colour, "%s\n%s\nHealth: %.0f/%.0f\nShield: %.0f/%.0f", Get_Translated_Preset_Name(Target), Driver ? Driver : "No driver", Commands->Get_Health(Target), Commands->Get_Max_Health(Target), Commands->Get_Shield_Strength(Target), Commands->Get_Max_Shield_Strength(Target));
		delete []Driver;
	}
	else
	{
		AddTextRender(20, 290, 0xFFFFFFFF, "Target");
		AddTextRender(20, 310, D3DCOLOR_ARGB(255, 255, 255, 255), "None");
	}


	int nod = 0,gdi = 0;
	for(GenericSLNode *x = BuildingGameObjList->HeadNode; x; x = x->NodeNext)
	{
		GameObject *b = As_BuildingGameObj((GameObject *)x->NodeData);
		if(!b) continue;

		int bteam = Get_Object_Type(b);
		char bname[32];
		int btype = Building_Type(b);

		if(btype == POWER_PLANT)
		{
			strcpy(bname, "pp");
		}
		else if(btype == SOLDIER_FACTORY)
		{
			if(bteam == 0)
			{	
				strcpy(bname, "hon");
			}
			else if(bteam == 1)
			{
				strcpy(bname, "bar");
			}
		}
		else if(btype == VEHICLE_FACTORY)
		{
			if(bteam == 0)
			{	
				strcpy(bname, "air");
			}
			else if(bteam == 1)
			{
				strcpy(bname, "war");
			}
		}
		else if(btype == REFINERY)
		{
			strcpy(bname, "ref");
		}
		else if(btype == COM_CENTER)
		{
			strcpy(bname, "com");
		}
		else if(btype == REPAIR_BAY)
		{
			strcpy(bname, "rep");
		}
		else if(btype == SHRINE)
		{
			strcpy(bname, "shr");
		}
		else if(btype == HELIPAD)
		{
			strcpy(bname, "hel");
		}
		else if(btype == CONYARD)
		{
			strcpy(bname, "con");
		}
		else if(btype == BASE_DEFENSE)
		{
			if(bteam == 0)
			{	
				strcpy(bname, "obi");
			}
			else if(bteam == 1)
			{
				strcpy(bname, "agt");
			}
		}	

		if(bteam == 0)
		{
			AddTextRender(20, (float)((20*(nod++))+400), D3DCOLOR_ARGB(255, 255, 0, 0), "%s: %.0f%%", bname, ((Commands->Get_Health(b) + Commands->Get_Shield_Strength(b)) / (Commands->Get_Max_Health(b) + Commands->Get_Max_Shield_Strength(b))) * 100);
		}
		else if(bteam == 1)
		{
			AddTextRender(900, (float)((20*(gdi++))+400), D3DCOLOR_ARGB(255, 255, 255, 0), "%s: %.0f%%", bname, ((Commands->Get_Health(b) + Commands->Get_Shield_Strength(b)) / (Commands->Get_Max_Health(b) + Commands->Get_Max_Shield_Strength(b))) * 100);
		}
	}
	Render2dText->Render();
}

void cScTextObj::Act()
{

	DWORD loc = 0x4B9F20;
	__asm
	{
		mov ecx, this;
		call loc;
	}
}