/* $VER: WBStars_main.c 2.08 (04 Jul 1998)
*/

#include <intuition/screens.h>
#include <proto/intuition.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <clib/alib_protos.h>
#include <clib/macros.h>
#include <math.h>
#include <string.h>

#include <stdio.h>

#include "WBStars_include.h"
#include "WBStars_protos.h"
#include "WBStars_plot.h"

struct Screen		*wbench=NULL;
struct RastPort		*RPort;
struct Layer_Info	*LInfo;
struct ColorMap		*cm;
char			pen_obtained;
int			bgcolor;

/*
long    _stack=4000;
char    *_procname="WBStars";
long    _priority=0;
long    _BackGroundIO=FALSE;
*/

int		minx,miny,width,height;
BYTE	CXpri=0;
UBYTE	*hotkey;
char	activated=FALSE;
char	locked=FALSE;
char	version[]="$VER: WBStars 2.0b5 (18 Jan 1998)\0";
int	bg_color;
int	fg_color;
int	Loop_Wait;
int	Max_Objects;
int	Max_Stick;
int	Check_Stick;
int	V_Flutter;
char	Planes;
char	Remember;
char	Force;
int	Wind;
int	few_Flakes;
char	Donotlock;

void	main(long argc, UBYTE **argv)
{
	UBYTE	**ttypes;
	struct EasyStruct	fail1={sizeof(struct EasyStruct),0,"WBStars","failed to open libraries"," Shit ! "};
	int	delay;

	if( OpenLibraries() )
	{
		/* read the Tooltypes */
		ttypes		= ArgArrayInit(argc, argv);
		delay		= ArgInt(ttypes, "INITIAL_DELAY", 0);
		CXpri		= (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
		hotkey		= ArgString(ttypes, "CX_POPKEY", "control alt w");
		bgcolor		= ArgInt(ttypes, "BG_COLOR", -1);
		Loop_Wait	= ArgInt(ttypes, "LOOP_WAIT", 3);
		Max_Objects	= ArgInt(ttypes, "MAX_OBJECTS", 1000);
		Max_Stick	= ArgInt(ttypes, "MAX_STICK", 100);
		V_Flutter	= MAX(MIN(ArgInt(ttypes, "V_FLUTTER", 20),100),0);
		Planes		= MAX(MIN(ArgInt(ttypes, "PLANES", 1),15),1);
		few_Flakes	= MAX(MIN(ArgInt(ttypes, "FEW_FLAKES", 1),32767),1);
		Remember	= (char)(0!=strcmp(ArgString(ttypes, "REMEMBER", "NO"),"NO\0"));
		Force		= (char)(0!=strcmp(ArgString(ttypes, "FORCE", "NO"),"NO\0"));
		Donotlock	= (char)(0!=strcmp(ArgString(ttypes, "DONOTLOCK", "NO"),"NO\0"));
		Wind		= ArgInt(ttypes, "WIND", 0);
		Check_Stick	= ArgInt(ttypes, "CHECK_STICK", 5);
		SetTaskPri(FindTask(0),MAX(MIN(ArgInt(ttypes, "TOOLPRI", 0),127),-128));
		ArgArrayDone();

		if( InitCx() )
		{
			Delay(delay);
			while( HandleCx() )
			{
				if(activated) PlotObjects();

				Delay(Loop_Wait);
			}
			RemovCx();
		}
	}
	else
	{
		if( IntuitionBase ) EasyRequestArgs(NULL,&fail1,0,NULL);
	}
	CloseLibraries();
	if( activated )	Inactivate();
}




void	Inactivate()
{
	if(activated)
	{
		ClearObjects();
		if(pen_obtained)ReleasePen(cm,fg_color);
		UnlockPubScreen( NULL, wbench);
		wbench=NULL;
		activated=FALSE;
	}
}

char	Activate()		/* is called, if WBStars should activate */
{				/* returns TRUE if Workbench could be locked */
	short	i;
	short	testcolor[256];
	short	h;
	double	dist;
	double	mindist=-1.0;
	ULONG	color;
	short	ncolors;
	short	r,g,b;

	if( activated ) Inactivate();	/* if activated twice inactivate before */
										/* locking workbench again */
	if( wbench=LockPubScreen("Workbench") )
	{
		RPort		= &(wbench->RastPort);	/* the WBench´s RPort is the */
		LInfo		= &(wbench->LayerInfo);	/* one we want to draw in */
		minx		= wbench->LeftEdge;
		miny		= wbench->TopEdge + wbench->BarHeight + 1;
		width		= wbench->Width;
		height		= wbench->Height - wbench->BarHeight - 1;
		cm		= wbench->ViewPort.ColorMap;
		ncolors		= (short)(1<<(RPort->BitMap->Depth));

		bg_color	=bgcolor;

		if( bg_color<0 )
		{
			for(i=0;i<(short)(1<<(RPort->BitMap->Depth));i++)
			{
				testcolor[i]=0;
			}
			for(i=0;i<width;i++)
			{
				testcolor[ReadPixel(RPort,i+minx,miny)]++;
			}
			bg_color=0;
			h=testcolor[0];
			for(i=1;i<(short)(1<<(RPort->BitMap->Depth));i++)
			{
				if(testcolor[i]>h)
				{
					bg_color=i;
					h=testcolor[i];
				}
			}
		}
		if(OS3)
		{
			fg_color=(int)ObtainBestPenA(cm,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,NULL);
			pen_obtained=TRUE;
		}
		if(!OS3 || fg_color==-1)
		{
			for(i=0;i<ncolors;i++)
			{
				color=GetRGB4(cm,(long)i);
				r=(color>>8)&15;
				g=(color>>4)&15;
				b=color&15;

				if( (i!=bg_color) && (((dist=pow((double)(15-r),2.0)+pow((double)(15-g),2.0)+pow((double)(15-b),2.0))<mindist)||(mindist<0.0)) )
				{
					mindist=dist;
					fg_color=i;
				}
			}
			pen_obtained=FALSE;
		}

		activated=TRUE;
		InitObjects();
	}

	return activated;
}

