#include"stdafx.h"
#include "UndoQueue.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

UndoQueue::UndoQueue()		
{
	RedoTot=Initalized=Current=Size=Filled=0;
	for(int i=0;i<MAX_UNDO;i++) Data[i]=0;
}

void UndoQueue::Initalize(int sze) 
{
	ASSERT(!Initalized);
	SetSize(sze);
	
	
}

void UndoQueue::UnInitalize() 
{
int i;
for(i=0;i<Size;i++) delete Data[i];

Size=0;
}

UndoQueue::~UndoQueue()
{
UnInitalize();
}

void UndoQueue::SetSize(int sze)
{
	int i;
	ASSERT(sze<=MAX_UNDO);
	
	RedoTot=Current=Filled=0;
	if(Initalized)
		for(i=0;i<Size;i++) {delete Data[i]; Data[i]=0;}
		
		Size=sze;
		for(i=0;i<Size;i++) Data[i]=new TCTTerrainData;
}

TCTTerrainData &UndoQueue::Undo(TCTTerrainData &cstate)
{
	ASSERT(Filled);
	*Data[Current]=cstate;
	Filled--;
	Current--;
	if(Current<0) Current=Size+Current;
	RedoTot++;
	return *Data[Current];
}

TCTTerrainData &UndoQueue::Redo()
{
	ASSERT(RedoTot);
	Current=(Current+1)%Size;
	RedoTot--;
	Filled++;
	return *Data[Current];
}

void UndoQueue::SaveState(TCTTerrainData &tdat)
{
	RedoTot=0;
	Filled++;
	if(Filled>Size) Filled=Size;
	*(Data[Current])=tdat;
	Current=(Current+1)%Size;
}

