Files
openttd/src/animated_tile.cpp
Jonathan G Rennison ead18b2af2 Merge branch 'master' into jgrpp
# Conflicts:
#	CMakeLists.txt
#	src/3rdparty/md5/md5.h
#	src/3rdparty/squirrel/squirrel/squtils.h
#	src/animated_tile.cpp
#	src/console_func.h
#	src/core/CMakeLists.txt
#	src/core/container_func.hpp
#	src/core/smallstack_type.hpp
#	src/crashlog.cpp
#	src/crashlog.h
#	src/debug.h
#	src/economy.cpp
#	src/gamelog.cpp
#	src/industry_gui.cpp
#	src/lang/catalan.txt
#	src/misc_gui.cpp
#	src/network/network_content.h
#	src/newgrf.cpp
#	src/newgrf.h
#	src/newgrf_config.cpp
#	src/newgrf_config.h
#	src/newgrf_gui.cpp
#	src/os/unix/font_unix.cpp
#	src/os/windows/crashlog_win.cpp
#	src/rail_cmd.cpp
#	src/saveload/animated_tile_sl.cpp
#	src/script/api/script_tilelist.cpp
#	src/settings.cpp
#	src/settingsgen/settingsgen.cpp
#	src/sl/oldloader_sl.cpp
#	src/station.cpp
#	src/station_cmd.cpp
#	src/stdafx.h
#	src/strgen/strgen.cpp
#	src/strgen/strgen_base.cpp
#	src/table/settings/gui_settings.ini
#	src/train_gui.cpp
#	src/vehicle.cpp
#	src/vehicle_base.h
#	src/vehicle_cmd.cpp
#	src/vehicle_gui_base.h
#	src/viewport_sprite_sorter.h
2023-07-02 12:02:36 +01:00

159 lines
4.2 KiB
C++

/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file animated_tile.cpp Everything related to animated tiles. */
#include "stdafx.h"
#include "animated_tile.h"
#include "core/alloc_func.hpp"
#include "core/container_func.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. */
btree::btree_map<TileIndex, AnimatedTileInfo> _animated_tiles;
/**
* Removes the given tile from the animated tile table.
* @param tile the tile to remove
*/
void DeleteAnimatedTile(TileIndex tile)
{
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.
* @param tile the tile to make animated
*/
void AddAnimatedTile(TileIndex tile, bool mark_dirty)
{
if (mark_dirty) MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE);
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;
}
/**
* Animate all tiles in the animated tile list, i.e.\ call AnimateTile on them.
*/
void AnimateAnimatedTiles()
{
extern void AnimateTile_Town(TileIndex tile);
extern void AnimateTile_Station(TileIndex tile);
extern void AnimateTile_Industry(TileIndex tile);
extern void AnimateTile_Object(TileIndex tile);
PerformanceAccumulator framerate(PFE_GL_LANDSCAPE);
const uint32 ticks = (uint) _scaled_tick_counter;
const uint8 max_speed = (ticks == 0) ? 32 : FindFirstBit(ticks);
auto iter = _animated_tiles.begin();
while (iter != _animated_tiles.end()) {
if (iter->second.pending_deletion) {
iter = _animated_tiles.erase(iter);
continue;
}
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;
}
}
/**
* Initialize all animated tile variables to some known begin point
*/
void InitializeAnimatedTiles()
{
_animated_tiles.clear();
}