//
// $Id: OEscortShip.m,v 1.1 1997/10/28 05:11:23 nygard Exp $
//

//
//  This file is a part of Empire, a game of exploration and conquest.
//  Copyright (C) 1996  Steve Nygard
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//  You may contact the author by:
//     e-mail:  nygard@telusplanet.net
//

#import "Empire.h"

RCSID ("$Id: OEscortShip.m,v 1.1 1997/10/28 05:11:23 nygard Exp $");

#import "OEscortShip.h"

#import "OShortestMoveTo.h"
#import "Map.h"
#import "MapView.h"
#import "Unit.h"

//======================================================================
//
// When escorting a ship, we want to follow the ship by about three
// squares.  We do this by moving towards the ship, but keeping track
// of our distance from it.  Since the ship that we are escorting is
// moving, we need to constantly revise are target location.
//
// This implementation could be a little slow if you are escorting a
// ship that is far away.
//
//======================================================================

#define OEscortShip_VERSION 1

@implementation OEscortShip

+ (void) initialize
{
    if (self == [OEscortShip class])
    {
        [self setVersion:OEscortShip_VERSION];
    }
}

//----------------------------------------------------------------------

- initForUnit:(Unit *)aUnit escort:(Unit *)ship onMap:(Map *)aMap
{
    [super initForUnit:aUnit];

    map = aMap;
    targetShip = ship;

    lastDirection = -1;

    subOrder = [[OShortestMoveTo alloc] initForUnit:unit moveToLocation:[targetShip unitLocation] onMap:map];

    return self;
}

//----------------------------------------------------------------------

- (void) dealloc
{
    SNRelease (subOrder);
    
    [super dealloc];
}

//----------------------------------------------------------------------

- (NSString *) orderText
{
    return @"Escort Ship";
}

//----------------------------------------------------------------------

- (Direction) nextMove
{
    Direction dir;

    [self reset];

    if ([subOrder isTooClose:3] == YES)
    {
        Direction directions[8] = {d_northwest, d_north, d_northeast, d_east, d_southeast, d_south, d_southwest, d_west};

        lastDirection = (lastDirection + 1) % 8;
        dir = directions[lastDirection];
    }
    else
    {
        dir = [subOrder nextMoveWithin:3];
    }

    if (dir == d_none)
        [unit skipMove];

    return dir;
}

//----------------------------------------------------------------------

- (void) unblocked
{
    [subOrder unblocked];
}

//----------------------------------------------------------------------

- (BOOL) wasBlocked
{
    blockedCount++;

    if (blockedCount == 1)
        [self reset];

    return blockedCount < 10;
}

//----------------------------------------------------------------------

- (BOOL) isSentried
{
    return [subOrder isSentried];
}

//----------------------------------------------------------------------

- (void) reset
{
    SNRelease (subOrder);
  
    subOrder = [[OShortestMoveTo alloc] initForUnit:unit moveToLocation:[targetShip unitLocation] onMap:map];
}

@end
