(svn r9850) -Codechange: Introduction of the Override/Substitute manager. Currently only used for newhouses.
Basically, it is more a gathering of IDs from grf files and ingame data.replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
parent
d2776ccde9
commit
3fb4003534
@ -0,0 +1,150 @@
|
||||
/* $Id$ */
|
||||
|
||||
/** @file newgrf_commons.cpp Implementation of the class OverrideManagerBase
|
||||
* and its descendance, present and futur
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "openttd.h"
|
||||
#include "town.h"
|
||||
#include "industry.h"
|
||||
#include "newgrf.h"
|
||||
#include "newgrf_commons.h"
|
||||
|
||||
/** Constructor of generic class
|
||||
* @param offset end of original data for this entity. i.e: houses = 110
|
||||
* @param maximum of entities this manager can deal with. i.e: houses = 512
|
||||
* @param invalid is the ID used to identify an invalid entity id
|
||||
*/
|
||||
OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
|
||||
{
|
||||
max_offset = offset;
|
||||
max_new_entities = maximum;
|
||||
invalid_ID = invalid;
|
||||
|
||||
mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
|
||||
entity_overrides = MallocT<uint16>(max_offset);
|
||||
memset(entity_overrides, invalid, sizeof(entity_overrides));
|
||||
}
|
||||
|
||||
/** Destructor of the generic class.
|
||||
* Frees allocated memory of constructor
|
||||
*/
|
||||
OverrideManagerBase::~OverrideManagerBase()
|
||||
{
|
||||
free(mapping_ID);
|
||||
free(entity_overrides);
|
||||
}
|
||||
|
||||
/** Since the entity IDs defined by the GRF file does not necessarily correlate
|
||||
* to those used by the game, the IDs used for overriding old entities must be
|
||||
* translated when the entity spec is set.
|
||||
* @param local_id id in grf file
|
||||
* @param entity_type original entity type
|
||||
*/
|
||||
void OverrideManagerBase::Add(uint8 local_id, uint entity_type)
|
||||
{
|
||||
assert(entity_type < max_offset);
|
||||
entity_overrides[entity_type] = local_id;
|
||||
}
|
||||
|
||||
/** Resets the mapping, which is used while initializing game */
|
||||
void OverrideManagerBase::ResetMapping()
|
||||
{
|
||||
memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
|
||||
}
|
||||
|
||||
/** Resets the override, which is used while initializing game */
|
||||
void OverrideManagerBase::ResetOverride()
|
||||
{
|
||||
for (uint16 i = 0; i < max_offset; i++) {
|
||||
entity_overrides[i] = invalid_ID;
|
||||
}
|
||||
}
|
||||
|
||||
/** Return the ID (if ever available) of a previously inserted entity.
|
||||
* @param grf_local_id ID of this enity withing the grfID
|
||||
* @param grfid ID of the grf file
|
||||
* @return the ID of the candidate, of the Invalid flag item ID
|
||||
*/
|
||||
uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid)
|
||||
{
|
||||
const EntityIDMapping *map;
|
||||
|
||||
for (uint16 id = max_offset; id < max_new_entities; id++) {
|
||||
map = &mapping_ID[id];
|
||||
if (map->entity_id == grf_local_id && map->grfid == grfid) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
return invalid_ID;
|
||||
}
|
||||
|
||||
/** Reserves a place in the mapping array for an entity to be installed
|
||||
* @param grf_local_id is an arbitrary id given by the grf's author. Also known as setid
|
||||
* @param grfid is the id of the grf file itself
|
||||
* @param substitute_id is the original entity from which data is copied for the new one
|
||||
* @return the proper usable slot id, or invalid marker if none is found
|
||||
*/
|
||||
uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
|
||||
{
|
||||
uint16 id = this->GetID(grf_local_id, grfid);
|
||||
EntityIDMapping *map;
|
||||
|
||||
/* Look to see if this entity has already been added. This is done
|
||||
* separately from the loop below in case a GRF has been deleted, and there
|
||||
* are any gaps in the array.
|
||||
*/
|
||||
if (id != invalid_ID) {
|
||||
return id;
|
||||
}
|
||||
|
||||
/* This entity hasn't been defined before, so give it an ID now. */
|
||||
for (id = max_offset; id < max_new_entities; id++) {
|
||||
map = &mapping_ID[id];
|
||||
|
||||
if (map->entity_id == 0 && map->grfid == 0) {
|
||||
map->entity_id = grf_local_id;
|
||||
map->grfid = grfid;
|
||||
map->substitute_id = substitute_id;
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
return invalid_ID;
|
||||
}
|
||||
|
||||
/** Gives the substitute of the entity, as specified by the grf file
|
||||
* @param entity_id of the entity being queried
|
||||
* @return mapped id
|
||||
*/
|
||||
uint16 OverrideManagerBase::GetSubstituteID(byte entity_id)
|
||||
{
|
||||
return mapping_ID[entity_id].substitute_id;
|
||||
}
|
||||
|
||||
/** Install the specs into the HouseSpecs array
|
||||
* It will find itself the proper slot onwhich it will go
|
||||
* @param hs HouseSpec read from the grf file, ready for inclusion
|
||||
*/
|
||||
void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
|
||||
{
|
||||
HouseID house_id = this->AddEntityID(hs->local_id, hs->grffile->grfid, hs->substitute_id);
|
||||
|
||||
if (house_id == invalid_ID) {
|
||||
grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(&_house_specs[house_id], hs, sizeof(*hs));
|
||||
|
||||
/* Now add the overrides. */
|
||||
for (int i = 0; i != max_offset; i++) {
|
||||
HouseSpec *overridden_hs = GetHouseSpecs(i);
|
||||
|
||||
if (entity_overrides[i] != hs->local_id) continue;
|
||||
|
||||
overridden_hs->override = house_id;
|
||||
entity_overrides[i] = invalid_ID;
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/* $Id$ */
|
||||
|
||||
/** @file newgrf_commons.h This file simplyfies and embeds a common mechanism of
|
||||
* loading/saving and mapping of grf entities.
|
||||
*/
|
||||
|
||||
#ifndef NEWGRF_COMMONS_H
|
||||
#define NEWGRF_COMMONS_H
|
||||
|
||||
/**
|
||||
* Maps an entity id stored on the map to a GRF file.
|
||||
* Entities are objects used ingame (houses, industries, industry tiles) for
|
||||
* which we need to correlate the ids from the grf files with the ones in the
|
||||
* the savegames themselves.
|
||||
* An array of EntityIDMapping structs is saved with the savegame so
|
||||
* that those GRFs can be loaded in a different order, or removed safely. The
|
||||
* index in the array is the entity's ID stored on the map.
|
||||
*
|
||||
* The substitute ID is the ID of an original entity that should be used instead
|
||||
* if the GRF containing the new entity is not available.
|
||||
*/
|
||||
struct EntityIDMapping {
|
||||
uint32 grfid; ///< The GRF ID of the file the entity belongs to
|
||||
uint8 entity_id; ///< The entity ID within the GRF file
|
||||
uint8 substitute_id; ///< The (original) entity ID to use if this GRF is not available
|
||||
};
|
||||
|
||||
class OverrideManagerBase
|
||||
{
|
||||
protected:
|
||||
uint16 *entity_overrides;
|
||||
|
||||
uint16 max_offset; ///< what is the length of the original entity's array of specs
|
||||
uint16 max_new_entities; ///< what is the amount of entities, old and new summed
|
||||
|
||||
uint16 invalid_ID; ///< ID used to dected invalid entities;
|
||||
|
||||
virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
|
||||
public:
|
||||
EntityIDMapping *mapping_ID; ///< mapping of ids from grf files. Public out of convenience
|
||||
|
||||
OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
|
||||
virtual ~OverrideManagerBase();
|
||||
|
||||
void ResetOverride();
|
||||
void ResetMapping();
|
||||
|
||||
void Add(uint8 local_id, uint entity_type);
|
||||
|
||||
uint16 GetSubstituteID(byte entity_id);
|
||||
uint16 GetID(uint8 grf_local_id, uint32 grfid);
|
||||
|
||||
inline uint16 GetMaxMapping() { return max_new_entities; };
|
||||
inline uint16 GetMaxOffset() { return max_offset; };
|
||||
};
|
||||
|
||||
|
||||
struct HouseSpec;
|
||||
class HouseOverrideManager : public OverrideManagerBase
|
||||
{
|
||||
public:
|
||||
HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) : OverrideManagerBase(offset, maximum, invalid) {};
|
||||
void SetEntitySpec(const HouseSpec *hs);
|
||||
};
|
||||
|
||||
|
||||
extern HouseOverrideManager _house_mngr;
|
||||
|
||||
#endif /* NEWGRF_COMMONS_H */
|
Loading…
Reference in New Issue