/***************************************************************************
 *
 * PROJECT: The Dark Mod
 * $Revision$
 * $Date$
 * $Author$
 *
 ***************************************************************************/

// Copyright (C) 2004 Id Software, Inc.
//

#ifndef __GAME_LIGHT_H__
#define __GAME_LIGHT_H__

/*
===============================================================================

  Generic light.

===============================================================================
*/
class CLightMaterial;

extern const idEventDef EV_Light_GetLightParm;
extern const idEventDef EV_Light_SetLightParm;
extern const idEventDef EV_Light_SetLightParms;

class idLight : public idEntity {
public:
	CLASS_PROTOTYPE( idLight );

	idLight();
	virtual ~idLight();

	void			Spawn( void );

	void			Save( idSaveGame *savefile ) const;					// archives object for save game file
	void			Restore( idRestoreGame *savefile );					// unarchives object from save game file

	virtual void	UpdateChangeableSpawnArgs( const idDict *source );
	virtual void	Think( void );
	virtual void	FreeLightDef( void );
	virtual bool	GetPhysicsToSoundTransform( idVec3 &origin, idMat3 &axis );
	void			Present( void );

	void			SaveState( idDict *args );
	virtual void	SetColor( const float red, const float green, const float blue );
	virtual void	SetColor( const idVec4 &color );
	virtual void	GetColor( idVec3 &out ) const;
	virtual void	GetColor( idVec4 &out ) const;

	/**
	* Tels: idLight::GetLightOrigin returns the origin of the light in the world. This
	* is different from the physics origin, since the light can be offset.
	*/
	const idVec3 &	GetLightOrigin( void ) const { return renderLight.origin; }

	const idVec3 &	GetBaseColor( void ) const { return baseColor; }
	void			SetShader( const char *shadername );
	void			SetLightParm( int parmnum, float value );
	void			SetLightParms( float parm0, float parm1, float parm2, float parm3 );
	void			SetRadiusXYZ( const float x, const float y, const float z );
	void			SetRadius( const float radius );
	void			On( void );
	void			Off( const bool stopSound = true );
	void			Fade( const idVec4 &to, float fadeTime );
	void			FadeOut( float time );
	void			FadeIn( float time );
	void			FadeTo( idVec3 color, float time );
	void			Killed( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location );
	void			BecomeBroken( idEntity *activator );
	qhandle_t		GetLightDefHandle( void ) const { return lightDefHandle; }
	void			SetLightParent( idEntity *lparent ) { lightParent = lparent; }
	void			SetLightLevel( void );

	/**
	 * greebo: Returns the current lightlevel (currentlevel).
	 */
	int				GetLightLevel() const;

	/**
	 * Tels: return the current light radius.
	 */
	void			GetRadius( idVec3 &out ) const;

	virtual void	ShowEditingDialog( void );

	enum {
		EVENT_BECOMEBROKEN = idEntity::EVENT_MAXEVENTS,
		EVENT_MAXEVENTS
	};

	virtual void	ClientPredictionThink( void );
	virtual void	WriteToSnapshot( idBitMsgDelta &msg ) const;
	virtual void	ReadFromSnapshot( const idBitMsgDelta &msg );
	virtual bool	ClientReceiveEvent( int event, int time, const idBitMsg &msg );

	/**	Returns a bounding box surrounding the light.
	 */
	idBounds		GetBounds();
	/**	Called to update m_renderTrigger after the render light is modified.
	 *	Only updates the render trigger if a thread is waiting for it.
	 */
	void			PresentRenderTrigger();

	/**
	 * This will return a grayscale value dependent on the value from the light.
	 * X and Y are the coordinates returned by calculating the position from the 
	 * player related to the light. The Z coordinate can be ignored. The distance
	 * is required when the light has no textures to calculate a falloff.
	 */
	float			GetDistanceColor(float distance, float x, float y);

	/**
	 * GetTextureIndex calculates the index into the texture based on the x/y coordinates
	 * given and returns the index.
	 */
	int				GetTextureIndex(float x, float y, int TextureWidth, int TextureHeight, int BytesPerPixel);

	/**
	 * Returns true if the light is a parallel light.
	 */
	inline bool		IsParallel(void) { return renderLight.parallel; };
	inline bool		IsPointlight(void) { return renderLight.pointLight; };
	bool			CastsShadow(void);
	bool			IsAmbient(void);	// Returns true if this is an ambient light. - J.C.Denton

	/**
	 * GetLightCone returns the lightdata.
	 * If the light is a pointlight it will return an ellipsoid defining the light.
	 * In case of a projected light, the returned data is a cone.
	 * If the light is a projected light and uses the additional vectors for
	 * cut off cones, it will return true.
	 */
	bool GetLightCone(idVec3 &Origin, idVec3 &Axis, idVec3 &Center);
	bool GetLightCone(idVec3 &Origin, idVec3 &Target, idVec3 &Right, idVec3 &Up, idVec3 &Start, idVec3 &End);

	/**
	 * grayman #2603 - add a switch to the switch list
	 */

	void AddSwitch(idEntity* newSwitch);

	/**
	 * grayman #2603 - If there are switches, returns the closest one to the calling user
	 */

	idEntity* GetSwitch(idAI* user);

	/**
	 * grayman #2603 - Change the flag that says if this light is being relit.
	 */

	void SetBeingRelit(bool relighting);

	/**
	 * grayman #2603 - Is an AI in the process or relighting this light?
	 */

	bool IsBeingRelit();

	/**
	 * grayman #2603 - Set the chance that this light can be barked about negatively
	 */

	void SetChanceNegativeBark(float newChance);

	/**
	 * grayman #2603 - can an AI make a negative bark about this light (found off, or won't relight)
	 */

	bool NegativeBark(idAI* ai);

	/**
	 * grayman #2603 - Get when the light was turned off
	 */

	int GetWhenTurnedOff();

	/**
	 * grayman #2603 - Get when can this light be relit
	 */

	int GetRelightAfter();

	/**
	 * grayman #2603 - Set when can this light be relit
	 */

	void SetRelightAfter();

	/*
	 * grayman #2603 - Get when an AI can next emit a "light's out" or "won't relight" bark
	 */

	int GetNextTimeLightOutBark();

	/*
	 * grayman #2603 - Set when an AI can next emit a "light's out" or "won't relight" bark
	 */

	void SetNextTimeLightOutBark(int newNextTimeLightOutBark);

	/*
	 * grayman #2603 - Is a flame vertical?
	 */

	bool IsVertical(float degreesFromVertical);

	/*
	 * grayman #2603 - flame is out and smoking?
	 */

	bool IsSmoking();

private:
	renderLight_t	renderLight;				// light presented to the renderer
	idVec3			localLightOrigin;			// light origin relative to the physics origin
	idMat3			localLightAxis;				// light axis relative to physics axis
	qhandle_t		lightDefHandle;				// handle to renderer light def
	int				levels;
	int				currentLevel;
	idVec3			baseColor;
	bool			breakOnTrigger;
	int				count;
	int				triggercount;
	idEntity *		lightParent;
	bool			beingRelit;					// grayman #2603 - true if being relit
	idList< idEntityPtr<idEntity> > switchList;	// grayman #2603 - list of my switches
	float			chanceNegativeBark;			// grayman #2603 - chance of negative barks ("light's out" and "won't relight")
	int				whenTurnedOff;				// grayman #2603 - when this light was turned off
	int				nextTimeLightOutBark;		// grayman #2603 - the next time an AI can bark about a light that's out, or a decision not to relight it
	int				relightAfter;				// grayman #2603 - when a light can be relit
	float			nextTimeVerticalCheck;		// grayman #2603 - the next time to check if a lit flame is vertical
	bool			smoking;					// grayman #2603 - the flame model has changed to a smoke partical model; considered "out"
	int				whenToDouse;				// grayman #2603 - when a non-vertical flame can be doused

	idVec4			fadeFrom;
	idVec4			fadeTo;
	int				fadeStart;
	int				fadeEnd;
	bool			soundWasPlaying;

	/**
	 * grayman #2603 - Per-AI info for counting negative barks. This allows
	 * lights to keep track of the number of negative barks it elicits from
	 * passing AI. AI are only allowed 2 negative barks per light. The list
	 * is cleared when the light is relit, allowing AI to once again make
	 * negative barks.
	 */
	struct AIBarks
	{
		// The number of negative barks for this AI
		int count;
		
		// The AI who made them
		idEntityPtr<idEntity> ai;
	};

	idList<AIBarks> aiBarks; // grayman #2603


private:
	void			PresentLightDefChange( void );
	void			PresentModelDefChange( void );

	void			Event_SetShader( const char *shadername );
	void			Event_GetLightParm( int parmnum );
	void			Event_SetLightParm( int parmnum, float value );
	void			Event_SetLightParms( float parm0, float parm1, float parm2, float parm3 );
	void			Event_SetRadiusXYZ( float x, float y, float z );
	void			Event_SetRadius( float radius );
	void			Event_Hide( void );
	void			Event_Show( void );
	void			Event_On( void );
	void			Event_Off( void );
	void			Event_ToggleOnOff( idEntity *activator );
	void			Event_SetSoundHandles( void );
	void			Event_FadeOut( float time );
	void			Event_FadeIn( float time );
	/**
	* Tels: Allows the color of the light to fade over to a new value over a time period.
	*/
	void			Event_FadeToLight( idVec3 &color, float time );
	/**
	* Allows script to get and set the light origin separate from model origin.
	* Used to achieve moving lights with a stationary model
	**/
	void			Event_GetLightOrigin( void );
	void			Event_SetLightOrigin( idVec3 &pos );

	/**
	* Allows us to get the light level, and tell if the light is on or off.
	* "On" light levels are > 0.0
	*/
	void			Event_GetLightLevel();


	/*
	*	Add the light to the appropriate LAS area
	*/
	void			Event_AddToLAS();

	/**	Returns 1 if the light is in PVS.
	 *	Doesn't take into account vis-area optimizations for shadowcasting lights.
	 */
	void			Event_InPVS();

	/**
	 * grayman #2603
	 */

	void			Event_Smoking(int state);

	/**
	 * Texturename for the falloff image
	 */
	const char		*m_MaterialName;

	/**
	 * Pointer to the material that is used for this light. This pointer
	 * is only loaded once. If the material needs to change dynamically
	 * for a light, the m_FalloffImage must be set to the new material name
	 * and m_LightMaterial must be set to NULL, to force the reload, next
	 * time the light should use a new material.
	 */
	CLightMaterial	*m_LightMaterial;

public:

	/**
	 * Each light also gets the maxlightradius, which determines which value
	 * is the maximum radius for that particular light,
	 */
	float			m_MaxLightRadius;

	/*!
	* Darkmod LAS
	* The area the light is in, assigned by The Dark Mod Lighting Awareness System (LAS)
	*/
	int LASAreaIndex;

};

#endif /* !__GAME_LIGHT_H__ */
