diff options
author | frosch <frosch@openttd.org> | 2008-09-13 15:49:29 +0000 |
---|---|---|
committer | frosch <frosch@openttd.org> | 2008-09-13 15:49:29 +0000 |
commit | 856157b6da0bd76cc2d4cf98b226ce14309ad33c (patch) | |
tree | cceaf7c87f5d8c8bdbc1328d00f46f73f8c7321d /src | |
parent | 0251a9c5294eae866d3d5db50601261575062edd (diff) | |
download | openttd-856157b6da0bd76cc2d4cf98b226ce14309ad33c.tar.xz |
(svn r14313) -Codechange: Move functions dealing with the EngineRenew pool to their own file.
Diffstat (limited to 'src')
-rw-r--r-- | src/autoreplace.cpp | 150 | ||||
-rw-r--r-- | src/autoreplace_base.h | 2 | ||||
-rw-r--r-- | src/engine.cpp | 142 | ||||
-rw-r--r-- | src/engine_type.h | 1 | ||||
-rw-r--r-- | src/misc.cpp | 4 | ||||
-rw-r--r-- | src/saveload.cpp | 2 |
6 files changed, 157 insertions, 144 deletions
diff --git a/src/autoreplace.cpp b/src/autoreplace.cpp new file mode 100644 index 000000000..a2da94776 --- /dev/null +++ b/src/autoreplace.cpp @@ -0,0 +1,150 @@ +/* $Id$ */ + +/** @file autoreplace.cpp Management of replacement lists. */ + +#include "stdafx.h" +#include "openttd.h" +#include "debug.h" +#include "command_func.h" +#include "saveload.h" +#include "group.h" +#include "autoreplace_base.h" +#include "oldpool_func.h" + +DEFINE_OLD_POOL_GENERIC(EngineRenew, EngineRenew) + +/** + * Retrieves the EngineRenew that specifies the replacement of the given + * engine type from the given renewlist */ +static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group) +{ + EngineRenew *er = (EngineRenew *)erl; + + while (er) { + if (er->from == engine && er->group_id == group) return er; + er = er->next; + } + return NULL; +} + +void RemoveAllEngineReplacement(EngineRenewList *erl) +{ + EngineRenew *er = (EngineRenew *)(*erl); + EngineRenew *next; + + while (er != NULL) { + next = er->next; + delete er; + er = next; + } + *erl = NULL; // Empty list +} + +EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group) +{ + const EngineRenew *er = GetEngineReplacement(erl, engine, group); + if (er == NULL && (group == DEFAULT_GROUP || (IsValidGroupID(group) && !GetGroup(group)->replace_protection))) { + /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */ + er = GetEngineReplacement(erl, engine, ALL_GROUP); + } + return er == NULL ? INVALID_ENGINE : er->to; +} + +CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, uint32 flags) +{ + EngineRenew *er; + + /* Check if the old vehicle is already in the list */ + er = GetEngineReplacement(*erl, old_engine, group); + if (er != NULL) { + if (flags & DC_EXEC) er->to = new_engine; + return CommandCost(); + } + + if (!EngineRenew::CanAllocateItem()) return CMD_ERROR; + + if (flags & DC_EXEC) { + er = new EngineRenew(old_engine, new_engine); + er->group_id = group; + + /* Insert before the first element */ + er->next = (EngineRenew *)(*erl); + *erl = (EngineRenewList)er; + } + + return CommandCost(); +} + +CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, uint32 flags) +{ + EngineRenew *er = (EngineRenew *)(*erl); + EngineRenew *prev = NULL; + + while (er) + { + if (er->from == engine && er->group_id == group) { + if (flags & DC_EXEC) { + if (prev == NULL) { // First element + /* The second becomes the new first element */ + *erl = (EngineRenewList)er->next; + } else { + /* Cut this element out */ + prev->next = er->next; + } + delete er; + } + return CommandCost(); + } + prev = er; + er = er->next; + } + + return CMD_ERROR; +} + +static const SaveLoad _engine_renew_desc[] = { + SLE_VAR(EngineRenew, from, SLE_UINT16), + SLE_VAR(EngineRenew, to, SLE_UINT16), + + SLE_REF(EngineRenew, next, REF_ENGINE_RENEWS), + SLE_CONDVAR(EngineRenew, group_id, SLE_UINT16, 60, SL_MAX_VERSION), + SLE_END() +}; + +static void Save_ERNW() +{ + EngineRenew *er; + + FOR_ALL_ENGINE_RENEWS(er) { + SlSetArrayIndex(er->index); + SlObject(er, _engine_renew_desc); + } +} + +static void Load_ERNW() +{ + int index; + + while ((index = SlIterateArray()) != -1) { + EngineRenew *er = new (index) EngineRenew(); + SlObject(er, _engine_renew_desc); + + /* Advanced vehicle lists, ungrouped vehicles got added */ + if (CheckSavegameVersion(60)) { + er->group_id = ALL_GROUP; + } else if (CheckSavegameVersion(71)) { + if (er->group_id == DEFAULT_GROUP) er->group_id = ALL_GROUP; + } + } +} + +extern const ChunkHandler _autoreplace_chunk_handlers[] = { + { 'ERNW', Save_ERNW, Load_ERNW, CH_ARRAY | CH_LAST}, +}; + +void InitializeEngineRenews() +{ + /* Clean the engine renew pool and create 1 block in it */ + _EngineRenew_pool.CleanPool(); + _EngineRenew_pool.AddBlockToPool(); +} diff --git a/src/autoreplace_base.h b/src/autoreplace_base.h index 065cc5661..ca5533e47 100644 --- a/src/autoreplace_base.h +++ b/src/autoreplace_base.h @@ -8,6 +8,8 @@ #include "oldpool.h" #include "autoreplace_type.h" +typedef uint16 EngineRenewID; + /** * Memory pool for engine renew elements. DO NOT USE outside of engine.c. Is * placed here so the only exception to this rule, the saveload code, can use diff --git a/src/engine.cpp b/src/engine.cpp index ffc6c0176..60000ac5b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -21,7 +21,6 @@ #include "functions.h" #include "window_func.h" #include "date_func.h" -#include "autoreplace_base.h" #include "autoreplace_gui.h" #include "string_func.h" #include "settings_type.h" @@ -569,137 +568,6 @@ CargoID GetEngineCargoType(EngineID engine) } } -/************************************************************************ - * Engine Replacement stuff - ************************************************************************/ - -DEFINE_OLD_POOL_GENERIC(EngineRenew, EngineRenew) - -/** - * Retrieves the EngineRenew that specifies the replacement of the given - * engine type from the given renewlist */ -static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group) -{ - EngineRenew *er = (EngineRenew *)erl; - - while (er) { - if (er->from == engine && er->group_id == group) return er; - er = er->next; - } - return NULL; -} - -void RemoveAllEngineReplacement(EngineRenewList *erl) -{ - EngineRenew *er = (EngineRenew *)(*erl); - EngineRenew *next; - - while (er != NULL) { - next = er->next; - delete er; - er = next; - } - *erl = NULL; // Empty list -} - -EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group) -{ - const EngineRenew *er = GetEngineReplacement(erl, engine, group); - if (er == NULL && (group == DEFAULT_GROUP || (IsValidGroupID(group) && !GetGroup(group)->replace_protection))) { - /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */ - er = GetEngineReplacement(erl, engine, ALL_GROUP); - } - return er == NULL ? INVALID_ENGINE : er->to; -} - -CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, uint32 flags) -{ - EngineRenew *er; - - /* Check if the old vehicle is already in the list */ - er = GetEngineReplacement(*erl, old_engine, group); - if (er != NULL) { - if (flags & DC_EXEC) er->to = new_engine; - return CommandCost(); - } - - if (!EngineRenew::CanAllocateItem()) return CMD_ERROR; - - if (flags & DC_EXEC) { - er = new EngineRenew(old_engine, new_engine); - er->group_id = group; - - /* Insert before the first element */ - er->next = (EngineRenew *)(*erl); - *erl = (EngineRenewList)er; - } - - return CommandCost(); -} - -CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, uint32 flags) -{ - EngineRenew *er = (EngineRenew *)(*erl); - EngineRenew *prev = NULL; - - while (er) - { - if (er->from == engine && er->group_id == group) { - if (flags & DC_EXEC) { - if (prev == NULL) { // First element - /* The second becomes the new first element */ - *erl = (EngineRenewList)er->next; - } else { - /* Cut this element out */ - prev->next = er->next; - } - delete er; - } - return CommandCost(); - } - prev = er; - er = er->next; - } - - return CMD_ERROR; -} - -static const SaveLoad _engine_renew_desc[] = { - SLE_VAR(EngineRenew, from, SLE_UINT16), - SLE_VAR(EngineRenew, to, SLE_UINT16), - - SLE_REF(EngineRenew, next, REF_ENGINE_RENEWS), - SLE_CONDVAR(EngineRenew, group_id, SLE_UINT16, 60, SL_MAX_VERSION), - SLE_END() -}; - -static void Save_ERNW() -{ - EngineRenew *er; - - FOR_ALL_ENGINE_RENEWS(er) { - SlSetArrayIndex(er->index); - SlObject(er, _engine_renew_desc); - } -} - -static void Load_ERNW() -{ - int index; - - while ((index = SlIterateArray()) != -1) { - EngineRenew *er = new (index) EngineRenew(); - SlObject(er, _engine_renew_desc); - - /* Advanced vehicle lists, ungrouped vehicles got added */ - if (CheckSavegameVersion(60)) { - er->group_id = ALL_GROUP; - } else if (CheckSavegameVersion(71)) { - if (er->group_id == DEFAULT_GROUP) er->group_id = ALL_GROUP; - } - } -} - static const SaveLoad _engine_desc[] = { SLE_CONDVAR(Engine, intro_date, SLE_FILE_U16 | SLE_VAR_I32, 0, 30), SLE_CONDVAR(Engine, intro_date, SLE_INT32, 31, SL_MAX_VERSION), @@ -805,13 +673,5 @@ static void Load_ENGS() extern const ChunkHandler _engine_chunk_handlers[] = { { 'ENGN', Save_ENGN, Load_ENGN, CH_ARRAY }, - { 'ENGS', NULL, Load_ENGS, CH_RIFF }, - { 'ERNW', Save_ERNW, Load_ERNW, CH_ARRAY | CH_LAST}, + { 'ENGS', NULL, Load_ENGS, CH_RIFF | CH_LAST }, }; - -void InitializeEngines() -{ - /* Clean the engine renew pool and create 1 block in it */ - _EngineRenew_pool.CleanPool(); - _EngineRenew_pool.AddBlockToPool(); -} diff --git a/src/engine_type.h b/src/engine_type.h index c9eb56a5e..5760d3ac6 100644 --- a/src/engine_type.h +++ b/src/engine_type.h @@ -15,7 +15,6 @@ #include "strings_type.h" typedef uint16 EngineID; -typedef uint16 EngineRenewID; struct Engine; diff --git a/src/misc.cpp b/src/misc.cpp index 463b05707..6e2b5d976 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -39,7 +39,7 @@ extern TileIndex _cur_tileloop_tile; void InitializeVehicles(); void InitializeWaypoints(); void InitializeDepots(); -void InitializeEngines(); +void InitializeEngineRenews(); void InitializeOrders(); void InitializeClearLand(); void InitializeRailGui(); @@ -76,7 +76,7 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date) InitializeOldNames(); } - InitializeEngines(); + InitializeEngineRenews(); InitializeVehicles(); InitializeWaypoints(); InitializeDepots(); diff --git a/src/saveload.cpp b/src/saveload.cpp index cee5257a3..b4bc4b919 100644 --- a/src/saveload.cpp +++ b/src/saveload.cpp @@ -1315,6 +1315,7 @@ extern const ChunkHandler _animated_tile_chunk_handlers[]; extern const ChunkHandler _newgrf_chunk_handlers[]; extern const ChunkHandler _group_chunk_handlers[]; extern const ChunkHandler _cargopacket_chunk_handlers[]; +extern const ChunkHandler _autoreplace_chunk_handlers[]; static const ChunkHandler * const _chunk_handlers[] = { _gamelog_chunk_handlers, @@ -1337,6 +1338,7 @@ static const ChunkHandler * const _chunk_handlers[] = { _newgrf_chunk_handlers, _group_chunk_handlers, _cargopacket_chunk_handlers, + _autoreplace_chunk_handlers, NULL, }; |