OpenTTD Source 20250905-master-g122023be8d
history.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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11
12#include "../core/bitmath_func.hpp"
13#include "history_type.hpp"
14#include "history_func.hpp"
15
16#include "../safeguards.h"
17
25void UpdateValidHistory(ValidHistoryMask &valid_history, const HistoryRange &hr, uint cur_month)
26{
27 /* Update for subdivisions first. */
28 if (hr.hr != nullptr) UpdateValidHistory(valid_history, *hr.hr, cur_month);
29
30 /* No need to update if our last entry is marked valid. */
31 if (HasBit(valid_history, hr.last - 1)) return;
32 /* Is it the right time for this history range? */
33 if (cur_month % hr.total_division != 0) return;
34 /* Is the previous history range valid yet? */
35 if (hr.division != 1 && !HasBit(valid_history, hr.first - hr.division)) return;
36
37 SB(valid_history, hr.first, hr.records, GB(valid_history, hr.first, hr.records) << 1ULL | 1ULL);
38}
39
47bool IsValidHistory(ValidHistoryMask valid_history, const HistoryRange &hr, uint age)
48{
49 if (hr.hr == nullptr) {
50 if (age < hr.periods) {
51 uint slot = hr.first + age;
52 return HasBit(valid_history, slot);
53 }
54 } else {
55 if (age * hr.division < static_cast<uint>(hr.hr->periods - hr.division)) {
56 uint start = age * hr.division + ((TimerGameEconomy::month / hr.hr->division) % hr.division);
57 return IsValidHistory(valid_history, *hr.hr, start);
58 }
59 if (age < hr.periods) {
60 uint slot = hr.first + age - ((hr.hr->periods / hr.division) - 1);
61 return HasBit(valid_history, slot);
62 }
63 }
64 return false;
65}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static Month month
Current month (0..11).
void UpdateValidHistory(ValidHistoryMask &valid_history, const HistoryRange &hr, uint cur_month)
Update mask of valid records for a historical data.
Definition history.cpp:25
bool IsValidHistory(ValidHistoryMask valid_history, const HistoryRange &hr, uint age)
Test if history data is valid, without extracting data.
Definition history.cpp:47
Functions for storing historical data.
Types for storing historical data.
uint64_t ValidHistoryMask
Mask of valid history records.
const uint8_t last
Index of last element in history data.
const uint8_t total_division
Number of divisions of the initial history range.
const uint8_t division
Number of divisions of the previous history range.
const uint8_t records
Number of records needed for this range.
const uint8_t first
Index of first element in history data.
const uint8_t periods
Number of periods for this range.