OpenTTD Source 20261112-master-gbcbec5e63f
cachecheck.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "aircraft.h"
12#include "company_base.h"
13#include "debug.h"
14#include "industry.h"
15#include "roadstop_base.h"
16#include "roadveh.h"
17#include "ship.h"
18#include "station_base.h"
19#include "station_func.h"
20#include "station_map.h"
21#include "subsidy_func.h"
22#include "town.h"
23#include "train.h"
24#include "vehicle_base.h"
25
26#include "safeguards.h"
27
28extern void AfterLoadCompanyStats();
29extern void RebuildTownCaches();
30
38{
39 /* Return here so it is easy to add checks that are run
40 * always to aid testing of caches. */
42
43 /* Check the town caches. */
44 std::vector<TownCache> old_town_caches;
45 for (const Town *t : Town::Iterate()) {
46 old_town_caches.push_back(t->cache);
47 }
48
51
52 uint i = 0;
53 for (Town *t : Town::Iterate()) {
54 if (old_town_caches[i] != t->cache) {
55 Debug(Facility::Desync, Severity::Warning, "warning: town cache mismatch: town {}", t->index);
56 }
57 i++;
58 }
59
60 /* Check company infrastructure cache. */
61 std::vector<CompanyInfrastructure> old_infrastructure;
62 for (const Company *c : Company::Iterate()) old_infrastructure.push_back(c->infrastructure);
63
65
66 i = 0;
67 for (const Company *c : Company::Iterate()) {
68 if (old_infrastructure[i] != c->infrastructure) {
69 Debug(Facility::Desync, Severity::Warning, "warning: infrastructure cache mismatch: company {}", c->index);
70 }
71 i++;
72 }
73
74 /* Strict checking of the road stop cache entries */
75 for (const RoadStop *rs : RoadStop::Iterate()) {
76 if (IsBayRoadStopTile(rs->xy)) continue;
77
78 rs->GetEntry(DiagDirection::NE).CheckIntegrity(rs);
79 rs->GetEntry(DiagDirection::NW).CheckIntegrity(rs);
80 }
81
82 std::vector<NewGRFCache> grf_cache;
83 std::vector<VehicleCache> veh_cache;
84 std::vector<GroundVehicleCache> gro_cache;
85 std::vector<TrainCache> tra_cache;
86
87 for (Vehicle *v : Vehicle::Iterate()) {
88 if (v != v->First() || v->vehstatus.Test(VehState::Crashed) || !v->IsPrimaryVehicle()) continue;
89
90 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
92 grf_cache.emplace_back(u->grf_cache);
93 veh_cache.emplace_back(u->vcache);
94 switch (u->type) {
96 gro_cache.emplace_back(Train::From(u)->gcache);
97 tra_cache.emplace_back(Train::From(u)->tcache);
98 break;
100 gro_cache.emplace_back(RoadVehicle::From(u)->gcache);
101 break;
102 default:
103 break;
104 }
105 }
106
107 switch (v->type) {
111 case VehicleType::Ship: Ship::From(v)->UpdateCache(); break;
112 default: break;
113 }
114
115 uint length = 0;
116 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
118 if (grf_cache[length] != u->grf_cache) {
119 Debug(Facility::Desync, Severity::Warning, "warning: newgrf cache mismatch: type {}, vehicle {}, company {}, unit number {}, wagon {}", v->type, v->index, v->owner, v->unitnumber, length);
120 }
121 if (veh_cache[length] != u->vcache) {
122 Debug(Facility::Desync, Severity::Warning, "warning: vehicle cache mismatch: type {}, vehicle {}, company {}, unit number {}, wagon {}", v->type, v->index, v->owner, v->unitnumber, length);
123 }
124 switch (u->type) {
126 if (gro_cache[length] != Train::From(u)->gcache) {
127 Debug(Facility::Desync, Severity::Warning, "warning: train ground vehicle cache mismatch: vehicle {}, company {}, unit number {}, wagon {}", v->index, v->owner, v->unitnumber, length);
128 }
129 if (tra_cache[length] != Train::From(u)->tcache) {
130 Debug(Facility::Desync, Severity::Warning, "warning: train cache mismatch: vehicle {}, company {}, unit number {}, wagon {}", v->index, v->owner, v->unitnumber, length);
131 }
132 break;
134 if (gro_cache[length] != RoadVehicle::From(u)->gcache) {
135 Debug(Facility::Desync, Severity::Warning, "warning: road vehicle ground vehicle cache mismatch: vehicle {}, company {}, unit number {}, wagon {}", v->index, v->owner, v->unitnumber, length);
136 }
137 break;
138 default:
139 break;
140 }
141 length++;
142 }
143
144 grf_cache.clear();
145 veh_cache.clear();
146 gro_cache.clear();
147 tra_cache.clear();
148 }
149
150 /* Check whether the caches are still valid */
151 for (Vehicle *v : Vehicle::Iterate()) {
152 [[maybe_unused]] const auto a = v->cargo.PeriodsInTransit();
153 [[maybe_unused]] const auto b = v->cargo.TotalCount();
154 [[maybe_unused]] const auto c = v->cargo.GetFeederShare();
155 v->cargo.InvalidateCache();
156 assert(a == v->cargo.PeriodsInTransit());
157 assert(b == v->cargo.TotalCount());
158 assert(c == v->cargo.GetFeederShare());
159 }
160
161 /* Backup stations_near */
162 std::vector<StationList> old_town_stations_near;
163 for (Town *t : Town::Iterate()) old_town_stations_near.push_back(t->stations_near);
164
165 std::vector<StationList> old_industry_stations_near;
166 for (Industry *ind : Industry::Iterate()) old_industry_stations_near.push_back(ind->stations_near);
167
168 std::vector<IndustryList> old_station_industries_near;
169 for (Station *st : Station::Iterate()) old_station_industries_near.push_back(st->industries_near);
170
171 for (Station *st : Station::Iterate()) {
172 for (GoodsEntry &ge : st->goods) {
173 if (!ge.HasData()) continue;
174
175 StationCargoList &cargo_list = ge.GetData().cargo;
176 [[maybe_unused]] const auto a = cargo_list.PeriodsInTransit();
177 [[maybe_unused]] const auto b = cargo_list.TotalCount();
178 cargo_list.InvalidateCache();
179 assert(a == cargo_list.PeriodsInTransit());
180 assert(b == cargo_list.TotalCount());
181 }
182
183 /* Check docking tiles */
184 TileArea ta;
185 std::map<TileIndex, bool> docking_tiles;
186 for (TileIndex tile : st->docking_station) {
187 ta.Add(tile);
188 docking_tiles[tile] = IsDockingTile(tile);
189 }
190 UpdateStationDockingTiles(st);
191 if (ta.tile != st->docking_station.tile || ta.w != st->docking_station.w || ta.h != st->docking_station.h) {
192 Debug(Facility::Desync, Severity::Warning, "warning: station docking mismatch: station {}, company {}", st->index, st->owner);
193 }
194 for (TileIndex tile : ta) {
195 if (docking_tiles[tile] != IsDockingTile(tile)) {
196 Debug(Facility::Desync, Severity::Warning, "warning: docking tile mismatch: tile {}", tile);
197 }
198 }
199 }
200
202
203 /* Check industries_near */
204 i = 0;
205 for (Station *st : Station::Iterate()) {
206 if (st->industries_near != old_station_industries_near[i]) {
207 Debug(Facility::Desync, Severity::Warning, "warning: station industries near mismatch: station {}", st->index);
208 }
209 i++;
210 }
211
212 /* Check stations_near */
213 i = 0;
214 for (Town *t : Town::Iterate()) {
215 if (t->stations_near != old_town_stations_near[i]) {
216 Debug(Facility::Desync, Severity::Warning, "warning: town stations near mismatch: town {}", t->index);
217 }
218 i++;
219 }
220 i = 0;
221 for (Industry *ind : Industry::Iterate()) {
222 if (ind->stations_near != old_industry_stations_near[i]) {
223 Debug(Facility::Desync, Severity::Warning, "warning: industry stations near mismatch: industry {}", ind->index);
224 }
225 i++;
226 }
227
228 /* Check the last vehicle cache. */
229 for (Vehicle *v : Vehicle::Iterate()) {
230 if (v != v->First() || v->vehstatus.Test(VehState::Crashed) || !v->IsPrimaryVehicle()) continue;
231
232 /* Check that the last vehicle is actually last. */
233 if (v->Last()->Next() != nullptr) {
234 Debug(Facility::Desync, Severity::Warning, "warning: vehicle cache mismatch, last vehicle must not have a next vehicle: type {}, vehicle {}, company {}, unit number {}, invalid 'Last()'", v->type, v->index, v->owner, v->unitnumber);
235 }
236
237 /* Ensure that all vehicles in the chain have the same last vehicle. */
238 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
239 if (u->Last() != v->Last()) {
240 Debug(Facility::Desync, Severity::Warning, "warning: vehicle cache mismatch, all vehicles in chain must have same last vehicle: type {}, vehicle {}, company {}, unit number {}, invalid 'Last()'", v->type, v->index, v->owner, v->unitnumber);
241 }
242 }
243 }
244}
Base for aircraft.
void UpdateAircraftCache(Aircraft *v, bool update_range=false)
Update cached values of an aircraft.
void RebuildTownCaches()
Rebuild all the cached variables of towns.
Definition town_sl.cpp:27
void AfterLoadCompanyStats()
Rebuilding of company statistics after loading a savegame.
void CheckCaches()
Check the validity of some of the caches.
uint PeriodsInTransit() const
Returns average number of cargo aging periods in transit for a cargo entity.
void InvalidateCache()
Invalidates the cached data and rebuilds it.
uint16_t w
The width of the area.
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
TileIndex tile
The base tile of the area.
uint16_t h
The height of the area.
CargoList that is used for stations.
uint TotalCount() const
Returns total count of cargo at the station, including cargo which is already reserved for loading.
Definition of stuff that is very close to a company, like the company struct itself.
Functions related to debugging.
bool IsVisibleSeverity(Facility facility, Severity severity)
Test if debug severity is visible for the given facility.
Definition debug.h:24
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Desync
Desync message facility.
Definition debug_type.h:41
@ Warning
Warning, wrong but okay if you don't know.
Definition debug_type.h:17
@ NW
Northwest.
@ NE
Northeast, upper right on your monitor.
Base of all industries.
void FillNewGRFVehicleCache(const Vehicle *v)
Fill the grf_cache of the given vehicle.
Base class for roadstops.
Road vehicle states.
void RoadVehUpdateCache(RoadVehicle *v, bool same_length=false)
Update the cache of a road vehicle.
A number of safeguards to prevent using unsafe methods.
Base for ships.
Base classes/functions for stations.
Functions related to stations.
Maps accessors for stations.
bool IsBayRoadStopTile(Tile t)
Is tile t a bay (non-drive through) road stop station?
Definition of base types and functions in a cross-platform compatible way.
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Stores station stats for a single cargo.
const GoodsEntryData & GetData() const
Get optional cargo packet/flow data.
bool HasData() const
Test if this goods entry has optional cargo packet/flow data.
Defines the internal data of a functional industry.
Definition industry.h:64
static Pool::IterateWrapper< Town > Iterate(size_t from=0)
A Stop for a Road Vehicle.
void UpdateCache()
Update the caches of this ship.
Definition ship_cmd.cpp:236
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
static Train * From(Vehicle *v)
Station data structure.
static void RecomputeCatchmentForAll()
Recomputes catchment of all stations.
Definition station.cpp:513
Town data structure.
Definition town.h:64
void ConsistChanged(ConsistChangeFlags allowed_changes)
Recalculates the cached stuff of a train.
Vehicle data structure.
Vehicle * Next() const
Get the next vehicle of this vehicle.
void RebuildSubsidisedSourceAndDestinationCache()
Perform a full rebuild of the subsidies cache.
Definition subsidy.cpp:103
Functions related to subsidies.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Base of the town class.
Base for the train class.
static constexpr ConsistChangeFlags CCF_TRACK
Valid changes while vehicle is driving, and possibly changing tracks.
Definition train.h:53
Base class for all vehicles.
@ Crashed
Vehicle is crashed.
@ Ship
Ship vehicle type.
@ Aircraft
Aircraft vehicle type.
@ Road
Road vehicle type.
@ Train
Train vehicle type.
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition water_map.h:378