From 0a72639c2d7254c37cd6f8ab0a9fa0f4d63fd2ea Mon Sep 17 00:00:00 2001 From: tron Date: Sat, 10 Jun 2006 08:37:41 +0000 Subject: (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc. --- ai/ai.c | 30 ++++++------- ai/default/default.c | 112 ++++++++++++++++++++----------------------------- ai/trolly/build.c | 22 ++++++---- ai/trolly/pathfinder.c | 25 +++++++---- ai/trolly/trolly.c | 81 ++++++++++++++++++----------------- 5 files changed, 134 insertions(+), 136 deletions(-) (limited to 'ai') diff --git a/ai/ai.c b/ai/ai.c index 907154ed3..1991ebefa 100644 --- a/ai/ai.c +++ b/ai/ai.c @@ -97,7 +97,7 @@ int32 AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint pr /* First, do a test-run to see if we can do this */ res = DoCommand(tile, p1, p2, flags & ~DC_EXEC, procc); /* The command failed, or you didn't want to execute, or you are quering, return */ - if ((CmdFailed(res)) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) { + if (CmdFailed(res) || !(flags & DC_EXEC) || (flags & DC_QUERY_COST)) { return res; } @@ -105,25 +105,28 @@ int32 AI_DoCommandCc(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint pr _cmd_text = tmp_cmdtext; /* If we did a DC_EXEC, and the command did not return an error, execute it - over the network */ - if (flags & DC_AUTO) procc |= CMD_AUTO; - if (flags & DC_NO_WATER) procc |= CMD_NO_WATER; + * over the network */ + if (flags & DC_AUTO) procc |= CMD_AUTO; + if (flags & DC_NO_WATER) procc |= CMD_NO_WATER; /* NetworkSend_Command needs _local_player to be set correctly, so - adjust it, and put it back right after the function */ + * adjust it, and put it back right after the function */ old_lp = _local_player; _local_player = _current_player; #ifdef ENABLE_NETWORK /* Send the command */ - if (_networking) + if (_networking) { /* Network is easy, send it to his handler */ NetworkSend_Command(tile, p1, p2, procc, callback); - else + } else { +#else + { #endif /* If we execute BuildCommands directly in SP, we have a big problem with events * so we need to delay is for 1 tick */ AI_PutCommandInQueue(_current_player, tile, p1, p2, procc, callback); + } /* Set _local_player back */ _local_player = old_lp; @@ -173,16 +176,14 @@ void AI_RunGameLoop(void) * them.. this avoids that, while loading a network game in singleplayer, does make * the AIs to continue ;)) */ - if (_networking && !_network_server && !_ai.network_client) - return; + if (_networking && !_network_server && !_ai.network_client) return; /* New tick */ _ai.tick++; /* Make sure the AI follows the difficulty rule.. */ assert(_opt.diff.competitor_speed <= 4); - if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0) - return; + if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0) return; /* Check for AI-client (so joining a network with an AI) */ if (_ai.network_client && _ai_player[_ai.network_playas].active) { @@ -191,7 +192,7 @@ void AI_RunGameLoop(void) AI_RunTick(_ai.network_playas); } else if (!_networking || _network_server) { /* Check if we want to run AIs (server or SP only) */ - Player *p; + const Player* p; FOR_ALL_PLAYERS(p) { if (p->is_active && p->is_ai) { @@ -224,8 +225,9 @@ void AI_StartNewAI(PlayerID player) */ void AI_PlayerDied(PlayerID player) { - if (_ai.network_client && _ai.network_playas == player) + if (_ai.network_client && _ai.network_playas == player) { _ai.network_playas = OWNER_SPECTATOR; + } /* Called if this AI died */ _ai_player[player].active = false; @@ -254,7 +256,7 @@ void AI_Initialize(void) */ void AI_Uninitialize(void) { - Player* p; + const Player* p; FOR_ALL_PLAYERS(p) { if (p->is_active && p->is_ai) AI_PlayerDied(p->index); diff --git a/ai/default/default.c b/ai/default/default.c index dd682581c..213ade79e 100644 --- a/ai/default/default.c +++ b/ai/default/default.c @@ -348,11 +348,11 @@ static void AiHandleReplaceRoadVeh(Player *p) tile = v->tile; if (!CmdFailed(DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) && - !CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH)) ) { + !CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH))) { VehicleID veh = _new_vehicle_id; + AiRestoreVehicleOrders(GetVehicle(veh), orderbak); DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH); - DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); } } @@ -1608,8 +1608,7 @@ clear_town_stuff:; } } else if (p->mode == 3) { //Clear stuff and then build single rail. - if (GetTileSlope(c, NULL) != SLOPE_FLAT) - return CMD_ERROR; + 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; @@ -1927,7 +1926,6 @@ static bool AiCheckRailPathBetter(AiRailFinder *arf, const byte *p) static inline void AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, const byte *p) { - TileIndex tile_new; Slope tileh; uint z; bool flag; @@ -1936,7 +1934,8 @@ static inline void AiCheckBuildRailBridgeHere(AiRailFinder *arf, TileIndex tile, tileh = GetTileSlope(tile, &z); if (tileh == _dir_table_1[dir2] || (tileh == SLOPE_FLAT && z != 0)) { - tile_new = tile; + TileIndex tile_new = tile; + // Allow bridges directly over bottom tiles flag = z == 0; for (;;) { @@ -1977,9 +1976,7 @@ static inline void AiCheckBuildRailTunnelHere(AiRailFinder *arf, TileIndex tile, if (!CmdFailed(cost) && cost <= (arf->player->player_money>>4)) { AiBuildRailRecursive(arf, _build_tunnel_endtile, p[0]&3); - if (arf->depth == 1) { - AiCheckRailPathBetter(arf, p); - } + if (arf->depth == 1) AiCheckRailPathBetter(arf, p); } } } @@ -2009,6 +2006,7 @@ static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, int dir) // Depth too deep? if (arf->depth >= 4) { uint dist = DistanceMaxPlusManhattan(tile, arf->final_tile); + if (dist < arf->cur_best_dist) { // Store the tile that is closest to the final position. arf->cur_best_depth = arf->depth; @@ -2037,9 +2035,7 @@ static void AiBuildRailRecursive(AiRailFinder *arf, TileIndex tile, int dir) } // At the bottom depth? - if (arf->depth == 1) { - AiCheckRailPathBetter(arf, p); - } + if (arf->depth == 1) AiCheckRailPathBetter(arf, p); p += 2; } while (!(p[0]&0x80)); @@ -2093,8 +2089,9 @@ static void AiBuildRailConstruct(Player *p) // Didn't find anything to build? if (arf.best_ptr == NULL) { // Terraform some - for (i=0; i!=5; i++) + for (i = 0; i != 5; i++) { AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0); + } if (++p->ai.state_counter == 21) { p->ai.state_counter = 40; @@ -2112,16 +2109,16 @@ static void AiBuildRailConstruct(Player *p) int i; int32 bridge_len = GetBridgeLength(arf.bridge_end_tile, p->ai.cur_tile_a); - /* Figure out what (rail)bridge type to build - start with best bridge, then go down to worse and worse bridges - unnecessary to check for worse bridge (i=0), since AI will always build that. - AI is so fucked up that fixing this small thing will probably not solve a thing - */ + /* Figure out which (rail)bridge type to build + * start with best bridge, then go down to worse and worse bridges + * unnecessary to check for worst bridge (i=0), since AI will always build + * that. AI is so fucked up that fixing this small thing will probably not + * solve a thing + */ 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 (!CmdFailed(cost) && cost < (p->player_money >> 5)) - break; + if (!CmdFailed(cost) && cost < (p->player_money >> 5)) break; } } @@ -2130,7 +2127,7 @@ static void AiBuildRailConstruct(Player *p) p->ai.cur_tile_a = arf.bridge_end_tile; p->ai.state_counter = 0; - } else if (arf.best_ptr[0]&0x40) { + } else if (arf.best_ptr[0] & 0x40) { // tunnel DoCommand(p->ai.cur_tile_a, p->ai.railtype_to_use, 0, DC_AUTO | DC_EXEC, CMD_BUILD_TUNNEL); p->ai.cur_tile_a = _build_tunnel_endtile; @@ -2348,9 +2345,7 @@ static EngineID AiFindBestWagon(CargoID cargo, RailType railtype) continue; } - if (rvi->cargo_type != cargo) { - continue; - } + if (rvi->cargo_type != cargo) continue; /* max_speed of 0 indicates no speed limit */ speed = rvi->max_speed == 0 ? 0xFFFF : rvi->max_speed; @@ -2374,7 +2369,7 @@ static void AiStateBuildRailVeh(Player *p) CargoID cargo; int32 cost; Vehicle *v; - uint loco_id; + VehicleID loco_id; ptr = _default_rail_track_data[p->ai.src.cur_building_rule]->data; while (ptr->mode != 0) ptr++; @@ -2395,14 +2390,12 @@ static void AiStateBuildRailVeh(Player *p) p->ai.wagon_list[i + 1] = INVALID_VEHICLE; return; } - if (cargo == CT_MAIL) - cargo = CT_PASSENGERS; - if (++i == p->ai.num_wagons * 2 - 1) - break; + if (cargo == CT_MAIL) cargo = CT_PASSENGERS; + if (++i == p->ai.num_wagons * 2 - 1) break; } // Which locomotive to build? - veh = AiChooseTrainToBuild(p->ai.railtype_to_use, p->player_money, (cargo!=CT_PASSENGERS)?1:0, tile); + veh = AiChooseTrainToBuild(p->ai.railtype_to_use, p->player_money, cargo != CT_PASSENGERS ? 1 : 0, tile); if (veh == INVALID_ENGINE) { handle_nocash: // after a while, if AI still doesn't have cash, get out of this block by selling the wagons. @@ -2435,7 +2428,7 @@ handle_nocash: } for (i = 0; p->ai.order_list_blocks[i] != 0xFF; i++) { - AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i]; + const AiBuildRec* aib = &p->ai.src + p->ai.order_list_blocks[i]; bool is_pass = ( p->ai.cargo_type == CT_PASSENGERS || p->ai.cargo_type == CT_MAIL || @@ -2615,11 +2608,8 @@ clear_town_stuff:; // Make sure the blocks are not too close to each other static bool AiCheckBlockDistances(Player *p, TileIndex tile) { - AiBuildRec *aib; - int num; - - num = p->ai.num_build_rec; - aib = &p->ai.src; + const AiBuildRec* aib = &p->ai.src; + uint num = p->ai.num_build_rec; do { if (aib->cur_building_rule != 255) { @@ -2835,7 +2825,6 @@ static bool AiBuildRoadHelper(TileIndex tile, int flags, int type) static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, const byte *p) { - TileIndex tile_new; Slope tileh; uint z; bool flag; @@ -2844,7 +2833,8 @@ static inline void AiCheckBuildRoadBridgeHere(AiRoadFinder *arf, TileIndex tile, tileh = GetTileSlope(tile, &z); if (tileh == _dir_table_1[dir2] || (tileh == SLOPE_FLAT && z != 0)) { - tile_new = tile; + TileIndex tile_new = tile; + // Allow bridges directly over bottom tiles flag = z == 0; for (;;) { @@ -2885,9 +2875,7 @@ static inline void AiCheckBuildRoadTunnelHere(AiRoadFinder *arf, TileIndex tile, if (!CmdFailed(cost) && cost <= (arf->player->player_money>>4)) { AiBuildRoadRecursive(arf, _build_tunnel_endtile, p[0]&3); - if (arf->depth == 1) { - AiCheckRoadPathBetter(arf, p); - } + if (arf->depth == 1) AiCheckRoadPathBetter(arf, p); } } } @@ -3032,7 +3020,6 @@ do_some_terraform: p->ai.cur_tile_a = _build_tunnel_endtile; p->ai.state_counter = 0; } else { - // road if (!AiBuildRoadHelper(tile, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_AI_BUILDING, arf.best_ptr[0])) goto do_some_terraform; @@ -3062,8 +3049,9 @@ static void AiBuildRoad(Player *p) uint i; // Terraform some and then try building again. - for (i = 0; i != 4; i++) + for (i = 0; i != 4; i++) { AiDoTerraformLand(p->ai.cur_tile_a, p->ai.cur_dir_a, 3, 0); + } if (++p->ai.state_counter == 4) { p->ai.state_counter = 0; @@ -3156,7 +3144,7 @@ static void AiStateBuildRoadVehicles(Player *p) { const AiDefaultBlockData *ptr; TileIndex tile; - uint loco_id; + VehicleID loco_id; EngineID veh; uint i; @@ -3227,7 +3215,7 @@ static void AiStateDeleteRoadBlocks(Player *p) static void AiStateAirportStuff(Player *p) { - Station *st; + const Station* st; byte acc_planes; int i; AiBuildRec *aib; @@ -3449,10 +3437,10 @@ static void AiStateBuildAircraftVehicles(Player *p) TileIndex tile; EngineID veh; int i; - uint loco_id; + VehicleID loco_id; ptr = _airport_default_block_data[p->ai.src.cur_building_rule]; - for (;ptr->mode!=0;ptr++) {} + for (; ptr->mode != 0; ptr++) {} tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs)); @@ -3465,7 +3453,7 @@ static void AiStateBuildAircraftVehicles(Player *p) if (CmdFailed(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) return; loco_id = _new_vehicle_id; - for (i=0; p->ai.order_list_blocks[i] != 0xFF; i++) { + for (i = 0; p->ai.order_list_blocks[i] != 0xFF; i++) { AiBuildRec *aib = (&p->ai.src) + p->ai.order_list_blocks[i]; bool is_pass = (p->ai.cargo_type == CT_PASSENGERS || p->ai.cargo_type == CT_MAIL); Order order; @@ -3664,27 +3652,20 @@ pos_3: if (GetRoadTileType(tile) == ROAD_TILE_DEPOT) { DiagDirection dir; + TileIndex t; // Check if there are any stations around. - if (IsTileType(tile + TileDiffXY(-1, 0), MP_STATION) && - IsTileOwner(tile + TileDiffXY(-1, 0), _current_player)) { - return; - } + t = tile + TileDiffXY(-1, 0); + if (IsTileType(t, MP_STATION) && IsTileOwner(t, _current_player)) return; - if (IsTileType(tile + TileDiffXY(1, 0), MP_STATION) && - IsTileOwner(tile + TileDiffXY(1, 0), _current_player)) { - return; - } + t = tile + TileDiffXY(1, 0); + if (IsTileType(t, MP_STATION) && IsTileOwner(t, _current_player)) return; - if (IsTileType(tile + TileDiffXY(0, -1), MP_STATION) && - IsTileOwner(tile + TileDiffXY(0, -1), _current_player)) { - return; - } + t = tile + TileDiffXY(0, -1); + if (IsTileType(t, MP_STATION) && IsTileOwner(t, _current_player)) return; - if (IsTileType(tile + TileDiffXY(0, 1), MP_STATION) && - IsTileOwner(tile + TileDiffXY(0, 1), _current_player)) { - return; - } + t = tile + TileDiffXY(0, 1); + if (IsTileType(t, MP_STATION) && IsTileOwner(t, _current_player)) return; dir = GetRoadDepotDirection(tile); @@ -3881,8 +3862,7 @@ void AiDoGameLoop(Player *p) // or in % _ai_service_interval = _patches.servint_ispercent?80:180; - if (IS_HUMAN_PLAYER(_current_player)) - return; + if (IS_HUMAN_PLAYER(_current_player)) return; AiAdjustLoan(p); AiBuildCompanyHQ(p); diff --git a/ai/trolly/build.c b/ai/trolly/build.c index 1f4324355..64339245e 100644 --- a/ai/trolly/build.c +++ b/ai/trolly/build.c @@ -68,14 +68,15 @@ int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag) if (type2 != 0) break; } } - // There is only one bridge that can be build.. + // There is only one bridge that can be built if (type2 == 0 && type != 0) type2 = type; // Now, simply, build the bridge! - if (p->ainew.tbt == AI_TRAIN) - return AI_DoCommand(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); - - return AI_DoCommand(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); + if (p->ainew.tbt == AI_TRAIN) { + return AI_DoCommand(tile_a, tile_b, (0x00 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); + } else { + return AI_DoCommand(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); + } } @@ -102,7 +103,10 @@ int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte fla // the first pieces and the last piece if (part < 1) part = 1; // When we are done, stop it - if (part >= PathFinderInfo->route_length - 1) { PathFinderInfo->position = -2; return 0; } + if (part >= PathFinderInfo->route_length - 1) { + PathFinderInfo->position = -2; + return 0; + } if (PathFinderInfo->rail_or_road) { @@ -112,7 +116,7 @@ int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte fla PathFinderInfo->position++; // TODO: problems! if (CmdFailed(cost)) { - DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!"); + DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be built!"); return 0; } return cost; @@ -123,7 +127,7 @@ int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte fla PathFinderInfo->position++; // TODO: problems! if (CmdFailed(cost)) { - DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!"); + DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be built!"); return 0; } return cost; @@ -131,7 +135,7 @@ int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte fla // Build normal rail // Keep it doing till we go an other way - if (route_extra[part-1] == 0 && route_extra[part] == 0) { + if (route_extra[part - 1] == 0 && route_extra[part] == 0) { while (route_extra[part] == 0) { // Get the current direction dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]); diff --git a/ai/trolly/pathfinder.c b/ai/trolly/pathfinder.c index 09027454c..772ddda46 100644 --- a/ai/trolly/pathfinder.c +++ b/ai/trolly/pathfinder.c @@ -57,7 +57,8 @@ static bool IsRoad(TileIndex tile) // Check if the current tile is in our end-area static int32 AyStar_AiPathFinder_EndNodeCheck(AyStar *aystar, OpenListNode *current) { - Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target; + const Ai_PathFinderInfo* PathFinderInfo = aystar->user_target; + // It is not allowed to have a station on the end of a bridge or tunnel ;) if (current->path.node.user_data[0] != 0) return AYSTAR_DONE; if (TILES_BETWEEN(current->path.node.tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) @@ -155,9 +156,11 @@ void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo // Now we add all the starting tiles for (x = TileX(PathFinderInfo->start_tile_tl); x <= TileX(PathFinderInfo->start_tile_br); x++) { for (y = TileY(PathFinderInfo->start_tile_tl); y <= TileY(PathFinderInfo->start_tile_br); y++) { - if (!(IsTileType(TileXY(x, y), MP_CLEAR) || IsTileType(TileXY(x, y), MP_TREES))) continue; - if (!TestCanBuildStationHere(TileXY(x, y), TEST_STATION_NO_DIR)) continue; - start_node.node.tile = TileXY(x, y); + TileIndex tile = TileXY(x, y); + + if (!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) continue; + if (!TestCanBuildStationHere(tile, TEST_STATION_NO_DIR)) continue; + start_node.node.tile = tile; aystar->addstart(aystar, &start_node.node, 0); } } @@ -167,8 +170,9 @@ void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo // The h-value, simple calculation static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent) { - Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target; + const Ai_PathFinderInfo* PathFinderInfo = aystar->user_target; int r, r2; + if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION) { // The station is pointing to a direction, add a tile towards that direction, so the H-value is more accurate r = DistanceManhattan(current->tile, PathFinderInfo->end_tile_tl + TileOffsByDir(PathFinderInfo->end_direction)); @@ -447,13 +451,15 @@ static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, // Check if we are going up or down, first for the starting point // In user_data[0] is at the 8th bit the direction if (!HASBIT(BRIDGE_NO_FOUNDATION, parent_tileh)) { - if (GetBridgeFoundation(parent_tileh, (current->user_data[0] >> 8) & 1) < 15) + if (GetBridgeFoundation(parent_tileh, (current->user_data[0] >> 8) & 1) < 15) { res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; + } } // Second for the end point if (!HASBIT(BRIDGE_NO_FOUNDATION, tileh)) { - if (GetBridgeFoundation(tileh, (current->user_data[0] >> 8) & 1) < 15) + if (GetBridgeFoundation(tileh, (current->user_data[0] >> 8) & 1) < 15) { res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; + } } if (parent_tileh == SLOPE_FLAT) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; if (tileh == SLOPE_FLAT) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; @@ -466,10 +472,11 @@ static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, if (parent->path.parent != NULL && AiNew_GetDirection(current->tile, parent->path.node.tile) != AiNew_GetDirection(parent->path.node.tile, parent->path.parent->node.tile)) { // When road exists, we don't like turning, but its free, so don't be to piggy about it - if (IsRoad(parent->path.node.tile)) + if (IsRoad(parent->path.node.tile)) { res += AI_PATHFINDER_DIRECTION_CHANGE_ON_EXISTING_ROAD_PENALTY; - else + } else { res += AI_PATHFINDER_DIRECTION_CHANGE_PENALTY; + } } } else { // For rail we have 1 exeption: diagonal rail.. diff --git a/ai/trolly/trolly.c b/ai/trolly/trolly.c index 9bd581a8e..33ef6020d 100644 --- a/ai/trolly/trolly.c +++ b/ai/trolly/trolly.c @@ -128,16 +128,19 @@ static void AiNew_State_WakeUp(Player *p) } else if (c < 100 && !_patches.ai_disable_veh_roadveh) { // Do we have any spots for road-vehicles left open? if (GetFreeUnitNumber(VEH_Road) <= _patches.max_roadveh) { - if (c < 85) + if (c < 85) { p->ainew.action = AI_ACTION_TRUCK_ROUTE; - else + } else { p->ainew.action = AI_ACTION_BUS_ROUTE; + } } - }/* else if (c < 200 && !_patches.ai_disable_veh_train) { +#if 0 + } else if (c < 200 && !_patches.ai_disable_veh_train) { if (GetFreeUnitNumber(VEH_Train) <= _patches.max_trains) { p->ainew.action = AI_ACTION_TRAIN_ROUTE; } - }*/ +#endif + } p->ainew.counter = 0; } @@ -208,8 +211,8 @@ static void AiNew_State_ActionDone(Player *p) static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte type) { if (type == AI_CITY) { - Town *t = GetTown(ic); - Station *st; + const Town* t = GetTown(ic); + const Station* st; uint count = 0; int j = 0; @@ -270,8 +273,8 @@ static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte type) return true; } if (type == AI_INDUSTRY) { - Industry *i = GetIndustry(ic); - Station *st; + const Industry* i = GetIndustry(ic); + const Station* st; int count = 0; int j = 0; @@ -379,10 +382,11 @@ static void AiNew_State_LocateRoute(Player *p) if (p->ainew.from_ic == -1) { if (p->ainew.temp == -1) { // First, we pick a random spot to search from - if (p->ainew.from_type == AI_CITY) + if (p->ainew.from_type == AI_CITY) { p->ainew.temp = AI_RandomRange(_total_towns); - else + } else { p->ainew.temp = AI_RandomRange(_total_industries); + } } if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_type)) { @@ -414,10 +418,11 @@ static void AiNew_State_LocateRoute(Player *p) // Find a to-city if (p->ainew.temp == -1) { // First, we pick a random spot to search to - if (p->ainew.to_type == AI_CITY) + if (p->ainew.to_type == AI_CITY) { p->ainew.temp = AI_RandomRange(_total_towns); - else + } else { p->ainew.temp = AI_RandomRange(_total_industries); + } } // The same city is not allowed @@ -425,9 +430,10 @@ static void AiNew_State_LocateRoute(Player *p) if (p->ainew.temp != p->ainew.from_ic && AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.to_type)) { // Maybe it is valid.. - // We need to know if they are not to far apart from eachother.. - // We do that by checking how much cargo we have to move and how long the route - // is. + /* We need to know if they are not to far apart from eachother.. + * We do that by checking how much cargo we have to move and how long the + * route is. + */ if (p->ainew.from_type == AI_CITY && p->ainew.tbt == AI_BUS) { const Town* town_from = GetTown(p->ainew.from_ic); @@ -470,7 +476,7 @@ static void AiNew_State_LocateRoute(Player *p) for (i = 0; i < lengthof(ind_temp->accepts_cargo); i++) { if (ind_temp->accepts_cargo[i] == CT_INVALID) break; if (ind_from->produced_cargo[0] == ind_temp->accepts_cargo[i]) { - // Found a compatbiel industry + // Found a compatible industry max_cargo = ind_from->total_production[0] - ind_from->total_transported[0]; found = true; p->ainew.from_deliver = true; @@ -672,7 +678,7 @@ static void AiNew_State_FindStation(Player *p) } } - // If i is still zero, we did not found anything :( + // If i is still zero, we did not find anything if (i == 0) { p->ainew.state = AI_STATE_NOTHING; return; @@ -682,7 +688,7 @@ static void AiNew_State_FindStation(Player *p) best = 0; new_tile = 0; - for (x=0;x best || (found_best[x] == best && DistanceManhattan(tile, new_tile) > DistanceManhattan(tile, found_spot[x]))) { new_tile = found_spot[x]; @@ -753,10 +759,7 @@ static void AiNew_State_FindPath(Player *p) p->ainew.path_info.end_direction = p->ainew.to_direction; } - if (p->ainew.tbt == AI_TRAIN) - p->ainew.path_info.rail_or_road = true; - else - p->ainew.path_info.rail_or_road = false; + p->ainew.path_info.rail_or_road = (p->ainew.tbt == AI_TRAIN); // First, clean the pathfinder with our new begin and endpoints clean_AyStar_AiPathFinder(p->ainew.pathfinder, &p->ainew.path_info); @@ -766,20 +769,21 @@ static void AiNew_State_FindPath(Player *p) // Start the pathfinder r = p->ainew.pathfinder->main(p->ainew.pathfinder); - // If it return: no match, stop it... - if (r == AYSTAR_NO_PATH) { - DEBUG(ai,1)("[AiNew] PathFinder found no route!"); - // Start all over again... - p->ainew.state = AI_STATE_NOTHING; - return; - } - if (r == AYSTAR_FOUND_END_NODE) { - // We found the end-point - p->ainew.temp = -1; - p->ainew.state = AI_STATE_FIND_DEPOT; - return; + switch (r) { + case AYSTAR_NO_PATH: + DEBUG(ai,1)("[AiNew] PathFinder found no route!"); + // Start all over again + p->ainew.state = AI_STATE_NOTHING; + break; + + case AYSTAR_FOUND_END_NODE: // We found the end-point + p->ainew.temp = -1; + p->ainew.state = AI_STATE_FIND_DEPOT; + break; + + // In any other case, we are still busy finding the route + default: break; } - // In any other case, we are still busy finding the route... } @@ -900,10 +904,11 @@ static int AiNew_HowManyVehicles(Player *p) length = p->ainew.path_info.route_length; // Calculating tiles a day a vehicle moves is not easy.. this is how it must be done! tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16; - if (p->ainew.from_deliver) + if (p->ainew.from_deliver) { max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0]; - else + } else { max_cargo = GetIndustry(p->ainew.to_ic)->total_production[0]; + } // This is because moving 60% is more than we can dream of! max_cargo *= 0.6; @@ -1109,7 +1114,7 @@ static void AiNew_State_BuildDepot(Player *p) if (IsTileType(p->ainew.depot_tile, MP_STREET) && GetRoadTileType(p->ainew.depot_tile) == ROAD_TILE_DEPOT) { if (IsTileOwner(p->ainew.depot_tile, _current_player)) { - // The depot is already builded! + // The depot is already built p->ainew.state = AI_STATE_BUILD_VEHICLE; return; } else { -- cgit v1.2.3-54-g00ecf