#ifndef FOREST_H
#define FOREST_H

#include "llist.h"
#include "entity.h"
#include "terrain.h"
#include "mesh.h"
#include "messages.h"
#include <d3d9.h>

class Tree;

//There are 2 classes necessary to define a forest; there is the
// overall forest object, and the tree object.  The forest is the overall
// structure that manages several subtrees.
class Forest: public AbstractEntity
{
public:
	//The parameters for the constructor tell the location of the central (and first) tree
	 Forest(LPDIRECT3DDEVICE9 NewCreationDevice = NULL, float XPos = 0, float YPos = 0);
	~Forest();
	
	unsigned int GetAction(float TimeStep, Msg** SentMsgs);
	unsigned int SendMsg(Msg* RecievedMsg, Msg** SentMsgs);

	void SetTerrainObject(Terrain* NewTerrainObject);
	void SetCreationDevice(LPDIRECT3DDEVICE9 NewCreationDevice);

	//This function causes all of the trees to grow, and tries to add 1 new tree near
	// the existing trees, if possible
	void Grow();

	void KillTrees();

	int NumTrees;

	//returns the number of trees; the same value that is put in treecount
	//NOTE: the CALLER is responsible for freeing the CloseTrees array (just the array; not the members)
	// If you can think of a better way to do this, let me know.
	int GetTreesWithinDistance(float XPos, float YPos, float Distance, int& TreeCount, Tree**& CloseTrees);
	int GetTrees(Tree**& AllTrees);
private:
	//This holds a newly created tree.  If calling the grow function creates a new
	// tree, then it is put here and sent as a NewEntity next time getaction is called.
	int NumNewTrees;
	Tree* NewTrees[MAXMESSAGES];  //we should probably make this variable, but I don't
						//want to at the moment.  It's faster this way, anyway

	Tree** ForestTrees;
	//int NumTrees;
	int MaxNumTrees;

	Terrain* TerrainObject;
	LPDIRECT3DDEVICE9 CreationDevice;
};

//At the moment, tree doesn't subclass entity, b/c it is basically
// just a structure used by the forest
class Tree : public Entity
{
public:
	Tree();
	~Tree();

	void GetMeshes(Mesh** MeshArray);
	int GetMeshCount();

	unsigned int GetAction(float TimeStep, Msg** SentMsgs);
	unsigned int SendMsg(Msg* RecievedMsg, Msg** SentMsgs);

    float Height;
	float LensSize;
	bool HasLens;
	bool HasSpawnedChildren;
	
	//TEMP: right now, every tree has its own mesh.  THIS IS NOT THE BEST WAY TO DO THIS;
	//  IN FACT, IT ISN'T EVEN GOOD.  DON'T KEEP IT LIKE THIS.
	Mesh* ShaftMesh;
};


#endif