Changeset - r23058:64521dfbcce5
[Not reviewed]
master
0 3 0
Niels Martin Hansen - 6 years ago 2018-11-01 17:46:19
nielsm@indvikleren.dk
Fix #6498: Use int64 for all company rating calculations
3 files changed with 12 insertions and 12 deletions:
0 comments (0 inline, 0 general)
src/economy.cpp
Show inline comments
 
@@ -74,49 +74,49 @@ INSTANTIATE_POOL_METHODS(CargoPayment)
 
 */
 
static inline int32 BigMulS(const int32 a, const int32 b, const uint8 shift)
 
{
 
	return (int32)((int64)a * (int64)b >> shift);
 
}
 

	
 
typedef SmallVector<Industry *, 16> SmallIndustryList;
 

	
 
/**
 
 * Score info, values used for computing the detailed performance rating.
 
 */
 
const ScoreInfo _score_info[] = {
 
	{     120, 100}, // SCORE_VEHICLES
 
	{      80, 100}, // SCORE_STATIONS
 
	{   10000, 100}, // SCORE_MIN_PROFIT
 
	{   50000,  50}, // SCORE_MIN_INCOME
 
	{  100000, 100}, // SCORE_MAX_INCOME
 
	{   40000, 400}, // SCORE_DELIVERED
 
	{       8,  50}, // SCORE_CARGO
 
	{10000000,  50}, // SCORE_MONEY
 
	{  250000,  50}, // SCORE_LOAN
 
	{       0,   0}  // SCORE_TOTAL
 
};
 

	
 
int _score_part[MAX_COMPANIES][SCORE_END];
 
int64 _score_part[MAX_COMPANIES][SCORE_END];
 
Economy _economy;
 
Prices _price;
 
Money _additional_cash_required;
 
static PriceMultipliers _price_base_multiplier;
 

	
 
/**
 
 * Calculate the value of the company. That is the value of all
 
 * assets (vehicles, stations, etc) and money minus the loan,
 
 * except when including_loan is \c false which is useful when
 
 * we want to calculate the value for bankruptcy.
 
 * @param c              the company to get the value of.
 
 * @param including_loan include the loan in the company value.
 
 * @return the value of the company.
 
 */
 
Money CalculateCompanyValue(const Company *c, bool including_loan)
 
{
 
	Owner owner = c->index;
 

	
 
	Station *st;
 
	uint num = 0;
 

	
 
	FOR_ALL_STATIONS(st) {
 
		if (st->owner == owner) num += CountBits((byte)st->facilities);
 
	}
 
@@ -162,114 +162,114 @@ int UpdateCompanyRatingAndValue(Company 
 
		Vehicle *v;
 
		Money min_profit = 0;
 
		bool min_profit_first = true;
 
		uint num = 0;
 

	
 
		FOR_ALL_VEHICLES(v) {
 
			if (v->owner != owner) continue;
 
			if (IsCompanyBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) {
 
				if (v->profit_last_year > 0) num++; // For the vehicle score only count profitable vehicles
 
				if (v->age > 730) {
 
					/* Find the vehicle with the lowest amount of profit */
 
					if (min_profit_first || min_profit > v->profit_last_year) {
 
						min_profit = v->profit_last_year;
 
						min_profit_first = false;
 
					}
 
				}
 
			}
 
		}
 

	
 
		min_profit >>= 8; // remove the fract part
 

	
 
		_score_part[owner][SCORE_VEHICLES] = num;
 
		/* Don't allow negative min_profit to show */
 
		if (min_profit > 0) {
 
			_score_part[owner][SCORE_MIN_PROFIT] = ClampToI32(min_profit);
 
			_score_part[owner][SCORE_MIN_PROFIT] = min_profit;
 
		}
 
	}
 

	
 
	/* Count stations */
 
	{
 
		uint num = 0;
 
		const Station *st;
 

	
 
		FOR_ALL_STATIONS(st) {
 
			/* Only count stations that are actually serviced */
 
			if (st->owner == owner && (st->time_since_load <= 20 || st->time_since_unload <= 20)) num += CountBits((byte)st->facilities);
 
		}
 
		_score_part[owner][SCORE_STATIONS] = num;
 
	}
 

	
 
	/* Generate statistics depending on recent income statistics */
 
	{
 
		int numec = min(c->num_valid_stat_ent, 12);
 
		if (numec != 0) {
 
			const CompanyEconomyEntry *cee = c->old_economy;
 
			Money min_income = cee->income + cee->expenses;
 
			Money max_income = cee->income + cee->expenses;
 

	
 
			do {
 
				min_income = min(min_income, cee->income + cee->expenses);
 
				max_income = max(max_income, cee->income + cee->expenses);
 
			} while (++cee, --numec);
 

	
 
			if (min_income > 0) {
 
				_score_part[owner][SCORE_MIN_INCOME] = ClampToI32(min_income);
 
				_score_part[owner][SCORE_MIN_INCOME] = min_income;
 
			}
 

	
 
			_score_part[owner][SCORE_MAX_INCOME] = ClampToI32(max_income);
 
			_score_part[owner][SCORE_MAX_INCOME] = max_income;
 
		}
 
	}
 

	
 
	/* Generate score depending on amount of transported cargo */
 
	{
 
		int numec = min(c->num_valid_stat_ent, 4);
 
		if (numec != 0) {
 
			const CompanyEconomyEntry *cee = c->old_economy;
 
			OverflowSafeInt64 total_delivered = 0;
 
			do {
 
				total_delivered += cee->delivered_cargo.GetSum<OverflowSafeInt64>();
 
			} while (++cee, --numec);
 

	
 
			_score_part[owner][SCORE_DELIVERED] = ClampToI32(total_delivered);
 
			_score_part[owner][SCORE_DELIVERED] = total_delivered;
 
		}
 
	}
 

	
 
	/* Generate score for variety of cargo */
 
	{
 
		_score_part[owner][SCORE_CARGO] = c->old_economy->delivered_cargo.GetCount();
 
	}
 

	
 
	/* Generate score for company's money */
 
	{
 
		if (c->money > 0) {
 
			_score_part[owner][SCORE_MONEY] = ClampToI32(c->money);
 
			_score_part[owner][SCORE_MONEY] = c->money;
 
		}
 
	}
 

	
 
	/* Generate score for loan */
 
	{
 
		_score_part[owner][SCORE_LOAN] = ClampToI32(_score_info[SCORE_LOAN].needed - c->current_loan);
 
		_score_part[owner][SCORE_LOAN] = _score_info[SCORE_LOAN].needed - c->current_loan;
 
	}
 

	
 
	/* Now we calculate the score for each item.. */
 
	{
 
		int total_score = 0;
 
		int s;
 
		score = 0;
 
		for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
 
			/* Skip the total */
 
			if (i == SCORE_TOTAL) continue;
 
			/*  Check the score */
 
			s = Clamp(_score_part[owner][i], 0, _score_info[i].needed) * _score_info[i].score / _score_info[i].needed;
 
			score += s;
 
			total_score += _score_info[i].score;
 
		}
 

	
 
		_score_part[owner][SCORE_TOTAL] = score;
 

	
 
		/*  We always want the score scaled to SCORE_MAX (1000) */
 
		if (total_score != SCORE_MAX) score = score * SCORE_MAX / total_score;
 
	}
 

	
 
	if (update) {
 
		c->old_economy[0].performance_history = score;
src/economy_func.h
Show inline comments
 
/* $Id$ */
 

	
 
/*
 
 * This file is part of OpenTTD.
 
 * 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.
 
 * 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.
 
 * 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/>.
 
 */
 

	
 
/** @file economy_func.h Functions related to the economy. */
 

	
 
#ifndef ECONOMY_FUNC_H
 
#define ECONOMY_FUNC_H
 

	
 
#include "economy_type.h"
 
#include "station_type.h"
 
#include "cargo_type.h"
 
#include "vehicle_type.h"
 
#include "company_type.h"
 

	
 
void ResetPriceBaseMultipliers();
 
void SetPriceBaseMultiplier(Price price, int factor);
 

	
 
extern const ScoreInfo _score_info[];
 
extern int _score_part[MAX_COMPANIES][SCORE_END];
 
extern int64 _score_part[MAX_COMPANIES][SCORE_END];
 
extern Economy _economy;
 
/* Prices and also the fractional part. */
 
extern Prices _price;
 

	
 
int UpdateCompanyRatingAndValue(Company *c, bool update);
 
void StartupIndustryDailyChanges(bool init_counter);
 

	
 
Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type);
 
uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations);
 

	
 
void PrepareUnload(Vehicle *front_v);
 
void LoadUnloadStation(Station *st);
 

	
 
Money GetPrice(Price index, uint cost_factor, const struct GRFFile *grf_file, int shift = 0);
 

	
 
void InitializeEconomy();
 
void RecomputePrices();
 
bool AddInflation(bool check_year = true);
 

	
 
/**
 
 * Is the economy in recession?
 
 * @return \c True if economy is in recession, \c false otherwise.
 
 */
 
static inline bool EconomyIsInRecession()
src/graph_gui.cpp
Show inline comments
 
@@ -1381,82 +1381,82 @@ struct PerformanceRatingDetailWindow : W
 

	
 
	virtual void DrawWidget(const Rect &r, int widget) const
 
	{
 
		/* No need to draw when there's nothing to draw */
 
		if (this->company == INVALID_COMPANY) return;
 

	
 
		if (IsInsideMM(widget, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST + 1)) {
 
			if (this->IsWidgetDisabled(widget)) return;
 
			CompanyID cid = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
 
			int offset = (cid == this->company) ? 1 : 0;
 
			Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
 
			DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
 
			return;
 
		}
 

	
 
		if (!IsInsideMM(widget, WID_PRD_SCORE_FIRST, WID_PRD_SCORE_LAST + 1)) return;
 

	
 
		ScoreID score_type = (ScoreID)(widget - WID_PRD_SCORE_FIRST);
 

	
 
		/* The colours used to show how the progress is going */
 
		int colour_done = _colour_gradient[COLOUR_GREEN][4];
 
		int colour_notdone = _colour_gradient[COLOUR_RED][4];
 

	
 
		/* Draw all the score parts */
 
		int val    = _score_part[company][score_type];
 
		int needed = _score_info[score_type].needed;
 
		int64 val    = _score_part[company][score_type];
 
		int64 needed = _score_info[score_type].needed;
 
		int score  = _score_info[score_type].score;
 

	
 
		/* SCORE_TOTAL has his own rules ;) */
 
		if (score_type == SCORE_TOTAL) {
 
			for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
 
			needed = SCORE_MAX;
 
		}
 

	
 
		uint bar_top  = r.top + WD_MATRIX_TOP;
 
		uint text_top = bar_top + 2;
 

	
 
		DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
 

	
 
		/* Draw the score */
 
		SetDParam(0, score);
 
		DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING, SA_RIGHT);
 

	
 
		/* Calculate the %-bar */
 
		uint x = Clamp(val, 0, needed) * this->bar_width / needed;
 
		uint x = Clamp<int64>(val, 0, needed) * this->bar_width / needed;
 
		bool rtl = _current_text_dir == TD_RTL;
 
		if (rtl) {
 
			x = this->bar_right - x;
 
		} else {
 
			x = this->bar_left + x;
 
		}
 

	
 
		/* Draw the bar */
 
		if (x != this->bar_left)  GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
 
		if (x != this->bar_right) GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
 

	
 
		/* Draw it */
 
		SetDParam(0, Clamp(val, 0, needed) * 100 / needed);
 
		SetDParam(0, Clamp<int64>(val, 0, needed) * 100 / needed);
 
		DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING, SA_HOR_CENTER);
 

	
 
		/* SCORE_LOAN is inversed */
 
		if (score_type == SCORE_LOAN) val = needed - val;
 

	
 
		/* Draw the amount we have against what is needed
 
		 * For some of them it is in currency format */
 
		SetDParam(0, val);
 
		SetDParam(1, needed);
 
		switch (score_type) {
 
			case SCORE_MIN_PROFIT:
 
			case SCORE_MIN_INCOME:
 
			case SCORE_MAX_INCOME:
 
			case SCORE_MONEY:
 
			case SCORE_LOAN:
 
				DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
 
				break;
 
			default:
 
				DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
 
		}
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget, int click_count)
 
	{
0 comments (0 inline, 0 general)