space.c

swrip101/src/space.c · 405.4 KB

From the archive swrip101.tgz

Download the archive   « Back to the archive contents

/***************************************************************************
*                   Star Wars: Rise in Power MUD Codebase                  *
*--------------------------------------------------------------------------*
* SWRiP Code Additions and changes from the SWReality and Smaug Code       *
* copyright (c) 2001 by Mark Miller (Darrik Vequir)                        *
*--------------------------------------------------------------------------*
* Star Wars Reality Code Additions and changes from the Smaug Code         *
* copyright (c) 1997 by Sean Cooper                                        *
* -------------------------------------------------------------------------*
* Starwars and Starwars Names copyright(c) Lucas Film Ltd.                 *
*--------------------------------------------------------------------------*
* SMAUG 1.0 (C) 1994, 1995, 1996 by Derek Snider                           *
* SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,                    *
* Scryn, Rennard, Swordbearer, Gorog, Grishnakh and Tricops                *
* ------------------------------------------------------------------------ *
* Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael        *
* Chastain, Michael Quan, and Mitchell Tse.                                *
* Original Diku Mud copyright (C) 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 <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include "mud.h"

SHIP_DATA * first_ship;
SHIP_DATA * last_ship;

MISSILE_DATA * first_missile;
MISSILE_DATA * last_missile;

SPACE_DATA * first_spaceobject;
SPACE_DATA * last_spaceobject;

int bus_pos =0;
int bus_planet =0;
int bus2_planet = 8;
int bus3_planet = 11;
int bus4_planet = 3;
int turbocar_stop =0;
int corus_shuttle =0;
int baycount = 0;

#define DEGMULTI 149760

#define MAX_COORD 15000000
#define MAX_COORD_S 13000000
#define MAX_STATION    10
#define MAX_BUS_STOP 14

#define STOP_PLANET     202
#define STOP_SHIPYARD   32015

#define SENATEPAD       32055
#define OUTERPAD        2064

struct	serin_shuttle
{
    char 		name	[20];	
    int			startingloc;
    int			cockpitvnum;
    int			planetloc;
    int		const  	bus_vnum [12];
    char * 	const   bus_stop [12];
};

/* New Serin system, makes it much easier to add new ones - Darrik Vequir, May, 01 */
/* MAX_BUS defined in mud.h */

struct serin_shuttle serin[MAX_BUS] =
{
{
  "Pluogus", 0, 32140, 0,
    { 201, 21943, 28613, 21100, 28038, 10500, 201 },
    { "Coruscant", "Corulag", "Kashyyyk", "Mon Calamari", "Gamorr", "Corellia", "Coruscant" }

},
{
  "Tocca", 0, 32410, 0,
    { 5550, 21943, 10500, 29001, 11800, 4100, 23082, 5550 },
    { "Ord Mantell", "Corulag", "Corellia", "Adari", "Roche", "Sullust", "Kuat", "Ord Mantell" }

},
{
  "Cassia", 0, 32425, 0,
    { 28247, 10500, 5200, 31872, 822, 28038, 10500, 28247 },
    { "Byss", "Corellia", "Af'el", "Tatooine", "Ryloth", "Gamorr", "Corellia", "Byss" }

},
{
  "Siego", 0, 32435, 0,
    { 201, 10500, 6264, 11206, 3060, 4305, 28247, 201 },
    { "Coruscant", "Corellia", "Bespin", "Hoth", "Endor", "Wroona", "Byss", "Coruscant"}

},
{
  "Amose", 0, 32445, 0,
    { 3060, 6264, 4100, 31872, 28038, 21100, 5550, 3060 },
    { "Endor", "Bespin", "Sullust", "Tatooine", "Gamorr", "Mon Calamari", "Ord Mantell", "Endor"}

},
{
  "Edena", 0, 32440, 0,
    { 28247, 201, 21943, 23082, 10500, 29001, 4305, 28247 },
    { "Byss", "Coruscant", "Corulag", "Kuat", "Corellia", "Adari", "Wroona", "Byss"}

},
{
  "Odani", 0, 32450, 0,
    { 4305, 11800, 28613, 5550, 23082, 10500, 29001, 4305 },
    { "Wroona", "Roche", "Kashyyyk", "Ord Mantell", "Kuat", "Corellia", "Adari", "Wroona"}

}
};


int     const   station_vnum [MAX_STATION] =
{
    215 , 216 , 217 , 218 , 219 , 220 , 221 ,222 , 223 , 224
};

char *  const   station_name [MAX_STATION] =
{
   "Menari Spaceport" , "Skydome Botanical Gardens" , "Grand Towers" ,
   "Grandis Mon Theater" , "Palace Station" , "Great Galactic Museum" ,
   "College Station" , "Holographic Zoo of Extinct Animals" ,
   "Dometown Station " , "Monument Plaza"
};

int     const   bus_vnum [MAX_BUS_STOP] =
{
    201 ,  21100 , 29001 , 28038 , 31872 , 822 , 6264, 28613 , 3060 , 28247, 32297, 2065, 10500, 11206
};
int     const   bus_vnum2 [MAX_BUS_STOP] =
{
    201, 11206 ,  10500 , 2065 , 32297 , 28247 , 3060 , 28613 , 6264, 822 , 31872, 28038, 29001, 21100
};

char *  const   bus_stop [MAX_BUS_STOP+1] =
{
   "Coruscant",
   "Mon Calamari", "Adari", "Gamorr", "Tatooine" , "Ryloth", "Bespin",
   "Kashyyyk", "Endor", "Byss", "Cloning Facilities", "Kuat", "Corellia", "Hoth", "Coruscant"  /* last should always be same as first */
};

char *  const bus_stoprev [MAX_BUS_STOP+1] =
{
   "Coruscant",
   "Hoth", "Corellia", "Kuat", "Cloning Facilities" , "Byss" , "Endor", "Kashyyyk",
   "Bespin", "Ryloth", "Tatooine", "Gamorr", "Adari", "Mon Calamari", "Coruscant"  /* last should always be same as first */
};


/* local routines */
void	fread_ship	args( ( SHIP_DATA *ship, FILE *fp ) );
bool	load_ship_file	args( ( char *shipfile ) );
void	write_ship_list	args( ( void ) );
void    fread_spaceobject      args( ( SPACE_DATA *spaceobject, FILE *fp ) );
bool    load_spaceobject  args( ( char *spaceobjectfile ) );
void    write_spaceobject_list args( ( void ) );
void    resetship args( ( SHIP_DATA *ship ) );
void    approachland args( ( SHIP_DATA *ship, char *arg ) );
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 );
void modtrainer( SHIP_DATA *ship, sh_int class );
void makedebris( SHIP_DATA *ship );
bool space_in_range_h( SHIP_DATA *ship, SPACE_DATA *space);
/* from comm.c */
bool    write_to_descriptor     args( ( int desc, char *txt, int length ) );

ROOM_INDEX_DATA *generate_exit( ROOM_INDEX_DATA *in_room, EXIT_DATA **pexit );


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 );
    }
}


bool  land_bus( SHIP_DATA *ship, int destination )
{
    char buf[MAX_STRING_LENGTH];
    
    if ( !ship_to_room( ship , destination ) )
    {
       return FALSE;
    }
    echo_to_ship( AT_YELLOW , ship , "You feel a slight thud as the ship sets down on the ground.");
    ship->location = destination;
    ship->lastdoc = ship->location;
    ship->shipstate = SHIP_LANDED;
    if (ship->spaceobject)
        ship_from_spaceobject( ship, ship->spaceobject );
    sprintf( buf, "%s lands on the platform.", ship->name );
    echo_to_room( AT_YELLOW , get_room_index(ship->location) , buf );
    sprintf( buf , "The hatch on %s opens." , ship->name);
    echo_to_room( AT_YELLOW , get_room_index(ship->location) , buf );
    echo_to_room( AT_YELLOW , get_room_index(ship->entrance) , "The hatch opens." );
    ship->hatchopen = TRUE;
    sound_to_room( get_room_index(ship->entrance) , "!!SOUND(door)" );
    sound_to_room( get_room_index(ship->location) , "!!SOUND(door)" );
    return TRUE;
}

void    launch_bus( SHIP_DATA *ship )
{
      char buf[MAX_STRING_LENGTH];

      sound_to_room( get_room_index(ship->entrance) , "!!SOUND(door)" );
       sound_to_room( get_room_index(ship->location) , "!!SOUND(door)" );
      sprintf( buf , "The hatch on %s closes and it begins to launch." , ship->name);
      echo_to_room( AT_YELLOW , get_room_index(ship->location) , buf );
      echo_to_room( AT_YELLOW , get_room_index(ship->entrance) , "The hatch slides shut." );
      ship->hatchopen = FALSE;
      extract_ship( ship );
      echo_to_ship( AT_YELLOW , ship , "The ship begins to launch.");
      ship->location = 0;
      ship->shipstate = SHIP_READY;
}

void update_traffic( )
{
    SHIP_DATA *shuttle, *senate;
    SHIP_DATA *turbocar;
    char       buf[MAX_STRING_LENGTH];

    shuttle = ship_from_cockpit( ROOM_CORUSCANT_SHUTTLE );
    senate = ship_from_cockpit( ROOM_SENATE_SHUTTLE );
    if ( senate != NULL && shuttle != NULL )
    {
        switch (corus_shuttle)
        {
             default:
                corus_shuttle++;
                break;

             case 0:
                land_bus( shuttle , STOP_PLANET );
                land_bus( senate , SENATEPAD );
                corus_shuttle++;
                echo_to_ship( AT_CYAN , shuttle , "Welcome to Menari Spaceport." );
                echo_to_ship( AT_CYAN , senate , "Welcome to Kuat Shipyard." );
                break;

             case 4:
                launch_bus( shuttle );
                launch_bus( senate );
                corus_shuttle++;
                break;

             case 5:
                land_bus( shuttle , STOP_SHIPYARD );
                land_bus( senate , OUTERPAD );
                echo_to_ship( AT_CYAN , shuttle , "Welcome to Coruscant Shipyard." );
                echo_to_ship( AT_CYAN , senate , "Welcome to the planet of Kuat." );
                corus_shuttle++;
                break;
                
             case 9:
                launch_bus( shuttle );
                launch_bus( senate );
                corus_shuttle++;
                break;

        }
        
        if ( corus_shuttle >= 10 )
              corus_shuttle = 0;
    }
    
    turbocar = ship_from_cockpit( ROOM_CORUSCANT_TURBOCAR );
    if ( turbocar != NULL )
    {
      	sprintf( buf , "The turbocar doors close and it speeds out of the station.");  
      	echo_to_room( AT_YELLOW , get_room_index(turbocar->location) , buf );
      	extract_ship( turbocar );
      	turbocar->location = 0;
       	ship_to_room( turbocar , station_vnum[turbocar_stop] );
       	echo_to_ship( AT_YELLOW , turbocar , "The turbocar makes a quick journey to the next station."); 
       	turbocar->location = station_vnum[turbocar_stop];
       	turbocar->lastdoc = turbocar->location;
       	turbocar->shipstate = SHIP_LANDED;
       	if (turbocar->spaceobject)
          ship_from_spaceobject( turbocar, turbocar->spaceobject );  
    	sprintf( buf, "A turbocar pulls into the platform and the doors slide open.");
    	echo_to_room( AT_YELLOW , get_room_index(turbocar->location) , buf );
    	sprintf( buf, "Welcome to %s." , station_name[turbocar_stop] );
    	echo_to_ship( AT_CYAN , turbocar , buf );
        turbocar->hatchopen = TRUE;
        
        turbocar_stop++;
        if ( turbocar_stop >= MAX_STATION )
           turbocar_stop = 0;
    }
       
}

void update_bus( )
{
    SHIP_DATA * ship[MAX_BUS];
/*    SHIP_DATA *ship2;
    SHIP_DATA *ship3;
    SHIP_DATA *ship4;
*/
    SHIP_DATA *target;
    int        destination, bus;
    char       buf[MAX_STRING_LENGTH];

   for( bus = 0; bus < MAX_BUS; bus++ )
     ship[bus] = ship_from_cockpit( serin[bus].cockpitvnum );

    switch (bus_pos)
    {

       case 0:
            for( bus = 0; bus < MAX_BUS; bus++ )
            {
            if( ship_from_cockpit( serin[bus].cockpitvnum ) == NULL )
              continue;
              target = ship_from_hanger( serin[bus].bus_vnum[serin[bus].planetloc] );
              if ( target != NULL && !target->spaceobject )
              {
                sprintf( buf,  "An electronic voice says, 'Cannot land at %s ... it seems to have dissapeared.'", serin[bus].bus_stop[serin[bus].planetloc] );
                echo_to_ship( AT_CYAN , ship[bus] , buf );
                bus_pos = 5;
              }
            }
/*              
            target = ship_from_hanger( bus_vnum[bus_planet] );
            if ( target != NULL && !target->spaceobject )
            {
               sprintf( buf,  "An electronic voice says, 'Cannot land at %s ... it seems to have dissapeared.'", bus_stop[bus_planet] );
               echo_to_ship( AT_CYAN , ship , buf );
               bus_pos = 5;
            }

            target = ship_from_hanger( bus_vnum[bus2_planet] );
            if ( target != NULL && !target->spaceobject )
            {
               sprintf( buf,  "An electronic voice says, 'Cannot land at %s ... it seems to have dissapeared.'", bus_stop[bus_planet] );
               echo_to_ship( AT_CYAN , ship2 , buf );
               bus_pos = 5;
            }
            target = ship_from_hanger( bus_vnum2[bus3_planet] );
            if ( target != NULL && !target->spaceobject )
            {
               sprintf( buf,  "An electronic voice says, 'Cannot land at %s ... it seems to have dissapeared.'", bus_stoprev[bus_planet] );
               echo_to_ship( AT_CYAN , ship3 , buf );
               bus_pos = 5;
            }
            target = ship_from_hanger( bus_vnum2[bus4_planet] );
            if ( target != NULL && !target->spaceobject )
            {
               sprintf( buf,  "An electronic voice says, 'Cannot land at %s ... it seems to have dissapeared.'", bus_stoprev[bus_planet] );
               echo_to_ship( AT_CYAN , ship4 , buf );
               bus_pos = 5;
            }
*/
            bus_pos++;
            break;

       case 6:

/*
            launch_bus ( ship );
            launch_bus ( ship2 );
            launch_bus ( ship3 );
            launch_bus ( ship4 );
*/  
	    for( bus = 0; bus < MAX_BUS; bus++ )
	    {
            if( ship_from_cockpit( serin[bus].cockpitvnum ) == NULL )
              continue;
	      launch_bus( ship[bus] );
	    }
            bus_pos++;
            break;

       case 7:
/*          echo_to_ship( AT_YELLOW , ship , "The ship lurches slightly as it makes the jump to lightspeed.");
            echo_to_ship( AT_YELLOW , ship2 , "The ship lurches slightly as it makes the jump to lightspeed.");
            echo_to_ship( AT_YELLOW , ship3 , "The ship lurches slightly as it makes the jump to lightspeed.");
            echo_to_ship( AT_YELLOW , ship4 , "The ship lurches slightly as it makes the jump to lightspeed.");
*/
	    for( bus = 0; bus < MAX_BUS; bus++ )
	    {
            if( ship_from_cockpit( serin[bus].cockpitvnum ) == NULL )
              continue;
    	
              echo_to_ship( AT_YELLOW , ship[bus] , "The ship lurches slightly as it makes the jump to lightspeed.");
            }
            bus_pos++;
            break;

       case 9:

/*            echo_to_ship( AT_YELLOW , ship , "The ship lurches slightly as it comes out of hyperspace..");
            echo_to_ship( AT_YELLOW , ship2 , "The ship lurches slightly as it comes out of hyperspace..");
            echo_to_ship( AT_YELLOW , ship3 , "The ship lurches slightly as it comes out of hyperspace..");
            echo_to_ship( AT_YELLOW , ship4 , "The ship lurches slightly as it comes out of hyperspace..");
*/
	    for( bus = 0; bus < MAX_BUS; bus++ )
	    {
            if( ship_from_cockpit( serin[bus].cockpitvnum ) == NULL )
              continue;
	      echo_to_ship( AT_YELLOW , ship[bus] , "The ship lurches slightly as it comes out of hyperspace.");
	    }
	      
            bus_pos++;
            break;

       case 1:

	    for( bus = 0; bus < MAX_BUS; bus++ )
          {
            if( ship_from_cockpit( serin[bus].cockpitvnum ) == NULL )
              continue;
              destination = serin[bus].bus_vnum[serin[bus].planetloc];
            if ( !land_bus( ship[bus], destination ) )
            {
               sprintf( buf, "An electronic voice says, 'Oh My, %s seems to have dissapeared.'" , serin[bus].bus_stop[serin[bus].planetloc] );
               echo_to_ship( AT_CYAN , ship[bus] , buf );
               echo_to_ship( AT_CYAN , ship[bus] , "An electronic voice says, 'Alderaan was bad enough... Landing aborted.'");
            }
            else
            {
               sprintf( buf,  "An electronic voice says, 'Welcome to %s'" , serin[bus].bus_stop[serin[bus].planetloc] );
               echo_to_ship( AT_CYAN , ship[bus] , buf);
               echo_to_ship( AT_CYAN , ship[bus] , "It continues, 'Please exit through the main ramp. Enjoy your stay.'");
            }
          }
/*          destination = bus_vnum[bus2_planet];
            if ( !land_bus( ship2, destination ) )
            {
               sprintf( buf, "An electronic voice says, 'Oh My, %s seems to have dissapeared.'" , bus_stop[bus2_planet] );
               echo_to_ship( AT_CYAN , ship2 , buf );
               echo_to_ship( AT_CYAN , ship2 , "An electronic voice says, 'I do hope it wasn't a superlaser. Landing aborted.'");
            }
            else
            {
               sprintf( buf,  "An electronic voice says, 'Welcome to %s'" , bus_stop[bus2_planet] );
               echo_to_ship( AT_CYAN , ship2 , buf);
               echo_to_ship( AT_CYAN , ship2 , "It continues, 'Please exit through the main ramp. Enjoy your stay.'");
            }
            destination = bus_vnum2[bus3_planet];
            if ( !land_bus( ship3, destination ) )
            {
               sprintf( buf, "An electronic voice says, 'Oh My, %s seems to have dissapeared.'" , bus_stoprev[bus3_planet] );
               echo_to_ship( AT_CYAN , ship3 , buf );
               echo_to_ship( AT_CYAN , ship3 , "An electronic voice says, 'I do hope it wasn't a superlaser. Landing aborted.'");
            }
            else
            {
               sprintf( buf,  "An electronic voice says, 'Welcome to %s'" , bus_stoprev[bus3_planet] );
               echo_to_ship( AT_CYAN , ship3 , buf);
               echo_to_ship( AT_CYAN , ship3 , "It continues, 'Please exit through the main ramp. Enjoy your stay.'");
            }
            destination = bus_vnum2[bus4_planet];
            if ( !land_bus( ship4, destination ) )
            {
               sprintf( buf, "An electronic voice says, 'Oh My, %s seems to have dissapeared.'" , bus_stoprev[bus4_planet] );
               echo_to_ship( AT_CYAN , ship4 , buf );
               echo_to_ship( AT_CYAN , ship4 , "An electronic voice says, 'I do hope it wasn't a superlaser. Landing aborted.'");
            }
            else
            {
               sprintf( buf,  "An electronic voice says, 'Welcome to %s'" , bus_stoprev[bus4_planet] );
               echo_to_ship( AT_CYAN , ship4 , buf);
               echo_to_ship( AT_CYAN , ship4 , "It continues, 'Please exit through the main ramp. Enjoy your stay.'");
            }
*/
            bus_pos++;
            break;

       case 5:
         for( bus = 0; bus < MAX_BUS; bus++)
         {
            if( ship_from_cockpit( serin[bus].cockpitvnum ) == NULL )
              continue;

            sprintf( buf, "It continues, 'Next stop, %s'" , serin[bus].bus_stop[serin[bus].planetloc+1] );
            echo_to_ship( AT_CYAN , ship[bus] , "An electronic voice says, 'Preparing for launch.'");
            echo_to_ship( AT_CYAN , ship[bus] , buf);
         }
/*          sprintf( buf, "It continues, 'Next stop, %s'" , bus_stop[bus2_planet+1] );
            echo_to_ship( AT_CYAN , ship2 , "An electronic voice says, 'Preparing for launch.'");
            echo_to_ship( AT_CYAN , ship2 , buf);
            sprintf( buf, "It continues, 'Next stop, %s'" , bus_stoprev[bus3_planet+1] );
            echo_to_ship( AT_CYAN , ship3 , "An electronic voice says, 'Preparing for launch.'");
            echo_to_ship( AT_CYAN , ship3 , buf);
            sprintf( buf, "It continues, 'Next stop, %s'" , bus_stoprev[bus4_planet+1] );
            echo_to_ship( AT_CYAN , ship4 , "An electronic voice says, 'Preparing for launch.'");
            echo_to_ship( AT_CYAN , ship4 , buf);
*/          bus_pos++;
            break;

       default:
            bus_pos++;
            break;
    }

    if ( bus_pos >= 10 )
    {
        bus_pos = 0;
	for( bus = 0; bus < MAX_BUS; bus++ )
	{
	  serin[bus].planetloc++;
          if( serin[bus].bus_vnum[serin[bus].planetloc] == serin[bus].bus_vnum[0] )
            serin[bus].planetloc = 0;
        }
/*        bus_planet++;
        bus2_planet++;
        bus3_planet++;
        bus4_planet++;
*/
    }
       
/*  if ( bus_planet >= MAX_BUS_STOP )
       bus_planet = 0;
    if ( bus2_planet >= MAX_BUS_STOP )
       bus2_planet = 0;
    if ( bus3_planet >= MAX_BUS_STOP )
       bus3_planet = 0;
    if ( bus4_planet >= MAX_BUS_STOP )
       bus4_planet = 0;
*/
}

void move_ships( )
{
   SHIP_DATA *ship;
   MISSILE_DATA *missile;
   MISSILE_DATA *m_next;
   SHIP_DATA *target;
   SPACE_DATA *spaceobj;
   float dx, dy, dz, change;
   char buf[MAX_STRING_LENGTH];
   CHAR_DATA *ch;
   bool ch_found = FALSE;
   sh_int crashsun = 0;
   int speed;
  
   for( spaceobj = first_spaceobject; spaceobj; spaceobj = spaceobj->next )
   {
     if ( spaceobj->speed > 0 )
        {
          
          change = sqrt( spaceobj->hx*spaceobj->hx + spaceobj->hy*spaceobj->hy + spaceobj->hz*spaceobj->hz ); 

           if (change > 0)
           {
             dx = spaceobj->hx/change;     
             dy = spaceobj->hy/change;
             dz = spaceobj->hz/change;
             spaceobj->xpos += (dx * spaceobj->speed/5);
             spaceobj->ypos += (dy * spaceobj->speed/5);
             spaceobj->zpos += (dz * spaceobj->speed/5);
           }
           
        } 

     if( spaceobj->xpos > MAX_COORD )
       spaceobj->xpos = -MAX_COORD_S;
     if( spaceobj->ypos > MAX_COORD )
       spaceobj->ypos = -MAX_COORD_S;
     if( spaceobj->zpos > MAX_COORD )
       spaceobj->zpos = -MAX_COORD_S;
     if( spaceobj->xpos < -MAX_COORD )
       spaceobj->xpos = MAX_COORD_S;
     if( spaceobj->ypos < -MAX_COORD )
       spaceobj->ypos = MAX_COORD_S;
     if( spaceobj->zpos < -MAX_COORD )
       spaceobj->zpos = MAX_COORD_S;

    }
   	

   for ( missile = first_missile; missile; missile = m_next )
   {
           m_next = missile->next;
           
              ship = missile->fired_from;
              target = missile->target;
              
              if ( target->spaceobject && missile_in_range( ship, missile ) )
              {
                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 , get_room_index(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+missile->missiletype*missile->missiletype*30 , 
                           50+missile->missiletype*missile->missiletype*missile->missiletype*50 , ch );
                      }
                    if ( !ch_found )
                       damage_ship( target , ship, 20+missile->missiletype*missile->missiletype*20 , 
                           30+missile->missiletype*missile->missiletype*ship->missiletype*30 );   
                    extract_missile( missile );
                   }
                   else
                   {
                    echo_to_room( AT_YELLOW , get_room_index(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
              { 
                   missile->age++;
                   if (missile->age >= 50)
                      extract_missile( missile );
              }

   }
   
   for ( ship = first_ship; ship; ship = ship->next )
   {
   
     if ( !ship->spaceobject )
        continue;
        
     if( ship->shipstate == SHIP_LANDED && ship->spaceobject )
       ship->shipstate = SHIP_READY;
 
     if ( ship->currspeed > 0 && ship->shipstate != SHIP_LAND && ship->shipstate != SHIP_LAND_2)
        {
          
          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 ( ship->tractoredby && ship->tractoredby->class <= ship->class )
     {
	    ship->tractoredby->currspeed = (ship->tractoredby->tractorbeam)/4;
	    ship->tractoredby->hx = ship->vx - ship->tractoredby->vx;
	    ship->tractoredby->hy = ship->vy - ship->tractoredby->vy;
	    ship->tractoredby->hz = ship->vz - ship->tractoredby->vz;

/*
          float hx,hy,hz;

	  hx = ship->tractoredby->vx - ship->vx;          
	  hy = ship->tractoredby->vy - ship->vy;          
	  hz = ship->tractoredby->vz - ship->vz;          
          
          change = sqrt( hx*hx + hy*hy + hz*hz ); 

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

  */         
        } 

     if ( ship->tractoredby && ship->tractoredby->class > ship->class )
     {

	    ship->currspeed = ship->tractoredby->tractorbeam/4;
	    ship->hx = ship->tractoredby->vx - ship->vx;
	    ship->hy = ship->tractoredby->vy - ship->vy;
	    ship->hz = ship->tractoredby->vz - ship->vz;

/*
          float hx,hy,hz;

	  hx = ship->tractored->vx - ship->vx;          
	  hy = ship->tractored->vy - ship->vy;          
	  hz = ship->tractored->vz - ship->vz;          
          
          change = sqrt( hx*hx + hy*hy + hz*hz ); 

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


	if ( ship->tractoredby )
	{
 	  if ( abs(ship->vx - ship->tractoredby->vx) < 10 &&
               abs(ship->vy - ship->tractoredby->vy) < 10 &&
               abs(ship->vz - ship->tractoredby->vz) < 10 )
          {
            if ( ship->class < ship->tractoredby->class )
            {
              ship->vx = ship->tractoredby->vx;
              ship->vy = ship->tractoredby->vy;
              ship->vz = ship->tractoredby->vz;
            }
            if ( ship->class >= ship->tractoredby->class )	
	    {
	    	ship->tractoredby->vx = ship->vx;
	    	ship->tractoredby->vy = ship->vy;
	    	ship->tractoredby->vz = ship->vz;
	    }
	  }
	}


        if ( autofly(ship) )
           continue;
        

        for( spaceobj = first_spaceobject; spaceobj; spaceobj = spaceobj->next )
	{           
/*          if ( ship->class != SHIP_PLATFORM && !autofly(ship) && ship->currspeed < 50)
          {
            if ( (spaceobj->gravity > 50) && strcmp(spaceobj->name,"") && space_in_range( ship, spaceobj ) )
            {
              if (ship->vx >= spaceobj->xpos + 1 || ship->vx <= spaceobj->xpos - 1 )
                ship->vx -= URANGE(-3,(spaceobj->gravity)/(ship->vx - spaceobj->xpos)/2,3);
              if (ship->vy >= spaceobj->ypos + 1 || ship->vy <= spaceobj->ypos - 1 )
                ship->vy -= URANGE(-3,(spaceobj->gravity)/(ship->vy - spaceobj->ypos)/2,3);
              if (ship->vz >= spaceobj->zpos + 1 || ship->vz <= spaceobj->zpos - 1 )
                ship->vz -= URANGE(-3,(spaceobj->gravity)/(ship->vz - spaceobj->zpos)/2,3);
            }
          }
*/
/*
          for ( target = ship->spaceobject->first_ship; target; target = target->next_in_spaceobject)
          {
                if ( target != ship &&
                    abs(ship->vx - target->vx) < 1 &&
                    abs(ship->vy - target->vy) < 1 &&
                    abs(ship->vz - target->vz) < 1 )
                {
                    ship->collision = target->maxhull;
                    target->collision = ship->maxhull;
                }
          }
*/
          if ( spaceobj->type == SPACE_SUN && spaceobj->name && strcmp(spaceobj->name,"") &&
                     abs(ship->vx - spaceobj->xpos) < 10 &&
                     abs(ship->vy - spaceobj->ypos) < 10 &&
                     abs(ship->vz - spaceobj->zpos) < 10 )
                {
          /* Why the redundence of code here?
             Only need to evade once.  Darrik Vequir */
                  crashsun = 1;
                }

                if (crashsun == 1)
                {
                /* Evading the sun added by Darrik Vequir */

                  ship->hx = 10 * ship->vx;
                  ship->hy = 10 * ship->vy;
                  ship->hz = 10 * ship->vz;
                  ship->energy -= ship->currspeed/10;
                  ship->currspeed = ship->realspeed;
                  echo_to_room( AT_RED , get_room_index(ship->pilotseat), "Automatic Override: Evading to avoid collision with sun!\n\r" );
    	            if ( ship->class == FIGHTER_SHIP || ( ship->class == MIDSIZE_SHIP && ship->manuever > 50 ) )
        	           ship->shipstate = SHIP_BUSY_3;
                  else if ( ship->class == MIDSIZE_SHIP || ( ship->class == CAPITAL_SHIP && ship->manuever > 50 ) )
        	           ship->shipstate = SHIP_BUSY_2;
                  else
        	           ship->shipstate = SHIP_BUSY;

		}

/*
                    echo_to_cockpit( AT_BLOOD+AT_BLINK, ship, "You fly directly into the sun.");
                    sprintf( buf , "%s flys directly into %s!", ship->name, ship->spaceobject->star2);
                    echo_to_system( AT_ORANGE , ship , buf , NULL );
                    destroy_ship(ship , NULL);
                    continue;
*/

        if ( ship->currspeed > 0 )
        {
          if ( spaceobj->type >= SPACE_PLANET && spaceobj->name && strcmp(spaceobj->name,"") &&
               abs(ship->vx - spaceobj->xpos) < 10 &&
                     abs(ship->vy - spaceobj->ypos) < 10 &&
                     abs(ship->vz - spaceobj->zpos) < 10 )
                {
                    sprintf( buf , "You begin orbitting %s.", spaceobj->name); 
                    echo_to_cockpit( AT_YELLOW, ship, buf);
                    sprintf( buf , "%s begins orbiting %s.", ship->name, spaceobj->name); 
                    echo_to_system( AT_ORANGE , ship , buf , NULL );
                    ship->inorbitof = spaceobj;
                    ship->currspeed = 0;
                }            

        }
       }
       
   }

   for ( ship = first_ship; ship; ship = ship->next )
   {
        if (ship->shipstate == SHIP_HYPERSPACE)
        {
            float tx, ty, tz;
            float dist, origdist;

            ship->hyperdistance -= ship->hyperspeed;

	    dist = (float) ship->hyperdistance;
	    origdist = (float) ship->orighyperdistance;

            if ( dist == 0)
              dist = -1;

            tx = ( ship->vx - ship->jx );
            ty = ( ship->vy - ship->jy );
            tz = ( ship->vz - ship->jz );
            ship->cx = ship->vx - (tx*(dist/origdist));
	    ship->cy = ship->vy - (ty*(dist/origdist));
	    ship->cz = ship->vz - (tz*(dist/origdist));

            ship->count++;

	for( spaceobj = first_spaceobject; spaceobj; spaceobj = spaceobj->next )
	  if( space_in_range_h( ship, spaceobj ) )
	  {
	    int damage;
            	    echo_to_room( AT_YELLOW, get_room_index(ship->pilotseat), "Hyperjump complete.");
            	    echo_to_ship( AT_YELLOW, ship, "The ship slams to a halt as it comes out of hyperspace.");
            	    sprintf( buf ,"%s enters the starsystem at %.0f %.0f %.0f" , ship->name, ship->vx, ship->vy, ship->vz );
                    damage = 15* number_range( 1, 4 );
                    ship->hull -= damage;
            	    echo_to_ship( AT_YELLOW, ship, "The hull cracks from the pressure.");
		    ship->vx = ship->cx;
		    ship->vy = ship->cy;
		    ship->vz = ship->cz;
		    ship_to_spaceobject( ship, ship->currjump );
		    ship->currjump = NULL;
            	    echo_to_system( AT_YELLOW, ship, buf , NULL );
            	    ship->shipstate = SHIP_READY;
            	    STRFREE( ship->home );
            	    ship->home = STRALLOC( ship->spaceobject->name );
          }
	  	
	  if( target )
	  {
            	    echo_to_room( AT_YELLOW, get_room_index(ship->pilotseat), "Hyperjump complete.");
            	    echo_to_ship( AT_YELLOW, ship, "The ship slams to a halt as it comes out of hyperspace.  An artificial gravity well surrounds you!");
            	    sprintf( buf ,"%s enters the starsystem at %.0f %.0f %.0f" , ship->name, ship->vx, ship->vy, ship->vz );
		    ship->vx = ship->cx;
		    ship->vy = ship->cy;
		    ship->vz = ship->cz;
		    ship_to_spaceobject( ship, ship->currjump );
		    ship->currjump = NULL;
            	    echo_to_system( AT_YELLOW, ship, buf , NULL );
            	    ship->shipstate = SHIP_READY;
            	    STRFREE( ship->home );
            	    ship->home = STRALLOC( ship->spaceobject->name );
          }


            if (ship->shipstate == SHIP_HYPERSPACE && ship->hyperdistance <= 0 && !ship->tracking)
            {
            	ship->count = 0;
            	ship_to_spaceobject (ship, ship->currjump);
            	
            	if (ship->spaceobject == NULL)
            	{
            	    echo_to_cockpit( AT_RED, ship, "Ship lost in Hyperspace. Make new calculations.");
            	}
            	else
            	{
            	    echo_to_room( AT_YELLOW, get_room_index(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 );
		    ship->cx = ship->vx;
		    ship->cy = ship->vy;
		    ship->cz = ship->vz;
		    ship_to_spaceobject( ship, ship->currjump );
		    ship->currjump = NULL;
            	    echo_to_system( AT_YELLOW, ship, buf , NULL );
            	    ship->shipstate = SHIP_READY;
            	    STRFREE( ship->home );
            	    ship->home = STRALLOC( ship->spaceobject->name );
            	}
            }
            else if ( ( ship->count >= (ship->tcount ? ship->tcount : 10 ) ) && ship->shipstate == SHIP_HYPERSPACE && ship->tracking == TRUE)
            	{
               	    ship_to_spaceobject (ship, ship->currjump);
            	if (ship->spaceobject == NULL)
            	{
            	    echo_to_cockpit( AT_RED, ship, "Ship lost in Hyperspace. Make new calculations.");
            	}
            	else
            	{

            	    echo_to_room( AT_YELLOW, get_room_index(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 );
		    ship->vx = ship->cx;
		    ship->vy = ship->cy;
		    ship->vz = ship->cz;
		    ship_to_spaceobject( ship, ship->currjump );
		    ship->currjump = NULL;
            	    echo_to_system( AT_YELLOW, ship, buf , NULL );
            	    ship->shipstate = SHIP_READY;
            	    STRFREE( ship->home );
            	    ship->home = STRALLOC( ship->spaceobject->name );
 		    speed = ship->hyperspeed;

		    ship->jx = ship->vx + ship->tx;
		    ship->jy = ship->vy + ship->ty;
		    ship->jz = ship->vz + ship->tz;

		    for( spaceobj = first_spaceobject; spaceobj; spaceobj = spaceobj->next )
		      if( space_in_range( ship, spaceobj ) )
		      {
		        ship->currjump = spaceobj;
 		        break;
		      }
		    if( !spaceobj )
		      ship->currjump = ship->spaceobject;

		    ship->hyperdistance  = abs(ship->vx - ship->jx) ;
    		    ship->hyperdistance += abs(ship->vy - ship->jy) ;
		    ship->hyperdistance += abs(ship->vz - ship->jz) ;
		    ship->hyperdistance /= 50;

		    ship->orighyperdistance = ship->hyperdistance;
		    
		    ship->count = 0;
		    do_radar( ship->ch, "" );
          	}
            }
            
            else if (ship->count >= 10 && ship->shipstate == SHIP_HYPERSPACE)
            {
               ship->count = 0;
               sprintf( buf ,"%d" , ship->hyperdistance );
               echo_to_room_dnr( AT_YELLOW , get_room_index(ship->pilotseat), "Remaining jump distance: " );
               echo_to_room( AT_WHITE , get_room_index(ship->pilotseat),  buf );
               
            }
            if( ship->shipstate == SHIP_HYPERSPACE )
              if( ship->count%2 && ship->hyperdistance < 10*ship->hyperspeed && ship->hyperdistance > 0 )
              {
                sprintf( buf ,"An alarm sounds. Your hyperjump is ending: %d" , ship->hyperdistance );
                echo_to_ship( AT_RED , ship,  buf );
               
              }
        }
   	
     if( ship->docked )
     {
       ship->vx = ship->docked->vx;
       ship->vy = ship->docked->vy;
       ship->vz = ship->docked->vz;
       ship->cx = ship->docked->cx;
       ship->cy = ship->docked->cy;
       ship->cz = ship->docked->cz;
       ship->ox = ship->docked->ox;
       ship->oy = ship->docked->oy;
       ship->oz = ship->docked->oz;
       ship->jx = ship->docked->jx;
       ship->jy = ship->docked->jy;
       ship->jz = ship->docked->jz;
       ship->hx = ship->docked->hx;
       ship->hy = ship->docked->hy;
       ship->hz = ship->docked->hz;
       ship->shipstate = ship->docked->shipstate;
       ship->hyperdistance = ship->docked->hyperdistance;
       ship->currspeed = ship->docked->currspeed;
       ship->orighyperdistance = ship->docked->orighyperdistance;
       ship->location = ship->docked->location;
       ship->dest = ship->docked->dest;
       ship->spaceobject = ship->docked->spaceobject;
       ship->currjump = ship->docked->currjump;
     }  
     
     if( ship->vx > MAX_COORD)
       ship->vx = -MAX_COORD_S;
     if( ship->vy > MAX_COORD)
       ship->vy = -MAX_COORD_S;
     if( ship->vz > MAX_COORD)
       ship->vz = -MAX_COORD_S;
     if( ship->vx > MAX_COORD)
       ship->vx = MAX_COORD_S;
     if( ship->vy > MAX_COORD)
       ship->vy = MAX_COORD_S;
     if( ship->vz > MAX_COORD)
       ship->vz = MAX_COORD_S;
     
   }
/*
       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 , NULL, ship->collision , ship->collision );
           ship->collision = 0;
       }
*/
}   

void recharge_ships( )
{
   SHIP_DATA *ship;
   char buf[MAX_STRING_LENGTH];
   bool closeem = FALSE;
   int distance, origchance = 100;
   baycount++;

   if ( baycount >= 60 )
   {
     closeem = TRUE;
     baycount = 0;
   }

   for ( ship = first_ship; ship; ship = ship->next )
   {                

        if ( ship->class == 3 )
          if ( closeem && ship->guard )
            ship->bayopen = FALSE;
           
        if (ship->statet0 > 0)
        {
           ship->energy -= ship->statet0;
           ship->statet0 = 0;
        }
        if (ship->statei0 > 0)
        {
           ship->energy -= 10*ship->statei0;
           ship->statei0 = 0;
        }
        if (ship->statet1 > 0)
        {
           ship->energy -= ship->statet1;
           ship->statet1 = 0;
        }
        if (ship->statet2 > 0)
        {
           ship->energy -= ship->statet2;
           ship->statet2 = 0;
        }
        if (ship->statet3 > 0)
        {
           ship->energy -= ship->statet3;
           ship->statet3 = 0;
        }
        if (ship->statet4 > 0)
        {
           ship->energy -= ship->statet4;
           ship->statet4 = 0;
        }
        if (ship->statet5 > 0)
        {
           ship->energy -= ship->statet5;
           ship->statet5 = 0;
        }
        if (ship->statet6 > 0)
        {
           ship->energy -= ship->statet6;
           ship->statet6 = 0;
        }
        if (ship->statet7 > 0)
        {
           ship->energy -= ship->statet7;
           ship->statet7 = 0;
        }
        if (ship->statet8 > 0)
        {
           ship->energy -= ship->statet8;
           ship->statet8 = 0;
        }
        if (ship->statet9 > 0)
        {
           ship->energy -= ship->statet9;
           ship->statet9 = 0;
        }
        if (ship->statet10 > 0)
        {
           ship->energy -= ship->statet10;
           ship->statet10 = 0;
        }

	if( ship->docked && ship->docked->class == SHIP_PLATFORM )
	{
	  if( ship->maxenergy - ship->energy > 500 )
	    ship->energy += 500;
	  else
	    ship->energy = ship->maxenergy;
	}

        if (ship->missilestate == MISSILE_RELOAD_2)
        {
           ship->missilestate = MISSILE_READY;
           if ( ship->missiles > 0 )
               echo_to_room( AT_YELLOW, get_room_index(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->spaceobject && ship->class != SHIP_DEBRIS )
          {
             if (ship->target0 && ship->statet0 != LASER_DAMAGED )
             {
                 int chance = 75;
                 SHIP_DATA * target = ship->target0;
                 int shots, guns;
                 int whichguns = 0;
                 
                 if ( ship->lasers && ship->ions && ship->lasers < 7 && ship->ions < 7 )
                   {
                     whichguns = 2;
                     guns = ship->lasers + ship->ions;
                   }
                 else if ( ship->target0->shield > 0 && ship->ions )
                 {
                   whichguns = 1;
                   guns = ship->ions;
                 }
                 else
                   guns = ship->lasers;
                   
                for ( shots=0 ; shots < guns; shots++ )
                {
                  if (ship->shipstate != SHIP_HYPERSPACE && ship->energy > 25 
                  && ship_in_range( ship, target )
                  && abs(target->vx - ship->vx) <= 1000 
                  && abs(target->vy - ship->vy) <= 1000
                  && abs(target->vz - ship->vz) <= 1000 )
                  {
                    if ( ship->class > 1 || is_facing ( ship , target ) )
                    {

	             distance = abs(target->vx - ship->vx) 
        	       + abs(target->vy - ship->vy) 
	               + abs(target->vz - ship->vz);
		     distance /= 3;
		     chance += target->class - ship->class;
		     chance += ship->currspeed - target->currspeed;
		     chance += ship->manuever - target->manuever;
		     chance -= distance/(10*(target->class+1));
		     chance -= origchance;
		     chance /= 2;
		     chance += origchance;
	             chance = URANGE( 1 , chance , 99 );

             		if ( number_percent( ) > chance )
             		{
           		    sprintf( buf , "%s fires at you but misses." , ship->name);  
             		    echo_to_cockpit( AT_ORANGE , target , buf );
      	                    sprintf( buf, "Weaponsfire from %s barely misses %s." , ship->name , target->name );
                            echo_to_system( AT_ORANGE , target , buf , NULL );	
             		} 
             		else
             		{
             		  if( whichguns == 0 )
             		  {
             		    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( ship->class == SHIP_PLATFORM )
			      damage_ship( target, ship, 100, 250 );
			    else if( ship->class == CAPITAL_SHIP && target->class != CAPITAL_SHIP )
			      damage_ship( target, ship, 50, 200 );
			    else 
                              damage_ship( target, ship , 5 , 10 );
                          }
             		  else if( whichguns == 1 )
             		  {
             		    sprintf( buf, "Blue plasma from %s engulfs %s." , ship->name, target->name );
             		    echo_to_system( AT_ORANGE , target , buf , NULL );
                            sprintf( buf , "You are engulfed by ion energy 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( ship->class == SHIP_PLATFORM )
			      damage_ship( target, ship, -250, -100 );
			    else if( ship->class == CAPITAL_SHIP && target->class != CAPITAL_SHIP )
			      damage_ship( target, ship, -200, -50 );
			    else 
                              damage_ship( target, ship , -10, -5 );
                          }
             		  else if( whichguns == 2 )
             		  {
             		    if( shots < ship->lasers )
             		    {
             		    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( ship->class == SHIP_PLATFORM )
			      damage_ship( target, ship, 100, 250 );
			    else if( ship->class == CAPITAL_SHIP && target->class != CAPITAL_SHIP )
			      damage_ship( target, ship, 50, 200 );
			    else 
                              damage_ship( target, ship , 5 , 10 );
                            }
			    else
             		    {
             		    sprintf( buf, "Ion energy from %s hits %s." , ship->name, target->name );
             		    echo_to_system( AT_ORANGE , target , buf , NULL );
                            sprintf( buf , "You are hit by an ion cannon 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( ship->class == SHIP_PLATFORM )
			      damage_ship( target, ship, -250, -100 );
			    else if( ship->class == CAPITAL_SHIP && target->class != CAPITAL_SHIP )
			      damage_ship( target, ship, -200, -50 );
			    else 
                              damage_ship( target, ship , -10, -5 );
                            }
                          }
                        }
                        ship->statet0++;
		        if ( autofly(target) && !target->target0)
		        {
			  sprintf( buf , "You are being targetted by %s." , target->name);  
			  echo_to_cockpit( AT_BLOOD , ship , buf );
			  target->target0 = ship;
			}

                    }
                  }
                }
             }
          }
       }
       
   }
}

void update_space( )
{
   SHIP_DATA *ship;
   SHIP_DATA *target;
   char buf[MAX_STRING_LENGTH];
   int too_close, target_too_close;
   SPACE_DATA *spaceobj;
   int recharge;

   for ( ship = first_ship; ship; ship = ship->next )
   {
          if ( ship->spaceobject && ship->energy > 0 && ship->shipstate == SHIP_DISABLED && ship->class != SHIP_PLATFORM )
             ship->energy -= 100;
          else if ( ship->energy > 0 )
             ship->energy += ( 5 + ship->class*5 );
          else
             ship->energy = 0;

        if ( ship->chaff_released > 0 )
           ship->chaff_released--;
        
        /* 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_READY && ship->tracking == TRUE )
	{
         if( ship->count == 0 )
         {
           ship->count++;
         }
         else
         {
          do_hyperspace( ship->ch, "" );
          ship->count = 0;
         }
        }         
    
    	if (ship->shipstate == SHIP_BUSY_3)
           {
              echo_to_room( AT_YELLOW, get_room_index(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)
        {
           approachland( ship, ship->dest );
           ship->shipstate = SHIP_LAND_2;
        }
        
        if (ship->shipstate == SHIP_LAUNCH_2)
           launchship( ship );
        if (ship->shipstate == SHIP_LAUNCH)
           ship->shipstate = SHIP_LAUNCH_2;

        if (ship->docking == SHIP_DOCK_2)
           dockship( ship->ch , ship );
        if (ship->docking == SHIP_DOCK)
           ship->docking = SHIP_DOCK_2;
        

        ship->shield = UMAX( 0 , ship->shield-1-ship->class);
                
        if (ship->autorecharge && ship->maxshield > ship->shield && ship->energy > 100)
        {
           recharge  = UMIN( ship->maxshield-ship->shield, 10 + ship->class*10 );           
           recharge  = UMIN( recharge , ship->energy/2 -100 );
           recharge  = UMAX( 1, recharge );
           ship->shield += recharge;
           ship->energy -= recharge;        
        }
        
        if ( autofly(ship) && ( ship->energy >= ((25+ship->class*25)*(2+ship->class) ) )
        && ((ship->maxshield - ship->shield) >= ( 25+ship->class*25 ) ) )
        {
    	  recharge  = 25+ship->class*25;
	  recharge  = UMIN(  ship->maxshield-ship->shield , recharge );
    	  ship->shield += recharge;
    	  ship->energy -= ( recharge*2 + recharge * ship->class );        
    	}
        
        
        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->spaceobject && ship->currspeed > 0 )
        {
               sprintf( buf, "%d",
                          ship->currspeed );
               echo_to_room_dnr( AT_BLUE , get_room_index(ship->pilotseat),  "Speed: " );
               echo_to_room_dnr( AT_LBLUE , get_room_index(ship->pilotseat),  buf );
               sprintf( buf, "%.0f %.0f %.0f",
                           ship->vx , ship->vy, ship->vz );
               echo_to_room_dnr( AT_BLUE , get_room_index(ship->pilotseat),  "  Coords: " );
               echo_to_room( AT_LBLUE , get_room_index(ship->pilotseat),  buf );
            if ( ship->pilotseat != ship->coseat )
            {
               sprintf( buf, "%d",
                          ship->currspeed );
               echo_to_room_dnr( AT_BLUE , get_room_index(ship->coseat),  "Speed: " );
               echo_to_room_dnr( AT_LBLUE , get_room_index(ship->coseat),  buf );
               sprintf( buf, "%.0f %.0f %.0f",
                           ship->vx , ship->vy, ship->vz );
               echo_to_room_dnr( AT_BLUE , get_room_index(ship->coseat),  "  Coords: " );
               echo_to_room( AT_LBLUE , get_room_index(ship->coseat),  buf );
            }
        } 

        if ( ship->spaceobject )
        {
          too_close = ship->currspeed + 50;
          too_close = ship->currspeed + 50;

	 for( spaceobj = first_spaceobject; spaceobj; spaceobj = spaceobj->next )
          if ( spaceobj->name &&  strcmp(spaceobj->name,"") && 
               abs(ship->vx - spaceobj->xpos) < too_close && 
               abs(ship->vy - spaceobj->ypos) < too_close &&
               abs(ship->vz - spaceobj->zpos) < too_close )
          {
                sprintf( buf, "Proximity alert: %s  %.0f %.0f %.0f", spaceobj->name,
                         spaceobj->xpos, spaceobj->ypos, spaceobj->zpos);
                echo_to_room( AT_RED , get_room_index(ship->pilotseat),  buf );
          }

          for ( target = first_ship; target; target = target->next )
          { 
            if( (target->docked && target->docked == ship) || (ship->docked &&  ship->docked == target ) )
              continue;
               if ( ship->docked && target->docked && 
                    target->docked == ship->docked )
                 continue;
          	
            target_too_close = too_close+target->currspeed;
            if( target->spaceobject )
              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 &&
                 ship->docked != target && target->docked != ship )
              {
                 sprintf( buf, "Proximity alert: %s  %.0f %.0f %.0f"
                            , target->name, target->vx - ship->vx, target->vy - ship->vy, target->vz - ship->vz);
                 echo_to_room( AT_RED , get_room_index(ship->pilotseat),  buf );    
              }                
          }
          too_close = ship->currspeed + 100;
        }
       

        if (ship->target0 && ship->class <= SHIP_PLATFORM)
        {
               sprintf( buf, "%s   %.0f %.0f %.0f", ship->target0->name,
                          ship->target0->vx , ship->target0->vy, ship->target0->vz );
               echo_to_room_dnr( AT_BLUE , get_room_index(ship->gunseat), "Target: " );
               echo_to_room( AT_LBLUE , get_room_index(ship->gunseat),  buf );
               if (!ship_in_range( ship, ship->target0 ) )
               {
	                echo_to_room( AT_LBLUE , get_room_index(ship->gunseat),  "Your target seems to have left.");
               		ship->target0 = NULL;
               }
         }
         
        if (ship->target1 && ship->class <= SHIP_PLATFORM)
        {
               sprintf( buf, "%s   %.0f %.0f %.0f", ship->target1->name,
                          ship->target1->vx , ship->target1->vy, ship->target1->vz );
               echo_to_room_dnr( AT_BLUE , get_room_index(ship->turret1), "Target: " );
               echo_to_room( AT_LBLUE , get_room_index(ship->turret1),  buf );
               if (!ship_in_range( ship, ship->target1 ) )
               		ship->target1 = NULL;
         }
        
        if (ship->target2 && ship->class <= SHIP_PLATFORM)
        {
               sprintf( buf, "%s   %.0f %.0f %.0f", ship->target2->name,
                          ship->target2->vx , ship->target2->vy, ship->target2->vz );
               echo_to_room_dnr( AT_BLUE , get_room_index(ship->turret2), "Target: " );
               echo_to_room( AT_LBLUE , get_room_index(ship->turret2),  buf );
               if (!ship_in_range( ship, ship->target2 ) )
               		ship->target2 = NULL;
         }
         
   	if (ship->energy < 100 && ship->spaceobject )
   	{
   	    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->target0 && autofly(ship) )
       	   if( !ship_in_range( ship->target0, ship ) )
       	   {
             echo_to_room( AT_BLUE , get_room_index(ship->pilotseat), "Target left, returning to NORMAL condition.\n\r" );
             ship->currspeed = 0;
       	     ship->target0 = NULL;
       	   }


       if (ship->autotrack && ship->docking == SHIP_READY && ship->target0 && ship->class < 3 )
       {
       	     
           target = ship->target0;
           too_close = ship->currspeed + 10;
           target_too_close = too_close+target->currspeed;
           if ( target != ship && ship->shipstate == SHIP_READY &&
           	 ship->docked == NULL && ship->shipstate != SHIP_DOCKED &&
                 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->target0->vx - ship->vx);
              ship->hy = 0-(ship->target0->vy - ship->vy);
              ship->hz = 0-(ship->target0->vz - ship->vz);
              ship->energy -= ship->currspeed/10;
              echo_to_room( AT_RED , get_room_index(ship->pilotseat), "Autotrack: Evading to avoid collision!\n\r" );
    	      if ( ship->class == FIGHTER_SHIP || ( ship->class == MIDSIZE_SHIP && ship->manuever > 50 ) )
        	ship->shipstate = SHIP_BUSY_3;
              else if ( ship->class == MIDSIZE_SHIP || ( ship->class == CAPITAL_SHIP && ship->manuever > 50 ) )
        	ship->shipstate = SHIP_BUSY_2;
    	      else
        	ship->shipstate = SHIP_BUSY;
           }
           else if  ( !is_facing(ship, ship->target0) 
             && ship->docked == NULL && ship->shipstate != SHIP_DOCKED )
           {
              ship->hx = ship->target0->vx - ship->vx;
              ship->hy = ship->target0->vy - ship->vy;
              ship->hz = ship->target0->vz - ship->vz;
              ship->energy -= ship->currspeed/10;
              echo_to_room( AT_BLUE , get_room_index(ship->pilotseat), "Autotracking target ... setting new course.\n\r" );
    	      if ( ship->class == FIGHTER_SHIP || ( ship->class == MIDSIZE_SHIP && ship->manuever > 50 ) )
        	ship->shipstate = SHIP_BUSY_3;
              else if ( ship->class == MIDSIZE_SHIP || ( ship->class == CAPITAL_SHIP && ship->manuever > 50 ) )
        	ship->shipstate = SHIP_BUSY_2;
    	      else
        	ship->shipstate = SHIP_BUSY;     
           }
       }

       if ( autofly(ship) && ship->class != SHIP_DEBRIS )
       {
          if ( ship->spaceobject )
          {
             check_hostile( ship );
             if (ship->target0 )
             {
                 int chance = 50;
		 int projectiles = -1;		

		 if ( !ship->target0->target0 && autofly(ship->target0))
		   ship->target0->target0 = ship;

                 /* auto assist ships */
                  
                 for ( target = first_ship; target; target = target->next)
                 {
                   if( ship_in_range( ship, target ) )
                    if ( autofly(target) && target->docked == NULL && target->shipstate != SHIP_DOCKED )
                       if ( !str_cmp ( target->owner , ship->owner ) && target != ship )
                         if ( target->target0 == NULL && ship->target0 != target )
                         {  
                           target->target0 = ship->target0;
                           sprintf( buf , "You are being targetted by %s." , target->name);  
                           echo_to_cockpit( AT_BLOOD , target->target0 , buf );
                           break;
                        }   
                 }
                   
                 target = ship->target0;
                 ship->autotrack = TRUE;
                 if( ship->class != SHIP_PLATFORM && !ship->guard 
                     && ship->docked == NULL && ship->shipstate != SHIP_DOCKED )
                      ship->currspeed = ship->realspeed;
                 if ( ship->energy >200  )
                    ship->autorecharge=TRUE;
                    

                 if (ship->shipstate != SHIP_HYPERSPACE && ship->energy > 25
                 && ship->missilestate == MISSILE_READY && ship_in_range( ship, target )
                 && abs(target->vx - ship->vx) <= 1200
                 && abs(target->vy - ship->vy) <= 1200
                 && abs(target->vz - ship->vz) <= 1200 )
                 {
                   if ( ship->class > 1 || is_facing( ship , target ) )
                   {
             		chance -= target->manuever/5;
                        chance -= target->currspeed/20;
                        chance += target->class*target->class*25;
                        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( ( target->class == SHIP_PLATFORM || ( target->class == CAPITAL_SHIP && target->currspeed < 50 )) && ship->rockets > 0 )
		     projectiles = HEAVY_ROCKET;
		   else if ( ( target->class == MIDSIZE_SHIP || ( target->class == CAPITAL_SHIP) ) && ship->torpedos > 0 )
		     projectiles = PROTON_TORPEDO;
		   else if ( ship->missiles < 0 && ship->torpedos > 0 )
		     projectiles = PROTON_TORPEDO;
		   else if ( ship->missiles < 0 && ship->rockets > 0 )
		     projectiles = HEAVY_ROCKET;
		   else if ( ship->missiles > 0 )
		     projectiles = CONCUSSION_MISSILE;
		   else
		     projectiles = -1;
		     
             		if ( number_percent( ) > chance || projectiles == -1 )
             		{
             		}
                        else
                        {
                                new_missile( ship , target , NULL , projectiles );
            		 	if( projectiles == CONCUSSION_MISSILE ) ship->missiles--;
            		 	if( projectiles == PROTON_TORPEDO ) ship->torpedos--;
            		 	if( projectiles == HEAVY_ROCKET ) ship->rockets--;
             		        sprintf( buf , "Incoming projectile from %s." , ship->name);
             		        echo_to_cockpit( AT_BLOOD , target , buf );
             		        sprintf( buf, "%s fires a projectile towards %s." , ship->name, target->name );
             		        echo_to_system( AT_ORANGE , target , buf , NULL );

            		 	if ( ship->class == CAPITAL_SHIP || ship->class == SHIP_PLATFORM )
                                    ship->missilestate = MISSILE_RELOAD_2;
                                else
                                    ship->missilestate = MISSILE_FIRED;
                        }
                   }
                 }

                 if( ship->missilestate ==  MISSILE_DAMAGED )
                     ship->missilestate =  MISSILE_READY;
                 if( ship->statet0 ==  LASER_DAMAGED )
                     ship->statet0 =  LASER_READY;
                 if( ship->statei0 ==  LASER_DAMAGED )
                     ship->statei0 =  LASER_READY;
                 if( ship->shipstate ==  SHIP_DISABLED )
                     ship->shipstate =  SHIP_READY;

             }
             else
                 ship->currspeed = 0;
          }
          else
          {
               if ( number_range(1, 25) == 25 )
               {
          	  ship_to_spaceobject(ship, spaceobject_from_name(ship->home) );
		  if( ship->spaceobject )
		  {
          	    ship->vx = ship->spaceobject->xpos + number_range( -5000 , 5000 );
          	    ship->vy = ship->spaceobject->ypos + number_range( -5000 , 5000 );
          	    ship->vz = ship->spaceobject->zpos + number_range( -5000 , 5000 );
                    ship->hx = 1;
                    ship->hy = 1;
                    ship->hz = 1;
                  }
               }
          }
       }

       if ( ( ship->class == CAPITAL_SHIP || ship->class == SHIP_PLATFORM )
       && ship->target0 == NULL )
       {


          if( ship->missiles < ship->maxmissiles )
             ship->missiles++;
          if( ship->torpedos < ship->maxtorpedos )
             ship->torpedos++;
          if( ship->rockets < ship->maxrockets )
             ship->rockets++;
          if( ship->chaff < ship->maxchaff )
             ship->chaff++;
       }
       
       save_ship( ship );
   }

}



void write_spaceobject_list( )
{
    SPACE_DATA *tspaceobject;
    FILE *fpout;
    char filename[256];

    sprintf( filename, "%s%s", SPACE_DIR, SPACE_LIST );
    fpout = fopen( filename, "w" );
    if ( !fpout )
    {
         bug( "FATAL: cannot open space.lst for writing!\n\r", 0 );
         return;
    }
    for ( tspaceobject = first_spaceobject; tspaceobject; tspaceobject = tspaceobject->next )
      fprintf( fpout, "%s\n", tspaceobject->filename );
    fprintf( fpout, "$\n" );
    fclose( fpout );
}


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

/*
 * Get pointer to space structure from the dock vnun.
 */
SPACE_DATA *spaceobject_from_vnum( int vnum )
{
    SPACE_DATA *spaceobject;
    SHIP_DATA *ship;

    for ( spaceobject = first_spaceobject; spaceobject; spaceobject = spaceobject->next )
       if ( vnum == spaceobject->doca ||
            vnum == spaceobject->docb ||
            vnum == spaceobject->docc )
         return spaceobject;

    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->hanger )
       {
         if( !(ship->bayopen) )
           return NULL; 
         return ship->spaceobject;
        }

    return NULL;
}


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

    if ( !spaceobject )
    {
	bug( "save_spaceobject: null pointer!", 0 );
	return;
    }

    if ( !spaceobject->filename || spaceobject->filename[0] == '\0' )
    {
	sprintf( buf, "save_spaceobject: %s has no filename", spaceobject->name );
	bug( buf, 0 );
	return;
    }
 
    sprintf( filename, "%s%s", SPACE_DIR, spaceobject->filename );
    
    fclose( fpReserve );
    if ( ( fp = fopen( filename, "w" ) ) == NULL )
    {
    	bug( "save_spaceobject: fopen", 0 );
    	perror( filename );
    }
    else
    {
	fprintf( fp, "#SPACE\n" );
	fprintf( fp, "Name         %s~\n",	spaceobject->name	);
	fprintf( fp, "Filename     %s~\n",	spaceobject->filename	);
	fprintf( fp, "Type         %d~\n",	spaceobject->type       );
	fprintf( fp, "Locationa      %s~\n",	spaceobject->locationa	);
	fprintf( fp, "Locationb      %s~\n",	spaceobject->locationb	);
	fprintf( fp, "Locationc      %s~\n",	spaceobject->locationc	);
	fprintf( fp, "Doca          %d\n",	spaceobject->doca	);
	fprintf( fp, "Docb          %d\n",      spaceobject->docb       );
	fprintf( fp, "Docc          %d\n",      spaceobject->docc       );        	
	fprintf( fp, "Seca          %d\n",	spaceobject->seca	);
	fprintf( fp, "Secb          %d\n",      spaceobject->secb       );
	fprintf( fp, "Secc          %d\n",      spaceobject->secc       );        	
	fprintf( fp, "Gravity     %d\n",       spaceobject->gravity    );
	fprintf( fp, "Xpos          %.0f\n",       spaceobject->xpos    );
	fprintf( fp, "Ypos          %.0f\n",       spaceobject->ypos    );	
	fprintf( fp, "Zpos          %.0f\n",       spaceobject->zpos    );	
	fprintf( fp, "HX            %.0f\n",       spaceobject->hx      );
	fprintf( fp, "HY            %.0f\n",       spaceobject->hy      );	
	fprintf( fp, "HZ            %.0f\n",       spaceobject->hz      );	
	fprintf( fp, "SP            %d\n",       spaceobject->speed   );	
	fprintf( fp, "Trainer       %d\n",       spaceobject->trainer    );	
	fprintf( fp, "End\n\n"						);
	fprintf( fp, "#END\n"						);
    }
    fclose( fp );
    fpReserve = fopen( NULL_FILE, "r" );
    return;
}


/*
 * Read in actual spaceobject data.
 */

#if defined(KEY)
#undef KEY
#endif

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

void fread_spaceobject( SPACE_DATA *spaceobject, 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 'D':
             KEY( "Doca",  spaceobject->doca,          fread_number( fp ) );
             KEY( "Docb",  spaceobject->docb,          fread_number( fp ) );
             KEY( "Docc",  spaceobject->docc,          fread_number( fp ) );
             break;
                                

	case 'E':
	    if ( !str_cmp( word, "End" ) )
	    {
		if (!spaceobject->name)
		  spaceobject->name		= STRALLOC( "" );
		if (!spaceobject->locationa)
		  spaceobject->locationa            = STRALLOC( "" );  
		if (!spaceobject->locationb)
		  spaceobject->locationb            = STRALLOC( "" );  
		if (!spaceobject->locationc)
		  spaceobject->locationc            = STRALLOC( "" );  
		return;
	    }
	    break;
	    
	case 'F':
	    KEY( "Filename",	spaceobject->filename,		fread_string_nohash( fp ) );
	    break;
        
        case 'G':
            KEY( "Gravity",  spaceobject->gravity,     fread_number( fp ) ); 
            break;  
        
        case 'H':
            KEY( "HX",  spaceobject->hx,     fread_number( fp ) ); 
            KEY( "HY",  spaceobject->hy,     fread_number( fp ) ); 
            KEY( "HZ",  spaceobject->hz,     fread_number( fp ) ); 
            break;  

        case 'L':
	    KEY( "Locationa",	spaceobject->locationa,		fread_string( fp ) );
	    KEY( "Locationb",	spaceobject->locationb,		fread_string( fp ) );
	    KEY( "Locationc",	spaceobject->locationc,		fread_string( fp ) );
	    break;
  
	case 'N':
	    KEY( "Name",	spaceobject->name,		fread_string( fp ) );
	    break;
/*        
        case 'P':
             break;

       	     KEY( "Star1",	spaceobject->star1,	fread_string( fp ) );
	     KEY( "Star2",	spaceobject->star2,	fread_string( fp ) );
	     KEY( "S1x",  spaceobject->s1x,          fread_number( fp ) ); 
             KEY( "S1y",  spaceobject->s1y,          fread_number( fp ) ); 
             KEY( "S1z",  spaceobject->s1z,          fread_number( fp ) ); 
             KEY( "S2x",  spaceobject->s2x,          fread_number( fp ) ); 
             KEY( "S2y",  spaceobject->s2y,          fread_number( fp ) );
             KEY( "S2z",  spaceobject->s2z,          fread_number( fp ) );
*/
       	case 'S':
            KEY( "Seca", spaceobject->seca, 		fread_number( fp ) );
            KEY( "Secb", spaceobject->secb, 		fread_number( fp ) );
            KEY( "Secc", spaceobject->secc, 		fread_number( fp ) );
            KEY( "SP", spaceobject->speed, 		fread_number( fp ) );
        case 'T':
            KEY( "Trainer",  spaceobject->trainer,     fread_number( fp ) ); 
            KEY( "Type",  spaceobject->type,	       fread_number( fp ) ); 
            
        case 'X':
            KEY( "Xpos",  spaceobject->xpos,     fread_number( fp ) ); 
        
        case 'Y':
            KEY( "Ypos",  spaceobject->ypos,     fread_number( fp ) ); 

        case 'Z':
            KEY( "Zpos",  spaceobject->zpos,     fread_number( fp ) ); 
                 
       	}

	if ( !fMatch )
	{
	    sprintf( buf, "Fread_spaceobject: no match: %s", word );
	    bug( buf, 0 );
	}
    }
}

/*
 * Load a spaceobject file
 */

bool load_spaceobject( char *spaceobjectfile )
{
    char filename[256];
    SPACE_DATA *spaceobject;
    FILE *fp;
    bool found;

    CREATE( spaceobject, SPACE_DATA, 1 );

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

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

	found = TRUE;
        LINK( spaceobject, first_spaceobject, last_spaceobject, next, prev );
	for ( ; ; )
	{
	    char letter;
	    char *word;

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

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

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

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

    if ( !(found) )
      DISPOSE( spaceobject );

    return found;
}

/*
 * Load in all the spaceobject files.
 */
void load_space( )
{
    FILE *fpList;
    char *filename;
    char spaceobjectlist[256];
    char buf[MAX_STRING_LENGTH];
    
    
    first_spaceobject	= NULL;
    last_spaceobject	= NULL;

    log_string( "Loading space..." );

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

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

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

    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: setspaceobject <spaceobject> <field> <values>\n\r", ch );
	send_to_char( "\n\rField being one of:\n\r", ch );
	send_to_char( "name filename type trainer,\n\r", ch );
	send_to_char( "xpos ypos zpos gravity seca secb secc,\n\r", ch );
	send_to_char( "locationa locationb locationc doca docb docc\n\r", ch );
	send_to_char( "", ch );
	return;
    }

    spaceobject = spaceobject_from_name( arg1 );
    if ( !spaceobject )
    {
	send_to_char( "No such spaceobject.\n\r", ch );
	return;
    }


    if ( !str_cmp( arg2, "trainer" ) )
    {
      if ( spaceobject->trainer )
	spaceobject->trainer = 0;
      else
        spaceobject->trainer = 1;
        
      send_to_char( "Done.\n\r", ch );
      save_spaceobject( spaceobject );
      return;
    }
    if ( !str_cmp( arg2, "seca" ) )
    {
      if ( spaceobject->seca )
	spaceobject->seca = 0;
      else
        spaceobject->seca = 1;
        
      send_to_char( "Done.\n\r", ch );
      save_spaceobject( spaceobject );
      return;
    }
    if ( !str_cmp( arg2, "secb" ) )
    {
      if ( spaceobject->secb )
	spaceobject->secb = 0;
      else
        spaceobject->secb = 1;
        
      send_to_char( "Done.\n\r", ch );
      save_spaceobject( spaceobject );
      return;
    }
    if ( !str_cmp( arg2, "secc" ) )
    {
      if ( spaceobject->secc )
	spaceobject->secc = 0;
      else
        spaceobject->secc = 1;
        
      send_to_char( "Done.\n\r", ch );
      save_spaceobject( spaceobject );
      return;
    }
    if ( !str_cmp( arg2, "type" ) )
    {
	spaceobject->type = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }

    if ( !str_cmp( arg2, "doca" ) )
    {
	spaceobject->doca = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    if ( !str_cmp( arg2, "docb" ) )
    {
	spaceobject->docb = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    if ( !str_cmp( arg2, "docc" ) )
    {
	spaceobject->docc = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }


    if ( !str_cmp( arg2, "xpos" ) )
    {
	spaceobject->xpos = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }

    if ( !str_cmp( arg2, "ypos" ) )
    {
	spaceobject->ypos = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    
    if ( !str_cmp( arg2, "zpos" ) )
    {
	spaceobject->zpos = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    
    if ( !str_cmp( arg2, "gravity" ) )
    {
	spaceobject->gravity = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    if ( !str_cmp( arg2, "hx" ) )
    {
	spaceobject->hx = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }

    if ( !str_cmp( arg2, "hy" ) )
    {
	spaceobject->hy = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    
    if ( !str_cmp( arg2, "hz" ) )
    {
	spaceobject->hz = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    
    if ( !str_cmp( arg2, "speed" ) )
    {
	spaceobject->speed = atoi( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    
    if ( !str_cmp( arg2, "name" ) )
    {
	STRFREE( spaceobject->name );
	spaceobject->name = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }

    if ( !str_cmp( arg2, "filename" ) )
    {
	DISPOSE( spaceobject->filename );
	spaceobject->filename = strdup( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    
    if ( !str_cmp( arg2, "locationa" ) )
    {
	STRFREE( spaceobject->locationa );
	spaceobject->locationa = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    if ( !str_cmp( arg2, "locationb" ) )
    {
	STRFREE( spaceobject->locationb );
	spaceobject->locationb = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }
    if ( !str_cmp( arg2, "locationc" ) )
    {
	STRFREE( spaceobject->locationc );
	spaceobject->locationc = STRALLOC( argument );
	send_to_char( "Done.\n\r", ch );
	save_spaceobject( spaceobject );
	return;
    }

    do_setspaceobject( ch, "" );
    return;
}

void showspaceobject( CHAR_DATA *ch , SPACE_DATA *spaceobject )
{   
    ch_printf( ch, "Space object:%s     Trainer: %s	Filename: %s  Type %d\n\r",
    			spaceobject->name, 
    			(spaceobject->trainer ? "Yes" : "No"),
    			spaceobject->filename, spaceobject->type );
    ch_printf( ch, "Coordinates: %.0f %0.f %.0f\n\rGravity: %d\n\r",
    			spaceobject->xpos , spaceobject->ypos, spaceobject->zpos, spaceobject->gravity);
    ch_printf( ch, "     doca: %5d (%s)\n\r",
    			spaceobject->doca, spaceobject->locationa);
    ch_printf( ch, "     docb: %5d (%s)\n\r",
    			spaceobject->docb, spaceobject->locationb);
    ch_printf( ch, "     docc: %5d (%s)\n\r",
    			spaceobject->docc, spaceobject->locationc);
    return;
}

void do_showspaceobject( CHAR_DATA *ch, char *argument )
{
   SPACE_DATA *spaceobject;

   spaceobject = spaceobject_from_name( argument );
   
   if ( spaceobject == NULL )
      send_to_char("&RNo such spaceobject.\n\r",ch);
   else
      showspaceobject(ch , spaceobject);
   
}

void do_makespaceobject( CHAR_DATA *ch, char *argument )
{   
    char arg[MAX_INPUT_LENGTH];
    char filename[256];
    SPACE_DATA *spaceobject;

    if ( !argument || argument[0] == '\0' )
    {
	send_to_char( "Usage: makespaceobject <spaceobject name>\n\r", ch );
	return;
    }


    CREATE( spaceobject, SPACE_DATA, 1 );
    LINK( spaceobject, first_spaceobject, last_spaceobject, next, prev );

    spaceobject->name		= STRALLOC( argument );
    
		  spaceobject->locationa            = STRALLOC( "" );  
		  spaceobject->locationb            = STRALLOC( "" );  
		  spaceobject->locationc            = STRALLOC( "" );  
    
    argument = one_argument( argument, arg );
    sprintf( filename, "%s.system" , strlower(arg) );
    spaceobject->filename = str_dup( filename );
    save_spaceobject( spaceobject );
    write_spaceobject_list();
}

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

    set_char_color( AT_RED, ch );

    for ( spaceobject = first_spaceobject; spaceobject; spaceobject = spaceobject->next )
    {
        if( spaceobject->type > SPACE_SUN )
          continue;
        if ( !(spaceobject->trainer && (!IS_GOD(ch))) )
          ch_printf( ch, "%s\n\r", spaceobject->name );
        count++;
    }

    ch_printf( ch, "\n\r" );

    set_char_color( AT_NOTE, ch );

    for ( spaceobject = first_spaceobject; spaceobject; spaceobject = spaceobject->next )
    {
        if( spaceobject->type != SPACE_PLANET )
          continue;
        if ( !(spaceobject->trainer && (!IS_GOD(ch))) )
          ch_printf( ch, "%s\n\r", spaceobject->name );
        count++;
    }

    if ( !count )
    {
        send_to_char( "There are no spaceobjects currently formed.\n\r", ch );
	return;
    }
}
   
void echo_to_ship( int color , SHIP_DATA *ship , char *argument )
{
     int room;
      
     for ( room = ship->firstroom ; room <= ship->lastroom ;room++ )
     {
         echo_to_room( color , get_room_index(room) , argument );
     }  
     
}

void sound_to_ship( SHIP_DATA *ship , char *argument )
{
     int roomnum;
     ROOM_INDEX_DATA *room;
     CHAR_DATA *vic;
      
     for ( roomnum = ship->firstroom ; roomnum <= ship->lastroom ;roomnum++ )
     {
        room = get_room_index( roomnum );
        if ( room == NULL ) continue;
        
        for ( vic = room->first_person; vic; vic = vic->next_in_room )
        {
	   if ( !IS_NPC(vic) && IS_SET( vic->act, PLR_SOUND ) )
	     send_to_char( argument, vic );
        }
     }  
     
}

void echo_to_docked( int color , SHIP_DATA *ship , char *argument )
{
  SHIP_DATA *dship;
  
  for( dship = first_ship; dship; dship = dship->next )
    if( dship->docked && dship->docked == ship)
      echo_to_cockpit( color, dship, argument );
}

void echo_to_cockpit( int color , SHIP_DATA *ship , char *argument )
{
     int room;
      
     for ( room = ship->firstroom ; room <= ship->lastroom ;room++ )
     {
         if ( room == ship->cockpit || room == ship->navseat
         || room == ship->pilotseat || room == ship->coseat
         || room == ship->gunseat || room == ship->engineroom
         || room == ship->turret1 || room == ship->turret2
         || room == ship->turret3 || room == ship->turret4
         || room == ship->turret5 || room == ship->turret6
         || room == ship->turret7 || room == ship->turret8
         || room == ship->turret9 || room == ship->turret0)
               echo_to_room( color , get_room_index(room) , argument );
     }  
     
}

bool ship_in_range( SHIP_DATA *ship, SHIP_DATA *target )
{
  if (target && ship && target != ship )
    if ( target->spaceobject && ship->spaceobject && 
         target->shipstate != SHIP_LANDED &&
         abs(target->vx - ship->vx) < 100*(ship->sensor+10)*((target->class == SHIP_DEBRIS ? 2 : target->class)+3) &&
         abs(target->vy - ship->vy) < 100*(ship->sensor+10)*((target->class == SHIP_DEBRIS ? 2 : target->class)+3) &&
         abs(target->vz - ship->vz) < 100*(ship->sensor+10)*((target->class == SHIP_DEBRIS ? 2 : target->class)+3) )
      return TRUE;
  return FALSE;
}	

bool missile_in_range( SHIP_DATA *ship, MISSILE_DATA *missile )
{
  if ( missile && ship )
    if ( ship->spaceobject && 
         abs(missile->mx - ship->vx) < 5000 &&
         abs(missile->my - ship->vy) < 5000 &&
         abs(missile->mz - ship->vz) < 5000 )
      return TRUE;
  return FALSE;
}	

bool space_in_range( SHIP_DATA *ship, SPACE_DATA *object )
{
  if (object && ship )
    if ( ship->spaceobject && 
         abs(object->xpos - ship->vx) < 100000 &&
         abs(object->ypos - ship->vy) < 100000 &&
         abs(object->zpos - ship->vz) < 100000 )
      return TRUE;
  return FALSE;
}	
bool space_in_range_c( SHIP_DATA *ship, SPACE_DATA *object )
{
  if (object && ship )
    if ( abs(object->xpos - ship->vx) < 10000 &&
         abs(object->ypos - ship->vy) < 10000 &&
         abs(object->zpos - ship->vz) < 10000 )
      return TRUE;
  return FALSE;
}	
bool space_in_range_h( SHIP_DATA *ship, SPACE_DATA *object )
{
  if (object && ship )
    if ( abs(object->xpos - ship->cx) < (object->gravity*5) &&
         abs(object->ypos - ship->cy) < (object->gravity*5) &&
         abs(object->zpos - ship->cz) < (object->gravity*5) )
      return TRUE;
  return FALSE;
}	

char * print_distance_ship( SHIP_DATA *ship, SHIP_DATA *target )
{
	char *buf;
	float hx, hy, hz;
	hx = target->vx - ship->vx;
	hy = target->vy - ship->vy;
	hz = target->vz - ship->vz;
	
	
	sprintf( buf, "%.0f %.0f %.0f", hx, hy, hz);
	
	return buf;
}
void echo_to_system( int color , SHIP_DATA *ship , char *argument , SHIP_DATA *ignore )
{
     SHIP_DATA *target;

     if (!ship->spaceobject)
        return;

     for ( target = first_ship; target; target = target->next )
     {
       if( !ship_in_range( ship, target ) )
         continue;
       if (target != ship && target != ignore )
         if ( abs(target->vx - ship->vx) < 100*(target->sensor+10)*((ship->class == SHIP_DEBRIS ? 2 : ship->class)+1) &&
         abs(target->vy - ship->vy) < 100*(target->sensor+10)*((ship->class == SHIP_DEBRIS ? 2 : ship->class)+1) &&
         abs(target->vz - ship->vz) < 100*(target->sensor+10)*((ship->class == SHIP_DEBRIS ? 2 : ship->class)+1) )


           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;
}
     	     								               	             	

long int get_ship_value( SHIP_DATA *ship )
{
     long int price;
          
     if (ship->class == FIGHTER_SHIP)
        price = 5000;
     else if (ship->class == MIDSIZE_SHIP)
     	price = 50000; 
     else if (ship->class == CAPITAL_SHIP) 
        price = 500000;
     else 
        price = 2000;
        
     if ( ship->class <= CAPITAL_SHIP ) 
       price += ( ship->manuever*100*(1+ship->class) );
     
     price += ( ship->tractorbeam * 100 );
     price += ( ship->realspeed * 10 );
     price += ( ship->astro_array *5 );
     price += ( 5 * ship->maxhull );
     price += ( 2 * ship->maxenergy );

     if (ship->maxenergy > 5000 )
          price += ( (ship->maxenergy-5000)*20 ) ;
     
     if (ship->maxenergy > 10000 )
          price += ( (ship->maxenergy-10000)*50 );
     
     if (ship->maxhull > 1000)
        price += ( (ship->maxhull-1000)*10 );
     
     if (ship->maxhull > 10000)
        price += ( (ship->maxhull-10000)*20 );
        
     if (ship->maxshield > 200)
          price += ( (ship->maxshield-200)*50 );
     
     if (ship->maxshield > 1000)
          price += ( (ship->maxshield-1000)*100 );

     if (ship->realspeed > 100 )
        price += ( (ship->realspeed-100)*500 ) ;
        
     if (ship->lasers > 5 )
        price += ( (ship->lasers-5)*500 );
      
     if (ship->maxshield)
     	price += ( 1000 + 10 * ship->maxshield);
     
     if (ship->lasers)
     	price += ( 500 + 500 * ship->lasers );
    
     if (ship->missiles )
     	price += ( 250 * ship->missiles );
     else if (ship->torpedos )
     	price += ( 500 * ship->torpedos );
     else if (ship->rockets )
        price += ( 1000 * ship->rockets );
         
     if (ship->turret1)
        price += 5000;
        
     if (ship->turret2)
        price += 5000;
     
     if (ship->hyperspeed)
        price += ( 1000 + ship->hyperspeed * 10 );
     
     if (ship->hanger)
        price += ( ship->class == MIDSIZE_SHIP ? 50000 : 100000 );
 
     price *= 1.5;
     
     return price;
     
}

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;
    }
    for ( tship = first_ship; tship; tship = tship->next )
    {
      if( tship->class != SHIP_DEBRIS )
        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;
     
    for ( ship = room->first_ship ; ship ; ship = ship->next_in_room )
    {
    if( ship->personalname )
     if ( !str_cmp( name, ship->personalname ) )
       return ship;
     if ( !str_cmp( name, ship->name ) )
         return ship;
    }
    for ( ship = room->first_ship ; ship ; ship = ship->next_in_room )
    {
    if( ship->personalname )
     if ( nifty_is_name_prefix( name, ship->personalname ) )
         return ship;
     if ( nifty_is_name_prefix( name, ship->name ) )
         return ship;
    }
    return NULL;    
}

/*
 * Get pointer to ship structure from ship name.
 */
SHIP_DATA *get_ship( char *name )
{
    SHIP_DATA *ship;
    

    for ( ship = first_ship; ship; ship = ship->next )
    {
    if( ship->personalname )
     if ( !str_cmp( name, ship->personalname ) )
         return ship;
     if ( !str_cmp( name, ship->name ) )
         return ship;
    if( ship->personalname )
     if ( nifty_is_name_prefix( name, ship->personalname ) )
         return ship;
     if ( nifty_is_name_prefix( name, ship->name ) )
         return ship;
    }
/*  for ( ship = first_ship; ship; ship = ship->next )
    {
    }
*/
    return NULL;
}

/*
 * Checks if ships in a spaceobject and returns poiner if it is.
 */
SHIP_DATA *get_ship_here( char *name , SHIP_DATA *eShip)
{
    SHIP_DATA *ship;
    int number, count = 0;
    char arg[MAX_INPUT_LENGTH];

    if ( eShip == NULL )
         return NULL;

    number = number_argument( name, arg );
    
    for ( ship = first_ship ; ship; ship = ship->next )
    {
     if( !ship_in_range( eShip, ship ) )
       continue;
     if( !ship->spaceobject )
       continue;
     if( ship->personalname )
      if ( !str_cmp( arg, ship->personalname ) )
      {
       count++;
       if( !number || count == number )
         return ship;
     }
     if ( !str_cmp( arg, ship->name ) )
     {
       count++;
       if( !number ||  count == number )
         return ship;
     }
    }
    count = 0;
    for ( ship = first_ship; ship; ship = ship->next )
    {
     if( !ship_in_range( eShip, ship ) )
       continue;
     if( ship->personalname )
      if ( nifty_is_name_prefix( arg, ship->personalname ) )
      {
       count++;
       if(  !number || count == number )
         return ship;
     }
     if ( nifty_is_name_prefix( arg, ship->name ) )
     {
       count++;
       if(  !number || count == number )
         return ship;
     }
    }
    return NULL;
}


/*
 * Get pointer to ship structure from pilot name.
 */
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 pilot name.
 */
SHIP_DATA *ship_from_pilot_num( char *name, int num )
{
    SHIP_DATA *ship;
    int count = 1;
    
    if ( !num )
      num = 1;
    for ( ship = first_ship; ship; ship = ship->next )
    {
       if ( !str_cmp( name, ship->pilot ) && count == num )
         return ship;
       if ( !str_cmp( name, ship->copilot ) && count == num )
         return ship;
       if ( !str_cmp( name, ship->owner ) && count == num )
         return ship;  
       count++;
    }
    return NULL;
}

/*
 * Get pointer to ship structure from cockpit, turret, or entrance ramp vnum.
 */
 
SHIP_DATA *ship_from_cockpit( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->cockpit || vnum == ship->turret1 || vnum == ship->turret2
          || vnum == ship->turret3 || vnum == ship->turret4 || vnum == ship->turret5
          || vnum == ship->turret6 || vnum == ship->turret7 || vnum == ship->turret8
          || vnum == ship->turret9 || vnum == ship->turret0 || vnum == ship->hanger
          || vnum == ship->pilotseat || vnum == ship->coseat || vnum == ship->navseat
          || vnum == ship->gunseat  || vnum == ship->engineroom )
         return ship;
    return NULL;
}

SHIP_DATA *ship_from_pilotseat( int vnum )
{
    SHIP_DATA *ship;

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

SHIP_DATA *ship_from_coseat( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->coseat )
         return ship;
    return NULL;
}

SHIP_DATA *ship_from_navseat( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->navseat )
         return ship;
    return NULL;
}

SHIP_DATA *ship_from_gunseat( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->gunseat )
         return ship;
    return NULL;
}

SHIP_DATA *ship_from_engine( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
    {   
      if (ship->engineroom)   
      {  
        if ( vnum == ship->engineroom )
          return ship;
      }
      else
      { 
        if ( vnum == ship->cockpit )
          return ship;
      }
    }

    return NULL;
}



SHIP_DATA *ship_from_turret( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->gunseat || vnum == ship->turret1 || vnum == ship->turret2
          || vnum == ship->turret3 || vnum == ship->turret4 || vnum == ship->turret5
          || vnum == ship->turret6 || vnum == ship->turret7 || vnum == ship->turret8
          || vnum == ship->turret9 || vnum == ship->turret0 )
         return ship;
    return NULL;
}

SHIP_DATA *ship_from_entrance( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->entrance )
         return ship;
    return NULL;
}

SHIP_DATA *ship_from_hanger( int vnum )
{
    SHIP_DATA *ship;
    
    for ( ship = first_ship; ship; ship = ship->next )
       if ( vnum == ship->hanger )
         return ship;
    return NULL;
}


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

    if ( !ship )
    {
	bug( "save_ship: null ship pointer!", 0 );
	return;
    }
    
    
    if ( ship->class == SHIP_DEBRIS )
      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, "PersonalName         %s~\n",	ship->personalname		);
	fprintf( fp, "Filename     %s~\n",	ship->filename		);
        fprintf( fp, "Description  %s~\n",	ship->description	);
	fprintf( fp, "Owner        %s~\n",	ship->owner		);
	fprintf( fp, "Pilot        %s~\n",      ship->pilot             );
	fprintf( fp, "Copilot      %s~\n",      ship->copilot           );
	fprintf( fp, "Class        %d\n",	ship->class		);
	fprintf( fp, "Tractorbeam  %d\n",	ship->tractorbeam	);
	fprintf( fp, "Shipyard     %d\n",	ship->shipyard		);

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

Site Stats

Snippets: 1791
Codebases: 571