//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Entity that propagates general data needed by clients for every player.
//
// $NoKeywords: $
//=============================================================================//

#ifndef C_RTSRESOURCE_H
#define C_RTSRESOURCE_H
#ifdef _WIN32
#pragma once
#endif

#include "shareddefs.h"
#include "c_baseentity.h"
#include "hl2wars/rts_shareddefs.h"
#include "utlmap.h"
#include "hl2wars/rts_parsedata.h"

class C_AI_BaseNPC;
class BitmapImage;
class CFXQuad2;

typedef CUtlVector< CHandle< C_AI_BaseNPC > > UnitInfoList;
// ##################################################################################
//	>> Global data
// ##################################################################################
class C_RTSResource : public C_BaseEntity//, public IGameResources
{
	DECLARE_CLASS( C_RTSResource, C_BaseEntity );
public:
	DECLARE_CLIENTCLASS();

	C_RTSResource();

	void	UpdateOnRemove();

	virtual void	Activate();

	virtual void	OnRestore();

	Vector GetColor( int team_id ) { return m_vTeamColorTable[team_id]; }
	Vector GetColor255( int team_id );

	// getter for unit index. Automatically adds the unit type to the list, if the unit doesn't exists yet.
	int GetUnitIndex( char *unit_type );
	const char *GetUnitType( int unit_index );
	float GetUnitViewDistance( int unit_index );

	// getters for unit and ability types
	int			GetAbilityType( int selected_ability, int unit_index  );

	// unit managing
	bool IsValidTeam( int team_id );
	int GetSize( int team_id );
	C_AI_BaseNPC *GetUnit( int team_id, int index );
	void AddUnit( int team_id, C_AI_BaseNPC *pNPC  );
	void RemoveUnit( int team_id, C_AI_BaseNPC *pNPC );
	void ChangeTeamUnit( int oldTeam_id, int newTeam_id, C_AI_BaseNPC *pNPC ); 

	virtual void	ClientThink();
	virtual void	OnDataChanged( DataUpdateType_t updateType );
	virtual bool	NeedsUpdate();
	virtual void	Update();

	virtual void	CalculateFOWHeight();

// GR getters
public:
	int GetIndex( int slot ) { return m_iIndex[slot]; }
	int GetPosition( int slot ) { return m_iPosition[slot]; }
	int GetGRColor( int slot ) { return m_iColor[slot]; }
	bool IsReady( int slot ) { return m_bReady[slot]; }
	int GetFaction( int slot ) { return m_iFaction[slot]; }
	int GetType( int slot ) { return m_iType[slot]; }

public:
	bool			IsUnitVisible( C_AI_BaseNPC *pNPC );

private:
	float m_fNeedsUpdate;

	Vector m_vTeamColorTable[MAX_TEAMS_ID_RTS + DEFAULT_TEAMS ];
	bool m_bColorInUse[TOTAL_COLORS];
	bool m_bPositionInUse[MAX_TEAMS_ID_RTS];
	bool m_bPositionsAvailable[MAX_TEAMS_ID_RTS];
	int m_iFaction[MAX_TEAMS_ID_RTS];
	EHANDLE	m_hGameMaster;

	// units registration client side
	UnitInfoList m_UnitInfoList[MAX_TEAMS_ID_RTS];

// GR data
private:
	int m_iType[MAX_TEAMS_ID_RTS];
	int m_iIndex[MAX_TEAMS_ID_RTS];
	int m_iPosition[MAX_TEAMS_ID_RTS];
	int m_iColor[MAX_TEAMS_ID_RTS];	
	bool m_bReady[MAX_TEAMS_ID_RTS];

public:
	// fog of war parameters
	bool m_pFogOfWar[FOW_SIZE / FOW_TILESIZE][FOW_SIZE / FOW_TILESIZE];
	ITexture* m_pTex;
	CFXQuad2 *m_pFogOfWarQuad;
	float m_fFogOfWarHeight;
	CUtlVector< changefogofwar_t > m_utlFogInfo;		// the fog of war texture will generate/clear the fog at this parts.
	//bool m_bFogNeedsToBeCleared;
	int m_iSizeFow;
	//IVTFTexture *pVTFTexture;

public:
	bool IsColorFree( int color ) { return !m_bColorInUse[color]; }

	// free is wheter a player is occupying it or not, available depends on the map
	bool IsPositionFree( int position ) { return !m_bPositionInUse[position]; }

	C_BasePlayer *GetGameMaster() { return ToBasePlayer(m_hGameMaster.Get()); }
};

extern C_RTSResource *g_RTSR;

#endif // C_RTSRESOURCE_H