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

#import "HEscortShipState.h"

#import "HSMarquee.h"
#import "Human.h"
#import "Map.h"
#import "MapView.h"
#import "Unit.h"

#import "../Orders.subproj/Orders.h"
#import <AppKit/psopsNeXT.h>

//======================================================================
// Allow the user to select a target ship to escort, and then give that
// order to the currently selected unit.
//======================================================================

@implementation HEscortShipState

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

    cursorLocation.row = -1;
    cursorLocation.column = -1;

    return self;
}

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

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

- (NSString *) stateKey
{
    return HS_ESCORT_SHIP;
}
  
//----------------------------------------------------------------------

- (void) stateEntered:(Human *)context
{
    MapView *mapView = [context mapView];
  
    Unit *unit = [context selectedUnit];
    Map *map = [context map];
    EMMapLocation unitLocation = [unit unitLocation];
    MapToken mapToken = [map tokenAtLocation:unitLocation];
  
    // Set cursor
    [mapView useDirectionCursor];
    [mapView setCursorEnabled:NO];
    //[mapView enableCompressedEvents:NO];

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

    if (unit == nil)
    {
        NSBeep ();
        [context restoreState];
    }
    else
    {
        if (EMTerrainComponent (mapToken) == t_city)
        {
            // Need to show unit on city.
            [map setToken:EMChangeIconComponent (mapToken, [unit icon]) atLocation:unitLocation];
        }

        [self showMarquee:context atLocation:unitLocation];
    }
}
  
//----------------------------------------------------------------------

- (void) stateExited:(Human *)context
{
    [self stopMarquee:context];
}

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

- (void) mouseDown:(Human *)context:(unsigned int)modifierFlags atLocation:(EMMapLocation)target
{
    Unit *unit = [context selectedUnit];
    MapView *mapView = [context mapView];
    //float pat[2] = {3};
    EMMapLocation unitLocation = [unit unitLocation];
    NSPoint a, b;

    [self stopMarquee:context];

    a = [mapView getCenterPointForLocation:unitLocation];
    b = [mapView getCenterPointForLocation:target];

    [mapView scrollLocationToVisible:target];
    [mapView lockFocus];

    PSnewinstance ();
    PSsetinstance (YES);

    HSDrawMarquee (a.x - 8, a.y - 7, a.x + 7, a.y + 8, 5 - offset);
    PSsetinstance (NO);

    [mapView unlockFocus];

    offset = (offset + 1) % 6;
}

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

- (void) mouseUp:(Human *)context:(unsigned int)modifierFlags atLocation:(EMMapLocation)target
{
    Unit *unit = [context selectedUnit];
    Unit *ship = [context primaryUnitAtLocation:target];
  
    MapView *mapView = [context mapView];
    Map *map = [context map];
    EMMapLocation unitLocation = [unit unitLocation];
    MapToken mapToken = [map tokenAtLocation:unitLocation];

    [mapView lockFocus];
    PSnewinstance ();
    [mapView unlockFocus];

    if (ship != nil  && ship != unit)
        [unit setOrder:[[[OEscortShip alloc] initForUnit:unit escort:ship onMap:[context map]] autorelease]];

    if (EMTerrainComponent (mapToken) == t_city)
    {
        // Need to show unit on city.
        [map setToken:EMChangeIconComponent (mapToken, i_none) atLocation:unitLocation];
    }

    [context restoreState];
}

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

- (void) keyDown:(Human *)context:(NSEvent *)theEvent
{
    NSString *eventCharacters = [theEvent characters];
    unichar uc = [eventCharacters characterAtIndex:0];

    switch (uc)
    {
      case 27: // Escape key
      case ' ':
          [context restoreState];
          break;
 
      default:
          [super keyDown:context:theEvent];
          break;
   }
}

@end
