From 7dd6027194a5fb12ac5fa074c9c7a055a67ad41a Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Sun, 15 Apr 2018 01:15:26 +0200 Subject: Codechange: Use a SmallVec for the animated tile list instead of replicating most of the logic. --- src/saveload/afterload.cpp | 15 +++++++-------- src/saveload/animated_tile_sl.cpp | 29 +++++++++++++---------------- src/saveload/oldloader_sl.cpp | 20 ++++++++------------ 3 files changed, 28 insertions(+), 36 deletions(-) (limited to 'src/saveload') diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index d5d9bc3a4..f01afb425 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -2157,22 +2157,21 @@ bool AfterLoadGame() /* Animated tiles would sometimes not be actually animated or * in case of old savegames duplicate. */ - extern TileIndex *_animated_tile_list; - extern uint _animated_tile_count; + extern SmallVector _animated_tiles; - for (uint i = 0; i < _animated_tile_count; /* Nothing */) { + for (TileIndex *tile = _animated_tiles.Begin(); tile < _animated_tiles.End(); /* Nothing */) { /* Remove if tile is not animated */ - bool remove = _tile_type_procs[GetTileType(_animated_tile_list[i])]->animate_tile_proc == NULL; + bool remove = _tile_type_procs[GetTileType(*tile)]->animate_tile_proc == NULL; /* and remove if duplicate */ - for (uint j = 0; !remove && j < i; j++) { - remove = _animated_tile_list[i] == _animated_tile_list[j]; + for (TileIndex *j = _animated_tiles.Begin(); !remove && j < tile; j++) { + remove = *tile == *j; } if (remove) { - DeleteAnimatedTile(_animated_tile_list[i]); + DeleteAnimatedTile(*tile); } else { - i++; + tile++; } } } diff --git a/src/saveload/animated_tile_sl.cpp b/src/saveload/animated_tile_sl.cpp index 3fc5f6175..2152c42e3 100644 --- a/src/saveload/animated_tile_sl.cpp +++ b/src/saveload/animated_tile_sl.cpp @@ -12,22 +12,21 @@ #include "../stdafx.h" #include "../tile_type.h" #include "../core/alloc_func.hpp" +#include "../core/smallvec_type.hpp" #include "saveload.h" #include "../safeguards.h" -extern TileIndex *_animated_tile_list; -extern uint _animated_tile_count; -extern uint _animated_tile_allocated; +extern SmallVector _animated_tiles; /** * Save the ANIT chunk. */ static void Save_ANIT() { - SlSetLength(_animated_tile_count * sizeof(*_animated_tile_list)); - SlArray(_animated_tile_list, _animated_tile_count, SLE_UINT32); + SlSetLength(_animated_tiles.Length() * sizeof(*_animated_tiles.Begin())); + SlArray(_animated_tiles.Begin(), _animated_tiles.Length(), SLE_UINT32); } /** @@ -38,22 +37,20 @@ static void Load_ANIT() /* Before version 80 we did NOT have a variable length animated tile table */ if (IsSavegameVersionBefore(80)) { /* In pre version 6, we has 16bit per tile, now we have 32bit per tile, convert it ;) */ - SlArray(_animated_tile_list, 256, IsSavegameVersionBefore(6) ? (SLE_FILE_U16 | SLE_VAR_U32) : SLE_UINT32); + TileIndex anim_list[256]; + SlArray(anim_list, 256, IsSavegameVersionBefore(6) ? (SLE_FILE_U16 | SLE_VAR_U32) : SLE_UINT32); - for (_animated_tile_count = 0; _animated_tile_count < 256; _animated_tile_count++) { - if (_animated_tile_list[_animated_tile_count] == 0) break; + for (int i = 0; i < 256; i++) { + if (anim_list[i] == 0) break; + *_animated_tiles.Append() = anim_list[i]; } return; } - _animated_tile_count = (uint)SlGetFieldLength() / sizeof(*_animated_tile_list); - - /* Determine a nice rounded size for the amount of allocated tiles */ - _animated_tile_allocated = 256; - while (_animated_tile_allocated < _animated_tile_count) _animated_tile_allocated *= 2; - - _animated_tile_list = ReallocT(_animated_tile_list, _animated_tile_allocated); - SlArray(_animated_tile_list, _animated_tile_count, SLE_UINT32); + uint count = (uint)SlGetFieldLength() / sizeof(*_animated_tiles.Begin()); + _animated_tiles.Clear(); + _animated_tiles.Append(count); + SlArray(_animated_tiles.Begin(), count, SLE_UINT32); } /** diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 0c5716681..61a5aa5c6 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -28,6 +28,7 @@ #include "../engine_func.h" #include "../company_base.h" #include "../disaster_vehicle.h" +#include "../core/smallvec_type.hpp" #include "saveload_internal.h" #include "oldloader.h" @@ -490,8 +491,7 @@ static inline uint RemapOrderIndex(uint x) return _savegame_type == SGT_TTO ? (x - 0x1AC4) / 2 : (x - 0x1C18) / 2; } -extern TileIndex *_animated_tile_list; -extern uint _animated_tile_count; +extern SmallVector _animated_tiles; extern char *_old_name_array; static uint32 _old_town_index; @@ -640,22 +640,18 @@ static bool LoadOldOrder(LoadgameState *ls, int num) static bool LoadOldAnimTileList(LoadgameState *ls, int num) { - /* This is slightly hackish - we must load a chunk into an array whose - * address isn't static, but instead pointed to by _animated_tile_list. - * To achieve that, create an OldChunks list on the stack on the fly. - * The list cannot be static because the value of _animated_tile_list - * can change between calls. */ - + TileIndex anim_list[256]; const OldChunks anim_chunk[] = { - OCL_VAR ( OC_TILE, 256, _animated_tile_list ), + OCL_VAR ( OC_TILE, 256, anim_list ), OCL_END () }; if (!LoadChunk(ls, NULL, anim_chunk)) return false; - /* Update the animated tile counter by counting till the first zero in the array */ - for (_animated_tile_count = 0; _animated_tile_count < 256; _animated_tile_count++) { - if (_animated_tile_list[_animated_tile_count] == 0) break; + /* The first zero in the loaded array indicates the end of the list. */ + for (int i = 0; i < 256; i++) { + if (anim_list[i] == 0) break; + *_animated_tiles.Append() = anim_list[i]; } return true; -- cgit v1.2.3-70-g09d2