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

 Wrecked 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: Wrecked 0.2 "__AMIGADATE__;

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

int maxheight, maxwidth, minheight, minwidth, delay;

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 Wrecked( void )
{
UWORD swidth, sheight, sdepth;
UWORD x, y, w, h, col, z;

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

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

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

    sdepth = scr->BitMap.Depth;

    SpritesOff();

    while( STILL_BLANKING )
            {

            for (z = 0; z<delay; z++)
                {
                    WaitTOF();
                }

            /* calculate random width and height */

            w = RangeRand(maxwidth - minwidth);
            w = w + minwidth;

            if (w<minwidth) w=minwidth;
            if (w>maxwidth) w=maxwidth;

            h = RangeRand(maxheight - minheight);
            h = h + minheight;

            if (h<minheight) h=minheight;
            if (h>maxheight) h=maxheight;


            /* choose x and y location - limited so they don't go offscreen */

            x = RangeRand( swidth - w - 1);
            y = RangeRand( sheight - h - 1);


            /* ...and a random colour from the screen's palette */

            col = RangeRand( power( 2, sdepth ));

            /* draw the squares... */
            SetAPen( rp, col ); RectFill( rp, x, y, (x+w), (y+h) );

            }

    SpritesOn();

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

void InterpretArgs( void )
{

    if ( !GetArgInt( dinfo->di_Args, "MINW", &minwidth ) )
        minwidth = 10;

    minwidth = bounds(minwidth, 10, 200);

    if ( !GetArgInt( dinfo->di_Args, "MINH", &minheight ) )
        minheight = 10;

    minheight = bounds(minheight, 10, 200);

    if ( !GetArgInt( dinfo->di_Args, "MAXW", &maxwidth ) )
        maxwidth = 200;

    maxwidth = bounds(maxwidth, 10, 200);

    if ( !GetArgInt( dinfo->di_Args, "MAXH", &maxheight ) )
        maxheight = 200;

    maxheight = bounds(maxheight, 10, 200);

    if ( !GetArgInt( dinfo->di_Args, "DELAY", &delay ) )
        delay = 1;

    delay = bounds( delay, 1, 50);

}

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));

                        Wrecked();

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