/
Sapphire/bin/
Sapphire/db/
Sapphire/db/OLC_rooms/
Sapphire/db/abi/
Sapphire/db/em_src/
Sapphire/db/helps/
Sapphire/db/helps/emman/ifunc/
Sapphire/db/npcs/Tatt/
Sapphire/db/objects/Tatt/
Sapphire/db/q_data/
Sapphire/db/rooms/Tatt/
Sapphire/doc/
Sapphire/doc/em/
Sapphire/etc/
Sapphire/src/abic/
Sapphire/src/areacon/
Sapphire/src/client/
Sapphire/src/embc/
Sapphire/src/emi/
Sapphire/src/emi/test/
Sapphire/src/include/
Sapphire/src/sapphire/em/
Sapphire/src/tcon/
/*
 * Copyright (C) 1995-1997 Christopher D. Granz
 *
 * This header may not be removed.
 *
 * Refer to the file "License" included in this package for further
 * information and before using any of the following.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "areacon.h"


/*
 * Globals
 */
static ROOM_INDEX_DATA *                                      pRoomList;
char                                                          cNullChar;


/*
 * Functions
 */
int main( int iArgC, char *pArgV[] )
{
    FILE *pFile;
    char *pWord;
    int i;

    /* if ( iArgC < 2 )
    {
        fprintf( stderr, "\nUsage: %s [input files]\n", pArgV[0] );
        return ( 0 );
    } */

    for ( i = 1; i < iArgC; i++ )
    {
        if ( ( pFile = fopen( pArgV[i], "r" ) ) == NULL )
        {
            error( "%s: %s.\n", pArgV[i], strerror( errno ) );
            continue;
        }

        for ( ; ; )
        {
            if ( fget_letter( pFile ) != '#' )
                fatal( "%s: `#' not found.", pArgV[i] );

            pWord = fget_word( pFile );

            if ( pWord[0] == '$' )
                break;
            else if ( strcmp( pWord, "AREA" ) == 0 )
                convert_area( pFile );
            else if ( strcmp( pWord, "SOCIALS" ) == 0 )
                convert_socials( pFile );
            else if ( strcmp( pWord, "HELPS" ) == 0 )
                convert_helps( pFile );
            else if ( strcmp( pWord, "MOBILES" ) == 0 )
                convert_mobiles( pFile );
            else if ( strcmp( pWord, "OBJECTS" ) == 0 )
                convert_objects( pFile );
            else if ( strcmp( pWord, "ROOMS" ) == 0 )
                convert_rooms( pFile );
            else if ( strcmp( pWord, "RESETS" ) == 0 )
                convert_resets( pFile );
            else if ( strcmp( pWord, "SHOPS" ) == 0 )
                convert_shops( pFile );
            else if ( strcmp( pWord, "SPECIALS" ) == 0 )
                convert_specials( pFile );
            else
                fatal( "%s: Unknown section `#%s'.", pArgV[i], pWord );
        }

        fclose( pFile );
        fput_rooms( );
        fprintf( stderr, "%s: Converted.\n", pArgV[i] );
    }

    if ( iArgC < 2 )
    {
        char cBuf[MAX_STRING];

        for ( ; ; )
        {
            strcpy( cBuf, fget_word( stdin ) );

            if ( strcmp( cBuf, "$" ) == 0 )
                break;

            if ( ( pFile = fopen( cBuf, "r" ) ) == NULL )
            {
                error( "%s: %s.\n", cBuf, strerror( errno ) );
                continue;
            }

            for ( ; ; )
            {
                if ( fget_letter( pFile ) != '#' )
                    fatal( "%s: `#' not found.", cBuf );

                pWord = fget_word( pFile );

                if ( pWord[0] == '$' )
                    break;
                else if ( strcmp( pWord, "AREA" ) == 0 )
                    convert_area( pFile );
                else if ( strcmp( pWord, "SOCIALS" ) == 0 )
                    convert_socials( pFile );
                else if ( strcmp( pWord, "HELPS" ) == 0 )
                    convert_helps( pFile );
                else if ( strcmp( pWord, "MOBILES" ) == 0 )
                    convert_mobiles( pFile );
                else if ( strcmp( pWord, "OBJECTS" ) == 0 )
                    convert_objects( pFile );
                else if ( strcmp( pWord, "ROOMS" ) == 0 )
                    convert_rooms( pFile );
                else if ( strcmp( pWord, "RESETS" ) == 0 )
                    convert_resets( pFile );
                else if ( strcmp( pWord, "SHOPS" ) == 0 )
                    convert_shops( pFile );
                else if ( strcmp( pWord, "SPECIALS" ) == 0 )
                    convert_specials( pFile );
                else
                    fatal( "%s: Unknown section `#%s'.", cBuf, pWord );
            }

            fclose( pFile );
            fput_rooms( );
            fprintf( stderr, "%s: Converted.\n", cBuf );
        }
    }

    return ( 0 );
}


char *remove_last_nl( char *pStr )
{
    static char cOutput[MAX_STRING];
    int i;

    strncpy( cOutput, pStr, ( MAX_STRING - 1 ) );
    cOutput[MAX_STRING - 1] = '\0';
    i                       = ( strlen( cOutput ) - 1 );

    if ( cOutput[i] == '\n' )
        cOutput[i]     = '\0';
    else if ( cOutput[i] == '\r' && i > 0 && cOutput[i - 1] == '\n' )
        cOutput[i - 1] = '\0';

    return ( cOutput );
}


/*
 * Skips over an area header.
 */
void convert_area( FILE *pFile )
{
    free_mem( fget_string( pFile ) );
    free_mem( fget_string( pFile ) );
    free_mem( fget_string( pFile ) );
    fget_number( pFile );
    fget_number( pFile );
}


/*
 * Skips over a `#SOCIALS' section.
 */
void convert_socials( FILE *pFile )
{
    char *pStr;

    for ( ; ; ) 
    {
        if ( strcmp( ( pStr = fget_word( pFile ) ), "#0" ) == 0 )
            return;

        fget_string_eol( pFile );

	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;

	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;

	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;

	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;

	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;

	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;

	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;
         
	  if ( strcmp( fget_string_eol( pFile ), "#" ) == 0 )
            continue;
    }
}


/*
 * Skips over a `#HELPS' section.
 */
void convert_helps( FILE *pFile )
{
    char *pStr;

    for ( ; ; )
    {
        fget_number( pFile );
        pStr = fget_string( pFile );

        if ( pStr[0] == '$' )
            break;

        free_mem( pStr );
        free_mem( fget_string( pFile ) );
    }
}


/*
 * Converts a `#MOBILES' section.
 */
void convert_mobiles( FILE *pFile )
{
    NPC_INDEX_DATA *pNPCIndex;
    int iNumber;
    char c;

    {
        char *temp;

        for ( temp = fget_word( pFile ); strcmp( temp, "#0" );
          temp = fget_word( pFile ) );

        return;
    }

    for ( ; ; )
    { 
        if ( fget_letter( pFile ) != '#' )
            fatal( "Symbol `#' not found." );
 
        if ( ( iNumber = fget_number( pFile ) ) == 0 )
            break;
  
        pNPCIndex                    = alloc_mem( sizeof( *pNPCIndex ) );
        pNPCIndex->iNumber           = iNumber;
        pNPCIndex->pNameList         = (char **) fget_string( pFile );
        pNPCIndex->sShortDescs[0]    = fget_string( pFile );
        pNPCIndex->sDesc             = fget_string( pFile );
        pNPCIndex->sLongDesc         = fget_string( pFile );
	  fget_string( pFile ); /* Race. */
 
        pNPCIndex->sDesc[0]          = UPPER( pNPCIndex->sDesc[0] );
        pNPCIndex->sLongDesc[0]      = UPPER( pNPCIndex->sLongDesc[0] );
 
        fget_flags( pFile ); /* Action flags. */
        fget_flags( pFile ); /* Affected by. */

        fget_number( pFile ); /* Alignment. */
        fget_number( pFile ); /* Group. */

        pNPCIndex->iLevel            = fget_number( pFile );
        pNPCIndex->iHitRoll          = fget_number( pFile );

        /* Read HP dice. */
        pNPCIndex->VarStats.iStatHP[0]  = fget_number( pFile ); 
        fget_letter( pFile ); /* 'd' */
        pNPCIndex->VarStats.iStatHP[1]  = fget_number( pFile );
        fget_letter( pFile ); /* '+' */
        pNPCIndex->VarStats.iStatHP[2]  = fget_number( pFile );

        /* Read MP dice. */
        pNPCIndex->VarStats.iStatMP[0]  = fget_number( pFile );
        fget_letter( pFile ); /* 'd' */
        pNPCIndex->VarStats.iStatMP[1]  = fget_number( pFile );
        fget_letter( pFile ); /* '+' */
        pNPCIndex->VarStats.iStatMP[2]  = fget_number( pFile );

        /* Read damage dice. */
        pNPCIndex->iDamage[0]           = fget_number( pFile );
        fget_letter( pFile ); /* 'd' */
        pNPCIndex->iDamage[1]           = fget_number( pFile );
        fget_letter( pFile ); /* '+' */
        pNPCIndex->iDamage[2]           = fget_number( pFile );

        fget_word( pFile ); /* Damage type. */

        /* Read armor class. */
        pNPCIndex->iAC[0] /* Pierce. */ = ( fget_number( pFile ) * 10 );
        pNPCIndex->iAC[1] /* Bash.   */ = ( fget_number( pFile ) * 10 );
        pNPCIndex->iAC[2] /* Slash.  */ = ( fget_number( pFile ) * 10 );
        pNPCIndex->iAC[3] /* Exotic. */ = ( fget_number( pFile ) * 10 );

        fget_flags( pFile ); /* off_flags  */
        fget_flags( pFile ); /* imm_flags  */
        fget_flags( pFile ); /* res_flags  */
        fget_flags( pFile ); /* vuln_flags */

        fget_word( pFile ); /* start_pos   */
        fget_word( pFile ); /* default_pos */
        fget_word( pFile ); /* sex         */
        fget_number( pFile ); /* wealth    */

        fget_flags( pFile ); /* form  */
        fget_flags( pFile ); /* parts */

        fget_word( pFile ); /* size     */
        fget_word( pFile ); /* material */
 
        for ( ; ; )
        {
            if ( ( c = fget_letter( pFile ) ) == 'F' )
            {
                fget_word( pFile );
                fget_flags( pFile );
            }
            else
            {
                ungetc( c, pFile );
                break;
            }
        }

        free_mem( pNPCIndex->pNameList );
        free_mem( pNPCIndex->sShortDescs[0] );
        free_mem( pNPCIndex->sDesc );
        free_mem( pNPCIndex->sLongDesc );
        free_mem( pNPCIndex );
    }
}


/*
 * Converts a `#OBJECTS' section.
 */
void convert_objects( FILE *pFile )
{
    {
        char *temp;

        for ( temp = fget_word( pFile ); strcmp( temp, "#0" );
          temp = fget_word( pFile ) );

        return;
    }
}



/*
 * Converts a `#ROOMS' section.
 */
void convert_rooms( FILE *pFile )
{
    ROOM_INDEX_DATA *pRoom;
    char c;
    int iNumber;
    int i;

    for ( ; ; )
    {
        if ( fget_letter( pFile ) != '#' )
            fatal( "Symbol `#' not found." );

        if ( ( iNumber = fget_number( pFile ) ) == 0 )
            break;

        if ( iNumber < 0 )
            fatal( "Illegal room number `%d'.", iNumber );

        if ( get_room( iNumber ) != NULL )
            fatal( "More then one room with the number `%d'.", iNumber );

        pRoom              = alloc_mem( sizeof( *pRoom ) );
        pRoom->iNumber     = iNumber;
        pRoom->sTitle      = fget_string( pFile );
        pRoom->sDesc       = fget_string( pFile );
        fget_number( pFile );
        pRoom->fRoomFlags  = fget_flags( pFile );
        pRoom->iSectorType = fget_number( pFile );

        for ( ; ; )
        {
            if ( ( c = fget_letter( pFile ) ) == 'S' )
                break;

            switch ( c )
            {
              case 'H':
              case 'M': fget_number( pFile );             break;
              case 'O':
              case 'C': free_mem( fget_string( pFile ) ); break;
              case 'D':
                  if ( ( i = fget_number( pFile ) ) < 0 || i >= 6 )
                      fatal( "Room %d: Illegal exit number.", iNumber );

                  /* Quick fix. */
                  if ( i == 1 )
                      i                          = 2;
                  else if ( i == 2 )
                      i                          = 1;

                  pRoom->sDirDescs[i]            = fget_string( pFile );
                  pRoom->eExits[i].pDoorNames    = (char **) fget_string(
                                                               pFile );
                  fget_number( pFile ); /* Locks. */
                  pRoom->eExits[i].uKey.iNumber  = fget_number( pFile );
                  pRoom->eExits[i].uRoom.iNumber = fget_number( pFile );
                  break;

              case 'E':
                  free_mem( fget_string( pFile ) );
                  free_mem( fget_string( pFile ) );
                  break;

              default :
                  fatal( "Room %d: Unknown command.", iNumber );
                  break;
            }
        }

        pRoom->pNext       = pRoomList;
        pRoomList          = pRoom;
    }
}


/*
 * Converts a `#RESETS' section.
 */
void convert_resets( FILE *pFile )
{
    ROOM_INDEX_DATA *pRoom;
    NPC_RESET_DATA *pNPCReset;
    OBJ_RESET_DATA *pObjReset;
    char c;
    int i;

    {
        char *temp;

        for ( temp = fget_word( pFile ); strcmp( temp, "S" );
          temp = fget_word( pFile ) );

        return;
    }

    for ( ; ; )
    {
        if ( ( c = fget_letter( pFile ) ) == 'S' )
            break;

        if ( c == '*' )
        {
            fget_string_eol( pFile );
            continue;
        }

        switch ( c )
        {
          case 'M':
              pNPCReset             = alloc_mem( sizeof( *pNPCReset ) );
              fget_number( pFile );
              pNPCReset->u1.iNumber = fget_number( pFile );
              pNPCReset->iResetTime = 600; /* 10 min. */
              fget_number( pFile );
              i                     = fget_number( pFile );
              fget_number( pFile );
              fget_string_eol( pFile );

              if ( ( pRoom = get_room( i ) ) == NULL )
                  fatal( "Bad room number `%d' in mobile reset.", i );

              pNPCReset->pNext      = pRoom->pNPCResets;
              pRoom->pNPCResets     = pNPCReset;
              break;

          case 'O':
              pObjReset             = alloc_mem( sizeof( *pObjReset ) );
              fget_number( pFile );
              pObjReset->u1.iNumber = fget_number( pFile );
              pObjReset->iResetTime = 600; /* 10 min. */
              fget_number( pFile );
              i                     = fget_number( pFile );
              fget_string_eol( pFile );

              if ( ( pRoom = get_room( i ) ) == NULL )
                  fatal( "Bad room number `%d' in object reset.", i );

              pObjReset->pNext      = pRoom->pObjResets;
              pRoom->pObjResets     = pObjReset;
              break;

          case 'G':
          case 'E':
              fget_string_eol( pFile );

              /* temp_index = get_obj_index( pReset->arg1 );
              temp_index->reset_num++; */

              break;

          case 'P':
              fget_string_eol( pFile );

              /* temp_index = get_obj_index( pReset->arg1 );
              temp_index->reset_num++;
              get_obj_index( pReset->arg3 ); */

              break;

          case 'D':
              fget_number( pFile );
              pRoom                  = get_room( fget_number( pFile ) );
              i                      = fget_number( pFile );
              fget_number( pFile ); /* Locks: Not yet imp'd. */
              fget_number( pFile );
              fget_string_eol( pFile );

              if ( i < 0 || i > 5 )
                  fatal( "Illegal direction in door reset." );

              if ( pRoom->eExits[i].pDoorNames == NULL )
                  fatal( "Door reset on non-existent exit." );

              pRoom->eExits[i].iExitType = TRUE;
              break;

          default :
              fatal( "Unknown command `%c' in reset.", c );
              break;
        }
    }
}


/*
 * Converts a `#SHOPS' section.
 */
void convert_shops( FILE *pFile )
{
    {
        char *temp;

        for ( temp = fget_string_eol( pFile ); strcmp( temp, "0" );
          temp = fget_string_eol( pFile ) );

        return;
    }
}


/*
 * Converts a `#SPECIALS' section.
 */
void convert_specials( FILE *pFile )
{
    {
        char *temp;

        for ( temp = fget_word( pFile ); strcmp( temp, "S" );
          temp = fget_word( pFile ) );

        return;
    }
}


/*
 * Returns a room structure given a number.
 */
ROOM_INDEX_DATA *get_room( int iNumber )
{
    ROOM_INDEX_DATA *pRoom;

    for ( pRoom = pRoomList; pRoom != NULL; pRoom = pRoom->pNext )
    {
        if ( pRoom->iNumber == iNumber )
            break;
    }

    return ( pRoom );
}


/*
 * This needs to be called after both the rooms and the resets have
 * been loaded, since Sapphire's resets need to be in the room files.
 */
void fput_rooms( void )
{
    FILE *pFile;
    ROOM_INDEX_DATA *pRoom;
    ROOM_INDEX_DATA *pRoomNext;
    char cBuf[256];
    int i;

    for ( pRoom = pRoomList; pRoom != NULL; pRoom = pRoomNext )
    {
        pRoomNext = pRoom->pNext;
        sprintf( cBuf, "%d.room", pRoom->iNumber );

        if ( ( pFile = fopen( cBuf, "w" ) ) == NULL )
            fatal( "%s: %s.\n", cBuf, strerror( errno ) );

        fprintf( pFile, "#%d\n", pRoom->iNumber );

        if ( pRoom->sTitle[0] != '\0' )
        {
            fprintf( pFile, "T  " );
            fput_string( pFile, pRoom->sTitle );
            fprintf( pFile, ";\n" );
        }

        if ( pRoom->sDesc[0] != '\0' )
        {
            fprintf( pFile, "D\n  " );
            fput_string( pFile, remove_last_nl( pRoom->sDesc ) );
            fprintf( pFile, "\n[ ];\n" );
        }

        if ( pRoom->sDirDescs[0] != NULL
          && pRoom->sDirDescs[0][0] != '\0' )
        {
            fprintf( pFile, "ND\n  " );
            fput_string( pFile, remove_last_nl( pRoom->sDirDescs[0] ) );
            fprintf( pFile, "\n[ ];\n" );
        }

        if ( pRoom->sDirDescs[1] != NULL
          && pRoom->sDirDescs[1][0] != '\0' )
        {
            fprintf( pFile, "SD\n  " );
            fput_string( pFile, remove_last_nl( pRoom->sDirDescs[1] ) );
            fprintf( pFile, "\n[ ];\n" );
        }

        if ( pRoom->sDirDescs[2] != NULL
          && pRoom->sDirDescs[2][0] != '\0' )
        {
            fprintf( pFile, "ED\n  " );
            fput_string( pFile, remove_last_nl( pRoom->sDirDescs[2] ) );
            fprintf( pFile, "\n[ ];\n" );
        }

        if ( pRoom->sDirDescs[3] != NULL
          && pRoom->sDirDescs[3][0] != '\0' )
        {
            fprintf( pFile, "WD\n  " );
            fput_string( pFile, remove_last_nl( pRoom->sDirDescs[3] ) );
            fprintf( pFile, "\n[ ];\n" );
        }

        if ( pRoom->sDirDescs[4] != NULL
          && pRoom->sDirDescs[4][0] != '\0' )
        {
            fprintf( pFile, "UD\n  " );
            fput_string( pFile, remove_last_nl( pRoom->sDirDescs[4] ) );
            fprintf( pFile, "\n[ ];\n" );
        }

        if ( pRoom->sDirDescs[5] != NULL
          && pRoom->sDirDescs[5][0] != '\0' )
        {
            fprintf( pFile, "DD\n  " );
            fput_string( pFile, remove_last_nl( pRoom->sDirDescs[5] ) );
            fprintf( pFile, "\n[ ];\n" );
        }

        if ( pRoom->eExits[0].pDoorNames != NULL
          && pRoom->eExits[0].uRoom.iNumber > 0 )
            fprintf( pFile, "N  %d STANDARD;\n",
              pRoom->eExits[0].uRoom.iNumber );

        if ( pRoom->eExits[1].pDoorNames != NULL
          && pRoom->eExits[1].uRoom.iNumber > 0 )
            fprintf( pFile, "SO %d STANDARD;\n",
              pRoom->eExits[1].uRoom.iNumber );

        if ( pRoom->eExits[2].pDoorNames != NULL
          && pRoom->eExits[2].uRoom.iNumber > 0 )
            fprintf( pFile, "E  %d STANDARD;\n",
              pRoom->eExits[2].uRoom.iNumber );

        if ( pRoom->eExits[3].pDoorNames != NULL
          && pRoom->eExits[3].uRoom.iNumber > 0 )
            fprintf( pFile, "W  %d STANDARD;\n",
              pRoom->eExits[3].uRoom.iNumber );

        if ( pRoom->eExits[4].pDoorNames != NULL
          && pRoom->eExits[4].uRoom.iNumber > 0 )
            fprintf( pFile, "U  %d STANDARD;\n",
              pRoom->eExits[4].uRoom.iNumber );

        if ( pRoom->eExits[5].pDoorNames != NULL
          && pRoom->eExits[5].uRoom.iNumber > 0 )
            fprintf( pFile, "DO %d STANDARD;\n",
              pRoom->eExits[5].uRoom.iNumber );

        fprintf( pFile, "END;\n" );
        fclose( pFile );

        for ( i = 0; i < 6; i++ )
        {
            free_mem( pRoom->sDirDescs[i] );
            free_mem( pRoom->eExits[i].pDoorNames );
        }

        free_mem( pRoom->sTitle );
        free_mem( pRoom->sDesc );
        free_mem( pRoom );
    }

    pRoomList = NULL;
}


/*
 * End of main.c
 */