diff options
author | peter1138 <peter1138@openttd.org> | 2008-01-23 13:20:51 +0000 |
---|---|---|
committer | peter1138 <peter1138@openttd.org> | 2008-01-23 13:20:51 +0000 |
commit | 622f2511972f2f31c893c898f4f4d77848e2d3c5 (patch) | |
tree | 6d12d48aac0eca6ada4ebf4bbf95154cb4ac247e | |
parent | 51d7692aca3fa63acdfc0d7752f9f89a1c64436a (diff) | |
download | openttd-622f2511972f2f31c893c898f4f4d77848e2d3c5.tar.xz |
(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.
-rw-r--r-- | src/ai/default/default.cpp | 21 | ||||
-rw-r--r-- | src/ai/trolly/build.cpp | 10 | ||||
-rw-r--r-- | src/build_vehicle_gui.cpp | 12 |
3 files changed, 19 insertions, 24 deletions
diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp index 6eb14a024..4d3cb8c42 100644 --- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -139,10 +139,9 @@ static EngineID AiChooseTrainToBuild(RailType railtype, Money money, byte flag, { 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 @@ static EngineID AiChooseTrainToBuild(RailType railtype, Money money, byte flag, 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 @@ static EngineID AiChooseRoadVehToBuild(CargoID cargo, Money money, TileIndex til { EngineID best_veh_index = INVALID_ENGINE; int32 best_veh_rating = 0; - EngineID i = ROAD_ENGINES_INDEX; - EngineID end = i + NUM_ROAD_ENGINES; + EngineID i; - for (; 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; if (!HasBit(e->player_avail, _current_player) || e->reliability < 0x8A3D) { continue; @@ -185,10 +181,10 @@ static EngineID AiChooseRoadVehToBuild(CargoID cargo, Money money, TileIndex til 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 @@ static EngineID AiChooseAircraftToBuild(Money money, byte flag) 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 @@ static EngineID AiChooseAircraftToBuild(Money money, byte flag) 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; diff --git a/src/ai/trolly/build.cpp b/src/ai/trolly/build.cpp index 714d80ab8..329c54d34 100644 --- a/src/ai/trolly/build.cpp +++ b/src/ai/trolly/build.cpp @@ -234,16 +234,12 @@ EngineID AiNew_PickVehicle(Player *p) } 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 @@ EngineID AiNew_PickVehicle(Player *p) 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; diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index f6f583f57..50655df53 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -773,7 +773,8 @@ static void GenerateBuildTrainList(Window *w) * 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 @@ static void GenerateBuildRoadVehList(Window *w) 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 @@ static void GenerateBuildShipList(Window *w) 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 @@ static void GenerateBuildAircraftList(Window *w) * 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; |