/*
 * @(#)PyraminxU.c
 *
 * Copyright 1994 - 2008  David A. Bagley, bagleyd@tux.org
 *
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the author not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * This program is distributed in the hope that it will be "useful",
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

/* Undo algorithm for Pyraminx */

#include "PyraminxP.h"

PyraminxLoc *startLoc[MAX_FACES] =
{
	NULL, NULL, NULL, NULL
};

static void
newStack(PyraminxStack *s)
{
	if (s->lastMove != NULL || s->firstMove != NULL)
		return;
	if (!(s->lastMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	if (!(s->firstMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	s->firstMove->previous = s->lastMove->next = NULL;
	s->firstMove->next = s->lastMove;
	s->lastMove->previous = s->firstMove;
	s->count = 0;
}

static void
pushStack(PyraminxStack *s, moveRecord **move)
{
	if (!(s->currMove = (MoveStack *) malloc(sizeof (MoveStack)))) {
		DISPLAY_ERROR("Not enough memory, exiting.");
	}
	s->lastMove->previous->next = s->currMove;
	s->currMove->previous = s->lastMove->previous;
	s->currMove->next = s->lastMove;
	s->lastMove->previous = s->currMove;
	*move = &(s->currMove->move);
	s->count++;
}

static void
popStack(PyraminxStack *s)
{
	s->currMove = s->lastMove->previous;
	s->lastMove->previous->previous->next = s->lastMove;
	s->lastMove->previous = s->lastMove->previous->previous;
	free(s->currMove);
	s->count--;
}

static moveRecord *
topStack(PyraminxStack *s)
{
	return &(s->lastMove->previous->move);
}

static int
emptyStack(PyraminxStack *s)
{
	return (s->lastMove->previous == s->firstMove);
}

static void
flushStack(PyraminxStack *s)
{
	while (s->lastMove->previous != s->firstMove) {
		s->currMove = s->lastMove->previous;
		s->lastMove->previous->previous->next = s->lastMove;
		s->lastMove->previous = s->lastMove->previous->previous;
		free(s->currMove);
	}
	s->count = 0;
}

static void
deleteStack(PyraminxStack *s)
{
	flushStack(s);
	if (s->firstMove) {
		free(s->firstMove);
		s->firstMove = NULL;
	}
	if (s->lastMove) {
		free(s->lastMove);
		s->lastMove = NULL;
	}
}

/**********************************/

void
newMoves(PyraminxStack *s)
{
	newStack(s);
}

void
deleteMoves(PyraminxStack *s)
{
	deleteStack(s);
}

static void
writeMove(moveRecord *move, int face, int position, int direction,
		int style, int control)
{
#if 0
	move->face = face;
	move->direction = direction;
	move->style = style;
	move->control = control;
#endif
	move->packed = ((control & 0xF) << 12) + ((style & 0xF) << 8) +
		((direction & 0xF) << 4) + (face & 0xF);
	move->position = position;
}

static void
readMove(moveRecord *move, int *face, int *position, int *direction,
		int *style, int *control)
{
#if 0
	*face = move->face;
	*direction = move->direction;
	*style = move->style;
	*control = move->control;
#endif
	*face = (int) (move->packed & 0xF);
	*direction = (int) ((move->packed >> 4) & 0xF);
	*style = (int) ((move->packed >> 8) & 0xF);
	*control = (int) ((move->packed >> 12) & 0xF);
	*position = move->position;
}

void
setMove(PyraminxStack *s, int face, int position, int direction,
		int style, int control)
{
	moveRecord *move;

	pushStack(s, &move);
	writeMove(move, face, position, direction, style, control);
}

void
getMove(PyraminxStack *s, int *face, int *position, int *direction,
		int *style, int *control)
{
	readMove(topStack(s), face, position, direction, style, control);
	popStack(s);
}

int
madeMoves(PyraminxStack *s)
{
	return !emptyStack(s);
}

void
flushMoves(PyraminxWidget w, PyraminxStack *s, Boolean undo)
{
	int face, position;

	flushStack(s);
	if (undo) {
		for (face = 0; face < MAX_FACES; face++)
			for (position = 0; position < w->pyraminx.sizeSize; position++) {
				startLoc[face][position].face =
					w->pyraminx.facetLoc[face][position].face;
				startLoc[face][position].rotation =
					w->pyraminx.facetLoc[face][position].rotation;
			}
	}
}

int
numMoves(PyraminxStack *s)
{
	return s->count;
}

void
scanMoves(FILE *fp, PyraminxWidget w, int moves)
{
	int face, position, direction, style, control, k, c;

	for (k = 0; k < moves; k++) {
		while ((c = getc(fp)) != EOF && c != SYMBOL);
		(void) fscanf(fp, "%d %d %d %d %d",
			&face, &position, &direction, &style, &control);
		movePuzzle(w, face, position, direction, style, control);
	}
}

void
printMoves(FILE *fp, PyraminxStack *s)
{
	int face, position, direction, style, control, counter = 0;

	s->currMove = s->firstMove->next;
	(void) fprintf(fp, "moves\tface\tpos\tdir\tstyle\tcon\n");
	while (s->currMove != s->lastMove) {
		readMove(&(s->currMove->move),
			&face, &position, &direction, &style, &control);
		(void) fprintf(fp, "%d%c\t%d\t%d\t%d\t%d\t%d\n",
		++counter, SYMBOL, face, position, direction, style, control);
		s->currMove = s->currMove->next;
	}
}

void
scanStartPosition(FILE *fp, PyraminxWidget w)
{
	int face, position, num, c;

	while ((c = getc(fp)) != EOF && c != SYMBOL);
	for (face = 0; face < MAX_FACES; face++)
		for (position = 0; position < w->pyraminx.sizeSize; position++) {
			(void) fscanf(fp, "%d ", &num);
			startLoc[face][position].face = num;
			if (w->pyraminx.orient) {
				(void) fscanf(fp, "%d ", &num);
				startLoc[face][position].rotation = num;
			}
		}
}

void
printStartPosition(FILE *fp, PyraminxWidget w)
{
	int face, position;

	(void) fprintf(fp, "\nstartingPosition%c\n", SYMBOL);
	for (face = 0; face < MAX_FACES; face++) {
		for (position = 0; position < w->pyraminx.sizeSize; position++) {
			(void) fprintf(fp, "%d ", startLoc[face][position].face);
			if (w->pyraminx.orient)
				(void) fprintf(fp, "%d  ",
					startLoc[face][position].rotation);
		}
		(void) fprintf(fp, "\n");
	}
}

void
setStartPosition(PyraminxWidget w)
{
	int face, position;

	for (face = 0; face < MAX_FACES; face++)
		for (position = 0; position < w->pyraminx.sizeSize; position++) {
			w->pyraminx.facetLoc[face][position].face =
				startLoc[face][position].face;
			if (w->pyraminx.orient)
				w->pyraminx.facetLoc[face][position].rotation =
					startLoc[face][position].rotation;
		}
	drawAllPieces(w);
}
