/*--------------------------------
   dice ver.0.00     2002.12.29
  (c)won-wan [Tsutsumi Hiroyuki]
--------------------------------*/
#ifdef	__BEOS__
#include	<bw.h>
#endif	__BEOS__

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<sys/bios.h>
#include	<sys/libwwc.h>

#define	schar	signed char
#define	uchar	unsigned char
#define	uint16	unsigned short
#define	uint	unsigned short

#define	KEY_X	0x00f0
#define	KEY_Y	0x0f00

int		main();
void	dice(uchar cnt,uchar tm,uchar far *me);
void	v_init();
void	v_cls();
void	v_spr_prt(int c,int x,int y,int p,int f);
void	v_spr_flush();
void	v_puts(int x,int y,int p,char far *s);
void	v_put(int x,int y,int p,uint16 c);
void	v_dice(int x,int y,uchar f,uchar n);

uint	v_cpaldata[]={	/* 16colors */
	0x000,0xefe,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.0(1) text */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.1 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.2 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.3 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.4 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.5 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.6 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.7 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.8 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.9 */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.a */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.b */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.c */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,/* pal.d */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0xd00,0x000,0x888,0xddd,0xccc,0xbbb,/* pal.e(2-7) white-dice */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
	0x000,0x000,0xf00,0x000,0x888,0xddd,0xeee,0xfff,/* pal.f(2-7) white-dice */
	0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
};

uchar	v_dicepat[32*36]={	/* 16packed */
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*1*/
	0x00,0x00,0x07,0x77,0x00,0x07,0x77,0x72,
	0x07,0x77,0x77,0x22,0x05,0x57,0x77,0x72,
	0x00,0x00,0x07,0x77,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*2*/
	0x00,0x00,0x07,0x77,0x00,0x07,0x77,0x77,
	0x07,0x77,0x37,0x77,0x05,0x57,0x77,0x77,
	0x00,0x00,0x07,0x77,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*3*/
	0x00,0x00,0x07,0x77,0x00,0x07,0x77,0x77,
	0x07,0x73,0x77,0x73,0x05,0x57,0x77,0x77,
	0x00,0x00,0x07,0x77,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*4*/
	0x00,0x00,0x07,0x77,0x00,0x07,0x77,0x73,
	0x07,0x77,0x37,0x77,0x05,0x57,0x77,0x73,
	0x00,0x00,0x07,0x77,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*5*/
	0x00,0x00,0x07,0x73,0x00,0x07,0x77,0x77,
	0x07,0x73,0x77,0x73,0x05,0x57,0x77,0x77,
	0x00,0x00,0x07,0x73,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*6*/
	0x00,0x00,0x07,0x73,0x00,0x07,0x73,0x77,
	0x07,0x73,0x77,0x77,0x05,0x57,0x77,0x77,
	0x00,0x00,0x07,0x73,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*2'*/
	0x00,0x00,0x07,0x77,0x00,0x07,0x77,0x73,
	0x07,0x77,0x77,0x77,0x05,0x57,0x77,0x73,
	0x00,0x00,0x07,0x77,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*3'*/
	0x00,0x00,0x07,0x73,0x00,0x07,0x77,0x77,
	0x07,0x77,0x77,0x73,0x05,0x57,0x77,0x77,
	0x00,0x00,0x07,0x73,0x00,0x00,0x00,0x07,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,	/*6'*/
	0x00,0x00,0x07,0x73,0x00,0x07,0x77,0x77,
	0x07,0x73,0x77,0x77,0x05,0x57,0x73,0x77,
	0x00,0x00,0x07,0x73,0x00,0x00,0x00,0x07,

	0x05,0x55,0x50,0x00,0x05,0x55,0x55,0x50,	/*1*/
	0x05,0x55,0x25,0x54,0x05,0x52,0x22,0x54,
	0x05,0x55,0x25,0x54,0x04,0x55,0x55,0x54,
	0x00,0x04,0x55,0x54,0x00,0x00,0x00,0x54,
	0x05,0x55,0x50,0x00,0x05,0x55,0x55,0x50,	/*2*/
	0x05,0x55,0x53,0x54,0x05,0x55,0x55,0x54,
	0x05,0x35,0x55,0x54,0x04,0x55,0x55,0x54,
	0x00,0x04,0x55,0x54,0x00,0x00,0x00,0x54,
	0x05,0x55,0x50,0x00,0x05,0x55,0x55,0x50,	/*3*/
	0x05,0x55,0x55,0x34,0x05,0x55,0x35,0x54,
	0x05,0x35,0x55,0x54,0x04,0x55,0x55,0x54,
	0x00,0x04,0x55,0x54,0x00,0x00,0x00,0x54,
	0x05,0x55,0x50,0x00,0x05,0x35,0x55,0x50,	/*4*/
	0x05,0x55,0x53,0x54,0x05,0x55,0x55,0x54,
	0x05,0x35,0x55,0x54,0x04,0x55,0x53,0x54,
	0x00,0x04,0x55,0x54,0x00,0x00,0x00,0x54,
	0x05,0x35,0x50,0x00,0x05,0x55,0x55,0x50,	/*5*/
	0x05,0x55,0x55,0x34,0x05,0x55,0x35,0x54,
	0x05,0x35,0x55,0x54,0x04,0x55,0x55,0x54,
	0x00,0x04,0x55,0x34,0x00,0x00,0x00,0x54,
	0x05,0x35,0x50,0x00,0x05,0x55,0x55,0x50,	/*6*/
	0x05,0x35,0x55,0x34,0x05,0x55,0x55,0x54,
	0x05,0x35,0x55,0x34,0x04,0x55,0x55,0x54,
	0x00,0x04,0x55,0x34,0x00,0x00,0x00,0x54,
	0x05,0x55,0x50,0x00,0x05,0x35,0x55,0x50,	/*2'*/
	0x05,0x55,0x55,0x54,0x05,0x55,0x55,0x54,
	0x05,0x55,0x55,0x54,0x04,0x55,0x53,0x54,
	0x00,0x04,0x55,0x54,0x00,0x00,0x00,0x54,
	0x05,0x35,0x50,0x00,0x05,0x55,0x55,0x50,	/*3'*/
	0x05,0x55,0x55,0x54,0x05,0x55,0x35,0x54,
	0x05,0x55,0x55,0x54,0x04,0x55,0x55,0x54,
	0x00,0x04,0x55,0x34,0x00,0x00,0x00,0x54,
	0x05,0x35,0x50,0x00,0x05,0x55,0x35,0x50,	/*6'*/
	0x05,0x55,0x55,0x34,0x05,0x55,0x55,0x54,
	0x05,0x35,0x55,0x54,0x04,0x55,0x35,0x54,
	0x00,0x04,0x55,0x34,0x00,0x00,0x00,0x54,

	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*1*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x55,0x00,
	0x00,0x05,0x55,0x00,0x00,0x55,0x55,0x50,
	0x00,0x55,0x25,0x50,0x05,0x52,0x22,0x55,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*2*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x55,0x00,
	0x00,0x05,0x35,0x00,0x00,0x55,0x55,0x50,
	0x00,0x55,0x55,0x50,0x05,0x55,0x55,0x55,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*3*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x35,0x00,
	0x00,0x05,0x55,0x00,0x00,0x55,0x55,0x50,
	0x00,0x55,0x55,0x50,0x05,0x55,0x35,0x55,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*4*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x55,0x00,
	0x00,0x05,0x35,0x00,0x00,0x55,0x55,0x50,
	0x00,0x55,0x55,0x50,0x05,0x53,0x53,0x55,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*5*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x35,0x00,
	0x00,0x05,0x55,0x00,0x00,0x55,0x55,0x50,
	0x00,0x55,0x55,0x50,0x05,0x35,0x35,0x35,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*6*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x35,0x00,
	0x00,0x05,0x55,0x00,0x00,0x53,0x55,0x50,
	0x00,0x55,0x55,0x50,0x05,0x35,0x55,0x35,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*2'*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x55,0x00,
	0x00,0x05,0x55,0x00,0x00,0x55,0x55,0x50,
	0x00,0x55,0x55,0x50,0x05,0x53,0x53,0x55,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*3'*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x55,0x00,
	0x00,0x05,0x55,0x00,0x00,0x55,0x55,0x50,
	0x00,0x55,0x55,0x50,0x05,0x35,0x35,0x35,
	0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,	/*6'*/
	0x00,0x00,0x55,0x00,0x00,0x05,0x35,0x00,
	0x00,0x05,0x55,0x00,0x00,0x55,0x53,0x50,
	0x00,0x55,0x55,0x50,0x05,0x35,0x55,0x35,

	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*1*/
	0x66,0x66,0x66,0x00,0x66,0x62,0x66,0x40,
	0x66,0x22,0x26,0x60,0x06,0x62,0x66,0x60,
	0x06,0x66,0x66,0x66,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*2*/
	0x66,0x66,0x36,0x00,0x66,0x66,0x66,0x40,
	0x66,0x66,0x66,0x60,0x06,0x36,0x66,0x60,
	0x06,0x66,0x66,0x66,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*3*/
	0x66,0x66,0x36,0x00,0x66,0x66,0x66,0x40,
	0x66,0x63,0x66,0x60,0x06,0x66,0x66,0x60,
	0x06,0x36,0x66,0x66,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*4*/
	0x63,0x66,0x36,0x00,0x66,0x66,0x66,0x40,
	0x66,0x66,0x66,0x60,0x06,0x36,0x63,0x60,
	0x06,0x66,0x66,0x66,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*5*/
	0x36,0x66,0x36,0x00,0x66,0x66,0x66,0x40,
	0x66,0x63,0x66,0x60,0x06,0x66,0x66,0x60,
	0x06,0x36,0x66,0x36,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*6*/
	0x36,0x36,0x36,0x00,0x66,0x66,0x66,0x40,
	0x66,0x66,0x66,0x60,0x06,0x66,0x66,0x60,
	0x06,0x36,0x36,0x36,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*2'*/
	0x63,0x66,0x66,0x00,0x66,0x66,0x66,0x40,
	0x66,0x66,0x66,0x60,0x06,0x66,0x63,0x60,
	0x06,0x66,0x66,0x66,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*3'*/
	0x36,0x66,0x66,0x00,0x66,0x66,0x66,0x40,
	0x66,0x63,0x66,0x60,0x06,0x66,0x66,0x60,
	0x06,0x66,0x66,0x36,0x00,0x44,0x44,0x44,
	0x00,0x00,0x00,0x00,0x66,0x66,0x64,0x00,	/*6'*/
	0x36,0x66,0x36,0x00,0x66,0x66,0x66,0x40,
	0x63,0x66,0x63,0x60,0x06,0x66,0x66,0x60,
	0x06,0x36,0x66,0x36,0x00,0x44,0x44,0x44,
};

int	v_sprs=0;

int	job;
int	key,key_old=KEY_START;

int main()
{
	uchar	me[17];		/* me[0].gokei , me[1-16].dice no muki */
	uchar	cnt;
	char	s[16];

#ifdef	__BEOS__
	if(bw_start()>0)return(1);printf("init ok\n");
#endif	__BEOS__

	v_init();

	dice(cnt=16,0,me);
	while(1){
		while(!(key=key_press_check()))rand();
		if(key&KEY_START)break;
		if(key&KEY_LEFT1)if(cnt>1)dice(--cnt,0,me);
		if(key&KEY_RIGHT1)if(cnt<16)dice(++cnt,0,me);
		if(key&KEY_A){dice(cnt,30,me);sprintf(s,"total=%d  ",me[0]);v_puts(10,15,0,s);}
		while(key_press_check())rand();
	}
	
#ifdef	__BEOS__
	bw_quit();
#endif	__BEOS__
	return(0);
}

void dice(uchar cnt,uchar tm,uchar far *me)
{
	int	i,x,y,f;
	while(1){
		for(i=0;i<cnt;i++){
			f=tm?rand()%4:0;
			x=i*12+10;y=(i*5%7)*12+5;
			v_dice(x,y,f,me[i+1]);
		}
		v_spr_flush();
		if(tm--==0)break;
		sys_wait((tm>7)?3:10-tm);
		for(i=0;i<cnt;i++)me[i+1]=rand()%24;
	}
	me[0]=0;for(i=0;i<cnt;i++)me[0]+=me[i+1]/4+1;
}
/******************/
void v_init()
{
	int	i,j;
	uchar	b;
	uchar	s[40];
	uchar far	*sp;
	display_control(0);
	wwc_set_color_mode(COLOR_MODE_16PACKED);
	for(i=0;i<16;i++)for(j=0;j<16;j++)wwc_palette_set_color(i,j,v_cpaldata[i*16+j]);
	for(i=0x00;i<0x80;i++){
		text_get_fontdata(i,(void far*)s);
		sp=s+8;
		for(j=0;j<8;j++)for(b=0x80;b;){
			*sp=(s[j]&b)?0x10:0x00;b>>=1;
			*sp|=(s[j]&b)?0x01:0x00;b>>=1;
			sp++;
		}
		wwc_font_set_colordata(i,1,(void far*)(s+8));
	}
	wwc_font_set_colordata(0x80,36,(uchar far*)v_dicepat);
	v_cls();
	display_control(DCM_SCR1|DCM_SCR2|DCM_SPR);
}

void v_cls()
{
	screen_fill_char(SCREEN2,0,0,32,32,0);
	screen_fill_char(SCREEN1,0,0,32,32,0);
	sprite_set_range(0,0);
	v_sprs=0;
}

void v_spr_prt(int c,int x,int y,int p,int f)
{
	if(v_sprs>127)return;
	p=(p&7)<<9;
	sprite_set_char_location(v_sprs++,c|p|f,x,y);
}

void v_spr_flush()
{
	sprite_set_range(0,v_sprs);
	v_sprs=0;
}

void v_puts(int x,int y,int p,char far *s)
{
	int	i,c;
	for(i=0;s[i];i++){
		c=s[i]|(p<<9);
		screen_set_char(SCREEN2,x++,y,1,1,(void far*)&c);
	}
}

void v_put(int x,int y,int p,uint16 c)
{
	c=((c&0x200)<<4)|(c&0x1ff)|(p<<9);
	screen_set_char(SCREEN2,x,y,1,1,(void far*)&c);
}

void v_dice(int x,int y,uchar f,uchar n)
{
	static uchar	d[]={
		0x31,0x43,0x12,0x24,
		0x52,0x35,0x20,0x03,
		0x40,0x54,0x01,0x15,
		0x04,0x10,0x45,0x51,
		0x25,0x02,0x53,0x30,
		0x13,0x21,0x34,0x42,
	};
	static uchar	t[]={
		0,6,7,3,4,8
	};
	int	c,p,ff;
	f&=3;n=n%24;
	if(f==0){
		c=(((n&3)==0)||((n&3)==3))?t[n/4]:n/4;p=0x0f;ff=0x0000;
		v_spr_prt(0x80+c,x,y,p,ff);
		ff^=0x4000;if(c==5)c=8;else if(c==8)c=5;
		v_spr_prt(0x80+c,x+7,y,p,ff);
		c=(((n&3)==0)||((n&3)==2))?t[d[n]>>4]:d[n]>>4;p=0x0f;ff=0x0000;
		v_spr_prt(0x89+c,x,y+6,p,ff);
		c=(((n&3)==0)||((n&3)==1))?d[n]&0xf:t[d[n]&0xf];p=0x0f;ff=0x4000;
		v_spr_prt(0x89+c,x+7,y+6,p,ff);
	}else if(f==1){
		c=(((n&3)==0)||((n&3)==3))?t[n/4]:n/4;p=0x0e;ff=0xc000;
		v_spr_prt(0x80+c,x+7,y+8,p,ff);
		ff^=0x4000;if(c==5)c=8;else if(c==8)c=5;
		v_spr_prt(0x80+c,x,y+8,p,ff);
		c=(((n&3)==0)||((n&3)==2))?t[d[n]>>4]:d[n]>>4;p=0x0f;ff=0xc000;
		v_spr_prt(0x89+c,x+7,y+2,p,ff);
		c=(((n&3)==0)||((n&3)==1))?d[n]&0xf:t[d[n]&0xf];p=0x0f;ff=0x8000;
		v_spr_prt(0x89+c,x,y+2,p,ff);
	}else if(f==2){
		c=(((n&3)==0)||((n&3)==3))?t[n/4]:n/4;p=0x0f;ff=0x0000;
		v_spr_prt(0x92+c,x,y,p,ff);
		ff^=0x8000;if(c==5)c=8;else if(c==8)c=5;
		v_spr_prt(0x92+c,x,y+7,p,ff);
		c=(((n&3)==0)||((n&3)==2))?t[d[n]>>4]:d[n]>>4;p=0x0f;ff=0x0000;
		v_spr_prt(0x9b+c,x+6,y,p,ff);
		c=(((n&3)==0)||((n&3)==1))?d[n]&0xf:t[d[n]&0xf];p=0x0e;ff=0x8000;
		v_spr_prt(0x9b+c,x+6,y+7,p,ff);
	}else{
		c=(((n&3)==0)||((n&3)==3))?t[n/4]:n/4;p=0x0f;ff=0xc000;
		v_spr_prt(0x92+c,x+6,y+7,p,ff);
		ff^=0x8000;if(c==5)c=8;else if(c==8)c=5;
		v_spr_prt(0x92+c,x+6,y,p,ff);
		c=(((n&3)==0)||((n&3)==2))?t[d[n]>>4]:d[n]>>4;p=0x0e;ff=0xc000;
		v_spr_prt(0x9b+c,x,y+7,p,ff);
		c=(((n&3)==0)||((n&3)==1))?d[n]&0xf:t[d[n]&0xf];p=0x0f;ff=0x4000;
		v_spr_prt(0x9b+c,x,y,p,ff);
	}
}

