//Copyright (C) 2007 SiegeLord
//See ReadMe.txt and License.txt for copying information

#include "solararray.h"
#include "quadtree.h"
#include "ship.h"
#include "world.h"
#include "thruster.h"
#include "flasher.h"

extern int g_nPlayerCash[2];
extern SQuadNode* g_qnRootNode;
extern SObjectEx* g_obRootObject;

vector<SSolarArray*> g_vecSolarArrays;

struct SSolarProjectile : SObjectEx
{
	SSolarProjectile()
	{
		type = solar_proj_type;
		dead = false;
		thruster = 0;
	}

	vector2d vel;
	zColor color;
	SThrusterParticleHolder* thruster;

	virtual int Handle()
	{
		vector2d pos = position;//GetGlobalPosition();
		if(pos.GetLengthSQ() < g_strWorld.planet_size * g_strWorld.planet_size)
		{
			dead = true;
			thruster->target_object = 0;
			return 0;
		}
		pos += vel * g_fDelta;
		SetPosition(pos.x, pos.y);
		return 0;
	}
	virtual void Draw()
	{
//		SetModelMatrix(net_transform);
//
//		DrawModeAlpha();
//
//		float old = SetLineThickness(2.0f);
//
//		DrawCircle(0, 0, 3, -10, color, 32);
//
//		SetLineThickness(old);
//
//		DrawModeSolid();
//
//		SetModelMatrix(g_trIdentity);
	}
};

SSolarArray* CreateSolarArray(SObject* parent, const vector2d& polar_pos)
{
	SSolarArray* array = new SSolarArray();
	InitObject(array, parent);

	array->polar_pos = polar_pos;
	array->SetPosition(polar_pos.x * cosf(polar_pos.y), polar_pos.x * sinf(polar_pos.y));
	array->player = -1;
	array->SetRotation(polar_pos.y + AL_PI / 2);
	array->timer = 0;
	
	CreateFlasher(array, 0, 2.0f, 0xffffffff)->SetPosition(50, 1);
	CreateFlasher(array, AL_PI, 2.0f, 0xffffffff)->SetPosition(-50, 1);
	
	CreateFlasher(array, AL_PI / 3, 2.0f, 0xffffffff)->SetPosition(31, 32);
	CreateFlasher(array, 2.0f * AL_PI / 3.0f, 2.0f, 0xffffffff)->SetPosition(-31, 32);
	CreateFlasher(array, 4.0f * AL_PI / 3.0f, 2.0f, 0xffffffff)->SetPosition(-31, -32);
	CreateFlasher(array, 5.0f * AL_PI / 3.0f, 2.0f, 0xffffffff)->SetPosition(31, -32);

	array->image = FindBitmap("solararray.tga");

	g_vecSolarArrays.push_back(array);

	return array;
}

int SSolarArray::Handle()
{
	list<SCollidableObject*> selected_list;

	vector2d cur_pos = GetGlobalPosition();

	if(QuadTreeCircleTestMulti(g_qnRootNode, cur_pos, 128, &selected_list))
	{
		int player_id = -1;
		bool first = true;
		bool only_one_type = true;//we only convert if there is only one type of ship in the area
		for(list<SCollidableObject*>::iterator it = selected_list.begin(); it != selected_list.end(); it++)
		{
			SShip* ship = (SShip*)(*it);
			if(first)
			{
				player_id = ship->player;
				first = false;
			}
			else
			{
				if(player_id != ship->player)
				{
					only_one_type = false;
					break;
				}
			}
		}
		if(!only_one_type)
			return 0;
		else
		{
			player = player_id;
		}
	}

	if(timer > 4.0f)
	{
		timer = 0.0f;
		if(player != -1)
		{
			g_nPlayerCash[player] += 40;
		}

		zColor color;

		if(player == 0)
			color = 0xffffff00;
		else if(player == 1)
			color = 0xff0f0fff;
		else
			color = 0xffffffff;

		SSolarProjectile* proj = new SSolarProjectile();
		InitObject(proj, g_obRootObject);

		vector2d pos = GetGlobalPosition();

		proj->color = color;
		proj->SetPosition(pos.x, pos.y);
		pos.Normalize();
		pos *= -200;
		proj->vel = pos;

		proj->thruster = CreateThrusterParticleHolder(g_obRootObject, proj, FindBitmap("part.tga"), color);

		proj->thruster->throttle = 1;
		proj->thruster->random_vel_y = 0;
		proj->thruster->SetRotation(AL_PI);
	}
	timer += g_fDelta;
	return 0;
}

void SSolarArray::Draw()
{
	SetModelMatrix(net_transform);

	DrawModeMask();
	DrawBitmap(image, -image->w / 2, -image->h / 2, -8);

	DrawModeAlpha();

	zColor color;

	if(player == 0)
		color = 0xffffff00;
	else if(player == 1)
		color = 0xff0f0fff;
	else
		color = 0xffffffff;

	float old = SetLineThickness(2);

	DrawCircle(0, -80, 2, -10, color, 16);

	SetLineThickness(old);

	DrawModeSolid();

	SetModelMatrix(g_trIdentity);
}

void ResetSolarArrays()
{
	g_vecSolarArrays.clear();
}
