summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine.c65
-rw-r--r--engine.h21
-rw-r--r--openttd.h1
-rw-r--r--station_cmd.c2
4 files changed, 54 insertions, 35 deletions
diff --git a/engine.c b/engine.c
index ba3b91fd2..d9ff4b01a 100644
--- a/engine.c
+++ b/engine.c
@@ -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(void)
{
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(void)
* 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(EngineRenewList erl, EngineID engine)
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(EngineRenewList *erl, EngineID old_engine, EngineID n
{
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(EngineRenewList *erl, EngineID old_engine, EngineID n
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(EngineRenewList *erl, EngineID old_engine, EngineID n
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);
}
}
diff --git a/engine.h b/engine.h
index f82ca90c5..42131b368 100644
--- a/engine.h
+++ b/engine.h
@@ -224,7 +224,7 @@ static inline const RoadVehicleInfo* RoadVehInfo(EngineID e)
* 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.
diff --git a/openttd.h b/openttd.h
index 8b97b031d..79f33febc 100644
--- a/openttd.h
+++ b/openttd.h
@@ -48,6 +48,7 @@ typedef uint32 SpriteID; ///< The number of a sprite, without mapping bits an
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;
diff --git a/station_cmd.c b/station_cmd.c
index 56c5af3e3..afd5e3581 100644
--- a/station_cmd.c
+++ b/station_cmd.c
@@ -52,7 +52,7 @@ static void StationPoolNewBlock(uint start_item)
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++;
}