#define DEBUG 0
/* 
	TOWNS囲碁棋譜記録プログラム
	                                      1991/12/11  久保田俊也

	91/12/11  display モジュールを本体より分離 


*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <egb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include "igo.h"
#include "banx.h"
#include "kiffile.h"
#include "title.h"

#define BANX 180
#define BANY 30
#define ISHI_R 11
#define BANX2 BANX + 20*ISHI_R*2
#define BANY2 BANY + 20*ISHI_R*2

static int banx1, bany1, banx2, bany2, ishi_r, ishi_2r, x_len;
static int type, bansize;
static char save_ban[MAX_BANSIZE2];
extern char *guiEgbPtr;
char *gwork;	/* グラフイックBIOSワーク */
char para[64] ;
int ban_color, line_color;


int disp_init()
{
BAN_TYPE ban_type;

	ban_type = *title_bantype_read();
	if( ban_type.type == NORMAL){
		type = NORMAL;
		bansize = ban_type.size;
	}

	banx1 = BANX;
	bany1 = BANY;
	ishi_r = ISHI_R;
	ishi_2r = ishi_r * 2;
	x_len = ISHI_R - 2;
	banx2 = banx1 + (bansize+1)*ishi_2r;
	bany2 = bany1 + (bansize+1)*ishi_2r;

	ban_color = 6;	/* 黄色 */
	line_color = 1; /* 青色 */
	
	gwork = guiEgbPtr;

	EGB_writePage( gwork, 0) ;	/* ページ 0	 に */

	disp_goban();
	banx_init( save_ban);

	EGB_writePage( gwork, 0 ) ;	/* ページ 0 に */
	EGB_setPalette( gwork, BLACK_COL, 0, 0, 0 );      /* [04]  */	

	return 0;
}

static int disp_goban()
{
int i, j ;	
int ichi;

	MG_mosDisp( 2 ) ;		/* マウスを表示	ｼﾅｲ */
	
	EGB_color( gwork, 0, 5) ;
	EGB_color( gwork, 2, 5) ;
	EGB_paintMode( gwork, 0x022 ); 
	EGB_box( gwork, BANX, BANY, BANX2, BANY2); 	/* [46] 四角を描く */

	EGB_color( gwork, 0, ban_color) ;	/* 色を黄(6)にして */
	EGB_color( gwork, 2, ban_color) ;	/* 色を黄(6)にして */
	EGB_paintMode( gwork, 0x022 ); 
	EGB_box( gwork, banx1, bany1, banx2, bany2); 	/* [46] 四角を描く */

	EGB_color( gwork, 0, line_color) ;
	EGB_color( gwork, 2, line_color) ;
	for ( i = 0; i < bansize; i++){
		EGB_singleLine( gwork, (i+1)*ishi_2r+banx1, bany1+ishi_2r, 
							   (i+1)*ishi_2r+banx1, bany2-ishi_2r) ; 
		EGB_singleLine( gwork, banx1+ishi_2r, bany1+(i+1)*ishi_2r,
							   banx2-ishi_2r, bany1+(i+1)*ishi_2r) ; 
	}

	if( type==NORMAL && bansize == 19){
		for ( i = 0; i < 3; i++){
			for ( j = 0; j < 3; j++){
				WORD(para + 0) = 6*ishi_2r*i+banx1+4*ishi_2r;
				WORD(para + 2) = 6*ishi_2r*j+bany1+4*ishi_2r;
				WORD(para + 4) = 2;
				EGB_circle( gwork, para ) ;  /* その位置に点を描く */
			}
		}
	}

/*  盤の回転等を表すために黒丸白丸を表示する */
	ichi = henkan_disp_in( 1, 1);
	EGB_color( gwork, 0, BLACK_COL ) ; 
	EGB_color( gwork, 2, BLACK_COL);
	
	if((ichi % (bansize+1)) == 1){
		WORD(para + 0) = banx1+5;
	}else{
		WORD(para + 0) = banx2-5;
	}
	
	if((ichi / (bansize+1)) == 1){
		WORD(para + 2) = bany1+5;
	}else{
		WORD(para + 2) = bany2-5;
	}
	
	WORD(para + 4) = 3;
	EGB_circle( gwork, para ) ;  /* その位置に点を描く */

	ichi = henkan_disp_in( bansize, 1);
	EGB_color( gwork, 0, WHITE_COL ) ; 
	EGB_color( gwork, 2, WHITE_COL);
	
	if((ichi % (bansize+1)) == 1){
		WORD(para + 0) = banx1+5;
	}else{
		WORD(para + 0) = banx2-5;
	}
	
	if((ichi / (bansize+1)) == 1){
		WORD(para + 2) = bany1+5;
	}else{
		WORD(para + 2) = bany2-5;
	}
	
	WORD(para + 4) = 3;
	EGB_circle( gwork, para ) ;  /* その位置に点を描く */

 	MG_mosDisp( 3 ) ;		/* マウスを表示する */

	return 0;
}

int disp_te(char ban[],int ex_ban[])
{
char s[4];
int stonex, stoney;
int ichi;
static int save_ex_ban[MAX_BANSIZE2];

	MG_mosDisp( 2 ) ;		/* マウスを表示	ｼﾅｲ */
	EGB_paintMode( gwork, 0x022 );  /*  設定しないといけない? */

	for(stoney=1;stoney<(bansize+1);stoney++){
		for(stonex=1;stonex<(bansize+1);stonex++){
			ichi = henkan_disp_out( stonex, stoney);
		    if( ban[ichi] != save_ban[stonex + stoney*(bansize+1)] ||
		   	 ex_ban[ichi] != save_ex_ban[stonex + stoney*(bansize+1)]){

				switch(ban[ichi]){
				case BLACK:
					EGB_color( gwork, 0, 1); 
					EGB_color( gwork, 2, BLACK_COL);
					gcircle((stonex)*ishi_2r+banx1, (stoney)*ishi_2r+bany1, 
							ishi_r);
					break; 
				case WHITE:
					EGB_color( gwork, 0, 1 ) ; 
					EGB_color( gwork, 2, WHITE_COL);
					gcircle((stonex)*ishi_2r+banx1, (stoney)*ishi_2r+bany1, 
							ishi_r);
					break; 
				case BLANK:
					disp_te_clear(stonex, stoney);
					break;
				case BLACK_CAPTURE:
					disp_te_clear(stonex, stoney);
					EGB_color( gwork, 0, 1); 
					EGB_color( gwork, 2, BLACK_COL);
					gcircle((stonex)*ishi_2r+banx1, (stoney)*ishi_2r+bany1, 
							ishi_r);
					EGB_color( gwork, 0, WHITE_COL); 
					EGB_color( gwork, 2, WHITE_COL);
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1) ; 
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1) ; 
					break; 
				case WHITE_CAPTURE:
					disp_te_clear(stonex, stoney);
					EGB_color( gwork, 0, 1); 
					EGB_color( gwork, 2, WHITE_COL);
					gcircle((stonex)*ishi_2r+banx1, (stoney)*ishi_2r+bany1, 
							ishi_r);
					EGB_color( gwork, 0, BLACK_COL); 
					EGB_color( gwork, 2, BLACK_COL);
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1) ; 
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1) ; 
					break; 
				case WALL_TERRITORY:
					EGB_color( gwork, 0, 1 ) ; 
					EGB_color( gwork, 2, GREY_COL);
					gcircle((stonex)*ishi_2r+banx1, (stoney)*ishi_2r+bany1, 
							ishi_r);
					break; 
				case BLACK_TERRITORY:
					disp_te_clear(stonex, stoney);
					EGB_color( gwork, 0, BLACK_COL); 
					EGB_color( gwork, 2, BLACK_COL);
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1) ; 
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1) ; 
					break;
				case WHITE_TERRITORY:
					disp_te_clear(stonex, stoney);
					EGB_color( gwork, 0, WHITE_COL); 
					EGB_color( gwork, 2, WHITE_COL);
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1) ; 
					EGB_singleLine( gwork,
									(stonex)*ishi_2r-x_len+banx1,
									(stoney)*ishi_2r+x_len+bany1, 
									(stonex)*ishi_2r+x_len+banx1,
									(stoney)*ishi_2r-x_len+bany1) ; 
					break;
				}
		    
		    	switch(ban[ichi]){
					case BLANK:
						break;
					case BLACK:
						EGB_color( gwork, 0, WHITE_COL) ; 
						EGB_color( gwork, 2, WHITE_COL) ; 
						break;
					case WHITE:
						EGB_color( gwork, 0, BLACK_COL) ; 
						EGB_color( gwork, 2, BLACK_COL) ; 
						break;
				}
				if(ban[ichi] != BLANK){
						if(ex_ban[ichi] >= 100){
							s[0] = '0' + ex_ban[ichi]/100;
							s[1] = '0' + (ex_ban[ichi]%100)/10;
							s[2] = '0' + (ex_ban[ichi]%10);
		    				s[3] = '\0';
		    				gprint(s, (stonex)*ishi_2r+banx1-12,
				    				  (stoney)*ishi_2r+bany1+8);
						}else if(ex_ban[ichi] >= 10){
							s[0] = '0' + ex_ban[ichi]/10;
							s[1] = '0' + (ex_ban[ichi]%10);
		    				s[2] = '\0';
				    		gprint(s, (stonex)*ishi_2r+banx1-8,
				    				  (stoney)*ishi_2r+bany1+8);
						}else if(ex_ban[ichi] >= 1){
							s[0] = '0' + (ex_ban[ichi]%10);
		    				s[1] = '\0';
				    		gprint(s, (stonex)*ishi_2r+banx1-4,
				    				  (stoney)*ishi_2r+bany1+8);
						}
				}
		    }
		
		save_ban[stonex + stoney*(bansize+1)]    = ban[ichi];
		save_ex_ban[stonex + stoney*(bansize+1)] = ex_ban[ichi];
		
		}
	}

	MG_mosDisp( 3 ) ;		/* マウスを表示する */

	return 0;
}

int disp_te_clear( int stonex, int stoney)
{

	EGB_color( gwork, 0, ban_color ) ; 
	EGB_color( gwork, 2, ban_color ) ; 
	EGB_box( gwork, 
			(stonex)*ishi_2r+banx1-ishi_r, 
			(stoney)*ishi_2r+bany1-ishi_r, 
			(stonex)*ishi_2r+banx1+ishi_r, 
			(stoney)*ishi_2r+bany1+ishi_r); 
			/* [46] 四角を描く */

	EGB_color( gwork, 0, line_color ) ; 
	EGB_color( gwork, 2, line_color ) ; 
	if(stonex == 1){
		if(stoney == 1){
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1+ishi_r);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1+ishi_r,
							(stoney)*ishi_2r+bany1);
		}else if(stoney == bansize){
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1-ishi_r, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1+ishi_r,
							(stoney)*ishi_2r+bany1);
		}else{
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1-ishi_r, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1+ishi_r);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1+ishi_r,
							(stoney)*ishi_2r+bany1);
		}
	}else if(stonex == bansize){
		if(stoney == 1){
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1+ishi_r);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1-ishi_r,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1);
		}else if(stoney == bansize){
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1-ishi_r, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1-ishi_r,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1);
		}else{
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1-ishi_r, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1+ishi_r);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1-ishi_r,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1);
		}
	}else{
		if(stoney == 1){
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1+ishi_r);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1-ishi_r,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1+ishi_r,
							(stoney)*ishi_2r+bany1);
		}else if(stoney == bansize){
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1-ishi_r, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1-ishi_r,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1+ishi_r,
							(stoney)*ishi_2r+bany1);
		}else{
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1-ishi_r, 
							(stonex)*ishi_2r+banx1,
							(stoney)*ishi_2r+bany1+ishi_r);
			EGB_singleLine( gwork, (stonex)*ishi_2r+banx1-ishi_r,
							(stoney)*ishi_2r+bany1, 
							(stonex)*ishi_2r+banx1+ishi_r,
							(stoney)*ishi_2r+bany1);
		}
	}
	
	if( type == NORMAL && bansize == 19){
		switch(stonex){
		case 4: case 10: case 16:
			switch(stoney){
			case 4: case 10: case 16:
				WORD(para + 0) = (stonex)*ishi_2r+banx1;
				WORD(para + 2) = (stoney)*ishi_2r+bany1;
				WORD(para + 4) = 2;
				EGB_circle( gwork, para ) ;  /* その位置に点を描く */
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}

	return 0;

}

int disp_chk(int x, int y)
{
int ichi, stonex, stoney;
	
	if(banx1 < x && x < banx2 && bany1 < y && y < bany2){
		/* 入力の時は盤の端を対象から外す */
		if(banx1+ishi_r < x && x < banx2-ishi_r
			&& bany1+ishi_r < y && y < bany2-ishi_r){
			stonex = ((x-(banx1+ishi_r)) / ishi_2r);
			stoney = ((y-(bany1+ishi_r)) / ishi_2r);
			ichi=henkan_disp_out( stonex+1, stoney+1);
			return ichi;
		}else{
			return -2;
		}
	}else{
		return -1;
	}
}

/* p 文字列 x 初期座標 y 初期座標 */
int gprint( char *p, int x, int y)
{
int i;
char dsp[80];

	WORD(dsp+0) = x;
	WORD(dsp+2) = y;
	for(i=0;i<80;i++){
		dsp[i+6] = (char)(BYTE(p+i));
		if(dsp[i+6] == (char)0 ){
			break;
		}
	}
	WORD(dsp+4) = i;
	EGB_writeMode(gwork, 0);
	EGB_sjisString(gwork, dsp);
	return i;
	
}

int gcircle(int x, int y, int r)
{

	WORD(para + 0) = x;
    WORD(para + 2) = y;
	WORD(para + 4) = r;
	EGB_circle( gwork, para ) ;/* その位置に点を描く */

	return 0;
	
}
