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

#import "OPatrolTo.h"

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

//======================================================================
//
// This order patrols a unit between two locations.  It uses the
// shortest path move to order to handle the movement in each direction.
//
//======================================================================

#define OPatrolTo_VERSION 1

@implementation OPatrolTo

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

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

- initForUnit:(Unit *)aUnit patrolFromLocation:(EMMapLocation)sourceLocation toLocation:(EMMapLocation)destinationLocation
        onMap:(Map *)aMap
{
    [super initForUnit:aUnit];

    source = sourceLocation;
    destination = destinationLocation;

    map = aMap;
    subOrder = [[[OShortestMoveTo alloc] initForUnit:aUnit moveToLocation:destination onMap:map] retain];
    returning = NO;

    return self;
}

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

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

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

- (NSString *) orderText
{
    if (returning == YES)
        return [NSString stringWithFormat:@"Patrol to %d,%d", source.row, source.column];
    else
        return [NSString stringWithFormat:@"Patrol to %d,%d", destination.row, destination.column];
}

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

- (Direction) nextMove
{
    Direction dir;

    dir = [subOrder nextMove];

    return dir;
}

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

- (BOOL) isOrderComplete
{
    if ([subOrder isOrderComplete] == YES)
    {
        SNRelease (subOrder);
        if (returning == YES)
        {
            subOrder = [[OShortestMoveTo alloc] initForUnit:unit moveToLocation:destination onMap:map];
            returning = NO;
        }
        else
        {
            subOrder = [[OShortestMoveTo alloc] initForUnit:unit moveToLocation:source onMap:map];
            returning = YES;
        }
    }

    return NO;
}

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

- (PathSegment *) pathInMapView:(MapView *)mapView
{
    return [[PathSegment alloc] initPathFromLocation:source toLocation:destination in:mapView];
}

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

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

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

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

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

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

@end
