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

@@ -8,16 +8,19 @@
/** @file animated_tile.cpp Everything related to animated tiles. */
#include "stdafx.h"
#include "animated_tile.h"
#include "core/alloc_func.hpp"
#include "core/smallvec_type.hpp"
#include "tile_cmd.h"
#include "viewport_func.h"
#include "framerate_type.h"
#include "date_func.h"
#include "3rdparty/cpp-btree/btree_map.h"
#include "safeguards.h"
/** The table/list with animated tiles. */
std::vector<TileIndex> _animated_tiles;
btree::btree_map<TileIndex, AnimatedTileInfo> _animated_tiles;
/**
* Removes the given tile from the animated tile table.
@@ -25,14 +28,43 @@ std::vector<TileIndex> _animated_tiles;
*/
void DeleteAnimatedTile(TileIndex tile)
{
auto to_remove = std::find(_animated_tiles.begin(), _animated_tiles.end(), tile);
if (to_remove != _animated_tiles.end()) {
/* The order of the remaining elements must stay the same, otherwise the animation loop may miss a tile. */
_animated_tiles.erase(to_remove);
auto to_remove = _animated_tiles.find(tile);
if (to_remove != _animated_tiles.end() && !to_remove->second.pending_deletion) {
to_remove->second.pending_deletion = true;
MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE);
}
}
static void UpdateAnimatedTileSpeed(TileIndex tile, AnimatedTileInfo &info)
{
extern uint8 GetAnimatedTileSpeed_Town(TileIndex tile);
extern uint8 GetAnimatedTileSpeed_Station(TileIndex tile);
extern uint8 GetAnimatedTileSpeed_Industry(TileIndex tile);
extern uint8 GetNewObjectTileAnimationSpeed(TileIndex tile);
switch (GetTileType(tile)) {
case MP_HOUSE:
info.speed = GetAnimatedTileSpeed_Town(tile);
break;
case MP_STATION:
info.speed = GetAnimatedTileSpeed_Station(tile);
break;
case MP_INDUSTRY:
info.speed = GetAnimatedTileSpeed_Industry(tile);
break;
case MP_OBJECT:
info.speed = GetNewObjectTileAnimationSpeed(tile);
break;
default:
info.speed = 0;
break;
}
}
/**
* Add the given tile to the animated tile table (if it does not exist
* on that table yet). Also increases the size of the table if necessary.
@@ -41,7 +73,18 @@ void DeleteAnimatedTile(TileIndex tile)
void AddAnimatedTile(TileIndex tile)
{
MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE);
include(_animated_tiles, tile);
AnimatedTileInfo &info = _animated_tiles[tile];
UpdateAnimatedTileSpeed(tile, info);
info.pending_deletion = false;
}
int GetAnimatedTileSpeed(TileIndex tile)
{
const auto iter = _animated_tiles.find(tile);
if (iter != _animated_tiles.end() && !iter->second.pending_deletion) {
return iter->second.speed;
}
return -1;
}
/**
@@ -56,40 +99,53 @@ void AnimateAnimatedTiles()
PerformanceAccumulator framerate(PFE_GL_LANDSCAPE);
const TileIndex *ti = _animated_tiles.data();
while (ti < _animated_tiles.data() + _animated_tiles.size()) {
const TileIndex curr = *ti;
switch (GetTileType(curr)) {
case MP_HOUSE:
AnimateTile_Town(curr);
break;
const uint32 ticks = (uint) _scaled_tick_counter;
const uint8 max_speed = (ticks == 0) ? 32 : FindFirstBit(ticks);
case MP_STATION:
AnimateTile_Station(curr);
break;
case MP_INDUSTRY:
AnimateTile_Industry(curr);
break;
case MP_OBJECT:
AnimateTile_Object(curr);
break;
default:
NOT_REACHED();
auto iter = _animated_tiles.begin();
while (iter != _animated_tiles.end()) {
if (iter->second.pending_deletion) {
iter = _animated_tiles.erase(iter);
continue;
}
/* During the AnimateTile call, DeleteAnimatedTile could have been called,
* deleting an element we've already processed and pushing the rest one
* slot to the left. We can detect this by checking whether the index
* in the current slot has changed - if it has, an element has been deleted,
* and we should process the current slot again instead of going forward.
* NOTE: this will still break if more than one animated tile is being
* deleted during the same AnimateTile call, but no code seems to
* be doing this anyway.
*/
if (*ti == curr) ++ti;
if (iter->second.speed <= max_speed) {
const TileIndex curr = iter->first;
switch (GetTileType(curr)) {
case MP_HOUSE:
AnimateTile_Town(curr);
break;
case MP_STATION:
AnimateTile_Station(curr);
break;
case MP_INDUSTRY:
AnimateTile_Industry(curr);
break;
case MP_OBJECT:
AnimateTile_Object(curr);
break;
default:
NOT_REACHED();
}
}
++iter;
}
}
void UpdateAllAnimatedTileSpeeds()
{
auto iter = _animated_tiles.begin();
while (iter != _animated_tiles.end()) {
if (iter->second.pending_deletion) {
iter = _animated_tiles.erase(iter);
continue;
}
UpdateAnimatedTileSpeed(iter->first, iter->second);
++iter;
}
}