847 |
835 |
848 return rcmd; |
836 return rcmd; |
849 } |
837 } |
850 |
838 |
851 /** |
839 /** |
852 * Check there are enough neighbor house tiles next to the current tile |
840 * Grows the town with an extra house. |
|
841 * Check if there are enough neighbor house tiles |
|
842 * next to the current tile. If there are enough |
|
843 * add another house. |
853 * |
844 * |
854 * @param tile current tile |
845 * @param t The current town |
855 * @return true if there are more than 2 house tiles next |
846 * @param tile The target tile for the extra house |
856 * to the current one |
847 * @return true if an extra house has been added |
857 */ |
848 */ |
858 static bool AreNeighborsHouseTiles(TileIndex tile) |
849 static bool GrowTownWithExtraHouse(Town *t, TileIndex tile) |
859 { |
850 { |
860 uint counter = 0; ///< counts the house neighbor tiles |
|
861 |
|
862 /* We can't look further than that. */ |
851 /* We can't look further than that. */ |
863 if (TileX(tile) < 1 || TileY(tile) < 1) { |
852 if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false; |
864 return false; |
853 |
865 } |
854 uint counter = 0; // counts the house neighbor tiles |
866 |
855 |
867 /* Check the tiles E,N,W and S of the current tile. */ |
856 /* Check the tiles E,N,W and S of the current tile for houses */ |
868 for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) { |
857 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) { |
869 if (IsTileType(AddDiagDirToTileIndex(tile, i), MP_HOUSE)) { |
858 |
870 counter++; |
859 if (IsTileType(TileAddByDiagDir(tile, dir), MP_HOUSE)) counter++; |
871 } |
860 |
872 |
861 /* If there are enough neighbors stop here */ |
873 /* If there are enougth neighbor's stop it here */ |
|
874 if (counter >= 3) { |
862 if (counter >= 3) { |
|
863 if (BuildTownHouse(t, tile)) { |
|
864 _grow_town_result = -1; |
|
865 return true; |
|
866 } |
|
867 return false; |
|
868 } |
|
869 } |
|
870 return false; |
|
871 } |
|
872 |
|
873 /** |
|
874 * Grows the town with a road piece. |
|
875 * |
|
876 * @param t The current town |
|
877 * @param tile The current tile |
|
878 * @param rcmd The RoadBits we want to build on the tile |
|
879 * @return true if the RoadBits have been added else false |
|
880 */ |
|
881 static bool GrowTownWithRoad(const Town *t, TileIndex tile, RoadBits rcmd) |
|
882 { |
|
883 if (CmdSucceeded(DoCommand(tile, rcmd, t->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { |
|
884 _grow_town_result = -1; |
|
885 return true; |
|
886 } |
|
887 return false; |
|
888 } |
|
889 |
|
890 /** |
|
891 * Grows the town with a bridge. |
|
892 * At first we check if a bridge is reasonable. |
|
893 * If so we check if we are able to build it. |
|
894 * |
|
895 * @param t The current town |
|
896 * @param tile The current tile |
|
897 * @param rcmd The RoadBits which are possible on this tile |
|
898 * @return true if a bridge has been build else false |
|
899 */ |
|
900 static bool GrowTownWithBridge(const Town *t, TileIndex tile, RoadBits rcmd) |
|
901 { |
|
902 DiagDirection bridge_dir; // The direction of a bridge we maybe want to build |
|
903 |
|
904 /* Determine direction of slope, |
|
905 * and build a road if not a special slope. */ |
|
906 switch (GetTileSlope(tile, NULL)) { |
|
907 case SLOPE_SW: bridge_dir = DIAGDIR_NE; break; |
|
908 case SLOPE_SE: bridge_dir = DIAGDIR_NW; break; |
|
909 case SLOPE_NW: bridge_dir = DIAGDIR_SE; break; |
|
910 case SLOPE_NE: bridge_dir = DIAGDIR_SW; break; |
|
911 |
|
912 default: return false; |
|
913 } |
|
914 |
|
915 /* Check if the bridge will be compatible to the RoadBits */ |
|
916 if (!(rcmd & DiagDirToRoadBits(ReverseDiagDir(bridge_dir)))) return false; |
|
917 |
|
918 /* We are in the right direction */ |
|
919 uint32 bridge_length = 0; // This value stores the length of the possible bridge |
|
920 TileIndex bridge_tile = tile; // Used to store the other waterside |
|
921 |
|
922 do { |
|
923 if (bridge_length++ >= 11) { |
|
924 /* Max 11 tile long bridges */ |
|
925 return false; |
|
926 } |
|
927 bridge_tile = TILE_MASK(bridge_tile + TileOffsByDiagDir(bridge_dir)); |
|
928 } while (IsClearWaterTile(bridge_tile)); |
|
929 |
|
930 /* no water tiles in between? */ |
|
931 if (bridge_length == 1) return false; |
|
932 |
|
933 for (uint8 times = 0; times <= 22; times++) { |
|
934 byte bridge_type = RandomRange(MAX_BRIDGES - 1); |
|
935 |
|
936 /* Can we actually build the bridge? */ |
|
937 if (CmdSucceeded(DoCommand(tile, bridge_tile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) { |
|
938 DoCommand(tile, bridge_tile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE); |
|
939 _grow_town_result--; |
875 return true; |
940 return true; |
876 } |
941 } |
877 } |
942 } |
|
943 /* Quit if it selecting an appropiate bridge type fails a large number of times. */ |
878 return false; |
944 return false; |
879 } |
945 } |
880 |
946 |
881 /** |
947 /** |
882 * Grows the given town. |
948 * Grows the given town. |
883 * There are at the moment 3 possible way's for |
949 * There are at the moment 3 possible way's for |
884 * the town expansion: |
950 * the town expansion: |
885 * @li Generate a random tile and check if there is a road allowed |
951 * @li Generate a random tile and check if there is a road allowed |
886 * @li TL_ORIGINAL |
952 * @li TL_ORIGINAL |
887 * @li TL_BETTER_ROADS |
953 * @li TL_BETTER_ROADS |
888 * @li Check if the town geometry allows a road and which one |
954 * @li Check if the town geometry allows a road and which one |
889 * @li TL_2X2_GRID |
955 * @li TL_2X2_GRID |
890 * @li TL_3X3_GRID |
956 * @li TL_3X3_GRID |
891 * @li Forbid roads, only build houses |
957 * @li Forbid roads, only build houses |
892 * @li TL_NO_ROADS |
958 * @li TL_NO_ROADS |
893 * |
959 * |
894 * @param tile_ptr The current tile |
960 * @param tile_ptr The current tile |
895 * @param cur_rb The current tiles RoadBits |
961 * @param cur_rb The current tiles RoadBits |
896 * @param target_dir The target road dir |
962 * @param target_dir The target road dir |
897 * @param t1 The current town |
963 * @param t1 The current town |
898 */ |
964 */ |
899 static void GrowTownInTile(TileIndex* tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town* t1) |
965 static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1) |
900 { |
966 { |
901 RoadBits rcmd = ROAD_NONE; ///< RoadBits for the road construction command |
967 RoadBits rcmd = ROAD_NONE; // RoadBits for the road construction command |
902 TileIndex tmptile; ///< Dummy tile for various things |
968 TileIndex tile = *tile_ptr; // The main tile on which we base our growth |
903 TileIndex tile = *tile_ptr; ///< The main tile on which we base our growth |
|
904 |
969 |
905 TILE_ASSERT(tile); |
970 TILE_ASSERT(tile); |
906 |
971 |
907 if (cur_rb == ROAD_NONE) { |
972 if (cur_rb == ROAD_NONE) { |
908 /* Tile has no road. First reset the status counter |
973 /* Tile has no road. First reset the status counter |
934 if (CHANCE16(1, 4)) { |
999 if (CHANCE16(1, 4)) { |
935 /* Randomize a new target dir */ |
1000 /* Randomize a new target dir */ |
936 do target_dir = RandomDiagDir(); while (target_dir == source_dir); |
1001 do target_dir = RandomDiagDir(); while (target_dir == source_dir); |
937 } |
1002 } |
938 |
1003 |
939 if (!IsRoadAllowedHere(AddDiagDirToTileIndex(tile, target_dir), target_dir)) { |
1004 if (!IsRoadAllowedHere(TileAddByDiagDir(tile, target_dir), target_dir)) { |
940 /* A road is not allowed to continue the randomized road, |
1005 /* A road is not allowed to continue the randomized road, |
941 * return if the road we're trying to build is curved. */ |
1006 * return if the road we're trying to build is curved. */ |
942 if (target_dir != ReverseDiagDir(source_dir)) return; |
1007 if (target_dir != ReverseDiagDir(source_dir)) return; |
943 |
1008 |
944 /* Return if neither side of the new road is a house */ |
1009 /* Return if neither side of the new road is a house */ |
945 if (!IsTileType(AddDiagDirToTileIndex(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) && |
1010 if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) && |
946 !IsTileType(AddDiagDirToTileIndex(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) { |
1011 !IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) { |
947 return; |
1012 return; |
948 } |
1013 } |
949 |
1014 |
950 /* That means that the road is only allowed if there is a house |
1015 /* That means that the road is only allowed if there is a house |
951 * at any side of the new road. */ |
1016 * at any side of the new road. */ |
995 * Randomize a direction and if it has a road, bail out. */ |
1059 * Randomize a direction and if it has a road, bail out. */ |
996 target_dir = RandomDiagDir(); |
1060 target_dir = RandomDiagDir(); |
997 if (cur_rb & DiagDirToRoadBits(target_dir)) return; |
1061 if (cur_rb & DiagDirToRoadBits(target_dir)) return; |
998 |
1062 |
999 /* This is the tile we will reach if we extend to this direction. */ |
1063 /* This is the tile we will reach if we extend to this direction. */ |
1000 tmptile = AddDiagDirToTileIndex(tile, target_dir); |
1064 TileIndex house_tile = TileAddByDiagDir(tile, target_dir); // position of a possible house |
1001 |
1065 |
1002 /* Don't walk into water. */ |
1066 /* Don't walk into water. */ |
1003 if (IsClearWaterTile(tmptile)) return; |
1067 if (IsClearWaterTile(house_tile)) return; |
1004 |
1068 |
1005 switch (_patches.town_layout) { |
1069 switch (_patches.town_layout) { |
1006 default: NOT_REACHED(); |
1070 default: NOT_REACHED(); |
1007 |
1071 |
1008 case TL_NO_ROADS: |
1072 case TL_NO_ROADS: |
1009 allow_house = true; |
1073 allow_house = true; |
1010 break; |
1074 break; |
1011 |
1075 |
1012 case TL_3X3_GRID: /* Use 2x2 grid afterwards! */ |
1076 case TL_3X3_GRID: /* Use 2x2 grid afterwards! */ |
1013 /* Fill gap if house has enougth neighbors */ |
1077 GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir)); |
1014 tmptile2 = AddDiagDirToTileIndex(tmptile, target_dir); |
1078 /* FALL THROUGH */ |
1015 if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { |
|
1016 _grow_town_result = -1; |
|
1017 } |
|
1018 |
1079 |
1019 case TL_2X2_GRID: |
1080 case TL_2X2_GRID: |
1020 rcmd = GetTownRoadGridElement(t1, tmptile, target_dir); |
1081 rcmd = GetTownRoadGridElement(t1, house_tile, target_dir); |
1021 allow_house = (rcmd == ROAD_NONE); |
1082 allow_house = (rcmd == ROAD_NONE); |
1022 break; |
1083 break; |
1023 |
1084 |
1024 case TL_BETTER_ROADS: /* Use original afterwards! */ |
1085 case TL_BETTER_ROADS: /* Use original afterwards! */ |
1025 /* Fill gap if house has enougth neighbors */ |
1086 GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir)); |
1026 tmptile2 = AddDiagDirToTileIndex(tmptile, target_dir); |
1087 /* FALL THROUGH */ |
1027 if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { |
|
1028 _grow_town_result = -1; |
|
1029 } |
|
1030 |
1088 |
1031 case TL_ORIGINAL: |
1089 case TL_ORIGINAL: |
1032 /* Allow a house at the edge. 60% chance or |
1090 /* Allow a house at the edge. 60% chance or |
1033 * always ok if no road allowed. */ |
1091 * always ok if no road allowed. */ |
1034 rcmd = DiagDirToRoadBits(target_dir); |
1092 rcmd = DiagDirToRoadBits(target_dir); |
1035 allow_house = (!IsRoadAllowedHere(tmptile, target_dir) || CHANCE16(6, 10)); |
1093 allow_house = (!IsRoadAllowedHere(house_tile, target_dir) || CHANCE16(6, 10)); |
1036 break; |
1094 break; |
1037 } |
1095 } |
1038 |
1096 |
1039 if (allow_house) { |
1097 if (allow_house) { |
1040 /* Build a house, but not if there already is a house there. */ |
1098 /* Build a house, but not if there already is a house there. */ |
1041 if (!IsTileType(tmptile, MP_HOUSE)) { |
1099 if (!IsTileType(house_tile, MP_HOUSE)) { |
1042 /* Level the land if possible */ |
1100 /* Level the land if possible */ |
1043 LevelTownLand(tmptile); |
1101 LevelTownLand(house_tile); |
1044 |
1102 |
1045 /* And build a house. |
1103 /* And build a house. |
1046 * Set result to -1 if we managed to build it. */ |
1104 * Set result to -1 if we managed to build it. */ |
1047 if (BuildTownHouse(t1, tmptile)) { |
1105 if (BuildTownHouse(t1, house_tile)) { |
1048 _grow_town_result = -1; |
1106 _grow_town_result = -1; |
1049 } |
1107 } |
1050 } |
1108 } |
1051 return; |
1109 return; |
1052 } |
1110 } |
1055 } |
1113 } |
1056 |
1114 |
1057 /* Return if a water tile */ |
1115 /* Return if a water tile */ |
1058 if (IsClearWaterTile(tile)) return; |
1116 if (IsClearWaterTile(tile)) return; |
1059 |
1117 |
1060 DiagDirection bridge_dir; ///< The direction of a bridge we maybe want to build |
1118 /* Make the roads look nicer */ |
1061 /* Determine direction of slope, |
1119 rcmd = CleanUpRoadBits(tile, rcmd); |
1062 * and build a road if not a special slope. */ |
1120 if (rcmd == ROAD_NONE) return; |
1063 switch (GetTileSlope(tile, NULL)) { |
1121 |
1064 case SLOPE_SW: bridge_dir = DIAGDIR_NE; break; |
1122 if (GrowTownWithBridge(t1, tile, rcmd)) return; |
1065 case SLOPE_SE: bridge_dir = DIAGDIR_NW; break; |
1123 |
1066 case SLOPE_NW: bridge_dir = DIAGDIR_SE; break; |
1124 GrowTownWithRoad(t1, tile, rcmd); |
1067 case SLOPE_NE: bridge_dir = DIAGDIR_SW; break; |
|
1068 |
|
1069 default: |
|
1070 build_road_and_exit: |
|
1071 if (CmdSucceeded(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { |
|
1072 _grow_town_result = -1; |
|
1073 } |
|
1074 return; |
|
1075 } |
|
1076 |
|
1077 /* Check if the bridge is in the right direction */ |
|
1078 if (!(rcmd & DiagDirToRoadBits(bridge_dir))) goto build_road_and_exit; |
|
1079 |
|
1080 /* We are in the right direction */ |
|
1081 uint32 bridge_length = 0; ///< This value stores the length of the possible bridge |
|
1082 tmptile = tile; ///< Now we use this dummy to store the other waterside |
|
1083 do { |
|
1084 if (bridge_length++ >= 11) { |
|
1085 /* Max 11 tile long bridges */ |
|
1086 goto build_road_and_exit; |
|
1087 } |
|
1088 tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(bridge_dir)); |
|
1089 } while (IsClearWaterTile(tmptile)); |
|
1090 |
|
1091 /* no water tiles in between? */ |
|
1092 if (bridge_length == 1) goto build_road_and_exit; |
|
1093 |
|
1094 for (uint times = 0; times <= 22; times++) { |
|
1095 byte bridge_type = RandomRange(MAX_BRIDGES - 1); |
|
1096 |
|
1097 /* Can we actually build the bridge? */ |
|
1098 if (CmdSucceeded(DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) { |
|
1099 DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE); |
|
1100 _grow_town_result = -1; |
|
1101 return; |
|
1102 } |
|
1103 } |
|
1104 /* Quit if it selecting an appropiate bridge type fails a large number of times. */ |
|
1105 } |
1125 } |
1106 |
1126 |
1107 /** Returns "growth" if a house was built, or no if the build failed. |
1127 /** Returns "growth" if a house was built, or no if the build failed. |
1108 * @param t town to inquiry |
1128 * @param t town to inquiry |
1109 * @param tile to inquiry |
1129 * @param tile to inquiry |
2108 if (_patches.town_growth_rate == 0 && t->fund_buildings_months == 0) return; |
2128 if (_patches.town_growth_rate == 0 && t->fund_buildings_months == 0) return; |
2109 |
2129 |
2110 /** Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the |
2130 /** Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the |
2111 * number of times towns are processed before a new building is built. */ |
2131 * number of times towns are processed before a new building is built. */ |
2112 static const uint16 _grow_count_values[2][6] = { |
2132 static const uint16 _grow_count_values[2][6] = { |
2113 { 120, 120, 120, 100, 80, 60 }, ///< Fund new buildings has been activated |
2133 { 120, 120, 120, 100, 80, 60 }, // Fund new buildings has been activated |
2114 { 320, 420, 300, 220, 160, 100 } ///< Normal values |
2134 { 320, 420, 300, 220, 160, 100 } // Normal values |
2115 }; |
2135 }; |
2116 |
2136 |
2117 if (t->fund_buildings_months != 0) { |
2137 if (t->fund_buildings_months != 0) { |
2118 m = _grow_count_values[0][min(n, 5)]; |
2138 m = _grow_count_values[0][min(n, 5)]; |
2119 t->fund_buildings_months--; |
2139 t->fund_buildings_months--; |