Changeset - r19266:a55aa4bf5bd3
[Not reviewed]
master
0 5 0
frosch - 12 years ago 2012-04-22 16:28:27
frosch@openttd.org
(svn r24169) -Add: Make NewGRFClass distinguish between defined specs and specs visible for the user.
5 files changed with 42 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/newgrf_airport.cpp
Show inline comments
 
@@ -22,24 +22,30 @@
 
 * This includes initialising the defaults classes with an empty
 
 * entry, for standard airports.
 
 */
 
template <typename Tspec, typename Tid, Tid Tmax>
 
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
 
{
 
	AirportClass::Get(AirportClass::Allocate('SMAL'))->name = STR_AIRPORT_CLASS_SMALL;
 
	AirportClass::Get(AirportClass::Allocate('LARG'))->name = STR_AIRPORT_CLASS_LARGE;
 
	AirportClass::Get(AirportClass::Allocate('HUB_'))->name = STR_AIRPORT_CLASS_HUB;
 
	AirportClass::Get(AirportClass::Allocate('HELI'))->name = STR_AIRPORT_CLASS_HELIPORTS;
 
}
 

	
 
template <typename Tspec, typename Tid, Tid Tmax>
 
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
 
{
 
	return true;
 
}
 

	
 
INSTANTIATE_NEWGRF_CLASS_METHODS(AirportClass, AirportSpec, AirportClassID, APC_MAX)
 

	
 

	
 
AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);
 

	
 
AirportSpec AirportSpec::specs[NUM_AIRPORTS]; ///< Airport specifications.
 

	
 
/**
 
 * Retrieve airport spec for the given airport. If an override is available
 
 *  it is returned.
 
 * @param type index of airport
 
 * @return A pointer to the corresponding AirportSpec
src/newgrf_class.h
Show inline comments
 
@@ -12,47 +12,54 @@
 
#ifndef NEWGRF_CLASS_H
 
#define NEWGRF_CLASS_H
 

	
 
#include "strings_type.h"
 

	
 
/**
 
 * Struct containing information relating to NewGRF classes for stations and airports.
 
 */
 
template <typename Tspec, typename Tid, Tid Tmax>
 
struct NewGRFClass {
 
private:
 
	uint count;       ///< Number of specs in this class.
 
	uint ui_count;    ///< Number of specs in this class potentially available to the user.
 
	Tspec **spec;     ///< Array of specifications.
 

	
 
	/**
 
	 * The actual classes.
 
	 * @note We store pointers to membes of this array in various places outside this class (e.g. to 'name' for GRF string resolving).
 
	 *       Thus this must be a static array, and cannot be a self-resizing SmallVector or similar.
 
	 */
 
	static NewGRFClass<Tspec, Tid, Tmax> classes[Tmax];
 

	
 
	void ResetClass();
 

	
 
	/** Initialise the defaults. */
 
	static void InsertDefaults();
 

	
 
public:
 
	uint32 global_id; ///< Global ID for class, e.g. 'DFLT', 'WAYP', etc.
 
	StringID name;    ///< Name of this class.
 

	
 
	void Insert(Tspec *spec);
 

	
 
	/** Get the number of allocated specs within the class. */
 
	uint GetSpecCount() const { return this->count; }
 
	/** Get the number of potentially user-available specs within the class. */
 
	uint GetUISpecCount() const { return this->ui_count; }
 

	
 
	const Tspec *GetSpec(uint index) const;
 

	
 
	/** Check whether the spec will be available to the user at some point in time. */
 
	bool IsUIAvailable(uint index) const;
 

	
 
	static void Reset();
 
	static Tid Allocate(uint32 global_id);
 
	static void Assign(Tspec *spec);
 
	static uint GetClassCount();
 
	static uint GetUIClassCount();
 
	static NewGRFClass *Get(Tid cls_id);
 

	
 
	static const Tspec *GetByGrf(uint32 grfid, byte local_id, int *index);
 
};
 

	
 
#endif /* NEWGRF_CLASS_H */
src/newgrf_class_func.h
Show inline comments
 
@@ -22,24 +22,25 @@
 
	type NewGRFClass<Tspec, Tid, Tmax>
 

	
 
/** Instantiate the array. */
 
template <typename Tspec, typename Tid, Tid Tmax>
 
NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
 

	
 
/** Reset the class, i.e. clear everything. */
 
DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
 
{
 
	this->global_id = 0;
 
	this->name      = STR_EMPTY;
 
	this->count     = 0;
 
	this->ui_count  = 0;
 

	
 
	free(this->spec);
 
	this->spec = NULL;
 
}
 

	
 
/** Reset the classes, i.e. clear everything. */
 
DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
 
{
 
	for (Tid i = (Tid)0; i < Tmax; i++) {
 
		classes[i].ResetClass();
 
	}
 

	
 
@@ -71,24 +72,26 @@ DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocat
 
}
 

	
 
/**
 
 * Insert a spec into the class.
 
 * @param spec The spec to insert.
 
 */
 
DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
 
{
 
	uint i = this->count++;
 
	this->spec = ReallocT(this->spec, this->count);
 

	
 
	this->spec[i] = spec;
 

	
 
	if (this->IsUIAvailable(i)) this->ui_count++;
 
}
 

	
 
/**
 
 * Assign a spec to one of the classes.
 
 * @param spec The spec to assign.
 
 * @note The spec must have a valid class id set.
 
 */
 
DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
 
{
 
	assert(spec->cls_id < Tmax);
 
	Get(spec->cls_id)->Insert(spec);
 
}
 
@@ -108,24 +111,37 @@ NewGRFClass<Tspec, Tid, Tmax> *NewGRFCla
 
/**
 
 * Get the number of allocated classes.
 
 * @return The number of classes.
 
 */
 
DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount()
 
{
 
	uint i;
 
	for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
 
	return i;
 
}
 

	
 
/**
 
 * Get the number of classes available to the user.
 
 * @return The number of classes.
 
 */
 
DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount()
 
{
 
	uint cnt = 0;
 
	for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
 
		if (classes[i].GetUISpecCount() > 0) cnt++;
 
	}
 
	return cnt;
 
}
 

	
 
/**
 
 * Get a spec from the class at a given index.
 
 * @param index  The index where to find the spec.
 
 * @return The spec at given location.
 
 */
 
DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const
 
{
 
	/* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
 
	return index < this->GetSpecCount() ? this->spec[index] : NULL;
 
}
 

	
 
/**
 
 * Retrieve a spec by GRF location.
 
@@ -154,14 +170,15 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *
 

	
 
#undef DEFINE_NEWGRF_CLASS_METHOD
 

	
 
/** Force instantiation of the methods so we don't get linker errors. */
 
#define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
 
	template void name::ResetClass(); \
 
	template void name::Reset(); \
 
	template Tid name::Allocate(uint32 global_id); \
 
	template void name::Insert(Tspec *spec); \
 
	template void name::Assign(Tspec *spec); \
 
	template NewGRFClass<Tspec, Tid, Tmax> *name::Get(Tid cls_id); \
 
	template uint name::GetClassCount(); \
 
	template uint name::GetUIClassCount(); \
 
	template const Tspec *name::GetSpec(uint index) const; \
 
	template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);
src/newgrf_object.cpp
Show inline comments
 
@@ -104,24 +104,30 @@ template <typename Tspec, typename Tid, 
 

	
 
	ObjectClassID cls = ObjectClass::Allocate('LTHS');
 
	ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS;
 
	_object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
 
	ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
 

	
 
	cls = ObjectClass::Allocate('TRNS');
 
	ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS;
 
	_object_specs[OBJECT_TRANSMITTER].cls_id = cls;
 
	ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
 
}
 

	
 
template <typename Tspec, typename Tid, Tid Tmax>
 
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
 
{
 
	return this->GetSpec(index)->IsEverAvailable();
 
}
 

	
 
INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
 

	
 

	
 
static uint32 ObjectGetRandomBits(const ResolverObject *object)
 
{
 
	TileIndex t = object->u.object.tile;
 
	return IsValidTile(t) && IsTileType(t, MP_OBJECT) ? GetObjectRandomBits(t) : 0;
 
}
 

	
 
static uint32 ObjectGetTriggers(const ResolverObject *object)
 
{
 
	return 0;
src/newgrf_station.cpp
Show inline comments
 
@@ -31,24 +31,30 @@ template <typename Tspec, typename Tid, 
 
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
 
{
 
	/* Set up initial data */
 
	classes[0].global_id = 'DFLT';
 
	classes[0].name = STR_STATION_CLASS_DFLT;
 
	classes[0].Insert(NULL);
 

	
 
	classes[1].global_id = 'WAYP';
 
	classes[1].name = STR_STATION_CLASS_WAYP;
 
	classes[1].Insert(NULL);
 
}
 

	
 
template <typename Tspec, typename Tid, Tid Tmax>
 
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
 
{
 
	return true;
 
}
 

	
 
INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX)
 

	
 
static const uint MAX_SPECLIST = 255;
 

	
 
enum TriggerArea {
 
	TA_TILE,
 
	TA_PLATFORM,
 
	TA_WHOLE,
 
};
 

	
 
struct ETileArea : TileArea {
 
	ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta)
0 comments (0 inline, 0 general)