changeset 8389:1604f6eb4b5a draft

(svn r11959) -Codechange: Use macro to loop for specific engine types instead of using specific indexes each time. -Codechange: Minor scope changes in said loops.
author peter1138 <peter1138@openttd.org>
date Wed, 23 Jan 2008 13:20:51 +0000
parents 11a08bc12922
children 381a22f19287
files src/ai/default/default.cpp src/ai/trolly/build.cpp src/build_vehicle_gui.cpp
diffstat 3 files changed, 20 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/default/default.cpp
+++ b/src/ai/default/default.cpp
@@ -139,10 +139,9 @@
 {
 	EngineID best_veh_index = INVALID_ENGINE;
 	byte best_veh_score = 0;
-	CommandCost ret;
 	EngineID i;
 
-	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);
 
@@ -154,7 +153,7 @@
 			continue;
 		}
 
-		ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE);
+		CommandCost ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE);
 		if (CmdSucceeded(ret) && ret.GetCost() <= money && rvi->ai_rank >= best_veh_score) {
 			best_veh_score = rvi->ai_rank;
 			best_veh_index = i;
@@ -168,14 +167,11 @@
 {
 	EngineID best_veh_index = INVALID_ENGINE;
 	int32 best_veh_rating = 0;
-	EngineID i = ROAD_ENGINES_INDEX;
-	EngineID end = i + NUM_ROAD_ENGINES;
-
-	for (; i != end; i++) {
+	EngineID i;
+
+	FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_ROAD) {
 		const RoadVehicleInfo *rvi = RoadVehInfo(i);
 		const Engine* e = GetEngine(i);
-		int32 rating;
-		CommandCost ret;
 
 		if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
 			continue;
@@ -185,10 +181,10 @@
 		if (rvi->cargo_type != cargo && !CanRefitTo(i, cargo)) continue;
 
 		/* Rate and compare the engine by speed & capacity */
-		rating = rvi->max_speed * rvi->capacity;
+		int rating = rvi->max_speed * rvi->capacity;
 		if (rating <= best_veh_rating) continue;
 
-		ret = DoCommand(tile, i, 0, 0, CMD_BUILD_ROAD_VEH);
+		CommandCost ret = DoCommand(tile, i, 0, 0, CMD_BUILD_ROAD_VEH);
 		if (CmdFailed(ret)) continue;
 
 		/* Add the cost of refitting */
@@ -208,9 +204,8 @@
 	Money best_veh_cost = 0;
 	EngineID i;
 
-	for (i = AIRCRAFT_ENGINES_INDEX; i != AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; i++) {
+	FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_AIRCRAFT) {
 		const Engine* e = GetEngine(i);
-		CommandCost ret;
 
 		if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
 			continue;
@@ -218,7 +213,7 @@
 
 		if ((AircraftVehInfo(i)->subtype & AIR_CTOL) != flag) continue;
 
-		ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT);
+		CommandCost ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT);
 		if (CmdSucceeded(ret) && ret.GetCost() <= money && ret.GetCost() >= best_veh_cost) {
 			best_veh_cost = ret.GetCost();
 			best_veh_index = i;
--- a/src/ai/trolly/build.cpp
+++ b/src/ai/trolly/build.cpp
@@ -234,16 +234,12 @@
 	} else {
 		EngineID best_veh_index = INVALID_ENGINE;
 		int32 best_veh_rating = 0;
-		EngineID start = ROAD_ENGINES_INDEX;
-		EngineID end   = ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES;
 		EngineID i;
 
 		/* Loop through all road vehicles */
-		for (i = start; i != end; i++) {
+		FOR_ALL_ENGINEIDS_OF_TYPE(i, VEH_ROAD) {
 			const RoadVehicleInfo *rvi = RoadVehInfo(i);
 			const Engine* e = GetEngine(i);
-			int32 rating;
-			CommandCost ret;
 
 			/* Skip vehicles which can't take our cargo type */
 			if (rvi->cargo_type != _players_ainew[p->index].cargo && !CanRefitTo(i, _players_ainew[p->index].cargo)) continue;
@@ -256,11 +252,11 @@
 			if (!HasBit(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
 
 			/* Rate and compare the engine by speed & capacity */
-			rating = rvi->max_speed * rvi->capacity;
+			int rating = rvi->max_speed * rvi->capacity;
 			if (rating <= best_veh_rating) continue;
 
 			// Can we build it?
-			ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
+			CommandCost ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
 			if (CmdFailed(ret)) continue;
 
 			best_veh_rating = rating;
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -773,7 +773,8 @@
 	 * Also check to see if the previously selected engine is still available,
 	 * and if not, reset selection to INVALID_ENGINE. This could be the case
 	 * when engines become obsolete and are removed */
-	for (EngineID eid = 0; eid < NUM_TRAIN_ENGINES; eid++) {
+	EngineID eid;
+	FOR_ALL_ENGINEIDS_OF_TYPE(eid, VEH_TRAIN) {
 		const RailVehicleInfo *rvi = RailVehInfo(eid);
 
 		if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, bv->filter.railtype)) continue;
@@ -811,7 +812,8 @@
 
 	EngList_RemoveAll(&bv->eng_list);
 
-	for (EngineID eid = ROAD_ENGINES_INDEX; eid < ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES; eid++) {
+	EngineID eid;
+	FOR_ALL_ENGINEIDS_OF_TYPE(eid, VEH_ROAD) {
 		if (!IsEngineBuildable(eid, VEH_ROAD, _local_player)) continue;
 		if (!HasBit(bv->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
 		EngList_Add(&bv->eng_list, eid);
@@ -829,7 +831,8 @@
 
 	EngList_RemoveAll(&bv->eng_list);
 
-	for (EngineID eid = SHIP_ENGINES_INDEX; eid < SHIP_ENGINES_INDEX + NUM_SHIP_ENGINES; eid++) {
+	EngineID eid;
+	FOR_ALL_ENGINEIDS_OF_TYPE(eid, VEH_SHIP) {
 		if (!IsEngineBuildable(eid, VEH_SHIP, _local_player)) continue;
 		EngList_Add(&bv->eng_list, eid);
 
@@ -850,7 +853,8 @@
 	 * Also check to see if the previously selected plane is still available,
 	 * and if not, reset selection to INVALID_ENGINE. This could be the case
 	 * when planes become obsolete and are removed */
-	for (EngineID eid = AIRCRAFT_ENGINES_INDEX; eid < AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; eid++) {
+	EngineID eid;
+	FOR_ALL_ENGINEIDS_OF_TYPE(eid, VEH_AIRCRAFT) {
 		if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_player)) continue;
 		/* First VEH_END window_numbers are fake to allow a window open for all different types at once */
 		if (w->window_number > VEH_END && !CanAircraftUseStation(eid, w->window_number)) continue;