space.c

sfmug-0.3/src/space.c · 191.0 KB

From the archive sfmug-0.3.tar.gz

Download the archive   « Back to the archive contents

/***************************************************************************
 *                         _____ ________  _____  ________              // *
 *                        / ___// ____/  |/  / / / / ____/       #### //   *
 *                        \__ \/ /_  / /|_/ / / / / / __       #####//#    *
 *                       ___/ / __/ / /  / / /_/ / /_/ /       ###//###    *
 *                      /____/_/   /_/  /_/\____/\____/         //###      *
 *                                                            //           *
 *                 Simulated Fururistic Multi Universe Game                *
 ***************************************************************************
 * - SFMUG 0.3  (c) 2003 by Bill Sica (Lews)                               *
 ***************************************************************************
 * - Chronicles Copyright 2001, 2002 by Brad Ensley (Orion Elder)          *
 ***************************************************************************
 * - SWR 2.0 (c) 1997, 1998 by Sean Cooper (specs@golden.net)              *
 *   based on a concept and ideas from the original SWR 1.0 immortals:     *
 *   Himself (Durga), Mark Matt (Merth), Jp Coldarone (Exar),              *
 *   Greg Baily (Thrawn), Ackbar, Satin, Streen and Bib                    *
 *   as well as much input from our other builders and players.            *
 ***************************************************************************
 * - SMAUG 1.4  Copyright 1994, 1995, 1996, 1998 by Derek Snider           *
 * - Merc  2.1  Copyright 1992, 1993 by Michael Chastain, Michael Quan,    *
 *   and Mitchell Tse.                                                     *
 * - DikuMud    Copyright 1990, 1991 by Sebastian Hammer, Michael Seifert, *
 *   Hans-Henrik Stærfeldt, Tom Madsen, and Katja Nyboe.                   *
 ***************************************************************************
 *                             Space module                                *
 ***************************************************************************/

#include <math.h> 
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "mud.h"
#include "files.h"

SHIP_DATA * first_ship;
SHIP_DATA * last_ship;

MISSILE_DATA * first_missile;
MISSILE_DATA * last_missile;

SPACE_DATA * first_starsystem;
SPACE_DATA * last_starsystem;


/* local routines */
void	fread_ship	args( ( SHIP_DATA *ship, FILE *fp ) );
bool	load_ship_file	args( ( char *shipfile ) );
void    fread_starsystem      args( ( SPACE_DATA *starsystem, FILE *fp ) );
bool    load_starsystem  args( ( char *starsystemfile ) );
void    write_starsystem_list args( ( void ) );
void    resetship args( ( SHIP_DATA *ship ) );
void    landship args( ( SHIP_DATA *ship, char *arg ) );
void    launchship args( ( SHIP_DATA *ship ) );
bool    land_bus args( ( SHIP_DATA *ship, int destination ) );
void    launch_bus args( ( SHIP_DATA *ship ) );
void    echo_to_room_dnr args( ( int ecolor , ROOM_INDEX_DATA *room ,  char *argument ) );
ch_ret drive_ship( CHAR_DATA *ch, SHIP_DATA *ship, EXIT_DATA  *exit , int fall );
bool    autofly(SHIP_DATA *ship);
bool is_facing( SHIP_DATA *ship , SHIP_DATA *target );
void sound_to_ship( SHIP_DATA *ship , char *argument );

/* from comm.c */
bool    write_to_descriptor     args( ( int desc, char *txt, int length ) );

/* from act_info.c */
bool 	is_online		args( ( char * argument ) );

void echo_to_room_dnr ( int ecolor , ROOM_INDEX_DATA *room ,  char *argument ) 
{
    CHAR_DATA *vic;
    
    if ( room == NULL )
    	return;
    	
    for ( vic = room->first_person; vic; vic = vic->next_in_room )
    {
	set_char_color( ecolor, vic );
	send_to_char( argument, vic );
    }
}


void move_ships( )
{
   SHIP_DATA *ship;
   SHIP_DATA *nextship;
   MISSILE_DATA *missile;
   MISSILE_DATA *m_next;
   SHIP_DATA *target;
   float dx, dy, dz, change;
   char buf[MAX_STRING_LENGTH];
   CHAR_DATA *ch;
   bool ch_found = FALSE;
   PLANET_DATA * planet;
      
   for ( missile = first_missile; missile; missile = m_next )
   {
           m_next = missile->next;
           
              ship = missile->fired_from;
              target = missile->target;
              
              if ( target && target->starsystem && target->starsystem == missile->starsystem )
              {
                if ( missile->mx < target->vx ) 
                  missile->mx += UMIN( missile->speed/5 , target->vx - missile->mx );
                else if ( missile->mx > target->vx ) 
                  missile->mx -= UMIN( missile->speed/5 , missile->mx - target->vx );  
                if ( missile->my < target->vy ) 
                  missile->my += UMIN( missile->speed/5 , target->vy - missile->my );
                else if ( missile->my > target->vy ) 
                  missile->my -= UMIN( missile->speed/5 , missile->my - target->vy );  
                if ( missile->mz < target->vz ) 
                  missile->mz += UMIN( missile->speed/5 , target->vz - missile->mz );
                else if ( missile->mz > target->vz ) 
                  missile->mz -= UMIN( missile->speed/5 , missile->mz - target->vz );  
              
                if ( abs(missile->mx) - abs(target->vx) <= 20 && abs(missile->mx) - abs(target->vx) >= -20
                && abs(missile->my) - abs(target->vy) <= 20 && abs(missile->my) - abs(target->vy) >= -20
                && abs(missile->mz) - abs(target->vz) <= 20 && abs(missile->mz) - abs(target->vz) >= -20 )
                {  
                   if ( target->chaff_released <= 0)
                   { 
                    echo_to_room( AT_YELLOW , ship->gunseat , "Your missile hits its target dead on!" );
                    echo_to_cockpit( AT_BLOOD, target, "The ship is hit by a missile.");
                    echo_to_ship( AT_RED , target , "A loud explosion shakes thee ship violently!" );
                    sprintf( buf, "You see a small explosion as %s is hit by a missile" , target->name );
                    echo_to_system( AT_ORANGE , target , buf , ship );
                    for ( ch = first_char; ch; ch = ch->next )
                      if ( !IS_NPC( ch ) && nifty_is_name( missile->fired_by, ch->name ) )
                      {   
                        ch_found = TRUE; 
                        damage_ship_ch( target , 30 , 50 , ch );
                      }
                    if ( !ch_found )
                       damage_ship( target , 30 , 50 );   
                    extract_missile( missile );
                   }
                   else
                   {
                    echo_to_room( AT_YELLOW , ship->gunseat , "Your missile explodes harmlessly in a cloud of chaff!" );
                    echo_to_cockpit( AT_YELLOW, target, "A missile explodes in your chaff.");
                    extract_missile( missile );
                   }
                   continue;
                }
                else
                {
                   missile->age++;
                   if (missile->age >= 50)
                   {
                      extract_missile( missile );
                      continue;
                   }
                }
              }
              else
              { 
                extract_missile( missile );
                continue;
              }

   }
   
   for ( ship = first_ship; ship; ship = nextship )
   {
     nextship = ship->next;
     
     if ( !ship->starsystem )
        continue;
          
     if ( ship->currspeed > 0 )
        {
          
          change = sqrt( ship->hx*ship->hx + ship->hy*ship->hy + ship->hz*ship->hz ); 

           if (change > 0)
           {
             dx = ship->hx/change;     
             dy = ship->hy/change;     
             dz = ship->hz/change;
             ship->vx += (dx * ship->currspeed/5);
             ship->vy += (dy * ship->currspeed/5);
             ship->vz += (dz * ship->currspeed/5);
           }
           
        } 

        if ( autofly(ship) )
           continue;
        
          if ( ship->starsystem->star1 && strcmp(ship->starsystem->star1,"") && 
                     abs(ship->vx - ship->starsystem->s1x) < 10 && 
                     abs(ship->vy - ship->starsystem->s1y) < 10 &&
                     abs(ship->vz - ship->starsystem->s1z) < 10 )
                {
                    echo_to_cockpit( AT_BLOOD+AT_BLINK, ship, "You fly directly into the sun.");
                    sprintf( buf , "%s flys directly into %s!", ship->name, ship->starsystem->star1); 
                    echo_to_system( AT_ORANGE , ship , buf , NULL );
                    destroy_ship(ship, NULL);
                    continue;
                }              
          if ( ship->starsystem->star2 && strcmp(ship->starsystem->star2,"") && 
               abs(ship->vx - ship->starsystem->s2x) < 10 && 
                     abs(ship->vy - ship->starsystem->s2y) < 10 &&
                     abs(ship->vz - ship->starsystem->s2z) < 10 )
                {
                    echo_to_cockpit( AT_BLOOD+AT_BLINK, ship, "You fly directly into the sun.");
                    sprintf( buf , "%s flys directly into %s!", ship->name, ship->starsystem->star2); 
                    echo_to_system( AT_ORANGE , ship , buf , NULL );
                    destroy_ship(ship , NULL);
                    continue;
                }            

        if ( ship->currspeed > 0 )
        { 
          for ( planet = ship->starsystem->first_planet ; planet ; planet = planet->next_in_system )      
               if (  abs(ship->vx - planet->x ) < 10 && 
                     abs(ship->vy - planet->y ) < 10 &&
                     abs(ship->vz - planet->z ) < 10 )
                {
                    sprintf( buf , "You begin orbitting %s.", planet->name); 
                    echo_to_cockpit( AT_YELLOW, ship, buf);
                    sprintf( buf , "%s begins orbiting %s.", ship->name, planet->name ); 
                    echo_to_system( AT_ORANGE , ship , buf , NULL );
                    ship->currspeed = 0;
                    continue;
                }            
        }
   }

   for ( ship = first_ship; ship; ship = nextship )
   {    
       nextship = ship->next;
       if (ship->collision) 
       {
           echo_to_cockpit( AT_WHITE+AT_BLINK , ship,  "You have collided with another ship!" );
           echo_to_ship( AT_RED , ship , "A loud explosion shakes the ship violently!" );   
           damage_ship( ship , ship->collision , ship->collision );
           ship->collision = 0;
       }
   }
}   

void recharge_ships( )
{
   SHIP_DATA *ship;
   SHIP_DATA *nextship;
   char buf[MAX_STRING_LENGTH];
   TURRET_DATA *turret;
   
   for ( ship = first_ship; ship; ship = nextship )
   {                
        nextship = ship->next;
   
        for( turret = ship->first_turret ; turret ; turret = turret->next )
          if (turret->laserstate > 0)
          {
             ship->energy -= turret->laserstate;
             turret->laserstate = 0;
          }
           
        if (ship->laserstate > 0)
        {
           ship->energy -= ship->laserstate;
           ship->laserstate = 0;
        }
        
        if (ship->missilestate == MISSILE_RELOAD_2)
        {
           ship->missilestate = MISSILE_READY;
           if ( ship->missiles > 0 )
               echo_to_room( AT_YELLOW, ship->gunseat, "Missile launcher reloaded.");
        }
        
        if (ship->missilestate == MISSILE_RELOAD )
        {
           ship->missilestate = MISSILE_RELOAD_2;
        }
        
        if (ship->missilestate == MISSILE_FIRED )
           ship->missilestate = MISSILE_RELOAD;
        
       if ( autofly(ship) )
       {
          if ( ship->starsystem )
          {
             if (ship->target && ship->laserstate != LASER_DAMAGED )
             {
                 int chance = 100;
                 SHIP_DATA * target = ship->target;
                 int shots;
                 
                for ( shots=0 ; shots <= ship->lasers ; shots++ ) 
                {   
                  if ( !ship->target )
                     break;
                
                  if (ship->shipstate != SHIP_HYPERSPACE && ship->energy > 25 
                  && ship->target->starsystem == ship->starsystem
                  && abs(target->vx - ship->vx) <= 1000 
                  && abs(target->vy - ship->vy) <= 1000
                  && abs(target->vz - ship->vz) <= 1000 
                  && ship->laserstate < ship->lasers )
                  {
                    if ( ship->class == SPACE_STATION || is_facing ( ship , target ) )
                    {
             		chance += target->model*10;
             		chance -= target->manuever/10;
             		chance -= target->currspeed/20;
             		chance -= ( abs(target->vx - ship->vx)/70 );
             		chance -= ( abs(target->vy - ship->vy)/70 );
             		chance -= ( abs(target->vz - ship->vz)/70 );
             		chance = URANGE( 10 , chance , 90 );
             		if ( number_percent( ) > chance )
             		{	  
           		    sprintf( buf , "%s fires at you but misses." , ship->name);  
             		    echo_to_cockpit( AT_ORANGE , target , buf );
      	                    sprintf( buf, "Laserfire from %s barely misses %s." , ship->name , target->name );
                            echo_to_system( AT_ORANGE , target , buf , NULL );	
             		} 
             		else
             		{
             		    sprintf( buf, "Laserfire from %s hits %s." , ship->name, target->name );
             		    echo_to_system( AT_ORANGE , target , buf , NULL );
                            sprintf( buf , "You are hit by lasers from %s!" , ship->name);  
                            echo_to_cockpit( AT_BLOOD , target , buf );           
                            echo_to_ship( AT_RED , target , "A small explosion vibrates through the ship." );           
                            if ( autofly( target ) )
                               target->target = ship;
                            damage_ship( target , 5 , 10 );
                        }
                        ship->laserstate++;
                    }
                  }
                }
             }
          }
       }
       
   }
}

void update_space( )
{
   SHIP_DATA *ship;
   SHIP_DATA *nextship;
   SHIP_DATA *target;
   char buf[MAX_STRING_LENGTH];
   int too_close, target_too_close;
   int recharge;
 
   for ( ship = first_ship; ship; ship = nextship )
   {    
        nextship = ship->next;
   
        if (ship->shipstate == SHIP_DESTROYED)
          destroy_ship(ship , NULL);

        if (ship->starsystem)
        {
          if ( ship->energy > 0 && ship->shipstate == SHIP_DISABLED && ship->class != SPACE_STATION )
             ship->energy -= 100;
          else if ( ship->energy > 0 )
             ship->energy += ( 5 + ship->model*2 );
          else 
             destroy_ship(ship , NULL);
        }
        
        if ( ship->chaff_released > 0 )
           ship->chaff_released--;
                
        if (ship->shipstate == SHIP_HYPERSPACE)
        {
            ship->hyperdistance -= ship->hyperspeed*2;
            ship->energy -= ( 2 + ship->model*2 );
            if (ship->hyperdistance <= 0)
            {
            	ship_to_starsystem (ship, ship->currjump);
            	
            	if (ship->starsystem == NULL)
            	{
            	    echo_to_cockpit( AT_RED, ship, "Ship lost in Hyperspace. Make new calculations.");
			    ship->shipstate = SHIP_LOST;
            	}
            	else
            	{
            	    echo_to_room( AT_YELLOW, ship->pilotseat, "Hyperjump complete.");
            	    echo_to_ship( AT_YELLOW, ship, "The ship lurches slightly as it comes out of hyperspace.");
            	    sprintf( buf ,"%s enters the starsystem at %.0f %.0f %.0f" , ship->name, ship->vx, ship->vy, ship->vz );
            	    echo_to_system( AT_YELLOW, ship, buf , NULL );
            	    ship->shipstate = SHIP_READY;
            	    STRFREE( ship->home );
            	    ship->home = STRALLOC( ship->starsystem->name );
                    
            	}
            }
            else
            {
		   if (ship->energy < (100+ship->hyperdistance) )
		   {
			echo_to_cockpit( AT_RED, ship, "Insufficiant Energy. Ship lost in Hyperspace.");
			ship->shipstate = SHIP_LOST;
		   }
		   else
		   {
                 sprintf( buf ,"%d" , ship->hyperdistance );
                 echo_to_room_dnr( AT_YELLOW , ship->pilotseat, "Remaining jump distance: " );
                 echo_to_room( AT_WHITE , ship->pilotseat,  buf );
		   }
               
            }


        }

	  if (ship->shipstate == SHIP_LOST)
	  {
          if ( ship->energy > 0 && ship->shipstate == SHIP_DISABLED && ship->class != SPACE_STATION )
             ship->energy -= 100;
          else if ( ship->energy > 0 )
             ship->energy += ( 10 + ship->model*2 );
          else 
             destroy_ship(ship , NULL);
	  }
        
        /* following was originaly to fix ships that lost their pilot 
           in the middle of a manuever and are stuck in a busy state 
           but now used for timed manouevers such as turning */
    
    	if (ship->shipstate == SHIP_BUSY_3)
           {
              echo_to_room( AT_YELLOW, ship->pilotseat, "Manuever complete.");  
              ship->shipstate = SHIP_READY;
           }
        if (ship->shipstate == SHIP_BUSY_2)
           ship->shipstate = SHIP_BUSY_3;
        if (ship->shipstate == SHIP_BUSY)
           ship->shipstate = SHIP_BUSY_2;
        
        if (ship->shipstate == SHIP_LAND_2)
           landship( ship , ship->dest );
        if (ship->shipstate == SHIP_LAND)
           ship->shipstate = SHIP_LAND_2;
        
        if (ship->shipstate == SHIP_LAUNCH_2)
           launchship( ship );
        if (ship->shipstate == SHIP_LAUNCH)
           ship->shipstate = SHIP_LAUNCH_2;
        
        
        ship->shield = UMAX( 0 , ship->shield - 1 - ship->model/5);
                
        if (ship->autorecharge && ship->maxshield > ship->shield && ship->energy > 100)
        {
           recharge  = UMIN( ship->maxshield-ship->shield, 10 + ship->model*2 );           
           recharge  = UMIN( recharge , ship->energy-100 );
           recharge  = UMAX( 1, recharge );
           ship->shield += recharge;
           ship->energy -= recharge;        
        }
        
        if (ship->shield > 0)
        {
          if (ship->energy < 200)
          {
          	ship->shield = 0;
          	echo_to_cockpit( AT_RED, ship,"The ships shields fizzle and die.");
                ship->autorecharge = FALSE;
          }
        }        
        
        if ( ship->starsystem && ship->currspeed > 0 )
        {
               sprintf( buf, "%d",
                          ship->currspeed );
               echo_to_room_dnr( AT_BLUE , ship->pilotseat,  "Speed: " );
               echo_to_room_dnr( AT_LBLUE , ship->pilotseat,  buf );
               sprintf( buf, "%.0f %.0f %.0f",
                           ship->vx , ship->vy, ship->vz );
               echo_to_room_dnr( AT_BLUE , ship->pilotseat,  "  Coords: " );
               echo_to_room( AT_LBLUE , ship->pilotseat,  buf );
        } 

        if ( ship->starsystem )
        {
          too_close = ship->currspeed + 50;
          for ( target = ship->starsystem->first_ship; target; target = target->next_in_starsystem)
          { 
            target_too_close = too_close+target->currspeed;
            if ( target != ship &&
                 abs(ship->vx - target->vx) < target_too_close &&
                 abs(ship->vy - target->vy) < target_too_close &&
                 abs(ship->vz - target->vz) < target_too_close )
            {
                sprintf( buf, "Proximity alert: %s  %.0f %.0f %.0f"
                            , target->name, target->vx, target->vy, target->vz);
                echo_to_room( AT_RED , ship->pilotseat,  buf );    
            }                
          }    
          too_close = ship->currspeed + 100;
          if ( ship->starsystem->star1 &&  strcmp(ship->starsystem->star1,"") && 
               abs(ship->vx - ship->starsystem->s1x) < too_close && 
               abs(ship->vy - ship->starsystem->s1y) < too_close &&
               abs(ship->vz - ship->starsystem->s1z) < too_close )
          {
                sprintf( buf, "Proximity alert: %s  %d %d %d", ship->starsystem->star1,
                         ship->starsystem->s1x, ship->starsystem->s1y, ship->starsystem->s1z);
                echo_to_room( AT_RED , ship->pilotseat,  buf );
          }               
          if ( ship->starsystem->star2 && strcmp(ship->starsystem->star2,"") && 
               abs(ship->vx - ship->starsystem->s2x) < too_close && 
               abs(ship->vy - ship->starsystem->s2y) < too_close &&
               abs(ship->vz - ship->starsystem->s2z) < too_close )
          {
                sprintf( buf, "Proximity alert: %s  %d %d %d", ship->starsystem->star2,
                         ship->starsystem->s2x, ship->starsystem->s2y, ship->starsystem->s2z);
                echo_to_room( AT_RED , ship->pilotseat,  buf );
          }               
        }
       

        if (ship->target)
        {
               sprintf( buf, "%s   %.0f %.0f %.0f", ship->target->name,
                          ship->target->vx , ship->target->vy, ship->target->vz );
               echo_to_room_dnr( AT_BLUE , ship->pilotseat , "Target: " );
               echo_to_room( AT_LBLUE , ship->pilotseat ,  buf );
               if (ship->starsystem != ship->target->starsystem)
               		ship->target = NULL;
         }
         
         
   	if (ship->energy < 100 && ship->starsystem )
   	{
   	    echo_to_cockpit( AT_RED , ship,  "Warning: Ship fuel low." );
        }
                    
        ship->energy = URANGE( 0 , ship->energy, ship->maxenergy );
   } 

   for ( ship = first_ship; ship; ship = ship->next )
   {
       
       if (ship->autotrack && ship->target && ship->class < SPACE_STATION )    
       {
           target = ship->target;
           too_close = ship->currspeed + 10; 
           target_too_close = too_close+target->currspeed;
           if ( target != ship && ship->shipstate == SHIP_READY && 
                 abs(ship->vx - target->vx) < target_too_close &&
                 abs(ship->vy - target->vy) < target_too_close &&
                 abs(ship->vz - target->vz) < target_too_close )
           {
              ship->hx = 0-(ship->target->vx - ship->vx);
              ship->hy = 0-(ship->target->vy - ship->vy);
              ship->hz = 0-(ship->target->vz - ship->vz);
              ship->energy -= ship->currspeed/10;
              echo_to_room( AT_RED , ship->pilotseat, "Autotrack: Evading to avoid collision!\n\r" );  
    	      if ( ship->manuever > 100 )
        	ship->shipstate = SHIP_BUSY_3;
              else if ( ship->manuever > 50  )
        	ship->shipstate = SHIP_BUSY_2;
    	      else
        	ship->shipstate = SHIP_BUSY;     
           }
           else if  ( !is_facing(ship, ship->target) )
           {
              ship->hx = ship->target->vx - ship->vx;
              ship->hy = ship->target->vy - ship->vy;
              ship->hz = ship->target->vz - ship->vz;
              ship->energy -= ship->currspeed/10;
              echo_to_room( AT_BLUE , ship->pilotseat, "Autotracking target ... setting new course.\n\r" );      
    	      if ( ship->manuever > 100  )
        	ship->shipstate = SHIP_BUSY_3;
              else if ( ship->manuever > 50  )
        	ship->shipstate = SHIP_BUSY_2;
    	      else
        	ship->shipstate = SHIP_BUSY;     
           }
       }

       if ( autofly(ship) )
       {
          if ( ship->starsystem )
          {
             if (ship->target)
             {
                 int chance = 100;
             
                 target = ship->target;
                 ship->autotrack = TRUE;
                 if( ship->class != SPACE_STATION )
                      ship->currspeed = ship->realspeed;
                 if ( ship->energy >200  )
                    ship->autorecharge=TRUE;
                 
                 if (ship->shipstate != SHIP_HYPERSPACE && ship->energy > 200 
                 && ship->hyperspeed > 0 && ship->target->starsystem == ship->starsystem
                 &&  ( abs(target->vx - ship->vx) > 5000 
                 || abs(target->vy - ship->vy) > 5000
                 || abs(target->vz - ship->vz) > 5000 )
                 && number_bits(2) == 0 )
                 {
                        ship->currjump = ship->starsystem;
                        ship->hyperdistance = 1;
                 
                     	sprintf( buf ,"%s disapears from your scanner." , ship->name );
                     	echo_to_system( AT_YELLOW, ship, buf , NULL );

    			ship_from_starsystem( ship , ship->starsystem );
    			ship->shipstate = SHIP_HYPERSPACE;
        
    			ship->energy -= 100;
    
    			ship->jx = target->vx;
    			ship->jy = target->vy;
    			ship->jz = target->vz;
                 }
                 
                 if (ship->shipstate != SHIP_HYPERSPACE && ship->energy > 25 
                 && ship->missilestate == MISSILE_READY && ship->target->starsystem == ship->starsystem
                 && abs(target->vx - ship->vx) <= 1200 
                 && abs(target->vy - ship->vy) <= 1200
                 && abs(target->vz - ship->vz) <= 1200
                 && ship->missiles > 0 )
                 {
                   if ( ship->class == SPACE_STATION || is_facing( ship , target ) )
                   {
             		chance -= target->manuever/5;
                        chance -= target->currspeed/20;
                        chance += target->model*target->model*2;
                        chance -= ( abs(target->vx - ship->vx)/100 );
                        chance -= ( abs(target->vy - ship->vy)/100 );
                        chance -= ( abs(target->vz - ship->vz)/100 );                          
                        chance += ( 30 );
                        chance = URANGE( 10 , chance , 90 );

             		if ( number_percent( ) > chance )
             		{
             		} 
                        else
                        {       
                                new_missile( ship , target , NULL );
            		 	ship->missiles-- ;
             		        sprintf( buf , "Incoming missile from %s." , ship->name);  
             		        echo_to_cockpit( AT_BLOOD , target , buf );
             		        sprintf( buf, "%s fires a missile towards %s." , ship->name, target->name );
             		        echo_to_system( AT_ORANGE , target , buf , NULL );

            		 	if ( ship->class == SPACE_STATION )
                                    ship->missilestate = MISSILE_RELOAD_2;
                                else
                                    ship->missilestate = MISSILE_FIRED;
                        }
                   }
                 }
             
                 if( ship->missilestate ==  MISSILE_DAMAGED )
                     ship->missilestate =  MISSILE_READY;
                 if( ship->laserstate ==  LASER_DAMAGED )
                     ship->laserstate =  LASER_READY;
                 if( ship->shipstate ==  SHIP_DISABLED )
                     ship->shipstate =  SHIP_READY;
                                 
             }
             else
             {
                 CLAN_DATA * clan = NULL;
                 CLAN_DATA * shipclan = NULL;
                 ROOM_INDEX_DATA * room;
                 CHAR_DATA *passenger;
                 int targetok;
                 
                 ship->currspeed = 0;
                 
                 for ( clan = first_clan ; clan ; clan = clan->next )
                     if ( !str_cmp( ship->owner , clan->name ) )
                         shipclan = clan;

                 if ( shipclan )
                   for ( target = ship->starsystem->first_ship; target; target = target->next_in_starsystem )
                   {
                     targetok = 0;
                     
                     if ( autofly(target) && !str_cmp( ship->owner , target->owner ) )
                        targetok = 1;
                        
                     for ( room = target->first_room ; room ; room = room->next_in_ship )       
                     {  
                           
                          for ( passenger = room->first_person ; passenger ; passenger = passenger->next_in_room )
                              if ( !IS_NPC(passenger) && passenger->pcdata )
                              {
                                 if ( passenger->pcdata->clan == shipclan )
                                     targetok = 1;
                                 else if ( passenger->pcdata->clan && passenger->pcdata->clan != shipclan 
                                 && targetok == 0 )
                                     if ( nifty_is_name(passenger->pcdata->clan->name , shipclan->atwar ) )
                                           targetok = -1;
                              }
                     }
                     
                     if ( targetok == 1 && target->target )     
                     { 
                           ship->target = target->target; 
                           sprintf( buf , "You are being targetted by %s." , ship->name);  
                           echo_to_cockpit( AT_BLOOD , target->target , buf );
                           break;
                     }

		     if ( targetok == 0 && target->target ) 
                     {
                          if ( !str_cmp( target->target->owner , shipclan->name ) )
			      targetok = -1;
                          else if ( nifty_is_name( target->owner , shipclan->atwar ) )
			      targetok = -1;
                          else 
                            for ( room = target->target->first_room ; room ; room = room->next_in_ship )       
                              for ( passenger = room->first_person ; passenger ; passenger = passenger->next_in_room )
                                if ( !IS_NPC(passenger) && passenger->pcdata && passenger->pcdata->clan == shipclan )
                                     targetok = -1;
                     }
                     
                     if ( targetok >= 0 ) 
                             continue;
                      
                      ship->target = target; 
                      sprintf( buf , "You are being scanned by %s." , ship->name);  
                      echo_to_cockpit( AT_WHITE , target , buf );
                      sprintf( buf , "You are being targetted by %s." , ship->name);  
                      echo_to_cockpit( AT_BLOOD , target , buf );

                      break;

                   }
                 
             }
          }   
          else if ( ( ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION 
				|| ship->type == MOB_SHIP ) && ship->home )
          {
               if ( number_range(1, 25) == 25 )
               {
          	  ship_to_starsystem(ship, starsystem_from_name(ship->home) );  
          	  ship->vx = number_range( -5000 , 5000 );
          	  ship->vy = number_range( -5000 , 5000 );
          	  ship->vz = number_range( -5000 , 5000 );
                  ship->hx = 1;
                  ship->hy = 1;
                  ship->hz = 1;
                  ship->autopilot = TRUE;
               }
          }   
       }
       
       if ( ( ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION )
       && ship->target == NULL )
       {
          if( ship->missiles < ship->maxmissiles )
             ship->missiles++;
       }
   }

}



void write_starsystem_list( )
{
    SPACE_DATA *tstarsystem;
    FILE *fpout;
    char filename[256];
    
    sprintf( filename, "%s%s", SPACE_DIR, SPACE_LIST );
    fpout = fopen( filename, "w" );
    if ( !fpout )
    {
         bug( "FATAL: cannot open starsystem.lst for writing!\n\r", 0 );
         return;
    }
    for ( tstarsystem = first_starsystem; tstarsystem; tstarsystem = tstarsystem->next )
    fprintf( fpout, "%s\n", tstarsystem->filename );
    fprintf( fpout, "$\n" );
    fclose( fpout );
}
                                                                    

/*
 * Get pointer to space structure from starsystem name.
 */
SPACE_DATA *starsystem_from_name( char *name )
{
    SPACE_DATA *starsystem;
    
    for ( starsystem = first_starsystem; starsystem; starsystem = starsystem->next )
       if ( !str_cmp( name, starsystem->name ) )
         return starsystem;
    
    for ( starsystem = first_starsystem; starsystem; starsystem = starsystem->next )
       if ( !str_prefix( name, starsystem->name ) )
         return starsystem;
    
    return NULL;
}

/*
 * Get pointer to space structure from the dock vnun.
 */
SPACE_DATA *starsystem_from_room( ROOM_INDEX_DATA * room )
{
    SHIP_DATA * ship;
    ROOM_INDEX_DATA * sRoom;
        
    if ( room == NULL )
        return NULL;
    
    if ( room->area != NULL && room->area->planet != NULL )
          return room->area->planet->starsystem;

    for ( ship = first_ship; ship; ship = ship->next )
        for ( sRoom = ship->first_room ; sRoom ; sRoom = sRoom->next_in_ship )
           if ( room == sRoom )
              return ship->starsystem;
                                
    return NULL;
}


/*
 * Save a starsystem's data to its data file
 */
void save_starsystem( SPACE_DATA *starsystem )
{
    FILE *fp;
    char filename[256];
    char buf[MAX_STRING_LENGTH];

    if ( !starsystem )
    {
	bug( "save_starsystem: null starsystem pointer!", 0 );
	return;
    }
        
    if ( !starsystem->filename || starsystem->filename[0] == '\0' )
    {
	sprintf( buf, "save_starsystem: %s has no filename", starsystem->name );
	bug( buf, 0 );
	return;
    }
 
    sprintf( filename, "%s%s", SPACE_DIR, starsystem->filename );
    
    fclose( fpReserve );
    if ( ( fp = fopen( filename, "w" ) ) == NULL )
    {
    	bug( "save_starsystem: fopen", 0 );
    	perror( filename );
    }
    else
    {
	fprintf( fp, "#SPACE\n" );
	fprintf( fp, "Name         %s~\n",	starsystem->name	);
	fprintf( fp, "Filename     %s~\n",	starsystem->filename	);
	fprintf( fp, "Star1        %s~\n",	starsystem->star1	);
	fprintf( fp, "Star2        %s~\n",	starsystem->star2	);
	fprintf( fp, "S1x          %d\n",       starsystem->s1x         );
	fprintf( fp, "S1y          %d\n",       starsystem->s1y         );
	fprintf( fp, "S1z          %d\n",       starsystem->s1z         );
	fprintf( fp, "S2x          %d\n",       starsystem->s2x         );
	fprintf( fp, "S2y          %d\n",       starsystem->s2y         );
	fprintf( fp, "S2z          %d\n",       starsystem->s2z         );
	fprintf( fp, "End\n\n"						);
	fprintf( fp, "#END\n"						);
    }
    fclose( fp );
    fpReserve = fopen( NULL_FILE, "r" );
    return;
}


/*
 * Read in actual starsystem data.
 */

#if defined(KEY)
#undef KEY
#endif

#define KEY( literal, field, value )					\
				if ( !str_cmp( word, literal ) )	\
				{					\
				    field  = value;			\
				    fMatch = TRUE;			\
				    break;				\
				}

void fread_starsystem( SPACE_DATA *starsystem, FILE *fp )
{
    char buf[MAX_STRING_LENGTH];
    char *word;
    bool fMatch;

 
    for ( ; ; )
    {
	word   = feof( fp ) ? "End" : fread_word( fp );
	fMatch = FALSE;

	switch ( UPPER(word[0]) )
	{
	case '*':
	    fMatch = TRUE;
	    fread_to_eol( fp );
	    break;

	case 'E':
	    if ( !str_cmp( word, "End" ) )
	    {
		if (!starsystem->name)
		  starsystem->name		= STRALLOC( "" );
		if (!starsystem->star1)
		  starsystem->star1            = STRALLOC( "" );  
		if (!starsystem->star2)
		  starsystem->star2            = STRALLOC( "" );
		return;
	    }
	    break;
	    
	case 'F':
	    KEY( "Filename",	starsystem->filename,		fread_string_nohash( fp ) );
	    break;
        
	case 'N':
	    KEY( "Name",	starsystem->name,		fread_string( fp ) );
	    break;
        
       	
       	case 'S':
       	     KEY( "Star1",	starsystem->star1,	fread_string( fp ) );
	     KEY( "Star2",	starsystem->star2,	fread_string( fp ) );
	     KEY( "S1x",  starsystem->s1x,          fread_number( fp ) ); 
             KEY( "S1y",  starsystem->s1y,          fread_number( fp ) ); 
             KEY( "S1z",  starsystem->s1z,          fread_number( fp ) ); 
             KEY( "S2x",  starsystem->s2x,          fread_number( fp ) ); 
             KEY( "S2y",  starsystem->s2y,          fread_number( fp ) );
             KEY( "S2z",  starsystem->s2z,          fread_number( fp ) );
            
       	}
	
	if ( !fMatch )
	{
	    sprintf( buf, "Fread_starsystem: no match: %s", word );
	    bug( buf, 0 );
	}
    }
}

/*
 * Load a starsystem file
 */

bool load_starsystem( char *starsystemfile )
{
    char filename[256];
    SPACE_DATA *starsystem;
    FILE *fp;
    bool found;

    CREATE( starsystem, SPACE_DATA, 1 );
    starsystem->first_planet = NULL;
    starsystem->last_planet = NULL;
    starsystem->first_ship = NULL;
    starsystem->last_ship = NULL;
    starsystem->first_missile = NULL;
    starsystem->last_missile = NULL;

    found = FALSE;
    sprintf( filename, "%s%s", SPACE_DIR, starsystemfile );

    if ( ( fp = fopen( filename, "r" ) ) != NULL )
    {

	found = TRUE;
        LINK( starsystem, first_starsystem, last_starsystem, next, prev );
	for ( ; ; )
	{
	    char letter;
	    char *word;

	    letter = fread_letter( fp );
	    if ( letter == '*' )
	    {
		fread_to_eol( fp );
		continue;
	    }

	    if ( letter != '#' )
	    {
		bug( "Load_starsystem_file: # not found.", 0 );
		break;
	    }

	    word = fread_word( fp );
	    if ( !str_cmp( word, "SPACE"	) )
	    {
	    	fread_starsystem( starsystem, fp );
	    	break;
	    }
	    else
	    if ( !str_cmp( word, "END"	) )
	        break;
	    else
	    {
		char buf[MAX_STRING_LENGTH];

		sprintf( buf, "Load_starsystem_file: bad section: %s.", word );
		bug( buf, 0 );
		break;
	    }
	}
	fclose( fp );
    }

    if ( !(found) )
      DISPOSE( starsystem );

    return found;
}

/*
 * Load in all the starsystem files.
 */
void load_space( )
{
    FILE *fpList;
    char *filename;
    char starsystemlist[256];
    char buf[MAX_STRING_LENGTH];
    
    
    first_starsystem	= NULL;
    last_starsystem	= NULL;

    log_string( "Loading space..." );

    sprintf( starsystemlist, "%s%s", SPACE_DIR, SPACE_LIST );
    fclose( fpReserve );
    if ( ( fpList = fopen( starsystemlist, "r" ) ) == NULL )
    {
	perror( starsystemlist );
	exit( 1 );
    }

    for ( ; ; )
    {
	filename = feof( fpList ) ? "$" : fread_word( fpList );
	if ( filename[0] == '$' )
	  break;
	  
       
	if ( !load_starsystem( filename ) )
	{
	  sprintf( buf, "Cannot load starsystem file: %s", filename );
	  bug( buf, 0 );
	}
    }
    fclose( fpList );
    log_string(" Done starsystems " );
    fpReserve = fopen( NULL_FILE, "r" );
    return;
}

void do_setstarsystem( CHAR_DATA *ch, char *argument )
{
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    SPACE_DATA *starsystem;

    if ( IS_NPC( ch ) )
    {
	send_to_char( "Huh?\n\r", ch );
	return;
    }

    argument = one_argument( argument, arg1 );
    argument = one_argument( argument, arg2 );

    if ( arg2[0] == '\0' || arg1[0] == '\0' )
    {
	send_to_char( "Usage: setstarsystem <starsystem> <field> <values>\n\r", ch );
	send_to_char( "\n\rField being one of:\n\r", ch );
	send_to_char( "name filename\n\r", ch );
	send_to_char( "star1 s1x s1y s1z\n\r", ch );
	send_to_char( "star2 s2x s2y s2z\n\r", ch );
	send_to_char( "", ch );
	return;
    }

    starsystem = starsystem_from_name( arg1 );
    if ( !starsystem )
    {
	send_to_char( "No such starsystem.\n\r", ch );
	return;
    }
    
    if ( !str_cmp( arg2, "s1x" ) )
    {
	starsystem->s1x = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }
    if ( !str_cmp( arg2, "s1y" ) )
    {
	starsystem->s1y = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }
    if ( !str_cmp( arg2, "s1z" ) )
    {
	starsystem->s1z = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }

    if ( !str_cmp( arg2, "s2x" ) )
    {
	starsystem->s2x = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }
    if ( !str_cmp( arg2, "s2y" ) )
    {
	starsystem->s2y = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }
    if ( !str_cmp( arg2, "s2z" ) )
    {
	starsystem->s2z = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }


    if ( !str_cmp( arg2, "name" ) )
    {
	STRFREE( starsystem->name );
	starsystem->name = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }
    
    if ( !str_cmp( arg2, "star1" ) )
    {
	STRFREE( starsystem->star1 );
	starsystem->star1 = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }
    
    if ( !str_cmp( arg2, "star2" ) )
    {
	STRFREE( starsystem->star2 );
	starsystem->star2 = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_starsystem( starsystem );
	return;
    }
    
    do_setstarsystem( ch, "" );
    return;
}

void showstarsystem( CHAR_DATA *ch , SPACE_DATA *starsystem )
{   
    PLANET_DATA * planet;
    
    ch_printf( ch, "Starsystem:%s     Filename: %s\n\r",
    			starsystem->name,
    			starsystem->filename);
    ch_printf( ch, "Star1: %s   Coordinates: %d %d %d\n\r",
    			starsystem->star1,
    			starsystem->s1x , starsystem->s1y, starsystem->s1z);
    ch_printf( ch, "Star2: %s   Coordinates: %d %d %d\n\r",
    			starsystem->star2,
    			starsystem->s2x , starsystem->s2y, starsystem->s2z);
    for ( planet = starsystem->first_planet ; planet ; planet = planet->next_in_system ) 
        ch_printf( ch, "Planet: %s   Coordinates: %d %d %d\n\r",
    			planet->name, 
    			planet->x , planet->y, planet->z);
    return;
}

void do_showstarsystem( CHAR_DATA *ch, char *argument )
{
   SPACE_DATA *starsystem;

   starsystem = starsystem_from_name( argument );
   
   if ( starsystem == NULL )
      send_to_char("&RNo such starsystem.\n\r",ch);
   else
      showstarsystem(ch , starsystem);
   
}

void do_starsystems( CHAR_DATA *ch, char *argument )
{
    SPACE_DATA *starsystem;
    int count = 0;

    for ( starsystem = first_starsystem; starsystem; starsystem = starsystem->next )
    {
        set_char_color( AT_NOTE, ch );
        ch_printf( ch, "%s\n\r", starsystem->name );
        count++;
    }

    if ( !count )
    {
        send_to_char( "There are no starsystems currently formed.\n\r", ch );
	return;
    }
}
   
void echo_to_ship( int color , SHIP_DATA *ship , char *argument )
{
     ROOM_INDEX_DATA * room;
     
     for ( room = ship->first_room ; room ; room = room->next_in_ship )
         echo_to_room( color , room , argument );
     
}

void echo_to_cockpit( int color , SHIP_DATA *ship , char *argument )
{
     TURRET_DATA * turret;

     echo_to_room( color , ship->pilotseat , argument );
     if ( ship->pilotseat != ship->gunseat )
        echo_to_room( color , ship->gunseat , argument );
     if ( ship->pilotseat != ship->viewscreen && ship->viewscreen != ship->gunseat )
        echo_to_room( color , ship->viewscreen , argument );

     for ( turret = ship->first_turret ; turret ; turret = turret->next )
        if ( turret->room )
            echo_to_room( color , turret->room , argument );
}

void echo_to_system( int color , SHIP_DATA *ship , char *argument , SHIP_DATA *ignore )
{
     SHIP_DATA *target;
     
     if (!ship->starsystem)
        return;
      
     for ( target = ship->starsystem->first_ship; target; target = target->next_in_starsystem )
     {
       if (target != ship && target != ignore )  
         echo_to_cockpit( color , target , argument );
     }  
     
}

bool is_facing( SHIP_DATA *ship , SHIP_DATA *target )
{
    	float dy, dx, dz, hx, hy, hz;
        float cosofa;
     	     	
     	hx = ship->hx;
     	hy = ship->hy;
     	hz = ship->hz;
     	     				
     	     					dx = target->vx - ship->vx;
     	     						dy = target->vy - ship->vy;
     	     							dz = target->vz - ship->vz;
     	     							
     	    cosofa = ( hx*dx + hy*dy + hz*dz ) 
     	     		/ ( sqrt(hx*hx+hy*hy+hz*hz) + sqrt(dx*dx+dy*dy+dz*dz) );
     	     								               
     	           	if ( cosofa > 0.75 )
     	                  	             return TRUE;
     	     								               	             
     	     								 return FALSE;
     	}
     	     								               	             	

void write_ship_list( )
{
    SHIP_DATA *tship;
    FILE *fpout;
    char filename[256];
    
    sprintf( filename, "%s%s", SHIP_DIR, SHIP_LIST );
    fpout = fopen( filename, "w" );
    if ( !fpout )
    {
         bug( "FATAL: cannot open ship.lst for writing!\n\r", 0 );
         return;
    }

	/* Lets sort ships by class, so that we don't have ships trying to dock in ships that	*
	 * don't exist yet -Lews 3/02											*/

/* Space Stations */
    for ( tship = first_ship; tship; tship = tship->next )
    if ( tship->class == SPACE_STATION )
      fprintf( fpout, "%s\n", tship->filename );
/* Capital Ships */
    for ( tship = first_ship; tship; tship = tship->next )
    if ( ( tship->type != MOB_SHIP && tship->class == CAPITAL_SHIP ) )
      fprintf( fpout, "%s\n", tship->filename );
/* the rest of the ships */
    for ( tship = first_ship; tship; tship = tship->next )
    if ( tship->type != MOB_SHIP && tship->owner && tship->owner[0] != '\0' 
		&& tship->class != SPACE_STATION && tship->class != CAPITAL_SHIP )
      fprintf( fpout, "%s\n", tship->filename );

    fprintf( fpout, "$\n" );
    fclose( fpout );
}
                                                                    
SHIP_DATA * ship_in_room( ROOM_INDEX_DATA *room, char *name )
{
    SHIP_DATA *ship;

    if ( !room )
     return NULL;

    /* make capital class ships always visible -lews 11/01 */
    for ( ship = room->first_ship ; ship ; ship = ship->next_in_room )
     if ( ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION )
       return ship;   

    for ( ship = room->first_ship ; ship ; ship = ship->next_in_room )
     if ( !str_cmp( name, ship->name ) )
      if ( ship->owner && ship->owner[0] != '\0' )
       if ( get_clan( ship->owner ) || is_online( ship->owner )
       || is_online( ship->pilot ) || is_online( ship->copilot ) 
       || ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION )  
         return ship;
    
    for ( ship = room->first_ship ; ship ; ship = ship->next_in_room )
     if ( nifty_is_name( name, ship->name ) )
      if ( ship->owner && ship->owner[0] != '\0' )
       if ( get_clan( ship->owner ) || is_online( ship->owner )
       || is_online( ship->pilot ) || is_online( ship->copilot )
       || ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION )  
         return ship;

    for ( ship = room->first_ship ; ship ; ship = ship->next_in_room )
     if ( !str_prefix( name, ship->name ) )
      if ( ship->owner && ship->owner[0] != '\0' )
       if ( get_clan( ship->owner ) || is_online( ship->owner )
       || is_online( ship->pilot ) || is_online( ship->copilot )
       || ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION )  
         return ship;
    
    for ( ship = room->first_ship ; ship ; ship = ship->next_in_room )
     if ( nifty_is_name_prefix( name, ship->name ) )
      if ( ship->owner && ship->owner[0] != '\0' )
       if ( get_clan( ship->owner ) || is_online( ship->owner )
       || is_online( ship->pilot ) || is_online( ship->copilot )
       || ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION )  
         return ship;

    return NULL;    
}

/*
 * Get pointer to ship structure from ship name.
 */
SHIP_DATA *get_ship( char *name )
{
    SHIP_DATA *ship;
    
    if( name == NULL )
    return NULL;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( !str_cmp( name, ship->name ) )
         return ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( nifty_is_name( name, ship->name ) )
         return ship;

    for ( ship = first_ship; ship; ship = ship->next )
       if ( !str_prefix( name, ship->name ) )
         return ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( nifty_is_name_prefix( name, ship->name ) )
         return ship;
    
    return NULL;
}

/* 
 * Gets the pointer to the ships hanger structure from ship name
 */
ROOM_INDEX_DATA *get_ship_hanger( char *name )
{
  SHIP_DATA *ship;
  ROOM_INDEX_DATA *hanger;

  ship = get_ship( name );
  hanger = ship->hanger;
  return hanger;
}  

/*
 * Checks if ships in a starsystem and returns poiner if it is.
 */
SHIP_DATA *get_ship_here( char *name , SPACE_DATA *starsystem)
{
    SHIP_DATA *ship;
    
    if ( starsystem == NULL )
         return NULL;
    
    for ( ship = starsystem->first_ship ; ship; ship = ship->next_in_starsystem )
       if ( !str_cmp( name, ship->name ) )
         return ship;
    
    for ( ship = starsystem->first_ship; ship; ship = ship->next_in_starsystem )
       if ( nifty_is_name( name, ship->name ) )
         return ship;

    for ( ship = starsystem->first_ship ; ship; ship = ship->next_in_starsystem )
       if ( !str_prefix( name, ship->name ) )
         return ship;
    
    for ( ship = starsystem->first_ship; ship; ship = ship->next_in_starsystem )
       if ( nifty_is_name_prefix( name, ship->name ) )
         return ship;
    
    return NULL;
}


SHIP_DATA *ship_from_pilot( char *name )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( !str_cmp( name, ship->pilot ) )
         return ship;
       if ( !str_cmp( name, ship->copilot ) )
         return ship;
       if ( !str_cmp( name, ship->owner ) )
         return ship;  
    return NULL;
}


/*
 * Get pointer to ship structure from cockpit, or entrance ramp vnum.
 */
 
SHIP_DATA *ship_from_room( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;
   ROOM_INDEX_DATA *sRoom;
   
   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
     for ( sRoom = ship->first_room ; sRoom ; sRoom = sRoom->next_in_ship ) 
        if ( room == sRoom )
              return ship;

    return NULL;
}

SHIP_DATA *ship_from_cockpit( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;
   TURRET_DATA *turret;
   
   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
   {
        if ( room == ship->pilotseat )
              return ship;
        if ( room == ship->gunseat )
              return ship;
        if ( room == ship->viewscreen )
              return ship;
        for ( turret = ship->first_turret ; turret ; turret = turret->next )
            if ( room == turret->room )
                  return ship;
   }
   
   return NULL;
}

SHIP_DATA *ship_from_pilotseat( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;

   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
        if ( room == ship->pilotseat )
              return ship;
   
   return NULL;
}

SHIP_DATA *ship_from_gunseat( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;

   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
        if ( room == ship->gunseat )
              return ship;
   
   return NULL;
}

SHIP_DATA *ship_from_entrance( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;

   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
        if ( room == ship->entrance )
              return ship;

    return NULL;
}
SHIP_DATA *ship_from_engine( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;

   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
        if ( room == ship->engine )
              return ship;

    return NULL;
}

SHIP_DATA *ship_from_turret( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;
   TURRET_DATA *turret;
   
   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
   {
        if ( room == ship->gunseat )
              return ship;
        for ( turret = ship->first_turret ; turret ; turret = turret->next )
            if ( room == turret->room )
                  return ship;
   }
   
    return NULL;
}


SHIP_DATA *ship_from_hanger( ROOM_INDEX_DATA * room )
{
   SHIP_DATA *ship;

   if ( room == NULL )
      return NULL;
   
   for ( ship = first_ship; ship; ship = ship->next )
        if ( room == ship->hanger )
              return ship;

    return NULL;
}

void save_ship( SHIP_DATA *ship )
{
    FILE *fp;
    char filename[256];
    char buf[MAX_STRING_LENGTH];
    ROOM_INDEX_DATA *room;

    if ( !ship )
    {
	bug( "save_ship: null ship pointer!", 0 );
	return;
    }

    if ( ship->type == MOB_SHIP )
       return;
        
    if ( !ship->filename || ship->filename[0] == '\0' )
    {
	sprintf( buf, "save_ship: %s has no filename", ship->name );
	bug( buf, 0 );
	return;
    }
 
    sprintf( filename, "%s%s", SHIP_DIR, ship->filename );
    
    fclose( fpReserve );
    if ( ( fp = fopen( filename, "w" ) ) == NULL )
    {
    	bug( "save_ship: fopen", 0 );
    	perror( filename );
    }
    else
    {
	fprintf( fp, "#SHIP\n" );
	fprintf( fp, "Name         %s~\n",	ship->name		);
	fprintf( fp, "Filename     %s~\n",	ship->filename		);
        for ( room = ship->first_room ; room ; room = room->next_in_ship )
            fprintf( fp, "Description  %s~\n",	room->description );
	fprintf( fp, "Owner        %s~\n",	ship->owner		);
	fprintf( fp, "Pilot        %s~\n",      ship->pilot             );
	fprintf( fp, "Copilot      %s~\n",      ship->copilot           );
	fprintf( fp, "Model        %d\n",	ship->model		);
	fprintf( fp, "Class        %d\n",	ship->class		);
	fprintf( fp, "Tractorbeam  %d\n",	ship->tractorbeam	);
	fprintf( fp, "Shipyard     %d\n",	ship->shipyard		);
	fprintf( fp, "Laserstate   %d\n",	ship->laserstate	);
	fprintf( fp, "Lasers       %d\n",	ship->lasers    	);
	fprintf( fp, "Missiles     %d\n",	ship->missiles		);
	fprintf( fp, "Maxmissiles  %d\n",	ship->maxmissiles	);
	fprintf( fp, "Lastdoc      %d\n",	ship->lastdoc		);
	fprintf( fp, "On_ship      %s~\n",	ship->on_ship		);
	fprintf( fp, "Shield       %d\n",	ship->shield		);
	fprintf( fp, "Maxshield    %d\n",	ship->maxshield		);
	fprintf( fp, "Hull         %d\n",	ship->hull		);
	fprintf( fp, "Maxhull      %d\n",	ship->maxhull		);
	fprintf( fp, "Maxenergy    %d\n",	ship->maxenergy		);
	fprintf( fp, "Hyperspeed   %d\n",	ship->hyperspeed	);
	fprintf( fp, "Chaff        %d\n",	ship->chaff		);
	fprintf( fp, "Maxchaff     %d\n",	ship->maxchaff		);
	fprintf( fp, "Realspeed    %d\n",	ship->realspeed		);
	fprintf( fp, "Type         %d\n",	ship->type		);
	fprintf( fp, "Shipstate    %d\n",	ship->shipstate		);
	fprintf( fp, "Missilestate %d\n",	ship->missilestate	);
	fprintf( fp, "Energy       %d\n",	ship->energy		);
	fprintf( fp, "Manuever     %d\n",       ship->manuever          );
	fprintf( fp, "Home         %s~\n",      ship->home              );
	fprintf( fp, "End\n\n"						);
	fprintf( fp, "#END\n"						);
    }
    fclose( fp );
    fpReserve = fopen( NULL_FILE, "r" );

    write_ship_list( );

    return;
}


/*
 * Read in actual ship data.
 */

#if defined(KEY)
#undef KEY
#endif

#define KEY( literal, field, value )					\
				if ( !str_cmp( word, literal ) )	\
				{					\
				    field  = value;			\
				    fMatch = TRUE;			\
				    break;				\
				}

void fread_ship( SHIP_DATA *ship, FILE *fp )
{
    char buf[MAX_STRING_LENGTH];
    char *word;
    bool fMatch;
    int dIndex = 0;
 
    for ( ; ; )
    {
	word   = feof( fp ) ? "End" : fread_word( fp );
	fMatch = FALSE;
        
	switch ( UPPER(word[0]) )
	{
	case '*':
	    fMatch = TRUE;
	    fread_to_eol( fp );
	    break;
        
        
        case 'C':
             KEY( "Class",       ship->class,            fread_number( fp ) );
             KEY( "Copilot",     ship->copilot,          fread_string( fp ) );
             KEY( "Chaff",       ship->chaff,      fread_number( fp ) );
             break;
                                

	case 'D':
	    if ( dIndex < MAX_SHIP_ROOMS )
	         KEY( "Description",	   ship->description[dIndex++],	fread_string( fp ) );
	    break;

	case 'E':
	    KEY( "Energy",      ship->energy,        fread_number( fp ) );
	    if ( !str_cmp( word, "End" ) )
	    {
		if (!ship->home)
		  ship->home		= STRALLOC( "" );
		if (!ship->on_ship)
		  ship->on_ship		= STRALLOC( "" );
		if (!ship->name)
		  ship->name		= STRALLOC( "" );
		if (!ship->owner)
		  ship->owner		= STRALLOC( "" );
		if (!ship->copilot)
		  ship->copilot 	= STRALLOC( "" );
		if (!ship->pilot)
		  ship->pilot   	= STRALLOC( "" );  
		if (ship->shipstate != SHIP_DISABLED)
		  ship->shipstate = SHIP_DOCKED;
		if (ship->laserstate != LASER_DAMAGED)
		  ship->laserstate = LASER_READY;
		if (ship->missilestate != MISSILE_DAMAGED)
		  ship->missilestate = MISSILE_READY;
		if (ship->shipyard <= 0) 
		  ship->shipyard = ROOM_LIMBO_SHIPYARD;
		if ( ( ship->lastdoc <= 0 ) && ( !ship->on_ship )) 
		  ship->lastdoc = ship->shipyard;
		ship->autopilot   = FALSE;
		ship->hatchopen = FALSE;
		ship->starsystem = NULL;
		ship->energy = ship->maxenergy;
		ship->hull = ship->maxhull;
		ship->in_room=NULL;
                ship->next_in_room=NULL;
                ship->prev_in_room=NULL;
                ship->first_turret=NULL;
                ship->last_turret=NULL;
                ship->first_hanger=NULL;
                ship->last_hanger=NULL;
                ship->hanger=NULL;
                ship->docked=NULL;
		create_ship_rooms(ship);
		return;
	    }
	    break;
	    
	case 'F':
	    KEY( "Filename",	ship->filename,		fread_string_nohash( fp ) );
            break;
        
        case 'H':
            KEY( "Home" , ship->home, fread_string( fp ) );
            KEY( "Hyperspeed",   ship->hyperspeed,      fread_number( fp ) );
            KEY( "Hull",      ship->hull,        fread_number( fp ) );
            break;
        
        case 'L':
            KEY( "Lasers",   ship->lasers,      fread_number( fp ) );
            KEY( "Laserstate",   ship->lasers,      fread_number( fp ) );
            KEY( "Lastdoc",    ship->lastdoc,       fread_number( fp ) );
            break;

        case 'M':
            KEY( "Model",   ship->model,      fread_number( fp ) );
            KEY( "Manuever",   ship->manuever,      fread_number( fp ) );
            KEY( "Maxmissiles",   ship->maxmissiles,      fread_number( fp ) );
            KEY( "Missiles",   ship->missiles,      fread_number( fp ) );
            KEY( "Maxshield",      ship->maxshield,        fread_number( fp ) );
            KEY( "Maxenergy",      ship->maxenergy,        fread_number( fp ) );
            KEY( "Missilestate",   ship->missilestate,        fread_number( fp ) );
            KEY( "Maxhull",      ship->maxhull,        fread_number( fp ) );
            KEY( "Maxchaff",       ship->maxchaff,      fread_number( fp ) );
             break;

	case 'N':
	    KEY( "Name",	ship->name,		fread_string( fp ) );
            break;
  
        case 'O':
            KEY( "Owner",            ship->owner,            fread_string( fp ) );
            KEY( "On_ship",          ship->on_ship,            fread_string( fp ) );
            break;
        
        case 'P':
            KEY( "Pilot",            ship->pilot,            fread_string( fp ) ); 
            break;
        
        case 'R':
            KEY( "Realspeed",   ship->realspeed,       fread_number( fp ) );
            break;
       
        case 'S':
            KEY( "Shipyard",    ship->shipyard,      fread_number( fp ) );
            KEY( "Shield",      ship->shield,        fread_number( fp ) );
            KEY( "Shipstate",   ship->shipstate,        fread_number( fp ) );
            break;

	case 'T':
	    KEY( "Type",	ship->type,	fread_number( fp ) );
	    KEY( "Tractorbeam", ship->tractorbeam,      fread_number( fp ) );
	    break;
	}
	
	if ( !fMatch )
	{
	    sprintf( buf, "Fread_ship: no match: %s", word );
	    bug( buf, 0 );
	}
    }
}

/*
 * Load a ship file
 */

bool load_ship_file( char *shipfile )
{
    char filename[256];
    SHIP_DATA *ship;
    FILE *fp;
    bool found;
    CLAN_DATA *clan;
    SHIP_DATA *docked = NULL;
        
    CREATE( ship, SHIP_DATA, 1 );

    found = FALSE;
    sprintf( filename, "%s%s", SHIP_DIR, shipfile );

    if ( ( fp = fopen( filename, "r" ) ) != NULL )
    {

	found = TRUE;
	for ( ; ; )
	{
	    char letter;
	    char *word;

	    letter = fread_letter( fp );
	    if ( letter == '*' )
	    {
		fread_to_eol( fp );
		continue;
	    }

	    if ( letter != '#' )
	    {
		bug( "Load_ship_file: # not found.", 0 );
		break;
	    }

	    word = fread_word( fp );
	    if ( !str_cmp( word, "SHIP"	) )
	    {
	    	fread_ship( ship, fp );
	    	break;
	    }
	    else
	    if ( !str_cmp( word, "END"	) )
	        break;
	    else
	    {
		char buf[MAX_STRING_LENGTH];

		sprintf( buf, "Load_ship_file: bad section: %s.", word );
		bug( buf, 0 );
		break;
	    }
	}
	fclose( fp );
    }
    if ( !(found) )
      DISPOSE( ship );
    else
    {      
       LINK( ship, first_ship, last_ship, next, prev );

       if ( ship->class == SPACE_STATION || ship->type == MOB_SHIP 
		|| ( ship->class == CAPITAL_SHIP && ( ship->home != STRALLOC( "" ) ) ) )
       {
       
     		ship->currspeed=0;
    		ship->energy=ship->maxenergy;
    	 	ship->chaff=ship->maxchaff;
    	 	ship->hull=ship->maxhull;
    	 	ship->shield=0;
     
   	  	ship->laserstate = LASER_READY; 
   	 	ship->missilestate = LASER_READY;
       
   	  	ship->currjump=NULL;
    	 	ship->target=NULL;
     
    	 	ship->hatchopen = FALSE;
     
    	 	ship->missiles = ship->maxmissiles;
    	 	ship->autorecharge = FALSE;
   	  	ship->autotrack = FALSE;
    	 	ship->autospeed = FALSE;
     
     	     ship_to_starsystem(ship, starsystem_from_name(ship->home) );  
     	     ship->vx = number_range( -5000 , 5000 );
     	     ship->vy = number_range( -5000 , 5000 );
     	     ship->vz = number_range( -5000 , 5000 );
     	     ship->hx = 1;
     	     ship->hy = 1;
     	     ship->hz = 1;
     	     ship->shipstate = SHIP_READY;
     	     ship->autopilot = TRUE;
     	     ship->autorecharge = TRUE;
     	     ship->shield = ship->maxshield;
       
     	    }
     	    else
	    { 
		if( ship->on_ship != STRALLOC( "" ))
		{
             docked = get_ship(ship->on_ship);
             ship_to_hanger( ship, docked->hanger ); /* put the ship in the hanger.. */
             ship->location = 0;
             ship->lastdoc =0;
         	}
         	else
         	{
		  if( ship->lastdoc != 0 )
         	  {
               ship_to_room( ship , ship->lastdoc ); /* ..or put the ship in the last docked room  -Lews */
               ship->location = ship->lastdoc;
               ship->docked = NULL;
		  }
		  else
		  {
               ship_to_room( ship , ship->shipyard ); /* ..or put the ship in the shipyard  -Lews */
               ship->location = ship->shipyard;
               ship->docked = NULL;
		  }
		}

	     }
              
       if ( ship->type != MOB_SHIP && (clan = get_clan( ship->owner )) != NULL )
       {
          if ( ship->class <= SPACE_STATION )
             clan->spacecraft++;
          else
             clan->vehicles++;
       }  
         
    }
    
    return found;
}

/*
 * Load in all the ship files.
 */
void load_ships( )
{
    FILE *fpList;
    char *filename;
    char shiplist[256];
    char buf[MAX_STRING_LENGTH];
    
    
    first_ship	= NULL;
    last_ship	= NULL;
    first_missile = NULL;
    last_missile = NULL;
    
    log_string( "Loading ships..." );

    sprintf( shiplist, "%s%s", SHIP_DIR, SHIP_LIST );
    fclose( fpReserve );
    if ( ( fpList = fopen( shiplist, "r" ) ) == NULL )
    {
	perror( shiplist );
	exit( 1 );
    }

    for ( ; ; )
    {
    
	filename = feof( fpList ) ? "$" : fread_word( fpList );

	if ( filename[0] == '$' )
	  break;
	         
	if ( !load_ship_file( filename ) )
	{
	  sprintf( buf, "Cannot load ship file: %s", filename );
	  bug( buf, 0 );
	}

    }
    fclose( fpList );
    log_string(" Done ships " );
    fpReserve = fopen( NULL_FILE, "r" );
    return;
}

void resetship( SHIP_DATA *ship )
{
     ship->shipstate = SHIP_READY;
     
     if ( ship->class != SPACE_STATION && ship->type != MOB_SHIP  && 
          ( ship->class != CAPITAL_SHIP && ship->home != NULL ) )
     {
           extract_ship( ship );
           ship_to_room( ship , ship->shipyard ); 
           ship->location = ship->shipyard;
           ship->shipstate = SHIP_DOCKED;
           STRFREE( ship->home );
           ship->home = STRALLOC( "" );
     }
     
     if (ship->starsystem)
        ship_from_starsystem( ship, ship->starsystem );  
  
     ship->currspeed=0;
     ship->energy=ship->maxenergy;
     ship->chaff=ship->maxchaff;
     ship->hull=ship->maxhull;
     ship->shield=0;
     
     ship->laserstate = LASER_READY; 
     ship->missilestate = LASER_READY;
       
     ship->currjump=NULL;
     ship->target=NULL;
     
     ship->hatchopen = TRUE;
     STRFREE( ship->on_ship );
     ship->on_ship = STRALLOC( "" );
     
     ship->missiles = ship->maxmissiles;
     ship->autorecharge = FALSE;
     ship->autotrack = FALSE;
     ship->autospeed = FALSE;
     
     save_ship(ship);               
}

void do_resetship( CHAR_DATA *ch, char *argument )
{    
     SHIP_DATA *ship;
     
     ship = get_ship( argument );
     if (ship == NULL)
     {
        send_to_char("&RNo such ship!",ch);
        return;
     } 
     
     resetship( ship ); 
     
     if ( ( ship->class == SPACE_STATION || ship->class == CAPITAL_SHIP || ship->type == MOB_SHIP )
		&& ship->home )
     {
          ship_to_starsystem(ship, starsystem_from_name(ship->home) );  
          ship->vx = number_range( -5000 , 5000 );
          ship->vy = number_range( -5000 , 5000 );
          ship->vz = number_range( -5000 , 5000 );
          ship->shipstate = SHIP_READY;
          ship->autopilot = TRUE;
          ship->autorecharge = TRUE;
          ship->shield = ship->maxshield;
     }
         
}

void do_setship( CHAR_DATA *ch, char *argument )
{
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    SHIP_DATA *ship;
    int  tempnum;
    ROOM_INDEX_DATA *roomindex;
    
    if ( IS_NPC(ch) || !ch->pcdata )
    	return;

    if ( !ch->desc )
	return;

    argument = one_argument( argument, arg1 );
    argument = one_argument( argument, arg2 );

    if ( arg1[0] == '\0' || arg2[0] == '\0' || arg1[0] == '\0' )
    {
	send_to_char( "Usage: setship <ship> <field> <values>\n\r", ch );
	send_to_char( "\n\rField being one of:\n\r", ch );
	send_to_char( "name owner copilot pilot home\n\r", ch );
	send_to_char( "manuever speed hyperspeed tractorbeam\n\r", ch );
	send_to_char( "lasers missiles shield hull energy chaff\n\r", ch );
	send_to_char( "lastdoc class onship\n\r", ch );
	return;
    }

    ship = get_ship( arg1 );
    if ( !ship )
    {
	send_to_char( "No such ship.\n\r", ch );
	return;
    }
    
    if ( !str_cmp( arg2, "owner" ) )
    {
         CLAN_DATA *clan;
         if ( ship->type != MOB_SHIP && (clan = get_clan( ship->owner )) != NULL )
         {
          if ( ship->class <= SPACE_STATION )
             clan->spacecraft--;
          else
             clan->vehicles--;
         }
	STRFREE( ship->owner );
	ship->owner = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	if ( ship->type != MOB_SHIP && (clan = get_clan( ship->owner )) != NULL )
         {
          if ( ship->class <= SPACE_STATION )
             clan->spacecraft++;
          else
             clan->vehicles++;
         }
	return;
    }
    
    if ( !str_cmp( arg2, "home" ) )
    {
	STRFREE( ship->home );
	ship->home = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
    
    if ( !str_cmp( arg2, "pilot" ) )
    {
	STRFREE( ship->pilot );
	ship->pilot = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "copilot" ) )
    {
	STRFREE( ship->copilot );
	ship->copilot = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
   

    if ( !str_cmp( arg2, "shipyard" ) )
    {   
        tempnum = atoi(argument); 
    	roomindex = get_room_index(tempnum);
    	if (roomindex == NULL)
    	{
    	   send_to_char("That room doesn't exist.",ch);
    	   return;
    	} 
	ship->shipyard = tempnum;
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "name" ) )
    {
	STRFREE( ship->name );
	ship->name = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "manuever" ) )
    {   
	ship->manuever = URANGE( 0, atoi(argument) , 120 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "lasers" ) )
    {   
	ship->lasers = URANGE( 0, atoi(argument) , 10 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
    
    if ( !str_cmp( arg2, "class" ) )
    {   
	ship->class = URANGE( 0, atoi(argument) , 9 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "lastdoc" ) )
    {   
	ship->lastdoc = URANGE( 0, atoi(argument) , 9 );
	ship->on_ship = STRALLOC( "" );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "onship" ) )
    {
	STRFREE( ship->on_ship );
	ship->on_ship = STRALLOC( argument );
	ship->lastdoc = 0;
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "missiles" ) )
    {   
	ship->maxmissiles = URANGE( 0, atoi(argument) , 255 );
	ship->missiles = URANGE( 0, atoi(argument) , 255 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }

    if ( !str_cmp( arg2, "speed" ) )
    {   
	ship->realspeed = URANGE( 0, atoi(argument) , 150 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
 
    if ( !str_cmp( arg2, "tractorbeam" ) )
    {   
	ship->tractorbeam = URANGE( 0, atoi(argument) , 255 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
 
    if ( !str_cmp( arg2, "hyperspeed" ) )
    {   
	ship->hyperspeed = URANGE( 0, atoi(argument) , 255 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
 
    if ( !str_cmp( arg2, "shield" ) )
    {   
	ship->maxshield = URANGE( 0, atoi(argument) , 1000 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
 
    if ( !str_cmp( arg2, "hull" ) )
    {   
	ship->hull = URANGE( 1, atoi(argument) , 20000 );
	ship->maxhull = URANGE( 1, atoi(argument) , 20000 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
 
    if ( !str_cmp( arg2, "energy" ) )
    {   
	ship->energy = URANGE( 1, atoi(argument) , 30000 );
	ship->maxenergy = URANGE( 1, atoi(argument) , 30000 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
 
    if ( !str_cmp( arg2, "chaff" ) )
    {   
	ship->chaff = URANGE( 0, atoi(argument) , 25 );
	ship->maxchaff = URANGE( 0, atoi(argument) , 25 );
	send_to_char( "Done.\n\r", ch );
	save_ship( ship );
	return;
    }
 
    do_setship( ch, "" );
    return;
}

void do_showship( CHAR_DATA *ch, char *argument )
{   
    SHIP_DATA *ship;

    if ( IS_NPC( ch ) )
    {
	send_to_char( "Huh?\n\r", ch );
	return;
    }

    if ( argument[0] == '\0' )
    {
	send_to_char( "Usage: showship <ship>\n\r", ch );
	return;
    }

    ship = get_ship( argument );
    if ( !ship )
    {
	send_to_char( "No such ship.\n\r", ch );
	return;
    }
    set_char_color( AT_YELLOW, ch );
    ch_printf( ch, "&O%s %s : &Y%s\n\r&OFilename: &Y%s\n\r",
		        ship->type == PLAYER_SHIP ? "Player" : "Mob" ,
		        ship->class == SPACECRAFT ? model[ship->model].name :
		       (ship->class == CAPITAL_SHIP ? model[ship->model].name :
		       (ship->class == SPACE_STATION ? "Space Station" : 
		       (ship->class == AIRCRAFT ? "Aircraft" : 
		       (ship->class == BOAT ? "Boat" : 
		       (ship->class == SUBMARINE ? "Submatine" : 
		       (ship->class == LAND_VEHICLE ? "land vehicle" : "Unknown" ) ) ) ) ) ), 
    			ship->name,
    			ship->filename);
    ch_printf( ch, "&OHome: &Y%s\n\r&OOwner: %s   &OPilot: &Y%s   &OCopilot: &Y%s\n\r",
    			ship->home,
    			ship->owner, ship->pilot,  ship->copilot );
    ch_printf( ch, "&OLocation: &Y%d   &OLastdoc: &Y%d   &OShipyard: &Y%d\n\r",
    			ship->location,
    			ship->lastdoc,
    			ship->shipyard);
    ch_printf( ch, "&ODocked: &Y%s  &OHanger: &Y%s\n\r",
    			ship->on_ship,
			ship->hanger != NULL ? "yes" : "no" );
    ch_printf( ch, "&OTractor Beam: &Y%d  ",
    			ship->tractorbeam);
    ch_printf( ch, "&OLasers : &Y%d  &OLaser Condition: &Y%s\n\r",
    			ship->lasers,
    			ship->laserstate == LASER_DAMAGED ? "Damaged" : "Good");		
    ch_printf( ch, "&OMissiles: &Y%d/%d  &OCondition: &Y%s\n\r",
       			ship->missiles,
    			ship->maxmissiles,
    			ship->missilestate == MISSILE_DAMAGED ? "Damaged" : "Good");		
    ch_printf( ch, "&OHull: &Y%d/%d  &OShip Condition: &Y%s\n\r",
                        ship->hull,
    		        ship->maxhull,	
    			ship->shipstate == SHIP_DISABLED ? "Disabled" : "Running");
    		
    ch_printf( ch, "&OShields: &Y%d/%d   &OEnergy(fuel): &Y%d/%d   &OChaff: &Y%d/%d\n\r",
                        ship->shield,
    		        ship->maxshield,
    		        ship->energy,
    		        ship->maxenergy,
    		        ship->chaff,
    		        ship->maxchaff);
    ch_printf( ch, "&OCurrent Coordinates: &Y%.0f %.0f %.0f\n\r",
                        ship->vx, ship->vy, ship->vz );
    ch_printf( ch, "&OCurrent Heading: &Y%.0f %.0f %.0f\n\r",
                        ship->hx, ship->hy, ship->hz );
    ch_printf( ch, "&OSpeed: &Y%d/%d   &OHyperspeed: &Y%d\n\r  &OManueverability: &Y%d",
                        ship->currspeed, ship->realspeed, ship->hyperspeed , ship->manuever );                    
    return;
}

void do_ships( CHAR_DATA *ch, char *argument )
{
    SHIP_DATA *ship;
    int count;
    
    if ( !IS_NPC(ch) )
    {
      count = 0;
      send_to_char( "&YThe following ships are owned by you or by your organization:\n\r", ch );
      send_to_char( "\n\r&WShip                               Location\n\r",ch);
      for ( ship = first_ship; ship; ship = ship->next )
      {   
        if ( str_cmp(ship->owner, ch->name) )
        {
           if ( !ch->pcdata || !ch->pcdata->clan || str_cmp(ship->owner,ch->pcdata->clan->name) || ship->class > SPACE_STATION )
               continue;
        }
         
        if (ship->type == MOB_SHIP)
           continue;
        set_char_color( AT_BLUE, ch );
        
        if (ship->class == CAPITAL_SHIP)
        	set_char_color( AT_LBLUE, ch );

        if (ship->class == SPACE_STATION)
        	set_char_color( AT_GREEN, ch );

        if  ( ship->in_room )       
        {
          if ( ship->in_room->area && ship->in_room->area->planet )
             ch_printf( ch, " %s \t\t -%s (%s)\n\r", ship->name, ship->in_room->name , ship->in_room->area->planet->name );
          else
             ch_printf( ch, " %s \t\t\t -%s\n\r", ship->name, ship->in_room->name );
        }
        else if ( ship->starsystem )
	  {
 		ch_printf( ch, " %s \t\t\t -%s System\n\r", ship->name, ship->starsystem->name );
	  }
	  else
          ch_printf( ch, " %s\n\r", ship->name );
        
        count++;
      }

      if ( !count )
      {
        send_to_char( "There are no ships owned by you.\n\r", ch );
      }
    
    }

    
}

void do_speeders( CHAR_DATA *ch, char *argument )
{
    SHIP_DATA *ship;
    int count;

    if ( !IS_NPC(ch) )
    {
      count = 0;
      send_to_char( "&YThe following are owned by you or by your organization:\n\r", ch );
      send_to_char( "\n\r&WVehicle                            Location\n\r",ch);
      for ( ship = first_ship; ship; ship = ship->next )
      {   
        if ( str_cmp(ship->owner, ch->name) )
        {
           if ( !ch->pcdata || !ch->pcdata->clan || str_cmp(ship->owner,ch->pcdata->clan->name) || ship->class <= SPACE_STATION )
               continue;
        }
        if ( ship->location != ch->in_room->vnum || ship->class <= SPACE_STATION)
               continue;
               
        if (ship->type == MOB_SHIP)
           continue;
        set_char_color( AT_BLUE, ch );
                
        if  ( ship->in_room )       
        {
          if ( ship->in_room->area && ship->in_room->area && ship->in_room->area->planet )
             ch_printf( ch, "  %s  -%s (%s)\n\r", ship->name, ship->in_room->name, ship->in_room->area->planet->name );
          else
             ch_printf( ch, "  %s  -%s\n\r", ship->name, ship->in_room->name );
        }
        else 
          ch_printf( ch, "  %s\n\r", ship->name );
        
        count++;
      }

      if ( !count )
      {
        send_to_char( "There are no land or air vehicles owned by you.\n\r", ch );
      }
    
    }

    
}

void do_allspeeders( CHAR_DATA *ch, char *argument )
{
    SHIP_DATA *ship;
    int count = 0;

      count = 0;
      send_to_char( "&Y\n\rThe following sea/land/air vehicles are currently formed:\n\r", ch );
    
      send_to_char( "\n\r&WVehicle                            Owner\n\r", ch );
      for ( ship = first_ship; ship; ship = ship->next )
      {   
        if ( ship->class <= SPACE_STATION ) 
           continue; 
      
        if (ship->type == MOB_SHIP)
           continue;
        set_char_color( AT_BLUE, ch );
        
        
        ch_printf( ch, "%-35s %-15s\n\r", ship->name, ship->owner );

        count++;
      }
    
      if ( !count )
      {
        send_to_char( "There are none currently formed.\n\r", ch );
	return;
      }
    
}

void do_allships( CHAR_DATA *ch, char *argument )
{
    SHIP_DATA *ship;
    int count = 0;

      count = 0;
      send_to_char( "&Y\n\rThe following ships are currently formed:\n\r", ch );
    
      send_to_char( "\n\r&WShip                               Owner\n\r", ch );
      
      for ( ship = first_ship; ship; ship = ship->next )
      {   
        if ( ship->class > SPACE_STATION ) 
           continue; 
      
        if (ship->type == MOB_SHIP)
           continue;
        set_char_color( AT_BLUE, ch );
        
        ch_printf( ch, "%-35s %-15s\n\r", ship->name, ship->owner );
        
        count++;
      }
    
      if ( !count )
      {
        send_to_char( "There are no ships currently formed.\n\r", ch );
	return;
      }
    
}


void ship_to_starsystem( SHIP_DATA *ship , SPACE_DATA *starsystem )
{
     if ( starsystem == NULL )
        return;
     
     if ( ship == NULL )
        return;
     
     LINK ( ship, starsystem->first_ship , starsystem->last_ship , next_in_starsystem , prev_in_starsystem );
     
     ship->starsystem = starsystem;
        
}

void new_missile( SHIP_DATA *ship , SHIP_DATA *target , CHAR_DATA *ch  )
{
     SPACE_DATA *starsystem;
     MISSILE_DATA *missile;

     if ( ship  == NULL )
        return;

     if ( target  == NULL )
        return;

     if ( ( starsystem = ship->starsystem ) == NULL )
        return;
          
     CREATE( missile, MISSILE_DATA, 1 );
     LINK( missile, first_missile, last_missile, next, prev );
     
     missile->target = target; 
     missile->fired_from = ship;
     if ( ch )
        missile->fired_by = STRALLOC( ch->name );
     else 
        missile->fired_by = STRALLOC( "" );
     missile->age =0;
     missile->speed = 200;
     
     missile->mx = ship->vx;
     missile->my = ship->vy;
     missile->mz = ship->vz;
            
     if ( starsystem->first_missile == NULL )
        starsystem->first_missile = missile;
     
     if ( starsystem->last_missile )
     {
         starsystem->last_missile->next_in_starsystem = missile;
         missile->prev_in_starsystem = starsystem->last_missile;
     }
     
     starsystem->last_missile = missile;
     
     missile->starsystem = starsystem;
        
}

void ship_from_starsystem( SHIP_DATA *ship , SPACE_DATA *starsystem )
{

     if ( starsystem == NULL )
        return;
     
     if ( ship == NULL )
        return;

     UNLINK ( ship, starsystem->first_ship , starsystem->last_ship , next_in_starsystem , prev_in_starsystem );
     
     ship->starsystem = NULL;

}

void extract_missile( MISSILE_DATA *missile )
{
    SPACE_DATA *starsystem;

     if ( missile == NULL )
        return;

     if ( ( starsystem = missile->starsystem ) != NULL )
     {
     
      if ( starsystem->last_missile == missile )
        starsystem->last_missile = missile->prev_in_starsystem;
        
      if ( starsystem->first_missile == missile )
        starsystem->first_missile = missile->next_in_starsystem;
        
      if ( missile->prev_in_starsystem )
        missile->prev_in_starsystem->next_in_starsystem = missile->next_in_starsystem;
     
      if ( missile->next_in_starsystem)
        missile->next_in_starsystem->prev_in_starsystem = missile->prev_in_starsystem;
        
      missile->starsystem = NULL;
      missile->next_in_starsystem = NULL;
      missile->prev_in_starsystem = NULL;   

     }
     
     UNLINK( missile, first_missile, last_missile, next, prev );
     
     missile->target = NULL; 
     missile->fired_from = NULL;
     if (  missile->fired_by )
        STRFREE( missile->fired_by );
     
     DISPOSE( missile );
          
}

bool check_pilot( CHAR_DATA *ch , SHIP_DATA *ship )
{
   if ( !str_cmp(ch->name,ship->owner) || !str_cmp(ch->name,ship->pilot) 
   || !str_cmp(ch->name,ship->copilot) )
      return TRUE;
   
   if ( !IS_NPC(ch) && ch->pcdata && ch->pcdata->clan )
   {        
      if ( !str_cmp(ch->pcdata->clan->name,ship->owner) ) 
      {
        if ( nifty_is_name(ch->name, ch->pcdata->clan->leader) )
          return TRUE;
        if ( ch->pcdata->bestowments && is_name( "pilot", ch->pcdata->bestowments) )
          return TRUE;
      }
   }
    
   return FALSE;
}

bool extract_ship( SHIP_DATA *ship )
{   
    ROOM_INDEX_DATA *room;
    
    if ( ( room = ship->in_room ) != NULL )
    {               
        UNLINK( ship, room->first_ship, room->last_ship, next_in_room, prev_in_room );
        ship->in_room = NULL;
    }
    return TRUE;
}

void damage_ship_ch( SHIP_DATA *ship , int min , int max , CHAR_DATA *ch )
{   
    int damage , shield_dmg;
     
    damage = number_range( min , max );
    
    if ( ship->shield > 0 )
    {   
        shield_dmg = UMIN( ship->shield , damage );
    	damage -= shield_dmg;
    	ship->shield -= shield_dmg;
    	if ( ship->shield == 0 )
    	  echo_to_cockpit( AT_BLOOD , ship , "Shields down..." );    	  
    }

    if ( damage > 0 )
    {
        if ( number_range(1, 100) <= 5 && ship->shipstate != SHIP_DISABLED )
        {
           echo_to_cockpit( AT_BLOOD + AT_BLINK , ship , "Ships Drive DAMAGED!" );   
           ship->shipstate = SHIP_DISABLED;
        }
        
        if ( number_range(1, 100) <= 5 && ship->missilestate != MISSILE_DAMAGED && ship->maxmissiles > 0 )
        {
           echo_to_room( AT_BLOOD + AT_BLINK , ship->gunseat , "Ships Missile Launcher DAMAGED!" );   
           ship->missilestate = MISSILE_DAMAGED;
        }
        
        if ( number_range(1, 100) <= 2 && ship->laserstate != LASER_DAMAGED )
        {
           echo_to_room( AT_BLOOD + AT_BLINK , ship->gunseat , "Lasers DAMAGED!" );   
           ship->laserstate = LASER_DAMAGED;
        }
    
    }
    
    ship->hull -= damage*5;
    
    if ( ship->hull <= 0 )
    {
       destroy_ship( ship , ch );
       return;
    }
    
    if ( ship->hull <= ship->maxhull/20 )
       echo_to_cockpit( AT_BLOOD+ AT_BLINK , ship , "WARNING! Ship hull severely damaged!" );    	  

}

void damage_ship( SHIP_DATA *ship , int min , int max )
{   
    int damage , shield_dmg;
     
    damage = number_range( min , max );
    
    if ( ship->shield > 0 )
    {   
        shield_dmg = UMIN( ship->shield , damage );
    	damage -= shield_dmg;
    	ship->shield -= shield_dmg;
    	if ( ship->shield == 0 )
    	  echo_to_cockpit( AT_BLOOD , ship , "Shields down..." );    	  
    }
    
    if ( damage > 0 )
    {

        if ( number_range(1, 100) <= 5 && ship->shipstate != SHIP_DISABLED )
        {
           echo_to_cockpit( AT_BLOOD + AT_BLINK , ship , "Ships Drive DAMAGED!" );   
           ship->shipstate = SHIP_DISABLED;
        }
        
        if ( number_range(1, 100) <= 5 && ship->missilestate != MISSILE_DAMAGED && ship->maxmissiles > 0 )
        {
           echo_to_room( AT_BLOOD + AT_BLINK , ship->gunseat , "Ships Missile Launcher DAMAGED!" );   
           ship->missilestate = MISSILE_DAMAGED;
        }
        
    
    }
    
    ship->hull -= damage*5;
    
    if ( ship->hull <= 0 )
    {
       destroy_ship( ship , NULL );
       return;
    }
    
    if ( ship->hull <= ship->maxhull/20 )
       echo_to_cockpit( AT_BLOOD+ AT_BLINK , ship , "WARNING! Ship hull severely damaged!" );    	  
     
}

void destroy_ship( SHIP_DATA *ship , CHAR_DATA *ch )
{   
    char buf[MAX_STRING_LENGTH];
    ROOM_INDEX_DATA *room;
    OBJ_DATA *robj;
    CHAR_DATA *rch;
    SHIP_DATA *rship;
    bool survived;
    int dIndex;
    SHIP_DATA *att;
    MISSILE_DATA * missile;
    MISSILE_DATA * m_next;
    TURRET_DATA * turret;
    
    sprintf( buf , "%s explodes in a blinding flash of light!", ship->name );  
    echo_to_system( AT_WHITE + AT_BLINK , ship , buf , NULL );
    
    sprintf( buf , "%s destroyed by %s", ship->name , ch ? ch->name : "(none)" );  
    log_string( buf );
    
    echo_to_ship( AT_WHITE , ship , "The ship is shaken by a FATAL explosion. You realize its escape or perish.");
    echo_to_ship( AT_WHITE , ship , "The last thing you remember is reaching for the escape pod release lever.");
    echo_to_ship( AT_WHITE + AT_BLINK , ship , "A blinding flash of light.");
    echo_to_ship( AT_WHITE, ship , "And then darkness....");
        
           
     for ( room = ship->first_room ; room ; room = room->next_in_ship )
     {
         rch = room->first_person;   
         while ( rch )
         {
            survived = FALSE;
         
            if ( !IS_NPC( rch ) && ship->starsystem  
            && ship->starsystem->last_planet && ship->starsystem->last_planet->area )
            {
                ROOM_INDEX_DATA * pRoom;
                OBJ_DATA * scraps;
                int rnum = 0;
                int tnum;
                
                tnum = number_range( 0 , ship->starsystem->last_planet->wilderness + ship->starsystem->last_planet->farmland );
                
                for ( pRoom = ship->starsystem->last_planet->area->first_room ; pRoom ; pRoom = pRoom->next_in_area )
                    if ( pRoom->sector_type != SECT_CITY && pRoom->sector_type != SECT_DUNNO
                    && pRoom->sector_type != SECT_INSIDE && pRoom->sector_type != SECT_UNDERGROUND )
                    {
                       if ( rnum++ < tnum )
                          continue;
                       
                       char_from_room(rch);
                       char_to_room( rch, pRoom );
                       if ( !IS_IMMORTAL( rch ) )
                          rch->hit = -1;
                       update_pos( rch );
                       echo_to_room( AT_WHITE , rch->in_room , "There is loud explosion as an escape pod hits the earth." );
                       
                       scraps        = create_object( get_obj_index( OBJ_VNUM_SCRAPS ), 0 );
                       scraps->timer = 15;
                       STRFREE( scraps->short_descr );
                       scraps->short_descr = STRALLOC( "a battered escape pod" );
                       STRFREE( scraps->description );
                       scraps->description = STRALLOC( "The smoking shell of an escape pod litters the earth.\n\r" );
                       obj_to_room( scraps, pRoom);

                       survived = TRUE;     
                       break;   
                    }       
            }
            
            if ( !survived && IS_IMMORTAL(rch) )
            {
                 char_from_room(rch);
                 char_to_room( rch, get_room_index(ROOM_VNUM_TEMPLE) );
                 survived = TRUE;
            }

            if ( !survived )
            {
              if ( ch )
                  raw_kill( ch , rch );
               else
                  raw_kill( rch , rch );
            }
            rch = room->first_person;     
         }
        
         for ( robj = room->first_content ; robj ; robj = robj->next_content )
         {
           separate_obj( robj );
           extract_obj( robj );
         }

       /* take care of ships docked in the hanger -Lews 11/01 */
        if( ship->class == CAPITAL_SHIP || ship->class == SPACE_STATION )
         for ( rship = room->first_ship ; rship ; rship = rship->next_in_room )
         {
           rship->shipstate = SHIP_DESTROYED;
         }
 
     }

    
     if (ship->starsystem)
        ship_from_starsystem( ship, ship->starsystem );  
  
     extract_ship( ship );
       
     if ( ship->type != MOB_SHIP )
     {
        CLAN_DATA *clan;
        
        if ( ship->type != MOB_SHIP && (clan = get_clan( ship->owner )) != NULL )
        {
          if ( ship->class <= SPACE_STATION )
             clan->spacecraft--;
          else
             clan->vehicles--;
        }
     }        
     STRFREE( ship->owner );
     STRFREE( ship->pilot );
     STRFREE( ship->copilot );
     STRFREE( ship->home );
     ship->owner = STRALLOC( "" );
     ship->pilot = STRALLOC( "" );
     ship->copilot = STRALLOC( "" );
     ship->home = STRALLOC( "" );     
     ship->target = NULL;
     for ( turret = ship->first_turret ; turret ; turret = turret->next )
                turret->target = NULL;
     for ( dIndex = 0 ; dIndex < MAX_SHIP_ROOMS ; dIndex++ )
       if ( ship->description[dIndex] );
          STRFREE( ship->description[dIndex] );

     UNLINK ( ship, first_ship , last_ship , next , prev );

     for ( att = first_ship ; att ; att = att->next )
     {
         if ( att->target == ship )
            att->target = NULL;
         for ( turret = att->first_turret ; turret ; turret = turret->next )
            if ( turret->target == ship )
                turret->target = NULL;
           
     }
     
     for ( missile = first_missile; missile; missile = m_next )
     {
         m_next = missile->next;
         
         if ( missile->target && missile->target == ship )
            extract_missile( missile );
         if ( missile->fired_from && missile->fired_from == ship )
            extract_missile( missile );            
     }
     
     write_ship_list();
}

bool ship_to_room(SHIP_DATA *ship , long vnum )
{
    ROOM_INDEX_DATA *shipto;
    
    if ( (shipto=get_room_index(vnum)) == NULL )
            return FALSE;
            
    LINK( ship, shipto->first_ship, shipto->last_ship, next_in_room, prev_in_room );
    ship->in_room = shipto; 
    return TRUE;
}

bool ship_to_hanger(SHIP_DATA *ship , ROOM_INDEX_DATA *shipto )
{
    
    if ( shipto == NULL )
      return FALSE;


    LINK( ship, shipto->first_ship, shipto->last_ship, next_in_room, prev_in_room );
    ship->in_room = shipto; 
    return TRUE;
}


void do_board( CHAR_DATA *ch, char *argument )
{
   ROOM_INDEX_DATA *fromroom;
   ROOM_INDEX_DATA *toroom;
   SHIP_DATA *ship;
   
   if ( !argument || argument[0] == '\0')
   {
       send_to_char( "Board what?\n\r", ch );
       return;
   }
   
   if ( ( ship = ship_in_room( ch->in_room , argument ) ) == NULL )
   {
            act( AT_PLAIN, "I see no $T here.", ch, NULL, argument, TO_CHAR );
           return;
   }

   if ( xIS_SET( ch->act, ACT_MOUNTED ) )
   {
          act( AT_PLAIN, "You can't go in there riding THAT.", ch, NULL, argument, TO_CHAR );  
          return;
   } 

   fromroom = ch->in_room;

        if ( ( toroom = ship->entrance )  != NULL )
   	{
   	   if ( ! ship->hatchopen )
   	   {
   	      send_to_char( "&RThe hatch is closed!\n\r", ch);
   	      return;
   	   }
   	
           if ( toroom->tunnel > 0 )
           {
	        CHAR_DATA *ctmp;
	        int count = 0;
	        
	       for ( ctmp = toroom->first_person; ctmp; ctmp = ctmp->next_in_room )
	       if ( ++count >= toroom->tunnel )
	       {
                  send_to_char( "There is no room for you in there.\n\r", ch );
		  return;
	       }
           }
            if ( ship->shipstate == SHIP_LAUNCH || ship->shipstate == SHIP_LAUNCH_2 )
            {
                 send_to_char("&rThat ship has already started launching!\n\r",ch);
                 return;
            }
            
            act( AT_PLAIN, "$n enters $T.", ch,
		NULL, ship->name , TO_ROOM );
	    act( AT_PLAIN, "You enter $T.", ch,
		NULL, ship->name , TO_CHAR );
   	    char_from_room( ch );
   	    char_to_room( ch , toroom );
   	    act( AT_PLAIN, "$n enters the ship.", ch,
		NULL, argument , TO_ROOM );
            do_look( ch , "auto" );

        }                                                                  
        else
          send_to_char("That ship has no entrance!\n\r", ch);
}

void do_leaveship( CHAR_DATA *ch, char *argument )
{
    ROOM_INDEX_DATA *fromroom;
    ROOM_INDEX_DATA *toroom = NULL;
    SHIP_DATA *ship;
    SHIP_DATA *target;
    
    fromroom = ch->in_room;
    
    if  ( (ship = ship_from_entrance(fromroom)) == NULL )
    {
        send_to_char( "I see no exit here.\n\r" , ch );
        return;
    }   
    
    if  ( ship->class == SPACE_STATION )
    {
        send_to_char( "You can't do that here.\n\r" , ch );
        return;
    }   
    
   /* taken out for hangers -Lews
    if ( ship->lastdoc != ship->location && ship->shipstate != SHIP_DOCKED )
    {
        send_to_char("&rMaybe you should wait until the ship lands.\n\r",ch);
        return;
    }
    */
    
    if ( ship->shipstate != SHIP_DOCKED && ship->shipstate != SHIP_DISABLED )
    {
        send_to_char("&rPlease wait till the ship is properly docked.\n\r",ch);
        return;
    }
    
    if ( ! ship->hatchopen )
    {
    	send_to_char("&RYou need to open the hatch first" , ch );
    	return;
    }

   
    /*Exit on to a hanger or just out of the ship */
    if ( ( toroom = get_room_index( ship->location ) ) != NULL ) 
    {
            act( AT_PLAIN, "$n exits the ship.", ch,
		NULL, argument , TO_ROOM );
	    act( AT_PLAIN, "You exit the ship.", ch,
		NULL, argument , TO_CHAR );
   	    char_from_room( ch );
   	    char_to_room( ch , toroom );
   	    act( AT_PLAIN, "$n steps out of a ship.", ch,
		NULL, argument , TO_ROOM );
            do_look( ch , "auto" );
     }       
     else if ( ship->on_ship )
     {
            target = get_ship( ship->on_ship );
            toroom = target->hanger;
            act( AT_PLAIN, "$n exits the ship.", ch,
		NULL, argument , TO_ROOM );
	    act( AT_PLAIN, "You exit the ship.", ch,
		NULL, argument , TO_CHAR );
   	    char_from_room( ch );
   	    char_to_room( ch , toroom );
   	    act( AT_PLAIN, "$n steps out of a ship.", ch,
		NULL, argument , TO_ROOM );
            do_look( ch , "auto" );
      }
      else
        send_to_char ( "The exit doesn't seem to be working properly.\n\r", ch );  
}

void do_launch
( CHAR_DATA *ch, char *argument )
{
    int chance; 
    long price = 0;
    SHIP_DATA *ship;
    char buf[MAX_STRING_LENGTH];

/*   His is taken out until I get hangers working -Lews 9/01
    SHIP_DATA *onship;
    HANGER_DATA *bayopen;

	if ( ( on_ship == ship_from_hanger( ship->lastdoc ) )  != NULL  && ! onship->bayopen )
	{
		send_to_char("&RThe hanger is closed!\n\r",ch);
		return;
	}
*/
            
    	        if ( (ship = ship_from_pilotseat(ch->in_room)) == NULL )  
    	        {
    	            send_to_char("&RYou must be in the pilots seat of a ship to do that!\n\r",ch);
    	            return;
    	        }
    	        
    	        if ( ship->class > SPACE_STATION )
    	        {
    	            send_to_char("&RThis isn't a spacecraft!\n\r",ch);
    	            return;
    	        }
    	        
    	        if ( autofly(ship) )
    	        {
    	            send_to_char("&RThe ship is set on autopilot, you'll have to turn it off first.\n\r",ch);
    	            return;
    	        }
    	        
                if  ( ship->class == SPACE_STATION )
                {
                   send_to_char( "You can't do that here.\n\r" , ch );
                   return;
                }   
    
    	        if ( !check_pilot( ch , ship ) )
    	        {
    	            send_to_char("&RHey, thats not your ship! Try renting a public one.\n\r",ch);
    	            return;
    	        }
    	        
             /* check if ship is in the same room as it landed -taken out for hangers -Lews
    	        if ( ( ship->lastdoc != ship->location ) )
                {
                       send_to_char("&rYou don't seem to be docked right now.\n\r",ch);
                       return;
                }
              */
    
    	        if ( ship->shipstate != SHIP_DOCKED && ship->shipstate != SHIP_DISABLED )
    	        {
    	            send_to_char("The ship is not docked right now.\n\r",ch);
    	            return;
    	        }
                
                chance = IS_NPC(ch) ? 100
	                 : (int)  (ch->pcdata->learned[gsn_spacecraft]) ;
                if ( number_percent( ) < chance )
    		{  
               /* ship launch price is the class * 100 + energy .2 + hull + missles & damage - Lews */
			price = ( (1+ship->class) * 100 );

                  price += ( ( ship->maxenergy-ship->energy ) * 0.2);

                  price += ( ship->maxhull-ship->hull );
                     if (ship->missiles )
     	                 price =+ ( 50 * (ship->maxmissiles-ship->missiles) );
                     
                     if (ship->shipstate == SHIP_DISABLED )
                            price += 300;
                     if ( ship->missilestate == MISSILE_DAMAGED )
                            price += 200;
                     if ( ship->laserstate == LASER_DAMAGED )
                            price += 100;

    	          if ( ch->pcdata && ch->pcdata->clan && !str_cmp(ch->pcdata->clan->name,ship->owner) ) 
                  {
                   if ( ch->pcdata->clan->funds < price )
                   {
                       ch_printf(ch, "&R%s doesn't have enough funds to prepare this ship for launch.\n\r", ch->pcdata->clan->name );
                       return;
                   }
    
                   ch->pcdata->clan->funds -= price;
                   ch_printf(ch, "&GIt costs %s %ld gold to ready this ship for launch.\n\r", ch->pcdata->clan->name, price );   
                  }
                  else
                  {
                   if ( ch->gold < price )
                   {
                       ch_printf(ch, "&RYou don't have enough funds to prepare this ship for launch.\n\r");
                       return;
                   }
    
                   ch->gold -= price;
                   ch_printf(ch, "&GYou pay %ld gold to ready the ship for launch.\n\r", price );   
                
                  }
                
                  ship->energy = ship->maxenergy;
                  ship->chaff = ship->maxchaff;
                  ship->missiles = ship->maxmissiles;
       		  ship->shield = 0;
       		  ship->autorecharge = FALSE;
       		  ship->autotrack = FALSE;
       		  ship->autospeed = FALSE;
       		  ship->hull = ship->maxhull;
       
       		  ship->missilestate = MISSILE_READY;
       		  ship->laserstate = LASER_READY;
       		  ship->shipstate = SHIP_DOCKED;
                
    		   if (ship->hatchopen)
    		   {
    		     ship->hatchopen = FALSE;
    		     sprintf( buf , "The hatch on %s closes." , ship->name);  
       	             echo_to_room( AT_YELLOW , get_room_index(ship->location) , buf );
       	             echo_to_room( AT_YELLOW , ship->entrance , "The hatch slides shut." );
       	   }
    		   set_char_color( AT_GREEN, ch );
    		   send_to_char( "Launch sequence initiated.\n\r", ch);
    		   act( AT_PLAIN, "$n starts up the ship and begins the launch sequence.", ch,
		        NULL, argument , TO_ROOM );
		   echo_to_ship( AT_YELLOW , ship , "The ship hums as it lifts off the ground.");
    		   sprintf( buf, "%s begins to launch.", ship->name );
    		   echo_to_room( AT_YELLOW , get_room_index(ship->location) , buf );
    		   ship->shipstate = SHIP_LAUNCH;
    		   ship->currspeed = ship->realspeed;
                   learn_from_success( ch, gsn_spacecraft );
                   return;   	   	
           }
                set_char_color( AT_RED, ch );
	        send_to_char("You fail to work the controls properly!\n\r",ch);
    	   	return;	
    	
}

void launchship( SHIP_DATA *ship )
{   
    char buf[MAX_STRING_LENGTH];
    SHIP_DATA *source;
    int plusminus;
    ROOM_INDEX_DATA * room;
    
    ship_to_starsystem( ship, starsystem_from_room( ship->in_room ) );
        
    if ( ship->starsystem == NULL )
    {
       echo_to_room( AT_YELLOW , ship->pilotseat , "Launch path blocked .. Launch aborted.");
       echo_to_ship( AT_YELLOW , ship , "The ship slowly sets back back down on the landing pad.");
       sprintf( buf ,  "%s slowly sets back down." ,ship->name );
       echo_to_room( AT_YELLOW , get_room_index(ship->location) , buf ); 
       ship->shipstate = SHIP_DOCKED;
       return;
    }    
    
    source = ship_from_room( ship->in_room );
    extract_ship(ship);    
    
    ship->location = 0;
    ship->on_ship =STRALLOC( "" );
    
    if (ship->shipstate != SHIP_DISABLED)
       ship->shipstate = SHIP_READY;
    
    plusminus = number_range ( -1 , 2 );
    if (plusminus > 0 )
        ship->hx = 1;
    else
        ship->hx = -1;
    
    plusminus = number_range ( -1 , 2 );

Showing the first 100 KB of 191.0 KB. Download the archive for the full file.

Site Stats

Snippets: 1791
Codebases: 571