


#include "dualshock.h"


int dsCurEvent, dsNumEvents;
DSHOCK_EVENT dShockEvents[DSHOCK_MAXEVENT];




int dsEvent_Init(void)
{
	dsCurEvent = 0;
	dsNumEvents = 0;
	memset(dShockEvents, 0, sizeof(DSHOCK_MAXEVENT));

	SHOCK_NOBUZZ(0);
	SHOCK_SET(0,0);

	return 1;
}



int dsEvent_Add(int buzzer, int time, int motor, int startSpeed, int endSpeed, int endTime, int queue)
{
int newEvent = dsCurEvent;
DSHOCK_EVENT *ev;

	// Check to see if we're to queue the event.
	if (queue)
	{
		if (dsNumEvents >= DSHOCK_MAXEVENT)			// Are there any event spaces left ?
		{
			return 0;
		} // end-if

		newEvent = DSHOCK_IDX_ROUND(dsCurEvent+dsNumEvents-1);	// Get the last event
		newEvent = DSHOCK_IDX_NEXT(newEvent);					// Get the index of the new event

//		printf("dsEvent : Queuing a new event at %i\n", newEvent);

	} else {										// else, just set this even off now

		newEvent = dsCurEvent;						// Set the new event to be put in place of the
													// current one.
	} // end-if-else

	ev = &dShockEvents[newEvent];					// Get a pointer to the new event.

//	ev->buzzer		= buzzer;						// Set the values of the event
//	ev->motor		= motor;						//
	ev->flags		= 0;
	if(buzzer)
		ev->flags	|= DSFLAG_BUZZER;
	if(motor)
		ev->flags	|= DSFLAG_MOTOR;
	ev->time		= time;							//
	ev->startSpeed	= startSpeed;					//
	ev->endSpeed	= endSpeed;						//
//	ev->done		= 0;							// RFW And "done" is used where, exactly?
	ev->endTime		= endTime;

	if (queue)										// If we're to queue the events, then
	{												// just increment the number of events

		if (dsNumEvents==0)
		{
			if (buzzer)
			{
				SHOCK_BUZZTIME(0, time);
			}

			if (motor)
			{
				SHOCK_SET(0, startSpeed);
				if (endTime > 0)
				{
					SHOCK_SLIDE(0, endSpeed, endTime);
				}
			}
		}

		dsNumEvents++;

	} else {										// Else, set the dual shock up

		if (buzzer)
		{
			SHOCK_BUZZTIME(0, time);
		}

		if (motor)
		{
			SHOCK_SET(0, startSpeed);
			if (endTime > 0)
			{
				SHOCK_SLIDE(0, endSpeed, endTime);
			}
		}

		if(dsNumEvents==0)
			dsNumEvents++;

	} // end-if-else

	return 1;
}



int dsEvent_IsFinished(DSHOCK_EVENT *ev)
{
int flag = 1;

	ASSERT(ev);

	if (ev->flags & DSFLAG_BUZZER)	//ev->buzzer)
	{
		if (padData.buzzTime[0] != 0)
			flag = 0;
	}

	if (ev->flags & DSFLAG_MOTOR)	// (ev->motor)
	{
		if ((padData.currShock[0] >> 16) != ev->endSpeed)
			flag = 0;
	}

	return flag;
}



int dsEvent_Update(void)
{
	if (dsNumEvents == 0)
	{
//		printf("No events !\n");
		return 0;
	}

	if (dsEvent_IsFinished(&dShockEvents[dsCurEvent]))
	{
//		printf("dsEvent : Event %i has finished.....", dsCurEvent);
		dsNumEvents--;
		if (dsNumEvents)
		{
			dsCurEvent = DSHOCK_IDX_NEXT(dsCurEvent);

//			printf("moving onto event %i\n", dsCurEvent);

			if (dShockEvents[dsCurEvent].flags & DSFLAG_BUZZER)			//.buzzer)
			{
				SHOCK_BUZZTIME(0, dShockEvents[dsCurEvent].time);
			}

			if (dShockEvents[dsCurEvent].flags & DSFLAG_MOTOR)			//.motor)
			{
				SHOCK_SET(0, dShockEvents[dsCurEvent].startSpeed);
				if (dShockEvents[dsCurEvent].endTime > 0)
				{
					SHOCK_SLIDE(0, dShockEvents[dsCurEvent].endSpeed, dShockEvents[dsCurEvent].endTime);
				}
			}
		} else {
//			printf("no more events\n");
		}
	}

	return 1;
}

