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

 PacMan 1.0  (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 pac1_cmap32[4][3];

#define IMGW 49
#define IMGH 45

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

extern UWORD pac1_imgdata[], pac2_imgdata[], pac3_imgdata[],
             pac1_mask[], pac2_mask[], pac3_mask[],
             __chip pallino1_imgdata[];

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

struct { UWORD *frame, *mask; } FrameAndMask[] = {
	{ pac1_imgdata, pac1_mask }, { pac2_imgdata, pac2_mask },
	{ pac3_imgdata, pac3_mask } };

#define WALK1  0
#define WALK2  1
#define WALK3  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 *PacMansbm, *storebm, *shadowbm, *scalebm, *breakbm, *scalebreakbm;
WORD x, y, dirx,
     blitx, blity, sizex, sizey, xoffs, yoffs;
UBYTE scaleX = 0, scaleY = 0;
struct BitMapScaleInfo bsi;
UBYTE depth, frame;
struct RastPort *rp;
PLANEPTR mask;


void MovePacMan( void )
{
x += dirx * 1<<scaleX;
if ( x < -(IMGW<<scaleX)+1 )
	{
	dirx = RangeRand( 3 ) + 1;
	x = -(IMGW<<scaleX)+1;
	bsi.bsi_Flags = BSIF_INVERTHOR;
	y = RangeRand( scr->Height + (IMGH>>1) ) - (IMGH>>1);
	}
else if ( x > scr->Width - 2 )
	{
	dirx = - RangeRand( 3 ) - 1;
	x = scr->Width - 2;
	bsi.bsi_Flags = 0L;
	y = RangeRand( scr->Height + (IMGH>>1) ) - (IMGH>>1);
	}
}


void BreakWall( void )
{
WORD hitx, hity = y + ((IMGH>>1)<<scaleY) - 3;

if ( dirx < 0 )
	hitx = x;
else
	hitx = x + ((IMGW - 7) << scaleX);

if ( hitx < 0 )
	{ blitx = -hitx; sizex = (7<<scaleX) + hitx; }
else if ( hitx >= scr->Width - (7<<scaleX) )
	{ blitx = 0; sizex = scr->Width - hitx - 1; }
else
	{ blitx = 0; sizex = 7<<scaleX; }
if ( hity < 0 )
	{ blity = -hity; sizey = (7<<scaleY) + hity; }
else if ( hity >= scr->Height - (7<<scaleY) )
	{ blity = 0; sizey = scr->Height - hity; }
else
	{ blity = 0; sizey = 7<<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,
	7 << scaleX, 7 << scaleY, 0xC0, 0xFF, NULL );
BltMaskBitMapRastPort( scalebreakbm, 0 + blitx, blity,
	rp, 0, 0,
	7 << scaleX, 7 << scaleY, (ABC|ABNC|ANBC), scalebreakbm->Planes[0] );
if ( (hitx + (7<<scaleX)) > 0 && hitx < scr->Width - 1 &&
     (hity + (7<<scaleY)) > 0 && hity < scr->Height - 1 ) 
	{
	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 );
	}
}


BOOL CreateFrames( void )
{
BOOL success = FALSE;

if ( breakbm = AllocVec( sizeof(struct BitMap), MEMF_ANY|MEMF_CLEAR ) )
	{
	InitBitMap( breakbm, depth + 1, 7, 7 );
	breakbm->Planes[0] = (UBYTE *)pallino1_imgdata;
	breakbm->Planes[1] = (UBYTE *)pallino1_imgdata + 14;
	breakbm->Planes[depth] = (UBYTE *)pallino1_imgdata;

	if ( scalebreakbm = CreateBitMap( 7 << scaleX, 7 << scaleY, depth + 1 ) )
		{
		if ( PacMansbm = AllocVec( sizeof(struct BitMap), MEMF_ANY|MEMF_CLEAR ) )
			{
			InitBitMap( PacMansbm, depth + 1, IMGW, IMGH );
			PacMansbm->Planes[0] = mem_ptr;
			PacMansbm->Planes[1] = mem_ptr + BITPLSIZE;
			PacMansbm->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_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 = 7;
					bsi.bsi_Height = 7;

					bsi.bsi_DestLeftEdge = 0;
					ScaleBitMap( &bsi );

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

					success = TRUE;
					}
				if ( !success ) DisposeBitMap( shadowbm );
				}
			if ( !success ) FreeVec( PacMansbm );
			}
		if ( !success ) DisposeBitMap( scalebreakbm );
		}
	if ( !success ) FreeVec( breakbm );	
	}

return success;
}


void Blank( void )
{
register int n, ticks = 0;
int status = WALK1;
ULONG mode;
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, ((pac1_cmap32[n][0]>>24)*brightness/100)<<24, ((pac1_cmap32[n][1]>>24)*brightness/100)<<24, ((pac1_cmap32[n][2]>>24)*brightness/100)<<24 );

					x = 1 - (IMGW << scaleX);
					dirx = RangeRand( 3 ) + 1;
					bsi.bsi_Flags = BSIF_INVERTHOR;
					y = RangeRand( scr->Height + (IMGH>>1) ) - (IMGH>>1);

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

							if ( status == WALK1 )
								{ MovePacMan(); status = WALK2; }
							else if ( status == WALK2 )
								{ MovePacMan(); status = WALK3; }
							else if ( status == WALK3 )
								{ MovePacMan(); BreakWall(); status = WALK1; }

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

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

							bsi.bsi_Width = IMGW;
							bsi.bsi_SrcLeftEdge = 0;

							bsi.bsi_Height = IMGH;
							bsi.bsi_SrcTopEdge = 0;

							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 << 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 << scaleY : sizey, 0xC0, 0xFF, NULL );
							}
						}
					SpritesOn();

					FreeVec( rp );
					}
				DisposeBitMap( scalebm );
				DisposeBitMap( shadowbm );
				FreeVec( PacMansbm );
				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 );
	}
}
