/****************************************************************************
*   File: trk_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 libtracked
*       Created: Mon Jul 13 11:35:57 PDT 1992
*       Author: jesmith
*      $Revision$                                                     *
*       Remarks:                                                            *
*                                                                           *
****************************************************************************/

#include "libtrk_local.h"
#include <libreader.h>
#include <libdeployables.h>

int32 tracked_mkill_cants_vehicle = 1;
int32 tracked_wave_effect = 0;
int32 tracked_mobility;
int32 tracked_mobility_plus_allow_env;
TRACKED_SOIL_TABLE tracked_soil_tables[TRACKED_MAX_SOIL_CONDITION];
char *tracked_plow_cat = NULL, *tracked_bridge_cat = NULL;
char *tracked_plow_speeds_symbol = NULL, *tracked_fwmr_symbol = NULL;
char *tracked_moldboard_symbol = NULL, *tracked_dirtmover_cat = NULL;

static void process_reader_file(char *data_path, char *filename);
static void process_soil_table(char *data_path, char *filename);

void tracked_init(
    int32 cant, 
    char *data_path,
    int32 constant_env,
    int32 mobility_sw)
{
    tracked_init_params();
    tracked_init_subclass();
    tracked_mkill_cants_vehicle = cant;
    tracked_wave_effect = !constant_env; /* DVW - roll, etc. in ocean */
    tracked_mobility = mobility_sw;
    if ((!constant_env) && tracked_mobility)
      tracked_mobility_plus_allow_env = TRUE;
    else
      tracked_mobility_plus_allow_env = FALSE;
    process_reader_file(data_path, "tracked.rdr");
    tracked_init_rao(data_path);
    tracked_sink_init(data_path, READER_DEFAULTS);
    process_soil_table(data_path, "tracked_soil.rdr");

    tracked_plow_cat = reader_get_symbol("plow");
    tracked_bridge_cat = reader_get_symbol("bridge");
    tracked_plow_speeds_symbol = reader_get_symbol("plow_speeds");
    tracked_dirtmover_cat = reader_get_symbol("dirtmover");
    tracked_fwmr_symbol = reader_get_symbol("FWMR");
    tracked_moldboard_symbol = reader_get_symbol("moldboard");

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


READER_UNION *tracked_speeds = NULL;


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

static void process_reader_file(char *data_path, char *filename)
{
    READER_UNION ru;

    if (reader_read(filename, data_path, &ru, READER_DEFAULTS) != 
	    READER_READ_OK)
    {
        fprintf(stderr, "\nCouldn't find %s..  using defaults\n", filename);
        return;
    }

    if ((tracked_speeds = reader_find_tag(reader_get_symbol("max_speed"), 
				  ru.array, READER_UNTAGGED, 0)) == NULL)
    {
        fprintf(stderr, "\nProblem reading %s..\n", filename);
        return;
    }
}

static void process_soil_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;
    }
  
  tracked_soil_tables[0].soil_condition = DRY_SOIL;
  for (i=0; i<15; i++)
    {
      tracked_soil_tables[0].soil_table[i].simnet_soil_type = 
	field[i+2].array[1].integer;
      tracked_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;
    }
  
  tracked_soil_tables[1].soil_condition = WET_SOIL;
  for (i=0; i<15; i++)
    {
      tracked_soil_tables[1].soil_table[i].simnet_soil_type = 
	field[i+2].array[1].integer;
      tracked_soil_tables[1].soil_table[i].cctt_soil_type = 
	field[i+2].array[2].integer;
    }  	
}

