//
// $Id: OPatrolFromShip.m,v 1.1 1997/10/28 05:11:35 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: OPatrolFromShip.m,v 1.1 1997/10/28 05:11:35 nygard Exp $");

#import "OPatrolFromShip.h"

#import "EmPlayer.h"
#import "Map.h"
#import "OMoveTo.h"
#import "OShortestMoveTo.h"
#import "Unit.h"

//======================================================================
//
// This is similar to the Patrol To order, but is for fighters
// stationed on board a Carrier.  Since one of the end points of the
// patrol (the Carrier) is mobile, we must do some extra work.
//
// First, we must reduce the maximum length of the patrol to ensure
// the fighters can make it back to the ship before running out of
// fuel.  Secondly, as the ship moves, the target location of the
// return trip must be updated.
//
//======================================================================

#define OPatrolFromShip_VERSION 1

@implementation OPatrolFromShip

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

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

- initForUnit:(Unit *)aUnit patrolFromLocation:(EMMapLocation)sourceLocation toLocation:(EMMapLocation)destinationLocation
        onMap:(Map *)aMap
{
    NSAssert (1 == 0, @"Incorrect initializer");

    return nil;
}

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

- initForUnit:(Unit *)aUnit patrolToLocation:(EMMapLocation)target onMap:(Map *)aMap
{
    Unit *aShip = [aUnit shipOnBoard];
    EMMapLocation shipLocation = [aShip unitLocation];

    [super initForUnit:aUnit patrolFromLocation:shipLocation toLocation:target onMap:aMap];

    deltaRow = target.row - shipLocation.row;
    deltaCol = target.column - shipLocation.column;
    ship = aShip;

    return self;
}

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

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

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

- (void) aboutToMove
{
    EMMapLocation shipLocation = [ship unitLocation];
  
    if (returning == YES && (shipLocation.row != source.row || shipLocation.column != source.column))
    {
        //Map *map = [[unit owner] map];
        EMMapSize mapSize = [map mapSize];

        source = shipLocation;
    
        destination.row = source.row + deltaRow;
        destination.column = source.column + deltaCol;

        if (destination.row < 0)
            destination.row = 0;

        if (destination.column < 0)
            destination.column = 0;

        if (destination.row >= mapSize.height)
            destination.row = mapSize.height - 1;

        if (destination.column >= mapSize.width)
            destination.column = mapSize.width - 1;

        SNRelease (subOrder);

        subOrder = [[OShortestMoveTo alloc] initForUnit:unit moveToLocation:source onMap:map];
    }
}

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

- (Direction) nextMove
{
    Direction dir;

    dir = [subOrder nextMove];

    return dir;
}

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

- (BOOL) wasBlocked
{
    BOOL try_again = [subOrder wasBlocked];

    if (try_again == NO && returning == YES)
    {
        subOrder = [[OShortestMoveTo alloc] initForUnit:unit moveToLocation:source onMap:map];
        returning = YES;
        try_again = YES;
        [self aboutToMove];
    }

    return try_again;
}

@end
