From 773a7c9cb64856bd2bca11f9c38eea049b353e4e Mon Sep 17 00:00:00 2001 From: rubidium Date: Mon, 18 Jun 2007 19:53:50 +0000 Subject: (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified. --- src/ai/trolly/build.cpp | 33 +++++++++++++++++---------------- src/ai/trolly/trolly.cpp | 14 +++++++------- 2 files changed, 24 insertions(+), 23 deletions(-) (limited to 'src/ai/trolly') 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;iainew.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 -- cgit v1.2.3-70-g09d2