Codechange: Store objectspecs in std::vector instead of flat array.

(cherry picked from commit 1ae263c5467105820e4d770b3d57d25ba01e22b1)
pull/484/head
Peter Nelson 2 years ago committed by Jonathan G Rennison
parent d34e1bbbc1
commit 74e4556a85

@ -2885,7 +2885,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, co
case A0RPI_GLOBALVAR_LIGHTHOUSE_GENERATE_AMOUNT:
case A0RPI_GLOBALVAR_TRANSMITTER_GENERATE_AMOUNT: {
if (MappedPropertyLengthMismatch(buf, 1, mapping_entry)) break;
extern ObjectSpec _object_specs[NUM_OBJECTS];
extern std::vector<ObjectSpec> _object_specs;
ObjectType type = (prop == A0RPI_GLOBALVAR_LIGHTHOUSE_GENERATE_AMOUNT) ? OBJECT_LIGHTHOUSE : OBJECT_TRANSMITTER;
_object_specs[type].generate_amount = buf->ReadByte();
break;

@ -313,10 +313,11 @@ void ObjectOverrideManager::SetEntitySpec(ObjectSpec *spec)
return;
}
extern ObjectSpec _object_specs[NUM_OBJECTS];
extern std::vector<ObjectSpec> _object_specs;
/* Now that we know we can use the given id, copy the spec to its final destination. */
memcpy(&_object_specs[type], spec, sizeof(*spec));
if (type >= _object_specs.size()) _object_specs.resize(type + 1);
_object_specs[type] = *spec;
}
/**

@ -31,7 +31,12 @@ ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJEC
extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
/** All the object specifications. */
ObjectSpec _object_specs[NUM_OBJECTS];
std::vector<ObjectSpec> _object_specs;
size_t ObjectSpec::Count()
{
return _object_specs.size();
}
/**
* Get the specification associated with a specific ObjectType.
@ -40,7 +45,11 @@ ObjectSpec _object_specs[NUM_OBJECTS];
*/
/* static */ const ObjectSpec *ObjectSpec::Get(ObjectType index)
{
/* Empty object if index is out of range -- this might happen if NewGRFs are changed. */
static ObjectSpec empty = {};
assert(index < NUM_OBJECTS);
if (index >= _object_specs.size()) return &empty;
return &_object_specs[index];
}
@ -90,7 +99,7 @@ bool ObjectSpec::IsAvailable() const
*/
uint ObjectSpec::Index() const
{
return this - _object_specs;
return this - _object_specs.data();
}
/**
@ -109,14 +118,13 @@ uint ObjectSpec::Index() const
void ResetObjects()
{
/* Clean the pool. */
for (uint16 i = 0; i < NUM_OBJECTS; i++) {
_object_specs[i] = {};
}
_object_specs.clear();
/* And add our originals. */
MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
_object_specs.resize(lengthof(_original_objects));
for (uint16 i = 0; i < lengthof(_original_objects); i++) {
_object_specs[i] = _original_objects[i];
_object_specs[i].grf_prop.local_id = i;
}

@ -134,6 +134,7 @@ struct ObjectSpec {
bool IsAvailable() const;
uint Index() const;
static size_t Count();
static const ObjectSpec *Get(ObjectType index);
static const ObjectSpec *GetByTile(TileIndex tile);

@ -269,7 +269,7 @@ CommandCost CmdBuildObject(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
CommandCost cost(EXPENSES_CONSTRUCTION);
ObjectType type = (ObjectType)GB(p1, 0, 16);
if (type >= NUM_OBJECTS) return CMD_ERROR;
if (type >= ObjectSpec::Count()) return CMD_ERROR;
uint8 view = GB(p2, 0, 2);
const ObjectSpec *spec = ObjectSpec::Get(type);
if (_game_mode == GM_NORMAL && !spec->IsAvailable() && !_generating_world) return CMD_ERROR;
@ -521,7 +521,7 @@ CommandCost CmdBuildObjectArea(TileIndex tile, DoCommandFlag flags, uint32 p1, u
if (!_settings_game.construction.build_object_area_permitted) return_cmd_error(STR_BUILD_OBJECT_NOT_PERMITTED_BULK);
ObjectType type = (ObjectType)GB(p2, 3, 16);
if (type >= NUM_OBJECTS) return CMD_ERROR;
if (type >= ObjectSpec::Count()) return CMD_ERROR;
uint8 view = GB(p2, 1, 2);
const ObjectSpec *spec = ObjectSpec::Get(type);
if (view >= spec->views) return CMD_ERROR;
@ -1085,7 +1085,7 @@ static bool TryBuildTransmitter()
void GenerateObjects()
{
/* Set a guestimate on how much we progress */
SetGeneratingWorldProgress(GWP_OBJECT, NUM_OBJECTS);
SetGeneratingWorldProgress(GWP_OBJECT, (uint)ObjectSpec::Count());
/* Determine number of water tiles at map border needed for freeform_edges */
uint num_water_tiles = 0;
@ -1101,7 +1101,7 @@ void GenerateObjects()
}
/* Iterate over all possible object types */
for (uint i = 0; i < NUM_OBJECTS; i++) {
for (uint i = 0; i < ObjectSpec::Count(); i++) {
const ObjectSpec *spec = ObjectSpec::Get(i);
/* Continue, if the object was never available till now or shall not be placed */

@ -273,11 +273,11 @@ 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 < NUM_OBJECTS; i++) {
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>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
height[spec->views / 4] = std::max<int>(spec->height, height[spec->views / 4]);
}
/* Determine the pixel heights. */

@ -18,7 +18,7 @@
/* static */ bool ScriptObjectType::IsValidObjectType(ObjectType object_type)
{
if (object_type >= NUM_OBJECTS) return false;
if (object_type >= ObjectSpec::Count()) return false;
return ObjectSpec::Get(object_type)->IsEverAvailable();
}

@ -15,7 +15,7 @@
ScriptObjectTypeList::ScriptObjectTypeList()
{
for (int i = 0; i < NUM_OBJECTS; i++) {
for (int i = 0; i < ObjectSpec::Count(); i++) {
const ObjectSpec *spec = ObjectSpec::Get(i);
if (!spec->IsEverAvailable()) continue;
this->AddItem(i);

Loading…
Cancel
Save