Cache animated tile speed, use btree map for animated tiles

This commit is contained in:
Jonathan G Rennison
2020-11-24 19:02:38 +00:00
parent 72a7ae25e8
commit 2ad446369d
23 changed files with 267 additions and 56 deletions

View File

@@ -63,6 +63,7 @@
#include "../bridge_signal_map.h"
#include "../water.h"
#include "../settings_func.h"
#include "../animated_tile.h"
#include "saveload_internal.h"
@@ -2461,16 +2462,9 @@ bool AfterLoadGame()
/* Animated tiles would sometimes not be actually animated or
* in case of old savegames duplicate. */
extern std::vector<TileIndex> _animated_tiles;
for (auto tile = _animated_tiles.begin(); tile < _animated_tiles.end(); /* Nothing */) {
for (auto tile = _animated_tiles.begin(); tile != _animated_tiles.end(); /* Nothing */) {
/* Remove if tile is not animated */
bool remove = _tile_type_procs[GetTileType(*tile)]->animate_tile_proc == nullptr;
/* and remove if duplicate */
for (auto j = _animated_tiles.begin(); !remove && j < tile; j++) {
remove = *tile == *j;
}
bool remove = _tile_type_procs[GetTileType(tile->first)]->animate_tile_proc == nullptr;
if (remove) {
tile = _animated_tiles.erase(tile);
@@ -3806,6 +3800,10 @@ bool AfterLoadGame()
RecalculateRoadCachedOneWayStates();
}
if (SlXvIsFeatureMissing(XSLFI_ANIMATED_TILE_EXTRA)) {
UpdateAllAnimatedTileSpeeds();
}
InitializeRoadGUI();
/* This needs to be done after conversion. */
@@ -3928,4 +3926,5 @@ void ReloadNewGRFData()
CheckTrainsLengths();
AfterLoadTemplateVehiclesUpdateImage();
AfterLoadTemplateVehiclesUpdateProperties();
UpdateAllAnimatedTileSpeeds();
}

View File

@@ -8,6 +8,7 @@
/** @file animated_tile_sl.cpp Code handling saving and loading of animated tiles */
#include "../stdafx.h"
#include "../animated_tile.h"
#include "../tile_type.h"
#include "../core/alloc_func.hpp"
#include "../core/smallvec_type.hpp"
@@ -16,15 +17,21 @@
#include "../safeguards.h"
extern std::vector<TileIndex> _animated_tiles;
/**
* Save the ANIT chunk.
*/
static void Save_ANIT()
{
SlSetLength(_animated_tiles.size() * sizeof(_animated_tiles.front()));
SlArray(_animated_tiles.data(), _animated_tiles.size(), SLE_UINT32);
uint count = 0;
for (const auto &it : _animated_tiles) {
if (!it.second.pending_deletion) count++;
}
SlSetLength(count * 5);
for (const auto &it : _animated_tiles) {
if (it.second.pending_deletion) continue;
SlWriteUint32(it.first);
SlWriteByte(it.second.speed);
}
}
/**
@@ -40,15 +47,26 @@ static void Load_ANIT()
for (int i = 0; i < 256; i++) {
if (anim_list[i] == 0) break;
_animated_tiles.push_back(anim_list[i]);
_animated_tiles[anim_list[i]] = {};
}
return;
}
uint count = (uint)SlGetFieldLength() / sizeof(_animated_tiles.front());
_animated_tiles.clear();
_animated_tiles.resize(_animated_tiles.size() + count);
SlArray(_animated_tiles.data(), count, SLE_UINT32);
if (SlXvIsFeaturePresent(XSLFI_ANIMATED_TILE_EXTRA)) {
uint count = (uint)SlGetFieldLength() / 5;
for (uint i = 0; i < count; i++) {
TileIndex tile = SlReadUint32();
AnimatedTileInfo info = {};
info.speed = SlReadByte();
_animated_tiles[tile] = info;
}
} else {
uint count = (uint)SlGetFieldLength() / 4;
for (uint i = 0; i < count; i++) {
_animated_tiles[SlReadUint32()] = {};
}
}
}
/**

View File

@@ -139,6 +139,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_ONE_WAY_DT_ROAD_STOP, XSCF_NULL, 1, 1, "one_way_dt_road_stop", nullptr, nullptr, nullptr },
{ XSLFI_ONE_WAY_ROAD_STATE, XSCF_NULL, 1, 1, "one_way_road_state", nullptr, nullptr, nullptr },
{ XSLFI_VENC_CHUNK, XSCF_IGNORABLE_ALL, 1, 1, "venc_chunk", nullptr, nullptr, "VENC" },
{ XSLFI_ANIMATED_TILE_EXTRA, XSCF_NULL, 1, 1, "animated_tile_extra", nullptr, nullptr, nullptr },
{ XSLFI_NULL, XSCF_NULL, 0, 0, nullptr, nullptr, nullptr, nullptr },// This is the end marker
};

View File

@@ -93,6 +93,7 @@ enum SlXvFeatureIndex {
XSLFI_ONE_WAY_DT_ROAD_STOP, ///< One-way drive-through road stops
XSLFI_ONE_WAY_ROAD_STATE, ///< One-way road state cache
XSLFI_VENC_CHUNK, ///< VENC chunk
XSLFI_ANIMATED_TILE_EXTRA, ///< Animated tile extra info
XSLFI_RIFF_HEADER_60_BIT, ///< Size field in RIFF chunk header is 60 bit
XSLFI_HEIGHT_8_BIT, ///< Map tile height is 8 bit instead of 4 bit, but savegame version may be before this became true in trunk

View File

@@ -26,6 +26,7 @@
#include "../engine_func.h"
#include "../company_base.h"
#include "../disaster_vehicle.h"
#include "../animated_tile.h"
#include "../core/smallvec_type.hpp"
#include "saveload_internal.h"
#include "oldloader.h"
@@ -483,7 +484,6 @@ static inline uint RemapOrderIndex(uint x)
return _savegame_type == SGT_TTO ? (x - 0x1AC4) / 2 : (x - 0x1C18) / 2;
}
extern std::vector<TileIndex> _animated_tiles;
extern char *_old_name_array;
static uint32 _old_town_index;
@@ -643,7 +643,7 @@ static bool LoadOldAnimTileList(LoadgameState *ls, int num)
/* 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.push_back(anim_list[i]);
_animated_tiles[anim_list[i]] = {};
}
return true;