/***************************/
/*  Potential Distribution */
/*                         */
/*   Mandelbrot for zē+c   */
/*   (C) 1996 by MaCheFi   */
/*    szczerba@us.edu.pl   */
/*     zfjmgr@us.edu.pl    */
/***************************/

#include <intuition/intuition.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/intuition.h>

#include <functions.h>
#include <graphics/modeid.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <m68881.h> /* watch out! */

#define WIDTH 512
#define HEIGHT 512
#define DEPTH 8
#define COLORS 256
#define N_MAX 1000	/* max. number of points */

int N,X,Y,x[N_MAX],y[N_MAX],m[N_MAX];
float BRIGHT=1.0;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *scr;

ULONG modeid=DEFAULT_MONITOR_ID | HIRESLACE_KEY;

void stupid(void)
{
	if (scr) CloseScreen(scr);
	if (GfxBase) CloseLibrary((struct Library *)GfxBase);
	if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
	exit(0);
}

void OpenAllLibraries(void)
{
	IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37);
	if (!IntuitionBase)
	{
		printf("You need intuition.library v.37+!!!\n");
		stupid();
	}
	GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37);
	if (!GfxBase)
	{
		printf("You need graphics.library v.37+!!!\n");
		stupid();
	}
}

void gimmecolors(void)
{
	ULONG i;
	for(i=0;i<COLORS;i++)
	{
		SetRGB32(&scr->ViewPort, i, (ULONG)(((float)i/0xff)*0xffffffff), (ULONG)(((float)i/0xff)*0xffffffff), (ULONG)(((float)i/0xff)*0xffffffff));
	}
}

float RND(void)
{
	return (float)(rand()/(float)RAND_MAX);
}

void randomize(void)
{
	int counter;
	for(counter=0;counter<N;counter++)
	{
		x[counter]=(int)(RND()*WIDTH);
		y[counter]=(int)(RND()*HEIGHT);
		m[counter]=(int)(RND()*360+1);
	}
}

int to_color(float c)
{
	if( (BRIGHT*c) > (COLORS-1) )
		return COLORS-1;
	else
		return (int)(BRIGHT*c);
}

float v(int index)
{
	float r;
	r=(float) (sqrt( (double)( (x[index]-X)*(x[index]-X)+0.00001+(y[index]-Y)*(y[index]-Y))));
	return((float)(m[index]/r));

} /* v() */

void main(int argc,char *argv[])
{
	int i;
	float tempv;
	long int t;
	UWORD SEED;

	if(argc!=4)
	{
		printf("Arguments: N BRIGHTNESS SEED\n");
		exit(0);
	}

	N      = atol(argv[1]);
	BRIGHT = atof(argv[2]);
	SEED   = atoi(argv[3]);

	if(SEED==-1)
	{
		time(&t);
		srand((UWORD)t);
	}
	else srand(SEED);

	randomize();
	OpenAllLibraries();

	if(!(scr=OpenScreenTags(0,
		SA_Width, WIDTH,
		SA_Height, HEIGHT,
		SA_Depth, DEPTH,
		SA_Type, PUBLICSCREEN,
		SA_DisplayID, modeid,
		TAG_DONE))) stupid();

	gimmecolors();

	SetRast(&scr->RastPort,0);

	for(Y=0;Y<HEIGHT;Y++)
	{
		if(scr->MouseX<2 && scr->MouseY<2)
		{
			stupid();
			break;
		}

		for(X=0;X<WIDTH;X++)
		{
			tempv=0.0;
			for(i=0;i<N;i++)
				tempv+=v(i);
			SetAPen(&scr->RastPort,to_color(tempv));
			WritePixel(&scr->RastPort,X,Y);
		}
	}
	for(;;)
	{
		if(scr->MouseX<2 && scr->MouseY<2)
		{
			stupid();
			break;
		}
		Delay(50);
		printf("Where is the top left screen corner...?\n");
	}
}
