/*****************************************************************************
 * $Id: mod_pony_buildings.cc,v 1.1 2006/11/24 21:56:58 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Buildings and other man-made edifices.
 *//*
 * LEGAL:	COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#define MOD_PONY_BUILDINGS_CC 1
#include "mod_pony_common.hh"
#include "mod_pony_buildings.hh"
#include "mod_pony_terrain.hh"

SafePtr<Runway> gRunway = NULL;  // can be assigned but not deleted, cleared by Runway::~Runway()

////////////////////////////////////////////////////////////////////////////////
//////////////////////////////    Runway     ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

fp Runway::msDefaultWidth  = WORLD(75.0);
fp Runway::msDefaultLength = WORLD(7000.0);
Poly16tex Runway::msPoly16tex[] =
{
	// quad
	{ "road1.tga", { { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }, { POLY16TEX_END_COORD, POLY16TEX_END_COORD } } },
};
Poly16 Runway::msPoly16[] =
{
	POLY16_NO_CULL|POLY16_TEXTURE,  POLY16_RGBA32(155,155,155,220),  0,/*texidx*/
	0, 1, 2, 3|POLY16_END,
    POLY16_LIST_END
};

/*****************************************************************************
 * Constructor.
 *****************************************************************************/
Runway::Runway( QuadrantPtrConst q, const Position& pos )
	: Parent(q,pos)
{
    Init( q, pos, msDefaultWidth, msDefaultLength );
	DEBUG_CODE( CheckObjectCtor(); )
}
Runway::Runway( QuadrantPtrConst q, const Position& pos, fp width, fp length )
	: Parent(q,pos)
{
    Init( q, pos, width, length );
	DEBUG_CODE( CheckObjectCtor(); )
}
void
Runway::Init( QuadrantPtrConst q, const Position& pos, fp width, fp length )
{
    mWidth  = width;
    mLength = length;

    // Plenty of empty space width-wise by runway.
    mRectEnlarged.Set( pos.x - width  * 4,
                       pos.x + width  * 4,
                       pos.z - length * 1.25,
                       pos.z + length * 1.25 );

	// Fill vertexs.
	fp y = GROUND_LEVEL - WORLD(12.0);
	fp widthHalf  = width  * 0.5;
	fp lengthHalf = length * 0.5;

	mWorldVertexs[0].x = pos.x - widthHalf;
	mWorldVertexs[0].y = pos.y;
	mWorldVertexs[0].z = pos.z - lengthHalf;

	mWorldVertexs[1].x = pos.x + widthHalf;
	mWorldVertexs[1].y = pos.y;
	mWorldVertexs[1].z = pos.z - lengthHalf;

	mWorldVertexs[2].x = pos.x + widthHalf;
	mWorldVertexs[2].y = pos.y;
	mWorldVertexs[2].z = pos.z + lengthHalf;

	mWorldVertexs[3].x = pos.x - widthHalf;
	mWorldVertexs[3].y = pos.y;
	mWorldVertexs[3].z = pos.z + lengthHalf;

	// Make two rows of trees by the long sides of the runway.
    // Use TreeUnreproducible class as regular Trees will disappear
    // when Quadrant is shrunk as the unreproducible Runway grows trees once.
	for ( fp z2 = mWorldVertexs[0].z; z2 < mWorldVertexs[3].z; z2 += WORLD(1200.0) )
	{
        Position pos1( mWorldVertexs[0].x - WORLD(50.0), y, z2 );
        Position pos2( mWorldVertexs[1].x + WORLD(50.0), y, z2 );
		new TreeUnreproducible( q, pos1, "runway" );
		new TreeUnreproducible( q, pos2, "runway" );	// select the same kind of tree
	}

	// Construct PolygonList.
	PolygonList& polygonList = GetPolygonList();
	PolygonListFromPoly16( polygonList, msPoly16, ELEMS(msPoly16), msPoly16tex, ELEMS(msPoly16tex) );
	polygonList.mDrawParm.EnableCull( false );
	polygonList.Optimize( GetVertexCnt() );

	FinalConstruction();
}
Runway::~Runway()
{
	gRunway = NULL;  // Runway, being non-reproducible should persist, but clear the global
	DEBUG_CODE( CheckObjectDtor(); )
}

/*****************************************************************************
 * Draw.
 *****************************************************************************/
void
Runway::Draw( void )
{
	GetPolygonList().Draw( this );
}
