/* Ship.m -- implementation for ship objects
   Copyright (C) 1992, 1993 David A. Strout

   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, 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.  */

/* Written by David Strout <dstrout@isi.edu>. */


#import "Ship.h"
#import "Thinker.h"

@implementation Ship

- initOnSide:(int)side
/* This is nasty.  Better to have the ships on each side be subclasses of Ship. */
{
  INITRAND;
  [super init];
  
  [(explosion[0] = [self findImageNamed:"Exp0"]) setScalable:NO];
  [(explosion[1] = [self findImageNamed:"Exp1"]) setScalable:NO];
  [(explosion[2] = [self findImageNamed:"Exp2"]) setScalable:NO];
  [(explosion[3] = [self findImageNamed:"Exp3"]) setScalable:NO];
  [(explosion[4] = [self findImageNamed:"Exp4"]) setScalable:NO];
  [(explosion[5] = [self findImageNamed:"Exp5"]) setScalable:NO];
  [(explosion[6] = [self findImageNamed:"Exp6"]) setScalable:NO];
  [(explosion[7] = [self findImageNamed:"Exp7"]) setScalable:NO];
  
  
  if(side==FED) {
    [(ship[NORTH] = [self findImageNamed:"Fed0"]) setScalable:NO];
    [(ship[NEAST] = [self findImageNamed:"Fed1"]) setScalable:NO];
    [(ship[EAST] = [self findImageNamed:"Fed2"]) setScalable:NO];
    [(ship[SEAST] = [self findImageNamed:"Fed3"]) setScalable:NO];
    [(ship[SOUTH] = [self findImageNamed:"Fed4"]) setScalable:NO];
    [(ship[SWEST] = [self findImageNamed:"Fed5"]) setScalable:NO];
    [(ship[WEST] = [self findImageNamed:"Fed6"]) setScalable:NO];
    [(ship[NWEST] = [self findImageNamed:"Fed7"]) setScalable:NO];
  }
  else { /* side == Klingon */
    [(ship[NORTH] = [self findImageNamed:"Kli0"]) setScalable:NO];
    [(ship[NEAST] = [self findImageNamed:"Kli1"]) setScalable:NO];
    [(ship[EAST] = [self findImageNamed:"Kli2"]) setScalable:NO];
    [(ship[SEAST] = [self findImageNamed:"Kli3"]) setScalable:NO];
    [(ship[SOUTH] = [self findImageNamed:"Kli4"]) setScalable:NO];
    [(ship[SWEST] = [self findImageNamed:"Kli5"]) setScalable:NO];
    [(ship[WEST] = [self findImageNamed:"Kli6"]) setScalable:NO];
    [(ship[NWEST] = [self findImageNamed:"Kli7"]) setScalable:NO];
  };
  
  [(backGround =[self findImageNamed:"back"]) setScalable:NO];
  
  [self beginLife];

/* Separate them at the begining, since random is being lazy at this point */
  if(side==FED) location.x=0;
  if(side==KLI) location.x=maxX;

  return self;
}

- beginLife
/* After we explode, or at the start, set ourselves up */
{
  [self newVector];
  location.x=RANDINT(11)*100;location.y=RANDINT(8)*100;
  damage=MAX_DAM;
  status=OK;
  return self;
}

- drawSelf
/* This simply composites the right image at location.  That means that someone else has already done the lockFocus, and will do the unlockFocus & flushing. */
{
  
  [backGround composite:NX_SOVER toPoint:&location]; /* Erase old one */
  
  if(status==OK) {	
    switch(heading) {
    case NORTH: 
      location.y+=speed; 
      break; 
    case SOUTH: 
      location.y-=speed; 
      break; 
    case EAST: 
      location.x+=speed;
      break; 
    case WEST: 
      location.x-=speed;
      break; 
    case NEAST: 
      location.x+=speed;
      location.y+=speed; 
      break;
    case SEAST: 
      location.x+=speed;
      location.y-=speed; 
      break;
    case SWEST: 
      location.x-=speed;
      location.y-=speed; 
      break;
    case NWEST: 
      location.x-=speed;
      location.y+=speed; 
      break;
    };
  };
  
  switch(status) {
  case OK:
    [ship[heading] composite:NX_SOVER toPoint:&location];
    break;
  case EXPLODE0:
  case EXPLODE1:
  case EXPLODE2:
  case EXPLODE3:
  case EXPLODE4:
  case EXPLODE5:
  case EXPLODE6:
  case EXPLODE7:
    [explosion[status] composite:NX_SOVER toPoint:&location];
    status++;
    break;
  case DEAD: /* Game over, dude.  Galactic Toast.  Krispy Klingons. */
    [self beginLife];
  };
  
  return self;
}


- takeHitForPoints:(int)num
{
  damage-=num;
  [self newVector]; /* Try to dodge */
  if((damage<=0) && (status==OK)) status=EXPLODE0;
  return self;
}

- (int)damage;
{
  return damage;
}

@end
