src/town_cmd.cpp
changeset 8137 67a9579abd74
parent 8099 fab936821b10
child 8138 7dc73953c47e
equal deleted inserted replaced
8136:d1a4486be2fd 8137:67a9579abd74
   131 {
   131 {
   132 	return (DiagDirection)(3 & Random());
   132 	return (DiagDirection)(3 & Random());
   133 }
   133 }
   134 
   134 
   135 /**
   135 /**
   136  * Move a TileIndex into a diagonal direction.
       
   137  *
       
   138  * @param tile The current tile
       
   139  * @param dir The direction in which we want to step
       
   140  * @return the moved tile
       
   141  */
       
   142 static inline TileIndex AddDiagDirToTileIndex(TileIndex tile, DiagDirection dir)
       
   143 {
       
   144 	return TILE_ADD(tile, TileOffsByDiagDir(dir));
       
   145 }
       
   146 
       
   147 /**
       
   148  * House Tile drawing handler.
   136  * House Tile drawing handler.
   149  * Part of the tile loop process
   137  * Part of the tile loop process
   150  * @param ti TileInfo of the tile to draw
   138  * @param ti TileInfo of the tile to draw
   151  */
   139  */
   152 static void DrawTile_Town(TileInfo *ti)
   140 static void DrawTile_Town(TileInfo *ti)
   659  * @return true if one of the neighboring tiles at the
   647  * @return true if one of the neighboring tiles at the
   660  *  given distance is a road tile else false
   648  *  given distance is a road tile else false
   661  */
   649  */
   662 static bool IsNeighborRoadTile(TileIndex tile, DiagDirection dir, uint dist_multi)
   650 static bool IsNeighborRoadTile(TileIndex tile, DiagDirection dir, uint dist_multi)
   663 {
   651 {
   664 	static TileIndexDiff tid_lt[3]; ///< lookup table for the used diff values
   652 	static TileIndexDiff tid_lt[3]; // lookup table for the used diff values
   665 	tid_lt[0] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT));
   653 	tid_lt[0] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT));
   666 	tid_lt[1] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT));
   654 	tid_lt[1] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT));
   667 	tid_lt[2] = TileOffsByDiagDir(ReverseDiagDir(dir));
   655 	tid_lt[2] = TileOffsByDiagDir(ReverseDiagDir(dir));
   668 
   656 
   669 	/* We add 1 to the distance because we want to get 1 for
   657 	/* We add 1 to the distance because we want to get 1 for
   679 		 * for each with 2nd bit set (2,3,6,7,..) add the reversed TileIndexDiff
   667 		 * for each with 2nd bit set (2,3,6,7,..) add the reversed TileIndexDiff
   680 		 */
   668 		 */
   681 		cur += tid_lt[(pos & 1) ? 0 : 1];
   669 		cur += tid_lt[(pos & 1) ? 0 : 1];
   682 		if (pos & 2) cur += tid_lt[2];
   670 		if (pos & 2) cur += tid_lt[2];
   683 
   671 
   684 		cur = (uint)(pos / 4) * cur; ///< Multiply for the fitting distance
   672 		cur = (uint)(pos / 4) * cur; // Multiply for the fitting distance
   685 		if (GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true;
   673 		if (GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true;
   686 	}
   674 	}
   687 	return false;
   675 	return false;
   688 }
   676 }
   689 
   677 
   795  *  the selected town layout
   783  *  the selected town layout
   796  */
   784  */
   797 static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile, DiagDirection dir)
   785 static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile, DiagDirection dir)
   798 {
   786 {
   799 	/* align the grid to the downtown */
   787 	/* align the grid to the downtown */
   800 	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); ///< Vector from downtown to the tile
   788 	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); // Vector from downtown to the tile
   801 	RoadBits rcmd = ROAD_NONE;
   789 	RoadBits rcmd = ROAD_NONE;
   802 
   790 
   803 	switch (_patches.town_layout) {
   791 	switch (_patches.town_layout) {
   804 		default: NOT_REACHED();
   792 		default: NOT_REACHED();
   805 
   793 
   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. */
   953 
  1018 
   954 				rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir);
  1019 				rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir);
   955 				break;
  1020 				break;
   956 		}
  1021 		}
   957 
  1022 
   958 	} else if (target_dir < (DiagDirection)5 && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) {
  1023 	} else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) {
   959 		/* Continue building on a partial road.
  1024 		/* Continue building on a partial road.
   960 		 * Should be allways OK, so we only generate
  1025 		 * Should be allways OK, so we only generate
   961 		 * the fitting RoadBits */
  1026 		 * the fitting RoadBits */
   962 		_grow_town_result = 0;
  1027 		_grow_town_result = 0;
   963 
  1028 
   976 			case TL_ORIGINAL:
  1041 			case TL_ORIGINAL:
   977 				rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir));
  1042 				rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir));
   978 				break;
  1043 				break;
   979 		}
  1044 		}
   980 	} else {
  1045 	} else {
   981 		bool allow_house = false; ///< Value which decides if we want to construct a house
  1046 		bool allow_house = false; // Value which decides if we want to construct a house
   982 		TileIndex tmptile2;       ///< Yet another dummy tile
       
   983 
  1047 
   984 		/* Reached a tunnel/bridge? Then continue at the other side of it. */
  1048 		/* Reached a tunnel/bridge? Then continue at the other side of it. */
   985 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
  1049 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   986 			if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) {
  1050 			if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) {
   987 				*tile_ptr = GetOtherTunnelEnd(tile);
  1051 				*tile_ptr = GetOtherTunnelEnd(tile);
   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
  1112 static int GrowTownAtRoad(Town *t, TileIndex tile)
  1132 static int GrowTownAtRoad(Town *t, TileIndex tile)
  1113 {
  1133 {
  1114 	/* Special case.
  1134 	/* Special case.
  1115 	 * @see GrowTownInTile Check the else if
  1135 	 * @see GrowTownInTile Check the else if
  1116 	 */
  1136 	 */
  1117 	DiagDirection target_dir = (DiagDirection)5; ///< The direction in which we want to extend the town
  1137 	DiagDirection target_dir = DIAGDIR_END; // The direction in which we want to extend the town
  1118 
  1138 
  1119 	TILE_ASSERT(tile);
  1139 	TILE_ASSERT(tile);
  1120 
  1140 
  1121 	/* Number of times to search.
  1141 	/* Number of times to search.
  1122 	 * Better roads, 2X2 and 3X3 grid grow quite fast so we give
  1142 	 * Better roads, 2X2 and 3X3 grid grow quite fast so we give
  1135 			_grow_town_result = 10 + t->num_houses * 4 / 9;
  1155 			_grow_town_result = 10 + t->num_houses * 4 / 9;
  1136 			break;
  1156 			break;
  1137 	}
  1157 	}
  1138 
  1158 
  1139 	do {
  1159 	do {
  1140 		RoadBits cur_rb = GetTownRoadBits(tile); ///< The RoadBits of the current tile
  1160 		RoadBits cur_rb = GetTownRoadBits(tile); // The RoadBits of the current tile
  1141 
  1161 
  1142 		/* Try to grow the town from this point */
  1162 		/* Try to grow the town from this point */
  1143 		GrowTownInTile(&tile, cur_rb, target_dir, t);
  1163 		GrowTownInTile(&tile, cur_rb, target_dir, t);
  1144 
  1164 
  1145 		/* Exclude the source position from the bitmask
  1165 		/* Exclude the source position from the bitmask
  1149 			return _grow_town_result;
  1169 			return _grow_town_result;
  1150 
  1170 
  1151 		/* Select a random bit from the blockmask, walk a step
  1171 		/* Select a random bit from the blockmask, walk a step
  1152 		 * and continue the search from there. */
  1172 		 * and continue the search from there. */
  1153 		do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir)));
  1173 		do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir)));
  1154 		tile = AddDiagDirToTileIndex(tile, target_dir);
  1174 		tile = TileAddByDiagDir(tile, target_dir);
  1155 
  1175 
  1156 		if (IsTileType(tile, MP_ROAD)) {
  1176 		if (IsTileType(tile, MP_ROAD)) {
  1157 			/* Don't allow building over roads of other cities */
  1177 			/* Don't allow building over roads of other cities */
  1158 			if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) {
  1178 			if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) {
  1159 				_grow_town_result = -1;
  1179 				_grow_town_result = -1;
  1218 
  1238 
  1219 	/* Current player is a town */
  1239 	/* Current player is a town */
  1220 	PlayerID old_player = _current_player;
  1240 	PlayerID old_player = _current_player;
  1221 	_current_player = OWNER_TOWN;
  1241 	_current_player = OWNER_TOWN;
  1222 
  1242 
  1223 	TileIndex tile = t->xy; ///< The tile we are working with ATM
  1243 	TileIndex tile = t->xy; // The tile we are working with ATM
  1224 
  1244 
  1225 	/* Find a road that we can base the construction on. */
  1245 	/* Find a road that we can base the construction on. */
  1226 	for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
  1246 	for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
  1227 		if (GetTownRoadBits(tile) != ROAD_NONE) {
  1247 		if (GetTownRoadBits(tile) != ROAD_NONE) {
  1228 			int r = GrowTownAtRoad(t, tile);
  1248 			int r = GrowTownAtRoad(t, tile);
  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--;
  2250 }
  2270 }
  2251 
  2271 
  2252 /* penalty for removing town-owned stuff */
  2272 /* penalty for removing town-owned stuff */
  2253 static const int _default_rating_settings [3][3] = {
  2273 static const int _default_rating_settings [3][3] = {
  2254 	/* ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE */
  2274 	/* ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE */
  2255 	{  0, 128, 384}, ///< Permissive
  2275 	{  0, 128, 384}, // Permissive
  2256 	{ 48, 192, 480}, ///< Neutral
  2276 	{ 48, 192, 480}, // Neutral
  2257 	{ 96, 384, 768}, ///< Hostile
  2277 	{ 96, 384, 768}, // Hostile
  2258 };
  2278 };
  2259 
  2279 
  2260 bool CheckforTownRating(uint32 flags, Town *t, byte type)
  2280 bool CheckforTownRating(uint32 flags, Town *t, byte type)
  2261 {
  2281 {
  2262 	int modemod;
  2282 	int modemod;