(svn r10898) [NewGRF_ports] -Codechange: replace GetAirport and GetValidAiports by GetAvailableAirports which gives a list of available airports that (primarily) AIs can iterate over. NewGRF_ports
authorrubidium
Tue, 14 Aug 2007 22:27:18 +0000
branchNewGRF_ports
changeset 6818 8c977ca780fe
parent 6817 ef4c1d63b69f
child 6819 67529db0fd74
(svn r10898) [NewGRF_ports] -Codechange: replace GetAirport and GetValidAiports by GetAvailableAirports which gives a list of available airports that (primarily) AIs can iterate over.
bin/data/sprites/airportsbasic.nfo
src/ai/default/default.cpp
src/airport.cpp
src/airport.h
src/airport_movement.h
src/station.h
src/table/ai_rail.h
--- a/bin/data/sprites/airportsbasic.nfo	Tue Aug 14 21:30:05 2007 +0000
+++ b/bin/data/sprites/airportsbasic.nfo	Tue Aug 14 22:27:18 2007 +0000
@@ -694,7 +694,7 @@
 //------------------------------
 // Heliport
 //------------------------------
-// 10 properties to change, 1 airport to change, airport id = 1
+// 10 properties to change, 1 airport to change, airport id = 2
    23 * 204	 00 0D 0A 01 02
 
 // Class ID
@@ -813,7 +813,7 @@
 // Oilrig
 //-------------------------
 
-// 10 properties to change, 1 station to change, station id = 03
+// 10 properties to change, 1 station to change, station id = 3
    27 * 189	 00 0D 09 01 03
 
 // Class ID
--- a/src/ai/default/default.cpp	Tue Aug 14 21:30:05 2007 +0000
+++ b/src/ai/default/default.cpp	Tue Aug 14 22:27:18 2007 +0000
@@ -3386,65 +3386,55 @@
 	p->ai.state_counter = 0;
 }
 
-static CommandCost AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag)
+static CommandCost AiDoBuildDefaultAirportBlock(TileIndex tile, uint16 airport, byte flag)
 {
-	uint32 avail_airports = GetValidAirports();
 	CommandCost total_cost, ret;
 
-	for (; p->mode == 0; p++) {
-		if (!HASBIT(avail_airports, p->attr)) return CMD_ERROR;
-		ret = DoCommand(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_AIRPORT);
-		if (CmdFailed(ret)) return CMD_ERROR;
-		total_cost.AddCost(ret);
-	}
+	ret = DoCommand(tile, airport, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_AIRPORT);
+	if (CmdFailed(ret)) return CMD_ERROR;
+	total_cost.AddCost(ret);
 
 	return total_cost;
 }
 
-static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
+static bool AiCheckAirportResources(TileIndex tile, const FSMportsSpec *p, byte cargo)
 {
 	uint values[NUM_CARGO];
 
-	for (; p->mode == 0; p++) {
-		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
-		const AirportFTAClass* airport = GetAirport(p->attr);
-		uint w = airport->size_x;
-		uint h = airport->size_y;
-		uint rad = _patches.modified_catchment ? airport->catchment : 4;
-
-		if (cargo & 0x80) {
-			GetProductionAroundTiles(values, tile2, w, h, rad);
-			return values[cargo & 0x7F] != 0;
-		} else {
-			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
-			return values[cargo] >= 8;
-		}
+	uint w = p->size_x[0];
+	uint h = p->size_y[0];
+	uint rad = _patches.modified_catchment ? p->portFSM->catchment : 4;
+
+	if (cargo & 0x80) {
+		GetProductionAroundTiles(values, tile, w, h, rad);
+		return values[cargo & 0x7F] != 0;
+	} else {
+		GetAcceptanceAroundTiles(values, tile, w, h, rad);
+		return values[cargo] >= 8;
 	}
 	return true;
 }
 
-static int AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, CommandCost *cost)
+static uint16 AiFindBestDefaultAirportBlock(TileIndex tile, byte cargo, byte heli, CommandCost *cost)
 {
-	const AiDefaultBlockData *p;
-	uint i;
-
-	for (i = 0; (p = _airport_default_block_data[i]) != NULL; i++) {
+	std::list<uint16> airports = GetAvailableAirports();
+
+	for (std::list<uint16>::const_iterator it = airports.begin(); it != airports.end(); it++) {
+		const FSMportsSpec *spec = GetCustomFSMportsSpec((FSMportsClassID)GB(*it, 8, 8), GB(*it, 0, 8));
 		// If we are doing a helicopter service, avoid building
 		// airports where they can't land.
-		if (heli && !(GetAirport(p->attr)->flags & AirportFTAClass::HELICOPTERS)) continue;
-
-		*cost = AiDoBuildDefaultAirportBlock(tile, p, 0);
-		if (CmdSucceeded(*cost) && AiCheckAirportResources(tile, p, cargo))
-			return i;
+		if (heli && !(spec->flags & AirportFTAClass::HELICOPTERS)) continue;
+
+		*cost = AiDoBuildDefaultAirportBlock(tile, *it, 0);
+		if (CmdSucceeded(*cost) && AiCheckAirportResources(tile, spec, cargo)) return *it;
 	}
-	return -1;
+	return 0xFFFF;
 }
 
 static void AiStateBuildDefaultAirportBlocks(Player *p)
 {
 	int i, j;
 	AiBuildRec *aib;
-	int rule;
 	CommandCost cost;
 
 	// time out?
@@ -3468,11 +3458,11 @@
 			aib->use_tile = AdjustTileCoordRandomly(aib->spec_tile, aib->rand_rng);
 
 			// check if the aircraft stuff can be built there.
-			rule = AiFindBestDefaultAirportBlock(aib->use_tile, aib->cargo, p->ai.build_kind, &cost);
+			int airport = AiFindBestDefaultAirportBlock(aib->use_tile, aib->cargo, p->ai.build_kind, &cost);
 
 //			SetRedErrorSquare(aib->use_tile);
 
-			if (rule == -1) {
+			if (airport == 0xFFFF) {
 				// cannot build, terraform after a while
 				if (p->ai.state_counter >= 600) {
 					AiDoTerraformLand(aib->use_tile, (DiagDirection)(Random() & 3), 3, (int8)p->ai.state_mode);
@@ -3486,11 +3476,11 @@
 				// player has money, build it.
 				CommandCost r;
 
-				aib->cur_building_rule = rule;
+				aib->cur_building_rule = 255;
 
 				r = AiDoBuildDefaultAirportBlock(
 					aib->use_tile,
-					_airport_default_block_data[rule],
+					airport,
 					DC_EXEC | DC_NO_TOWN_RATING
 				);
 				assert(CmdSucceeded(r));
@@ -3509,26 +3499,13 @@
 	p->ai.state = AIS_BUILD_AIRCRAFT_VEHICLES;
 }
 
-static StationID AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
-{
-	const AiDefaultBlockData *p = _airport_default_block_data[id];
-	while (p->mode != 1) p++;
-	return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)));
-}
-
 static void AiStateBuildAircraftVehicles(Player *p)
 {
-	const AiDefaultBlockData *ptr;
-	TileIndex tile;
+	TileIndex tile = p->ai.src.use_tile;
 	EngineID veh;
 	int i;
 	VehicleID loco_id;
 
-	ptr = _airport_default_block_data[p->ai.src.cur_building_rule];
-	for (; ptr->mode != 0; ptr++) {}
-
-	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
-
 	veh = AiChooseAircraftToBuild(p->player_money, p->ai.build_kind != 0 ? 0 : AIR_CTOL);
 	if (veh == INVALID_ENGINE) return;
 
@@ -3539,13 +3516,12 @@
 	loco_id = _new_vehicle_id;
 
 	for (i = 0; p->ai.order_list_blocks[i] != 0xFF; i++) {
-		AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i];
 		bool is_pass = (p->ai.cargo_type == CT_PASSENGERS || p->ai.cargo_type == CT_MAIL);
 		Order order;
 
 		order.type = OT_GOTO_STATION;
 		order.flags = 0;
-		order.dest = AiGetStationIdFromAircraftBlock(aib->use_tile, aib->cur_building_rule);
+		order.dest = GetStationIndex(tile);
 
 		if (!is_pass && i == 1) order.flags |= OF_UNLOAD;
 		if (p->ai.num_want_fullload != 0 && (is_pass || i == 0))
--- a/src/airport.cpp	Tue Aug 14 21:30:05 2007 +0000
+++ b/src/airport.cpp	Tue Aug 14 22:27:18 2007 +0000
@@ -9,9 +9,10 @@
 #include "airport.h"
 #include "macros.h"
 #include "variables.h"
-#include "airport_movement.h"
 #include "date.h"
 #include "helpers.hpp"
+#include "newgrf_fsmports.h"
+#include "airport_movement.h"
 
 /* Uncomment this to print out a full report of the airport-structure
  * You should either use
@@ -20,7 +21,7 @@
  * - false: give a summarized report which only shows current and next position */
 //#define DEBUG_AIRPORT false
 
-static AirportFTAClass *DummyAirport;
+AirportFTAClass *DummyAirport;
 static AirportFTAClass *Oilrig;
 
 
@@ -326,21 +327,20 @@
 }
 #endif
 
-const AirportFTAClass *GetAirport(const byte airport_type)
+std::list<uint16> GetAvailableAirports()
 {
-	//FIXME -- AircraftNextAirportPos_and_Order -> Needs something nicer, don't like this code
-	// needs constant change if more airports are added
-	switch (airport_type) {
-		default: return DummyAirport;
-		case AT_OILRIG: return Oilrig;
+	std::list<uint16> airports;
+
+	for (uint i = 0; i < GetNumFSMportsClasses(); i++) {
+		for (uint j = 0; j < GetNumCustomFSMports((FSMportsClassID)i); j++) {
+			const FSMportsSpec *spec = GetCustomFSMportsSpec((FSMportsClassID)i, j);
+
+			if (!HASBIT(spec->callbackmask, CBM_STATION_AVAIL) ||
+					GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, spec, NULL, INVALID_TILE) != 0) {
+				airports.push_back(i << 8 | j);
+			}
+		}
 	}
+
+	return airports;
 }
-
-
-uint32 GetValidAirports()
-{
-	return 0;
-}
-
-
-
--- a/src/airport.h	Tue Aug 14 21:30:05 2007 +0000
+++ b/src/airport.h	Tue Aug 14 22:27:18 2007 +0000
@@ -7,6 +7,7 @@
 
 #include "direction.h"
 #include "fsmblockmap.h"
+#include <list>
 
 enum {MAX_TERMINALS =  36};
 enum {MAX_HELIPADS  =  24};
@@ -105,13 +106,12 @@
 
 void InitializeAirports();
 void UnInitializeAirports();
-const AirportFTAClass *GetAirport(const byte airport_type);
 
-/** Get buildable airport bitmask.
- * @return get all buildable airports at this given time, bitmasked.
- * Bit 0 means the small airport is buildable, etc.
- * @todo set availability of airports by year, instead of airplane
+/**
+ * Get a list of currently available airports.
+ * @return the list with available airports.
+ * @note return is "class index" << 8 | "index within class"
  */
-uint32 GetValidAirports();
+std::list<uint16> GetAvailableAirports();
 
 #endif /* AIRPORT_H */
--- a/src/airport_movement.h	Tue Aug 14 21:30:05 2007 +0000
+++ b/src/airport_movement.h	Tue Aug 14 22:27:18 2007 +0000
@@ -7,15 +7,6 @@
 
 #include "airport_states.h"
 
-// state machine input struct (from external file, etc.)
-// Finite sTate mAchine --> FTA
-struct AirportFTAbuildup {
-	byte position; // the position that an airplane is at
-	byte heading;  // the current orders (eg. TAKEOFF, HANGAR, ENDLANDING, etc.)
-	uint64 block;  // the block this position is on on the airport (st->airport_flags)
-	byte next;     // next position from this position
-};
-
 ///////////////////////////////////////////////////////////////////////
 /////*********Movement Positions on Airports********************///////
 
--- a/src/station.h	Tue Aug 14 21:30:05 2007 +0000
+++ b/src/station.h	Tue Aug 14 22:27:18 2007 +0000
@@ -117,10 +117,10 @@
 
 		const AirportFTAClass *Airport() const
 		{
-			if (IsCustomFSMportsSpecIndex(airport_tile))
-				return fsmportsspeclist[1].spec->portFSM;
-			if (airport_tile == 0) return GetAirport(AT_DUMMY);
-			return GetAirport(airport_type);
+			if (IsCustomFSMportsSpecIndex(airport_tile)) return fsmportsspeclist[1].spec->portFSM;
+
+			extern AirportFTAClass *DummyAirport;
+			return DummyAirport;
 		}
 
 	TileIndex xy;
--- a/src/table/ai_rail.h	Tue Aug 14 21:30:05 2007 +0000
+++ b/src/table/ai_rail.h	Tue Aug 14 22:27:18 2007 +0000
@@ -561,50 +561,3 @@
 #undef MKRAIL
 #undef MKCLRRAIL
 #undef MKEND
-
-
-#define MKAIR(a, b, c) {0, (DiagDirection)a, {b, c}}
-#define MKEND() {1, (DiagDirection)0, {0, 0}}
-
-static const AiDefaultBlockData _airportdata_ai_0[] = {
-	MKAIR(1, 0, 0),
-	MKEND(),
-};
-
-static const AiDefaultBlockData _airportdata_ai_1[] = {
-	MKAIR(0, 0, 0),
-	MKEND(),
-};
-
-static const AiDefaultBlockData _airportdata_ai_3[] = {
-	MKAIR(3, 0, 0),
-	MKEND(),
-};
-
-static const AiDefaultBlockData _airportdata_ai_4[] = {
-	MKAIR(4, 0, 0),
-	MKEND(),
-};
-
-static const AiDefaultBlockData _airportdata_ai_5[] = {
-	MKAIR(5, 0, 0),
-	MKEND(),
-};
-
-static const AiDefaultBlockData _airportdata_ai_7[] = {
-	MKAIR(7, 0, 0),
-	MKEND(),
-};
-
-#undef MKAIR
-#undef MDEND
-
-static const AiDefaultBlockData * const _airport_default_block_data[] = {
-	_airportdata_ai_7, // intercontinental airport
-	_airportdata_ai_4, // international airport
-	_airportdata_ai_3, // metropolitan airport
-	_airportdata_ai_0, // city airport
-	_airportdata_ai_5, // commuter airport
-	_airportdata_ai_1, // country airport
-	NULL
-};