#include <stdbool.h>
#include <stdint.h>

#include "GPIO.h"

// #include "model_generator.h"
#include "model_from_solver_generator.h"
#include "model_generator.h"
#include "model_root.h"
#include "segment.h"
#include "train.h"
#include "turnout.h"

#include "close_trains.h"
#include "end_of_siding.h"
#include "misaligned_turnout.h"
#include "train_locations.h"
#include "objects.h"
#include "random_models.h"

// Configs shared with timer - START

// Choose measurement scenario
#define GENERATED 0
#define RANDOM 1
#define MEASUREMENT_SCENARIO RANDOM

// If it is a measurement scenario over random models, choose query
#if MEASUREMENT_SCENARIO == RANDOM
//#define CLOSE_TRAINS_RANDOM
// #define END_OF_SIDING_RANDOM
// #define MISALIGNED_TURNOUT_RANDOM
// #define TRAIN_LOCATIONS_RANDOM
#endif // MEASUREMENT_SCENARIO == RANDOM

#define REPETITIONS 100

// Configs shared with timer - END

// void wait_for_interrupt(void) { __WFI(); }

void config_signal() {
  P0_12_set_mode(OUTPUT_PP_GP);
  P0_12_set_driver_strength(STRONG);
  P0_12_reset();
  // P0_0_set_mode(OUTPUT_PP_GP);
  // P0_0_set_driver_strength(STRONG);
  // P0_0_reset();
}

void config_input() {
  P2_6_set_mode(INPUT);
  P5_1_set_mode(INPUT);
  P5_2_set_mode(INPUT);
  P5_7_set_mode(INPUT);
  P3_3_set_mode(INPUT);
  P0_6_set_mode(INPUT);
}

uint16_t read_input() {
  uint16_t result = 0;
  result |= P2_6_read() != 0;
  result |= (P5_1_read() != 0) << 1;
  result |= (P5_2_read() != 0) << 2;
  result |= (P5_7_read() != 0) << 3;
  result |= (P3_3_read() != 0) << 4;
  result |= (P0_6_read() != 0) << 5;
  return result;
}

void active_wait_delay() {
  // Helper variable to generate delay between signals
  volatile int jj = 0;

  // Forced delay before toggling pin
  while (jj < 1000) {
    jj++;
  }
}

void signal_start() {
  active_wait_delay();
  // XMC4500 - signal start
  // *((unsigned long *)0x48028004UL) = 0x00000001UL;
  P0_12_set();
  // P0_0_set();
}

void signal_end() {
  active_wait_delay();
  // XMC4500 - signal end
  // *((unsigned long *)0x48028004UL) = 0x00010000UL;
  P0_12_reset();
  // P0_0_reset();
}

void config_leds() {
  P1_0_set_mode(OUTPUT_PP_GP);
  P1_0_set_driver_strength(STRONG);
  P1_0_reset();

  P1_1_set_mode(OUTPUT_PP_GP);
  P1_1_set_driver_strength(STRONG);
  P1_1_reset();
}

// Displays a number between 0 and 3 in binary format on LED1 and LED2
void set_leds(uint8_t value) {
  (value & 0x1) ? P1_1_set() : P1_1_reset();
  (value & 0x2) ? P1_0_set() : P1_0_reset();
}

void board_init() {
  config_leds();
  config_signal();
  config_input();
  CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  DWT->CYCCNT = 0;
  DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
}

#if MEASUREMENT_SCENARIO == GENERATED
// ModelRoot close_trains_model;
// ModelRoot misaligned_turnout_model;
// ModelRoot end_of_siding_model;
// ModelRoot train_locations_model;
#endif // MEASUREMENT_SCENARIO == GENERATED
ModelRoot modes3_model;

CloseTrainsMatchSet close_trains_matches;
CloseTrainsFrame close_trains_frame;
MisalignedTurnoutMatchSet misaligned_turnout_matches;
MisalignedTurnoutFrame misaligned_turnout_frame;
EndOfSidingMatchSet end_of_siding_matches;
EndOfSidingFrame end_of_siding_frame;
TrainLocationsMatchSet train_locations_matches;
TrainLocationsFrame train_locations_frame;

#if MEASUREMENT_SCENARIO == GENERATED
void close_trains_wrapper_close_trains_model() {
  // close_trains_matcher(&close_trains_model, &close_trains_frame, 0,
  //                      &close_trains_matches);
}
void misaligned_turnout_wrapper_close_trains_model() {
  // misaligned_turnout_matcher(&close_trains_model, &misaligned_turnout_frame,
  // 0,
  //                            &misaligned_turnout_matches);
}
void end_of_siding_wrapper_close_trains_model() {
  // end_of_siding_matcher(&close_trains_model, &end_of_siding_frame, 0,
  //                       &end_of_siding_matches);
}
void train_locations_wrapper_close_trains_model() {
  // train_locations_matcher(&close_trains_model, &train_locations_frame, 0,
  //                         &train_locations_matches);
}
void close_trains_wrapper_misaligned_turnout_model() {
  // close_trains_matcher(&misaligned_turnout_model, &close_trains_frame, 0,
  //                      &close_trains_matches);
}
void misaligned_turnout_wrapper_misaligned_turnout_model() {
  // misaligned_turnout_matcher(&misaligned_turnout_model,
  //                            &misaligned_turnout_frame, 0,
  //                            &misaligned_turnout_matches);
}
void end_of_siding_wrapper_misaligned_turnout_model() {
  // end_of_siding_matcher(&misaligned_turnout_model, &end_of_siding_frame, 0,
  //                       &end_of_siding_matches);
}
void train_locations_wrapper_misaligned_turnout_model() {
  // train_locations_matcher(&misaligned_turnout_model, &train_locations_frame,
  // 0,
  //                         &train_locations_matches);
}
void close_trains_wrapper_end_of_siding_model() {
  // close_trains_matcher(&end_of_siding_model, &close_trains_frame, 0,
  //                      &close_trains_matches);
}
void misaligned_turnout_wrapper_end_of_siding_model() {
  // misaligned_turnout_matcher(&end_of_siding_model, &misaligned_turnout_frame,
  // 0,
  //                            &misaligned_turnout_matches);
}
void end_of_siding_wrapper_end_of_siding_model() {
  // end_of_siding_matcher(&end_of_siding_model, &end_of_siding_frame, 0,
  //                       &end_of_siding_matches);
}
void train_locations_wrapper_end_of_siding_model() {
  // train_locations_matcher(&end_of_siding_model, &train_locations_frame, 0,
  //                         &train_locations_matches);
}
void close_trains_wrapper_train_locations_model() {
  // close_trains_matcher(&train_locations_model, &close_trains_frame, 0,
  //                      &close_trains_matches);
}
void misaligned_turnout_wrapper_train_locations_model() {
  // misaligned_turnout_matcher(&train_locations_model,
  // &misaligned_turnout_frame,
  //                            0, &misaligned_turnout_matches);
}
void end_of_siding_wrapper_train_locations_model() {
  // end_of_siding_matcher(&train_locations_model, &end_of_siding_frame, 0,
  //                       &end_of_siding_matches);
}
void train_locations_wrapper_train_locations_model() {
  // train_locations_matcher(&train_locations_model, &train_locations_frame, 0,
  //                         &train_locations_matches);
}
void close_trains_wrapper_modes3_model() {
  close_trains_matcher(&modes3_model, &close_trains_frame, 0,
                       &close_trains_matches);
}
void misaligned_turnout_wrapper_modes3_model() {
  misaligned_turnout_matcher(&modes3_model, &misaligned_turnout_frame, 0,
                             &misaligned_turnout_matches);
}
void end_of_siding_wrapper_modes3_model() {
  end_of_siding_matcher(&modes3_model, &end_of_siding_frame, 0,
                        &end_of_siding_matches);
}
void train_locations_wrapper_modes3_model() {
  train_locations_matcher(&modes3_model, &train_locations_frame, 0,
                          &train_locations_matches);
}

void nop_wrapper() {}

typedef void (*matcher)();

// clang-format off
matcher matchers[] = {
    nop_wrapper,
    nop_wrapper,
    close_trains_wrapper_close_trains_model,
    misaligned_turnout_wrapper_close_trains_model,
    end_of_siding_wrapper_close_trains_model,
    train_locations_wrapper_close_trains_model,
    close_trains_wrapper_misaligned_turnout_model,
    misaligned_turnout_wrapper_misaligned_turnout_model,
    end_of_siding_wrapper_misaligned_turnout_model,
    train_locations_wrapper_misaligned_turnout_model,
    close_trains_wrapper_end_of_siding_model,
    misaligned_turnout_wrapper_end_of_siding_model,
    end_of_siding_wrapper_end_of_siding_model,
    train_locations_wrapper_end_of_siding_model,
    close_trains_wrapper_train_locations_model,
    misaligned_turnout_wrapper_train_locations_model,
    end_of_siding_wrapper_train_locations_model,
    train_locations_wrapper_train_locations_model,
    close_trains_wrapper_modes3_model,
    misaligned_turnout_wrapper_modes3_model,
    end_of_siding_wrapper_modes3_model,
    train_locations_wrapper_modes3_model
};
// clang-format on
#endif // MEASUREMENT_SCENARIO == GENERATED

#if MEASUREMENT_SCENARIO == RANDOM
typedef void (*generator)(ModelRoot *);

// clang-format off
generator generators [] = {
 generate_model_result0,
 generate_model_result1,
 generate_model_result2,
 generate_model_result3,
 generate_model_result4,
 generate_model_result5,
 generate_model_result6,
 generate_model_result7,
 generate_model_result8,
 generate_model_result9,
 generate_model_result10,
 generate_model_result11,
 generate_model_result12,
 generate_model_result13,
 generate_model_result14,
 generate_model_result15,
 generate_model_result16,
 generate_model_result17,
 generate_model_result18,
 generate_model_result19,
 generate_model_result20,
 generate_model_result21,
 generate_model_result22,
 generate_model_result23,
 generate_model_result24,
 generate_model_result25,
 generate_model_result26,
 generate_model_result27,
 generate_model_result28,
 generate_model_result29,
 generate_model_result30,
 generate_model_result31,
 generate_model_result32,
 generate_model_result33,
 generate_model_result34,
 generate_model_result35,
 generate_model_result36,
 generate_model_result37,
 generate_model_result38,
 generate_model_result39,
 generate_model_result40,
 generate_model_result41,
 generate_model_result42,
 generate_model_result43,
 generate_model_result44,
 generate_model_result45,
 generate_model_result46,
 generate_model_result47,
 generate_model_result48,
 generate_model_result49,
 generate_model_result50,
 generate_model_result51,
 generate_model_result52,
 generate_model_result53,
 generate_model_result54,
 generate_model_result55,
 generate_model_result56,
 generate_model_result57,
 generate_model_result58,
 generate_model_result59,
 generate_model_result60,
 generate_model_result61,
 generate_model_result62,
 generate_model_result63,
 generate_model_result64,
 generate_model_result65,
 generate_model_result66,
 generate_model_result67,
 generate_model_result68,
 generate_model_result69,
 generate_model_result70,
 generate_model_result71,
 generate_model_result72,
 generate_model_result73,
 generate_model_result74,
 generate_model_result75,
 generate_model_result76,
 generate_model_result77,
 generate_model_result78,
 generate_model_result79,
 generate_model_result80,
 generate_model_result81,
 generate_model_result82,
 generate_model_result83,
 generate_model_result84,
 generate_model_result85,
 generate_model_result86,
 generate_model_result87,
 generate_model_result88,
 generate_model_result89,
 generate_model_result90,
 generate_model_result91,
 generate_model_result92,
 generate_model_result93,
 generate_model_result94,
 generate_model_result95,
 generate_model_result96,
 generate_model_result97,
 generate_model_result98,
 generate_model_result99,
 generate_model_result100,
 generate_model_result101,
 generate_model_result102,
 generate_model_result103,
 generate_model_result104,
 generate_model_result105,
 generate_model_result106,
 generate_model_result107,
 generate_model_result108,
 generate_model_result109,
 generate_model_result110,
 generate_model_result111,
 generate_model_result112,
 generate_model_result113,
 generate_model_result114,
 generate_model_result115,
 generate_model_result116,
 generate_model_result117,
 generate_model_result118,
 generate_model_result119,
 generate_model_result120,
 generate_model_result121,
 generate_model_result122,
 generate_model_result123,
 generate_model_result124,
 generate_model_result125,
 generate_model_result126,
 generate_model_result127,
 generate_model_result128,
 generate_model_result129,
 generate_model_result130,
 generate_model_result131,
 generate_model_result132,
 generate_model_result133,
 generate_model_result134,
 generate_model_result135,
 generate_model_result136,
 generate_model_result137,
 generate_model_result138,
 generate_model_result139,
 generate_model_result140,
 generate_model_result141,
 generate_model_result142,
 generate_model_result143,
 generate_model_result144,
 generate_model_result145,
 generate_model_result146,
 generate_model_result147,
 generate_model_result148,
 generate_model_result149,
 generate_model_result150,
 generate_model_result151,
 generate_model_result152,
 generate_model_result153,
 generate_model_result154,
 generate_model_result155,
 generate_model_result156,
 generate_model_result157,
 generate_model_result158,
 generate_model_result159,
 generate_model_result160,
 generate_model_result161,
 generate_model_result162,
 generate_model_result163,
 generate_model_result164,
 generate_model_result165,
 generate_model_result166,
 generate_model_result167,
 generate_model_result168,
 generate_model_result169,
 generate_model_result170,
 generate_model_result171,
 generate_model_result172,
 generate_model_result173,
 generate_model_result174,
 generate_model_result175,
 generate_model_result176,
 generate_model_result177,
 generate_model_result178,
 generate_model_result179,
 generate_model_result180,
 generate_model_result181,
 generate_model_result182,
 generate_model_result183,
 generate_model_result184,
 generate_model_result185,
 generate_model_result186,
 generate_model_result187,
 generate_model_result188,
 generate_model_result189,
 generate_model_result190,
 generate_model_result191,
 generate_model_result192,
 generate_model_result193,
 generate_model_result194,
 generate_model_result195,
 generate_model_result196,
 generate_model_result197,
 generate_model_result198,
 generate_model_result199,
 generate_model_result200,
 generate_model_result201,
 generate_model_result202,
 generate_model_result203,
 generate_model_result204,
 generate_model_result205,
 generate_model_result206,
 generate_model_result207,
 generate_model_result208,
 generate_model_result209,
 generate_model_result210,
 generate_model_result211,
 generate_model_result212,
 generate_model_result213,
 generate_model_result214,
 generate_model_result215,
 generate_model_result216,
 generate_model_result217,
 generate_model_result218,
 generate_model_result219,
 generate_model_result220,
 generate_model_result221,
 generate_model_result222,
 generate_model_result223,
 generate_model_result224,
 generate_model_result225,
 generate_model_result226,
 generate_model_result227,
 generate_model_result228,
 generate_model_result229,
 generate_model_result230,
 generate_model_result231,
 generate_model_result232,
 generate_model_result233,
 generate_model_result234,
 generate_model_result235,
 generate_model_result236,
 generate_model_result237,
 generate_model_result238,
 generate_model_result239,
 generate_model_result240,
 generate_model_result241,
 generate_model_result242,
 generate_model_result243,
 generate_model_result244,
 generate_model_result245,
 generate_model_result246,
 generate_model_result247,
 generate_model_result248,
 generate_model_result249,
generate_model_result250,
generate_model_result251,
generate_model_result252,
generate_model_result253,
generate_model_result254,
generate_model_result255,
generate_model_result256,
generate_model_result257,
generate_model_result258,
generate_model_result259,
generate_model_result260,
generate_model_result261,
generate_model_result262,
generate_model_result263,
generate_model_result264,
generate_model_result265,
generate_model_result266,
generate_model_result267,
generate_model_result268,
generate_model_result269,
generate_model_result270,
generate_model_result271,
generate_model_result272,
generate_model_result273,
generate_model_result274,
generate_model_result275,
generate_model_result276,
generate_model_result277,
generate_model_result278,
generate_model_result279,
generate_model_result280,
generate_model_result281,
generate_model_result282,
generate_model_result283,
generate_model_result284,
generate_model_result285,
generate_model_result286,
generate_model_result287,
generate_model_result288,
generate_model_result289,
generate_model_result290,
generate_model_result291,
generate_model_result292,
generate_model_result293,
generate_model_result294,
generate_model_result295,
generate_model_result296,
generate_model_result297,
generate_model_result298,
generate_model_result299,
generate_model_result300,
generate_model_result301,
generate_model_result302,
generate_model_result303,
generate_model_result304,
generate_model_result305,
generate_model_result306,
generate_model_result307,
generate_model_result308,
generate_model_result309,
generate_model_result310,
generate_model_result311,
generate_model_result312,
generate_model_result313,
generate_model_result314,
generate_model_result315,
generate_model_result316,
generate_model_result317,
generate_model_result318,
generate_model_result319,
generate_model_result320,
generate_model_result321,
generate_model_result322,
generate_model_result323,
generate_model_result324,
generate_model_result325,
generate_model_result326,
generate_model_result327,
generate_model_result328,
generate_model_result329,
generate_model_result330,
generate_model_result331,
generate_model_result332,
generate_model_result333,
generate_model_result334,
generate_model_result335,
generate_model_result336,
generate_model_result337,
generate_model_result338,
generate_model_result339,
generate_model_result340,
generate_model_result341,
generate_model_result342,
generate_model_result343,
generate_model_result344,
generate_model_result345,
generate_model_result346,
generate_model_result347,
generate_model_result348,
generate_model_result349,
generate_model_result350,
generate_model_result351,
generate_model_result352,
generate_model_result353,
generate_model_result354,
generate_model_result355,
generate_model_result356,
generate_model_result357,
generate_model_result358,
generate_model_result359,
generate_model_result360,
generate_model_result361,
generate_model_result362,
generate_model_result363,
generate_model_result364,
generate_model_result365,
generate_model_result366,
generate_model_result367,
generate_model_result368,
generate_model_result369,
generate_model_result370,
generate_model_result371,
generate_model_result372,
generate_model_result373,
generate_model_result374,
generate_model_result375,
generate_model_result376,
generate_model_result377,
generate_model_result378,
generate_model_result379,
generate_model_result380,
generate_model_result381,
generate_model_result382,
generate_model_result383,
generate_model_result384,
generate_model_result385,
generate_model_result386,
generate_model_result387,
generate_model_result388,
generate_model_result389,
generate_model_result390,
generate_model_result391,
generate_model_result392,
generate_model_result393,
generate_model_result394,
generate_model_result395,
generate_model_result396,
generate_model_result397,
generate_model_result398,
generate_model_result399,
generate_model_result400,
generate_model_result401,
generate_model_result402,
generate_model_result403,
generate_model_result404,
generate_model_result405,
generate_model_result406,
generate_model_result407,
generate_model_result408,
generate_model_result409,
generate_model_result410,
generate_model_result411,
generate_model_result412,
generate_model_result413,
generate_model_result414,
generate_model_result415,
generate_model_result416,
generate_model_result417,
generate_model_result418,
generate_model_result419,
generate_model_result420,
generate_model_result421,
generate_model_result422,
generate_model_result423,
generate_model_result424,
generate_model_result425,
generate_model_result426,
generate_model_result427,
generate_model_result428,
generate_model_result429,
generate_model_result430,
generate_model_result431,
generate_model_result432,
generate_model_result433,
generate_model_result434,
generate_model_result435,
generate_model_result436,
generate_model_result437,
generate_model_result438,
generate_model_result439,
generate_model_result440,
generate_model_result441,
generate_model_result442,
generate_model_result443,
generate_model_result444,
generate_model_result445,
generate_model_result446,
generate_model_result447,
generate_model_result448,
generate_model_result449,
generate_model_result450,
generate_model_result451,
generate_model_result452,
generate_model_result453,
generate_model_result454,
generate_model_result455,
generate_model_result456,
generate_model_result457,
generate_model_result458,
generate_model_result459,
generate_model_result460,
generate_model_result461,
generate_model_result462,
generate_model_result463,
generate_model_result464,
generate_model_result465,
generate_model_result466,
generate_model_result467,
generate_model_result468,
generate_model_result469,
generate_model_result470,
generate_model_result471,
generate_model_result472,
generate_model_result473,
generate_model_result474,
generate_model_result475,
generate_model_result476,
generate_model_result477,
generate_model_result478,
generate_model_result479,
generate_model_result480,
generate_model_result481,
generate_model_result482,
generate_model_result483,
generate_model_result484,
generate_model_result485,
generate_model_result486,
generate_model_result487,
generate_model_result488,
generate_model_result489,
generate_model_result490,
generate_model_result491,
generate_model_result492,
generate_model_result493,
generate_model_result494,
generate_model_result495,
generate_model_result496,
generate_model_result497,
generate_model_result498,
generate_model_result499,
generate_model_close_trains_solver,
generate_model_end_of_siding_solver,
generate_model_misaligned_turnout_solver,
generate_model_train_locations_solver,
generate_model_modes3
};
// clang-format on
#endif // MEASUREMENT_SCENARIO == RANDOM

volatile uint32_t start;
volatile uint32_t end;
volatile uint32_t time;

int main(void) {

  volatile i;

  board_init();

#if MEASUREMENT_SCENARIO == GENERATED
  // generate_model_close_trains_solver(&close_trains_model);
  // generate_model_misaligned_turnout_solver(&misaligned_turnout_model);
  // generate_model_end_of_siding_solver(&end_of_siding_model);
  // generate_model_train_locations_solver(&train_locations_model);
  generate_model_modes3(&modes3_model);
#endif // MEASUREMENT_SCENARIO == GENERATED

#if MEASUREMENT_SCENARIO == RANDOM
  // Generate random models and initialize static variables
  for (int i = 0; i < 505; i++) {
    generators[i](&modes3_model);
    start = DWT->CYCCNT;
#if defined(CLOSE_TRAINS_RANDOM)
    close_trains_matcher(&modes3_model, &close_trains_frame, 0,
                         &close_trains_matches);
#elif defined(END_OF_SIDING_RANDOM)
    end_of_siding_matcher(&modes3_model, &end_of_siding_frame, 0,
                          &end_of_siding_matches);
#elif defined(MISALIGNED_TURNOUT_RANDOM)
    misaligned_turnout_matcher(&modes3_model, &misaligned_turnout_frame, 0,
                               &misaligned_turnout_matches);
#elif defined(TRAIN_LOCATIONS_RANDOM)
    train_locations_matcher(&modes3_model, &train_locations_frame, 0,
                            &train_locations_matches);
#else
#error("No query is selected to execute on random models")
#endif // Query selection
#endif // MEASUREMENT_SCENARIO == RANDOM
    end = DWT->CYCCNT;
    time = end - start;
    time = time + 1;

    train0_obj.train_id = 0; train0_obj.speed = 0.0; train0_obj.location = NULL;
    train1_obj.train_id = 0; train1_obj.speed = 0.0; train1_obj.location = NULL;
    train2_obj.train_id = 0; train2_obj.speed = 0.0; train2_obj.location = NULL;
    train3_obj.train_id = 0; train3_obj.speed = 0.0; train3_obj.location = NULL;
    train4_obj.train_id = 0; train4_obj.speed = 0.0; train4_obj.location = NULL;
    simplesegment4_obj.segment_id = 0; simplesegment4_obj.train = NULL; simplesegment4_obj.connected_to_count = 0;
    simplesegment3_obj.segment_id = 0; simplesegment3_obj.train = NULL; simplesegment3_obj.connected_to_count = 0;
    simplesegment2_obj.segment_id = 0; simplesegment2_obj.train = NULL; simplesegment2_obj.connected_to_count = 0;
    simplesegment1_obj.segment_id = 0; simplesegment1_obj.train = NULL; simplesegment1_obj.connected_to_count = 0;
    simplesegment0_obj.segment_id = 0; simplesegment0_obj.train = NULL; simplesegment0_obj.connected_to_count = 0;
    simplesegment5_obj.segment_id = 0; simplesegment5_obj.train = NULL; simplesegment5_obj.connected_to_count = 0;
    simplesegment6_obj.segment_id = 0; simplesegment6_obj.train = NULL; simplesegment6_obj.connected_to_count = 0;
    simplesegment7_obj.segment_id = 0; simplesegment7_obj.train = NULL; simplesegment7_obj.connected_to_count = 0;
    simplesegment8_obj.segment_id = 0; simplesegment8_obj.train = NULL; simplesegment8_obj.connected_to_count = 0;
    simplesegment9_obj.segment_id = 0; simplesegment9_obj.train = NULL; simplesegment9_obj.connected_to_count = 0;
    simplesegment10_obj.segment_id = 0; simplesegment10_obj.train = NULL; simplesegment10_obj.connected_to_count = 0;
    simplesegment11_obj.segment_id = 0; simplesegment11_obj.train = NULL; simplesegment11_obj.connected_to_count = 0;
    simplesegment12_obj.segment_id = 0; simplesegment12_obj.train = NULL; simplesegment12_obj.connected_to_count = 0;
    simplesegment13_obj.segment_id = 0; simplesegment13_obj.train = NULL; simplesegment13_obj.connected_to_count = 0;
    simplesegment14_obj.segment_id = 0; simplesegment14_obj.train = NULL; simplesegment14_obj.connected_to_count = 0;
    simplesegment15_obj.segment_id = 0; simplesegment15_obj.train = NULL; simplesegment15_obj.connected_to_count = 0;
    simplesegment16_obj.segment_id = 0; simplesegment16_obj.train = NULL; simplesegment16_obj.connected_to_count = 0;
    simplesegment17_obj.segment_id = 0; simplesegment17_obj.train = NULL; simplesegment17_obj.connected_to_count = 0;
    simplesegment18_obj.segment_id = 0; simplesegment18_obj.train = NULL; simplesegment18_obj.connected_to_count = 0;
    simplesegment19_obj.segment_id = 0; simplesegment19_obj.train = NULL; simplesegment19_obj.connected_to_count = 0;
    turnout0_obj.segment.segment_id = 0; turnout0_obj.segment.train = NULL; turnout0_obj.segment.connected_to_count = 0;
    turnout1_obj.segment.segment_id = 0; turnout1_obj.segment.train = NULL; turnout1_obj.segment.connected_to_count = 0;
    turnout2_obj.segment.segment_id = 0; turnout2_obj.segment.train = NULL; turnout2_obj.segment.connected_to_count = 0;
    turnout3_obj.segment.segment_id = 0; turnout3_obj.segment.train = NULL; turnout3_obj.segment.connected_to_count = 0;
    turnout4_obj.segment.segment_id = 0; turnout4_obj.segment.train = NULL; turnout4_obj.segment.connected_to_count = 0;

  }

  return 0;
}
