/* All the game type stuff goes in here! */
#include "gameinc.h"

/* Describe the games state */
int game_map_loaded=0;
br_actor *game_rootactor=NULL;
br_actor *game_worldactor=NULL;
float previous_gametimer = -1.0F;

void Engine_StartupGame(br_actor *this)
{

	game_worldactor = BrActorAllocate(BR_ACTOR_NONE, NULL);
	BrActorAdd(this,game_worldactor);

	Editor_OverlayDebug("");
	Editor_OverlayDebug("Startup game server V1.00");
	Editor_OverlayDebug("=========================");
	Editor_OverlayDebug("");

	Entity_Init();
	Level_LoadMap("start");

	Entity_InitForGame();
	Editor_OverlayDebug("Exec game loop");

}


void Engine_RunGameLoop(void)
{
	float gametimer;
	int numticks,i;
	static int tickspassed;

// FIXME Add in the entity add and remove list and funxtion support for this here
// somewhere.

	/* Trap first case and always send a ticker event */
	if (previous_gametimer == -1.0F)
	{
		previous_gametimer = (float) DOSClockRead();
		previous_gametimer = previous_gametimer/1000000.0F;
		gametimer = previous_gametimer;
		Entity_SendMessageToAll(ENTITY_MESSAGE_TICKER);
		tickspassed=0;
	}
	gametimer = (float) DOSClockRead();
	gametimer = gametimer/1000000.0F;

	numticks = (int) (25.0F * (gametimer - previous_gametimer));
	if (numticks < 0 )
	{
		previous_gametimer = (float) DOSClockRead();
		previous_gametimer = previous_gametimer/1000000.0F;
		gametimer = previous_gametimer;
		Entity_SendMessageToAll(ENTITY_MESSAGE_TICKER);
		tickspassed=0;
		numticks=0;
		Entity_SendMessageToAll(ENTITY_MESSAGE_TICKER);
		Editor_OverlayDebug("Clock over ran!");
	}

	for (i=tickspassed;i<numticks;i++)
	{
		Entity_SendMessageToAll(ENTITY_MESSAGE_TICKER);
	}

	tickspassed = numticks;
}

