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

 Dhalsim 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 UWORD z_imgdata[], z_mask[];
extern ULONG z_cmap32[16][3];

#define IMGW 64
#define IMGH 82

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

#define STAND  0
#define FLY    1

char *ver = "$VER: Dhalsim 1.1 "__AMIGADATE__;

#define GAPTOT 10
WORD vgap[GAPTOT] = { 0, -2, -3, -3, -2, 0, 2, 3, 3, 2 };

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 *Dhalsimsbm, *storebm, *shadowbm, *scalebm;
WORD x, y, dirx, diry,
     blitx, blity, sizex, sizey, y_gap;
UBYTE scaleX = 0, scaleY = 0;
struct BitMapScaleInfo bsi;
UBYTE depth, frame;
struct RastPort *rp;
PLANEPTR mask;
int status = STAND;


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

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


BOOL CreateFrames( void )
{
BOOL success = FALSE;

if ( Dhalsimsbm = AllocVec( sizeof(struct BitMap), MEMF_ANY|MEMF_CLEAR ) )
	{
	InitBitMap( Dhalsimsbm, depth + 1, IMGW, IMGH );
	Dhalsimsbm->Planes[0] = mem_ptr;
	Dhalsimsbm->Planes[1] = mem_ptr + BITPLSIZE;
	Dhalsimsbm->Planes[2] = mem_ptr + 2 * BITPLSIZE;
	Dhalsimsbm->Planes[3] = mem_ptr + 3 * BITPLSIZE;
	Dhalsimsbm->Planes[depth] = mem_ptr + 4 * BITPLSIZE;

	if ( shadowbm = CreateBitMap( IMGW << scaleX, IMGH << 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_DestLeftEdge = 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 = Dhalsimsbm;
			bsi.bsi_DestBitMap = scalebm;
			bsi.bsi_Width = IMGW;
			bsi.bsi_Height = IMGH;

			Decrunch30( mem_ptr, z_imgdata );
			Decrunch30( Dhalsimsbm->Planes[depth], z_mask );

			success = TRUE;
			}
		if ( !success ) DisposeBitMap( shadowbm );
		}
	if ( !success ) FreeVec( Dhalsimsbm );
	}

return success;
}


BOOL ChangeStatus( void )
{
static int minp = 0;
if (( ++minp >= ( status == STAND ? 50 : 100 ) ) && ( RangeRand( 10 ) == 9 ))
	{
	minp = 0;
	return TRUE;
	}
return FALSE;
}


void GetDirection( void )
{
dirx = diry = 0;

while ( ( dirx == 0 ) && ( diry == 0 ) )
	{
	dirx = RangeRand( 7 ) - 3;
	diry = RangeRand( 9 ) - 4;
	}
if ( dirx < 0 )
	bsi.bsi_Flags = 0L;
else
	bsi.bsi_Flags = BSIF_INVERTHOR;
}


void Blank( void )
{
register int n, ticks = 0;
ULONG mode, gapno = 0;
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;

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

					SpritesOff();

					for ( n = 1; n < 16; n++ )
						SetRGB32( &scr->ViewPort, n, ((z_cmap32[n][0]>>24)*brightness/100)<<24, ((z_cmap32[n][1]>>24)*brightness/100)<<24, ((z_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 == STAND )
								{
								if ( ChangeStatus() )
									{
									status = FLY;
									GetDirection();
									}
								}
							else /* FLY */
								{
								MoveDhalsim();
								if ( ChangeStatus() )
									status = STAND;
								else
								if ( !RangeRand( 40 ) )
									GetDirection();
								}

							y_gap = y + ( vgap[ ++gapno % GAPTOT ] << scaleY );

							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_gap < 0 )
								{ blity = -y_gap; sizey = (IMGH<<scaleY) + y_gap; }
							else if ( y_gap > scr->Height - 1 - (IMGH<<scaleY) )
								{ blity = 0; sizey = scr->Height - y_gap; }
							else
								{ blity = 0; sizey = IMGH<<scaleY; }

							ScaleBitMap( &bsi );

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

							BltMaskBitMapRastPort( scalebm, blitx, blity,
								rp, 0, 0, sizex, sizey, (ABC|ABNC|ANBC), mask );

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

					FreeVec( rp );
					}
				DisposeBitMap( scalebm );
				DisposeBitMap( shadowbm );
				FreeVec( Dhalsimsbm );
				}
			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 ) )
				{
				if ( dinfo = OpenCommunication() )
					{
					CurrentTime( &RangeSeed, &RangeSeed );
					Blank();
					CloseCommunication( dinfo );
					}
				FreeVec( mem_ptr );
				}
			CloseLibrary( BitMapBase );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
