//
// $Id: CombatEvent.m,v 1.7 1997/10/31 04:51:39 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: CombatEvent.m,v 1.7 1997/10/31 04:51:39 nygard Exp $");

#import "CombatEvent.h"

#import "Map.h"
#import "MapView.h"

//======================================================================
// A Combat Event records activity visible to the player that happened
// during the other players turns.
//======================================================================

@implementation CombatEvent

+ updateAtLocation:(EMMapLocation)target ofMap:(Map *)map
{
    return [[[CombatEvent alloc] initUpdateAtLocation:target ofMap:map] autorelease];
}

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

+ update3x3AroundLocation:(EMMapLocation)target ofMap:(Map *)map
{
    return [[[CombatEvent alloc] initUpdate3x3AroundLocation:target ofMap:map] autorelease];
}

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

+ showExplosions:(int)count atLocation:(EMMapLocation)target
{
    return [[[CombatEvent alloc] initShowExplosions:count atLocation:target] autorelease];
}
    
//----------------------------------------------------------------------

+ stripIconsOfPlayer:(Player)number
{
    return [[[CombatEvent alloc] initStripIconsOfPlayer:number] autorelease];
}

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

- initUpdateAtLocation:(EMMapLocation)target ofMap:(Map *)map
{
    [super init];

    combatEventType = cet_update1;
    eventLocation = target;

    tokens[0] = [map tokenAtLocation:target];

    return self;
}

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

- initUpdate3x3AroundLocation:(EMMapLocation)target ofMap:(Map *)map
{
    [super init];

    combatEventType = cet_update3;
    eventLocation = target;

    [map get3x3Tokens:tokens aroundLocation:target];

    return self;
}

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

- initShowExplosions:(int)count atLocation:(EMMapLocation)target
{
    [super init];

    combatEventType = cet_explosions;
    eventLocation = target;
    explosionCount = count;

    return self;
}

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

- initStripIconsOfPlayer:(Player)number
{
    [super init];

    combatEventType = cet_strip_icons;
    player = number;

    return self;
}


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

- (void) replayCombatFor:(Map *)map in:(MapView *)mapView
{
    switch (combatEventType)
    {
      case cet_update1:
          [mapView centerLocation:eventLocation ifNotVisible:YES];
          [map setToken:tokens[0] atLocation:eventLocation];
          break;

      case cet_update3:
          [mapView centerLocation:eventLocation ifNotVisible:YES];
          [map set3x3Tokens:tokens aroundLocation:eventLocation];
          break;

      case cet_explosions:
          [mapView centerLocation:eventLocation ifNotVisible:YES];
          [mapView showExplosions:explosionCount atLocation:eventLocation];
          break;

      case cet_strip_icons:
          [map stripIconsOfPlayer:player];
          break;

      default:
          NSLog (@"Unknown combat type...\n");
          break;
    }
}

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

- (NSString *) description
{
    NSString *str = nil;
    
    switch (combatEventType)
    {
      case cet_update1:
          str = [NSString stringWithFormat:@"update 1x1 (%d,%d)", eventLocation.row, eventLocation.column];
          break;

      case cet_update3:
          str = [NSString stringWithFormat:@"update 3x3 (%d,%d)", eventLocation.row, eventLocation.column];
          break;

      case cet_explosions:
          str = [NSString stringWithFormat:@"show %d explosions at (%d,%d)",
                          explosionCount, eventLocation.row, eventLocation.column];
          break;

      case cet_strip_icons:
          str = [NSString stringWithFormat:@"strip icons of player %d", player];
          break;

      default:
          str = @"Unknown combat type";
          break;
    }

    return str;
}

@end
