/*

------------------------------------------------------------------

Black Nebula

File :				intro.c
Programmer:		Colin Adams
Date:				15/5/91
Last Modified :	10/6/91

Description:

Does all the stuff for the introduction screens at the start.

------------------------------------------------------------------

*/


/*	------------------------------------------------------------------
		Includes and Defines
		------------------------------------------------------------------
*/

#define AMIGA_INCLUDES
#include "3d.h"

#include "intro.h"

/*	------------------------------------------------------------------
		Data/Variables
		------------------------------------------------------------------
*/

struct IntuiMessage *message;
struct Window *window1;
struct Screen *screen;
struct RastPort *intro_rast;

USHORT Code;
ULONG Class;
APTR IAddress;

static int x;
static int y;

extern char *EnterName(void);
extern int player_score;

/*	------------------------------------------------------------------
		Routines
		------------------------------------------------------------------
*/

void cls(void)
{
	x = 15;
	y = 135;
	
	SetAPen(intro_rast, 0);
	RectFill(intro_rast, x, y-10, 635, 250);
	SetAPen(intro_rast, 2);
	Move(intro_rast, x, y);
}

void printit(char *string, char flag)
{
	Move(intro_rast, x, y);
	Text(intro_rast, string, strlen(string));
  
	x += TextLength(intro_rast, string, strlen(string));
  
	if(flag)
	{
		y+= 10;
		x = 15;
	}
}

void print(char *string)
{
  printit(string, 0);
}

void println(char *string)
{
	printit(string, 1);
}

void ViewFame(void)
{
	FILE *fp;
	int i, score, c, counter, number;
	char name[50];
	char printbuff[20];
	
	if(!(fp=fopen("fame.dat","r")))
	{
		DisplayBeep(NULL);
		return;
	}
	
	cls();
	
	fscanf(fp, "%d",&number);
		
	SetAPen(intro_rast, 3);
	print("Name:");
	x = 480;
	println("Score:");
		
	for(i=0; i<number; i++)
	{
		SetAPen(intro_rast, 2);
		counter = 0;
		while((c=getc(fp))!='+')
		{
			if(c!='\n')
				name[counter++] = c;
		}
		
		name[counter] = 0;

		fscanf(fp,"%d",&score);
		
		print(name);
		x = 480;
		sprintf(printbuff, "%6d",score);
		SetAPen(intro_rast, 1);
		println(printbuff);
	}
	fclose(fp);
}

void Instructions(void)
{
	cls();
	SetAPen(intro_rast, 3);
	println("Objective:");
	SetAPen(intro_rast, 2);
	println("  Blow everything to pieces.");
	SetAPen(intro_rast, 3);
	println("Controls:");
	SetAPen(intro_rast, 2);
	println("  Joystick up - increase altitude");
	println("  Joystick down - decrease altitude");
	println("  Joystick right - rotate ship clockwise");
	println("  Joystick left - rotate ship anti-clockwise");
	println("  Space Bar - Increase velocity");
	println("  Backspace - Decrease velocity");
	println("  Escape - Quit");
	println("  P - Pause");
	println("  U - Unpause");
}
  
void OutputScore(void)
{
	char pscore[10], flag = 0;
	FILE *fp;
	int number, c, i, counter, score[10];
	char name[10][50];
	
	cls();
	SetAPen(intro_rast, 2);
	print("You scored ");
	sprintf(pscore, "%d",player_score);
	SetAPen(intro_rast, 3);
	print(pscore);
	SetAPen(intro_rast, 2);
	println(" points.");


	/* read in high score table */
	
	if(!(fp=fopen("fame.dat","r")))
	{
		DisplayBeep(NULL);
		return;
	}
	
	fscanf(fp, "%d",&number);
		
	for(i=0; i<number; i++)
	{
		counter = 0;
		while((c=getc(fp))!='+')
		{
			if(c!='\n')
				name[i][counter++] = c;
		}
		
		name[i][counter] = 0;

		fscanf(fp,"%d",&score[i]);
	}
	
	fclose(fp);

	/* check if player score higher than someone in the hall of fame */
	
	for(i=0; i<number; i++)
	{
		if(player_score > score[i])
		{
			int j;
			
			println("");
			println("Congratulations!!! You have a new high score.");
			
			for(j=number-1; j>i; j--)
			{
				strcpy(&name[j][0], &name[j-1][0]);
				score[j] = score[j-1];
			}
			
			strcpy(&name[i][0], EnterName());
			score[i] = player_score;
			flag = 1;
			break;
		}
	}

	if(!flag)
	{
		println("");
		println("You didn't score high enough to qualify for the hall");
		println("of fame.");
		SetAPen(intro_rast, 3);
		println("");
		println("More practise is recommended!!");
		return;
	}

	/* write it out again */
	
	
	if(!(fp=fopen("fame.dat","w")))
	{
		DisplayBeep(NULL);
		return;
	}
	
	fprintf(fp,"%d\n",number);
	
	for(i=0; i<number; i++)
		fprintf(fp,"%s+ %d\n",&name[i][0],score[i]);
		
	fclose(fp);
		
}

void EventLoop(void)
{
	while(1)
	{
		if((message=(struct IntuiMessage *)
			GetMsg(window1->UserPort))==NULL)
		{
			Wait(1 << window1->UserPort->mp_SigBit);
			continue;
		}
		
		/* store fields needed */
		
		Class = message->Class; 
		Code = message->Code;
		IAddress = message->IAddress;
		
		ReplyMsg((struct Message *) message);	/* reply to intuition */

		if(Class == GADGETUP)
		{
			if(IAddress==(APTR) &Gadget1)
			{
				Do3d();
				OutputScore();
			}
			else if(IAddress==(APTR) &Gadget2)
				ViewFame();
			else if(IAddress==(APTR) &Gadget3)
				Instructions();
			else if(IAddress==(APTR) &Gadget4)
				return;
		}

	}
}

void DoIntro(void)
{
	if(!(screen=(struct Screen *) OpenScreen(&NewScreenStructure)))
	{
		printf("Open Screen failed.\n");
		CleanUpandExit();
	}
	
	NewWindowStructure1.Screen = screen;

  if(!(window1=(struct Window *) OpenWindow(&NewWindowStructure1)))
	{
		printf("Open Window failed.\n");
		CleanUpandExit();
	}
	
	intro_rast = window1->RPort;
	ActivateWindow(window1); /* make window the active one */

	EventLoop();
}

/* end of module intro.c */
