/*
*	Yamana's Otomeza Plug-in Tool
*		木を植える
*	
*	1995.07.10  乙女座対応版
*	1995.08.11  スタンプ化。タイルパターンも有効になるようにした
*	1995.08.13  一気に処理速度を 60〜70% 高速化した
*	
*/
#include	"otome_pi.h"
#include	"costbl.h"

const char longname[]  = "STAMP : 木を植える";
int			cnfg_max = 2;
PI_CNFG		cnfg[] =
			{	/* 1234567890123456 ,min,max,def,set */
				{ "木の高さ → 高"	,  2, 15,  6,  6 },
				{ "幹の太さ → 太"	,  2, 12,  6,  6 },
			};

#define	USE_ENV 	PI_SET_ENV
#define	USE_PEN 	PI_USER_PEN

#define 	USE_TYPE	PI_DRAW_STAMP

	/* たぶんこれ以上にはならないはずなんだが？ */
POINT		stamp={ 320*2,480*2 };

#include	"otome_pi.c"


/********************************/

void mk_treeLR(x,y,pen,len,arc)
int 	x,y,pen,len;

#ifdef USE_LIB
	float	arc;
#else
	int 	arc;
#endif

{
	int 	x_L,y_L,x_R,y_R;
	int 	len_L,len_R;
#ifdef USE_LIB
	float	arc_L,arc_R;
#else
	int 	arc_L,arc_R;
#endif
	struct
	{	short	n;
		short	x0,y0;
		short	x1,y1;
		short	x2,y2;
	}tree;
	
	int 	ch,mx,my;
	
	if( pen < 1 )	return;
	MOS_rdpos( &ch, &mx, &my );
	if( (ch & 3)== 0x02 )	return;
	
	srand( (unsigned int)clock() );
	
	/* 元の長さの 3/4 くらい... */
	len_L = (6 + (rand() & 1)) * len / 8 ;
	len_R = (6 + (rand() & 1)) * len / 8 ;
	
#ifdef USE_LIB
	/* 元の枝から曲がる角 */
	arc_L = arc - (double)( ((rand() & 1)+ 1) * _PI / ((rand() & 1)+10) );
	arc_R = arc + (double)( ((rand() & 1)+ 1) * _PI / ((rand() & 1)+10) );
	
	/* 枝の位置 */
	x_L = x + len_L * sin( arc_L );
	y_L = y - len_L * cos( arc_L );
	x_R = x + len_R * sin( arc_R );
	y_R = y - len_R * cos( arc_R );
	
#else
	/* 元の枝から曲がる角 */
	/* だいたい 11°〜34°の範囲にあれば自然に見えるかな？ */
	arc_L = arc - (8 + (rand() & 0x0f));
	arc_R = arc + (8 + (rand() & 0x0f));
	arc_L &= 0xff;
	arc_R &= 0xff;
	
	/* 枝の位置  256倍されているので256で割ること */
	x_L = x + ((len_L * sin256( arc_L )) >>8 );
	y_L = y - ((len_L * cos256( arc_L )) >>8 );
	x_R = x + ((len_R * sin256( arc_R )) >>8 );
	y_R = y - ((len_R * cos256( arc_R )) >>8 );
#endif
	
	tree.n  = 3;
	tree.x0 = x_L,	tree.y0 = y_L;
	tree.x1 = x,	tree.y1 = y  ;
	tree.x2 = x_R,	tree.y2 = y_R;
	
	EGB_penSize( EgbPtr, pen );
	EGB_connect( EgbPtr, &tree );
	
	mk_treeLR( x_L, y_L, pen-1, len_L, arc_L );
	mk_treeLR( x_R, y_R, pen-1, len_R, arc_R );
	
}


void mk_treeFirst(x,y,pen,len)
int 	x,y,pen,len;
{
	int 	ox,oy;
	struct
	{	short int n;
		Rect	fr;
	}connect;
	
	len *= 3;
	ox = x;
	oy = y-len;
	
	EGB_penSize( EgbPtr, pen+2 );
	connect.n = 2;
	SetRect( &connect.fr, x,y, ox,oy );
	EGB_connect( EgbPtr, &connect );
	
	mk_treeLR( ox,oy, pen, len, 0 );
	
}

/********************************/

int APL_exec()
{
	int 	x,y,pen,len;
	
//	clock_t	t;
//	t=clock();
	
	
	/* EGB_pset と同じ形式で値が入る */
	x = WORD(g_para + 2);
	y = WORD(g_para + 4);
	
	len  = cnfg[0].val * 2;		/* 高さ */
	pen  = cnfg[1].val;			/* 太さ */
	
	
	/* 描画ページの指定 */
	EGB_writePage( EgbPtr, pi_imge->page );
	
	/* タイルパターンが選択されていたらそれを有効にする */
	if( pi_data->til_mode )
	{	EGB_tilePattern( EgbPtr, EGB_FORECOL,
					PI_TILE_X, PI_TILE_Y, PI_TILE_PAT );
		EGB_paintMode( EgbPtr, EGB_PAINT_TILE );
	}
	else
	{	EGB_color( EgbPtr, 0,  PI_FORECOL );
		EGB_paintMode( EgbPtr, EGB_PAINT_BETA );
	}
	
	/* ペン形状の指定 */
	EGB_pen  ( EgbPtr, 1 );	/* 四角 */
	
	mk_treeFirst( x,y,pen,len );
	
//	printf("time = %d", clock()-t );
	
	/* スタンプ描画位置を返す */
	ret_fr->lupx = x-stamp.x/2;
	ret_fr->rdwx = x+stamp.x/2;
	ret_fr->lupy = y-stamp.y/2;
	ret_fr->rdwy = y+stamp.y/2;
	
	return NOERR;
}

/**
	速度比較
			長6,太6		長10,太8
	math		39			157
	table		14			 59
	
**/
