Mercurial > hg > openttd
changeset 4348:84151151ad4a draft
(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
author | truelight <truelight@openttd.org> |
---|---|
date | Tue, 22 Aug 2006 16:22:07 +0000 |
parents | 22864837654b |
children | d636a071e112 |
files | engine.c engine.h openttd.h station_cmd.c |
diffstat | 4 files changed, 54 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/engine.c +++ b/engine.c @@ -453,19 +453,13 @@ 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 @@ { 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 @@ * 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 @@ 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 @@ { 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 @@ 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 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 @@ 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); } }
--- a/engine.h +++ b/engine.h @@ -224,7 +224,7 @@ * it. */ struct EngineRenew { - uint16 index; + EngineRenewID index; EngineID from; EngineID to; struct EngineRenew *next; @@ -240,6 +240,25 @@ 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.
--- a/openttd.h +++ b/openttd.h @@ -48,6 +48,7 @@ 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;
--- a/station_cmd.c +++ b/station_cmd.c @@ -52,7 +52,7 @@ 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++; }