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

#import "OLoadShip.h"
#import "EmPlayer.h"
#import "City.h"
#import "OMoveTo.h"
#import "Unit.h"

//======================================================================
//
// This is for Transports or Carriers.  It instructs the ship to
// remain sentried until it is full of units.  However, it also
// actively instructs nearby (immediately adjacent) units to move onto
// the ship, even if they are within a city (but not if they are
// on board another ship).
//
// This could probably use some more work to make it function better.
//
//======================================================================

#define OLoadShip_VERSION 1

@implementation OLoadShip

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

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

- initForUnit:(Unit *)aUnit
{
    [super initForUnit:aUnit];

    [unit skipMove];

    return self;
}

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

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

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

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

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

- (void) aboutToMove
{
    // Order nearby armies to board.

    // Get list of adjacent armies/fighters.
    // Figure out how many more units we need.
    // Order 'em aboard!

    // Armies in cities without orders (other than sentry)
    // Armies without orders (other than sentry)
  
    EmPlayer *owner = [unit owner];
    EMMapLocation shipLocation = [unit unitLocation];
    EMMapLocation unitLocation;
    int dr, dc;
    NSMutableArray *unitList = [owner unitList];
    NSEnumerator *unitEnumerator;
    Unit *aUnit;

    UnitType cargoType;
    int remainingCargoCapacity = [unit remainingCargoCapacity];

    if ([unit unitType] == u_transport)
    {
        cargoType = u_army;
    }
    else if ([unit unitType] == u_carrier)
    {
        cargoType = u_fighter;
    }
    else
    {
        cargoType = u_unknown;
    }

    if (remainingCargoCapacity > 0)
    {
        unitEnumerator = [unitList objectEnumerator];
        aUnit = [unitEnumerator nextObject];
        while (aUnit != nil && remainingCargoCapacity > 0)
        {
            if ([aUnit unitType] == cargoType && [aUnit isOnBoardShip] == NO)
            {
                unitLocation = [aUnit unitLocation];
                dr = shipLocation.row - unitLocation.row;
                dc = shipLocation.column - unitLocation.column;

                if (dr >= -1 && dr <= 1 && dc >= -1 && dc <= 1)
                {
                    [aUnit setOrder:[[[OMoveTo alloc] initForUnit:aUnit
                                                      moveToLocation:shipLocation
                                                      onMap:[owner map]] autorelease]];
                    [owner activateThisUnit:aUnit];
                    remainingCargoCapacity--;
                }
            }

            aUnit = [unitEnumerator nextObject];
        }

        [unit skipMove];
    }
}

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

- (Direction) nextMove
{
    // Skip move...
    return d_none;
}

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

- (BOOL) wasBlocked
{
    return NO;
}

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

- (BOOL) isSentried
{
    // Sentried until order is complete.
    // This needs to return NO s that the -aboutToMove method is called.
    return NO;
}

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

- (BOOL) isOrderComplete
{
    return [unit isFull];
}

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

- (void) unitDidEnter:(Unit *)aUnit
{
    if ([self isOrderComplete] == YES)
    {
        [[unit owner] activateThisUnit:unit];
    }
}

@end
