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

#import "HSetFlightPathState.h"

#import "City.h"
#import "HFlightPathState.h"
#import "HSMarquee.h"
#import "Human.h"
#import "MapView.h"

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

//======================================================================
// Set the friendly target city for a flight path.  The source city was
// selected in the previous state.
//======================================================================

@implementation HSetFlightPathState

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

    patternOffset = 0;

    return self;
}

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

- (NSString *) stateName
{
    return @"Set Flight Path";
}
  
//----------------------------------------------------------------------

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

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

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

- (void) mouseDown:(Human *)context:(unsigned int)modifierFlags atLocation:(EMMapLocation)target
{
    City *city = [context selectedCity];
    EMMapLocation cityLocation = [city cityLocation];
    MapView *mapView = [context mapView];
    NSPoint a, b;
    float pat[2] = {3};
    int dr = target.row - cityLocation.row;
    int dc = target.column - cityLocation.column;
    int distance;

    if (dr < 0)
        dr = -dr;
    if (dc < 0)
        dc = -dc;

    distance = (dr > dc) ? dr : dc;

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

    //[mapView centerCell:row:col ifNotVisible:YES];
    [mapView scrollLocationToVisible:target];

    [context setStatusLine:[NSString stringWithFormat:@"Distance %d", distance]];

    [mapView lockFocus];
    PSsetlinewidth (3);
    PSsetdash (pat, 1, patternOffset);
    PSnewinstance ();
    PSsetinstance (YES);

    PSsetgray (NSWhite);
    PSmoveto (a.x, a.y);
    PSlineto (b.x, b.y);
    PSstroke ();

    HSDrawMarquee (a.x - 8, a.y - 7, a.x + 7, a.y + 8, 5 - patternOffset);
    PSsetinstance (NO);
    [mapView unlockFocus];

    patternOffset = (patternOffset + 1) % 6;
}

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

- (void) mouseUp:(Human *)context:(unsigned int)modifierFlags atLocation:(EMMapLocation)target
{
    MapView *mapView = [context mapView];
    City *city = [context selectedCity];
    EMMapLocation cityLocation = [city cityLocation];

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

    if ((cityLocation.row != target.row || cityLocation.column != target.column) && [context cityAtLocation:target] != nil)
    {
        OMoveTo *order = [[[OShortestMoveTo alloc] initForUnit:nil moveToLocation:target onMap:[context map]] autorelease];
        [city setFlightPath:order];
    }
    else
    {
        [city setFlightPath:nil];
    }

    [context changeState:HS_FLIGHT_PATH];
}

@end
