/*----		FLAGTRIV.C		Version 1   Revision 12/29/86

		  by Michael Rupertus



	note:  This program was developed using Alcyon C  version 4.14
----*/

#include <stdio.h>
#include <osbind.h>
#include <flagdata.h>
#include <instruct.h>

#define	XOFFSET	27
#define	YOFFSET	45

#define PLAYER_ONE 1
#define PLAYER_TWO 2


long	main_screen, secondary_screen, time, score_1, score_2;

int	contrl[12], intin[256], ptsin[256], intout[256], ptsout[256],
	h, j, work_in[10], work_out[57], flags_shown[100],
	rgb[4], pxy[12], gr_1, gr_2, gr_3, gr_4, number_of_players,
	game_over, cindex, place, did_quess, rounds, draw_in_black,
	black[] = { 0,0,0 }, bkgrnd[] = { 375,250,375 }, x, y, ctry,
	difficulty, speed[] = { 0,401,251,151 }, *dp, *da[100];

char	get_ready_screen[32768], scr_1_string[7], scr_2_string[7],
	medium_message[] = {"[3][|You must use low resolution!] Confirmed]"},
	mono_message[] = {"[3][|You must use a color monitor!] Confirmed]"},
	*na[100];

main()
{
	open_workstation();

	switch( Getrez() )
	       {
		 case 0:
			 {
			   palette_handler();
			   init_arrays();
			   play();
			   palette_handler();
			   break;
		 }

		 case 1:
			 {
			   form_alert( 1, medium_message );
			   break;
		 }

		 case 2:
			 {
			   form_alert( 1, mono_message );
		 }
	}

	close_workstation();
}


open_workstation()
{
	appl_init();
	h = graf_handle( &gr_1, &gr_2, &gr_3, &gr_4 );
	for( j = 0;  j < 10;  work_in[j++] = 1 ) ;  work_in[10] = 2;
	v_opnvwk( work_in, &h, work_out );
	graf_mouse( 256, 0L );
	v_clrwk( h );
}


close_workstation()
{
	graf_mouse( 257, 0L );
	v_clsvwk( h );
	appl_exit();
}


palette_handler()
{
static int hold_palette[16],
	   default_palette[16][3] =
				{
				{	1000,	1000,	1000	},
				{	1000,	   0,	   0	},
				{	   0,	1000,	   0	},
				{	1000,	1000,	   0	},
				{	   0,	   0,	1000	},
				{	1000,	   0,	1000	},
				{	   0,	1000,	1000	},
				{	 750,	 750,	 750	},
				{	 500,	 500,	 500	},
				{	1000,	 500,	 500	},
				{	 500,	1000,	 500	},
				{	1000,	1000,	 500	},
				{	 500,	 500,	1000	},
				{	1000,	 500,	1000	},
				{	 500,	1000,	1000	},
				{	   0,	   0,	   0	}
								};

	if( game_over == TRUE )
	   {
	     Setpalette( hold_palette );
	}
	else {
	       for( j = 0;  j < 16;  ++j )
		   {
		     hold_palette[j] = Setcolor( j, -1 );
	       }

	       for( x = 0;  x < 16;  ++x )
		   {
		     for( y = 0;  y < 3;  ++y )
			 {
			   rgb[y] = default_palette[x][y];
		     }

		     vs_color( h, x, rgb );
	       }
	}
}


play()
{
	show_title();

	while( game_over == FALSE )
	      {
		for( j = 0;  j < 100;  flags_shown[j++] = 0 ) ;

		score_1=score_2=rounds = 0;

		go_for_awhile();
	}
}


init_arrays()
{
	da[ 0] = d_00;	na[ 0] = n_00;
	da[ 1] = d_01;	na[ 1] = n_01;
	da[ 2] = d_02;	na[ 2] = n_02;
	da[ 3] = d_03;	na[ 3] = n_03;
	da[ 4] = d_04;	na[ 4] = n_04;
	da[ 5] = d_05;	na[ 5] = n_05;
	da[ 6] = d_06;	na[ 6] = n_06;
	da[ 7] = d_07;	na[ 7] = n_07;
	da[ 8] = d_08;	na[ 8] = n_08;
	da[ 9] = d_09;	na[ 9] = n_09;
	da[10] = d_10;	na[10] = n_10;
	da[11] = d_11;	na[11] = n_11;
	da[12] = d_12;	na[12] = n_12;
	da[13] = d_13;	na[13] = n_13;
	da[14] = d_14;	na[14] = n_14;
	da[15] = d_15;	na[15] = n_15;
	da[16] = d_16;	na[16] = n_16;
	da[17] = d_17;	na[17] = n_17;
	da[18] = d_18;	na[18] = n_18;
	da[19] = d_19;	na[19] = n_19;
	da[20] = d_20;	na[20] = n_20;
	da[21] = d_21;	na[21] = n_21;
	da[22] = d_22;	na[22] = n_22;
	da[23] = d_23;	na[23] = n_23;
	da[24] = d_24;	na[24] = n_24;
	da[25] = d_25;	na[25] = n_25;
	da[26] = d_26;	na[26] = n_26;
	da[27] = d_27;	na[27] = n_27;
	da[28] = d_28;	na[28] = n_28;
	da[29] = d_29;	na[29] = n_29;
	da[30] = d_30;	na[30] = n_30;
	da[31] = d_31;	na[31] = n_31;
	da[32] = d_32;	na[32] = n_32;
	da[33] = d_33;	na[33] = n_33;
	da[34] = d_34;	na[34] = n_34;
	da[35] = d_35;	na[35] = n_35;
	da[36] = d_36;	na[36] = n_36;
	da[37] = d_37;	na[37] = n_37;
	da[38] = d_38;	na[38] = n_38;
	da[39] = d_39;	na[39] = n_39;
	da[40] = d_40;	na[40] = n_40;
	da[41] = d_41;	na[41] = n_41;
	da[42] = d_42;	na[42] = n_42;
	da[43] = d_43;	na[43] = n_43;
	da[44] = d_44;	na[44] = n_44;
	da[45] = d_45;	na[45] = n_45;
	da[46] = d_46;	na[46] = n_46;
	da[47] = d_47;	na[47] = n_47;
	da[48] = d_48;	na[48] = n_48;
	da[49] = d_49;	na[49] = n_49;
	da[50] = d_50;	na[50] = n_50;
	da[51] = d_51;	na[51] = n_51;
	da[52] = d_52;	na[52] = n_52;
	da[53] = d_53;	na[53] = n_53;
	da[54] = d_54;	na[54] = n_54;
	da[55] = d_55;	na[55] = n_55;
	da[56] = d_56;	na[56] = n_56;
	da[57] = d_57;	na[57] = n_57;
	da[58] = d_58;	na[58] = n_58;
	da[59] = d_59;	na[59] = n_59;
	da[60] = d_60;	na[60] = n_60;
	da[61] = d_61;	na[61] = n_61;
	da[62] = d_62;	na[62] = n_62;
	da[63] = d_63;	na[63] = n_63;
	da[64] = d_64;	na[64] = n_64;
	da[65] = d_65;	na[65] = n_65;
	da[66] = d_66;	na[66] = n_66;
	da[67] = d_67;	na[67] = n_67;
	da[68] = d_68;	na[68] = n_68;
	da[69] = d_69;	na[69] = n_69;
	da[70] = d_70;	na[70] = n_70;
	da[71] = d_71;	na[71] = n_71;
	da[72] = d_72;	na[72] = n_72;
	da[73] = d_73;	na[73] = n_73;
	da[74] = d_74;	na[74] = n_74;
	da[75] = d_75;	na[75] = n_75;
	da[76] = d_76;	na[76] = n_76;
	da[77] = d_77;	na[77] = n_77;
	da[78] = d_78;	na[78] = n_78;
	da[79] = d_79;	na[79] = n_79;
	da[80] = d_80;	na[80] = n_80;
	da[81] = d_81;	na[81] = n_81;
	da[82] = d_82;	na[82] = n_82;
	da[83] = d_83;	na[83] = n_83;
	da[84] = d_84;	na[84] = n_84;
	da[85] = d_85;	na[85] = n_85;
	da[86] = d_86;	na[86] = n_86;
	da[87] = d_87;	na[87] = n_87;
	da[88] = d_88;	na[88] = n_88;
	da[89] = d_89;	na[89] = n_89;
	da[90] = d_90;	na[90] = n_90;
	da[91] = d_91;	na[91] = n_91;
	da[92] = d_92;	na[92] = n_92;
	da[93] = d_93;	na[93] = n_93;
	da[94] = d_94;	na[94] = n_94;
	da[95] = d_95;	na[95] = n_95;
	da[96] = d_96;	na[96] = n_96;
	da[97] = d_97;	na[97] = n_97;
	da[98] = d_98;	na[98] = n_98;
	da[99] = d_99;	na[99] = n_99;
}


show_title()
{
	main_screen = Logbase();
	secondary_screen = ( 0xffff00 & (long)get_ready_screen )+0x0100;

	Setscreen( -1L, secondary_screen, -1 );

	show_flag();

	vsf_color( h, 4 );
	v_contourfill( h, 30, 50, -1 );

	vst_height( h, 12, &gr_1, &gr_2, &gr_3, &gr_4 );

	rgb[0] = 1000;  rgb[1] = 500;  rgb[2] = 0;
	vs_color( h, 12, rgb );
	vst_color( h, 12 );
	vst_effects( h, 5 );
	vswr_mode( h, 2 );

	v_gtext( h, 65, 87, "FLAG TRIVIA" );

	vst_height( h, 6, &gr_1, &gr_2, &gr_3, &gr_4 );

	rgb[0] = 625;  rgb[1] = 0;  rgb[2] = 625;
	vs_color( h, 13, rgb );
	vst_color( h, 13 );
	vst_effects( h, 0 );

	v_gtext( h, 135, 110, "by" );

	vst_height( h, 9, &gr_1, &gr_2, &gr_3, &gr_4 );
	vst_effects( h, 2 );

	v_gtext( h, 52, 140, "MICHAEL RUPERTUS" );

	Setscreen( secondary_screen, main_screen, -1 );

	vsf_interior( h, 2 );
	vsf_style( h, 9 );
	rgb[0] = 1000;  rgb[1]=rgb[2] = 0;
	vs_color( h, 14, rgb );
	vsf_color( h, 14 );
	v_contourfill( h, 0, 0, -1 );
	vsf_interior( h, 1 );

	vst_height( h, 60, &gr_1, &gr_2, &gr_3, &gr_4 );
	vst_effects( h, 33 );
	vst_color( h, 4 );

	v_gtext( h, 80,  100, "GET" );
	v_gtext( h, 140, 130, "READY!" );

	vst_height( h, 7, &gr_1, &gr_2, &gr_3, &gr_4 );
	vst_effects( h, 4 );
	vst_color( h, 2 );
	vst_rotation( h, 900 );
	v_gtext( h, 310, 190, "Kilroy" );
	v_gtext( h, 310, 123, "was" );
	v_gtext( h, 310,  86, "here!" );
	vst_rotation( h, 0 );
	vst_height( h, 6, &gr_1, &gr_2, &gr_3, &gr_4 );

	Setscreen( main_screen, -1L, -1 );

	v_gtext( h, 65, 183, "Press any key to play" );
	Cnecin();

	draw_in_black = TRUE;
	erase_a_flag();
	draw_in_black = FALSE;

	vst_height( h, 7, &gr_1, &gr_2, &gr_3, &gr_4 );
	v_gtext( h, 30, 90, "Do you need instructions?" );
	v_gtext( h, 90, 120, "Press Y or N" );

	for( j = 0;  j != 'y'  &&  j != 'Y'  &&  j != 'n'  &&  j != 'N'; )
	    {
	      j = (int)Bconin( 2 );
	}

	if( j == 'y'  ||  j == 'Y' )
	   {
	     show_instructions();
	}

	erase_a_flag();

	vst_height( h, 8, &gr_1, &gr_2, &gr_3, &gr_4 );
	v_gtext( h, 45, 100, "How many will play?" );
	vst_height( h, 6, &gr_1, &gr_2, &gr_3, &gr_4 );
	v_gtext( h, 60, 140, "Press 1 or 2" );
	vst_height( h,  6, &gr_1, &gr_2, &gr_3, &gr_4 );

	for( j = 0;  j != '1'  &&  j != '2'; )
	    {
	      j = (int)Bconin( 2 );
	}

	number_of_players = j-48;

	draw_in_black = TRUE;
	erase_a_flag();

	vst_height( h, 8, &gr_1, &gr_2, &gr_3, &gr_4 );
	v_gtext( h, 50, 100, "Select skill level:" );
	vst_height( h, 6, &gr_1, &gr_2, &gr_3, &gr_4 );

	vst_color( h, 11 );
	v_gtext( h, 112, 120, "1 =" );
	v_gtext( h, 112, 130, "2 =" );
	v_gtext( h, 112, 140, "3 =" );

	vst_color( h, 12 );
	v_gtext( h, 144, 120, "Slow" );
	v_gtext( h, 144, 130, "Quick" );
	v_gtext( h, 144, 140, "Fast" );

	for( j = 0;  j != '1'  &&  j != '2'  &&  j != '3'; )
	    {
	      j = (int)Bconin( 2 );
	}

	difficulty = j-48;

	draw_in_black = FALSE;
	erase_a_flag();
	vswr_mode( h, 1 );
}


show_instructions()
{
int index, line;

	rgb[0] = 0;  rgb[1] = 750;  rgb[2] = 0;
	vs_color( h, 10, rgb );

	rgb[0] = 1000;  rgb[1]= 0;
	vs_color( h, 11, rgb );

	draw_in_black = TRUE;

	for( index=j = 0;  j < 5;  ++j )
	    {
	      vst_color( h, 11 );

	      erase_a_flag();

	      y = y_start[j];

	      for( line = 0;  line < lines_to_show[j];  ++line, y += 12 )
		  {
		    x = 144-(10*(strlen( &instructions[index][0] )/2));

		    v_gtext( h, x, y, &instructions[index++][0] );
	      }

	      vst_color( h, 10 );
	      vst_height( h, 6, &gr_1, &gr_2, &gr_3, &gr_4 );
	      v_gtext( h, 45, 180, press_to_continue );
	      vst_height( h, 8, &gr_1, &gr_2, &gr_3, &gr_4 );
	      Cnecin();
	}

	draw_in_black = FALSE;
}


show_flag()
{
static	int flag_outline[] = { 25,42,26,42, 25,43,26,43, 26,44,27,44,
			       26,45,267,45, 267,45,267,186, 267,186,27,186,
			       27,186,27,45 };

	vs_color( h, 0, bkgrnd );

	vs_color( h, 1, black );

	rgb[0]=rgb[1]=rgb[2] = 1000;
	vs_color( h, 4, rgb );

		/*---- Draw vertical part of pole ----*/

	pxy[0] = 12;  pxy[1] = 199;  pxy[2] = 12;  pxy[3] = 42;
	v_pline( h, 2, pxy );

	pxy[0] = 13;  pxy[1] = 199;  pxy[2] = 13;  pxy[3] = 42;
	v_pline( h, 2, pxy );

	pxy[0] = 22;  pxy[1] = 199;  pxy[2] = 22;  pxy[3] = 42;
	v_pline( h, 2, pxy );

	pxy[0] = 23;  pxy[1] = 199;  pxy[2] = 23;  pxy[3] = 42;
	v_pline( h, 2, pxy );

		/*------------------------------------*/


		/*---- Draw upper part of pole ----*/

	pxy[0] = 9;  pxy[1] = 41;  pxy[2] = 26;  pxy[3] = 41;
	v_pline( h, 2, pxy );

	pxy[0] = 9;  pxy[1] = 40;  pxy[2] = 26;  pxy[3] = 40;
	v_pline( h, 2, pxy );

	pxy[0] = 9;  pxy[1] = 39;  pxy[2] = 9;  pxy[3] = 37;
	v_pline( h, 2, pxy );

	pxy[0] = 10;  pxy[1] = 39;  pxy[2] = 10;  pxy[3] = 37;
	v_pline( h, 2, pxy );

	pxy[0] = 25;  pxy[1] = 39;  pxy[2] = 25;  pxy[3] = 37;
	v_pline( h, 2, pxy );

	pxy[0] = 26;  pxy[1] = 39;  pxy[2] = 26;  pxy[3] = 37;
	v_pline( h, 2, pxy );

	pxy[0] = 9;  pxy[1] = 36;  pxy[2] = 26;  pxy[3] = 36;
	v_pline( h, 2, pxy );

	pxy[0] = 9;  pxy[1] = 35;  pxy[2] = 26;  pxy[3] = 35;
	v_pline( h, 2, pxy );

		/*---------------------------------*/


		/*---- Draw flag outline ----*/

	v_pline( h, 14, flag_outline );

	pxy[0] = 27;  pxy[1] = 186;  pxy[2] = 27;  pxy[3] = 199;
	v_pline( h, 2, pxy );

		/*---------------------------*/


		/*---- Draw gold ball on top of the pole ----*/

	rgb[0] = 875;  rgb[1] = 500;  rgb[2] = 0;
	vs_color( h, 2, rgb );
	vsl_color( h, 2 );

	pxy[0] = 15;  pxy[1] = 34;  pxy[2] = 20;  pxy[3] = 34;
	v_pline( h, 2, pxy );

	pxy[0] = 14;  pxy[1] = 33;  pxy[2] = 21;  pxy[3] = 33;
	v_pline( h, 2, pxy );

	pxy[0] = 14;  pxy[1] = 32;  pxy[2] = 21;  pxy[3] = 32;
	v_pline( h, 2, pxy );

	pxy[0] = 14;  pxy[1] = 31;  pxy[2] = 21;  pxy[3] = 31;
	v_pline( h, 2, pxy );

	pxy[0] = 14;  pxy[1] = 30;  pxy[2] = 21;  pxy[3] = 30;
	v_pline( h, 2, pxy );

	pxy[0] = 15;  pxy[1] = 29;  pxy[2] = 20;  pxy[3] = 29;
	v_pline( h, 2, pxy );

	pxy[0] = 16;  pxy[1] = 28;  pxy[2] = 19;  pxy[3] = 28;
	v_pline( h, 2, pxy );

		/*--------------------------*/


		/*---- Fill the pole with silver ----*/

	rgb[0]=rgb[1]=rgb[2] = 875;
	vs_color( h, 3, rgb );
	vsf_color( h, 3 );

	v_contourfill( h, 14, 199, -1 );
	v_contourfill( h, 14,  39, -1 );
}


draw_a_flag()
{
	while( *dp != DONE )
	      {
		switch( *dp )
		       {
			 case    NEW:
			 case   MORE:
				      {
					do_lines();
					break;
			 }

			 case   FILL:
			 case  MFILL:
				      {
					do_fill();
					break;
			 }

			 case CIRCLE:
				      {
					do_circle();
					break;
			 }

			 case    ARC:
				      {
					do_arc();
					break;
			 }


			 case ELLARC:
				      {
					do_ellarc();
					break;
			 }
		}

	        ++dp;
	}
}


do_lines()
{
register int j, draw_color, pairs;
int temp_data[100];

	if( *dp == NEW )
	   {
	     draw_color = *++dp;

	     rgb[0] = *++dp;
	     rgb[1] = *++dp;
	     rgb[2] = *++dp;

	     vs_color( h, draw_color, rgb );
	     vsl_color( h, draw_color );
	}

	pairs = *++dp;

	for( j = 0;  j < pairs * 2;  temp_data[j++] = *++dp ) ;

	v_pline( h, pairs, temp_data );
}


do_fill()
{
register int reg, x_start, y_start;

	if( *dp == FILL )
	   {
	     reg = *++dp;

	     rgb[0] = *++dp;
	     rgb[1] = *++dp;
	     rgb[2] = *++dp;

	     vs_color( h, reg, rgb );
	     vsf_color( h, reg );
	}

	x_start = *++dp;
	y_start = *++dp;

	v_contourfill( h, x_start, y_start, -1 );
}


do_circle()
{
register int reg, x_center, y_center, radius;

	reg = *++dp;

	rgb[0] = *++dp;
	rgb[1] = *++dp;
	rgb[2] = *++dp;
	vs_color( h, reg, rgb );
	vsf_color( h, reg );

	x_center = *++dp;
	y_center = *++dp;
	radius = *++dp;

	v_circle( h, x_center, y_center, radius );
}


do_arc()
{
register int x_center, y_center, begang, endang, radius;
int reg;

	reg = *++dp;

	rgb[0] = *++dp;
	rgb[1] = *++dp;
	rgb[2] = *++dp;
	vs_color( h, reg, rgb );
	vsl_color( h, reg );

	x_center = *++dp;
	y_center = *++dp;
	begang = *++dp;
	endang = *++dp;
	radius = *++dp;

	v_arc( h, x_center, y_center, begang, endang, radius );
}


do_ellarc()
{
register int x_center, y_center, x_radius, y_radius, begang;
int endang, reg;

	reg = *++dp;

	rgb[0] = *++dp;
	rgb[1] = *++dp;
	rgb[2] = *++dp;
	vs_color( h, reg, rgb );
	vsl_color( h, reg );

	x_center = *++dp;
	y_center = *++dp;
	x_radius = *++dp;
	y_radius = *++dp;
	begang = *++dp;
	endang = *++dp;

	v_ellarc( h, x_center, y_center, x_radius, y_radius, begang, endang );
}


erase_a_flag()
{
	if( draw_in_black == TRUE )
	   {
	     vs_color( h, 8, black );
	     vsl_color( h, 8 );
	}
	else {
	       vsl_color( h, 4 );
	}

	switch( (int)(Random() & 3) )
	       {
		 case 0:
			 {
			   out_box();
			   break;
		 }

		 case 1:
			 {
			   explode();
			   break;
		 }

		 case 2:
			 {
			   implode();
			   break;
		 }

		 case 3:
			 {
			   in_box();
		 }

	}
}


implode()
{
int xy_l[4], xy_r[4];

	xy_l[0] = 1+XOFFSET;  xy_l[1] = 1+YOFFSET;
	xy_l[2] = xy_l[0];    xy_l[3] = 140+YOFFSET;

	xy_r[0] = 239+XOFFSET;  xy_r[1] = 1+YOFFSET;
	xy_r[2] = xy_r[0];      xy_r[3] = 140+YOFFSET;

	while( xy_l[0] < (121+XOFFSET) )
	      {
		v_pline( h, 2, xy_l );
		v_pline( h, 2, xy_r );

		++xy_l[0];  ++xy_l[2];
		--xy_r[0];  --xy_r[2];
	}
}


explode()
{
int xy_l[4], xy_r[4];

	xy_l[0] = 120+XOFFSET;
	xy_l[1] = 1+YOFFSET;
	xy_l[2] = xy_l[0];
	xy_l[3] = 140+YOFFSET;

	xy_r[0] = xy_l[0];
	xy_r[1] = xy_l[1];
	xy_r[2] = xy_l[2];
	xy_r[3] = xy_l[3];

	while( xy_l[0] > XOFFSET )
	      {
		v_pline( h, 2, xy_l );
		v_pline( h, 2, xy_r );

		--xy_l[0];  --xy_l[2];
		++xy_r[0];  ++xy_r[2];
	}
}


out_box()
{
int square[12];

	pxy[0] = 70+XOFFSET;
	pxy[1] = 70+YOFFSET;
	pxy[2] = pxy[0];
	pxy[3] = pxy[1]+1;

	for( ;  pxy[0] < (171+XOFFSET);  ++pxy[0], ++pxy[2] )
	    {
	      v_pline( h, 2, pxy );
	}

	square[0] = 69+XOFFSET;
	square[1] = pxy[1]-1;

	square[2] = pxy[2];
	square[3] = square[1];

	square[4] = square[2];
	square[5] = pxy[3]+1;

	square[6] = square[0];
	square[7] = square[5];

	square[8] = square[0];
	square[9] = square[1];

	while( (square[0] > XOFFSET) && (square[1] > YOFFSET) )
	      {
		v_pline( h, 5, square );

		--square[0];
		--square[1];

		++square[2];
		--square[3];

		++square[4];
		++square[5];

		--square[6];
		++square[7];

		--square[8];
		--square[9];

		delay( 2500L );
	}
}


in_box()
{
int square[12];

	square[0] = 1+XOFFSET;
	square[1] = 1+YOFFSET;

	square[2] = 239+XOFFSET;
	square[3] = 1+YOFFSET;

	square[4] = square[2];
	square[5] = 140+YOFFSET;

	square[6] = square[0];
	square[7] = square[5];

	square[8] = square[0];
	square[9] = square[1];

	while( square[1] < (71+YOFFSET) )
	      {
	        v_pline( h, 5, square );

	        ++square[0];
	        ++square[1];

	        --square[2];
	        ++square[3];

	        --square[4];
	        --square[5];

	        ++square[6];
	        --square[7];

	        ++square[8];
	        ++square[9];

	        delay( 2500L );
	}
}


go_for_awhile()
{
	while( ++rounds < 21 )
	      {
		while( Bconstat( 2 ) )		/* Flush keyboard buffer */
		      {
			Bconin( 2 );
		}

		select_a_new_flag();

		vs_color( h, 0, black );

		Setscreen( -1L, secondary_screen, -1 );

		draw_a_flag();

		show_countries();

		vst_height( h, 8, &gr_1, &gr_2, &gr_3, &gr_4 );
		v_gtext( h, 30, 41, "                           " );

		vs_color( h, 0, bkgrnd );

		Setscreen( -1L, main_screen, -1 );

		countdown();

		erase_a_flag();
	}

	draw_in_black = TRUE;
	erase_a_flag();
	draw_in_black = FALSE;

	vswr_mode( h, 2 );
	v_gtext( h, 104, 80, "Game Over" );

	if( number_of_players == 2 )
	   {
	     if( score_1 > score_2 )
		{
		  v_gtext( h, 72, 100, "Player one wins" );
	     }
	     else if( score_1 < score_2 )
		     {
		       v_gtext( h, 72, 100, "Player two wins" );
		  }
		  else {
			 v_gtext( h, 94, 100, "A tie game!" );
		  }
	}

	v_gtext( h, 94, 120, "Play Again?" );
	v_gtext( h, 84, 130, "Press Y or N" );
	vswr_mode( h, 1 );

	for( j = 0;  j != 'y'  &&  j != 'Y'  &&  j != 'n'  &&  j != 'N'; )
	    {
	      j = (int)Bconin( 2 );
	}

	if( j == 'y'  ||  j == 'Y' )
	   {
	     game_over = FALSE;
	}
	else {
	       game_over = TRUE;
	}

	erase_a_flag();
}


select_a_new_flag()
{
	do
	  {
	    while( (cindex = (int)(Random() & 127)) > 99 ) ;
	}
	while( flags_shown[cindex] );

	flags_shown[cindex] = 1;

	dp = da[cindex];
}


countdown()
{
char time_string[5], round_string[5];
int quess, j;

	j=did_quess = 0;
	time = speed[difficulty];

	rgb[0]=rgb[1] = 0;  rgb[2] = 750;
	vs_color( h, 12, rgb );
	rgb[0] = 750;  rgb[2] = 375;
	vs_color( h, 11, rgb );

	vst_color( h, 15 );
	vst_effects( h, 32 );
	v_gtext( h, 40, 199, "TIME" );
	v_gtext( h, 160, 199, "ROUND" );

	vst_effects( h, 0 );
	v_gtext( h, 87, 199, "=" );
	v_gtext( h, 217, 199, "=" );

	vst_color( h, 4 );

	v_gtext( h, 230, 199, "  " );
	sprintf( round_string, "%d", rounds );
	v_gtext( h, 230, 199, round_string );

	while( --time )
	      {
		if( time < 100 )
		   {
		     vst_color( h, 11 );
		}

		sprintf( time_string, "%3ld", time );
		v_gtext( h, 100, 199, time_string );

		if( (quess = poll_players()) > 0 )
		   {
		     if( quess < 5 )
			{
			  j = check_quess( PLAYER_ONE, quess );
		     }
		     else if( number_of_players == 2 )
			     {
			       j = check_quess( PLAYER_TWO, quess-6 );
			  }

		     if( j )
		        {
		          return;
		     }
		}
	}

	v_gtext( h, 123, 199, "0" );

	vst_color( h, 15 );
	vst_effects( h, 4 );
	v_gtext( h, 30, 41, "                      " );
	v_gtext( h, 110, 41, "Time's Up!!" );
	vst_effects( h, 0 );

	for( j = 20;  j > 0;  --j )
	    {
	      rgb[0] = (int)(Random() & 1023);
	      vs_color( h, 11, rgb );
	      delay( 38000L );
	}

	rgb[0] = 1000;
	vs_color( h, 11, rgb );

	show_score();
}


poll_players()
{
	if( Bconstat( 2 ) == 0 )
	   {
	     return( 0 );
	}

	switch( (int)(Bconin( 2 ) >> 16) )
	       {
		 case 59:  return( 1 );
		 case 60:  return( 2 );
		 case 61:  return( 3 );
		 case 62:  return( 4 );

		 case 65:  return( 7 );
		 case 66:  return( 8 );
		 case 67:  return( 9 );
		 case 68:  return( 10 );

		 default:  return( 0 );
	}
}


check_quess( player, quess )
int player, quess;
{
	if( player & did_quess )
	   {
	     return( 0 );
	}

	if( quess == place )
	   {
	     they_got_it( player );
	     return( 1 );
	}

	did_quess |= player;

	if( did_quess == 3 )
	   {
	     vswr_mode( h, 2 );
	     v_gtext( h, 42, 41, "Both of you are WRONG!" );
	     vswr_mode( h, 1 );
	     delay( 300000L );
	     show_score();
	     return( 1 );
	}

	if( number_of_players == 1 )
	   {
	     vswr_mode( h, 2 );
	     v_gtext( h, 42, 41, "Sorry, you are WRONG!" );
	     vswr_mode( h, 1 );
	     delay( 300000L );
	     show_score();
	     return( 1 );
	}

	return( 0 );
}


they_got_it( player )
int player;
{
	if( player == PLAYER_ONE )
	   {
	     if( number_of_players == 1 )
		{
		  v_gtext( h, 70, 41, "You are correct!" );
	     }
	     else {
		    v_gtext( h, 42, 41, "Player one is right!" );
	     }

	     delay( 300000L );
	     score_1 += 1000 + (difficulty * time);
	}
	else {
	       v_gtext( h, 42, 41, "Player two is right!" );
	       delay( 300000L );
	       score_2 += 1000 + (difficulty * time);
	}

	for( j = 30;  j > 0;  --j )
	    {
	      rgb[0] = (int)(Random() & 1023);
	      rgb[1] = (int)(Random() & 1023);
	      rgb[2] = (int)(Random() & 1023);
	      vs_color( h, 13, rgb );

	      delay( 20000L );
	}

	rgb[0]=rgb[1]=rgb[2] = 1000;
	vs_color( h, 13, rgb );
	show_score();
	delay( 300000L );
}


show_score()
{
	v_gtext( h, 30, 41, "                       " );

	vst_color( h, 15 );
	v_gtext( h, 30, 41, "1P =" );

	vst_color( h, 4 );
	sprintf( scr_1_string, "%ld", score_1 );
	v_gtext( h, 80, 41, scr_1_string );

	if( number_of_players == 2 )
	   {
	     vst_color( h, 15 );
	     v_gtext( h, 150, 41, "2P =" );
	     sprintf( scr_2_string, "%ld", score_2 );
	     vst_color( h, 4 );
	     v_gtext( h, 200, 41, scr_2_string );
	}

	delay( 40000L );
}


show_countries()
{
		/*---- Draw text grid ----*/

	rgb[0] = 0;  rgb[1] = 625;  rgb[2] = 625;
	vs_color( h, 15, rgb );
	vsl_color( h, 15 );
	vsl_udsty( h, 21845 );		/* every other bit is on */
	vsl_type( h, 7 );

	pxy[0] = 0;  pxy[1] = 0;  pxy[2] = 319;  pxy[3] = 0;
	v_pline( h, 2, pxy );

	pxy[0] = 0;  pxy[1] = 11;  pxy[2] = 319;  pxy[3] = 11;
	v_pline( h, 2, pxy );

	pxy[0] = 0;  pxy[1] = 23;  pxy[2] = 319;  pxy[3] = 23;
	v_pline( h, 2, pxy );

	pxy[0] = 0;  pxy[1] = 0;  pxy[2] = 0;  pxy[3] = 23;
	v_pline( h, 2, pxy );

	pxy[0] = 160;  pxy[1] = 0;  pxy[2] = 160;  pxy[3] = 23;
	v_pline( h, 2, pxy );

	pxy[0] = 319;  pxy[1] = 0;  pxy[2] = 319;  pxy[3] = 23;
	v_pline( h, 2, pxy );

		/*--------------------------------*/


		/*---- Erase text areas ----*/

	vsl_color( h, 1 );
	vsl_type( h, 1 );

	pxy[0] = 159;  pxy[1] = 1;  pxy[2] = 159;  pxy[3] = 10;

	while( pxy[0] > 0 )            /* Area one */
	      {
		v_pline( h, 2, pxy );
		--pxy[0];
		--pxy[2];
	}

	pxy[0] = 161;  pxy[1] = 1;  pxy[2] = 161;  pxy[3] = 10;

	while( pxy[0] < 319 )          /* Area two */
	      {
		v_pline( h, 2, pxy );
		++pxy[0];
		++pxy[2];
	}

	pxy[0] = 159;  pxy[1] = 12;  pxy[2] = 159;  pxy[3] = 22;

	while( pxy[0] > 0 )            /* Area three */
	      {
		v_pline( h, 2, pxy );
		--pxy[0];
		--pxy[2];
	}

	pxy[0] = 161;  pxy[1] = 12;  pxy[2] = 161;  pxy[3] = 22;

	while( pxy[0] < 319 )          /* Area four */
	      {
		v_pline( h, 2, pxy );
		++pxy[0];
		++pxy[2];
	}

		/*-------------------------------------*/

	print_countries();
}


print_countries()
{
static int x_table[5][18] = {

	{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },

	{ 0,0,0,0,72,64,56,48,40,32,24,16,8,8,8,8,8 },

	{ 0,0,0,0,232,224,216,208,200,192,184,176,168,168,168,168,168 },

	{ 0,0,0,0,72,64,56,48,40,32,24,16,8,8,8,8,8 },

	{ 0,0,0,0,232,224,216,208,200,192,184,176,168,168,168,168,168 } },

	y_table[] = { 0,8,8,20,20 };

char	str_array[5][18];

int	abcd[5], e, len;

	for( e = 1;  e < 5;  abcd[e++] = -1 );

	place = (int)(Random() & 3 );  ++place;

	strcpy( &str_array[place][0], na[cindex] );

	abcd[place] = cindex;

	for( j = 1;  j < 5;  ++j )
	    {
	      if( abcd[j] < 0 )
		 {
		   do
		     {
		       while( (ctry = (int)(Random() & 127)) > 99 ) ;

		       for( e = 0, x = 1;  x < 5;  ++x )
			   {
			     if( abcd[x] == ctry )
		       		{
				  e = 1;
			     }
		       }

			/*---- Indonesia & Monaco are identical ----*/
		       if( (cindex == 42  &&  ctry == 64) ||
			   (cindex == 64  &&  ctry == 42) )
			  {
			    e = 1;
		       }
		   }
		   while( e );

		   abcd[j] = ctry;

		   strcpy( &str_array[j][0], na[ctry] );
	      }
	}

	rgb[0]=rgb[1] = 1000;  rgb[2] = 0;	/* Text color */
	vs_color( h, 5, rgb );
	vs_color( h, 13, rgb );
	vst_effects( h, 0 );
	vst_height( h, 6, &gr_1, &gr_2, &gr_3, &gr_4 );
	vswr_mode( h, 2 );

	for( j = 1;  j < 5;  ++j )
	    {
	      if( j == place )
		 {
		   vst_color( h, 13 );
	      }
	      else {
		     vst_color( h, 5 );
	      }

	      len = strlen( &str_array[j][0] );

	      v_gtext( h, x_table[j][len], y_table[j], &str_array[j][0] );
	}

	vswr_mode( h, 1 );
}


delay( t )
long t;
{
	while( --t ) ;
}


		/*--------------  end of FLAGTRIV.C  --------------*/
