Changeset - r4348:231a69d3add0
[Not reviewed]
master
0 4 0
truelight - 18 years ago 2006-08-22 16:22:07
truelight@openttd.org
(svn r6049) -Codechange: forgot EngineRenew in r6047
-Codechange: cleaned up the EngineRenew code a bit (coding style mostly)
-Codechange: forgot the correct comment in station_cmd
-Codechange: move pool-stuff to engine.h, like we always do
4 files changed with 54 insertions and 35 deletions:
0 comments (0 inline, 0 general)
engine.c
Show inline comments
 
@@ -453,19 +453,13 @@ enum {
 

	
 
MemoryPool _engine_renew_pool = { "EngineRe", ENGINE_RENEW_POOL_MAX_BLOCKS, ENGINE_RENEW_POOL_BLOCK_SIZE_BITS, sizeof(EngineRenew), &EngineRenewPoolNewBlock, NULL, 0, 0, NULL };
 

	
 
static inline uint16 GetEngineRenewPoolSize(void)
 
{
 
	return _engine_renew_pool.total_items;
 
}
 

	
 
#define FOR_ALL_ENGINE_RENEWS_FROM(er, start) for (er = GetEngineRenew(start); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL) if (er->from != INVALID_ENGINE)
 
#define FOR_ALL_ENGINE_RENEWS(er) FOR_ALL_ENGINE_RENEWS_FROM(er, 0)
 

	
 
static void EngineRenewPoolNewBlock(uint start_item)
 
{
 
	EngineRenew *er;
 

	
 
	FOR_ALL_ENGINE_RENEWS_FROM(er, start_item) {
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 *  TODO - This is just a temporary stage, this will be removed. */
 
	for (er = GetEngineRenew(start_item); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL) if (er->from != INVALID_ENGINE) {
 
		er->index = start_item++;
 
		er->from = INVALID_ENGINE;
 
	}
 
@@ -476,12 +470,14 @@ static EngineRenew *AllocateEngineRenew(
 
{
 
	EngineRenew *er;
 

	
 
	FOR_ALL_ENGINE_RENEWS(er) {
 
		if (er->from == INVALID_ENGINE) {
 
			er->to = INVALID_ENGINE;
 
			er->next = NULL;
 
			return er;
 
		}
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 *  TODO - This is just a temporary stage, this will be removed. */
 
	for (er = GetEngineRenew(0); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL) if (er->from != INVALID_ENGINE) {
 
		if (IsValidEngineRenew(er)) continue;
 

	
 
		er->to = INVALID_ENGINE;
 
		er->next = NULL;
 
		return er;
 
	}
 

	
 
	/* Check if we can add a block to the pool */
 
@@ -495,7 +491,8 @@ static EngineRenew *AllocateEngineRenew(
 
 * engine type from the given renewlist */
 
static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine)
 
{
 
	EngineRenew *er = (EngineRenew*)erl; /* Fetch first element */
 
	EngineRenew *er = (EngineRenew *)erl;
 

	
 
	while (er) {
 
		if (er->from == engine) return er;
 
		er = er->next;
 
@@ -505,12 +502,13 @@ static EngineRenew *GetEngineReplacement
 

	
 
void RemoveAllEngineReplacement(EngineRenewList *erl)
 
{
 
	EngineRenew *er = (EngineRenew*)(*erl); /* Fetch first element */
 
	EngineRenew *er = (EngineRenew *)(*erl);
 

	
 
	while (er) {
 
		er->from = INVALID_ENGINE; /* "Deallocate" all elements */
 
		er->from = INVALID_ENGINE; // "Deallocate" elements
 
		er = er->next;
 
	}
 
	*erl = NULL; /* Empty list */
 
	*erl = NULL; // Empty list
 
}
 

	
 
EngineID EngineReplacement(EngineRenewList erl, EngineID engine)
 
@@ -523,7 +521,7 @@ int32 AddEngineReplacement(EngineRenewLi
 
{
 
	EngineRenew *er;
 

	
 
	// Check if the old vehicle is already in the list
 
	/* Check if the old vehicle is already in the list */
 
	er = GetEngineReplacement(*erl, old_engine);
 
	if (er != NULL) {
 
		if (flags & DC_EXEC) er->to = new_engine;
 
@@ -536,9 +534,10 @@ int32 AddEngineReplacement(EngineRenewLi
 
	if (flags & DC_EXEC) {
 
		er->from = old_engine;
 
		er->to = new_engine;
 
		er->next = (EngineRenew*)(*erl); /* Resolve the first element in the list */
 

	
 
		*erl = (EngineRenewList)er; /* Insert before the first element */
 
		/* Insert before the first element */
 
		er->next = (EngineRenew *)(*erl);
 
		*erl = (EngineRenewList)er;
 
	}
 

	
 
	return 0;
 
@@ -546,27 +545,29 @@ int32 AddEngineReplacement(EngineRenewLi
 

	
 
int32 RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, uint32 flags)
 
{
 
	EngineRenew *er = (EngineRenew*)(*erl); /* Start at the first element */
 
	EngineRenew *er = (EngineRenew *)(*erl);
 
	EngineRenew *prev = NULL;
 

	
 
	while (er)
 
	{
 
		if (er->from == engine) {
 
			if (flags & DC_EXEC) {
 
				if (prev == NULL) { /* First element */
 
					(*erl) = (EngineRenewList)er->next; /* The second becomes the new first element */
 
				if (prev == NULL) { // First element
 
					/* The second becomes the new first element */
 
					*erl = (EngineRenewList)er->next;
 
				} else {
 
					prev->next = er->next; /* Cut this element out */
 
					/* Cut this element out */
 
					prev->next = er->next;
 
				}
 
				er->from = INVALID_ENGINE; /* Deallocate */
 
				er->from = INVALID_ENGINE; // Deallocate
 
			}
 
			return 0;
 
		}
 
		prev = er;
 
		er = er->next; /* Look at next element */
 
		er = er->next;
 
	}
 

	
 
	return CMD_ERROR; /* Not found? */
 
	return CMD_ERROR;
 
}
 

	
 
static const SaveLoad _engine_renew_desc[] = {
 
@@ -583,10 +584,8 @@ static void Save_ERNW(void)
 
	EngineRenew *er;
 

	
 
	FOR_ALL_ENGINE_RENEWS(er) {
 
		if (er->from != INVALID_ENGINE) {
 
			SlSetArrayIndex(er->index);
 
			SlObject(er, _engine_renew_desc);
 
		}
 
		SlSetArrayIndex(er->index);
 
		SlObject(er, _engine_renew_desc);
 
	}
 
}
 

	
engine.h
Show inline comments
 
@@ -224,7 +224,7 @@ static inline const RoadVehicleInfo* Roa
 
 * it.
 
 */
 
struct EngineRenew {
 
	uint16 index;
 
	EngineRenewID index;
 
	EngineID from;
 
	EngineID to;
 
	struct EngineRenew *next;
 
@@ -240,6 +240,25 @@ typedef struct EngineRenew EngineRenew;
 
extern MemoryPool _engine_renew_pool;
 

	
 
/**
 
 * Get the current size of the EngineRenewPool
 
 */
 
static inline uint16 GetEngineRenewPoolSize(void)
 
{
 
	return _engine_renew_pool.total_items;
 
}
 

	
 
/**
 
 * Check if a EngineRenew really exists.
 
 */
 
static inline bool IsValidEngineRenew(const EngineRenew *er)
 
{
 
	return er->from != INVALID_ENGINE;
 
}
 

	
 
#define FOR_ALL_ENGINE_RENEWS_FROM(er, start) for (er = GetEngineRenew(start); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL) if (er->from != INVALID_ENGINE) if (IsValidEngineRenew(er))
 
#define FOR_ALL_ENGINE_RENEWS(er) FOR_ALL_ENGINE_RENEWS_FROM(er, 0)
 

	
 
/**
 
 * DO NOT USE outside of engine.c. Is
 
 * placed here so the only exception to this rule, the saveload code, can use
 
 * it.
openttd.h
Show inline comments
 
@@ -48,6 +48,7 @@ typedef uint32 SpriteID;    ///< The num
 
typedef uint32 PalSpriteID; ///< The number of a sprite plus all the mapping bits and colortables
 
typedef uint32 CursorID;
 
typedef uint16 EngineID; ///< All enginenumbers should be of this type
 
typedef uint16 EngineRenewID;
 
typedef uint16 UnitID;   ///< All unitnumber stuff is of this type (or anyway, should be)
 

	
 
typedef uint32 WindowNumber;
station_cmd.c
Show inline comments
 
@@ -52,7 +52,7 @@ static void StationPoolNewBlock(uint sta
 
	Station *st;
 

	
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 *  This is just a temporary stage, this will be removed. */
 
	 *  TODO - This is just a temporary stage, this will be removed. */
 
	for (st = GetStation(start_item); st != NULL; st = (st->index + 1 < GetStationPoolSize()) ? GetStation(st->index + 1) : NULL) st->index = start_item++;
 
}
 

	
0 comments (0 inline, 0 general)