//
// $Id: HInitialState.m,v 1.2 1997/10/31 03:38:13 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: HInitialState.m,v 1.2 1997/10/31 03:38:13 nygard Exp $");

#import "HInitialState.h"

#import "City.h"
#import "GameManager.h" // For berserk
#import "HMoveState.h"
#import "Human.h"
#import "MapView.h"
#import "Unit.h"

//======================================================================
// Show production of all cities that just produced a unit.
//======================================================================

@implementation HInitialState

- initWithGameManager:(GameManager *)theGameManager
{
    [super initWithGameManager:theGameManager];

    newUnitEnumerator = nil;

    return self;
}

//======================================================================
// Subclass responsibilities
//======================================================================

- (NSString *) stateName
{
    return @"Initial";
}
  
//----------------------------------------------------------------------

- (NSString *) stateKey
{
    return HS_INITIAL;
}

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

- (void) stateEntered:(Human *)context
{
    MapView *mapView = [context mapView];
  
    // Set cursor
    [mapView useDefaultCursor];
    [mapView setFastCursor:NO];
    [mapView setCursorEnabled:YES];

    // Disable both buttons
    [context enableContinueButton:YES];
    [context enableEndTurnButton:NO];

    newUnitEnumerator = [[[context newUnitList] objectEnumerator] retain];

    [self continue:context];
}
 
//----------------------------------------------------------------------

- (void) continue:(Human *)context
{
    MapView *mapView = [context mapView];
    City *city;
    EMMapLocation location;
    Unit *newUnit;

    NSAssert (newUnitEnumerator != nil, @"new unit enumerator is nil.");

    newUnit = [newUnitEnumerator nextObject];
    
    [context setStatusLine:@""];

    if ([gameManager isBerserk] == NO && newUnit != nil)
    {
        city = [newUnit city];
        location = [city cityLocation];

        [mapView positionCursorAtLocation:location];
        [mapView centerScreenAroundCursor];

        [context setStatusLine:[NSString stringWithFormat:@"%@ produced a %@",
                                         [city cityName], EMUnitTypeName ([newUnit unitType])]];

        [context selectThing:city];
    }
    else
    {
        SNRelease (newUnitEnumerator);
        [context changeState:HS_MOVE];
    }
}

@end
