/*																				*/
/* Copyright (c) 1991															*/
/* Pages / KVM  Banzai Research Inst.											*/
/* 3914 Murphy Cyn Rd.															*/
/* San Diego, CA 92123															*/
/* (619) 492-9050																*/
/*																				*/

/* RCS History Section-------------------------------------------------------	*/

#ifdef RCSID
static char _rcsid[] = "$Header$";
#endif

/*
 * $Log$
 */


/* RCS History Section-------------------------------------------------------	*/


/* Description --------------------------------------------------------------	*/
/*  																			*/
/*		This class contains the BackSpace view for the Toasters module, which	*/
/*		was inspired by the "After Dark" module of the same name.				*/
/*  																			*/
/*  	Nov 14, 1991: Bruce Henderson (bruce@pages.com)							*/
/*  																			*/
/* Description --------------------------------------------------------------	*/

#import "ToastersViewClass.h"
#import "FlyingImage.h"
#import <objc/List.h>
#import <appkit/Application.h>
#import <appkit/NXImage.h>
#import <dpsclient/wraps.h>
#import <libc.h>
#import <math.h>

#define	DEF_TOASTERS	13
#define	DEF_TOAST		4

#define 	BIG_RAND			2147483647

@implementation ToastersView

- initFrame:(NXRect *)frameRect
{
	int	count;
	NXPoint	startPoint = {0.0};
	NXPoint	maxPoint = {0.0};
	id			newBeastie;
	NXSize	screen;
	long		rNum;
	float		xOffset;
	float		yOffset;
	float		speed = 0.25;
	int		startFrame;
	
	[super initFrame:frameRect];
	[self allocateGState];		// For faster lock/unlockFocus
	[self setClipping:NO];		// For faster lock/unlockFocus

	[NXApp getScreenSize:&screen];
	maxPoint.x = screen.width;
	maxPoint.y = screen.height;
	
	thingies = [[List allocFromZone:[self zone]] init];
	
	/* make the eggie! */
	newBeastie = [FlyingImage allocFromZone:[self zone]];
	
	rNum = random();
	xOffset = floor(((float)rNum/(float)BIG_RAND) * maxPoint.x);
	
	rNum = random();
	yOffset = floor(((float)rNum/(float)BIG_RAND) * maxPoint.y);
	
	speed = 3.0;
	
	startPoint.x = maxPoint.x - xOffset;
	startPoint.y = maxPoint.y - yOffset;
	[newBeastie initAtPoint:&startPoint andSpeed:speed foodType:T_EGGIE for:self];
	[thingies addObject:newBeastie];


	for(count = 0; count < DEF_TOAST;count++)
	{
		newBeastie = [FlyingImage allocFromZone:[self zone]];
		
		rNum = random();
		xOffset = floor(((float)rNum/(float)BIG_RAND) * maxPoint.x);
		
		rNum = random();
		yOffset = floor(((float)rNum/(float)BIG_RAND) * maxPoint.y);
		
		speed = 3.0;
		
		startPoint.x = maxPoint.x - xOffset;
		startPoint.y = maxPoint.y - yOffset;
		[newBeastie initAtPoint:&startPoint andSpeed:speed foodType:T_TOAST for:self];
		[thingies addObject:newBeastie];
	}


	for(count = 0; count < DEF_TOASTERS;count++)
	{
		newBeastie = [FlyingImage allocFromZone:[self zone]];
		
		rNum = random();
		xOffset = floor(((float)rNum/(float)BIG_RAND) * maxPoint.x);
		
		rNum = random();
		yOffset = floor(((float)rNum/(float)BIG_RAND) * maxPoint.y);
		
		startPoint.x = maxPoint.x - xOffset;
		startPoint.y = maxPoint.y - yOffset;
		[newBeastie initAtPoint:&startPoint andSpeed:[self aRandomSpeed] foodType:T_TOASTER for:self];
		
		rNum = random();
		startFrame = (int)rint(((float)rNum/(float)BIG_RAND) * 3);
		
		[newBeastie setImageFrame:startFrame];
		
		[thingies addObject:newBeastie];
	}

	return self;
}


- drawSelf:(const NXRect *)rects :(int)rectCount
{
	if(!rects || !rectCount)
		return self;
		
	PSsetgray(0);
	NXRectFill(rects);

	[super drawSelf:rects :rectCount];
	
	[thingies makeObjectsPerform:@selector(display)];

	return self;
}

- oneStep
{
	[thingies makeObjectsPerform:@selector(move:) with:window];

	return self;
}

- (BOOL) useBufferedWindow
{
	return YES;
}

- verifyNewRect:(NXRect*)newRect for:flapper
{
	NXRect	dummyRect;
	id			checkBuzzard;
	int		count,maxThings = [thingies count];
	float		flapperSpeed = [flapper speed];
	float		buzzardSpeed;
	long		rNum;
	float		rfloat;

	for(count=0;count < maxThings;count++)
	{
		checkBuzzard = [thingies objectAt:count];
		if(checkBuzzard != flapper)
		{
			dummyRect = [checkBuzzard targetRect];
			if(NXIntersectsRect(&dummyRect, newRect))
			{
				/* undo last changes */
				NX_X(newRect) += flapperSpeed;
				NX_Y(newRect) += flapperSpeed;
				
				/* tryMovingDown */
				NX_Y(newRect) -= flapperSpeed;
				
				if(NXIntersectsRect(&dummyRect, newRect))
				{
					
					/* undo last changes */
					NX_Y(newRect) += flapperSpeed;
					
					/* tryMovingUp */
				
					NX_X(newRect) -= flapperSpeed;
					NX_Y(newRect) += flapperSpeed;
					
					if(NXIntersectsRect(&dummyRect, newRect))
					{
						/* undo last changes */

						NX_Y(newRect) -= flapperSpeed;
						NX_X(newRect) += flapperSpeed;
						
						/*just match speed */
						while(NXIntersectsRect(&dummyRect, newRect))
						{
							NX_X(newRect) += 2.0;
						}
						
						buzzardSpeed = [checkBuzzard speed];
						[flapper setSpeed:buzzardSpeed];

						NX_X(newRect) -= buzzardSpeed;
						NX_Y(newRect) -= buzzardSpeed;

					}
				}
			}
			else
			{
				rNum = random();
				rfloat = (float)rNum/(float)BIG_RAND;
				if(rfloat > 0.95)						
					[flapper setSpeed:[self aRandomSpeed]];
			}
		}
	
	}

	return self;
}

- (float)aRandomSpeed
{
	float		speed = 0.25;
	long		rNum;

	rNum = random();
	speed = (((float)rNum/(float)BIG_RAND) *3) + 3;
	if(speed < 3.5)
		speed = 4.0;

    return speed;
}


@end
