diff options
author | rubidium <rubidium@openttd.org> | 2007-06-18 19:53:50 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2007-06-18 19:53:50 +0000 |
commit | 773a7c9cb64856bd2bca11f9c38eea049b353e4e (patch) | |
tree | 62843984493ffedb69f91d7b85cb631ecb61ff3e /src/ai | |
parent | bcdb89906b06a64c368e89d2f267cd617b2210c1 (diff) | |
download | openttd-773a7c9cb64856bd2bca11f9c38eea049b353e4e.tar.xz |
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
Diffstat (limited to 'src/ai')
-rw-r--r-- | src/ai/ai.cpp | 2 | ||||
-rw-r--r-- | src/ai/default/default.cpp | 43 | ||||
-rw-r--r-- | src/ai/trolly/build.cpp | 33 | ||||
-rw-r--r-- | src/ai/trolly/trolly.cpp | 14 |
4 files changed, 47 insertions, 45 deletions
diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index ec8837f2c..8086ef29a 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -84,7 +84,7 @@ static void AI_PutCommandInQueue(PlayerID player, TileIndex tile, uint32 p1, uin CommandCost AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc, CommandCallback* callback) { PlayerID old_lp; - CommandCost res = 0; + CommandCost res; const char* tmp_cmdtext; /* If you enable DC_EXEC with DC_QUERY_COST you are a really strange diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp index d5d3922c8..a5bc10514 100644 --- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -152,7 +152,7 @@ static EngineID AiChooseTrainToBuild(RailType railtype, int32 money, byte flag, } ret = DoCommand(tile, i, 0, 0, CMD_BUILD_RAIL_VEHICLE); - if (CmdSucceeded(ret) && ret <= money && rvi->ai_rank >= best_veh_score) { + if (CmdSucceeded(ret) && ret.GetCost() <= money && rvi->ai_rank >= best_veh_score) { best_veh_score = rvi->ai_rank; best_veh_index = i; } @@ -189,8 +189,8 @@ static EngineID AiChooseRoadVehToBuild(CargoID cargo, int32 money, TileIndex til if (CmdFailed(ret)) continue; /* Add the cost of refitting */ - if (rvi->cargo_type != cargo) ret += GetRefitCost(i); - if (ret > money) continue; + if (rvi->cargo_type != cargo) ret.AddCost(GetRefitCost(i)); + if (ret.GetCost() > money) continue; best_veh_rating = rating; best_veh_index = i; @@ -216,8 +216,8 @@ static EngineID AiChooseAircraftToBuild(int32 money, byte flag) if ((AircraftVehInfo(i)->subtype & AIR_CTOL) != flag) continue; ret = DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT); - if (CmdSucceeded(ret) && ret <= money && ret >= best_veh_cost) { - best_veh_cost = ret; + if (CmdSucceeded(ret) && ret.GetCost() <= money && ret.GetCost() >= best_veh_cost) { + best_veh_cost = ret.GetCost(); best_veh_index = i; } } @@ -1641,7 +1641,7 @@ static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, b static CommandCost AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBlockData* p, RailType railtype, byte flag) { CommandCost ret; - CommandCost total_cost = 0; + CommandCost total_cost; Town *t = NULL; int rating = 0; int i, j, k; @@ -1662,7 +1662,7 @@ static CommandCost AiDoBuildDefaultRailTrack(TileIndex tile, const AiDefaultBloc } if (CmdFailed(ret)) return CMD_ERROR; - total_cost += ret; + total_cost.AddCost(ret); clear_town_stuff:; if (_cleared_town != NULL) { @@ -1684,7 +1684,7 @@ clear_town_stuff:; k = i; ret = DoCommand(c, railtype, i, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); if (CmdFailed(ret)) return CMD_ERROR; - total_cost += ret; + total_cost.AddCost(ret); } } @@ -1699,17 +1699,18 @@ clear_town_stuff:; ret = DoCommand(c, k, 0, flag, CMD_BUILD_SIGNALS); } while (--j); } else { - ret = _price.build_signals; + ret.AddCost(_price.build_signals); } if (CmdFailed(ret)) return CMD_ERROR; - total_cost += ret; + total_cost.AddCost(ret); } } else if (p->mode == 3) { //Clear stuff and then build single rail. if (GetTileSlope(c, NULL) != SLOPE_FLAT) return CMD_ERROR; ret = DoCommand(c, 0, 0, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_LANDSCAPE_CLEAR); if (CmdFailed(ret)) return CMD_ERROR; - total_cost += ret + _price.build_rail; + total_cost.AddCost(ret); + total_cost.AddCost(_price.build_rail); if (flag & DC_EXEC) { DoCommand(c, railtype, p->attr & 1, flag | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, CMD_BUILD_SINGLE_RAIL); @@ -2069,7 +2070,7 @@ static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) { CommandCost cost = DoCommand(tile, arf->player->ai.railtype_to_use, 0, DC_AUTO, CMD_BUILD_TUNNEL); - if (CmdSucceeded(cost) && cost <= (arf->player->player_money >> 4)) { + if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) { AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0] & 3); if (arf->depth == 1) AiCheckRailPathBetter(arf, p); } @@ -2210,8 +2211,8 @@ static void AiBuildRailConstruct(Player *p) */ for (i = MAX_BRIDGES - 1; i != 0; i--) { if (CheckBridge_Stuff(i, bridge_len)) { - int32 cost = DoCommand(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE); - if (CmdSucceeded(cost) && cost < (p->player_money >> 5)) break; + CommandCost cost = DoCommand(arf.bridge_end_tile, p->ai.cur_tile_a, i | (p->ai.railtype_to_use << 8), DC_AUTO, CMD_BUILD_BRIDGE); + if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 5)) break; } } @@ -2629,7 +2630,7 @@ static int AiFindBestDefaultRoadBlock(TileIndex tile, byte direction, byte cargo static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag) { CommandCost ret; - CommandCost total_cost = 0; + CommandCost total_cost; Town *t = NULL; int rating = 0; int roadflag = 0; @@ -2651,7 +2652,7 @@ static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBloc ret = DoCommand(c, p->attr, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); if (CmdFailed(ret)) return CMD_ERROR; - total_cost += ret; + total_cost.AddCost(ret); continue; } @@ -2671,7 +2672,7 @@ static CommandCost AiDoBuildDefaultRoadBlock(TileIndex tile, const AiDefaultBloc clear_town_stuff:; if (CmdFailed(ret)) return CMD_ERROR; - total_cost += ret; + total_cost.AddCost(ret); if (_cleared_town != NULL) { if (t != NULL && t != _cleared_town) return CMD_ERROR; @@ -2969,7 +2970,7 @@ static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, if (GetTileSlope(tile, &z) == _dir_table_2[p[0] & 3] && z != 0) { CommandCost cost = DoCommand(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL); - if (CmdSucceeded(cost) && cost <= (arf->player->player_money >> 4)) { + if (CmdSucceeded(cost) && cost.GetCost() <= (arf->player->player_money >> 4)) { AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0] & 3); if (arf->depth == 1) AiCheckRoadPathBetter(arf, p); } @@ -3102,7 +3103,7 @@ do_some_terraform: for (i = 10; i != 0; i--) { if (CheckBridge_Stuff(i, bridge_len)) { CommandCost cost = DoCommand(tile, p->ai.cur_tile_a, i + ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE); - if (CmdSucceeded(cost) && cost < (p->player_money >> 5)) break; + if (CmdSucceeded(cost) && cost.GetCost() < (p->player_money >> 5)) break; } } @@ -3390,13 +3391,13 @@ static void AiStateAirportStuff(Player *p) static CommandCost AiDoBuildDefaultAirportBlock(TileIndex tile, const AiDefaultBlockData *p, byte flag) { uint32 avail_airports = GetValidAirports(); - CommandCost total_cost = 0, ret; + 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 += ret; + total_cost.AddCost(ret); } return total_cost; diff --git a/src/ai/trolly/build.cpp b/src/ai/trolly/build.cpp index 25b7ebd8d..41c80529c 100644 --- a/src/ai/trolly/build.cpp +++ b/src/ai/trolly/build.cpp @@ -97,7 +97,7 @@ CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, TileIndex *route = PathFinderInfo->route; int dir; int old_dir = -1; - CommandCost cost = 0; + CommandCost cost; CommandCost res; // We need to calculate the direction with the parent of the parent.. so we skip // the first pieces and the last piece @@ -105,30 +105,30 @@ CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, // When we are done, stop it if (part >= PathFinderInfo->route_length - 1) { PathFinderInfo->position = -2; - return 0; + return CommandCost(); } if (PathFinderInfo->rail_or_road) { // Tunnel code if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { - cost += AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL); + cost.AddCost(AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL)); PathFinderInfo->position++; // TODO: problems! if (CmdFailed(cost)) { DEBUG(ai, 0, "[BuildPath] tunnel could not be built (0x%X)", route[part]); - return 0; + return CommandCost(); } return cost; } // Bridge code if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { - cost += AiNew_Build_Bridge(p, route[part], route[part - 1], flag); + cost.AddCost(AiNew_Build_Bridge(p, route[part], route[part - 1], flag)); PathFinderInfo->position++; // TODO: problems! if (CmdFailed(cost)) { DEBUG(ai, 0, "[BuildPath] bridge could not be built (0x%X, 0x%X)", route[part], route[part - 1]); - return 0; + return CommandCost(); } return cost; } @@ -147,9 +147,9 @@ CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, if (CmdFailed(res)) { // Problem.. let's just abort it all! p->ainew.state = AI_STATE_NOTHING; - return 0; + return CommandCost(); } - cost += res; + cost.AddCost(res); // Go to the next tile part++; // Check if it is still in range.. @@ -162,23 +162,23 @@ CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, } else { // Tunnel code if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { - cost += AI_DoCommand(route[part], 0x200 | ROADTYPES_ROAD, 0, flag, CMD_BUILD_TUNNEL); + cost.AddCost(AI_DoCommand(route[part], 0x200 | ROADTYPES_ROAD, 0, flag, CMD_BUILD_TUNNEL)); PathFinderInfo->position++; // TODO: problems! if (CmdFailed(cost)) { DEBUG(ai, 0, "[BuildPath] tunnel could not be built (0x%X)", route[part]); - return 0; + return CommandCost(); } return cost; } // Bridge code if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { - cost += AiNew_Build_Bridge(p, route[part], route[part + 1], flag); + cost.AddCost(AiNew_Build_Bridge(p, route[part], route[part + 1], flag)); PathFinderInfo->position++; // TODO: problems! if (CmdFailed(cost)) { DEBUG(ai, 0, "[BuildPath] bridge could not be built (0x%X, 0x%X)", route[part], route[part + 1]); - return 0; + return CommandCost(); } return cost; } @@ -203,10 +203,10 @@ CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, // Problem.. let's just abort it all! DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]); p->ainew.state = AI_STATE_NOTHING; - return 0; + return CommandCost(); } - if (CmdSucceeded(res)) cost += res; + if (CmdSucceeded(res)) cost.AddCost(res); } // Go to the next tile part++; @@ -314,11 +314,12 @@ CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT); } else { ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT); - if (CmdFailed(ret)) return ret; + if (CmdFailed(ret2)) return ret; // Try to build the road from the depot ret2 = AI_DoCommand(tile + TileOffsByDiagDir(direction), DiagDirToRoadBits(ReverseDiagDir(direction)), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); // If it fails, ignore it.. if (CmdFailed(ret2)) return ret; - return ret + ret2; + ret.AddCost(ret2); + return ret; } } diff --git a/src/ai/trolly/trolly.cpp b/src/ai/trolly/trolly.cpp index f0e3e2500..fb0263d41 100644 --- a/src/ai/trolly/trolly.cpp +++ b/src/ai/trolly/trolly.cpp @@ -691,7 +691,7 @@ static void AiNew_State_FindStation(Player *p) // See how much it is going to cost us... r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST); - p->ainew.new_cost += r; + p->ainew.new_cost += r.GetCost(); direction = AI_PATHFINDER_NO_DIRECTION; } else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) { @@ -850,7 +850,7 @@ static void AiNew_State_FindDepot(Player *p) r = AiNew_Build_Depot(p, t, ReverseDiagDir(j), 0); if (CmdFailed(r)) continue; // Found a spot! - p->ainew.new_cost += r; + p->ainew.new_cost += r.GetCost(); p->ainew.depot_tile = t; p->ainew.depot_direction = ReverseDiagDir(j); // Reverse direction p->ainew.state = AI_STATE_VERIFY_ROUTE; @@ -935,7 +935,7 @@ static void AiNew_State_VerifyRoute(Player *p) do { p->ainew.path_info.position++; - p->ainew.new_cost += AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_QUERY_COST); + p->ainew.new_cost += AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_QUERY_COST).GetCost(); } while (p->ainew.path_info.position != -2); // Now we know the price of build station + path. Now check how many vehicles @@ -951,7 +951,7 @@ static void AiNew_State_VerifyRoute(Player *p) // Check how much it it going to cost us.. for (i=0;i<res;i++) { - p->ainew.new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST); + p->ainew.new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST).GetCost(); } // Now we know how much the route is going to cost us @@ -985,7 +985,7 @@ static void AiNew_State_VerifyRoute(Player *p) // Build the stations static void AiNew_State_BuildStation(Player *p) { - CommandCost res = 0; + CommandCost res; assert(p->ainew.state == AI_STATE_BUILD_STATION); if (p->ainew.temp == 0) { if (!IsTileType(p->ainew.from_tile, MP_STATION)) @@ -1103,7 +1103,7 @@ static void AiNew_State_BuildPath(Player *p) // Builds the depot static void AiNew_State_BuildDepot(Player *p) { - CommandCost res = 0; + CommandCost res; assert(p->ainew.state == AI_STATE_BUILD_DEPOT); if (IsTileType(p->ainew.depot_tile, MP_STREET) && GetRoadTileType(p->ainew.depot_tile) == ROAD_TILE_DEPOT) { @@ -1278,7 +1278,7 @@ static void AiNew_CheckVehicle(Player *p, Vehicle *v) if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return; { - CommandCost ret = 0; + CommandCost ret; if (v->type == VEH_ROAD) ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT); // This means we can not find a depot :s |