Changeset - r20832:34bd0d1c35f9
[Not reviewed]
master
0 2 0
planetmaker - 11 years ago 2013-10-17 21:41:17
planetmaker@openttd.org
(svn r25876) -Codechange: Unify object placement during map creation
2 files changed with 41 insertions and 21 deletions:
0 comments (0 inline, 0 general)
src/object_cmd.cpp
Show inline comments
 
@@ -703,47 +703,67 @@ static bool TryBuildTransmitter()
 
	}
 
	return false;
 
}
 

	
 
void GenerateObjects()
 
{
 
	if (_settings_game.game_creation.landscape == LT_TOYLAND) return;
 
	/* Set a guestimate on how much we progress */
 
	SetGeneratingWorldProgress(GWP_OBJECT, NUM_OBJECTS);
 

	
 
	/* add radio tower */
 
	int radiotower_to_build = ScaleByMapSize(15); // maximum number of radio towers on the map
 
	int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);
 

	
 
	/* Scale the amount of lighthouses with the amount of land at the borders. */
 
	if (_settings_game.construction.freeform_edges && lighthouses_to_build != 0) {
 
	/* Determine number of water tiles at map border needed for freeform_edges */
 
		uint num_water_tiles = 0;
 
	if (_settings_game.construction.freeform_edges) {
 
		for (uint x = 0; x < MapMaxX(); x++) {
 
			if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
 
			if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
 
		}
 
		for (uint y = 1; y < MapMaxY() - 1; y++) {
 
			if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
 
			if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
 
		}
 
		/* The -6 is because the top borders are MP_VOID (-2) and all corners
 
		 * are counted twice (-4). */
 
		lighthouses_to_build = lighthouses_to_build * num_water_tiles / (2 * MapMaxY() + 2 * MapMaxX() - 6);
 
	}
 

	
 
	SetGeneratingWorldProgress(GWP_OBJECT, radiotower_to_build + lighthouses_to_build);
 
	/* Iterate over all possible object types */
 
	for (uint i = 0; i < NUM_OBJECTS; i++) {
 
		const ObjectSpec *spec = ObjectSpec::Get(i);
 

	
 
		/* Continue, if the object was never available till now or shall not be placed */
 
		if (!spec->WasEverAvailable() || spec->generate_amount == 0) continue;
 

	
 
		uint16 amount = spec->generate_amount;
 

	
 
	for (uint i = ScaleByMapSize(1000); i != 0 && Object::CanAllocateItem(); i--) {
 
		if (!TryBuildTransmitter()) continue;
 
		IncreaseGeneratingWorldProgress(GWP_OBJECT);
 
		if (--radiotower_to_build == 0) break;
 
		/* Scale by map size */
 
		if ((spec->flags & OBJECT_FLAG_SCALE_BY_WATER) && _settings_game.construction.freeform_edges) {
 
			/* Scale the amount of lighthouses with the amount of land at the borders.
 
			 * The -6 is because the top borders are MP_VOID (-2) and all corners
 
			 * are counted twice (-4). */
 
			amount = ScaleByMapSize1D(amount * num_water_tiles) / (2 * MapMaxY() + 2 * MapMaxX() - 6);
 
		} else if (spec->flags & OBJECT_FLAG_SCALE_BY_WATER) {
 
			amount = ScaleByMapSize1D(amount);
 
		} else {
 
			amount = ScaleByMapSize(amount);
 
	}
 

	
 
	/* add lighthouses */
 
	for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0 && Object::CanAllocateItem(); loop_count++) {
 
		if (!TryBuildLightHouse()) continue;
 
		/* Now try to place the requested amount of this object */
 
		for (uint j = ScaleByMapSize(1000); j != 0 && amount != 0 && Object::CanAllocateItem(); j--) {
 
			switch (i) {
 
				case OBJECT_LIGHTHOUSE:
 
					if (TryBuildTransmitter()) amount--;
 
					break;
 

	
 
				case OBJECT_TRANSMITTER:
 
					if (TryBuildLightHouse()) amount--;
 
					break;
 

	
 
				default:
 
					uint8 view = RandomRange(spec->views);
 
					if (CmdBuildObject(RandomTile(), DC_EXEC | DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, i, view, NULL).Succeeded()) amount--;
 
					break;
 
			}
 
		}
 
		IncreaseGeneratingWorldProgress(GWP_OBJECT);
 
		lighthouses_to_build--;
 
	}
 
}
 

	
 
static void ChangeTileOwner_Object(TileIndex tile, Owner old_owner, Owner new_owner)
 
{
 
	if (!IsTileOwner(tile, old_owner)) return;
src/table/object_land.h
Show inline comments
 
@@ -120,13 +120,13 @@ static const DrawTileSprites _object_hq[
 
	TILE_SPRITE_LINE(SPR_HUGEHQ_EAST_GROUND,   _object_hq_huge_east)
 
	TILE_SPRITE_LINE(SPR_HUGEHQ_SOUTH,         _object_nothing)
 
};
 

	
 
#undef TILE_SPRITE_LINE
 

	
 
#define M(name, size, build_cost_multiplier, clear_cost_multiplier, height, climate, gen_amount, flags) { GRFFilePropsBase<2>(), INVALID_OBJECT_CLASS, name, climate, size, build_cost_multiplier, clear_cost_multiplier, 0, 0, flags, {0, 0, 0, 0}, 0, height, 1, gen_amount, true }
 
#define M(name, size, build_cost_multiplier, clear_cost_multiplier, height, climate, gen_amount, flags) { GRFFilePropsBase<2>(), INVALID_OBJECT_CLASS, name, climate, size, build_cost_multiplier, clear_cost_multiplier, 0, 0xFFFFFFFF, flags, {0, 0, 0, 0}, 0, height, 1, gen_amount, true }
 

	
 
/* Climates
 
 * T = Temperate
 
 * A = Sub-Arctic
 
 * S = Sub-Tropic
 
 * Y = Toyland */
 
@@ -134,13 +134,13 @@ static const DrawTileSprites _object_hq[
 
#define A 2
 
#define S 4
 
#define Y 8
 
/** Specification of the original object structures. */
 
extern const ObjectSpec _original_objects[] = {
 
	M(STR_LAI_OBJECT_DESCRIPTION_TRANSMITTER,          0x11,   0,   0, 10, T|A|S  , 15, OBJECT_FLAG_CANNOT_REMOVE | OBJECT_FLAG_ONLY_IN_SCENEDIT),
 
	M(STR_LAI_OBJECT_DESCRIPTION_LIGHTHOUSE,           0x11,   0,   0,  8, T|A    ,  7, OBJECT_FLAG_CANNOT_REMOVE | OBJECT_FLAG_ONLY_IN_SCENEDIT | OBJECT_FLAG_SCALE_BY_WATER),
 
	M(STR_LAI_OBJECT_DESCRIPTION_LIGHTHOUSE,           0x11,   0,   0,  8, T|A    ,  8, OBJECT_FLAG_CANNOT_REMOVE | OBJECT_FLAG_ONLY_IN_SCENEDIT | OBJECT_FLAG_SCALE_BY_WATER),
 
	M(STR_TOWN_BUILDING_NAME_STATUE_1,                 0x11,   0,   0,  5, T|S|A|Y,  0, OBJECT_FLAG_CANNOT_REMOVE | OBJECT_FLAG_ONLY_IN_GAME | OBJECT_FLAG_ONLY_IN_SCENEDIT), // Yes, we disallow building this everywhere. Happens in "special" case!
 
	M(STR_LAI_OBJECT_DESCRIPTION_COMPANY_OWNED_LAND,   0x11,  10,  10,  0, T|S|A|Y,  0, OBJECT_FLAG_AUTOREMOVE | OBJECT_FLAG_ONLY_IN_GAME | OBJECT_FLAG_CLEAR_INCOME | OBJECT_FLAG_HAS_NO_FOUNDATION ), // Only non-silly use case is to use it when you cannot build a station, so disallow bridges
 
	M(STR_LAI_OBJECT_DESCRIPTION_COMPANY_HEADQUARTERS, 0x22,   0,   0,  7, T|S|A|Y,  0, OBJECT_FLAG_CANNOT_REMOVE | OBJECT_FLAG_ONLY_IN_GAME),
 
};
 

	
 
#undef M
0 comments (0 inline, 0 general)