;/*
sc RESOPT IGNORE=73 DATA=FAR CODE=FAR NMINC UCHAR CONSTLIB STREQ STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTTIME OPTPEEP Decay.c
slink from LIB:c.o Decay.o to hd2:System/Blankers/BServer/Clients/Decay LIB LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD NOICONS STRIPDEBUG
delete Decay.o
quit

 Decay 0.2  (Client for BServer)

    by Ali Graham   (based on code by Stefano Reksten of 3AM)

*/

#include <exec/types.h>
#include <exec/execbase.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/utility.h>
#include <proto/icon.h>
#include <clib/alib_protos.h>
#include <string.h>

#include <stdlib.h>
#include <time.h>

#include "/include/client.h"

#define min(a,b) ((a)<=(b)?(a):(b))

char *ver = "$VER: Decay 0.2 "__AMIGADATE__;

struct Screen *scr;
struct RastPort *rport;
UWORD swidth, sheight, swidth_m, sheight_m, swidth_mm;

int maxheight, maxwidth, minheight, minwidth, rate;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

struct DisplayIDInformation *dinfo;

UWORD bounds( UWORD a, UWORD min, UWORD max )
{

    if (a<min) return min;
    if (a>max) return max;

    return a;

}


UWORD power( UWORD a, UWORD b)
{

    UWORD sum=a, count;

    if (a==0) return 0;
    if (a==1) return 1;

    if (b==0) return 1;

    for ( count=0; count<b; count++ )
        {
        sum = sum * a;
        }

    return sum;

}

void Decay( void )
{
UWORD swidth, sheight, x, y, z;

if ( scr = CloneFrontmostScreen( GETBRIGHTNESS(dinfo) ) )
    {

    register struct RastPort *rp = &(scr->RastPort);

    swidth = scr->Width;
    sheight = scr->Height;

    SetAPen( rp, DarkestColorIndex( scr ));

    SpritesOff();

    while( STILL_BLANKING )
            {

            WaitTOF();

            for (z = 0; z<rate; z++)
                {
                    x = RangeRand( swidth );
                    y = RangeRand( sheight );

                    Move( rp, x, y ); Draw( rp, x, y );
                }

            }

    SpritesOn();

    CloseScreen( scr );
    }
else
    SendClientMsg( ACTION_FAILED );
}

void InterpretArgs( void )
{

    if ( !GetArgInt( dinfo->di_Args, "RATE", &rate ) )
        rate = 100;

    rate = bounds( rate, 1, 1000);

}

void main( void )
{

long t;

if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
        {
        if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
                {
                if ( dinfo = OpenCommunication() )
                        {

                        InterpretArgs();

                        /* Set up the randomizer-seed */
                        srand(time(&t));

                        Decay();

                        CloseCommunication( dinfo );
                        }
                CloseLibrary( (struct Library *)GfxBase );
                }
        CloseLibrary( (struct Library *)IntuitionBase );
        }
}
