src/town_cmd.cpp
branchnoai
changeset 9722 ebf0ece7d8f6
parent 9718 f82a4facea8b
child 9723 eee46cb39750
equal deleted inserted replaced
9721:9a27928bcd5e 9722:ebf0ece7d8f6
    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 #include "autoslope.h"
    45 #include "waypoint.h"
    45 #include "waypoint.h"
       
    46 #include "transparency.h"
    46 
    47 
    47 /* Initialize the town-pool */
    48 /* Initialize the town-pool */
    48 DEFINE_OLD_POOL_GENERIC(Town, Town)
    49 DEFINE_OLD_POOL_GENERIC(Town, Town)
    49 
    50 
    50 Town::Town(TileIndex tile)
    51 Town::Town(TileIndex tile)
   181 			ti->y + dcts->subtile_y,
   182 			ti->y + dcts->subtile_y,
   182 			dcts->width,
   183 			dcts->width,
   183 			dcts->height,
   184 			dcts->height,
   184 			dcts->dz,
   185 			dcts->dz,
   185 			ti->z,
   186 			ti->z,
   186 			HASBIT(_transparent_opt, TO_HOUSES)
   187 			IsTransparencySet(TO_HOUSES)
   187 		);
   188 		);
   188 
   189 
   189 		if (HASBIT(_transparent_opt, TO_HOUSES)) return;
   190 		if (IsTransparencySet(TO_HOUSES)) return;
   190 	}
   191 	}
   191 
   192 
   192 	{
   193 	{
   193 		int proc = dcts->draw_proc - 1;
   194 		int proc = dcts->draw_proc - 1;
   194 
   195 
   353 	/* progress in construction stages */
   354 	/* progress in construction stages */
   354 	IncHouseConstructionTick(tile);
   355 	IncHouseConstructionTick(tile);
   355 	if (GetHouseConstructionTick(tile) != 0) return;
   356 	if (GetHouseConstructionTick(tile) != 0) return;
   356 
   357 
   357 	/* Check and/or  */
   358 	/* Check and/or  */
   358 	if (HASBIT(GetHouseSpecs(GetHouseType(tile))->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
   359 	if (HasBit(GetHouseSpecs(GetHouseType(tile))->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
   359 		uint16 callback_res = GetHouseCallback(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
   360 		uint16 callback_res = GetHouseCallback(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
   360 		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(tile, callback_res);
   361 		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(tile, callback_res);
   361 	}
   362 	}
   362 
   363 
   363 	if (IsHouseCompleted(tile)) {
   364 	if (IsHouseCompleted(tile)) {
   410 
   411 
   411 	t = GetTownByTile(tile);
   412 	t = GetTownByTile(tile);
   412 
   413 
   413 	r = Random();
   414 	r = Random();
   414 
   415 
   415 	if (HASBIT(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
   416 	if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
   416 		for (uint i = 0; i < 256; i++) {
   417 		for (uint i = 0; i < 256; i++) {
   417 			uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, tile);
   418 			uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, tile);
   418 
   419 
   419 			if (callback == CALLBACK_FAILED) break;
   420 			if (callback == CALLBACK_FAILED) break;
   420 			if (callback == 0x20FF) break;
   421 			if (callback == 0x20FF) break;
   464 	}
   465 	}
   465 
   466 
   466 	_current_player = OWNER_TOWN;
   467 	_current_player = OWNER_TOWN;
   467 
   468 
   468 	if (hs->building_flags & BUILDING_HAS_1_TILE &&
   469 	if (hs->building_flags & BUILDING_HAS_1_TILE &&
   469 			HASBIT(t->flags12, TOWN_IS_FUNDED) &&
   470 			HasBit(t->flags12, TOWN_IS_FUNDED) &&
   470 			CanDeleteHouse(tile) &&
   471 			CanDeleteHouse(tile) &&
   471 			max(_cur_year - GetHouseConstructionYear(tile), 0) >= hs->minimum_life &&
   472 			max(_cur_year - GetHouseConstructionYear(tile), 0) >= hs->minimum_life &&
   472 			--t->time_until_rebuild == 0) {
   473 			--t->time_until_rebuild == 0) {
   473 		t->time_until_rebuild = GB(r, 16, 8) + 192;
   474 		t->time_until_rebuild = GB(r, 16, 8) + 192;
   474 
   475 
   530 	for (uint8 i = 0; i < lengthof(accepts); i++) {
   531 	for (uint8 i = 0; i < lengthof(accepts); i++) {
   531 		accepts[i] = hs->accepts_cargo[i];
   532 		accepts[i] = hs->accepts_cargo[i];
   532 	}
   533 	}
   533 
   534 
   534 	/* Check for custom accepted cargo types */
   535 	/* Check for custom accepted cargo types */
   535 	if (HASBIT(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) {
   536 	if (HasBit(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) {
   536 		uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
   537 		uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
   537 		if (callback != CALLBACK_FAILED) {
   538 		if (callback != CALLBACK_FAILED) {
   538 			/* Replace accepted cargo types with translated values from callback */
   539 			/* Replace accepted cargo types with translated values from callback */
   539 			accepts[0] = GetCargoTranslation(GB(callback,  0, 5), hs->grffile);
   540 			accepts[0] = GetCargoTranslation(GB(callback,  0, 5), hs->grffile);
   540 			accepts[1] = GetCargoTranslation(GB(callback,  5, 5), hs->grffile);
   541 			accepts[1] = GetCargoTranslation(GB(callback,  5, 5), hs->grffile);
   541 			accepts[2] = GetCargoTranslation(GB(callback, 10, 5), hs->grffile);
   542 			accepts[2] = GetCargoTranslation(GB(callback, 10, 5), hs->grffile);
   542 		}
   543 		}
   543 	}
   544 	}
   544 
   545 
   545 	/* Check for custom cargo acceptance */
   546 	/* Check for custom cargo acceptance */
   546 	if (HASBIT(hs->callback_mask, CBM_HOUSE_CARGO_ACCEPTANCE)) {
   547 	if (HasBit(hs->callback_mask, CBM_HOUSE_CARGO_ACCEPTANCE)) {
   547 		uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
   548 		uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
   548 		if (callback != CALLBACK_FAILED) {
   549 		if (callback != CALLBACK_FAILED) {
   549 			if (accepts[0] != CT_INVALID) ac[accepts[0]] = GB(callback, 0, 4);
   550 			if (accepts[0] != CT_INVALID) ac[accepts[0]] = GB(callback, 0, 4);
   550 			if (accepts[1] != CT_INVALID) ac[accepts[1]] = GB(callback, 4, 4);
   551 			if (accepts[1] != CT_INVALID) ac[accepts[1]] = GB(callback, 4, 4);
   551 			if (_opt.landscape != LT_TEMPERATE && HASBIT(callback, 12)) {
   552 			if (_opt.landscape != LT_TEMPERATE && HasBit(callback, 12)) {
   552 				/* The 'S' bit indicates food instead of goods */
   553 				/* The 'S' bit indicates food instead of goods */
   553 				ac[CT_FOOD] = GB(callback, 8, 4);
   554 				ac[CT_FOOD] = GB(callback, 8, 4);
   554 			} else {
   555 			} else {
   555 				if (accepts[2] != CT_INVALID) ac[accepts[2]] = GB(callback, 8, 4);
   556 				if (accepts[2] != CT_INVALID) ac[accepts[2]] = GB(callback, 8, 4);
   556 			}
   557 			}
   588 
   589 
   589 static bool GrowTown(Town *t);
   590 static bool GrowTown(Town *t);
   590 
   591 
   591 static void TownTickHandler(Town *t)
   592 static void TownTickHandler(Town *t)
   592 {
   593 {
   593 	if (HASBIT(t->flags12, TOWN_IS_FUNDED)) {
   594 	if (HasBit(t->flags12, TOWN_IS_FUNDED)) {
   594 		int i = t->grow_counter - 1;
   595 		int i = t->grow_counter - 1;
   595 		if (i < 0) {
   596 		if (i < 0) {
   596 			if (GrowTown(t)) {
   597 			if (GrowTown(t)) {
   597 				i = t->growth_rate;
   598 				i = t->growth_rate;
   598 			} else {
   599 			} else {
   726 			}
   727 			}
   727 		}
   728 		}
   728 
   729 
   729 		/* If the tile is not a slope in the right direction, then
   730 		/* If the tile is not a slope in the right direction, then
   730 		 * maybe terraform some. */
   731 		 * maybe terraform some. */
   731 		desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NE : SLOPE_NW;
   732 		desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NW : SLOPE_NE;
   732 		if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) {
   733 		if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) {
   733 			uint32 r = Random();
   734 			if (CHANCE16(1, 8)) {
   734 
   735 				CommandCost res = CMD_ERROR;
   735 			if (CHANCE16I(1, 8, r) && !_generating_world) {
   736 				if (!_generating_world && CHANCE16(1, 10)) {
   736 				CommandCost res;
       
   737 
       
   738 				if (CHANCE16I(1, 16, r)) {
       
   739 					res = DoCommand(tile, cur_slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
       
   740 				} else {
       
   741 					/* Note: Do not replace " ^ 0xF" with ComplementSlope(). The slope might be steep. */
   737 					/* Note: Do not replace " ^ 0xF" with ComplementSlope(). The slope might be steep. */
   742 					res = DoCommand(tile, cur_slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
   738 					res = DoCommand(tile, CHANCE16(1, 16) ? cur_slope : cur_slope ^ 0xF, 0,
       
   739 							DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
   743 				}
   740 				}
   744 				if (CmdFailed(res) && CHANCE16I(1, 3, r)) {
   741 				if (CmdFailed(res) && CHANCE16(1, 3)) {
   745 					/* We can consider building on the slope, though. */
   742 					/* We can consider building on the slope, though. */
   746 					goto no_slope;
   743 					goto no_slope;
   747 				}
   744 				}
   748 			}
   745 			}
   749 			return false;
   746 			return false;
   808 			if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y;
   805 			if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y;
   809 			if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X;
   806 			if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X;
   810 			break;
   807 			break;
   811 	}
   808 	}
   812 
   809 
   813 	/* Stop if the tile is not a part of the grid lines */
       
   814 	if (rcmd == ROAD_NONE) return rcmd;
       
   815 
       
   816 	/* Optimise only X-junctions */
   810 	/* Optimise only X-junctions */
   817 	if (COUNTBITS(rcmd) != 2) {
   811 	if (rcmd != ROAD_ALL) return rcmd;
   818 		RoadBits rb_template;
   812 
   819 
   813 	RoadBits rb_template;
   820 		switch (GetTileSlope(tile, NULL)) {
   814 
   821 			default:       rb_template = ROAD_ALL; break;
   815 	switch (GetTileSlope(tile, NULL)) {
   822 			case SLOPE_W:  rb_template = ROAD_NW | ROAD_SW; break;
   816 		default:       rb_template = ROAD_ALL; break;
   823 			case SLOPE_SW: rb_template = ROAD_Y  | ROAD_SW; break;
   817 		case SLOPE_W:  rb_template = ROAD_NW | ROAD_SW; break;
   824 			case SLOPE_S:  rb_template = ROAD_SW | ROAD_SE; break;
   818 		case SLOPE_SW: rb_template = ROAD_Y  | ROAD_SW; break;
   825 			case SLOPE_SE: rb_template = ROAD_X  | ROAD_SE; break;
   819 		case SLOPE_S:  rb_template = ROAD_SW | ROAD_SE; break;
   826 			case SLOPE_E:  rb_template = ROAD_SE | ROAD_NE; break;
   820 		case SLOPE_SE: rb_template = ROAD_X  | ROAD_SE; break;
   827 			case SLOPE_NE: rb_template = ROAD_Y  | ROAD_NE; break;
   821 		case SLOPE_E:  rb_template = ROAD_SE | ROAD_NE; break;
   828 			case SLOPE_N:  rb_template = ROAD_NE | ROAD_NW; break;
   822 		case SLOPE_NE: rb_template = ROAD_Y  | ROAD_NE; break;
   829 			case SLOPE_NW: rb_template = ROAD_X  | ROAD_NW; break;
   823 		case SLOPE_N:  rb_template = ROAD_NE | ROAD_NW; break;
   830 			case SLOPE_STEEP_W:
   824 		case SLOPE_NW: rb_template = ROAD_X  | ROAD_NW; break;
   831 			case SLOPE_STEEP_S:
   825 		case SLOPE_STEEP_W:
   832 			case SLOPE_STEEP_E:
   826 		case SLOPE_STEEP_S:
   833 			case SLOPE_STEEP_N:
   827 		case SLOPE_STEEP_E:
   834 				rb_template = ROAD_NONE;
   828 		case SLOPE_STEEP_N:
   835 				break;
   829 			rb_template = ROAD_NONE;
   836 		}
   830 			break;
   837 
   831 	}
   838 		/* Stop if the template is compatible to the growth dir */
   832 
   839 		if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template;
   833 	/* Stop if the template is compatible to the growth dir */
   840 		/* If not generate a straight road in the direction of the growth */
   834 	if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template;
   841 		return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir));
   835 	/* If not generate a straight road in the direction of the growth */
   842 	}
   836 	return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir));
   843 
       
   844 	return rcmd;
       
   845 }
   837 }
   846 
   838 
   847 /**
   839 /**
   848  * Grows the town with an extra house.
   840  * Grows the town with an extra house.
   849  *  Check if there are enough neighbor house tiles
   841  *  Check if there are enough neighbor house tiles
   900  *  At first we check if a bridge is reasonable.
   892  *  At first we check if a bridge is reasonable.
   901  *  If so we check if we are able to build it.
   893  *  If so we check if we are able to build it.
   902  *
   894  *
   903  * @param t The current town
   895  * @param t The current town
   904  * @param tile The current tile
   896  * @param tile The current tile
   905  * @param rcmd The RoadBits which are possible on this tile
   897  * @param bridge_dir The valid direction in which to grow a bridge
   906  * @return true if a bridge has been build else false
   898  * @return true if a bridge has been build else false
   907  */
   899  */
   908 static bool GrowTownWithBridge(const Town *t, TileIndex tile, RoadBits rcmd)
   900 static bool GrowTownWithBridge(const Town *t, TileIndex tile, DiagDirection bridge_dir)
   909 {
   901 {
   910 	DiagDirection bridge_dir; // The direction of a bridge we maybe want to build
   902 	assert(bridge_dir < DIAGDIR_END);
   911 
   903 
   912 	/* Determine direction of slope,
   904 	const Slope slope = GetTileSlope(tile, NULL);
   913 	 *  and build a road if not a special slope. */
   905 	if (slope == SLOPE_FLAT) return false; // no slope, no bridge
   914 	switch (GetTileSlope(tile, NULL)) {
   906 
   915 		case SLOPE_SW: bridge_dir = DIAGDIR_NE; break;
   907 	/* Make sure the direction is compatible with the slope.
   916 		case SLOPE_SE: bridge_dir = DIAGDIR_NW; break;
   908 	 * If any of the following bits match, the slope is forbidden for
   917 		case SLOPE_NW: bridge_dir = DIAGDIR_SE; break;
   909 	 *  that diagdir. This means 5 non-steep slopes, and 3 steep-slopes
   918 		case SLOPE_NE: bridge_dir = DIAGDIR_SW; break;
   910 	 *  per diagdir.
   919 
   911 	 * 0 -> 0b1100
   920 		default: return false;
   912 	 * 1 -> 0b0110
   921 	}
   913 	 * 2 -> 0b0011
   922 
   914 	 * 3 -> 0b1001
   923 	/* Check if the bridge will be compatible to the RoadBits */
   915 	 * 0xCC is 0b11001100, so we just shift it right with
   924 	if (!(rcmd & DiagDirToRoadBits(ReverseDiagDir(bridge_dir)))) return false;
   916 	 * the direction to get the forbidden slope mask. */
       
   917 	if (HASBITS(slope & 0x0F, 0xCC >> bridge_dir)) return false;
       
   918 
       
   919 	/* Assure that the bridge is connectable to the start side */
       
   920 	if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false;
   925 
   921 
   926 	/* We are in the right direction */
   922 	/* We are in the right direction */
   927 	uint32 bridge_length = 0;     // This value stores the length of the possible bridge
   923 	uint8 bridge_length = 0;     // This value stores the length of the possible bridge
   928 	TileIndex bridge_tile = tile; // Used to store the other waterside
   924 	TileIndex bridge_tile = tile; // Used to store the other waterside
       
   925 
       
   926 	int delta = TileOffsByDiagDir(bridge_dir);
   929 
   927 
   930 	do {
   928 	do {
   931 		if (bridge_length++ >= 11) {
   929 		if (bridge_length++ >= 11) {
   932 			/* Max 11 tile long bridges */
   930 			/* Max 11 tile long bridges */
   933 			return false;
   931 			return false;
   934 		}
   932 		}
   935 		bridge_tile = TILE_MASK(bridge_tile + TileOffsByDiagDir(bridge_dir));
   933 		bridge_tile += delta;
   936 	} while (IsWaterTile(bridge_tile));
   934 	} while (TileX(bridge_tile) != 0 && TileY(bridge_tile) != 0 && IsWaterTile(bridge_tile) && TileX(bridge_tile) != 0);
   937 
   935 
   938 	/* no water tiles in between? */
   936 	/* no water tiles in between? */
   939 	if (bridge_length == 1) return false;
   937 	if (bridge_length == 1) return false;
   940 
   938 
   941 	for (uint8 times = 0; times <= 22; times++) {
   939 	for (uint8 times = 0; times <= 22; times++) {
  1104 
  1102 
  1105 		if (allow_house) {
  1103 		if (allow_house) {
  1106 			/* Build a house, but not if there already is a house there. */
  1104 			/* Build a house, but not if there already is a house there. */
  1107 			if (!IsTileType(house_tile, MP_HOUSE)) {
  1105 			if (!IsTileType(house_tile, MP_HOUSE)) {
  1108 				/* Level the land if possible */
  1106 				/* Level the land if possible */
  1109 				LevelTownLand(house_tile);
  1107 				if (CHANCE16(1, 6)) LevelTownLand(house_tile);
  1110 
  1108 
  1111 				/* And build a house.
  1109 				/* And build a house.
  1112 				 * Set result to -1 if we managed to build it. */
  1110 				 * Set result to -1 if we managed to build it. */
  1113 				if (BuildTownHouse(t1, house_tile)) {
  1111 				if (BuildTownHouse(t1, house_tile)) {
  1114 					_grow_town_result = GROWTH_SUCCEED;
  1112 					_grow_town_result = GROWTH_SUCCEED;
  1125 
  1123 
  1126 	/* Make the roads look nicer */
  1124 	/* Make the roads look nicer */
  1127 	rcmd = CleanUpRoadBits(tile, rcmd);
  1125 	rcmd = CleanUpRoadBits(tile, rcmd);
  1128 	if (rcmd == ROAD_NONE) return;
  1126 	if (rcmd == ROAD_NONE) return;
  1129 
  1127 
  1130 	if (GrowTownWithBridge(t1, tile, rcmd)) return;
  1128 	/* Only use the target direction for bridges to ensure they're connected.
       
  1129 	 * The target_dir is as computed previously according to town layout, so
       
  1130 	 * it will match it perfectly. */
       
  1131 	if (GrowTownWithBridge(t1, tile, target_dir)) return;
  1131 
  1132 
  1132 	GrowTownWithRoad(t1, tile, rcmd);
  1133 	GrowTownWithRoad(t1, tile, rcmd);
  1133 }
  1134 }
  1134 
  1135 
  1135 /** Returns "growth" if a house was built, or no if the build failed.
  1136 /** Returns "growth" if a house was built, or no if the build failed.
  1712 					hs = GetHouseSpecs(house);
  1713 					hs = GetHouseSpecs(house);
  1713 				}
  1714 				}
  1714 
  1715 
  1715 				if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue;
  1716 				if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue;
  1716 
  1717 
  1717 				if (HASBIT(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
  1718 				if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
  1718 					uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile);
  1719 					uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile);
  1719 					if (callback_res != CALLBACK_FAILED && callback_res == 0) continue;
  1720 					if (callback_res != CALLBACK_FAILED && callback_res == 0) continue;
  1720 				}
  1721 				}
  1721 			}
  1722 			}
  1722 
  1723 
  1723 			if (_cur_year < hs->min_date || _cur_year > hs->max_date) continue;
  1724 			if (_cur_year < hs->min_date || _cur_year > hs->max_date) continue;
  1724 
  1725 
  1725 			/* Special houses that there can be only one of. */
  1726 			/* Special houses that there can be only one of. */
  1726 			if (hs->building_flags & BUILDING_IS_CHURCH) {
  1727 			if (hs->building_flags & BUILDING_IS_CHURCH) {
  1727 				SETBIT(oneof, TOWN_HAS_CHURCH);
  1728 				SetBit(oneof, TOWN_HAS_CHURCH);
  1728 			} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  1729 			} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  1729 				SETBIT(oneof, TOWN_HAS_STADIUM);
  1730 				SetBit(oneof, TOWN_HAS_STADIUM);
  1730 			} else {
  1731 			} else {
  1731 				oneof = 0;
  1732 				oneof = 0;
  1732 			}
  1733 			}
  1733 
  1734 
  1734 			if (HASBITS(t->flags12 , oneof)) continue;
  1735 			if (HASBITS(t->flags12 , oneof)) continue;
  1846 	t->num_houses--;
  1847 	t->num_houses--;
  1847 	DecreaseBuildingCount(t, house);
  1848 	DecreaseBuildingCount(t, house);
  1848 
  1849 
  1849 	/* Clear flags for houses that only may exist once/town. */
  1850 	/* Clear flags for houses that only may exist once/town. */
  1850 	if (hs->building_flags & BUILDING_IS_CHURCH) {
  1851 	if (hs->building_flags & BUILDING_IS_CHURCH) {
  1851 		CLRBIT(t->flags12, TOWN_HAS_CHURCH);
  1852 		ClrBit(t->flags12, TOWN_HAS_CHURCH);
  1852 	} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  1853 	} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  1853 		CLRBIT(t->flags12, TOWN_HAS_STADIUM);
  1854 		ClrBit(t->flags12, TOWN_HAS_STADIUM);
  1854 	}
  1855 	}
  1855 
  1856 
  1856 	/* Do the actual clearing of tiles */
  1857 	/* Do the actual clearing of tiles */
  1857 	eflags = hs->building_flags;
  1858 	eflags = hs->building_flags;
  1858 	DoClearTownHouseHelper(tile);
  1859 	DoClearTownHouseHelper(tile);
  2009 static void TownActionBuildStatue(Town* t)
  2010 static void TownActionBuildStatue(Town* t)
  2010 {
  2011 {
  2011 	TileIndex tile = t->xy;
  2012 	TileIndex tile = t->xy;
  2012 
  2013 
  2013 	if (CircularTileSearch(tile, 9, SearchTileForStatue, t->index))
  2014 	if (CircularTileSearch(tile, 9, SearchTileForStatue, t->index))
  2014 		SETBIT(t->statues, _current_player); // Once found and built, "inform" the Town
  2015 		SetBit(t->statues, _current_player); // Once found and built, "inform" the Town
  2015 }
  2016 }
  2016 
  2017 
  2017 static void TownActionFundBuildings(Town* t)
  2018 static void TownActionFundBuildings(Town* t)
  2018 {
  2019 {
  2019 	/* Build next tick */
  2020 	/* Build next tick */
  2020 	t->grow_counter = 1;
  2021 	t->grow_counter = 1;
  2021 	/* If we were not already growing */
  2022 	/* If we were not already growing */
  2022 	SETBIT(t->flags12, TOWN_IS_FUNDED);
  2023 	SetBit(t->flags12, TOWN_IS_FUNDED);
  2023 	/* And grow for 3 months */
  2024 	/* And grow for 3 months */
  2024 	t->fund_buildings_months = 3;
  2025 	t->fund_buildings_months = 3;
  2025 }
  2026 }
  2026 
  2027 
  2027 static void TownActionBuyRights(Town* t)
  2028 static void TownActionBuyRights(Town* t)
  2095 
  2096 
  2096 	if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
  2097 	if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
  2097 
  2098 
  2098 	t = GetTown(p1);
  2099 	t = GetTown(p1);
  2099 
  2100 
  2100 	if (!HASBIT(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR;
  2101 	if (!HasBit(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR;
  2101 
  2102 
  2102 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  2103 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  2103 
  2104 
  2104 	cost.AddCost((_price.build_industry >> 8) * _town_action_costs[p2]);
  2105 	cost.AddCost((_price.build_industry >> 8) * _town_action_costs[p2]);
  2105 
  2106 
  2137 					t->ratings[st->owner] -= 15;
  2138 					t->ratings[st->owner] -= 15;
  2138 			}
  2139 			}
  2139 		}
  2140 		}
  2140 	}
  2141 	}
  2141 
  2142 
  2142 	CLRBIT(t->flags12, TOWN_IS_FUNDED);
  2143 	ClrBit(t->flags12, TOWN_IS_FUNDED);
  2143 	if (_patches.town_growth_rate == 0 && t->fund_buildings_months == 0) return;
  2144 	if (_patches.town_growth_rate == 0 && t->fund_buildings_months == 0) return;
  2144 
  2145 
  2145 	/** Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the
  2146 	/** Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the
  2146 	 * number of times towns are processed before a new building is built. */
  2147 	 * number of times towns are processed before a new building is built. */
  2147 	static const uint16 _grow_count_values[2][6] = {
  2148 	static const uint16 _grow_count_values[2][6] = {
  2174 
  2175 
  2175 	t->growth_rate = m / (t->num_houses / 50 + 1);
  2176 	t->growth_rate = m / (t->num_houses / 50 + 1);
  2176 	if (m <= t->grow_counter)
  2177 	if (m <= t->grow_counter)
  2177 		t->grow_counter = m;
  2178 		t->grow_counter = m;
  2178 
  2179 
  2179 	SETBIT(t->flags12, TOWN_IS_FUNDED);
  2180 	SetBit(t->flags12, TOWN_IS_FUNDED);
  2180 }
  2181 }
  2181 
  2182 
  2182 static void UpdateTownAmounts(Town *t)
  2183 static void UpdateTownAmounts(Town *t)
  2183 {
  2184 {
  2184 	/* Using +1 here to prevent overflow and division by zero */
  2185 	/* Using +1 here to prevent overflow and division by zero */
  2264 			!IsValidPlayer(_current_player) ||
  2265 			!IsValidPlayer(_current_player) ||
  2265 			(_cheats.magic_bulldozer.value && add < 0)) {
  2266 			(_cheats.magic_bulldozer.value && add < 0)) {
  2266 		return;
  2267 		return;
  2267 	}
  2268 	}
  2268 
  2269 
  2269 	SETBIT(t->have_ratings, _current_player);
  2270 	SetBit(t->have_ratings, _current_player);
  2270 
  2271 
  2271 	rating = t->ratings[_current_player];
  2272 	rating = t->ratings[_current_player];
  2272 
  2273 
  2273 	if (add < 0) {
  2274 	if (add < 0) {
  2274 		if (rating > max) {
  2275 		if (rating > max) {