src/ai/default/default.cpp
branchNewGRF_ports
changeset 10184 fcf5fb2548eb
parent 6878 7d1ff2f621c7
child 10242 52b4a9006029
--- a/src/ai/default/default.cpp	Mon Apr 14 20:32:36 2008 +0000
+++ b/src/ai/default/default.cpp	Tue Apr 15 00:47:19 2008 +0000
@@ -11,11 +11,9 @@
 #include "../../roadveh.h"
 #include "../../station_map.h"
 #include "../../tunnel_map.h"
-#include "../../engine.h"
 #include "../../command_func.h"
 #include "../../town.h"
 #include "../../industry.h"
-#include "../../station.h"
 #include "../../pathfind.h"
 #include "../../airport.h"
 #include "../../depot.h"
@@ -32,6 +30,7 @@
 #include "../../settings_type.h"
 #include "default.h"
 #include "../../tunnelbridge.h"
+#include "../../order_func.h"
 
 #include "../../table/ai_rail.h"
 
@@ -264,8 +263,8 @@
 
 	FOR_VEHICLE_ORDERS(v, o) {
 		if (!o->IsValid()) continue;
-		if (!IsValidStationID(o->dest)) continue;
-		const Station *st = GetStation(o->dest);
+		if (!IsValidStationID(o->GetDestination())) continue;
+		const Station *st = GetStation(o->GetDestination());
 		if (!(st->facilities & FACIL_AIRPORT)) continue;
 
 		AirportFTAClass::Flags flags = st->Airport()->flags;
@@ -300,7 +299,7 @@
 
 static void AiHandleGotoDepot(Player *p, int cmd)
 {
-	if (_players_ai[p->index].cur_veh->current_order.type != OT_GOTO_DEPOT)
+	if (!_players_ai[p->index].cur_veh->current_order.IsType(OT_GOTO_DEPOT))
 		DoCommand(0, _players_ai[p->index].cur_veh->index, 0, DC_EXEC, cmd);
 
 	if (++_players_ai[p->index].state_counter <= 1387) {
@@ -308,9 +307,8 @@
 		return;
 	}
 
-	if (_players_ai[p->index].cur_veh->current_order.type == OT_GOTO_DEPOT) {
-		_players_ai[p->index].cur_veh->current_order.type = OT_DUMMY;
-		_players_ai[p->index].cur_veh->current_order.flags = 0;
+	if (_players_ai[p->index].cur_veh->current_order.IsType(OT_GOTO_DEPOT)) {
+		_players_ai[p->index].cur_veh->current_order.MakeDummy();
 		InvalidateWindow(WC_VEHICLE_VIEW, _players_ai[p->index].cur_veh->index);
 	}
 }
@@ -319,8 +317,8 @@
 {
 	if (bak->order == NULL) return;
 
-	for (uint i = 0; bak->order[i].type != OT_NOTHING; i++) {
-		if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
+	for (uint i = 0; !bak->order[i].IsType(OT_NOTHING); i++) {
+		if (!DoCommandP(0, v->index + (i << 16), bak->order[i].Pack(), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
 			break;
 	}
 }
@@ -1923,7 +1921,7 @@
 	arpfd.tile2 = _players_ai[p->index].cur_tile_a;
 	arpfd.flag = false;
 	arpfd.count = 0;
-	FollowTrack(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a), TRANSPORT_RAIL, 0, ReverseDiagDir(_players_ai[p->index].cur_dir_a),
+	FollowTrack(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(_players_ai[p->index].cur_dir_a), PATHFIND_FLAGS_NONE, TRANSPORT_RAIL, 0, ReverseDiagDir(_players_ai[p->index].cur_dir_a),
 		(TPFEnumProc*)AiEnumFollowTrack, NULL, &arpfd);
 	return arpfd.count > 8;
 }
@@ -1952,9 +1950,6 @@
 	{1, 3, 5, 0, 3, 2, 128 + 3, 67}
 };
 
-static const byte _dir_table_1[] = { 3, 9, 12, 6};
-static const byte _dir_table_2[] = {12, 6,  3, 9};
-
 
 static bool AiIsTileBanned(const Player* p, TileIndex tile, byte val)
 {
@@ -2027,7 +2022,7 @@
 	DiagDirection dir2 = (DiagDirection)(p[0] & 3);
 
 	tileh = GetTileSlope(tile, &z);
-	if (tileh == _dir_table_1[dir2] || (tileh == SLOPE_FLAT && z != 0)) {
+	if (tileh == InclinedSlope(ReverseDiagDir(dir2)) || (tileh == SLOPE_FLAT && z != 0)) {
 		TileIndex tile_new = tile;
 
 		// Allow bridges directly over bottom tiles
@@ -2064,7 +2059,7 @@
 {
 	uint z;
 
-	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
+	if (GetTileSlope(tile, &z) == InclinedSlope((DiagDirection)(p[0] & 3)) && z != 0) {
 		CommandCost cost = DoCommand(tile, _players_ai[arf->player->index].railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL);
 
 		if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) {
@@ -2139,8 +2134,6 @@
 }
 
 
-static const byte _dir_table_3[] = {0x25, 0x2A, 0x19, 0x16};
-
 static void AiBuildRailConstruct(Player *p)
 {
 	AiRailFinder arf;
@@ -2269,8 +2262,6 @@
 
 static bool AiRemoveTileAndGoForward(Player *p)
 {
-	byte b;
-	int bit;
 	const byte *ptr;
 	TileIndex tile = _players_ai[p->index].cur_tile_a;
 	TileIndex tilenew;
@@ -2299,11 +2290,11 @@
 	}
 
 	// Find the railtype at the position. Quit if no rail there.
-	b = GetRailTrackStatus(tile) & _dir_table_3[_players_ai[p->index].cur_dir_a];
-	if (b == 0) return false;
+	TrackBits bits = GetRailTrackStatus(tile) & DiagdirReachesTracks(ReverseDiagDir(_players_ai[p->index].cur_dir_a));
+	if (bits == TRACK_BIT_NONE) return false;
 
 	// Convert into a bit position that CMD_REMOVE_SINGLE_RAIL expects.
-	bit = FindFirstBit(b);
+	Track track = FindFirstTrack(bits);
 
 	// Then remove and signals if there are any.
 	if (IsTileType(tile, MP_RAILWAY) &&
@@ -2312,12 +2303,12 @@
 	}
 
 	// And also remove the rail.
-	if (CmdFailed(DoCommand(tile, 0, bit, DC_EXEC, CMD_REMOVE_SINGLE_RAIL)))
+	if (CmdFailed(DoCommand(tile, 0, track, DC_EXEC, CMD_REMOVE_SINGLE_RAIL)))
 		return false;
 
 	// Find the direction at the other edge of the rail.
 	ptr = _ai_table_15[ReverseDiagDir(_players_ai[p->index].cur_dir_a)];
-	while (ptr[0] != bit) ptr += 2;
+	while (ptr[0] != track) ptr += 2;
 	_players_ai[p->index].cur_dir_a = ReverseDiagDir((DiagDirection)ptr[1]);
 
 	// And then also switch tile.
@@ -2460,7 +2451,7 @@
 	uint16 best_speed    = 0;
 	uint speed;
 
-	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
+	FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_TRAIN) {
 		const RailVehicleInfo *rvi = RailVehInfo(i);
 		const Engine* e = GetEngine(i);
 
@@ -2561,15 +2552,13 @@
 		);
 		Order order;
 
-		order.type = OT_GOTO_STATION;
-		order.flags = 0;
-		order.dest = AiGetStationIdByDef(aib->use_tile, aib->cur_building_rule);
-
-		if (!is_pass && i == 1) order.flags |= OFB_UNLOAD;
+		order.MakeGoToStation(AiGetStationIdByDef(aib->use_tile, aib->cur_building_rule));
+
+		if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD);
 		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
-			order.flags |= OFB_FULL_LOAD;
-
-		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+			order.SetLoadType(OLFB_FULL_LOAD);
+
+		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 	}
 
 	DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_TRAIN);
@@ -2689,10 +2678,10 @@
 		} else if (p->mode == 1) {
 			if (_want_road_truck_station) {
 				// Truck station
-				ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | RoadStop::TRUCK, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
+				ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | ROADSTOP_TRUCK, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
 			} else {
 				// Bus station
-				ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | RoadStop::BUS, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
+				ret = DoCommand(c, p->attr, ROADTYPES_ROAD << 2 | ROADSTOP_BUS, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_ROAD_STOP);
 			}
 clear_town_stuff:;
 
@@ -2831,16 +2820,10 @@
 struct AiRoadEnum {
 	TileIndex dest;
 	TileIndex best_tile;
-	int best_track;
+	Trackdir best_track;
 	uint best_dist;
 };
 
-static const DiagDirection _dir_by_track[] = {
-	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE,
-	DIAGDIR_NE, DIAGDIR_NE,
-	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE,
-};
-
 static void AiBuildRoadRecursive(AiRoadFinder *arf, TileIndex tile, DiagDirection dir);
 
 static bool AiCheckRoadPathBetter(AiRoadFinder *arf, const byte *p)
@@ -2877,12 +2860,12 @@
 }
 
 
-static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, int track, uint length)
+static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, Trackdir track, uint length)
 {
 	uint dist = DistanceManhattan(tile, a->dest);
 
 	if (dist <= a->best_dist) {
-		TileIndex tile2 = TILE_MASK(tile + TileOffsByDiagDir(_dir_by_track[track]));
+		TileIndex tile2 = TILE_MASK(tile + TileOffsByDiagDir(TrackdirToExitdir(track)));
 
 		if (IsNormalRoadTile(tile2)) {
 			a->best_dist = dist;
@@ -2894,32 +2877,24 @@
 	return false;
 }
 
-static const uint16 _ai_road_table_and[4] = {
-	0x1009,
-	0x16,
-	0x520,
-	0x2A00,
-};
-
 static bool AiCheckRoadFinished(Player *p)
 {
 	AiRoadEnum are;
 	TileIndex tile;
 	DiagDirection dir = _players_ai[p->index].cur_dir_a;
-	uint32 bits;
 
 	are.dest = _players_ai[p->index].cur_tile_b;
 	tile = TILE_MASK(_players_ai[p->index].cur_tile_a + TileOffsByDiagDir(dir));
 
 	if (IsRoadStopTile(tile) || IsTileDepotType(tile, TRANSPORT_ROAD)) return false;
-	bits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, ROADTYPES_ROAD)) & _ai_road_table_and[dir];
-	if (bits == 0) return false;
+	TrackdirBits bits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, ROADTYPES_ROAD)) & DiagdirReachesTrackdirs(dir);
+	if (bits == TRACKDIR_BIT_NONE) return false;
 
 	are.best_dist = (uint)-1;
 
-	uint i;
-	FOR_EACH_SET_BIT(i, bits) {
-		FollowTrack(tile, 0x1000 | TRANSPORT_ROAD, ROADTYPES_ROAD, (DiagDirection)_dir_by_track[i], (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
+	while (bits != TRACKDIR_BIT_NONE) {
+		Trackdir trackdir = RemoveFirstTrackdir(&bits);
+		FollowTrack(tile, PATHFIND_FLAGS_DISABLE_TILE_HASH, TRANSPORT_ROAD, ROADTYPES_ROAD, TrackdirToExitdir(trackdir), (TPFEnumProc*)AiEnumFollowRoad, NULL, &are);
 	}
 
 	if (DistanceManhattan(tile, are.dest) <= are.best_dist) return false;
@@ -2927,7 +2902,7 @@
 	if (are.best_dist == 0) return true;
 
 	_players_ai[p->index].cur_tile_a = are.best_tile;
-	_players_ai[p->index].cur_dir_a = _dir_by_track[are.best_track];
+	_players_ai[p->index].cur_dir_a = TrackdirToExitdir(are.best_track);
 	return false;
 }
 
@@ -2954,7 +2929,7 @@
 	DiagDirection dir2 = (DiagDirection)(p[0] & 3);
 
 	tileh = GetTileSlope(tile, &z);
-	if (tileh == _dir_table_1[dir2] || (tileh == SLOPE_FLAT && z != 0)) {
+	if (tileh == InclinedSlope(ReverseDiagDir(dir2)) || (tileh == SLOPE_FLAT && z != 0)) {
 		TileIndex tile_new = tile;
 
 		// Allow bridges directly over bottom tiles
@@ -2992,7 +2967,7 @@
 {
 	uint z;
 
-	if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) {
+	if (GetTileSlope(tile, &z) == InclinedSlope((DiagDirection)(p[0] & 3)) && z != 0) {
 		CommandCost cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
 
 		if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) {
@@ -3312,15 +3287,13 @@
 		);
 		Order order;
 
-		order.type = OT_GOTO_STATION;
-		order.flags = 0;
-		order.dest = AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule);
-
-		if (!is_pass && i == 1) order.flags |= OFB_UNLOAD;
+		order.MakeGoToStation(AiGetStationIdFromRoadBlock(aib->use_tile, aib->cur_building_rule));
+
+		if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD);
 		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
-			order.flags |= OFB_FULL_LOAD;
-
-		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+			order.SetLoadType(OLFB_FULL_LOAD);
+
+		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 	}
 
 	DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
@@ -3569,15 +3542,13 @@
 		bool is_pass = (_players_ai[p->index].cargo_type == CT_PASSENGERS || _players_ai[p->index].cargo_type == CT_MAIL);
 		Order order;
 
-		order.type = OT_GOTO_STATION;
-		order.flags = 0;
-		order.dest = GetStationIndex(tile);
-
-		if (!is_pass && i == 1) order.flags |= OFB_UNLOAD;
+		order.MakeGoToStation(GetStationIndex(tile));
+
+		if (!is_pass && i == 1) order.SetUnloadType(OUFB_UNLOAD);
 		if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
-			order.flags |= OFB_FULL_LOAD;
-
-		DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+			order.SetLoadType(OLFB_FULL_LOAD);
+
+		DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
 	}
 
 	DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
@@ -3612,7 +3583,7 @@
 		if (v->type == VEH_TRAIN) {
 
 			if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
-				if (v->current_order.type != OT_GOTO_DEPOT)
+				if (!v->current_order.IsType(OT_GOTO_DEPOT))
 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_TRAIN_TO_DEPOT);
 				goto going_to_depot;
 			}
@@ -3622,7 +3593,7 @@
 
 		} else if (v->type == VEH_ROAD) {
 			if (!v->IsStoppedInDepot()) {
-				if (v->current_order.type != OT_GOTO_DEPOT)
+				if (!v->current_order.IsType(OT_GOTO_DEPOT))
 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
 				goto going_to_depot;
 			}
@@ -3630,7 +3601,7 @@
 			DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
 		} else if (v->type == VEH_AIRCRAFT) {
 			if (!v->IsStoppedInDepot()) {
-				if (v->current_order.type != OT_GOTO_DEPOT)
+				if (!v->current_order.IsType(OT_GOTO_DEPOT))
 					DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
 				goto going_to_depot;
 			}
@@ -3645,9 +3616,8 @@
 going_to_depot:;
 	if (++_players_ai[p->index].state_counter <= 832) return;
 
-	if (v->current_order.type == OT_GOTO_DEPOT) {
-		v->current_order.type = OT_DUMMY;
-		v->current_order.flags = 0;
+	if (v->current_order.IsType(OT_GOTO_DEPOT)) {
+		v->current_order.MakeDummy();
 		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
 	}
 return_to_loop:;
@@ -3668,7 +3638,7 @@
 	byte *in_use = MallocT<byte>(GetMaxStationIndex() + 1);
 	memset(in_use, 0, GetMaxStationIndex() + 1);
 	FOR_ALL_ORDERS(ord) {
-		if (ord->type == OT_GOTO_STATION) in_use[ord->dest] = 1;
+		if (ord->IsType(OT_GOTO_STATION)) in_use[ord->GetDestination()] = 1;
 	}
 
 	// Go through all stations and delete those that aren't in use