#ifndef __JITTER_H__
#define __JITTER_H__

#include "g_local.h"
#include "trigger.h"

class BaseJitter : public Trigger
{
protected:
	qboolean angleactive;
	float    angleduration;
	float    anglemagnitude;
	float    anglefalloff;
	qboolean offsetactive;
	float    offsetduration;
	float    offsetmagnitude;
	float    offsetfalloff;

public:
	CLASS_PROTOTYPE(BaseJitter);
	BaseJitter();
	qboolean AngleActive() {return angleactive;};
	qboolean OffsetActive() {return offsetactive;};

	virtual void Archive(Archiver &arc);
	virtual void Unarchive(Archiver &arc);
};

inline void BaseJitter::Archive (Archiver &arc)
{
	Trigger::Archive(arc);

	arc.WriteBoolean(angleactive);
	arc.WriteFloat(angleduration);
	arc.WriteFloat(anglemagnitude);
	arc.WriteFloat(anglefalloff);
	arc.WriteBoolean(offsetactive);
	arc.WriteFloat(offsetduration);
	arc.WriteFloat(offsetmagnitude);
	arc.WriteFloat(offsetfalloff);
}

inline void BaseJitter::Unarchive (Archiver &arc)
{
	Trigger::Unarchive(arc);

	angleactive = arc.ReadBoolean();
	angleduration = arc.ReadFloat();
	anglemagnitude = arc.ReadFloat();
	anglefalloff = arc.ReadFloat();
	offsetactive = arc.ReadBoolean();
	offsetduration = arc.ReadFloat();
	offsetmagnitude = arc.ReadFloat();
	offsetfalloff = arc.ReadFloat();
}

extern float anglejitter_time; // timmer for angle jitter
extern float anglejitter_magnitude; // angle jitter magnitude
extern float anglejitter_falloff; // falloffrate for angle jitter
extern float offsetjitter_time; // timmer for offset jitter
extern float offsetjitter_magnitude; // offset jitter magnitude
extern float offsetjitter_falloff; // falloffrate for offset jitter

class GlobalJitter : public BaseJitter
{
public:
	CLASS_PROTOTYPE(GlobalJitter);
	GlobalJitter();
	void Activate(Event *ev);
	void DeactivateAngle(Event *ev);
	void DeactivateOffset(Event *ev);
	qboolean AngleActive() {return angleactive;};
	qboolean OffsetActive() {return offsetactive;};
};

class RadiusJitter : public BaseJitter
{
protected:
	float jitterradius;
	float radiusfalloff;
	float anglecurrent;
	float angletime;
	float offsetcurrent;
	float offsettime;

public:
	CLASS_PROTOTYPE(RadiusJitter);
	RadiusJitter();
	void Activate(Event *ev);
	void ApplyJitter(Event *ev);
	void DeactivateAngle(Event *ev);
	void DeactivateOffset(Event *ev);
	qboolean AngleActive() {return angleactive;};
	qboolean OffsetActive() {return offsetactive;};
	void Setup(Vector org, float rad, float outer,
		float angdur, float angmag, float angfall,
		float ofsdur, float ofsmag, float ofsfall);
	void SetupSmall(Vector org); // sets it up as a small explosion
	void SetupLarge(Vector org); // sets it up as a large explosion

	virtual void Archive(Archiver &arc);
	virtual void Unarchive(Archiver &arc);
};

inline void RadiusJitter::Archive (Archiver &arc)
{
	BaseJitter::Archive(arc);

	arc.WriteFloat(jitterradius);
	arc.WriteFloat(radiusfalloff);
	arc.WriteFloat(anglecurrent);
	arc.WriteFloat(angletime);
	arc.WriteFloat(offsetcurrent);
	arc.WriteFloat(offsettime);
}

inline void RadiusJitter::Unarchive (Archiver &arc)
{
	BaseJitter::Unarchive(arc);

	jitterradius = arc.ReadFloat();
	radiusfalloff = arc.ReadFloat();
	anglecurrent = arc.ReadFloat();
	angletime = arc.ReadFloat();
	offsetcurrent = arc.ReadFloat();
	offsettime = arc.ReadFloat();
}

#endif /* jitter.h */
