/****************************************************************************
*   File: whl_init.c
*                                                                           *
*       Copyright 1995 by Loral Advanced Distributed Simulation, Inc.       *
*                                                                           *
*               Loral Advanced Distributed Simulation, Inc.                 *
*               50 Moulton Street                                           *
*               Cambridge, MA 02138                                         *
*               617-441-2000                                                *
*                                                                           *
*       This software was developed by Loral under U. S. Government contracts *
*       and may be reproduced by or for the U. S. Government pursuant to    *
*       the copyright license under the clause at DFARS 252.227-7013        *
*       (OCT 1988).                                                         *
*                                                                           *
*       Contents: Code to initialize libwheeled                             *
*       Created: Mon Jul 13 11:35:57 PDT 1992                               *
*       Author: wtaylor                                                     *
*      $Revision$                                                     *
*       Remarks:                                                            *
*                                                                           *
****************************************************************************/

#include "libwhl_local.h"
#include <libreader.h>

WHEELED_SOIL_TABLE wheeled_soil_tables[WHEELED_MAX_SOIL_CONDITION];
WHEELED_SPEED_TABLE wheeled_speed_tables[WHEELED_MAX_SOIL_CONDITION];
int32 wheeled_mkill_cants_vehicle = 1;
int32 wheeled_mobility;
int32 wheeled_mobility_plus_allow_env;
static void process_reader_file(char *data_path, char *filename);
static void process_soil_table(char *data_path, char *filename);
static void process_max_speed_table(char *data_path, char *filename);

void wheeled_init(int32 cant, char *data_path, int32 constant_env,
		  int32 mobility_sw )
{
    wheeled_init_params();
    wheeled_init_subclass();
    wheeled_mkill_cants_vehicle = cant;
    wheeled_mobility = mobility_sw;
    if ((!constant_env) && wheeled_mobility)
      wheeled_mobility_plus_allow_env = TRUE;
    else
      wheeled_mobility_plus_allow_env = FALSE;

    process_reader_file(data_path, "wheeled.rdr");
    process_soil_table(data_path, "wheeled_soil.rdr");
    process_max_speed_table(data_path, "wheeled_max_speed.rdr");

    /* Read CCTT Read Data Tables */
    wheeled_cctt_read_tables(data_path);
}



READER_UNION *wheeled_speeds;


/*
 * read in the tracked data file... 
 * the rdr file should be in this format:
 * 
 *   ( 
 *     (max_speed
 *          (on_bridge 5.0)
 *          (in_breach_lane 4.0)
 *     )
 *   )
 *
 */

static void process_reader_file(char *data_path, char *filename)
{
    READER_UNION ru, *field, *speeds;
    
    if (reader_read(filename, data_path, &ru, READER_DEFAULTS) != 
	READER_READ_OK)
    {
        fprintf(stderr, "\nCouldn't find %s..  using defaults\n", filename);
        return;
    }
    
    if ((wheeled_speeds = reader_find_tag(reader_get_symbol("max_speed"), 
                                          ru.array, READER_UNTAGGED,
					  READER_NO_ERRORS)) == NULL)
    {
        fprintf(stderr, "\nProblem reading %s; using defaults\n", filename);
        return;
    }
    
}

static void process_soil_table(char *data_path, char *filename)
{
    int32 i;
    READER_UNION ru, *field;
  
    if (reader_read(filename, data_path, &ru, READER_DEFAULTS) != 
	READER_READ_OK)
    {
	fprintf(stderr, "\nCouldn't find %s..  using defaults\n", filename);
	return;
    }
  
    if ((field= reader_find_tag(reader_get_symbol("dry_soil"), 
				ru.array, READER_UNTAGGED,
				READER_NO_ERRORS)) == NULL)
    {
	fprintf(stderr, "\nProblem reading %s; using defaults\n", filename);
	return;
    }
  
    wheeled_soil_tables[0].soil_condition = DRY_SOIL;
    for (i=0; i<15; i++)
    {
	wheeled_soil_tables[0].soil_table[i].simnet_soil_type = 
	  field[i+2].array[1].integer;
	wheeled_soil_tables[0].soil_table[i].cctt_soil_type= 
	  field[i+2].array[2].integer;
    }
  
    if ((field= reader_find_tag(reader_get_symbol("wet_soil"), 
				ru.array, READER_UNTAGGED,
				READER_NO_ERRORS)) == NULL)
    
    { 
	fprintf(stderr, "\nProblem reading %s; using defaults\n", filename);
	return;
    }
    wheeled_soil_tables[1].soil_condition = WET_SOIL;
    for (i=0; i<15; i++)
    {
	wheeled_soil_tables[1].soil_table[i].simnet_soil_type = 
	  field[i+2].array[1].integer;
	wheeled_soil_tables[1].soil_table[i].cctt_soil_type= 
	  field[i+2].array[2].integer;
    }  
}

static void process_max_speed_table(char *data_path, char *filename)
{
    READER_UNION ru, *field;
    int32 i;
  
    if (reader_read(filename, data_path, &ru, READER_DEFAULTS) != 
	READER_READ_OK)
    {
	fprintf(stderr, "\nCouldn't find %s..  using defaults\n", filename);
	return;
    }
  
    if ((field= reader_find_tag(reader_get_symbol("dry_soil"), 
				ru.array, READER_UNTAGGED,
				READER_NO_ERRORS)) == NULL)
    {
	fprintf(stderr, "\nProblem reading %s; using defaults\n", filename);
	return;
    }
  
    wheeled_speed_tables[0].soil_condition = DRY_SOIL;
    for (i=0; i<15; i++)
    {
	wheeled_speed_tables[0].speed_table[i].simnet_soil_type = 
	  field[i+2].array[1].integer;
	wheeled_speed_tables[0].speed_table[i].max_speed = 
	  field[i+2].array[2].real;
    }  
    if ((field= reader_find_tag(reader_get_symbol("wet_soil"), 
				ru.array, READER_UNTAGGED,
				READER_NO_ERRORS)) == NULL)
    {
	fprintf(stderr, "\nProblem reading %s; using defaults\n", filename);
	return;
    }
  
    wheeled_speed_tables[1].soil_condition = WET_SOIL;
    for (i=0; i<15; i++)
    {
	wheeled_speed_tables[1].speed_table[i].simnet_soil_type = 
	  field[i+2].array[1].integer;
	wheeled_speed_tables[1].speed_table[i].max_speed = 
	  field[i+2].array[2].real;
    }  	
}
