
#import "XoxWars.h"
#import "TrackMander.h"
#import "XWShip.h"

@implementation TrackMander

- init
{
	[super init];
	tracks = [[List alloc] init];
	return self;
}

- addShip:ship
{
	[self addTrack:[[Tracker alloc] initFor:ship]];
	return [super addShip:ship];
}

- addTrack:track
{
	[tracks addObject:track];
  return self;
}


- resetShip:ship
{
//	[ship setShields:YES];
	return ship;
}

- oneStep:scenario
{
	extern float frandom(void);

	int n = [tracks count];
	int ne = [targets count];

	while (n--) {
		Tracker* track = [tracks objectAt:n];
		[track track:[targets objectAt:(n%ne)]];
		[self track:track];
	}
	return self;
}

- track:(Tracker*)track;
{
	SEL mode;
	float d = [track distance];
	[track shieldsWithin:300.0];

#ifdef DEBUG_TRACK
	printf("d: %f v: %f \t a: %f av: %f\n", d, [track velocity],
		[track angle], [track Vangle]);
#endif
	if (d < 200.0) {
			mode = @selector(turnTowards);
	} else if ([track velocity] > d/50.0){
		mode = @selector(matchVelocity);
	} else {
		mode = @selector(thrustTowards);
	}
	if ([track perform:mode] && [scenario hasBullets:alliance])
		[track fireBetween:200.0 :50.0];
	return self;
}
@end