src/town_cmd.cpp
branchNewGRF_ports
changeset 10994 cd9968b6f96b
parent 10991 d8811e327d12
equal deleted inserted replaced
10991:d8811e327d12 10994:cd9968b6f96b
  1640  * @param stage of construction (used for drawing)
  1640  * @param stage of construction (used for drawing)
  1641  * @param type of house. Index into house specs array
  1641  * @param type of house. Index into house specs array
  1642  * @param random_bits required for newgrf houses
  1642  * @param random_bits required for newgrf houses
  1643  * @pre house can be built here
  1643  * @pre house can be built here
  1644  */
  1644  */
  1645 static inline void ClearMakeHouseTile(TileIndex tile, TownID tid, byte counter, byte stage, HouseID type, byte random_bits)
  1645 static inline void ClearMakeHouseTile(TileIndex tile, Town *t, byte counter, byte stage, HouseID type, byte random_bits)
  1646 {
  1646 {
  1647 	CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR);
  1647 	CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR);
  1648 
  1648 
  1649 	assert(CmdSucceeded(cc));
  1649 	assert(CmdSucceeded(cc));
  1650 
  1650 
  1651 	MakeHouseTile(tile, tid, counter, stage, type, random_bits);
  1651 	IncreaseBuildingCount(t, type);
       
  1652 	MakeHouseTile(tile, t->index, counter, stage, type, random_bits);
  1652 	if (GetHouseSpecs(type)->building_flags & BUILDING_IS_ANIMATED) AddAnimatedTile(tile);
  1653 	if (GetHouseSpecs(type)->building_flags & BUILDING_IS_ANIMATED) AddAnimatedTile(tile);
  1653 
  1654 
  1654 	MarkTileDirtyByTile(tile);
  1655 	MarkTileDirtyByTile(tile);
  1655 }
  1656 }
  1656 
  1657 
  1663  * @param stage of construction (used for drawing)
  1664  * @param stage of construction (used for drawing)
  1664  * @param type of house. Index into house specs array
  1665  * @param type of house. Index into house specs array
  1665  * @param random_bits required for newgrf houses
  1666  * @param random_bits required for newgrf houses
  1666  * @pre house can be built here
  1667  * @pre house can be built here
  1667  */
  1668  */
  1668 static void MakeTownHouse(TileIndex t, TownID tid, byte counter, byte stage, HouseID type, byte random_bits)
  1669 static void MakeTownHouse(TileIndex t, Town *town, byte counter, byte stage, HouseID type, byte random_bits)
  1669 {
  1670 {
  1670 	BuildingFlags size = GetHouseSpecs(type)->building_flags;
  1671 	BuildingFlags size = GetHouseSpecs(type)->building_flags;
  1671 
  1672 
  1672 	ClearMakeHouseTile(t, tid, counter, stage, type, random_bits);
  1673 	ClearMakeHouseTile(t, town, counter, stage, type, random_bits);
  1673 	if (size & BUILDING_2_TILES_Y)   ClearMakeHouseTile(t + TileDiffXY(0, 1), tid, counter, stage, ++type, random_bits);
  1674 	if (size & BUILDING_2_TILES_Y)   ClearMakeHouseTile(t + TileDiffXY(0, 1), town, counter, stage, ++type, random_bits);
  1674 	if (size & BUILDING_2_TILES_X)   ClearMakeHouseTile(t + TileDiffXY(1, 0), tid, counter, stage, ++type, random_bits);
  1675 	if (size & BUILDING_2_TILES_X)   ClearMakeHouseTile(t + TileDiffXY(1, 0), town, counter, stage, ++type, random_bits);
  1675 	if (size & BUILDING_HAS_4_TILES) ClearMakeHouseTile(t + TileDiffXY(1, 1), tid, counter, stage, ++type, random_bits);
  1676 	if (size & BUILDING_HAS_4_TILES) ClearMakeHouseTile(t + TileDiffXY(1, 1), town, counter, stage, ++type, random_bits);
  1676 }
  1677 }
  1677 
  1678 
  1678 
  1679 
  1679 /**
  1680 /**
  1680  * Checks if a house can be built here. Important is slope, bridge above
  1681  * Checks if a house can be built here. Important is slope, bridge above
  1927 				house = hs->override;
  1928 				house = hs->override;
  1928 				hs = GetHouseSpecs(house);
  1929 				hs = GetHouseSpecs(house);
  1929 			}
  1930 			}
  1930 
  1931 
  1931 			if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue;
  1932 			if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue;
  1932 
       
  1933 			if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
       
  1934 				uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile);
       
  1935 				if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) == 0) continue;
       
  1936 			}
       
  1937 		}
  1933 		}
  1938 
  1934 
  1939 		if (_cur_year < hs->min_year || _cur_year > hs->max_year) continue;
  1935 		if (_cur_year < hs->min_year || _cur_year > hs->max_year) continue;
  1940 
  1936 
  1941 		/* Special houses that there can be only one of. */
  1937 		/* Special houses that there can be only one of. */
  1945 			SetBit(oneof, TOWN_HAS_CHURCH);
  1941 			SetBit(oneof, TOWN_HAS_CHURCH);
  1946 		} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  1942 		} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  1947 			SetBit(oneof, TOWN_HAS_STADIUM);
  1943 			SetBit(oneof, TOWN_HAS_STADIUM);
  1948 		}
  1944 		}
  1949 
  1945 
  1950 		if (HASBITS(t->flags12 , oneof)) continue;
  1946 		if (HASBITS(t->flags12, oneof)) continue;
  1951 
  1947 
  1952 		/* Make sure there is no slope? */
  1948 		/* Make sure there is no slope? */
  1953 		bool noslope = (hs->building_flags & TILE_NOT_SLOPED) != 0;
  1949 		bool noslope = (hs->building_flags & TILE_NOT_SLOPED) != 0;
  1954 		if (noslope && slope != SLOPE_FLAT) continue;
  1950 		if (noslope && slope != SLOPE_FLAT) continue;
  1955 
  1951 
  1961 			if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SE)) continue;
  1957 			if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SE)) continue;
  1962 		} else {
  1958 		} else {
  1963 			/* 1x1 house checks are already done */
  1959 			/* 1x1 house checks are already done */
  1964 		}
  1960 		}
  1965 
  1961 
       
  1962 		if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
       
  1963 			uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile);
       
  1964 			if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) == 0) continue;
       
  1965 		}
       
  1966 
  1966 		/* build the house */
  1967 		/* build the house */
  1967 		t->num_houses++;
  1968 		t->num_houses++;
  1968 		IncreaseBuildingCount(t, house);
       
  1969 
  1969 
  1970 		/* Special houses that there can be only one of. */
  1970 		/* Special houses that there can be only one of. */
  1971 		t->flags12 |= oneof;
  1971 		t->flags12 |= oneof;
  1972 
  1972 
  1973 		byte construction_counter = 0;
  1973 		byte construction_counter = 0;
  1984 			} else {
  1984 			} else {
  1985 				construction_counter = GB(r, 2, 2);
  1985 				construction_counter = GB(r, 2, 2);
  1986 			}
  1986 			}
  1987 		}
  1987 		}
  1988 
  1988 
  1989 		MakeTownHouse(tile, t->index, construction_counter, construction_stage, house, Random());
  1989 		MakeTownHouse(tile, t, construction_counter, construction_stage, house, Random());
  1990 
  1990 
  1991 		return true;
  1991 		return true;
  1992 	}
  1992 	}
  1993 
  1993 
  1994 	return false;
  1994 	return false;
  1995 }
  1995 }
  1996 
  1996 
  1997 
  1997 
  1998 static void DoClearTownHouseHelper(TileIndex tile)
  1998 static void DoClearTownHouseHelper(TileIndex tile, Town *t, HouseID house)
  1999 {
  1999 {
  2000 	assert(IsTileType(tile, MP_HOUSE));
  2000 	assert(IsTileType(tile, MP_HOUSE));
       
  2001 	DecreaseBuildingCount(t, house);
  2001 	DoClearSquare(tile);
  2002 	DoClearSquare(tile);
  2002 	DeleteAnimatedTile(tile);
  2003 	DeleteAnimatedTile(tile);
  2003 }
  2004 }
  2004 
  2005 
  2005 /**
  2006 /**
  2044 	if (IsHouseCompleted(tile)) {
  2045 	if (IsHouseCompleted(tile)) {
  2045 		ChangePopulation(t, -hs->population);
  2046 		ChangePopulation(t, -hs->population);
  2046 	}
  2047 	}
  2047 
  2048 
  2048 	t->num_houses--;
  2049 	t->num_houses--;
  2049 	DecreaseBuildingCount(t, house);
       
  2050 
  2050 
  2051 	/* Clear flags for houses that only may exist once/town. */
  2051 	/* Clear flags for houses that only may exist once/town. */
  2052 	if (hs->building_flags & BUILDING_IS_CHURCH) {
  2052 	if (hs->building_flags & BUILDING_IS_CHURCH) {
  2053 		ClrBit(t->flags12, TOWN_HAS_CHURCH);
  2053 		ClrBit(t->flags12, TOWN_HAS_CHURCH);
  2054 	} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  2054 	} else if (hs->building_flags & BUILDING_IS_STADIUM) {
  2055 		ClrBit(t->flags12, TOWN_HAS_STADIUM);
  2055 		ClrBit(t->flags12, TOWN_HAS_STADIUM);
  2056 	}
  2056 	}
  2057 
  2057 
  2058 	/* Do the actual clearing of tiles */
  2058 	/* Do the actual clearing of tiles */
  2059 	uint eflags = hs->building_flags;
  2059 	uint eflags = hs->building_flags;
  2060 	DoClearTownHouseHelper(tile);
  2060 	DoClearTownHouseHelper(tile, t, house);
  2061 	if (eflags & BUILDING_2_TILES_X)   DoClearTownHouseHelper(tile + TileDiffXY(1, 0));
  2061 	if (eflags & BUILDING_2_TILES_Y)   DoClearTownHouseHelper(tile + TileDiffXY(0, 1), t, ++house);
  2062 	if (eflags & BUILDING_2_TILES_Y)   DoClearTownHouseHelper(tile + TileDiffXY(0, 1));
  2062 	if (eflags & BUILDING_2_TILES_X)   DoClearTownHouseHelper(tile + TileDiffXY(1, 0), t, ++house);
  2063 	if (eflags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1));
  2063 	if (eflags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1), t, ++house);
  2064 }
  2064 }
  2065 
  2065 
  2066 static bool IsUniqueTownName(const char *name)
  2066 static bool IsUniqueTownName(const char *name)
  2067 {
  2067 {
  2068 	const Town *t;
  2068 	const Town *t;