/*
*	Yamana's Otomeza Plug-in Tool
*		熊手ペン
*	
*	1995.07.10	乙女座対応版
*	1995.07.23	画面外への対処、マウス範囲の表示等
*	1995.08.03	マウスを乗っ取らないようにした。高速化。
*	1995.08.07	マスク部分の動作を設定できるようにした
*	
*/
#include	"otome_pi.h"

const char longname[] = "PEN   : 熊手ペン";
int			cnfg_max = 3;
PI_CNFG		cnfg[] = {
				{"熊手の数"			, 1, 16,  4,  4 },
				{"熊手の大きさ"		, 2, 32, 12, 12 },
				{"マスク→散らない"	, 0,  1,  0,  0 },
			};

#define	USE_ENV		PI_SET_ENV
#define	USE_TYPE	PI_DRAW_DOT
#define	USE_PEN		PI_USER_PEN

#include	"otome_pi.c"

/*******************************************************/

int Bit[] = { 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01 };

/* 描画可能のとき !0 を返す */
int 	getMaskBit( _x, _y)
int 	_x,_y;
{
	return (pi_imge->edit[ (_x>>3) + _y*(pi_imge->size.x>>3) ]
					& Bit[ _x & 7] );
}


void	chgFunc16( x1,y1,x2,y2 ,mode)
int 	x1,y1,x2,y2,mode;
{
	int col1 = (pi_imge->image[ (x1>>1) + y1*(pi_imge->size.x>>1) ]
					>>((x1 & 1)? 4:0))& 0x0f ;
	int col2 = (pi_imge->image[ (x2>>1) + y2*(pi_imge->size.x>>1) ]
					>>((x2 & 1)? 4:0))& 0x0f ;
	char	*p;
	
	if( mode == 0 )
	{
		if( getMaskBit(x2,y2) )
		{	p = &( pi_imge->image[ (x2>>1) + y2*(pi_imge->size.x>>1)] );
			
			if( x2 & 1 )	*p = (*p & 0x0f) | ((col1<<4)& 0xf0);
					else	*p = (*p & 0xf0) | ((col1   )& 0x0f);
		}
		if( getMaskBit(x1,y1) )
		{	p = &( pi_imge->image[ (x1>>1) + y1*(pi_imge->size.x>>1)] );
		
			if( x1 & 1 )	*p = (*p & 0x0f) | ((col2<<4)& 0xf0);
					else	*p = (*p & 0xf0) | ((col2   )& 0x0f);
		}
	}
	else	/* 共にマスク外 */
	{	if( getMaskBit(x1,y1) && getMaskBit(x2,y2) )
		{	p = &( pi_imge->image[ (x2>>1) + y2*(pi_imge->size.x>>1)] );
			
			if( x2 & 1 )	*p = (*p & 0x0f) | ((col1<<4)& 0xf0);
					else	*p = (*p & 0xf0) | ((col1   )& 0x0f);
			
			p = &( pi_imge->image[ (x1>>1) + y1*(pi_imge->size.x>>1)] );
			
			if( x1 & 1 )	*p = (*p & 0x0f) | ((col2<<4)& 0xf0);
					else	*p = (*p & 0xf0) | ((col2   )& 0x0f);
		}
	}
}

void	chgFunc256( x1,y1,x2,y2 ,mode)
int 	x1,y1,x2,y2,mode;
{
	int 	col1 = (pi_imge->image[ x1 + y1*(pi_imge->size.x) ])& 0xff ;
	int 	col2 = (pi_imge->image[ x2 + y2*(pi_imge->size.x) ])& 0xff ;
	
	if( mode==0 )
	{	
		if( getMaskBit(x2,y2) )
			pi_imge->image[ x2 + y2*(pi_imge->size.x) ] = col1;
		if( getMaskBit(x1,y1) )
			pi_imge->image[ x1 + y1*(pi_imge->size.x) ] = col2;
	}
	else
	{	if( getMaskBit(x1,y1) && getMaskBit(x2,y2) )
		{	pi_imge->image[ x2 + y2*(pi_imge->size.x) ] = col1;
			pi_imge->image[ x1 + y1*(pi_imge->size.x) ] = col2;
		}
	}
}

void	chgFunc32K( x1,y1,x2,y2 ,mode)
int 	x1,y1,x2,y2,mode;
{
	char	*p1 = &( pi_imge->image[(x1<<1) + y1*(pi_imge->size.x<<1)] );
	char	*p2 = &( pi_imge->image[(x2<<1) + y2*(pi_imge->size.x<<1)] );
	char	tmp[2];
	
	tmp[0] = *p2,	tmp[1] = *(p2+1);
	
	if( mode==0 )
	{	if( getMaskBit(x2,y2) )
			*p2 = *p1,		*(p2+1) = *(p1+1);
		if( getMaskBit(x1,y1) )
			*p1 = tmp[0],	*(p1+1) = tmp[1];
	}else
	{	if( getMaskBit(x1,y1) && getMaskBit(x2,y2) )
		{	*p2 = *p1,		*(p2+1) = *(p1+1);
			*p1 = tmp[0],	*(p1+1) = tmp[1];
		}
	}
}

#if 0
void	chg_point( x1,y1,x2,y2)
int 	x1,y1,x2,y2;
{
	int 	col;
	PSET	pset;
	pset.n = 1;
	
	EGB_point( EgbPtr, 0, x1,y1, &col );
	EGB_color( EgbPtr, 0, col );
	EGB_point( EgbPtr, 0, x2,y2, &col );
	pset.x = x2;
	pset.y = y2;
	EGB_pset( EgbPtr, &pset );
	
	EGB_color( EgbPtr, 0, col );
	pset.x = x1;
	pset.y = y1;
	EGB_pset( EgbPtr, &pset );
	
}
#endif

/*******************************************************/

int APL_exec()
{
	int 	i;
	int		val,size,mode;
	int 	x,y,x1,y1,x2,y2,xs,ys,dx,dy;
	void 	(*chgFunc)();
	
	val  = cnfg[0].val * 2;
	size = cnfg[1].val;
	mode = cnfg[2].val;
	
	if( pi_imge->pix  < 8 )	chgFunc = chgFunc16;
	if( pi_imge->pix == 8 )	chgFunc = chgFunc256;
	if( pi_imge->pix  > 8 )	chgFunc = chgFunc32K;
	
	{
		xs= ys = size;
		x = WORD( g_para+2 ) - size/2 ;
		y = WORD( g_para+4 ) - size/2 ;
		
		/* 画面外にならないようチェック */
		if( x < 0 )
			dx=(-x),x+=dx,xs-=dx;
		else if( (dx=( x+size - pi_imge->size.x )) > 0 )
			xs -= dx;
		if( y < 0 )
			dy=(-y),y+=dy,ys-=dy;
		else if( (dy=( y+size - pi_imge->size.y )) > 0 )
			ys -= dy;
		
		srand( (unsigned int)clock() );
		for( i=0; i<val ; i++ )
		{
			x1 = x + (rand() % xs+1);
			y1 = y + (rand() % ys+1);
			x2 = x + (rand() % xs  );
			y2 = y + (rand() % ys  );
			
//			chg_point( x1,y1,x2,y2 );
			chgFunc( x1,y1,x2,y2, mode );
		}
		
	}
	
	ret_fr->lupx = x;
	ret_fr->lupy = y;
	ret_fr->rdwx = x+size-1;
	ret_fr->rdwy = y+size-1;
	
	return NOERR;
}

