Update readme.md
[openttd-joker.git] / src / triphistory_cmd.cpp
blob4bb7a613cb14e5e03a71bb9c595fea77d3362169
1 /** @file triphistory_cmd.cpp */
3 #include "stdafx.h"
4 #include "triphistory.h"
5 #include "table/strings.h"
6 #include <numeric>
8 void TripHistory::AddValue(Money profit, Ticks ticks)
10 if (ticks > 0) {
11 this->entries[0].profit += profit;
12 this->entries[0].ticks = ticks;
16 void TripHistory::NewRound()
18 if (this->entries[1].ticks != 0) {
19 this->entries[0].time_between_trips = this->entries[0].ticks - this->entries[1].ticks;
22 if (this->entries[1].profit != 0) {
23 this->entries[0].profit_change = FindPercentChange(this->entries[0].profit, this->entries[1].profit);
26 if (this->entries[1].time_between_trips != 0) {
27 this->entries[0].time_between_trips_change =
28 FindPercentChange(this->entries[0].time_between_trips, this->entries[1].time_between_trips);
31 std::rotate(std::begin(this->entries), std::end(this->entries) - 1, std::end(this->entries));
33 this->entries[0].profit = 0;
34 this->entries[0].ticks = this->entries[1].ticks;
37 /**
38 * Update info for GUI
40 * @return Number of valid rows
42 int32 TripHistory::UpdateCalculated(bool update_entries)
44 if (update_entries) {
45 for (auto i = 0; i < lengthof(this->entries) - 1; ++i) {
46 if (this->entries[i + 1].ticks != 0) {
47 this->entries[i].time_between_trips = this->entries[i].ticks - this->entries[i + 1].ticks;
50 if (this->entries[i + 1].profit != 0) {
51 this->entries[i].profit_change = FindPercentChange(this->entries[i].profit, this->entries[i + 1].profit);
54 if (this->entries[i + 1].time_between_trips != 0) {
55 this->entries[i].time_between_trips_change =
56 FindPercentChange(this->entries[i].time_between_trips, this->entries[i + 1].time_between_trips);
61 // We ignore the first entry since it's still ongoing and would mess up the averages.
62 this->total_profit = std::accumulate(std::begin(this->entries) + 1, std::end(this->entries), (Money)0,
63 [](auto sum, auto entry) { return sum + entry.profit; });
65 auto total_time_between_trips = std::accumulate(std::begin(this->entries) + 1, std::end(this->entries), 0,
66 [](auto sum, auto entry) { return sum + entry.time_between_trips; });
68 auto valid_entries = std::count_if(std::begin(this->entries), std::end(this->entries),
69 [](TripHistoryEntry entry) { return entry.ticks != 0; });
71 auto valid_tbt_entries = std::count_if(std::begin(this->entries) + 1, std::end(this->entries),
72 [](TripHistoryEntry entry) { return entry.time_between_trips != 0; });
74 this->avg_profit = (valid_entries > 1) ? (total_profit / (Money)(valid_entries - 1)) : 0;
75 this->avg_time_between_trips = (valid_tbt_entries > 0) ? (total_time_between_trips / valid_tbt_entries) : 0;
77 return (int32)valid_entries;
80 int32 TripHistory::FindPercentChange(float current_value, float previous_value)
82 return std::round(((current_value - previous_value) * 100.0f) / (float)previous_value);