Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / triphistory_cmd.cpp
blobfe251be6fb41f53022901c09510f441a7554e0fe
1 /** @file triphistory_cmd.cpp */
3 #include "stdafx.h"
4 #include "triphistory.h"
5 #include "table/strings.h"
7 void
8 TripHistory::AddValue( Money mvalue, Date dvalue ) {
9 if ( 0 < dvalue ) {
10 t[ 0 ].profit += mvalue;
11 t[ 0 ].date = dvalue;
15 void
16 TripHistory::NewRound( ) {
17 //move down
18 for ( int i = TRIP_LENGTH - 1; i > 0; i-- ) {
19 this->t[ i ] = this->t[ i - 1 ];
20 //this->trip_history_date_array[ i ] = this->trip_history_date_array[ i - 1 ];
23 this->t[ 0 ].profit = 0;
24 this->t[ 0 ].date = this->t[ 1 ].date;
26 //t.push_front( TripHistoryEntry( ) );
29 size_t
30 TripHistory::UpdateCalculated( ) {
32 this->total_profit = 0;
33 this->total_change = 0;
34 this->avg_daylength = 0;
35 this->profit_per_day = 0;
36 uint i = 0;
39 while ( i < TRIP_LENGTH && t [ i ].date ) {
41 if ( i > 0 ) {
42 t[ i - 1 ].profit_change =
43 FindPercentChange( t [ i - 1 ].profit, t[ i ].profit );
44 t[ i - 1 ].TBT = t[ i - 1 ].date - t[ i ].date;
46 if ( i > 1 ) t[ i - 2 ].TBT_change = t[ i - 2 ].TBT - t[ i - 1 ].TBT;//bad line i don't like it
48 //omit first -100% row
49 if ( i > 1 || t [ 0 ].profit_change != -100 )
50 this->total_change += t[ i - 1 ].profit_change;
51 this->avg_daylength += t[ i - 1 ].TBT;
54 // prepare summary
57 this->total_profit += t[ i ].profit;
58 i++;
61 if ( i == 0 ) return 0 ;
63 this->avg_daylength /= --i + 1;
65 if ( t[ 0 ].date != t[ i ].date ) {
66 this->profit_per_day = total_profit / ( t[ 0 ].date - t[ i ].date );
69 return i;
71 Trips::reverse_iterator i = t.rbegin( );
72 while ( i < t.rend( ) ) {
74 if ( i + 1 != t.rend( ) ) {
75 (*( i + 1 )).profit_change =
76 FindPercentChange( ( *i ).profit, ( *( i + 1 ) ).profit ); // reverse_itenrator
78 ( *( i + 1 ) ).TBT = ( *i ).date - ( *( i + 1 ) ).date;
80 if ( ( *i ).TBT ) ( *( i + 1 ) ).TBT_change = ( *i ).TBT - ( *( i + 1 ) ).TBT;
82 //omit first -100% row
83 this->total_change += ( *i ).profit_change;
86 // prepare summary
87 this->total_profit += ( *i ).profit;
88 this->avg_daylength += ( *i ).TBT;
90 i++;
93 this->avg_daylength /= t.size( );
95 if ( t.front( ).date != t.back( ).date ) {
96 this->profit_per_day = total_profit / ( t.front( ).date - t.back( ).date );
99 return t.size( );*/