;/*
if not exists ChunLiData.o
	sc DATA=FAR ChunLiData.c
endif
sc RESOPT IGNORE=73 DATA=NEAR NMINC UCHAR CONSTLIB STREQ STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE ChunLi.c
slink from LIB:c.o ChunLi.o ChunLiData.o to ///Clients/ChunLi LIB LIB:sc.lib LIB:amiga.lib //lib/client.lib SC SD NOICONS STRIPDEBUG
quit

 ChunLi 1.1  (Client for BServer)

 Copyright © 1995 by Stefano Reksten of 3AM - The Three Amigos!!!
 All rights reserved.
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/icon.h>
#include <clib/alib_protos.h>

#include <stdio.h>

#include "//include/client.h"
#include "//include/bitmap/bitmap.h"
#include "//include/bitmap/bitmap_pragmas.h"

extern ULONG ChunLi_cmap32[16][3];

#define IMGW 100
#define IMGH 89

#define BITPLSIZE ((((IMGW+15)>>4)<<1)*IMGH)

extern UWORD walk1_imgdata[], walk2_imgdata[], walk3_imgdata[],
             breath1_imgdata[], breath2_imgdata[], breath3_imgdata[], 
             prepkick1_imgdata[], prepkick2_imgdata[], kick1_imgdata[],
             kick2_imgdata[], kick3_imgdata[], kick4_imgdata[],
             kick5_imgdata[], kick6_imgdata[], kick7_imgdata[],
             walk1_mask[], walk2_mask[], walk3_mask[],
             breath1_mask[], breath2_mask[], breath3_mask[], 
             prepkick1_mask[], prepkick2_mask[], kick1_mask[],
             kick2_mask[], kick3_mask[], kick4_mask[],
             kick5_mask[], kick6_mask[], kick7_mask[];

extern UWORD brk1_imgdata[], brk2_imgdata[],
             brk3_imgdata[], brk4_imgdata[],
             brk5_imgdata[], brk6_imgdata[],
             brk7_imgdata[], brk8_imgdata[];

char *ver = "$VER: ChunLi 1.0 "__AMIGADATE__;

struct { UWORD *frame, *mask; } FrameAndMask[] = {
	{ walk1_imgdata, walk1_mask }, { walk2_imgdata, walk2_mask },
	{ walk3_imgdata, walk3_mask },
	{ breath1_imgdata, breath1_mask }, { breath2_imgdata, breath2_mask },
	{ breath3_imgdata, breath3_mask }, { prepkick1_imgdata, prepkick1_mask },
	{ prepkick2_imgdata, prepkick2_mask }, { kick1_imgdata, kick1_mask },
	{ kick2_imgdata, kick2_mask },{ kick3_imgdata, kick3_mask },
	{ kick4_imgdata, kick4_mask },{ kick5_imgdata, kick5_mask },
	{ kick6_imgdata, kick6_mask },{ kick7_imgdata, kick7_mask }, };

UWORD *Breaks[] = {
	brk1_imgdata, brk2_imgdata,	brk3_imgdata, brk4_imgdata,
	brk5_imgdata, brk6_imgdata,	brk7_imgdata, brk8_imgdata };

UBYTE WhichFrame[] = {
1, 0, 1, 2, 5, 4, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 7, 6 };

#define WALK1     0
#define WALK2     1
#define WALK3     2
#define WALK4     3
#define BREATH1   4
#define BREATH2   5
#define BREATH3   6
#define BREATH4   7
#define PREPKICK1 8
#define PREPKICK2 9
#define KICK1     10
#define KICK2     11
#define KICK3     12
#define KICK4     13
#define KICK5     14
#define KICK6     15
#define KICK7     16
#define ENDKICK1  17
#define ENDKICK2  18

UBYTE *mem_ptr;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *BitMapBase, *IconBase;
struct DisplayIDInformation *dinfo;
BOOL blanking = TRUE;

extern ULONG RangeSeed;
struct Screen *scr;
struct BitMap *ChunLisbm, *storebm, *shadowbm, *scalebm, *breakbm, *scalebreakbm;
WORD x, y, dirx, diry,
     blitx, blity, sizex, sizey, xoffs, yoffs;
UBYTE scaleX = 0, scaleY = 0;
struct BitMapScaleInfo bsi;
UBYTE depth, frame;
struct RastPort *rp;
PLANEPTR mask;


void MoveChunLi( void )
{
x += dirx * 1<<scaleX;
if ( x < -((IMGW<<scaleX)>>1) )
	{
	x = -((IMGW<<scaleX)>>1);
	dirx = 4;
	bsi.bsi_Flags = 0L;
	}
if ( x >= scr->Width - 1 - (IMGW>>1) )
	{
	x = scr->Width - 1 - (IMGW>>1);
	dirx = - 4;
	bsi.bsi_Flags = BSIF_INVERTHOR;
	}

y += diry * 1<<scaleY;
if ( y < -((IMGH<<scaleY)>>1) )
	{
	y = -((IMGH<<scaleY)>>1);
	diry = 4;
	}
if ( y >= scr->Height - 1 - (IMGH>>1) )
	{
	y = scr->Height - 1 - (IMGH>>1);
	diry = - 4;
	}
}



void BreakWall( void )
{
int n = RangeRand( 8 );
WORD hitx, hity = y + (IMGH<<scaleY)/3 - 20;

if ( !bsi.bsi_Flags )
	hitx = x + ((IMGW-0x20) << scaleX);
else
	hitx = x;

if ( hitx < 0 )
	{ blitx = -hitx; sizex = (0x20<<scaleX) + hitx; }
else if ( hitx > scr->Width - (0x20<<scaleX) )
	{ blitx = 0; sizex = scr->Width - hitx - 1; }
else
	{ blitx = 0; sizex = 0x20<<scaleX; }
if ( hity < 0 )
	{ blity = -hity; sizey = (0x42<<scaleY) + hity; }
else if ( hity > scr->Height - (0x42<<scaleY) )
	{ blity = 0; sizey = scr->Height - hity - 1; }
else
	{ blity = 0; sizey = 0x42<<scaleY; }

BltBitMap( storebm, hitx < 0 ? 0 : ( hitx >= scr->Width ? scr->Width - 1 - hitx : hitx ), hity < 0 ? 0 : ( hity >= scr->Height ? scr->Height - 1 - hity : hity ),
	shadowbm, 0, 0,
	0x20 << scaleX, 0x42 << scaleY, 0xC0, 0xFF, NULL );
BltMaskBitMapRastPort( scalebreakbm, ((n * 0x20) << scaleX) + blitx, blity,
	rp, 0, 0,
	0x20 << scaleX, 0x42 << scaleY, (ABC|ABNC|ANBC), scalebreakbm->Planes[0] );
if ( (hitx + (0x20<<scaleX)) >= 0 && hitx < scr->Width &&
     (hity + (0x42<<scaleY)) >= 0 && hity < scr->Height ) 
	{
	BltBitMap( shadowbm, 0, 0,
		storebm, hitx < 0 ? 0 : ( hitx >= scr->Width ? scr->Width - 1 - hitx : hitx ), hity < 0 ? 0 : ( hity >= scr->Height ? scr->Height - 1 - hity : hity ),
		sizex, sizey, 0xC0, 0xFF, NULL );
	}
else
	DisplayBeep( NULL );
}


BOOL CreateFrames( void )
{
int n;
BOOL success = FALSE;

if ( breakbm = AllocVec( sizeof(struct BitMap), MEMF_ANY|MEMF_CLEAR ) )
	{
	InitBitMap( breakbm, depth + 1, 0x20, 0x42 );
	breakbm->Planes[0] = mem_ptr;
	breakbm->Planes[1] = mem_ptr + 528;
	breakbm->Planes[2] = mem_ptr + 1056;
	breakbm->Planes[3] = mem_ptr + 1584;
	breakbm->Planes[depth] = mem_ptr;

	if ( scalebreakbm = CreateBitMap( (0x20 * 10) << scaleX, 0x42 << scaleY, depth + 1 ) )
		{
		if ( ChunLisbm = AllocVec( sizeof(struct BitMap), MEMF_ANY|MEMF_CLEAR ) )
			{
			InitBitMap( ChunLisbm, depth + 1, IMGW, IMGH );
			ChunLisbm->Planes[0] = mem_ptr;
			ChunLisbm->Planes[1] = mem_ptr + BITPLSIZE;
			ChunLisbm->Planes[2] = mem_ptr + 2 * BITPLSIZE;
			ChunLisbm->Planes[3] = mem_ptr + 3 * BITPLSIZE;
			ChunLisbm->Planes[depth] = mem_ptr + 4 * BITPLSIZE;

			if ( shadowbm = CreateBitMap( IMGW << scaleX, (IMGH+4) << scaleY, depth + 1 ) )
				{
				if ( scalebm = CreateBitMap( IMGW << scaleX, IMGH << scaleY, depth + 1 ) )
					{
					mask = scalebm->Planes[depth];

					bsi.bsi_SrcLeftEdge = 0;
					bsi.bsi_SrcTopEdge = 0;
					bsi.bsi_DestTopEdge = 0;
					bsi.bsi_HorDen = 1;
					bsi.bsi_VertDen = 1;
					bsi.bsi_TempBitMap = shadowbm;
					bsi.bsi_HorNum = 1 << scaleX;
					bsi.bsi_VertNum = 1 << scaleY;
					bsi.bsi_Flags = NULL;
					bsi.bsi_SrcBitMap = breakbm;
					bsi.bsi_DestBitMap = scalebreakbm;
					bsi.bsi_Width = 0x20;
					bsi.bsi_Height = 0x42;

					for ( n = 0; n < 8 && blanking; n++ )
						{
						if ( STILL_BLANKING )
							{
							Decrunch30( mem_ptr, Breaks[n] );
							bsi.bsi_DestLeftEdge = (n * 0x20) << scaleX;
							bsi.bsi_Flags = NULL;
							ScaleBitMap( &bsi );
							}
						else blanking = FALSE;
						}

					bsi.bsi_DestLeftEdge = 0;
					bsi.bsi_SrcBitMap = ChunLisbm;
					bsi.bsi_DestBitMap = scalebm;
					bsi.bsi_Width = IMGW;
					bsi.bsi_Height = IMGH;

					success = blanking;

					if ( !success ) DisposeBitMap( scalebm );
					}
				if ( !success ) DisposeBitMap( shadowbm );
				}
			if ( !success ) FreeVec( ChunLisbm );
			}
		if ( !success ) DisposeBitMap( scalebreakbm );
		}
	if ( !success ) FreeVec( breakbm );	
	}

return success;
}


void Blank( void )
{
register int n, ticks = 0;
int status = BREATH1;
ULONG mode;
Sound *scream;
UBYTE volume = GETVOLUME(dinfo);
BOOL success = FALSE;
UBYTE brightness = GETBRIGHTNESS(dinfo);

if ( scr = GetDeeperFrontmostScreen( brightness, 4 ) )
	{
	depth = scr->RastPort.BitMap->Depth;
	if ( depth <= 7 )
		{
		mode = scr->ViewPort.Modes;
		if ( mode & SUPERHIRES )
			{
			if ( mode & LACE )
				{ scaleX = 1; scaleY = 0; }
			else
				{ scaleX = 2; scaleY = 0; }
			}
		else if ( mode & HIRES )
			{
			if ( mode & LACE )
				{ scaleX = 1; scaleY = 1; }
			else
				{ scaleX = 1; scaleY = 0; }
			}
		else if ( mode & LACE )
			{ scaleX = 0; scaleY = 1; }

		if ( storebm = CreateBitMap( scr->Width, scr->Height, depth ) )
			{
			BltBitMap( scr->RastPort.BitMap, 0, 0,
				storebm, 0, 0,
				scr->Width, scr->Height, 0xC0, 0xFF, NULL );

			if ( CreateFrames() )
				{
				if ( rp = AllocVec( sizeof(struct RastPort), MEMF_ANY|MEMF_CLEAR ) )
					{
					success = TRUE;

					scream = Open8SVX( "8SVX/ChunLi.scream" );

					InitRastPort( rp );
					rp->BitMap = shadowbm;

					SpritesOff();

					for ( n = 1; n < 16; n++ )
						SetRGB32( &scr->ViewPort, n, ((ChunLi_cmap32[n][0]>>24)*brightness/100)<<24, ((ChunLi_cmap32[n][1]>>24)*brightness/100)<<24, ((ChunLi_cmap32[n][2]>>24)*brightness/100)<<24 );

					x = RangeRand( scr->Width - 1 - (IMGW << scaleX) );
					y = RangeRand( scr->Height - 1 - (IMGH << scaleY) );

					while( STILL_BLANKING )
						{
						if ( !(bsi.bsi_Flags + scaleX + scaleY) && ++ticks < 5 )
							WaitTOF();
						else
							{
							ticks = 0;

							if ( status == BREATH1 )
								{
								n = RangeRand( 4 );
								if ( !n )
									status = PREPKICK1;
								else if ( n == 1 )
									{
									status = WALK1;
									while ( ( dirx == 0 ) && ( diry == 0 ) )
										{
										dirx = RangeRand( 3 ) ? 4 : -4;
										diry = RangeRand( 3 ) ? 4 : -4;
										}
									if ( dirx < 0 )
										bsi.bsi_Flags = BSIF_INVERTHOR;
									else
										bsi.bsi_Flags = 0L;
									}
								else status = BREATH2;
								}
							else if ( status == BREATH2 )
								status = BREATH3;
							else if ( status == BREATH3 )
								status = BREATH4;
							else if ( status == BREATH4 )
								status = BREATH1;
							else if ( status == WALK1 )
								{ MoveChunLi(); status = WALK2; }
							else if ( status == WALK2 )
								{ MoveChunLi(); status = WALK3; }
							else if ( status == WALK3 )
								{ MoveChunLi(); status = WALK4; }
							else if ( status == WALK4 )
								{ MoveChunLi();
								if ( RangeRand( 5 ) ) status = WALK1;
								else status = BREATH1; }
							else if ( status == PREPKICK1 )
								status = PREPKICK2;
							else if ( status == PREPKICK2 )
								{ status = KICK1; PlayAsynch8SVX( scream, volume ); BreakWall(); }
							else if ( status == KICK1 )
								{ status = KICK2; PlayAsynch8SVX( scream, volume ); BreakWall(); }
							else if ( status == KICK2 )
								{ status = KICK3; PlayAsynch8SVX( scream, volume ); BreakWall(); }
							else if ( status == KICK3 )
								{ status = KICK4; PlayAsynch8SVX( scream, volume ); BreakWall(); }
							else if ( status == KICK4 )
								{ status = KICK5; PlayAsynch8SVX( scream, volume ); BreakWall(); }
							else if ( status == KICK5 )
								{ status = KICK6; PlayAsynch8SVX( scream, volume ); BreakWall(); }
							else if ( status == KICK6 )
								{ status = KICK7; PlayAsynch8SVX( scream, volume ); BreakWall(); }
							else if ( status == KICK7 )
								{
								if ( RangeRand( 3 ) )
									status = KICK1;
								else status = ENDKICK1;
								PlayAsynch8SVX( scream, volume );
								BreakWall();
								}
							else if ( status == ENDKICK1 )
								status = ENDKICK2;
							else if ( status == ENDKICK2 )
								status = BREATH1;

							if ( x < 0 )
								{ blitx = -x; sizex = (IMGW<<scaleX) + x; }
							else if ( x > scr->Width - 1 - (IMGW<<scaleX) )
								{ blitx = 0; sizex = scr->Width - x; }
							else
								{ blitx = 0; sizex = IMGW<<scaleX; }

							if ( y < 0 )
								{ blity = -y; sizey = ((IMGH+4)<<scaleY) + y; }
							else if ( y > scr->Height - 1 - ((IMGH+4)<<scaleY) )
								{ blity = 0; sizey = scr->Height - y; }
							else
								{ blity = 0; sizey = (IMGH+4)<<scaleY; }

							frame = WhichFrame[status];
							Decrunch30( mem_ptr, FrameAndMask[frame].frame );
							Decrunch30( ChunLisbm->Planes[depth], FrameAndMask[frame].mask );

							ScaleBitMap( &bsi );

							BltBitMap( storebm, x < 0 ? 0 : ( x >= scr->Width ? scr->Width - 1 - x : x ), y < 0 ? 0 : ( y >= scr->Height ? scr->Height - 1 - y : y ),
								shadowbm, 0, 0,
								IMGW << scaleX ,(IMGH+4) << scaleY, 0xC0, 0xFF, NULL );

							BltMaskBitMapRastPort( scalebm, blitx, blity, rp,
								0, 0, ( IMGW << scaleX ) - blitx, ( IMGH << scaleY ) - blity,
								(ABC|ABNC|ANBC), mask );

							WaitTOF();

							BltBitMap( shadowbm, 0, 0,
								scr->RastPort.BitMap, x < 0 ? 0 : ( x >= scr->Width ? scr->Width - 1 - x : x ), y < 0 ? 0 : ( y >= scr->Height ? scr->Height - 1 - y : y ),
								x < 0 ? IMGW << scaleX : sizex, y < 0 ? (IMGH+4) << scaleY : sizey, 0xC0, 0xFF, NULL );
							}
						}
					SpritesOn();

					Close8SVX( scream );

					FreeVec( rp );
					}
				DisposeBitMap( scalebm );
				DisposeBitMap( shadowbm );
				FreeVec( ChunLisbm );
				DisposeBitMap( scalebreakbm );
				FreeVec( breakbm );
				}
			DisposeBitMap( storebm );
			}
		}
	CloseScreen( scr );
	}

if ( !success )
	SendClientMsg( ACTION_FAILED );
}


void __main( void )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
		{
		if ( BitMapBase = OpenLibrary( "bitmap.library", 0L ) )
			{
			if ( mem_ptr = AllocVec( 5 * BITPLSIZE, MEMF_CHIP | MEMF_CLEAR ) )
				{
				if ( dinfo = OpenCommunication() )
					{
					CurrentTime( &RangeSeed, &RangeSeed );
					Blank();
					CloseCommunication( dinfo );
					}
				FreeVec( mem_ptr );
				}
			CloseLibrary( BitMapBase );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
