<l>
#include <intuition/intuition.h>

#include <proto/exec.h>

#include <proto/graphics.h>

#include <proto/intuition.h>

#include <functions.h>

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

#define WIDTH 500

#define HEIGHT 500

struct IntuitionBase *IntuitionBase;

struct GfxBase *GfxBase;

struct Window *win;

long N, t0, t, krok;

int SEED, loopx, loopy;

UBYTE d[WIDTH][HEIGHT];

float RND(void)

/* Funkcja zwraca losowâ wartoôê z zakresu <0;1) */

{

	return (float)(rand()/(float)RAND_MAX);

}

void stupid(void)

{

	if (win) CloseWindow(win);

	if (GfxBase) CloseLibrary((struct Library *)GfxBase);

	if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);

	exit(0);

}

void OpenLibraries(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();

	}

}

/* funkcja wypeînia "las" "drzewami" */

/*

** 0 - puste pole - kolor 0

** 1 - drzewo - kolor 2

** 2 - palâce sië drzewo - kolor 3

** 3 - spalone drzewo - kolor 1

*/

void losuj(void)

{

	int counter, tempx, tempy;

	d[0][0]=1;



	SetAPen(win->RPort, 2);

	WritePixel(win->RPort, 0, 0);

	

	for(counter=1;counter<N;counter++)

	{

check:

		tempx = RND()*WIDTH;

		tempy = RND()*HEIGHT;

		if(d[tempx][tempy]==0)

		{

			d[tempx][tempy]=1;

			WritePixel(win->RPort, tempx, tempy);

		}

		else goto check;

	}

}

void main(int argc,char *argv[])

{

	int xx, yy, i, j;



	if(argc!=3)

	{

		printf("Argumenty: N SEED N\n");

		exit(0);

	}

	N     = atol(argv[1]);

	SEED  = atoi(argv[2]);

	if(SEED==-1)

	{

		time(&t);

		srand((UWORD)t);

	}

	else srand(SEED);

	OpenLibraries();

	if(!(win=OpenWindowTags(0,

		WA_Width, WIDTH,

		WA_Height, HEIGHT,

		WA_Flags, WFLG_GIMMEZEROZERO | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_RMBTRAP | WFLG_ACTIVATE | WFLG_CLOSEGADGET,

		WA_IDCMP, IDCMP_CLOSEWINDOW,

		TAG_DONE))) stupid();

	losuj();

	d[0][0]=2; /* podpalamy pierwsze drzewo */

	SetAPen(win->RPort, 3);

	WritePixel(win->RPort, 0, 0);

	for(loopx=0; loopx<WIDTH; loopx++)

	{

		for(loopy=0; loopy<HEIGHT; loopy++)

		{

			if(d[loopx][loopy]==2)

			{

				/************ podpal wszystkich sâsiadów ***********/

				for(j=-1; j<=1; j++)

				{

					yy=loopy+j;

					if(yy<0) yy=0;

					for(i=-1; i<=1; i++)

					{

						xx=loopx+i;

						if(xx<0) xx=0;

						if(d[xx][yy]==1)

						{

							d[xx][yy]=2;

							SetAPen(win->RPort, 3);

							WritePixel(win->RPort, xx, yy);

						}

						else if(d[xx][yy]==0) krok++;

					}

				}

				d[loopx][loopy]=3;

				SetAPen(win->RPort, 1);

				WritePixel(win->RPort, loopx, loopy);

				/**************************************************/

			}

		}

	}

	printf("Gëstoôê drzew %f, czas %ld\n",(float)N/(WIDTH*HEIGHT),krok);

	Wait(1<<win->UserPort->mp_SigBit);

	stupid();

}

