Changeset - r27037:fd6e1ed8dd82
[Not reviewed]
master
0 5 0
Peter Nelson - 23 months ago 2023-01-15 00:04:53
peter1138@openttd.org
Change: Expose ObjectSpec vector to simplify iteration.
5 files changed with 21 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/newgrf_object.cpp
Show inline comments
 
@@ -31,6 +31,11 @@ extern const ObjectSpec _original_object
 
/** All the object specifications. */
 
std::vector<ObjectSpec> _object_specs;
 

	
 
const std::vector<ObjectSpec> &ObjectSpec::Specs()
 
{
 
	return _object_specs;
 
}
 

	
 
size_t ObjectSpec::Count()
 
{
 
	return _object_specs.size();
src/newgrf_object.h
Show inline comments
 
@@ -99,6 +99,7 @@ struct ObjectSpec {
 
	bool IsAvailable() const;
 
	uint Index() const;
 

	
 
	static const std::vector<ObjectSpec> &Specs();
 
	static size_t Count();
 
	static const ObjectSpec *Get(ObjectType index);
 
	static const ObjectSpec *GetByTile(TileIndex tile);
src/object_cmd.cpp
Show inline comments
 
@@ -808,21 +808,20 @@ void GenerateObjects()
 
	}
 

	
 
	/* Iterate over all possible object types */
 
	for (uint i = 0; i < ObjectSpec::Count(); i++) {
 
		const ObjectSpec *spec = ObjectSpec::Get(i);
 
	for (const auto &spec : ObjectSpec::Specs()) {
 

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

	
 
		uint16 amount = spec->generate_amount;
 
		uint16 amount = spec.generate_amount;
 

	
 
		/* Scale by map size */
 
		if ((spec->flags & OBJECT_FLAG_SCALE_BY_WATER) && _settings_game.construction.freeform_edges) {
 
		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 = Map::ScaleBySize1D(amount * num_water_tiles) / (2 * Map::MaxY() + 2 * Map::MaxX() - 6);
 
		} else if (spec->flags & OBJECT_FLAG_SCALE_BY_WATER) {
 
		} else if (spec.flags & OBJECT_FLAG_SCALE_BY_WATER) {
 
			amount = Map::ScaleBySize1D(amount);
 
		} else {
 
			amount = Map::ScaleBySize(amount);
 
@@ -830,7 +829,7 @@ void GenerateObjects()
 

	
 
		/* Now try to place the requested amount of this object */
 
		for (uint j = Map::ScaleBySize(1000); j != 0 && amount != 0 && Object::CanAllocateItem(); j--) {
 
			switch (i) {
 
			switch (spec.Index()) {
 
				case OBJECT_TRANSMITTER:
 
					if (TryBuildTransmitter()) amount--;
 
					break;
 
@@ -840,8 +839,8 @@ void GenerateObjects()
 
					break;
 

	
 
				default:
 
					uint8 view = RandomRange(spec->views);
 
					if (CmdBuildObject(DC_EXEC | DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, RandomTile(), i, view).Succeeded()) amount--;
 
					uint8 view = RandomRange(spec.views);
 
					if (CmdBuildObject(DC_EXEC | DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, RandomTile(), spec.Index(), view).Succeeded()) amount--;
 
					break;
 
			}
 
		}
src/object_gui.cpp
Show inline comments
 
@@ -275,11 +275,10 @@ public:
 
				uint height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
 

	
 
				/* Get the height and view information. */
 
				for (int i = 0; i < ObjectSpec::Count(); i++) {
 
					const ObjectSpec *spec = ObjectSpec::Get(i);
 
					if (!spec->IsEverAvailable()) continue;
 
					two_wide |= spec->views >= 2;
 
					height[spec->views / 4] = std::max<int>(spec->height, height[spec->views / 4]);
 
				for (const auto &spec : ObjectSpec::Specs()) {
 
					if (!spec.IsEverAvailable()) continue;
 
					two_wide |= spec.views >= 2;
 
					height[spec.views / 4] = std::max<int>(spec.height, height[spec.views / 4]);
 
				}
 

	
 
				/* Determine the pixel heights. */
src/script/api/script_objecttypelist.cpp
Show inline comments
 
@@ -15,9 +15,8 @@
 

	
 
ScriptObjectTypeList::ScriptObjectTypeList()
 
{
 
	for (int i = 0; i < ObjectSpec::Count(); i++) {
 
		const ObjectSpec *spec = ObjectSpec::Get(i);
 
		if (!spec->IsEverAvailable()) continue;
 
		this->AddItem(i);
 
	for (const auto &spec : ObjectSpec::Specs()) {
 
		if (!spec.IsEverAvailable()) continue;
 
		this->AddItem(spec.Index());
 
	}
 
}
0 comments (0 inline, 0 general)