airport.c
changeset 950 165341d74973
parent 909 65cdb609b7a6
child 1019 6bae6c11e865
equal deleted inserted replaced
949:419c0e2f438b 950:165341d74973
     8 AirportFTAClass *Heliport, *Oilrig;
     8 AirportFTAClass *Heliport, *Oilrig;
     9 AirportFTAClass *MetropolitanAirport;
     9 AirportFTAClass *MetropolitanAirport;
    10 AirportFTAClass *InternationalAirport;
    10 AirportFTAClass *InternationalAirport;
    11 
    11 
    12 static void AirportFTAClass_Constructor(AirportFTAClass *Airport,
    12 static void AirportFTAClass_Constructor(AirportFTAClass *Airport,
    13 																				const byte nofterminals, const byte nofterminalgroups,
    13 																				const byte *terminals, const byte *helipads,
    14 																				const byte nofhelipads,  const byte nofhelipadgroups,
       
    15 																				const byte entry_point,  const byte acc_planes,
    14 																				const byte entry_point,  const byte acc_planes,
    16 																				const AirportFTAbuildup *FA,
    15 																				const AirportFTAbuildup *FA,
    17 																				const TileIndexDiffC *depots, const byte nof_depots);
    16 																				const TileIndexDiffC *depots, const byte nof_depots);
    18 static void AirportFTAClass_Destructor(AirportFTAClass *Airport);
    17 static void AirportFTAClass_Destructor(AirportFTAClass *Airport);
    19 
    18 
    24 static byte AirportBlockToString(uint32 block);*/
    23 static byte AirportBlockToString(uint32 block);*/
    25 
    24 
    26 void InitializeAirports()
    25 void InitializeAirports()
    27 {
    26 {
    28 	// country airport
    27 	// country airport
    29 	CountryAirport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass));
    28 	CountryAirport = malloc(sizeof(AirportFTAClass));
    30 	AirportFTAClass_Constructor(CountryAirport, 2, 1, 0, 0, 16, ALL, _airport_fta_country, _airport_depots_country, lengthof(_airport_depots_country));
    29 	
       
    30 	AirportFTAClass_Constructor(
       
    31 		CountryAirport, 
       
    32 		_airport_terminal_country, 
       
    33 		NULL, 
       
    34 		16, 
       
    35 		ALL, 
       
    36 		_airport_fta_country, 
       
    37 		_airport_depots_country, 
       
    38 		lengthof(_airport_depots_country)
       
    39 	);
    31 
    40 
    32 	// city airport
    41 	// city airport
    33 	CityAirport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass));
    42 	CityAirport = malloc(sizeof(AirportFTAClass));
    34 	AirportFTAClass_Constructor(CityAirport, 3, 1, 0, 0, 19, ALL, _airport_fta_city, _airport_depots_city, lengthof(_airport_depots_city));
    43 	
       
    44 	AirportFTAClass_Constructor(
       
    45 		CityAirport, 
       
    46 		_airport_terminal_city, 
       
    47 		NULL, 
       
    48 		19, 
       
    49 		ALL, 
       
    50 		_airport_fta_city, 
       
    51 		_airport_depots_city, 
       
    52 		lengthof(_airport_depots_city)
       
    53 	);
    35 
    54 
    36 	// metropolitan airport
    55 	// metropolitan airport
    37 	MetropolitanAirport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass));
    56 	MetropolitanAirport = malloc(sizeof(AirportFTAClass));
    38 	AirportFTAClass_Constructor(MetropolitanAirport, 3, 1, 0, 0, 20, ALL, _airport_fta_metropolitan, _airport_depots_metropolitan, lengthof(_airport_depots_metropolitan));
    57 	
       
    58 	AirportFTAClass_Constructor(
       
    59 		MetropolitanAirport, 
       
    60 		_airport_terminal_metropolitan, 
       
    61 		NULL, 
       
    62 		20, 
       
    63 		ALL, 
       
    64 		_airport_fta_metropolitan, 
       
    65 		_airport_depots_metropolitan, 
       
    66 		lengthof(_airport_depots_metropolitan)
       
    67 	);
    39 
    68 
    40 	// international airport
    69 	// international airport
    41 	InternationalAirport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass));
    70 	InternationalAirport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass));
    42 	AirportFTAClass_Constructor(InternationalAirport, 6, 2, 2, 1, 37, ALL, _airport_fta_international, _airport_depots_international, lengthof(_airport_depots_international));
    71 	
       
    72 	AirportFTAClass_Constructor(
       
    73 		InternationalAirport, 
       
    74 		_airport_terminal_international, 
       
    75 		_airport_helipad_international, 
       
    76 		37, 
       
    77 		ALL, 
       
    78 		_airport_fta_international, 
       
    79 		_airport_depots_international, 
       
    80 		lengthof(_airport_depots_international)
       
    81 	);
    43 
    82 
    44 	// heliport, oilrig
    83 	// heliport, oilrig
    45 	Heliport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass));
    84 	Heliport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass));
    46 	AirportFTAClass_Constructor(Heliport, 0, 0, 1, 1, 7, HELICOPTERS_ONLY, _airport_fta_heliport_oilrig, NULL, 0);
    85 	
       
    86 	AirportFTAClass_Constructor(
       
    87 		Heliport, 
       
    88 		NULL, 
       
    89 		_airport_helipad_heliport_oilrig, 
       
    90 		7, 
       
    91 		HELICOPTERS_ONLY, 
       
    92 		_airport_fta_heliport_oilrig, 
       
    93 		NULL, 
       
    94 		0
       
    95 	);
       
    96 	
    47 	Oilrig = Heliport;  // exactly the same structure for heliport/oilrig, so share state machine
    97 	Oilrig = Heliport;  // exactly the same structure for heliport/oilrig, so share state machine
    48 }
    98 }
    49 
    99 
    50 void UnInitializeAirports()
   100 void UnInitializeAirports()
    51 {
   101 {
    55 	AirportFTAClass_Destructor(MetropolitanAirport);
   105 	AirportFTAClass_Destructor(MetropolitanAirport);
    56 	AirportFTAClass_Destructor(InternationalAirport);
   106 	AirportFTAClass_Destructor(InternationalAirport);
    57 }
   107 }
    58 
   108 
    59 static void AirportFTAClass_Constructor(AirportFTAClass *Airport,
   109 static void AirportFTAClass_Constructor(AirportFTAClass *Airport,
    60 																				const byte nofterminals, const byte nofterminalgroups,
   110 																				const byte *terminals, const byte *helipads,
    61 																				const byte nofhelipads, const byte nofhelipadgroups,
       
    62 																				const byte entry_point, const byte acc_planes,
   111 																				const byte entry_point, const byte acc_planes,
    63 																				const AirportFTAbuildup *FA,
   112 																				const AirportFTAbuildup *FA,
    64 																				const TileIndexDiffC *depots, const byte nof_depots)
   113 																				const TileIndexDiffC *depots, const byte nof_depots)
    65 {
   114 {
       
   115 	byte nofterminals, nofhelipads;
       
   116 	byte nofterminalgroups = 0;
       
   117 	byte nofhelipadgroups = 0;
       
   118 	const byte * curr;
       
   119 	int i;
       
   120 	nofterminals = nofhelipads = 0;
       
   121 	
       
   122 	//now we read the number of terminals we have
       
   123 	if (terminals != NULL) {
       
   124 		i = terminals[0];
       
   125 		nofterminalgroups = i;
       
   126 		curr = terminals;
       
   127 		while (i-- > 0) {
       
   128 			curr++;
       
   129 			assert(*curr != 0);	//we don't want to have an empty group
       
   130 			nofterminals += *curr;
       
   131 		}
       
   132 	
       
   133 	}
       
   134 	Airport->terminals = terminals;
       
   135 
       
   136 	//read helipads	
       
   137 	if (helipads != NULL) {
       
   138 		i = helipads[0];
       
   139 		nofhelipadgroups = i;
       
   140 		curr = helipads;
       
   141 		while (i-- > 0) {
       
   142 			curr++;
       
   143 			assert(*curr != 0); //no empty groups please
       
   144 			nofhelipads += *curr;
       
   145 		}
       
   146 	
       
   147 	}
       
   148 	Airport->helipads = helipads;
       
   149 
    66 	// if there are more terminals than 6, internal variables have to be changed, so don't allow that
   150 	// if there are more terminals than 6, internal variables have to be changed, so don't allow that
    67 	// same goes for helipads
   151 	// same goes for helipads
    68 	if (nofterminals > MAX_TERMINALS) { printf("Currently only maximum of %2d terminals are supported (you wanted %2d)\n", MAX_TERMINALS, nofterminals);}
   152 	if (nofterminals > MAX_TERMINALS) { printf("Currently only maximum of %2d terminals are supported (you wanted %2d)\n", MAX_TERMINALS, nofterminals);}
    69 	if (nofhelipads > MAX_HELIPADS) { printf("Currently only maximum of %2d helipads are supported (you wanted %2d)\n", MAX_HELIPADS, nofhelipads);}
   153 	if (nofhelipads > MAX_HELIPADS) { printf("Currently only maximum of %2d helipads are supported (you wanted %2d)\n", MAX_HELIPADS, nofhelipads);}
    70 	// terminals/helipads are divided into groups. Groups are computed by dividing the number
   154 	// terminals/helipads are divided into groups. Groups are computed by dividing the number
    71 	// of terminals by the number of groups. Half in half. If #terminals is uneven, first group
   155 	// of terminals by the number of groups. Half in half. If #terminals is uneven, first group
    72 	// will get the less # of terminals
   156 	// will get the less # of terminals
    73 	if (nofterminalgroups > nofterminals) { printf("# of terminalgroups (%2d) must be less or equal to terminals (%2d)", nofterminals, nofterminalgroups);}
       
    74 	if (nofhelipadgroups > nofhelipads) { printf("# of helipadgroups (%2d) must be less or equal to helipads (%2d)", nofhelipads, nofhelipadgroups);}
       
    75 
   157 
    76 	assert(nofterminals <= MAX_TERMINALS);
   158 	assert(nofterminals <= MAX_TERMINALS);
    77 	assert(nofhelipads <= MAX_HELIPADS);
   159 	assert(nofhelipads <= MAX_HELIPADS);
    78 	assert(nofterminalgroups <= nofterminals);
       
    79 	assert(nofhelipadgroups <= nofhelipads);
       
    80 
   160 
    81 	Airport->nofelements = AirportGetNofElements(FA);
   161 	Airport->nofelements = AirportGetNofElements(FA);
    82 	// check
   162 	// check
    83 	if (entry_point >= Airport->nofelements) {printf("Entry point (%2d) must be within the airport positions (which is max %2d)\n", entry_point, Airport->nofelements);}
   163 	if (entry_point >= Airport->nofelements) {printf("Entry point (%2d) must be within the airport positions (which is max %2d)\n", entry_point, Airport->nofelements);}
    84 	assert(entry_point < Airport->nofelements);
   164 	assert(entry_point < Airport->nofelements);
    85 
   165 
    86 	Airport->nofterminals = nofterminals;
       
    87 	Airport->nofterminalgroups = nofterminalgroups;
       
    88 	Airport->nofhelipads = nofhelipads;
       
    89 	Airport->nofhelipadgroups = nofhelipadgroups;
       
    90 	Airport->acc_planes = acc_planes;
   166 	Airport->acc_planes = acc_planes;
    91 	Airport->entry_point = entry_point;
   167 	Airport->entry_point = entry_point;
    92 	Airport->airport_depots = depots;
   168 	Airport->airport_depots = depots;
    93 	Airport->nof_depots = nof_depots;
   169 	Airport->nof_depots = nof_depots;
    94 
   170 
    95 
   171 
    96 	// build the state machine
   172 	// build the state machine
    97 	AirportBuildAutomata(Airport, FA);
   173 	AirportBuildAutomata(Airport, FA);
    98 		DEBUG(misc, 1) ("#Elements %2d; #Terminals %2d in %d group(s); #Helipads %2d in %d group(s)", Airport->nofelements,
   174 		DEBUG(misc, 1) ("#Elements %2d; #Terminals %2d in %d group(s); #Helipads %2d in %d group(s); Entry Point %d", Airport->nofelements,
    99 				  Airport->nofterminals, Airport->nofterminalgroups, Airport->nofhelipads, Airport->nofhelipadgroups);
   175 				  nofterminals, nofterminalgroups, nofhelipads, nofhelipadgroups, Airport->entry_point);
   100 
   176 
   101 
   177 
   102 	{
   178 	{
   103 		byte _retval = AirportTestFTA(Airport);
   179 		byte _retval = AirportTestFTA(Airport);
   104 		if (_retval != MAX_ELEMENTS) {printf("ERROR with element: %d\n", _retval-1);}
   180 		if (_retval != MAX_ELEMENTS) {printf("ERROR with element: %d\n", _retval-1);}