src/town_cmd.cpp
branchnoai
changeset 9703 d2a6acdbd665
parent 9701 d1ac22c62f64
child 9704 197cb8c6ae17
equal deleted inserted replaced
9702:e782b59f1f6a 9703:d2a6acdbd665
    39 #include "newgrf_callbacks.h"
    39 #include "newgrf_callbacks.h"
    40 #include "newgrf_house.h"
    40 #include "newgrf_house.h"
    41 #include "newgrf_commons.h"
    41 #include "newgrf_commons.h"
    42 #include "newgrf_townname.h"
    42 #include "newgrf_townname.h"
    43 #include "misc/autoptr.hpp"
    43 #include "misc/autoptr.hpp"
       
    44 #include "autoslope.h"
    44 
    45 
    45 /* Initialize the town-pool */
    46 /* Initialize the town-pool */
    46 DEFINE_OLD_POOL_GENERIC(Town, Town)
    47 DEFINE_OLD_POOL_GENERIC(Town, Town)
    47 
    48 
    48 Town::Town(TileIndex tile)
    49 Town::Town(TileIndex tile)
   117 	variant ^= x >> 6;
   118 	variant ^= x >> 6;
   118 	variant ^= y >> 4;
   119 	variant ^= y >> 4;
   119 	variant -= y >> 6;
   120 	variant -= y >> 6;
   120 	variant &= 3;
   121 	variant &= 3;
   121 	return variant;
   122 	return variant;
       
   123 }
       
   124 
       
   125 /**
       
   126  * Return a random direction
       
   127  *
       
   128  * @return a random direction
       
   129  */
       
   130 static inline DiagDirection RandomDiagDir()
       
   131 {
       
   132 	return (DiagDirection)(3 & Random());
       
   133 }
       
   134 
       
   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));
   122 }
   145 }
   123 
   146 
   124 /**
   147 /**
   125  * House Tile drawing handler.
   148  * House Tile drawing handler.
   126  * Part of the tile loop process
   149  * Part of the tile loop process
   158 	image = dcts->building.sprite;
   181 	image = dcts->building.sprite;
   159 	if (image != 0) {
   182 	if (image != 0) {
   160 		AddSortableSpriteToDraw(image, dcts->building.pal,
   183 		AddSortableSpriteToDraw(image, dcts->building.pal,
   161 			ti->x + dcts->subtile_x,
   184 			ti->x + dcts->subtile_x,
   162 			ti->y + dcts->subtile_y,
   185 			ti->y + dcts->subtile_y,
   163 			dcts->width + 1,
   186 			dcts->width,
   164 			dcts->height + 1,
   187 			dcts->height,
   165 			dcts->dz,
   188 			dcts->dz,
   166 			ti->z,
   189 			ti->z,
   167 			HASBIT(_transparent_opt, TO_HOUSES)
   190 			HASBIT(_transparent_opt, TO_HOUSES)
   168 		);
   191 		);
   169 
   192 
   254 	}
   277 	}
   255 	return false;
   278 	return false;
   256 }
   279 }
   257 
   280 
   258 /**
   281 /**
   259  * Marks the town sign as needing a repaint
   282  * Marks the town sign as needing a repaint.
   260  * @param t Town requesting repaint
   283  *
       
   284  * This function marks the area of the sign of a town as dirty for repaint.
       
   285  *
       
   286  * @param t Town requesting town sign for repaint
       
   287  * @ingroup dirty
   261  */
   288  */
   262 static void MarkTownSignDirty(Town *t)
   289 static void MarkTownSignDirty(Town *t)
   263 {
   290 {
   264 	MarkAllViewportsDirty(
   291 	MarkAllViewportsDirty(
   265 		t->sign.left - 6,
   292 		t->sign.left - 6,
   561 static void ChangeTileOwner_Town(TileIndex tile, PlayerID old_player, PlayerID new_player)
   588 static void ChangeTileOwner_Town(TileIndex tile, PlayerID old_player, PlayerID new_player)
   562 {
   589 {
   563 	/* not used */
   590 	/* not used */
   564 }
   591 }
   565 
   592 
   566 
       
   567 static const TileIndexDiffC _roadblock_tileadd[] = {
       
   568 	{ 0, -1},
       
   569 	{ 1,  0},
       
   570 	{ 0,  1},
       
   571 	{-1,  0},
       
   572 
       
   573 	/* Store the first 3 elements again.
       
   574 	 * Lets us rotate without using &3. */
       
   575 	{ 0, -1},
       
   576 	{ 1,  0},
       
   577 	{ 0,  1}
       
   578 };
       
   579 
       
   580 /**
       
   581  * Distance multiplyer
       
   582  * Defines the possible distances between 2 road tiles
       
   583  */
       
   584 enum RoadBlockTitleDistance {
       
   585 	RB_TILE_DIST1 = 1, ///< 1 tile between
       
   586 	RB_TILE_DIST2,     ///< 2 tiles between
       
   587 };
       
   588 
       
   589 static bool GrowTown(Town *t);
   593 static bool GrowTown(Town *t);
   590 
   594 
   591 static void TownTickHandler(Town *t)
   595 static void TownTickHandler(Town *t)
   592 {
   596 {
   593 	if (HASBIT(t->flags12, TOWN_IS_FUNDED)) {
   597 	if (HASBIT(t->flags12, TOWN_IS_FUNDED)) {
   621 
   625 
   622 		if (IsValidTownID(i)) TownTickHandler(GetTown(i));
   626 		if (IsValidTownID(i)) TownTickHandler(GetTown(i));
   623 	}
   627 	}
   624 }
   628 }
   625 
   629 
   626 static RoadBits GetTownRoadMask(TileIndex tile)
   630 /**
       
   631  * Return the RoadBits of a tile
       
   632  *
       
   633  * @note There are many other functions doing things like that.
       
   634  * @note Needs to be checked for needlessness.
       
   635  * @param tile The tile we want to analyse
       
   636  * @return The roadbits of the given tile
       
   637  */
       
   638 static RoadBits GetTownRoadBits(TileIndex tile)
   627 {
   639 {
   628 	TrackBits b = GetAnyRoadTrackBits(tile, ROADTYPE_ROAD);
   640 	TrackBits b = GetAnyRoadTrackBits(tile, ROADTYPE_ROAD);
   629 	RoadBits r = ROAD_NONE;
   641 	RoadBits r = ROAD_NONE;
   630 
   642 
   631 	if (b == TRACK_BIT_NONE) return r;
   643 	if (b == TRACK_BIT_NONE) return r;
   643  *
   655  *
   644  * @param tile curent tile
   656  * @param tile curent tile
   645  * @param dir target direction
   657  * @param dir target direction
   646  * @param dist_multi distance multiplyer
   658  * @param dist_multi distance multiplyer
   647  * @return true if one of the neighboring tiles at the
   659  * @return true if one of the neighboring tiles at the
   648  *  given distance is a road tile else
   660  *  given distance is a road tile else false
   649  */
   661  */
   650 static bool IsNeighborRoadTile(TileIndex tile, int dir, RoadBlockTitleDistance dist_multi)
   662 static bool IsNeighborRoadTile(TileIndex tile, DiagDirection dir, uint dist_multi)
   651 {
   663 {
   652 	return (HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * ToTileIndexDiff(_roadblock_tileadd[dir + 1]))), dir ^ 2) ||
   664 	static TileIndexDiff tid_lt[3]; ///< lookup table for the used diff values
   653 			HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * ToTileIndexDiff(_roadblock_tileadd[dir + 3]))), dir ^ 2) ||
   665 	tid_lt[0] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT));
   654 			HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * (ToTileIndexDiff(_roadblock_tileadd[dir + 1]) + ToTileIndexDiff(_roadblock_tileadd[dir + 2])))), dir) ||
   666 	tid_lt[1] = TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT));
   655 			HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * (ToTileIndexDiff(_roadblock_tileadd[dir + 3]) + ToTileIndexDiff(_roadblock_tileadd[dir + 2])))), dir));
   667 	tid_lt[2] = TileOffsByDiagDir(ReverseDiagDir(dir));
   656 }
   668 
   657 
   669 	/* We add 1 to the distance because we want to get 1 for
   658 static bool IsRoadAllowedHere(TileIndex tile, int dir)
   670 	 * the min distance multiplyer and not 0.
       
   671 	 * Therefore we start at 4. The 4 is used because
       
   672 	 * there are 4 tiles per distance step to check.
       
   673 	 */
       
   674 	dist_multi = (dist_multi + 1) * 4;
       
   675 	for (uint pos = 4; pos < dist_multi; pos++) {
       
   676 		TileIndexDiff cur = 0;
       
   677 		/* For each even value of pos add the right TileIndexDiff
       
   678 		 * for each uneven value the left TileIndexDiff
       
   679 		 * for each with 2nd bit set (2,3,6,7,..) add the reversed TileIndexDiff
       
   680 		 */
       
   681 		cur += tid_lt[(pos & 1) ? 0 : 1];
       
   682 		if (pos & 2) cur += tid_lt[2];
       
   683 
       
   684 		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;
       
   686 	}
       
   687 	return false;
       
   688 }
       
   689 
       
   690 /**
       
   691  * Check if a Road is allowed on a given tile
       
   692  *
       
   693  * @param tile The target tile
       
   694  * @param dir The direction in which we want to extend the town
       
   695  * @return true if it is allowed else false
       
   696  */
       
   697 static bool IsRoadAllowedHere(TileIndex tile, DiagDirection dir)
   659 {
   698 {
   660 	if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false;
   699 	if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false;
   661 
   700 
   662 	Slope k;
   701 	Slope cur_slope, desired_slope;
   663 	Slope slope;
       
   664 
   702 
   665 	/* If this assertion fails, it might be because the world contains
   703 	/* If this assertion fails, it might be because the world contains
   666 	 *  land at the edges. This is not ok. */
   704 	 *  land at the edges. This is not ok. */
   667 	TILE_ASSERT(tile);
   705 	TILE_ASSERT(tile);
   668 
   706 
   669 	for (;;) {
   707 	for (;;) {
   670 		/* Check if there already is a road at this point? */
   708 		/* Check if there already is a road at this point? */
   671 		if (GetAnyRoadTrackBits(tile, ROADTYPE_ROAD) == 0) {
   709 		if (GetTownRoadBits(tile) == ROAD_NONE) {
   672 			/* No, try to build one in the direction.
   710 			/* No, try to build one in the direction.
   673 			 * if that fails clear the land, and if that fails exit.
   711 			 * if that fails clear the land, and if that fails exit.
   674 			 * This is to make sure that we can build a road here later. */
   712 			 * This is to make sure that we can build a road here later. */
   675 			if (CmdFailed(DoCommand(tile, (dir & ROAD_NW ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) &&
   713 			if (CmdFailed(DoCommand(tile, ((dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) &&
   676 					CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
   714 					CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
   677 				return false;
   715 				return false;
   678 		}
   716 		}
   679 
   717 
   680 		slope = GetTileSlope(tile, NULL);
   718 		cur_slope = GetTileSlope(tile, NULL);
   681 		if (slope == SLOPE_FLAT) {
   719 		if (cur_slope == SLOPE_FLAT) {
   682 no_slope:
   720 no_slope:
   683 			/* Tile has no slope */
   721 			/* Tile has no slope */
   684 			switch (_patches.town_layout) {
   722 			switch (_patches.town_layout) {
   685 				default: NOT_REACHED();
   723 				default: NOT_REACHED();
   686 
   724 
   687 				case TL_ORIGINAL: /* Disallow the road if any neighboring tile has a road (distance: 1) */
   725 				case TL_ORIGINAL: /* Disallow the road if any neighboring tile has a road (distance: 1) */
   688 					return !IsNeighborRoadTile(tile, dir, RB_TILE_DIST1);
   726 					return !IsNeighborRoadTile(tile, dir, 1);
   689 
   727 
   690 				case TL_BETTER_ROADS: /* Disallow the road if any neighboring tile has a road (distance: 1 and 2). */
   728 				case TL_BETTER_ROADS: /* Disallow the road if any neighboring tile has a road (distance: 1 and 2). */
   691 					return !(IsNeighborRoadTile(tile, dir, RB_TILE_DIST1) ||
   729 					return !IsNeighborRoadTile(tile, dir, 2);
   692 							IsNeighborRoadTile(tile, dir, RB_TILE_DIST2));
       
   693 			}
   730 			}
   694 		}
   731 		}
   695 
   732 
   696 		/* If the tile is not a slope in the right direction, then
   733 		/* If the tile is not a slope in the right direction, then
   697 		 * maybe terraform some. */
   734 		 * maybe terraform some. */
   698 		k = (dir & ROAD_NW) ? SLOPE_NE : SLOPE_NW;
   735 		desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NE : SLOPE_NW;
   699 		if (k != slope && ComplementSlope(k) != slope) {
   736 		if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) {
   700 			uint32 r = Random();
   737 			uint32 r = Random();
   701 
   738 
   702 			if (CHANCE16I(1, 8, r) && !_generating_world) {
   739 			if (CHANCE16I(1, 8, r) && !_generating_world) {
   703 				CommandCost res;
   740 				CommandCost res;
   704 
   741 
   705 				if (CHANCE16I(1, 16, r)) {
   742 				if (CHANCE16I(1, 16, r)) {
   706 					res = DoCommand(tile, slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER,
   743 					res = DoCommand(tile, cur_slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
   707 					                      CMD_TERRAFORM_LAND);
       
   708 				} else {
   744 				} else {
   709 					/* Note: Do not replace " ^ 0xF" with ComplementSlope(). The slope might be steep. */
   745 					/* Note: Do not replace " ^ 0xF" with ComplementSlope(). The slope might be steep. */
   710 					res = DoCommand(tile, slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER,
   746 					res = DoCommand(tile, cur_slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
   711 					                      CMD_TERRAFORM_LAND);
       
   712 				}
   747 				}
   713 				if (CmdFailed(res) && CHANCE16I(1, 3, r)) {
   748 				if (CmdFailed(res) && CHANCE16I(1, 3, r)) {
   714 					/* We can consider building on the slope, though. */
   749 					/* We can consider building on the slope, though. */
   715 					goto no_slope;
   750 					goto no_slope;
   716 				}
   751 				}
   753 /**
   788 /**
   754  * Generate the RoadBits of a grid tile
   789  * Generate the RoadBits of a grid tile
   755  *
   790  *
   756  * @param t current town
   791  * @param t current town
   757  * @param tile tile in reference to the town
   792  * @param tile tile in reference to the town
       
   793  * @param dir The direction to which we are growing ATM
   758  * @return the RoadBit of the current tile regarding
   794  * @return the RoadBit of the current tile regarding
   759  *  the selected town layout
   795  *  the selected town layout
   760  */
   796  */
   761 static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile)
   797 static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile, DiagDirection dir)
   762 {
   798 {
   763 	/* align the grid to the downtown */
   799 	/* align the grid to the downtown */
   764 	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); ///< Vector from downtown to the tile
   800 	TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); ///< Vector from downtown to the tile
   765 
   801 	RoadBits rcmd = ROAD_NONE;
   766 	/* lx, ly description:
       
   767 	 * @li lx and ly are true  if the tile is a crossing tile.
       
   768 	 * @li lx xor ly are true  if the tile is a straight road tile.
       
   769 	 * @li lx and ly are false if the tile is a house tile.
       
   770 	 */
       
   771 	bool lx, ly;
       
   772 
   802 
   773 	switch (_patches.town_layout) {
   803 	switch (_patches.town_layout) {
   774 		default: NOT_REACHED();
   804 		default: NOT_REACHED();
   775 
   805 
   776 		case TL_2X2_GRID:
   806 		case TL_2X2_GRID:
   777 			lx = ((grid_pos.x % 3) == 0);
   807 			if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y;
   778 			ly = ((grid_pos.y % 3) == 0);
   808 			if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X;
   779 			break;
   809 			break;
   780 
   810 
   781 		case TL_3X3_GRID:
   811 		case TL_3X3_GRID:
   782 			lx = ((grid_pos.x % 4) == 0);
   812 			if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y;
   783 			ly = ((grid_pos.y % 4) == 0);
   813 			if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X;
   784 			break;
   814 			break;
   785 	}
   815 	}
   786 
   816 
   787 	/* generate the basic grid structure */
   817 	/* Stop if the tile is not a part of the grid lines */
   788 	if (!lx && !ly) {         ///< It is a house tile
   818 	if (rcmd == ROAD_NONE) return rcmd;
   789 		return ROAD_NONE;
   819 
   790 	} else if (lx && !ly) {   ///< It is a Y-dir road tile
   820 	/* Optimise only X-junctions */
   791 		return ROAD_Y;
   821 	if (COUNTBITS(rcmd) != 2) {
   792 	} else if (!lx && ly) {   ///< It is a X-dir road tile
   822 		RoadBits rb_template;
   793 		return ROAD_X;
   823 
   794 	} else {                  ///< It is a crossing tile
       
   795 		/* Presets for junctions on slopes
       
   796 		 * not nice :( */
       
   797 		switch (GetTileSlope(tile, NULL)) {
   824 		switch (GetTileSlope(tile, NULL)) {
   798 			case SLOPE_W:
   825 			default:       rb_template = ROAD_ALL; break;
   799 				return ROAD_NW | ROAD_SW;
   826 			case SLOPE_W:  rb_template = ROAD_NW | ROAD_SW; break;
   800 			case SLOPE_S:
   827 			case SLOPE_SW: rb_template = ROAD_Y  | ROAD_SW; break;
   801 				return ROAD_SE | ROAD_SW;
   828 			case SLOPE_S:  rb_template = ROAD_SW | ROAD_SE; break;
   802 			case SLOPE_SW:
   829 			case SLOPE_SE: rb_template = ROAD_X  | ROAD_SE; break;
   803 				return ROAD_Y | ROAD_SW;
   830 			case SLOPE_E:  rb_template = ROAD_SE | ROAD_NE; break;
   804 			case SLOPE_E:
   831 			case SLOPE_NE: rb_template = ROAD_Y  | ROAD_NE; break;
   805 				return ROAD_NE | ROAD_SE;
   832 			case SLOPE_N:  rb_template = ROAD_NE | ROAD_NW; break;
   806 			case SLOPE_SE:
   833 			case SLOPE_NW: rb_template = ROAD_X  | ROAD_NW; break;
   807 				return ROAD_X | ROAD_SE;
       
   808 			case SLOPE_N:
       
   809 				return ROAD_NW | ROAD_NE;
       
   810 			case SLOPE_NW:
       
   811 				return ROAD_X | ROAD_NW;
       
   812 			case SLOPE_NE:
       
   813 				return ROAD_Y | ROAD_NE;
       
   814 			case SLOPE_STEEP_W:
   834 			case SLOPE_STEEP_W:
   815 			case SLOPE_STEEP_N:
       
   816 				return ROAD_X;
       
   817 			case SLOPE_STEEP_S:
   835 			case SLOPE_STEEP_S:
   818 			case SLOPE_STEEP_E:
   836 			case SLOPE_STEEP_E:
   819 				return ROAD_Y;
   837 			case SLOPE_STEEP_N:
   820 			default:
   838 				rb_template = ROAD_NONE;
   821 				return ROAD_ALL;
   839 				break;
   822 		}
   840 		}
   823 	}
   841 
       
   842 		/* Stop if the template is compatible to the growth dir */
       
   843 		if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template;
       
   844 		/* If not generate a straight road in the direction of the growth */
       
   845 		return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir));
       
   846 	}
       
   847 
       
   848 	return rcmd;
   824 }
   849 }
   825 
   850 
   826 /**
   851 /**
   827  * Check there are enougth neighbor house tiles next to the current tile
   852  * Check there are enough neighbor house tiles next to the current tile
   828  *
   853  *
   829  * @param tile current tile
   854  * @param tile current tile
   830  * @return true if there are more than 2 house tiles next
   855  * @return true if there are more than 2 house tiles next
   831  *  to the current one
   856  *  to the current one
   832  */
   857  */
   838 	if (TileX(tile) < 1 || TileY(tile) < 1) {
   863 	if (TileX(tile) < 1 || TileY(tile) < 1) {
   839 		return false;
   864 		return false;
   840 	}
   865 	}
   841 
   866 
   842 	/* Check the tiles E,N,W and S of the current tile. */
   867 	/* Check the tiles E,N,W and S of the current tile. */
   843 	for (uint i = 0; i < 4; i++) {
   868 	for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
   844 		if (IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[i])), MP_HOUSE)) {
   869 		if (IsTileType(AddDiagDirToTileIndex(tile, i), MP_HOUSE)) {
   845 			counter++;
   870 			counter++;
   846 		}
   871 		}
   847 
   872 
   848 		/* If there are enougth neighbor's stop it here */
   873 		/* If there are enougth neighbor's stop it here */
   849 		if (counter >= 3) {
   874 		if (counter >= 3) {
   864  * 	@li TL_2X2_GRID
   889  * 	@li TL_2X2_GRID
   865  * 	@li TL_3X3_GRID
   890  * 	@li TL_3X3_GRID
   866  * @li Forbid roads, only build houses
   891  * @li Forbid roads, only build houses
   867  * 	@li TL_NO_ROADS
   892  * 	@li TL_NO_ROADS
   868  *
   893  *
   869  * @param tile_ptr current tile
   894  * @param tile_ptr The current tile
   870  * @param mask current tiles RoadBits
   895  * @param cur_rb The current tiles RoadBits
   871  * @param block road block
   896  * @param target_dir The target road dir
   872  * @param t1 current town
   897  * @param t1 The current town
   873  */
   898  */
   874 static void GrowTownInTile(TileIndex* tile_ptr, RoadBits mask, int block, Town* t1)
   899 static void GrowTownInTile(TileIndex* tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town* t1)
   875 {
   900 {
   876 	RoadBits rcmd;
   901 	RoadBits rcmd = ROAD_NONE;  ///< RoadBits for the road construction command
   877 	TileIndex tmptile;
   902 	TileIndex tmptile;          ///< Dummy tile for various things
   878 	DiagDirection i;
   903 	TileIndex tile = *tile_ptr; ///< The main tile on which we base our growth
   879 	int j;
       
   880 	TileIndex tile = *tile_ptr;
       
   881 
   904 
   882 	TILE_ASSERT(tile);
   905 	TILE_ASSERT(tile);
   883 
   906 
   884 	if (mask == 0) {
   907 	if (cur_rb == ROAD_NONE) {
   885 		int a;
       
   886 		int b;
       
   887 
       
   888 		/* Tile has no road. First reset the status counter
   908 		/* Tile has no road. First reset the status counter
   889 		 * to say that this is the last iteration. */
   909 		 * to say that this is the last iteration. */
   890 		_grow_town_result = 0;
   910 		_grow_town_result = 0;
   891 
   911 
   892 		/* Remove hills etc */
   912 		/* Remove hills etc */
   899 			case TL_NO_ROADS: /* Disallow Roads */
   919 			case TL_NO_ROADS: /* Disallow Roads */
   900 				return;
   920 				return;
   901 
   921 
   902 			case TL_3X3_GRID:
   922 			case TL_3X3_GRID:
   903 			case TL_2X2_GRID:
   923 			case TL_2X2_GRID:
   904 				rcmd = GetTownRoadGridElement(t1, tile);
   924 				rcmd = GetTownRoadGridElement(t1, tile, target_dir);
   905 				if (rcmd == ROAD_NONE) {
   925 				if (rcmd == ROAD_NONE) return;
   906 					return;
       
   907 				}
       
   908 				break;
   926 				break;
   909 
   927 
   910 			case TL_BETTER_ROADS:
   928 			case TL_BETTER_ROADS:
   911 			case TL_ORIGINAL:
   929 			case TL_ORIGINAL:
   912 				if (!IsRoadAllowedHere(tile, block)) {
   930 				if (!IsRoadAllowedHere(tile, target_dir)) return;
   913 					return;
   931 
       
   932 				DiagDirection source_dir = ReverseDiagDir(target_dir);
       
   933 
       
   934 				if (CHANCE16(1, 4)) {
       
   935 					/* Randomize a new target dir */
       
   936 					do target_dir = RandomDiagDir(); while (target_dir == source_dir);
   914 				}
   937 				}
   915 
   938 
   916 				/* Randomize new road block numbers */
   939 				if (!IsRoadAllowedHere(AddDiagDirToTileIndex(tile, target_dir), target_dir)) {
   917 				a = block;
       
   918 				b = block ^ 2;
       
   919 				if (CHANCE16(1, 4)) {
       
   920 					do {
       
   921 						a = GB(Random(), 0, 2);
       
   922 					} while (a == b);
       
   923 				}
       
   924 
       
   925 				if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) {
       
   926 					/* A road is not allowed to continue the randomized road,
   940 					/* A road is not allowed to continue the randomized road,
   927 					 *   return if the road we're trying to build is curved. */
   941 					 *  return if the road we're trying to build is curved. */
   928 					if (a != (b ^ 2)) {
   942 					if (target_dir != ReverseDiagDir(source_dir)) return;
   929 						return;
       
   930 					}
       
   931 
   943 
   932 					/* Return if neither side of the new road is a house */
   944 					/* Return if neither side of the new road is a house */
   933 					if (!IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 1])), MP_HOUSE) &&
   945 					if (!IsTileType(AddDiagDirToTileIndex(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) &&
   934 							!IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 3])), MP_HOUSE)) {
   946 							!IsTileType(AddDiagDirToTileIndex(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) {
   935 						return;
   947 						return;
   936 					}
   948 					}
   937 
   949 
   938 					/* That means that the road is only allowed if there is a house
   950 					/* That means that the road is only allowed if there is a house
   939 					 *  at any side of the new road. */
   951 					 *  at any side of the new road. */
   940 				}
   952 				}
   941 
   953 
   942 				rcmd = (RoadBits)((ROAD_NW << a) + (ROAD_NW << b));
   954 				rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir);
   943 				break;
   955 				break;
   944 		}
   956 		}
   945 
   957 
   946 	} else if (block < 5 && !HASBIT(mask, block ^ 2)) {
   958 	} else if (target_dir < (DiagDirection)5 && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) {
   947 		/* Continue building on a partial road.
   959 		/* Continue building on a partial road.
   948 		 * Always OK. */
   960 		 * Should be allways OK, so we only generate
       
   961 		 * the fitting RoadBits */
   949 		_grow_town_result = 0;
   962 		_grow_town_result = 0;
   950 
   963 
   951 		switch (_patches.town_layout) {
   964 		switch (_patches.town_layout) {
   952 			default: NOT_REACHED();
   965 			default: NOT_REACHED();
   953 
   966 
   954 			case TL_NO_ROADS: /* Disallow Roads */
   967 			case TL_NO_ROADS: /* Disallow Roads */
   955 				return;
   968 				return;
   956 
   969 
   957 			case TL_3X3_GRID:
   970 			case TL_3X3_GRID:
   958 			case TL_2X2_GRID:
   971 			case TL_2X2_GRID:
   959 			 	rcmd = GetTownRoadGridElement(t1, tile);
   972 			 	rcmd = GetTownRoadGridElement(t1, tile, target_dir);
   960 				break;
   973 				break;
   961 
   974 
   962 			case TL_BETTER_ROADS:
   975 			case TL_BETTER_ROADS:
   963 			case TL_ORIGINAL:
   976 			case TL_ORIGINAL:
   964 				rcmd = (RoadBits)(ROAD_NW << (block ^ 2));
   977 				rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir));
   965 				break;
   978 				break;
   966 		}
   979 		}
   967 	} else {
   980 	} else {
   968 		int i;
   981 		bool allow_house = false; ///< Value which decides if we want to construct a house
   969 		bool allow_house = false;
   982 		TileIndex tmptile2;       ///< Yet another dummy tile
   970 		TileIndex tmptile2;
       
   971 
   983 
   972 		/* Reached a tunnel/bridge? Then continue at the other side of it. */
   984 		/* Reached a tunnel/bridge? Then continue at the other side of it. */
   973 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   985 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   974 			if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) {
   986 			if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) {
   975 				*tile_ptr = GetOtherTunnelEnd(tile);
   987 				*tile_ptr = GetOtherTunnelEnd(tile);
   979 			return;
   991 			return;
   980 		}
   992 		}
   981 
   993 
   982 		/* Possibly extend the road in a direction.
   994 		/* Possibly extend the road in a direction.
   983 		 * Randomize a direction and if it has a road, bail out. */
   995 		 * Randomize a direction and if it has a road, bail out. */
   984 		i = GB(Random(), 0, 2);
   996 		target_dir = RandomDiagDir();
   985 		if (HASBIT(mask, i)) return;
   997 		if (cur_rb & DiagDirToRoadBits(target_dir)) return;
   986 
   998 
   987 		/* This is the tile we will reach if we extend to this direction. */
   999 		/* This is the tile we will reach if we extend to this direction. */
   988 		tmptile = TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[i]));
  1000 		tmptile = AddDiagDirToTileIndex(tile, target_dir);
   989 
  1001 
   990 		/* Don't do it if it reaches to water. */
  1002 		/* Don't walk into water. */
   991 		if (IsClearWaterTile(tmptile)) return;
  1003 		if (IsClearWaterTile(tmptile)) return;
   992 
  1004 
   993 		switch (_patches.town_layout) {
  1005 		switch (_patches.town_layout) {
   994 			default: NOT_REACHED();
  1006 			default: NOT_REACHED();
   995 
  1007 
   997 				allow_house = true;
  1009 				allow_house = true;
   998 				break;
  1010 				break;
   999 
  1011 
  1000 			case TL_3X3_GRID: /* Use 2x2 grid afterwards! */
  1012 			case TL_3X3_GRID: /* Use 2x2 grid afterwards! */
  1001 				/* Fill gap if house has enougth neighbors */
  1013 				/* Fill gap if house has enougth neighbors */
  1002 				tmptile2 = TILE_ADD(tmptile, ToTileIndexDiff(_roadblock_tileadd[i]));
  1014 				tmptile2 = AddDiagDirToTileIndex(tmptile, target_dir);
  1003 				if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) {
  1015 				if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) {
  1004 					_grow_town_result = -1;
  1016 					_grow_town_result = -1;
  1005 				}
  1017 				}
  1006 
  1018 
  1007 			case TL_2X2_GRID:
  1019 			case TL_2X2_GRID:
  1008 				rcmd = GetTownRoadGridElement(t1, tmptile);
  1020 				rcmd = GetTownRoadGridElement(t1, tmptile, target_dir);
  1009 				allow_house = (rcmd == ROAD_NONE);
  1021 				allow_house = (rcmd == ROAD_NONE);
  1010 				break;
  1022 				break;
  1011 
  1023 
  1012 			case TL_BETTER_ROADS: /* Use original afterwards! */
  1024 			case TL_BETTER_ROADS: /* Use original afterwards! */
  1013 				/* Fill gap if house has enougth neighbors */
  1025 				/* Fill gap if house has enougth neighbors */
  1014 				tmptile2 = TILE_ADD(tmptile, ToTileIndexDiff(_roadblock_tileadd[i]));
  1026 				tmptile2 = AddDiagDirToTileIndex(tmptile, target_dir);
  1015 				if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) {
  1027 				if (AreNeighborsHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) {
  1016 					_grow_town_result = -1;
  1028 					_grow_town_result = -1;
  1017 				}
  1029 				}
  1018 
  1030 
  1019 			case TL_ORIGINAL:
  1031 			case TL_ORIGINAL:
  1020 				 /* Allow a house at the edge. 60% chance or
  1032 				 /* Allow a house at the edge. 60% chance or
  1021 				  * always ok if no road allowed. */
  1033 				  * always ok if no road allowed. */
  1022 				allow_house = (!IsRoadAllowedHere(tmptile, i) || CHANCE16(6, 10));
  1034 				rcmd = DiagDirToRoadBits(target_dir);
       
  1035 				allow_house = (!IsRoadAllowedHere(tmptile, target_dir) || CHANCE16(6, 10));
  1023 				break;
  1036 				break;
  1024 		}
  1037 		}
  1025 
       
  1026 
  1038 
  1027 		if (allow_house) {
  1039 		if (allow_house) {
  1028 			/* Build a house, but not if there already is a house there. */
  1040 			/* Build a house, but not if there already is a house there. */
  1029 			if (!IsTileType(tmptile, MP_HOUSE)) {
  1041 			if (!IsTileType(tmptile, MP_HOUSE)) {
  1030 				/* Level the land if possible */
  1042 				/* Level the land if possible */
  1038 			}
  1050 			}
  1039 			return;
  1051 			return;
  1040 		}
  1052 		}
  1041 
  1053 
  1042 		_grow_town_result = 0;
  1054 		_grow_town_result = 0;
  1043 		rcmd = (RoadBits)(ROAD_NW << i);
       
  1044 	}
  1055 	}
  1045 
  1056 
  1046 	/* Return if a water tile */
  1057 	/* Return if a water tile */
  1047 	if (IsClearWaterTile(tile)) return;
  1058 	if (IsClearWaterTile(tile)) return;
  1048 
  1059 
       
  1060 	DiagDirection bridge_dir; ///< The direction of a bridge we maybe want to build
  1049 	/* Determine direction of slope,
  1061 	/* Determine direction of slope,
  1050 	 *  and build a road if not a special slope. */
  1062 	 *  and build a road if not a special slope. */
  1051 	switch (GetTileSlope(tile, NULL)) {
  1063 	switch (GetTileSlope(tile, NULL)) {
  1052 		case SLOPE_SW: i = DIAGDIR_NE; break;
  1064 		case SLOPE_SW: bridge_dir = DIAGDIR_NE; break;
  1053 		case SLOPE_SE: i = DIAGDIR_NW; break;
  1065 		case SLOPE_SE: bridge_dir = DIAGDIR_NW; break;
  1054 		case SLOPE_NW: i = DIAGDIR_SE; break;
  1066 		case SLOPE_NW: bridge_dir = DIAGDIR_SE; break;
  1055 		case SLOPE_NE: i = DIAGDIR_SW; break;
  1067 		case SLOPE_NE: bridge_dir = DIAGDIR_SW; break;
  1056 
  1068 
  1057 		default:
  1069 		default:
  1058 build_road_and_exit:
  1070 build_road_and_exit:
  1059 			if (CmdSucceeded(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) {
  1071 			if (CmdSucceeded(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) {
  1060 				_grow_town_result = -1;
  1072 				_grow_town_result = -1;
  1061 			}
  1073 			}
  1062 			return;
  1074 			return;
  1063 	}
  1075 	}
  1064 
  1076 
  1065 	/* Check if the bridge is in the right direction */
  1077 	/* Check if the bridge is in the right direction */
  1066 	if ((rcmd == ROAD_X && (i == DIAGDIR_NW || i == DIAGDIR_SE)) ||
  1078 	if (!(rcmd & DiagDirToRoadBits(bridge_dir))) goto build_road_and_exit;
  1067 			(rcmd == ROAD_Y && (i == DIAGDIR_NE || i == DIAGDIR_SW))) {
  1079 
  1068 		goto build_road_and_exit;
  1080 	/* We are in the right direction */
  1069 	}
  1081 	uint32 bridge_length = 0; ///< This value stores the length of the possible bridge
  1070 
  1082 	tmptile = tile;           ///< Now we use this dummy to store the other waterside
  1071 	tmptile = tile;
       
  1072 
       
  1073 	/* Now it contains the direction of the slope */
       
  1074 	j = -11; // max 11 tile long bridges
       
  1075 	do {
  1083 	do {
  1076 		if (++j == 0)
  1084 		if (bridge_length++ >= 11) {
       
  1085 			/* Max 11 tile long bridges */
  1077 			goto build_road_and_exit;
  1086 			goto build_road_and_exit;
  1078 		tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(i));
  1087 		}
       
  1088 		tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(bridge_dir));
  1079 	} while (IsClearWaterTile(tmptile));
  1089 	} while (IsClearWaterTile(tmptile));
  1080 
  1090 
  1081 	/* no water tiles in between? */
  1091 	/* no water tiles in between? */
  1082 	if (j == -10)
  1092 	if (bridge_length == 1) goto build_road_and_exit;
  1083 		goto build_road_and_exit;
  1093 
  1084 
  1094 	for (uint times = 0; times <= 22; times++) {
  1085 	/* Quit if it selecting an appropiate bridge type fails a large number of times. */
       
  1086 	j = 22;
       
  1087 	do {
       
  1088 		byte bridge_type = RandomRange(MAX_BRIDGES - 1);
  1095 		byte bridge_type = RandomRange(MAX_BRIDGES - 1);
       
  1096 
  1089 		/* Can we actually build the bridge? */
  1097 		/* Can we actually build the bridge? */
  1090 		if (CmdSucceeded(DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) {
  1098 		if (CmdSucceeded(DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) {
  1091 			DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE);
  1099 			DoCommand(tile, tmptile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE);
  1092 				_grow_town_result = -1;
  1100 			_grow_town_result = -1;
  1093 
       
  1094 			/* obviously, if building any bridge would fail, there is no need to try other bridge-types */
       
  1095 			return;
  1101 			return;
  1096 		}
  1102 		}
  1097 	} while (--j != 0);
  1103 	}
       
  1104 	/* Quit if it selecting an appropiate bridge type fails a large number of times. */
  1098 }
  1105 }
  1099 
  1106 
  1100 /** Returns "growth" if a house was built, or no if the build failed.
  1107 /** Returns "growth" if a house was built, or no if the build failed.
  1101  * @param t town to inquiry
  1108  * @param t town to inquiry
  1102  * @param tile to inquiry
  1109  * @param tile to inquiry
  1103  * @return something other than zero(0)if town expansion was possible
  1110  * @return something other than zero(0)if town expansion was possible
  1104  */
  1111  */
  1105 static int GrowTownAtRoad(Town *t, TileIndex tile)
  1112 static int GrowTownAtRoad(Town *t, TileIndex tile)
  1106 {
  1113 {
  1107 	int block = 5; // special case
  1114 	/* Special case.
       
  1115 	 * @see GrowTownInTile Check the else if
       
  1116 	 */
       
  1117 	DiagDirection target_dir = (DiagDirection)5; ///< The direction in which we want to extend the town
  1108 
  1118 
  1109 	TILE_ASSERT(tile);
  1119 	TILE_ASSERT(tile);
  1110 
  1120 
  1111 	/* Number of times to search.
  1121 	/* Number of times to search.
  1112 	 * Better roads, 2X2 and 3X3 grid grow quite fast so we give
  1122 	 * Better roads, 2X2 and 3X3 grid grow quite fast so we give
  1125 			_grow_town_result = 10 + t->num_houses * 4 / 9;
  1135 			_grow_town_result = 10 + t->num_houses * 4 / 9;
  1126 			break;
  1136 			break;
  1127 	}
  1137 	}
  1128 
  1138 
  1129 	do {
  1139 	do {
  1130 		/* Get a bitmask of the road blocks on a tile */
  1140 		RoadBits cur_rb = GetTownRoadBits(tile); ///< The RoadBits of the current tile
  1131 		RoadBits mask = GetTownRoadMask(tile);
       
  1132 
  1141 
  1133 		/* Try to grow the town from this point */
  1142 		/* Try to grow the town from this point */
  1134 		GrowTownInTile(&tile, mask, block, t);
  1143 		GrowTownInTile(&tile, cur_rb, target_dir, t);
  1135 
  1144 
  1136 		/* Exclude the source position from the bitmask
  1145 		/* Exclude the source position from the bitmask
  1137 		 * and return if no more road blocks available */
  1146 		 * and return if no more road blocks available */
  1138 		ClrBitT(mask, (block ^ 2));
  1147 		cur_rb &= ~DiagDirToRoadBits(ReverseDiagDir(target_dir));
  1139 		if (mask == ROAD_NONE)
  1148 		if (cur_rb == ROAD_NONE)
  1140 			return _grow_town_result;
  1149 			return _grow_town_result;
  1141 
  1150 
  1142 		/* Select a random bit from the blockmask, walk a step
  1151 		/* Select a random bit from the blockmask, walk a step
  1143 		 * and continue the search from there. */
  1152 		 * and continue the search from there. */
  1144 		do block = Random() & 3; while (!HASBIT(mask, block));
  1153 		do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir)));
  1145 		tile += ToTileIndexDiff(_roadblock_tileadd[block]);
  1154 		tile = AddDiagDirToTileIndex(tile, target_dir);
  1146 
  1155 
  1147 		if (IsTileType(tile, MP_ROAD)) {
  1156 		if (IsTileType(tile, MP_ROAD)) {
  1148 			/* Don't allow building over roads of other cities */
  1157 			/* Don't allow building over roads of other cities */
  1149 			if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) {
  1158 			if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) {
  1150 				_grow_town_result = -1;
  1159 				_grow_town_result = -1;
  1160 	} while (--_grow_town_result >= 0);
  1169 	} while (--_grow_town_result >= 0);
  1161 
  1170 
  1162 	return (_grow_town_result == -2);
  1171 	return (_grow_town_result == -2);
  1163 }
  1172 }
  1164 
  1173 
  1165 /** Generate a random road block
  1174 /**
       
  1175  * Generate a random road block.
  1166  * The probability of a straight road
  1176  * The probability of a straight road
  1167  * is somewhat higher than a curved. */
  1177  * is somewhat higher than a curved.
       
  1178  *
       
  1179  * @return A RoadBits value with 2 bits set
       
  1180  */
  1168 static RoadBits GenRandomRoadBits()
  1181 static RoadBits GenRandomRoadBits()
  1169 {
  1182 {
  1170 	uint32 r = Random();
  1183 	uint32 r = Random();
  1171 	uint a = GB(r, 0, 2);
  1184 	uint a = GB(r, 0, 2);
  1172 	uint b = GB(r, 8, 2);
  1185 	uint b = GB(r, 8, 2);
  1173 	if (a == b) b ^= 2;
  1186 	if (a == b) b ^= 2;
  1174 	return (RoadBits)((1 << a) + (1 << b));
  1187 	return (RoadBits)((ROAD_NW << a) + (ROAD_NW << b));
  1175 }
  1188 }
  1176 
  1189 
  1177 /** Grow the town
  1190 /** Grow the town
  1178  * @Return true if a house was built, or no if the build failed. */
  1191  * @Return true if a house was built, or no if the build failed. */
  1179 static bool GrowTown(Town *t)
  1192 static bool GrowTown(Town *t)
  1180 {
  1193 {
  1181 	TileIndex tile;
  1194 
  1182 	const TileIndexDiffC *ptr;
  1195 	/* Let the town be a ghost town
  1183 	PlayerID old_player;
  1196 	 * The player wanted it in such a way. Thus there he has it. ;)
       
  1197 	 * Never reached in editor mode. */
       
  1198 	if (_patches.town_layout == TL_NO_ROADS && _generating_world) {
       
  1199 		return false;
       
  1200 	}
  1184 
  1201 
  1185 	static const TileIndexDiffC _town_coord_mod[] = {
  1202 	static const TileIndexDiffC _town_coord_mod[] = {
  1186 		{-1,  0},
  1203 		{-1,  0},
  1187 		{ 1,  1},
  1204 		{ 1,  1},
  1188 		{ 1, -1},
  1205 		{ 1, -1},
  1195 		{-2,  2},
  1212 		{-2,  2},
  1196 		{ 2,  2},
  1213 		{ 2,  2},
  1197 		{ 2, -2},
  1214 		{ 2, -2},
  1198 		{ 0,  0}
  1215 		{ 0,  0}
  1199 	};
  1216 	};
  1200 
  1217 	const TileIndexDiffC *ptr;
  1201 	/* Let the town be a ghost town
       
  1202 	 * The player wanted it in such a way. Thus there he has it. ;)
       
  1203 	 * Never reached in editor mode. */
       
  1204 	if (_patches.town_layout == TL_NO_ROADS && _generating_world) {
       
  1205 		return false;
       
  1206 	}
       
  1207 
  1218 
  1208 	/* Current player is a town */
  1219 	/* Current player is a town */
  1209 	old_player = _current_player;
  1220 	PlayerID old_player = _current_player;
  1210 	_current_player = OWNER_TOWN;
  1221 	_current_player = OWNER_TOWN;
  1211 
  1222 
       
  1223 	TileIndex tile = t->xy; ///< The tile we are working with ATM
       
  1224 
  1212 	/* Find a road that we can base the construction on. */
  1225 	/* Find a road that we can base the construction on. */
  1213 	tile = t->xy;
       
  1214 	for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
  1226 	for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
  1215 		if (GetAnyRoadTrackBits(tile, ROADTYPE_ROAD) != 0) {
  1227 		if (GetTownRoadBits(tile) != ROAD_NONE) {
  1216 			int r = GrowTownAtRoad(t, tile);
  1228 			int r = GrowTownAtRoad(t, tile);
  1217 			_current_player = old_player;
  1229 			_current_player = old_player;
  1218 			return r != 0;
  1230 			return r != 0;
  1219 		}
  1231 		}
  1220 		tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
  1232 		tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
  1922 static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id)
  1934 static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id)
  1923 {
  1935 {
  1924 	PlayerID old;
  1936 	PlayerID old;
  1925 	CommandCost r;
  1937 	CommandCost r;
  1926 
  1938 
  1927 	if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return false;
  1939 	/* Statues can be build on slopes, just like houses. Only the steep slopes is a no go. */
       
  1940 	if (IsSteepSlope(GetTileSlope(tile, NULL))) return false;
  1928 
  1941 
  1929 	if (!IsTileType(tile, MP_HOUSE) &&
  1942 	if (!IsTileType(tile, MP_HOUSE) &&
  1930 			!IsTileType(tile, MP_CLEAR) &&
  1943 			!IsTileType(tile, MP_CLEAR) &&
  1931 			!IsTileType(tile, MP_TREES)) {
  1944 			!IsTileType(tile, MP_TREES)) {
  1932 		return false;
  1945 		return false;
  2301 	_town_sort_dirty = true;
  2314 	_town_sort_dirty = true;
  2302 }
  2315 }
  2303 
  2316 
  2304 static CommandCost TerraformTile_Town(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
  2317 static CommandCost TerraformTile_Town(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
  2305 {
  2318 {
       
  2319 	if (AutoslopeEnabled()) {
       
  2320 		HouseID house = GetHouseType(tile);
       
  2321 		HouseSpec *hs = GetHouseSpecs(house);
       
  2322 
       
  2323 		/* Here we differ from TTDP by checking TILE_NOT_SLOPED */
       
  2324 		if (((hs->building_flags & TILE_NOT_SLOPED) == 0) && !IsSteepSlope(tileh_new) &&
       
  2325 			(GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) return _price.terraform;
       
  2326 	}
       
  2327 
  2306 	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  2328 	return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  2307 }
  2329 }
  2308 
  2330 
  2309 extern const TileTypeProcs _tile_type_town_procs = {
  2331 extern const TileTypeProcs _tile_type_town_procs = {
  2310 	DrawTile_Town,           /* draw_tile_proc */
  2332 	DrawTile_Town,           /* draw_tile_proc */