OpenTTD Source: src/misc/history.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
25 void 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
47 bool 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}
SB
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.
Definition bitmath_func.hpp:58
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
TimerGameEconomy::month
static Month month
Current month (0..11).
Definition timer_game_economy.h:37
UpdateValidHistory
void UpdateValidHistory(ValidHistoryMask &valid_history, const HistoryRange &hr, uint cur_month)
Update mask of valid records for a historical data.
Definition history.cpp:25
IsValidHistory
bool IsValidHistory(ValidHistoryMask valid_history, const HistoryRange &hr, uint age)
Test if history data is valid, without extracting data.
Definition history.cpp:47
history_func.hpp
Functions for storing historical data.
history_type.hpp
Types for storing historical data.
ValidHistoryMask
uint64_t ValidHistoryMask
Mask of valid history records.
Definition history_type.hpp:56
HistoryRange
Definition history_type.hpp:13
HistoryRange::last
const uint8_t last
Index of last element in history data.
Definition history_type.hpp:18
HistoryRange::total_division
const uint8_t total_division
Number of divisions of the initial history range.
Definition history_type.hpp:20
HistoryRange::division
const uint8_t division
Number of divisions of the previous history range.
Definition history_type.hpp:19
HistoryRange::records
const uint8_t records
Number of records needed for this range.
Definition history_type.hpp:16
HistoryRange::first
const uint8_t first
Index of first element in history data.
Definition history_type.hpp:17
HistoryRange::periods
const uint8_t periods
Number of periods for this range.
Definition history_type.hpp:15