Codechange: move tick-counter into TimerGameTick (#10712)

pull/544/head
Patric Stout 1 year ago committed by GitHub
parent 7aa2b9ab0a
commit 31ad990831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -25,8 +25,6 @@
#include "safeguards.h"
uint64 _tick_counter; ///< Ever incrementing tick counter for setting off various events
#define M(a, b) ((a << 5) | b)
static const uint16 _month_date_from_year_day[] = {
M( 0, 1), M( 0, 2), M( 0, 3), M( 0, 4), M( 0, 5), M( 0, 6), M( 0, 7), M( 0, 8), M( 0, 9), M( 0, 10), M( 0, 11), M( 0, 12), M( 0, 13), M( 0, 14), M( 0, 15), M( 0, 16), M( 0, 17), M( 0, 18), M( 0, 19), M( 0, 20), M( 0, 21), M( 0, 22), M( 0, 23), M( 0, 24), M( 0, 25), M( 0, 26), M( 0, 27), M( 0, 28), M( 0, 29), M( 0, 30), M( 0, 31),

@ -12,8 +12,6 @@
#include "date_type.h"
extern uint64 _tick_counter;
void ConvertDateToYMD(Date date, YearMonthDay *ymd);
Date ConvertYMDToDate(Year year, Month month, Day day);

@ -15,6 +15,7 @@
#include "console_func.h"
#include "debug.h"
#include "date_func.h"
#include "timer/timer_game_tick.h"
#include "rev.h"
#include <stdarg.h>
@ -387,7 +388,7 @@ static LoggedChange *GamelogChange(GamelogChangeType ct)
_current_action = &_gamelog_action[_gamelog_actions++];
_current_action->at = _gamelog_action_type;
_current_action->tick = _tick_counter;
_current_action->tick = TimerGameTick::counter;
_current_action->change = nullptr;
_current_action->changes = 0;
}

@ -16,8 +16,8 @@
#include "network/network.h"
#include "heightmap.h"
#include "viewport_func.h"
#include "date_func.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_tick.h"
#include "engine_func.h"
#include "water.h"
#include "video/video_driver.hpp"
@ -154,7 +154,7 @@ static void _GenerateWorld()
SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
for (i = 0; i < 0x500; i++) {
RunTileLoop();
_tick_counter++;
TimerGameTick::counter++;
IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
}

@ -46,6 +46,7 @@
#include "terraform_cmd.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_tick.h"
#include "table/strings.h"
#include "table/industry_land.h"
@ -661,7 +662,7 @@ static void AnimateOilWell(TileIndex tile, IndustryGfx gfx)
static void AnimateMineTower(TileIndex tile)
{
int state = _tick_counter & 0x7FF;
int state = TimerGameTick::counter & 0x7FF;
if ((state -= 0x400) < 0) return;
@ -702,36 +703,36 @@ static void AnimateTile_Industry(TileIndex tile)
switch (gfx) {
case GFX_SUGAR_MINE_SIEVE:
if ((_tick_counter & 1) == 0) AnimateSugarSieve(tile);
if ((TimerGameTick::counter & 1) == 0) AnimateSugarSieve(tile);
break;
case GFX_TOFFEE_QUARY:
if ((_tick_counter & 3) == 0) AnimateToffeeQuarry(tile);
if ((TimerGameTick::counter & 3) == 0) AnimateToffeeQuarry(tile);
break;
case GFX_BUBBLE_CATCHER:
if ((_tick_counter & 1) == 0) AnimateBubbleCatcher(tile);
if ((TimerGameTick::counter & 1) == 0) AnimateBubbleCatcher(tile);
break;
case GFX_POWERPLANT_SPARKS:
if ((_tick_counter & 3) == 0) AnimatePowerPlantSparks(tile);
if ((TimerGameTick::counter & 3) == 0) AnimatePowerPlantSparks(tile);
break;
case GFX_TOY_FACTORY:
if ((_tick_counter & 1) == 0) AnimateToyFactory(tile);
if ((TimerGameTick::counter & 1) == 0) AnimateToyFactory(tile);
break;
case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
if ((_tick_counter & 3) == 0) AnimatePlasticFountain(tile, gfx);
if ((TimerGameTick::counter & 3) == 0) AnimatePlasticFountain(tile, gfx);
break;
case GFX_OILWELL_ANIMATED_1:
case GFX_OILWELL_ANIMATED_2:
case GFX_OILWELL_ANIMATED_3:
if ((_tick_counter & 7) == 0) AnimateOilWell(tile, gfx);
if ((TimerGameTick::counter & 7) == 0) AnimateOilWell(tile, gfx);
break;
case GFX_COAL_MINE_TOWER_ANIMATED:
@ -879,7 +880,7 @@ static void TileLoop_Industry(TileIndex tile)
case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
if (!(TimerGameTick::counter & 0x400) && Chance16(1, 2)) {
switch (gfx) {
case GFX_COAL_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COAL_MINE_TOWER_ANIMATED; break;
case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
@ -902,7 +903,7 @@ static void TileLoop_Industry(TileIndex tile)
case GFX_COAL_MINE_TOWER_ANIMATED:
case GFX_COPPER_MINE_TOWER_ANIMATED:
case GFX_GOLD_MINE_TOWER_ANIMATED:
if (!(_tick_counter & 0x400)) {
if (!(TimerGameTick::counter & 0x400)) {
switch (gfx) {
case GFX_COAL_MINE_TOWER_ANIMATED: gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED; break;
case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;

@ -22,6 +22,7 @@
#include "fios.h"
#include "date_func.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_tick.h"
#include "water.h"
#include "effectvehicle_func.h"
#include "landscape_type.h"
@ -780,7 +781,7 @@ void RunTileLoop()
assert(tile != 0);
/* Manually update tile 0 every 256 ticks - the LFSR never iterates over it itself. */
if (_tick_counter % 256 == 0) {
if (TimerGameTick::counter % 256 == 0) {
_tile_type_procs[GetTileType(0)]->tile_loop_proc(0);
count--;
}

@ -16,6 +16,7 @@
#include "newgrf_house.h"
#include "economy_func.h"
#include "date_func.h"
#include "timer/timer_game_tick.h"
#include "texteff.hpp"
#include "gfx_func.h"
#include "gamelog.h"
@ -65,7 +66,7 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin
_pause_mode = PM_UNPAUSED;
_game_speed = 100;
_tick_counter = 0;
TimerGameTick::counter = 0;
_cur_tileloop_tile = 1;
_thd.redsq = INVALID_TILE;
if (reset_settings) MakeNewgameSettingsLive();

@ -38,6 +38,7 @@
#include "fios.h"
#include "strings_func.h"
#include "date_func.h"
#include "timer/timer_game_tick.h"
#include "string_func.h"
#include "network/core/config.h"
#include <map>
@ -6526,7 +6527,7 @@ bool GetGlobalVariable(byte param, uint32 *value, const GRFFile *grffile)
return true;
case 0x0A: // animation counter
*value = GB(_tick_counter, 0, 16);
*value = GB(TimerGameTick::counter, 0, 16);
return true;
case 0x0B: { // TTDPatch version
@ -9942,14 +9943,14 @@ void LoadNewGRF(uint load_index, uint num_baseset)
Date date = TimerGameCalendar::date;
Year year = TimerGameCalendar::year;
DateFract date_fract = TimerGameCalendar::date_fract;
uint64 tick_counter = _tick_counter;
uint64 tick_counter = TimerGameTick::counter;
byte display_opt = _display_opt;
if (_networking) {
TimerGameCalendar::year = _settings_game.game_creation.starting_year;
TimerGameCalendar::date = ConvertYMDToDate(TimerGameCalendar::year, 0, 1);
TimerGameCalendar::date_fract = 0;
_tick_counter = 0;
TimerGameTick::counter = 0;
_display_opt = 0;
}
@ -10046,6 +10047,6 @@ void LoadNewGRF(uint load_index, uint num_baseset)
TimerGameCalendar::year = year;
TimerGameCalendar::date = date;
TimerGameCalendar::date_fract = date_fract;
_tick_counter = tick_counter;
TimerGameTick::counter = tick_counter;
_display_opt = display_opt;
}

@ -11,7 +11,7 @@
#include "animated_tile_func.h"
#include "core/random_func.hpp"
#include "date_func.h"
#include "timer/timer_game_tick.h"
#include "viewport_func.h"
#include "newgrf_animation_type.h"
#include "newgrf_callbacks.h"
@ -60,7 +60,7 @@ struct AnimationBase {
* increasing this value by one doubles the wait. 0 is the minimum value
* allowed for animation_speed, which corresponds to 30ms, and 16 is the
* maximum, corresponding to around 33 minutes. */
if (_tick_counter % (1ULL << animation_speed) != 0) return;
if (TimerGameTick::counter % (1ULL << animation_speed) != 0) return;
uint8 frame = Tframehelper::Get(obj, tile);
uint8 num_frames = spec->animation.frames;

@ -8,7 +8,6 @@
/** @file newgrf_profiling.cpp Profiling of NewGRF action 2 handling. */
#include "newgrf_profiling.h"
#include "date_func.h"
#include "fileio_func.h"
#include "string_func.h"
#include "console_func.h"
@ -16,6 +15,7 @@
#include "walltime_func.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_tick.h"
#include <chrono>
@ -50,7 +50,7 @@ void NewGRFProfiler::BeginResolve(const ResolverObject &resolver)
this->cur_call.root_sprite = resolver.root_spritegroup->nfo_line;
this->cur_call.subs = 0;
this->cur_call.time = (uint32)time_point_cast<microseconds>(high_resolution_clock::now()).time_since_epoch().count();
this->cur_call.tick = _tick_counter;
this->cur_call.tick = TimerGameTick::counter;
this->cur_call.cb = resolver.callback;
this->cur_call.feat = resolver.GetFeature();
this->cur_call.item = resolver.GetDebugID();
@ -89,7 +89,7 @@ void NewGRFProfiler::Start()
{
this->Abort();
this->active = true;
this->start_tick = _tick_counter;
this->start_tick = TimerGameTick::counter;
}
uint32 NewGRFProfiler::Finish()
@ -148,7 +148,7 @@ uint32 NewGRFProfiler::FinishAll()
for (NewGRFProfiler &pr : _newgrf_profilers) {
if (pr.active) {
total_microseconds += pr.Finish();
max_ticks = std::max(max_ticks, _tick_counter - pr.start_tick);
max_ticks = std::max(max_ticks, TimerGameTick::counter - pr.start_tick);
}
}

@ -3192,7 +3192,7 @@ bool AfterLoadGame()
/* Use current order time to approximate last loading time */
if (IsSavegameVersionBefore(SLV_LAST_LOADING_TICK)) {
for (Vehicle *v : Vehicle::Iterate()) {
v->last_loading_tick = std::max(_tick_counter, static_cast<uint64>(v->current_order_time)) - v->current_order_time;
v->last_loading_tick = std::max(TimerGameTick::counter, static_cast<uint64>(v->current_order_time)) - v->current_order_time;
}
}

@ -76,8 +76,8 @@ static const SaveLoad _date_desc[] = {
SLEG_CONDVAR("date", TimerGameCalendar::date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
SLEG_CONDVAR("date", TimerGameCalendar::date, SLE_INT32, SLV_31, SL_MAX_VERSION),
SLEG_VAR("date_fract", TimerGameCalendar::date_fract, SLE_UINT16),
SLEG_CONDVAR("tick_counter", _tick_counter, SLE_FILE_U16 | SLE_VAR_U64, SL_MIN_VERSION, SLV_U64_TICK_COUNTER),
SLEG_CONDVAR("tick_counter", _tick_counter, SLE_UINT64, SLV_U64_TICK_COUNTER, SL_MAX_VERSION),
SLEG_CONDVAR("tick_counter", TimerGameTick::counter, SLE_FILE_U16 | SLE_VAR_U64, SL_MIN_VERSION, SLV_U64_TICK_COUNTER),
SLEG_CONDVAR("tick_counter", TimerGameTick::counter, SLE_UINT64, SLV_U64_TICK_COUNTER, SL_MAX_VERSION),
SLEG_CONDVAR("age_cargo_skip_counter", _age_cargo_skip_counter, SLE_UINT8, SL_MIN_VERSION, SLV_162),
SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
SLEG_CONDVAR("cur_tileloop_tile", _cur_tileloop_tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),

@ -1617,7 +1617,7 @@ static const OldChunks main_chunk[] = {
OCL_NULL( 2 ), ///< land_code, no longer in use
OCL_VAR ( OC_FILE_U16 | OC_VAR_U8, 1, &_age_cargo_skip_counter ),
OCL_VAR ( OC_FILE_U16 | OC_VAR_U64, 1, &_tick_counter ),
OCL_VAR ( OC_FILE_U16 | OC_VAR_U64, 1, &TimerGameTick::counter ),
OCL_VAR ( OC_TILE, 1, &_cur_tileloop_tile ),
OCL_ASSERT( OC_TTO, 0x3A2E ),

@ -341,7 +341,7 @@ enum SaveLoadVersion : uint16 {
SLV_DOCK_DOCKINGTILES, ///< 298 PR#9578 All tiles around docks may be docking tiles.
SLV_REPAIR_OBJECT_DOCKING_TILES, ///< 299 PR#9594 v12.0 Fixing issue with docking tiles overlapping objects.
SLV_U64_TICK_COUNTER, ///< 300 PR#10035 Make _tick_counter 64bit to avoid wrapping.
SLV_U64_TICK_COUNTER, ///< 300 PR#10035 Make tick counter 64bit to avoid wrapping.
SLV_LAST_LOADING_TICK, ///< 301 PR#9693 Store tick of last loading for vehicles.
SLV_MULTITRACK_LEVEL_CROSSINGS, ///< 302 PR#9931 v13.0 Multi-track level crossings.
SLV_NEWGRF_ROAD_STOPS, ///< 303 PR#10144 NewGRF road stops.

@ -62,6 +62,7 @@
#include "newgrf_roadstop.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_tick.h"
#include "table/strings.h"
@ -3979,16 +3980,18 @@ void OnTick_Station()
StationHandleSmallTick(st);
/* Clean up the link graph about once a week. */
if (Station::IsExpected(st) && (_tick_counter + st->index) % STATION_LINKGRAPH_TICKS == 0) {
if (Station::IsExpected(st) && (TimerGameTick::counter + st->index) % STATION_LINKGRAPH_TICKS == 0) {
DeleteStaleLinks(Station::From(st));
};
/* Run STATION_ACCEPTANCE_TICKS = 250 tick interval trigger for station animation.
* Station index is included so that triggers are not all done
* at the same time. */
if ((_tick_counter + st->index) % STATION_ACCEPTANCE_TICKS == 0) {
/* Spread out big-tick over STATION_ACCEPTANCE_TICKS ticks. */
if ((TimerGameTick::counter + st->index) % STATION_ACCEPTANCE_TICKS == 0) {
/* Stop processing this station if it was deleted */
if (!StationHandleBigTick(st)) continue;
}
/* Spread out station animation over STATION_ACCEPTANCE_TICKS ticks. */
if ((TimerGameTick::counter + st->index) % STATION_ACCEPTANCE_TICKS == 0) {
TriggerStationAnimation(st, st->xy, SAT_250_TICKS);
TriggerRoadStopAnimation(st, st->xy, SAT_250_TICKS);
if (Station::IsExpected(st)) AirportAnimationTrigger(Station::From(st), AAT_STATION_250_TICKS);

@ -67,8 +67,6 @@ void TimerManager<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed delta)
{
assert(delta == 1);
_tick_counter++;
if (_game_mode == GM_MENU) return;
TimerGameCalendar::date_fract++;

@ -16,6 +16,8 @@
#include "safeguards.h"
uint64 TimerGameTick::counter = 0;
template<>
void IntervalTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
{
@ -51,6 +53,8 @@ void TimeoutTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
template<>
void TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
{
TimerGameTick::counter++;
for (auto timer : TimerManager<TimerGameTick>::GetTimers()) {
timer->Elapsed(delta);
}

@ -29,6 +29,8 @@ public:
struct TStorage {
uint elapsed;
};
static uint64 counter; ///< Monotonic counter, in ticks, since start of game.
};
#endif /* TIMER_GAME_TICK_H */

@ -34,7 +34,6 @@
#include "newgrf_cargo.h"
#include "cheat_type.h"
#include "animated_tile_func.h"
#include "date_func.h"
#include "subsidy_func.h"
#include "core/pool_func.hpp"
#include "town.h"
@ -54,6 +53,7 @@
#include "tunnelbridge_cmd.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_tick.h"
#include "table/strings.h"
#include "table/town_land.h"
@ -345,7 +345,7 @@ static void AnimateTile_Town(TileIndex tile)
return;
}
if (_tick_counter & 3) return;
if (TimerGameTick::counter & 3) return;
/* If the house is not one with a lift anymore, then stop this animating.
* Not exactly sure when this happens, but probably when a house changes.
@ -597,7 +597,7 @@ static void TileLoop_Town(TileIndex tile)
/* Binomial distribution per tick, by a series of coin flips */
/* Reduce generation rate to a 1/4, using tile bits to spread out distribution.
* As tick counter is incremented by 256 between each call, we ignore the lower 8 bits. */
if (GB(_tick_counter, 8, 2) == GB(tile, 0, 2)) {
if (GB(TimerGameTick::counter, 8, 2) == GB(tile, 0, 2)) {
/* Make a bitmask with up to 32 bits set, one for each potential pax */
int genmax = (hs->population + 7) / 8;
uint32 genmask = (genmax >= 32) ? 0xFFFFFFFF : ((1 << genmax) - 1);

@ -22,7 +22,7 @@
#include "company_base.h"
#include "core/random_func.hpp"
#include "newgrf_generic.h"
#include "date_func.h"
#include "timer/timer_game_tick.h"
#include "tree_cmd.h"
#include "landscape_cmd.h"
@ -707,11 +707,11 @@ static void TileLoop_Trees(TileIndex tile)
AmbientSoundEffect(tile);
/* _tick_counter is incremented by 256 between each call, so ignore lower 8 bits.
/* TimerGameTick::counter is incremented by 256 between each call, so ignore lower 8 bits.
* Also, we use a simple hash to spread the updates evenly over the map.
* 11 and 9 are just some co-prime numbers for better spread.
*/
uint32 cycle = 11 * TileX(tile) + 9 * TileY(tile) + (_tick_counter >> 8);
uint32 cycle = 11 * TileX(tile) + 9 * TileY(tile) + (TimerGameTick::counter >> 8);
/* Handle growth of grass (under trees/on MP_TREES tiles) at every 8th processings, like it's done for grass on MP_CLEAR tiles. */
if ((cycle & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
@ -841,7 +841,7 @@ void OnTick_Trees()
* this is the maximum number of ticks that are skipped. Number of ticks to skip is
* inversely proportional to map size, so that is handled to create a mask. */
int skip = Map::ScaleBySize(16);
if (skip < 16 && (_tick_counter & (16 / skip - 1)) != 0) return;
if (skip < 16 && (TimerGameTick::counter & (16 / skip - 1)) != 0) return;
/* place a tree at a random rainforest spot */
if (_settings_game.game_creation.landscape == LT_TROPIC) {

@ -58,6 +58,7 @@
#include "newgrf_roadstop.h"
#include "timer/timer.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_tick.h"
#include "table/strings.h"
@ -2116,7 +2117,7 @@ void Vehicle::BeginLoading()
{
assert(IsTileType(this->tile, MP_STATION) || this->type == VEH_SHIP);
Ticks travel_time = _tick_counter - this->last_loading_tick;
Ticks travel_time = TimerGameTick::counter - this->last_loading_tick;
if (this->current_order.IsType(OT_GOTO_STATION) &&
this->current_order.GetDestination() == this->last_station_visited) {
this->DeleteUnreachedImplicitOrders();
@ -2281,7 +2282,7 @@ void Vehicle::LeaveStation()
/* if the vehicle could load here or could stop with cargo loaded set the last loading station */
this->last_loading_station = this->last_station_visited;
this->last_loading_tick = _tick_counter;
this->last_loading_tick = TimerGameTick::counter;
} else {
/* if the vehicle couldn't load and had to unload or transfer everything
* set the last loading station to invalid as it will leave empty. */

@ -332,7 +332,7 @@ public:
StationID last_station_visited; ///< The last station we stopped at.
StationID last_loading_station; ///< Last station the vehicle has stopped at and could possibly leave from with any cargo loaded.
uint64_t last_loading_tick; ///< Last time (relative to _tick_counter) the vehicle has stopped at a station and could possibly leave with any cargo loaded.
uint64_t last_loading_tick; ///< Last time (based on TimerGameTick counter) the vehicle has stopped at a station and could possibly leave with any cargo loaded.
CargoID cargo_type; ///< type of cargo this vehicle is carrying
byte cargo_subtype; ///< Used for livery refits (NewGRF variations)

Loading…
Cancel
Save