2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file economy.cpp Handling of the economy. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "tile_cmd.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2008-10-25 14:24:50 +00:00
|
|
|
#include "industry_map.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "town.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network/network.h"
|
2008-05-30 18:20:26 +00:00
|
|
|
#include "network/network_func.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "vehicle_gui.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2007-01-27 12:29:55 +00:00
|
|
|
#include "aircraft.h"
|
2009-05-22 22:55:41 +00:00
|
|
|
#include "train.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2006-10-01 17:56:38 +00:00
|
|
|
#include "newgrf_sound.h"
|
2007-07-05 05:41:56 +00:00
|
|
|
#include "newgrf_industries.h"
|
2007-07-11 15:03:29 +00:00
|
|
|
#include "newgrf_industrytiles.h"
|
2008-04-19 23:19:12 +00:00
|
|
|
#include "newgrf_station.h"
|
2006-03-31 08:59:19 +00:00
|
|
|
#include "unmovable.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "group.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "gfx_func.h"
|
2008-04-26 14:20:39 +00:00
|
|
|
#include "autoreplace_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_gui.h"
|
2008-09-16 20:57:49 +00:00
|
|
|
#include "signs_base.h"
|
2009-07-01 18:45:05 +00:00
|
|
|
#include "subsidy_base.h"
|
2009-05-23 15:46:00 +00:00
|
|
|
#include "subsidy_func.h"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "station_base.h"
|
2009-07-22 10:18:19 +00:00
|
|
|
#include "waypoint_base.h"
|
2009-06-28 15:12:59 +00:00
|
|
|
#include "economy_base.h"
|
2009-06-29 19:55:36 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
|
|
|
|
/* Initialize the cargo payment-pool */
|
|
|
|
CargoPaymentPool _cargo_payment_pool("CargoPayment");
|
|
|
|
INSTANTIATE_POOL_METHODS(CargoPayment)
|
|
|
|
|
2007-11-19 20:18:27 +00:00
|
|
|
/**
|
|
|
|
* Multiply two integer values and shift the results to right.
|
|
|
|
*
|
|
|
|
* This function multiplies two integer values. The result is
|
|
|
|
* shifted by the amount of shift to right.
|
|
|
|
*
|
|
|
|
* @param a The first integer
|
|
|
|
* @param b The second integer
|
|
|
|
* @param shift The amount to shift the value to right.
|
|
|
|
* @return The shifted result
|
|
|
|
*/
|
|
|
|
static inline int32 BigMulS(const int32 a, const int32 b, const uint8 shift)
|
|
|
|
{
|
|
|
|
return (int32)((int64)a * (int64)b >> shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply two unsigned integers and shift the results to right.
|
|
|
|
*
|
|
|
|
* This function multiplies two unsigned integers. The result is
|
|
|
|
* shifted by the amount of shift to right.
|
|
|
|
*
|
|
|
|
* @param a The first unsigned integer
|
|
|
|
* @param b The second unsigned integer
|
|
|
|
* @param shift The amount to shift the value to right.
|
|
|
|
* @return The shifted result
|
|
|
|
*/
|
|
|
|
static inline uint32 BigMulSU(const uint32 a, const uint32 b, const uint8 shift)
|
|
|
|
{
|
|
|
|
return (uint32)((uint64)a * (uint64)b >> shift);
|
|
|
|
}
|
|
|
|
|
2008-12-20 17:21:22 +00:00
|
|
|
typedef SmallVector<Industry *, 16> SmallIndustryList;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Score info */
|
2005-08-01 16:31:19 +00:00
|
|
|
const ScoreInfo _score_info[] = {
|
2006-06-27 21:25:53 +00:00
|
|
|
{ SCORE_VEHICLES, 120, 100 },
|
|
|
|
{ SCORE_STATIONS, 80, 100 },
|
|
|
|
{ SCORE_MIN_PROFIT, 10000, 100 },
|
|
|
|
{ SCORE_MIN_INCOME, 50000, 50 },
|
|
|
|
{ SCORE_MAX_INCOME, 100000, 100 },
|
|
|
|
{ SCORE_DELIVERED, 40000, 400 },
|
|
|
|
{ SCORE_CARGO, 8, 50 },
|
|
|
|
{ SCORE_MONEY, 10000000, 50 },
|
|
|
|
{ SCORE_LOAN, 250000, 50 },
|
|
|
|
{ SCORE_TOTAL, 0, 0 }
|
2005-08-01 16:31:19 +00:00
|
|
|
};
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
int _score_part[MAX_COMPANIES][SCORE_END];
|
2007-12-21 21:50:46 +00:00
|
|
|
Economy _economy;
|
|
|
|
Prices _price;
|
|
|
|
uint16 _price_frac[NUM_PRICES];
|
2007-12-21 22:50:51 +00:00
|
|
|
Money _cargo_payment_rates[NUM_CARGO];
|
|
|
|
uint16 _cargo_payment_rates_frac[NUM_CARGO];
|
|
|
|
Money _additional_cash_required;
|
2005-08-01 16:31:19 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
Money CalculateCompanyValue(const Company *c)
|
2005-09-30 20:37:25 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
Owner owner = c->index;
|
2007-10-21 16:44:19 +00:00
|
|
|
Money value = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-21 16:44:19 +00:00
|
|
|
Station *st;
|
|
|
|
uint num = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-21 16:44:19 +00:00
|
|
|
FOR_ALL_STATIONS(st) {
|
2009-07-04 11:26:57 +00:00
|
|
|
if (st->owner == owner) num += CountBits((byte)st->facilities);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-21 16:44:19 +00:00
|
|
|
value += num * _price.station_value * 25;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-21 16:44:19 +00:00
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->owner != owner) continue;
|
|
|
|
|
|
|
|
if (v->type == VEH_TRAIN ||
|
|
|
|
v->type == VEH_ROAD ||
|
2009-07-13 16:37:27 +00:00
|
|
|
(v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft()) ||
|
2007-10-21 16:44:19 +00:00
|
|
|
v->type == VEH_SHIP) {
|
|
|
|
value += v->value * 3 >> 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 21:44:47 +00:00
|
|
|
/* Add real money value */
|
2008-09-30 20:39:50 +00:00
|
|
|
value -= c->current_loan;
|
|
|
|
value += c->money;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2007-10-21 16:44:19 +00:00
|
|
|
return max(value, (Money)1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/** if update is set to true, the economy is updated with this score
|
|
|
|
* (also the house is updated, should only be true in the on-tick event)
|
|
|
|
* @param update the economy with calculated score
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param c company been evaluated
|
|
|
|
* @return actual score of this company
|
2007-02-23 18:55:07 +00:00
|
|
|
* */
|
2008-09-30 20:39:50 +00:00
|
|
|
int UpdateCompanyRatingAndValue(Company *c, bool update)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
Owner owner = c->index;
|
2004-08-23 10:59:03 +00:00
|
|
|
int score = 0;
|
|
|
|
|
|
|
|
memset(_score_part[owner], 0, sizeof(_score_part[owner]));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Count vehicles */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2007-06-21 14:32:27 +00:00
|
|
|
Money min_profit = 0;
|
2006-01-06 22:16:17 +00:00
|
|
|
bool min_profit_first = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint num = 0;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2006-06-27 21:25:53 +00:00
|
|
|
if (v->owner != owner) continue;
|
2008-09-30 20:39:50 +00:00
|
|
|
if (IsCompanyBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) {
|
2004-08-09 17:04:08 +00:00
|
|
|
num++;
|
|
|
|
if (v->age > 730) {
|
2006-01-06 22:16:17 +00:00
|
|
|
/* Find the vehicle with the lowest amount of profit */
|
2008-02-20 17:06:58 +00:00
|
|
|
if (min_profit_first || min_profit > v->profit_last_year) {
|
|
|
|
min_profit = v->profit_last_year;
|
2006-01-06 22:16:17 +00:00
|
|
|
min_profit_first = false;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 17:06:58 +00:00
|
|
|
min_profit >>= 8; // remove the fract part
|
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_VEHICLES] = num;
|
2006-01-06 22:16:17 +00:00
|
|
|
/* Don't allow negative min_profit to show */
|
2005-02-23 14:12:32 +00:00
|
|
|
if (min_profit > 0)
|
2007-06-21 14:32:27 +00:00
|
|
|
_score_part[owner][SCORE_MIN_PROFIT] = ClampToI32(min_profit);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Count stations */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint num = 0;
|
2009-01-10 00:31:47 +00:00
|
|
|
const Station *st;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2009-07-04 11:26:57 +00:00
|
|
|
if (st->owner == owner) num += CountBits((byte)st->facilities);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_STATIONS] = num;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Generate statistics depending on recent income statistics */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
int numec = min(c->num_valid_stat_ent, 12);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (numec != 0) {
|
2008-09-30 20:39:50 +00:00
|
|
|
const CompanyEconomyEntry *cee = c->old_economy;
|
|
|
|
Money min_income = cee->income + cee->expenses;
|
|
|
|
Money max_income = cee->income + cee->expenses;
|
2007-06-18 21:44:47 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2008-09-30 20:39:50 +00:00
|
|
|
min_income = min(min_income, cee->income + cee->expenses);
|
|
|
|
max_income = max(max_income, cee->income + cee->expenses);
|
2009-03-14 18:16:29 +00:00
|
|
|
} while (++cee, --numec);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (min_income > 0) {
|
2007-06-21 14:32:27 +00:00
|
|
|
_score_part[owner][SCORE_MIN_INCOME] = ClampToI32(min_income);
|
2008-09-30 20:39:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-21 14:32:27 +00:00
|
|
|
_score_part[owner][SCORE_MAX_INCOME] = ClampToI32(max_income);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Generate score depending on amount of transported cargo */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
const CompanyEconomyEntry *cee;
|
2004-08-09 17:04:08 +00:00
|
|
|
int numec;
|
|
|
|
uint32 total_delivered;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
numec = min(c->num_valid_stat_ent, 4);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (numec != 0) {
|
2008-09-30 20:39:50 +00:00
|
|
|
cee = c->old_economy;
|
2004-08-09 17:04:08 +00:00
|
|
|
total_delivered = 0;
|
|
|
|
do {
|
2008-09-30 20:39:50 +00:00
|
|
|
total_delivered += cee->delivered_cargo;
|
2009-03-14 18:16:29 +00:00
|
|
|
} while (++cee, --numec);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_DELIVERED] = total_delivered;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-23 10:59:03 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Generate score for variety of cargo */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
uint num = CountBits(c->cargo_types);
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_CARGO] = num;
|
2008-09-30 20:39:50 +00:00
|
|
|
if (update) c->cargo_types = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Generate score for company's money */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->money > 0) {
|
|
|
|
_score_part[owner][SCORE_MONEY] = ClampToI32(c->money);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Generate score for loan */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
_score_part[owner][SCORE_LOAN] = ClampToI32(_score_info[SCORE_LOAN].needed - c->current_loan);
|
2004-08-23 10:59:03 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Now we calculate the score for each item.. */
|
2004-08-23 10:59:03 +00:00
|
|
|
{
|
|
|
|
int total_score = 0;
|
|
|
|
int s;
|
|
|
|
score = 0;
|
2007-01-10 18:56:51 +00:00
|
|
|
for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Skip the total */
|
2004-08-23 10:59:03 +00:00
|
|
|
if (i == SCORE_TOTAL) continue;
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Check the score */
|
2007-11-19 18:38:10 +00:00
|
|
|
s = Clamp(_score_part[owner][i], 0, _score_info[i].needed) * _score_info[i].score / _score_info[i].needed;
|
2004-08-23 10:59:03 +00:00
|
|
|
score += s;
|
2005-08-01 16:31:19 +00:00
|
|
|
total_score += _score_info[i].score;
|
2004-08-23 10:59:03 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_TOTAL] = score;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* We always want the score scaled to SCORE_MAX (1000) */
|
2006-02-18 14:41:24 +00:00
|
|
|
if (total_score != SCORE_MAX) score = score * SCORE_MAX / total_score;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
if (update) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->old_economy[0].performance_history = score;
|
|
|
|
UpdateCompanyHQ(c, score);
|
|
|
|
c->old_economy[0].company_value = CalculateCompanyValue(c);
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
InvalidateWindow(WC_PERFORMANCE_DETAIL, 0);
|
2004-09-01 21:54:12 +00:00
|
|
|
return score;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* use INVALID_OWNER as new_owner to delete the company. */
|
|
|
|
void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-12-08 12:31:34 +00:00
|
|
|
Town *t;
|
2008-09-30 20:39:50 +00:00
|
|
|
CompanyID old = _current_company;
|
2007-03-31 12:19:22 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
assert(old_owner != new_owner);
|
2007-04-01 10:55:31 +00:00
|
|
|
|
2007-03-31 12:19:22 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c;
|
2007-03-31 12:19:22 +00:00
|
|
|
uint i;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* See if the old_owner had shares in other companies */
|
|
|
|
_current_company = old_owner;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
2007-03-31 12:19:22 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->share_owners[i] == old_owner) {
|
2007-03-31 12:19:22 +00:00
|
|
|
/* Sell his shares */
|
2008-09-30 20:39:50 +00:00
|
|
|
CommandCost res = DoCommand(0, c->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
|
2007-03-31 12:19:22 +00:00
|
|
|
/* Because we are in a DoCommand, we can't just execute an other one and
|
|
|
|
* expect the money to be removed. We need to do it ourself! */
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompany(res);
|
2007-03-31 12:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sell all the shares that people have on this company */
|
2009-05-16 23:34:14 +00:00
|
|
|
c = Company::Get(old_owner);
|
2007-03-31 12:19:22 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2008-09-30 20:39:50 +00:00
|
|
|
_current_company = c->share_owners[i];
|
|
|
|
if (_current_company != INVALID_OWNER) {
|
2007-03-31 12:19:22 +00:00
|
|
|
/* Sell the shares */
|
2008-09-30 20:39:50 +00:00
|
|
|
CommandCost res = DoCommand(0, old_owner, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
|
2007-03-31 12:19:22 +00:00
|
|
|
/* Because we are in a DoCommand, we can't just execute an other one and
|
|
|
|
* expect the money to be removed. We need to do it ourself! */
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompany(res);
|
2007-03-31 12:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
_current_company = old_owner;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Temporarily increase the company's money, to be sure that
|
2006-09-04 20:40:33 +00:00
|
|
|
* removing his/her property doesn't fail because of lack of money.
|
|
|
|
* Not too drastically though, because it could overflow */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (new_owner == INVALID_OWNER) {
|
2009-05-16 23:34:14 +00:00
|
|
|
Company::Get(old_owner)->money = UINT64_MAX >> 2; // jackpot ;p
|
2006-08-14 11:44:19 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (new_owner == INVALID_OWNER) {
|
2004-08-09 17:04:08 +00:00
|
|
|
Subsidy *s;
|
2009-07-01 17:43:26 +00:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 19:54:35 +00:00
|
|
|
if (s->IsAwarded() && Station::Get(s->to)->owner == old_owner) {
|
2009-07-01 17:43:26 +00:00
|
|
|
s->cargo_type = CT_INVALID;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-12 23:47:45 +00:00
|
|
|
/* Take care of rating in towns */
|
2006-12-08 12:31:34 +00:00
|
|
|
FOR_ALL_TOWNS(t) {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* If a company takes over, give the ratings to that company. */
|
|
|
|
if (new_owner != INVALID_OWNER) {
|
|
|
|
if (HasBit(t->have_ratings, old_owner)) {
|
|
|
|
if (HasBit(t->have_ratings, new_owner)) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* use max of the two ratings. */
|
2008-09-30 20:39:50 +00:00
|
|
|
t->ratings[new_owner] = max(t->ratings[new_owner], t->ratings[old_owner]);
|
2006-08-14 11:44:19 +00:00
|
|
|
} else {
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(t->have_ratings, new_owner);
|
|
|
|
t->ratings[new_owner] = t->ratings[old_owner];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-08-14 11:44:19 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-12-08 12:31:34 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Reset the ratings for the old owner */
|
|
|
|
t->ratings[old_owner] = RATING_INITIAL;
|
|
|
|
ClrBit(t->have_ratings, old_owner);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-12-26 23:53:07 +00:00
|
|
|
FreeUnitIDGenerator unitidgen[] = {
|
|
|
|
FreeUnitIDGenerator(VEH_TRAIN, new_owner), FreeUnitIDGenerator(VEH_ROAD, new_owner),
|
|
|
|
FreeUnitIDGenerator(VEH_SHIP, new_owner), FreeUnitIDGenerator(VEH_AIRCRAFT, new_owner)
|
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-12-26 23:53:07 +00:00
|
|
|
Vehicle *v;
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_VEHICLES(v) {
|
2008-12-26 20:45:02 +00:00
|
|
|
if (v->owner == old_owner && IsCompanyBuildableVehicleType(v->type)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (new_owner == INVALID_OWNER) {
|
2008-12-26 20:45:02 +00:00
|
|
|
if (v->Previous() == NULL) delete v;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2008-09-30 20:39:50 +00:00
|
|
|
v->owner = new_owner;
|
2009-02-09 02:57:15 +00:00
|
|
|
v->colourmap = PAL_NONE;
|
2009-07-13 16:35:22 +00:00
|
|
|
if (v->IsEngineCountable()) Company::Get(new_owner)->num_engines[v->engine_type]++;
|
2008-12-26 23:53:07 +00:00
|
|
|
if (v->IsPrimaryVehicle()) v->unitnumber = unitidgen[v->type].NextID();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Change ownership of tiles */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-12 23:47:45 +00:00
|
|
|
TileIndex tile = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2008-09-30 20:39:50 +00:00
|
|
|
ChangeTileOwner(tile, old_owner, new_owner);
|
2005-01-03 18:59:58 +00:00
|
|
|
} while (++tile != MapSize());
|
2008-01-05 22:04:11 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (new_owner != INVALID_OWNER) {
|
|
|
|
/* Update all signals because there can be new segment that was owned by two companies
|
2008-01-17 19:49:06 +00:00
|
|
|
* and signals were not propagated
|
|
|
|
* Similiar with crossings - it is needed to bar crossings that weren't before
|
|
|
|
* because of different owner of crossing and approaching train */
|
2008-01-05 22:04:11 +00:00
|
|
|
tile = 0;
|
|
|
|
|
|
|
|
do {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, new_owner) && HasSignals(tile)) {
|
2008-01-05 22:04:11 +00:00
|
|
|
TrackBits tracks = GetTrackBits(tile);
|
|
|
|
do { // there may be two tracks with signals for TRACK_BIT_HORZ and TRACK_BIT_VERT
|
|
|
|
Track track = RemoveFirstTrack(&tracks);
|
2008-09-30 20:39:50 +00:00
|
|
|
if (HasSignalOnTrack(tile, track)) AddTrackToSignalBuffer(tile, track, new_owner);
|
2008-01-05 22:04:11 +00:00
|
|
|
} while (tracks != TRACK_BIT_NONE);
|
2008-09-30 20:39:50 +00:00
|
|
|
} else if (IsLevelCrossingTile(tile) && IsTileOwner(tile, new_owner)) {
|
2008-01-17 19:49:06 +00:00
|
|
|
UpdateLevelCrossing(tile);
|
2008-01-05 22:04:11 +00:00
|
|
|
}
|
|
|
|
} while (++tile != MapSize());
|
|
|
|
}
|
2008-01-18 02:16:39 +00:00
|
|
|
|
|
|
|
/* update signals in buffer */
|
|
|
|
UpdateSignalsInBuffer();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-26 14:07:42 +00:00
|
|
|
/* convert owner of stations (including deleted ones, but excluding buoys) */
|
|
|
|
Station *st;
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->owner == old_owner) {
|
|
|
|
/* if a company goes bankrupt, set owner to OWNER_NONE so the sign doesn't disappear immediately
|
|
|
|
* also, drawing station window would cause reading invalid company's colour */
|
|
|
|
st->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the same for waypoints (we need to do this here so deleted waypoints are converted too) */
|
|
|
|
Waypoint *wp;
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
|
|
|
if (wp->owner == old_owner) {
|
|
|
|
wp->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-26 14:20:39 +00:00
|
|
|
/* In all cases clear replace engine rules.
|
|
|
|
* Even if it was copied, it could interfere with new owner's rules */
|
2009-05-16 23:34:14 +00:00
|
|
|
RemoveAllEngineReplacementForCompany(Company::Get(old_owner));
|
2008-04-26 14:20:39 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (new_owner == INVALID_OWNER) {
|
|
|
|
RemoveAllGroupsForCompany(old_owner);
|
2008-04-26 14:20:39 +00:00
|
|
|
} else {
|
|
|
|
Group *g;
|
|
|
|
FOR_ALL_GROUPS(g) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (g->owner == old_owner) g->owner = new_owner;
|
2008-04-26 14:20:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-16 20:57:49 +00:00
|
|
|
Sign *si;
|
|
|
|
FOR_ALL_SIGNS(si) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (si->owner == old_owner) si->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
|
2008-09-16 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Change colour of existing windows */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (new_owner != INVALID_OWNER) ChangeWindowOwner(old_owner, new_owner);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
_current_company = old;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void ChangeNetworkOwner(Owner current_owner, Owner new_owner)
|
2007-03-08 21:05:05 +00:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
if (!_networking) return;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (current_owner == _local_company) {
|
|
|
|
SetLocalCompany(new_owner);
|
2007-03-08 21:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_network_server) return;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
NetworkServerChangeOwner(current_owner, new_owner);
|
2007-03-08 21:05:05 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void CompanyCheckBankrupt(Company *c)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
/* If the company has money again, it does not go bankrupt */
|
|
|
|
if (c->money >= 0) {
|
|
|
|
c->quarters_of_bankrupcy = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
c->quarters_of_bankrupcy++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-07-18 16:26:51 +00:00
|
|
|
CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
|
2008-09-30 20:39:50 +00:00
|
|
|
cni->FillData(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
switch (c->quarters_of_bankrupcy) {
|
2008-12-26 21:49:00 +00:00
|
|
|
case 0:
|
|
|
|
case 1:
|
2008-07-18 16:26:51 +00:00
|
|
|
free(cni);
|
|
|
|
break;
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
case 2:
|
2009-04-21 23:40:56 +00:00
|
|
|
SetDParam(0, STR_NEWS_COMPANY_IN_TROUBLE_TITLE);
|
|
|
|
SetDParam(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION);
|
2008-07-18 16:26:51 +00:00
|
|
|
SetDParamStr(2, cni->company_name);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddCompanyNewsItem(STR_NEWS_MESSAGE, NS_COMPANY_TROUBLE, cni);
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::BroadcastNewEvent(new AIEventCompanyInTrouble(c->index));
|
2004-12-04 17:54:56 +00:00
|
|
|
break;
|
|
|
|
case 3: {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* XXX - In multiplayer, should we ask other companies if it wants to take
|
2004-12-04 17:54:56 +00:00
|
|
|
over when it is a human company? -- TrueLight */
|
2009-06-10 22:05:01 +00:00
|
|
|
if (!c->is_ai) {
|
2009-04-21 23:40:56 +00:00
|
|
|
SetDParam(0, STR_NEWS_COMPANY_IN_TROUBLE_TITLE);
|
|
|
|
SetDParam(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION);
|
2008-07-18 16:26:51 +00:00
|
|
|
SetDParamStr(2, cni->company_name);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddCompanyNewsItem(STR_NEWS_MESSAGE, NS_COMPANY_TROUBLE, cni);
|
2004-12-04 17:54:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Check if the company has any value.. if not, declare it bankrupt
|
|
|
|
* right now */
|
2008-09-30 20:39:50 +00:00
|
|
|
Money val = CalculateCompanyValue(c);
|
2004-12-04 17:54:56 +00:00
|
|
|
if (val > 0) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->bankrupt_value = val;
|
|
|
|
c->bankrupt_asked = 1 << c->index; // Don't ask the owner
|
|
|
|
c->bankrupt_timeout = 0;
|
2008-07-18 16:26:51 +00:00
|
|
|
free(cni);
|
2004-12-04 17:54:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Else, falltrue to case 4... */
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
2008-12-26 21:49:00 +00:00
|
|
|
default:
|
|
|
|
case 4:
|
|
|
|
if (!_networking && _local_company == c->index) {
|
|
|
|
/* If we are in offline mode, leave the company playing. Eg. there
|
|
|
|
* is no THE-END, otherwise mark the client as spectator to make sure
|
|
|
|
* he/she is no long in control of this company. However... when you
|
|
|
|
* join another company (cheat) the "unowned" company can bankrupt. */
|
|
|
|
c->bankrupt_asked = MAX_UVALUE(CompanyMask);
|
|
|
|
c->bankrupt_timeout = 0x456;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Close everything the owner has open */
|
2008-09-30 20:39:50 +00:00
|
|
|
DeleteCompanyWindows(c->index);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Show bankrupt news */
|
2009-04-21 23:40:56 +00:00
|
|
|
SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_TITLE);
|
|
|
|
SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_DESCRIPTION);
|
2008-07-18 16:26:51 +00:00
|
|
|
SetDParamStr(2, cni->company_name);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddCompanyNewsItem(STR_NEWS_MESSAGE, NS_COMPANY_BANKRUPT, cni);
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Remove the company */
|
2008-12-26 21:49:00 +00:00
|
|
|
ChangeNetworkOwner(c->index, COMPANY_SPECTATOR);
|
2008-09-30 20:39:50 +00:00
|
|
|
ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
|
2005-09-07 15:10:11 +00:00
|
|
|
|
2009-06-10 22:05:01 +00:00
|
|
|
if (c->is_ai) AI::Stop(c->index);
|
2008-07-18 16:40:29 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
CompanyID c_index = c->index;
|
2008-09-30 20:39:50 +00:00
|
|
|
delete c;
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void CompaniesGenStatistics()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *c;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2008-09-30 20:39:50 +00:00
|
|
|
_current_company = st->owner;
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost cost(EXPENSES_PROPERTY, _price.station_value >> 1);
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompany(cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 02:53:18 +00:00
|
|
|
if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_month))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
memmove(&c->old_economy[1], &c->old_economy[0], sizeof(c->old_economy) - sizeof(c->old_economy[0]));
|
|
|
|
c->old_economy[0] = c->cur_economy;
|
|
|
|
memset(&c->cur_economy, 0, sizeof(c->cur_economy));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->num_valid_stat_ent != 24) c->num_valid_stat_ent++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
UpdateCompanyRatingAndValue(c, true);
|
|
|
|
if (c->block_preview != 0) c->block_preview--;
|
2009-05-27 23:37:47 +00:00
|
|
|
CompanyCheckBankrupt(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindow(WC_INCOME_GRAPH, 0);
|
|
|
|
InvalidateWindow(WC_OPERATING_PROFIT, 0);
|
|
|
|
InvalidateWindow(WC_DELIVERED_CARGO, 0);
|
|
|
|
InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
|
|
|
|
InvalidateWindow(WC_COMPANY_VALUE, 0);
|
|
|
|
InvalidateWindow(WC_COMPANY_LEAGUE, 0);
|
|
|
|
}
|
|
|
|
|
2007-06-18 21:44:47 +00:00
|
|
|
static void AddSingleInflation(Money *value, uint16 *frac, int32 amt)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 21:44:47 +00:00
|
|
|
/* Is it safe to add inflation ? */
|
2007-06-19 00:05:26 +00:00
|
|
|
if ((INT64_MAX / amt) < (*value + 1)) {
|
|
|
|
*value = INT64_MAX / amt;
|
2007-06-18 21:44:47 +00:00
|
|
|
*frac = 0;
|
|
|
|
} else {
|
|
|
|
int64 tmp = (int64)*value * amt + *frac;
|
|
|
|
*frac = GB(tmp, 0, 16);
|
|
|
|
*value += tmp >> 16;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-07-26 14:58:08 +00:00
|
|
|
static void AddInflation(bool check_year = true)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-07-13 12:08:37 +00:00
|
|
|
/* The cargo payment inflation differs from the normal inflation, so the
|
|
|
|
* relative amount of money you make with a transport decreases slowly over
|
|
|
|
* the 170 years. After a few hundred years we reach a level in which the
|
|
|
|
* games will become unplayable as the maximum income will be less than
|
|
|
|
* the minimum running cost.
|
|
|
|
*
|
|
|
|
* Furthermore there are a lot of inflation related overflows all over the
|
|
|
|
* place. Solving them is hardly possible because inflation will always
|
|
|
|
* reach the overflow threshold some day. So we'll just perform the
|
|
|
|
* inflation mechanism during the first 170 years (the amount of years that
|
|
|
|
* one had in the original TTD) and stop doing the inflation after that
|
|
|
|
* because it only causes problems that can't be solved nicely and the
|
|
|
|
* inflation doesn't add anything after that either; it even makes playing
|
|
|
|
* it impossible due to the diverging cost and income rates.
|
|
|
|
*/
|
2008-07-26 14:58:08 +00:00
|
|
|
if (check_year && (_cur_year - _settings_game.game_creation.starting_year) >= (ORIGINAL_MAX_YEAR - ORIGINAL_BASE_YEAR)) return;
|
2007-07-13 12:08:37 +00:00
|
|
|
|
2007-02-23 08:03:30 +00:00
|
|
|
/* Approximation for (100 + infl_amount)% ** (1 / 12) - 100%
|
|
|
|
* scaled by 65536
|
|
|
|
* 12 -> months per year
|
|
|
|
* This is only a good approxiamtion for small values
|
|
|
|
*/
|
2008-06-02 06:42:27 +00:00
|
|
|
int32 inf = _economy.infl_amount * 54;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 08:03:30 +00:00
|
|
|
for (uint i = 0; i != NUM_PRICES; i++) {
|
2007-06-18 21:44:47 +00:00
|
|
|
AddSingleInflation((Money*)&_price + i, _price_frac + i, inf);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-13 12:08:37 +00:00
|
|
|
AddSingleInflation(&_economy.max_loan_unround, &_economy.max_loan_unround_fract, inf);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-07-13 12:08:37 +00:00
|
|
|
if (_economy.max_loan + 50000 <= _economy.max_loan_unround) _economy.max_loan += 50000;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
inf = _economy.infl_amount_pr * 54;
|
2007-03-21 13:19:01 +00:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2004-09-10 19:02:27 +00:00
|
|
|
AddSingleInflation(
|
2007-06-18 21:44:47 +00:00
|
|
|
(Money*)_cargo_payment_rates + i,
|
2004-08-09 17:04:08 +00:00
|
|
|
_cargo_payment_rates_frac + i,
|
|
|
|
inf
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindowClasses(WC_BUILD_VEHICLE);
|
2005-01-22 23:41:23 +00:00
|
|
|
InvalidateWindowClasses(WC_REPLACE_VEHICLE);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindowClasses(WC_VEHICLE_DETAILS);
|
|
|
|
InvalidateWindow(WC_PAYMENT_RATES, 0);
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void CompaniesPayInterest()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
const Company *c;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
_current_company = c->index;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-04 14:32:30 +00:00
|
|
|
/* Over a year the paid interest should be "loan * interest percentage",
|
|
|
|
* but... as that number is likely not dividable by 12 (pay each month),
|
|
|
|
* one needs to account for that in the monthly fee calculations.
|
|
|
|
* To easily calculate what one should pay "this" month, you calculate
|
|
|
|
* what (total) should have been paid up to this month and you substract
|
|
|
|
* whatever has been paid in the previous months. This will mean one month
|
|
|
|
* it'll be a bit more and the other it'll be a bit less than the average
|
|
|
|
* monthly fee, but on average it will be exact. */
|
|
|
|
Money yearly_fee = c->current_loan * _economy.interest_rate / 100;
|
|
|
|
Money up_to_previous_month = yearly_fee * _cur_month / 12;
|
|
|
|
Money up_to_this_month = yearly_fee * (_cur_month + 1) / 12;
|
|
|
|
|
|
|
|
SubtractMoneyFromCompany(CommandCost(EXPENSES_LOAN_INT, up_to_this_month - up_to_previous_month));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, _price.station_value >> 2));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void HandleEconomyFluctuations()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.difficulty.economy == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (--_economy.fluct == 0) {
|
2005-11-15 08:49:46 +00:00
|
|
|
_economy.fluct = -(int)GB(Random(), 0, 2);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddNewsItem(STR_NEWS_BEGIN_OF_RECESSION, NS_ECONOMY);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (_economy.fluct == -12) {
|
2005-11-15 08:49:46 +00:00
|
|
|
_economy.fluct = GB(Random(), 0, 8) + 312;
|
2009-05-24 16:52:42 +00:00
|
|
|
AddNewsItem(STR_NEWS_END_OF_RECESSION, NS_ECONOMY);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static byte _price_category[NUM_PRICES] = {
|
|
|
|
0, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 1, 1, 1, 1, 1, 1,
|
|
|
|
2,
|
|
|
|
};
|
|
|
|
|
2007-06-18 21:44:47 +00:00
|
|
|
static const Money _price_base[NUM_PRICES] = {
|
2007-02-23 18:55:07 +00:00
|
|
|
100, ///< station_value
|
|
|
|
100, ///< build_rail
|
|
|
|
95, ///< build_road
|
|
|
|
65, ///< build_signals
|
|
|
|
275, ///< build_bridge
|
|
|
|
600, ///< build_train_depot
|
|
|
|
500, ///< build_road_depot
|
|
|
|
700, ///< build_ship_depot
|
|
|
|
450, ///< build_tunnel
|
|
|
|
200, ///< train_station_track
|
|
|
|
180, ///< train_station_length
|
|
|
|
600, ///< build_airport
|
|
|
|
200, ///< build_bus_station
|
|
|
|
200, ///< build_truck_station
|
|
|
|
350, ///< build_dock
|
|
|
|
400000, ///< build_railvehicle
|
|
|
|
2000, ///< build_railwagon
|
|
|
|
700000, ///< aircraft_base
|
|
|
|
14000, ///< roadveh_base
|
|
|
|
65000, ///< ship_base
|
|
|
|
20, ///< build_trees
|
|
|
|
250, ///< terraform
|
2007-11-27 16:02:13 +00:00
|
|
|
20, ///< clear_grass
|
|
|
|
40, ///< clear_roughland
|
|
|
|
200, ///< clear_rocks
|
|
|
|
500, ///< clear_fields
|
2007-02-23 18:55:07 +00:00
|
|
|
20, ///< remove_trees
|
|
|
|
-70, ///< remove_rail
|
|
|
|
10, ///< remove_signals
|
|
|
|
50, ///< clear_bridge
|
|
|
|
80, ///< remove_train_depot
|
|
|
|
80, ///< remove_road_depot
|
|
|
|
90, ///< remove_ship_depot
|
|
|
|
30, ///< clear_tunnel
|
|
|
|
10000, ///< clear_water
|
|
|
|
50, ///< remove_rail_station
|
|
|
|
30, ///< remove_airport
|
|
|
|
50, ///< remove_bus_station
|
|
|
|
50, ///< remove_truck_station
|
|
|
|
55, ///< remove_dock
|
|
|
|
1600, ///< remove_house
|
|
|
|
40, ///< remove_road
|
2008-02-21 14:30:36 +00:00
|
|
|
5600, ///< running_rail[0] steam
|
|
|
|
5200, ///< running_rail[1] diesel
|
|
|
|
4800, ///< running_rail[2] electric
|
2007-02-23 18:55:07 +00:00
|
|
|
9600, ///< aircraft_running
|
|
|
|
1600, ///< roadveh_running
|
|
|
|
5600, ///< ship_running
|
|
|
|
1000000, ///< build_industry
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2005-10-12 07:27:56 +00:00
|
|
|
static byte price_base_multiplier[NUM_PRICES];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset changes to the price base multipliers.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void ResetPriceBaseMultipliers()
|
2005-10-12 07:27:56 +00:00
|
|
|
{
|
2005-10-12 09:54:29 +00:00
|
|
|
uint i;
|
2005-10-12 07:27:56 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* 8 means no multiplier. */
|
2005-10-12 07:27:56 +00:00
|
|
|
for (i = 0; i < NUM_PRICES; i++)
|
|
|
|
price_base_multiplier[i] = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change a price base by the given factor.
|
|
|
|
* The price base is altered by factors of two, with an offset of 8.
|
|
|
|
* NewBaseCost = OldBaseCost * 2^(n-8)
|
|
|
|
* @param price Index of price base to change.
|
|
|
|
* @param factor Amount to change by.
|
|
|
|
*/
|
2005-10-12 09:54:29 +00:00
|
|
|
void SetPriceBaseMultiplier(uint price, byte factor)
|
2005-10-12 07:27:56 +00:00
|
|
|
{
|
2005-10-12 09:54:29 +00:00
|
|
|
assert(price < NUM_PRICES);
|
|
|
|
price_base_multiplier[price] = factor;
|
2005-10-12 07:27:56 +00:00
|
|
|
}
|
|
|
|
|
2008-09-15 17:18:22 +00:00
|
|
|
/**
|
|
|
|
* Initialize the variables that will maintain the daily industry change system.
|
|
|
|
* @param init_counter specifies if the counter is required to be initialized
|
|
|
|
*/
|
2009-01-04 15:32:25 +00:00
|
|
|
void StartupIndustryDailyChanges(bool init_counter)
|
2008-09-15 17:18:22 +00:00
|
|
|
{
|
|
|
|
uint map_size = MapLogX() + MapLogY();
|
|
|
|
/* After getting map size, it needs to be scaled appropriately and divided by 31,
|
|
|
|
* which stands for the days in a month.
|
|
|
|
* Using just 31 will make it so that a monthly reset (based on the real number of days of that month)
|
|
|
|
* would not be needed.
|
|
|
|
* Since it is based on "fractionnal parts", the leftover days will not make much of a difference
|
|
|
|
* on the overall total number of changes performed */
|
|
|
|
_economy.industry_daily_increment = (1 << map_size) / 31;
|
|
|
|
|
|
|
|
if (init_counter) {
|
|
|
|
/* A new game or a savegame from an older version will require the counter to be initialized */
|
|
|
|
_economy.industry_daily_change_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void StartupEconomy()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-06-18 21:44:47 +00:00
|
|
|
assert(sizeof(_price) == NUM_PRICES * sizeof(Money));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != NUM_PRICES; i++) {
|
2007-06-18 21:44:47 +00:00
|
|
|
Money price = _price_base[i];
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_price_category[i] != 0) {
|
2008-05-29 15:13:28 +00:00
|
|
|
uint mod = _price_category[i] == 1 ? _settings_game.difficulty.vehicle_costs : _settings_game.difficulty.construction_cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (mod < 1) {
|
|
|
|
price = price * 3 >> 2;
|
|
|
|
} else if (mod > 1) {
|
|
|
|
price = price * 9 >> 3;
|
|
|
|
}
|
|
|
|
}
|
2005-10-12 07:27:56 +00:00
|
|
|
if (price_base_multiplier[i] > 8) {
|
|
|
|
price <<= price_base_multiplier[i] - 8;
|
|
|
|
} else {
|
|
|
|
price >>= 8 - price_base_multiplier[i];
|
|
|
|
}
|
2007-06-18 21:44:47 +00:00
|
|
|
((Money*)&_price)[i] = price;
|
2004-08-09 17:04:08 +00:00
|
|
|
_price_frac[i] = 0;
|
|
|
|
}
|
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
_economy.interest_rate = _settings_game.difficulty.initial_interest;
|
|
|
|
_economy.infl_amount = _settings_game.difficulty.initial_interest;
|
|
|
|
_economy.infl_amount_pr = max(0, _settings_game.difficulty.initial_interest - 1);
|
|
|
|
_economy.max_loan_unround = _economy.max_loan = _settings_game.difficulty.max_loan;
|
2005-07-21 06:31:02 +00:00
|
|
|
_economy.fluct = GB(Random(), 0, 8) + 168;
|
2008-09-15 17:18:22 +00:00
|
|
|
|
|
|
|
StartupIndustryDailyChanges(true); // As we are starting a new game, initialize the counter too
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-07-26 14:58:08 +00:00
|
|
|
void ResetEconomy()
|
|
|
|
{
|
|
|
|
/* Test if resetting the economy is needed. */
|
|
|
|
bool needed = false;
|
|
|
|
|
2009-07-16 20:40:06 +00:00
|
|
|
const CargoSpec *cs;
|
|
|
|
FOR_ALL_CARGOSPECS(cs) {
|
|
|
|
if (_cargo_payment_rates[cs->Index()] == 0) {
|
2008-07-26 14:58:08 +00:00
|
|
|
needed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!needed) return;
|
|
|
|
|
|
|
|
/* Remember old unrounded maximum loan value. NewGRF has the ability
|
|
|
|
* to change all the other inflation affected base costs. */
|
|
|
|
Money old_value = _economy.max_loan_unround;
|
|
|
|
|
|
|
|
/* Reset the economy */
|
|
|
|
StartupEconomy();
|
|
|
|
InitializeLandscapeVariables(false);
|
|
|
|
|
|
|
|
/* Reapply inflation, ignoring the year */
|
|
|
|
while (old_value > _economy.max_loan_unround) {
|
|
|
|
AddInflation(false);
|
|
|
|
}
|
|
|
|
}
|
2008-02-21 19:09:10 +00:00
|
|
|
|
|
|
|
Money GetPriceByIndex(uint8 index)
|
|
|
|
{
|
|
|
|
if (index > NUM_PRICES) return 0;
|
|
|
|
|
|
|
|
return ((Money*)&_price)[index];
|
|
|
|
}
|
|
|
|
|
2007-06-18 22:09:54 +00:00
|
|
|
Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-16 19:00:13 +00:00
|
|
|
const CargoSpec *cs = CargoSpec::Get(cargo_type);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-12 19:33:05 +00:00
|
|
|
/* Use callback to calculate cargo profit, if available */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) {
|
2007-04-12 19:33:05 +00:00
|
|
|
uint32 var18 = min(dist, 0xFFFF) | (min(num_pieces, 0xFF) << 16) | (transit_days << 24);
|
|
|
|
uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs);
|
|
|
|
if (callback != CALLBACK_FAILED) {
|
|
|
|
int result = GB(callback, 0, 14);
|
|
|
|
|
|
|
|
/* Simulate a 15 bit signed value */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(callback, 14)) result = 0x4000 - result;
|
2007-04-12 19:33:05 +00:00
|
|
|
|
|
|
|
/* "The result should be a signed multiplier that gets multiplied
|
|
|
|
* by the amount of cargo moved and the price factor, then gets
|
|
|
|
* divided by 8192." */
|
|
|
|
return result * num_pieces * _cargo_payment_rates[cargo_type] / 8192;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-25 19:26:33 +00:00
|
|
|
/* zero the distance (thus income) if it's the bank and very short transport. */
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.game_creation.landscape == LT_TEMPERATE && cs->label == 'VALU' && dist < 10) return 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-07-25 19:26:33 +00:00
|
|
|
static const int MIN_TIME_FACTOR = 31;
|
|
|
|
static const int MAX_TIME_FACTOR = 255;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-25 19:26:33 +00:00
|
|
|
const int days1 = cs->transit_days[0];
|
|
|
|
const int days2 = cs->transit_days[1];
|
2008-12-13 16:16:44 +00:00
|
|
|
const int days_over_days1 = max( transit_days - days1, 0);
|
|
|
|
const int days_over_days2 = max(days_over_days1 - days2, 0);
|
2007-07-25 19:26:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The time factor is calculated based on the time it took
|
|
|
|
* (transit_days) compared two cargo-depending values. The
|
|
|
|
* range is divided into three parts:
|
|
|
|
*
|
|
|
|
* - constant for fast transits
|
|
|
|
* - linear decreasing with time with a slope of -1 for medium transports
|
|
|
|
* - linear decreasing with time with a slope of -2 for slow transports
|
|
|
|
*
|
|
|
|
*/
|
2008-12-13 16:16:44 +00:00
|
|
|
const int time_factor = max(MAX_TIME_FACTOR - days_over_days1 - days_over_days2, MIN_TIME_FACTOR);
|
2007-07-25 19:26:33 +00:00
|
|
|
|
2007-11-19 20:18:27 +00:00
|
|
|
return BigMulS(dist * time_factor * num_pieces, _cargo_payment_rates[cargo_type], 21);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 15:42:03 +00:00
|
|
|
/**
|
|
|
|
* @note THIS STRUCTURE WILL BE REMOVED SOON!
|
|
|
|
*/
|
2008-10-25 14:24:50 +00:00
|
|
|
struct FindIndustryToDeliverData {
|
|
|
|
const Rect *rect; ///< Station acceptance rectangle
|
|
|
|
CargoID cargo_type; ///< Cargo type that was delivered
|
|
|
|
|
|
|
|
Industry *ind; ///< Returns found industry
|
|
|
|
const IndustrySpec *indspec; ///< Spec of ind
|
|
|
|
uint cargo_index; ///< Index of cargo_type in acceptance list of ind
|
|
|
|
};
|
|
|
|
|
2009-06-25 15:42:03 +00:00
|
|
|
/**
|
|
|
|
* @note THIS FUNCTION WILL BE REMOVED SOON!
|
|
|
|
*/
|
2008-10-25 14:24:50 +00:00
|
|
|
static bool FindIndustryToDeliver(TileIndex ind_tile, void *user_data)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-10-25 14:24:50 +00:00
|
|
|
FindIndustryToDeliverData *callback_data = (FindIndustryToDeliverData *)user_data;
|
|
|
|
const Rect *rect = callback_data->rect;
|
|
|
|
CargoID cargo_type = callback_data->cargo_type;
|
2007-05-17 20:19:55 +00:00
|
|
|
|
2008-10-25 14:24:50 +00:00
|
|
|
/* Only process industry tiles */
|
|
|
|
if (!IsTileType(ind_tile, MP_INDUSTRY)) return false;
|
2007-05-17 20:19:55 +00:00
|
|
|
|
2008-10-25 14:24:50 +00:00
|
|
|
/* Only process tiles in the station acceptance rectangle */
|
|
|
|
int x = TileX(ind_tile);
|
|
|
|
int y = TileY(ind_tile);
|
|
|
|
if (x < rect->left || x > rect->right || y < rect->top || y > rect->bottom) return false;
|
2007-05-17 20:19:55 +00:00
|
|
|
|
2008-10-25 14:24:50 +00:00
|
|
|
Industry *ind = GetIndustryByTile(ind_tile);
|
|
|
|
const IndustrySpec *indspec = GetIndustrySpec(ind->type);
|
2007-07-08 17:40:04 +00:00
|
|
|
|
2008-10-25 14:24:50 +00:00
|
|
|
uint cargo_index;
|
|
|
|
for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
|
|
|
|
if (cargo_type == ind->accepts_cargo[cargo_index]) break;
|
|
|
|
}
|
|
|
|
/* Check if matching cargo has been found */
|
|
|
|
if (cargo_index >= lengthof(ind->accepts_cargo)) return false;
|
2007-05-17 20:19:55 +00:00
|
|
|
|
2008-10-25 14:24:50 +00:00
|
|
|
/* Check if industry temporarly refuses acceptance */
|
|
|
|
if (HasBit(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) {
|
|
|
|
uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->xy);
|
|
|
|
if (res == 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-25 14:24:50 +00:00
|
|
|
/* Found industry accepting the cargo */
|
|
|
|
callback_data->ind = ind;
|
|
|
|
callback_data->indspec = indspec;
|
|
|
|
callback_data->cargo_index = cargo_index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer goods from station to industry.
|
|
|
|
* All cargo is delivered to the nearest (Manhattan) industry to the station sign, which is inside the acceptance rectangle and actually accepts the cargo.
|
|
|
|
* @param st The station that accepted the cargo
|
|
|
|
* @param cargo_type Type of cargo delivered
|
|
|
|
* @param nun_pieces Amount of cargo delivered
|
2009-06-25 15:42:03 +00:00
|
|
|
* @note THIS FUNCTION WILL BE REMOVED SOON!
|
2008-10-25 14:24:50 +00:00
|
|
|
*/
|
2009-06-25 15:42:03 +00:00
|
|
|
static Industry *DeliverGoodsToIndustryCheckOldStyle(const Station *st, CargoID cargo_type, int num_pieces)
|
2008-10-25 14:24:50 +00:00
|
|
|
{
|
2009-06-25 15:42:03 +00:00
|
|
|
if (st->rect.IsEmpty()) return NULL;
|
2008-10-25 14:24:50 +00:00
|
|
|
|
|
|
|
/* Compute acceptance rectangle */
|
2008-10-25 22:00:51 +00:00
|
|
|
int catchment_radius = st->GetCatchmentRadius();
|
2008-10-25 14:24:50 +00:00
|
|
|
Rect rect = {
|
2008-10-25 22:00:51 +00:00
|
|
|
max<int>(st->rect.left - catchment_radius, 0),
|
|
|
|
max<int>(st->rect.top - catchment_radius, 0),
|
|
|
|
min<int>(st->rect.right + catchment_radius, MapMaxX()),
|
|
|
|
min<int>(st->rect.bottom + catchment_radius, MapMaxY())
|
2008-10-25 14:24:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Compute maximum extent of acceptance rectangle wrt. station sign */
|
|
|
|
TileIndex start_tile = st->xy;
|
|
|
|
uint max_radius = max(
|
|
|
|
max(DistanceManhattan(start_tile, TileXY(rect.left , rect.top)), DistanceManhattan(start_tile, TileXY(rect.left , rect.bottom))),
|
|
|
|
max(DistanceManhattan(start_tile, TileXY(rect.right, rect.top)), DistanceManhattan(start_tile, TileXY(rect.right, rect.bottom)))
|
|
|
|
);
|
|
|
|
|
|
|
|
FindIndustryToDeliverData callback_data;
|
|
|
|
callback_data.rect = ▭
|
|
|
|
callback_data.cargo_type = cargo_type;
|
|
|
|
callback_data.ind = NULL;
|
|
|
|
callback_data.indspec = NULL;
|
|
|
|
callback_data.cargo_index = 0;
|
|
|
|
|
|
|
|
/* Find the nearest industrytile to the station sign inside the catchment area, whose industry accepts the cargo.
|
|
|
|
* This fails in three cases:
|
|
|
|
* 1) The station accepts the cargo because there are enough houses around it accepting the cargo.
|
|
|
|
* 2) The industries in the catchment area temporarily reject the cargo, and the daily station loop has not yet updated station acceptance.
|
|
|
|
* 3) The results of callbacks CBID_INDUSTRY_REFUSE_CARGO and CBID_INDTILE_CARGO_ACCEPTANCE are inconsistent. (documented behaviour)
|
|
|
|
*/
|
2009-06-25 15:42:03 +00:00
|
|
|
if (CircularTileSearch(&start_tile, 2 * max_radius + 1, FindIndustryToDeliver, &callback_data)) return callback_data.ind;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-28 14:36:25 +00:00
|
|
|
/** The industries we've currently brought cargo to. */
|
|
|
|
static SmallIndustryList _cargo_delivery_destinations;
|
2009-06-25 15:42:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer goods from station to industry.
|
|
|
|
* All cargo is delivered to the nearest (Manhattan) industry to the station sign, which is inside the acceptance rectangle and actually accepts the cargo.
|
|
|
|
* @param st The station that accepted the cargo
|
|
|
|
* @param cargo_type Type of cargo delivered
|
|
|
|
* @param nun_pieces Amount of cargo delivered
|
|
|
|
*/
|
2009-06-28 14:36:25 +00:00
|
|
|
static void DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, int num_pieces)
|
2009-06-25 15:42:03 +00:00
|
|
|
{
|
|
|
|
/* Find the nearest industrytile to the station sign inside the catchment area, whose industry accepts the cargo.
|
|
|
|
* This fails in three cases:
|
|
|
|
* 1) The station accepts the cargo because there are enough houses around it accepting the cargo.
|
|
|
|
* 2) The industries in the catchment area temporarily reject the cargo, and the daily station loop has not yet updated station acceptance.
|
|
|
|
* 3) The results of callbacks CBID_INDUSTRY_REFUSE_CARGO and CBID_INDTILE_CARGO_ACCEPTANCE are inconsistent. (documented behaviour)
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (uint i = 0; i < st->industries_near.Length(); i++) {
|
|
|
|
Industry *ind = st->industries_near[i];
|
|
|
|
const IndustrySpec *indspec = GetIndustrySpec(ind->type);
|
|
|
|
|
|
|
|
uint cargo_index;
|
|
|
|
for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
|
|
|
|
if (cargo_type == ind->accepts_cargo[cargo_index]) break;
|
|
|
|
}
|
|
|
|
/* Check if matching cargo has been found */
|
|
|
|
if (cargo_index >= lengthof(ind->accepts_cargo)) continue;
|
|
|
|
|
|
|
|
/* Check if industry temporarily refuses acceptance */
|
|
|
|
if (HasBit(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) {
|
|
|
|
uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->xy);
|
|
|
|
if (res == 0) continue;
|
|
|
|
}
|
2007-07-07 08:53:19 +00:00
|
|
|
|
2009-06-28 14:36:25 +00:00
|
|
|
/* Insert the industry into _cargo_delivery_destinations, if not yet contained */
|
|
|
|
_cargo_delivery_destinations.Include(ind);
|
2007-07-04 18:27:21 +00:00
|
|
|
|
2009-06-25 15:42:03 +00:00
|
|
|
assert(DeliverGoodsToIndustryCheckOldStyle(st, cargo_type, num_pieces) == ind); // safety check, will be removed soon
|
|
|
|
|
|
|
|
ind->incoming_cargo_waiting[cargo_index] = min(num_pieces + ind->incoming_cargo_waiting[cargo_index], 0xFFFF);
|
|
|
|
|
|
|
|
return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-06-25 15:42:03 +00:00
|
|
|
|
|
|
|
assert(DeliverGoodsToIndustryCheckOldStyle(st, cargo_type, num_pieces) == NULL); // safety check, will be removed soon
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-20 17:21:22 +00:00
|
|
|
/**
|
|
|
|
* Delivers goods to industries/towns and calculates the payment
|
|
|
|
* @param num_pieces amount of cargo delivered
|
|
|
|
* @param source Originstation of the cargo
|
|
|
|
* @param dest Station the cargo has been unloaded
|
|
|
|
* @param source_tile The origin of the cargo for distance calculation
|
|
|
|
* @param days_in_transit Travel time
|
2009-06-28 14:33:10 +00:00
|
|
|
* @param company The company delivering the cargo
|
2008-12-20 17:21:22 +00:00
|
|
|
* The cargo is just added to the stockpile of the industry. It is due to the caller to trigger the industry's production machinery
|
|
|
|
*/
|
2009-06-28 14:36:25 +00:00
|
|
|
static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID source, StationID dest, TileIndex source_tile, byte days_in_transit, Company *company)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-23 19:43:09 +00:00
|
|
|
bool subsidised = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
assert(num_pieces > 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Update company statistics */
|
2009-06-28 14:33:10 +00:00
|
|
|
company->cur_economy.delivered_cargo += num_pieces;
|
|
|
|
SetBit(company->cargo_types, cargo_type);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-23 19:43:09 +00:00
|
|
|
const Station *s_to = Station::Get(dest);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-23 19:43:09 +00:00
|
|
|
if (source != INVALID_STATION) {
|
|
|
|
const Station *s_from = Station::Get(source);
|
|
|
|
|
|
|
|
/* Check if a subsidy applies. */
|
2009-07-01 09:45:30 +00:00
|
|
|
subsidised = CheckSubsidised(s_from, s_to, cargo_type, company->index);
|
2009-05-23 19:43:09 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Increase town's counter for some special goods types */
|
2009-07-16 19:00:13 +00:00
|
|
|
const CargoSpec *cs = CargoSpec::Get(cargo_type);
|
2007-03-16 17:40:31 +00:00
|
|
|
if (cs->town_effect == TE_FOOD) s_to->town->new_act_food += num_pieces;
|
|
|
|
if (cs->town_effect == TE_WATER) s_to->town->new_act_water += num_pieces;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Give the goods to the industry. */
|
2009-06-28 14:36:25 +00:00
|
|
|
DeliverGoodsToIndustry(s_to, cargo_type, num_pieces);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Determine profit */
|
2009-05-23 19:43:09 +00:00
|
|
|
Money profit = GetTransportedGoodsIncome(num_pieces, DistanceManhattan(source_tile, s_to->xy), days_in_transit, cargo_type);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Modify profit if a subsidy is in effect */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (subsidised) {
|
2008-05-29 15:13:28 +00:00
|
|
|
switch (_settings_game.difficulty.subsidy_multiplier) {
|
2006-04-25 06:56:22 +00:00
|
|
|
case 0: profit += profit >> 1; break;
|
|
|
|
case 1: profit *= 2; break;
|
|
|
|
case 2: profit *= 3; break;
|
|
|
|
default: profit *= 4; break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return profit;
|
|
|
|
}
|
|
|
|
|
2008-12-20 17:21:22 +00:00
|
|
|
/**
|
|
|
|
* Inform the industry about just delivered cargo
|
|
|
|
* DeliverGoodsToIndustry() silently incremented incoming_cargo_waiting, now it is time to do something with the new cargo.
|
|
|
|
* @param i The industry to process
|
|
|
|
*/
|
|
|
|
static void TriggerIndustryProduction(Industry *i)
|
|
|
|
{
|
|
|
|
const IndustrySpec *indspec = GetIndustrySpec(i->type);
|
|
|
|
uint16 callback = indspec->callback_flags;
|
|
|
|
|
|
|
|
i->was_cargo_delivered = true;
|
|
|
|
i->last_cargo_accepted_at = _date;
|
|
|
|
|
|
|
|
if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
|
|
|
|
if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) {
|
|
|
|
IndustryProductionCallback(i, 0);
|
|
|
|
} else {
|
|
|
|
InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (uint cargo_index = 0; cargo_index < lengthof(i->incoming_cargo_waiting); cargo_index++) {
|
|
|
|
uint cargo_waiting = i->incoming_cargo_waiting[cargo_index];
|
|
|
|
if (cargo_waiting == 0) continue;
|
|
|
|
|
|
|
|
i->produced_cargo_waiting[0] = min(i->produced_cargo_waiting[0] + (cargo_waiting * indspec->input_cargo_multiplier[cargo_index][0] / 256), 0xFFFF);
|
|
|
|
i->produced_cargo_waiting[1] = min(i->produced_cargo_waiting[1] + (cargo_waiting * indspec->input_cargo_multiplier[cargo_index][1] / 256), 0xFFFF);
|
|
|
|
|
|
|
|
i->incoming_cargo_waiting[cargo_index] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerIndustry(i, INDUSTRY_TRIGGER_RECEIVED_CARGO);
|
|
|
|
StartStopIndustryTileAnimation(i, IAT_INDUSTRY_RECEIVED_CARGO);
|
|
|
|
}
|
|
|
|
|
2009-06-28 15:12:59 +00:00
|
|
|
/**
|
|
|
|
* Makes us a new cargo payment helper.
|
|
|
|
* @param front The front of the train
|
|
|
|
* @param destinations List to add the destinations of 'our' cargo to
|
|
|
|
*/
|
|
|
|
CargoPayment::CargoPayment(Vehicle *front) :
|
|
|
|
front(front),
|
|
|
|
current_station(front->last_station_visited)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CargoPayment::~CargoPayment()
|
|
|
|
{
|
2009-06-29 19:55:36 +00:00
|
|
|
if (this->CleaningPool()) return;
|
|
|
|
|
|
|
|
this->front->cargo_payment = NULL;
|
|
|
|
|
2009-06-28 15:12:59 +00:00
|
|
|
if (this->visual_profit == 0) return;
|
|
|
|
|
|
|
|
CompanyID old_company = _current_company;
|
|
|
|
_current_company = this->front->owner;
|
|
|
|
|
|
|
|
SubtractMoneyFromCompany(CommandCost(this->front->GetExpenseType(true), -this->route_profit));
|
2009-06-29 19:17:23 +00:00
|
|
|
this->front->profit_this_year += this->visual_profit << 8;
|
2009-06-28 15:12:59 +00:00
|
|
|
|
|
|
|
if (this->route_profit != 0) {
|
|
|
|
if (IsLocalCompany() && !PlayVehicleSound(this->front, VSE_LOAD_UNLOAD)) {
|
|
|
|
SndPlayVehicleFx(SND_14_CASHTILL, this->front);
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowCostOrIncomeAnimation(this->front->x_pos, this->front->y_pos, this->front->z_pos, -this->visual_profit);
|
|
|
|
} else {
|
|
|
|
ShowFeederIncomeAnimation(this->front->x_pos, this->front->y_pos, this->front->z_pos, this->visual_profit);
|
|
|
|
}
|
|
|
|
|
|
|
|
_current_company = old_company;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle payment for final delivery of the given cargo packet.
|
|
|
|
* @param cp The cargo packet to pay for.
|
|
|
|
* @param count The number of packets to pay for.
|
|
|
|
*/
|
|
|
|
void CargoPayment::PayFinalDelivery(CargoPacket *cp, uint count)
|
|
|
|
{
|
|
|
|
if (this->owner == NULL) {
|
|
|
|
this->owner = Company::Get(this->front->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle end of route payment */
|
|
|
|
Money profit = DeliverGoods(count, this->ct, cp->source, this->current_station, cp->source_xy, cp->days_in_transit, this->owner);
|
|
|
|
this->route_profit += profit;
|
|
|
|
|
|
|
|
/* The vehicle's profit is whatever route profit there is minus feeder shares. */
|
|
|
|
this->visual_profit += profit - cp->feeder_share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle payment for transfer of the given cargo packet.
|
|
|
|
* @param cp The cargo packet to pay for.
|
|
|
|
* @param count The number of packets to pay for.
|
|
|
|
*/
|
|
|
|
void CargoPayment::PayTransfer(CargoPacket *cp, uint count)
|
|
|
|
{
|
|
|
|
Money profit = GetTransportedGoodsIncome(
|
|
|
|
count,
|
|
|
|
/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
|
|
|
|
DistanceManhattan(cp->loaded_at_xy, Station::Get(this->current_station)->xy),
|
|
|
|
cp->days_in_transit,
|
|
|
|
this->ct);
|
|
|
|
|
|
|
|
this->visual_profit += profit; // accumulate transfer profits for whole vehicle
|
|
|
|
cp->feeder_share += profit; // account for the (virtual) profit already made for the cargo packet
|
|
|
|
}
|
|
|
|
|
2007-05-01 16:45:03 +00:00
|
|
|
/**
|
2009-06-29 19:55:36 +00:00
|
|
|
* Prepare the vehicle to be unloaded.
|
2007-05-01 16:45:03 +00:00
|
|
|
* @param front_v the vehicle to be unloaded
|
|
|
|
*/
|
2009-06-29 19:55:36 +00:00
|
|
|
void PrepareUnload(Vehicle *front_v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-05-02 18:29:11 +00:00
|
|
|
/* At this moment loading cannot be finished */
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(front_v->vehicle_flags, VF_LOADING_FINISHED);
|
2007-05-02 18:29:11 +00:00
|
|
|
|
|
|
|
/* Start unloading in at the first possible moment */
|
|
|
|
front_v->load_unload_time_rem = 1;
|
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
if ((front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
|
|
|
|
for (Vehicle *v = front_v; v != NULL; v = v->Next()) {
|
|
|
|
if (v->cargo_cap > 0 && !v->cargo.Empty()) {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
|
2007-05-01 16:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
assert(front_v->cargo_payment == NULL);
|
|
|
|
front_v->cargo_payment = new CargoPayment(front_v);
|
2007-05-01 16:45:03 +00:00
|
|
|
}
|
2007-03-02 18:49:11 +00:00
|
|
|
|
2007-05-06 14:59:01 +00:00
|
|
|
/**
|
|
|
|
* Loads/unload the vehicle if possible.
|
|
|
|
* @param v the vehicle to be (un)loaded
|
2007-05-14 20:12:32 +00:00
|
|
|
* @param cargo_left the amount of each cargo type that is
|
|
|
|
* virtually left on the platform to be
|
|
|
|
* picked up by another vehicle when all
|
|
|
|
* previous vehicles have loaded.
|
2007-05-06 14:59:01 +00:00
|
|
|
*/
|
2007-05-14 20:12:32 +00:00
|
|
|
static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
|
2007-05-01 16:45:03 +00:00
|
|
|
{
|
2008-04-05 23:46:01 +00:00
|
|
|
assert(v->current_order.IsType(OT_LOADING));
|
2009-07-21 17:14:05 +00:00
|
|
|
assert(v->load_unload_time_rem != 0);
|
2007-05-14 16:07:05 +00:00
|
|
|
|
|
|
|
/* We have not waited enough time till the next round of loading/unloading */
|
2007-05-14 20:12:32 +00:00
|
|
|
if (--v->load_unload_time_rem != 0) {
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.improved_load && (v->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
|
2007-05-14 20:12:32 +00:00
|
|
|
/* 'Reserve' this cargo for this vehicle, because we were first. */
|
2007-08-30 13:03:56 +00:00
|
|
|
for (; v != NULL; v = v->Next()) {
|
2009-02-21 15:56:02 +00:00
|
|
|
int cap_left = v->cargo_cap - v->cargo.Count();
|
|
|
|
if (cap_left > 0) cargo_left[v->cargo_type] -= cap_left;
|
2007-05-14 20:12:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2007-05-14 16:07:05 +00:00
|
|
|
|
2008-01-09 17:09:53 +00:00
|
|
|
StationID last_visited = v->last_station_visited;
|
2009-05-16 23:34:14 +00:00
|
|
|
Station *st = Station::Get(last_visited);
|
2008-01-09 17:09:53 +00:00
|
|
|
|
|
|
|
if (v->type == VEH_TRAIN && (!IsTileType(v->tile, MP_STATION) || GetStationIndex(v->tile) != st->index)) {
|
2007-06-10 20:27:28 +00:00
|
|
|
/* The train reversed in the station. Take the "easy" way
|
|
|
|
* out and let the train just leave as it always did. */
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(v->vehicle_flags, VF_LOADING_FINISHED);
|
2007-06-10 20:27:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-13 21:24:58 +00:00
|
|
|
int unloading_time = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
Vehicle *u = v;
|
|
|
|
int result = 0;
|
2007-05-02 18:29:11 +00:00
|
|
|
|
2008-01-10 13:13:18 +00:00
|
|
|
bool completely_emptied = true;
|
2007-05-13 21:24:58 +00:00
|
|
|
bool anything_unloaded = false;
|
|
|
|
bool anything_loaded = false;
|
|
|
|
uint32 cargo_not_full = 0;
|
|
|
|
uint32 cargo_full = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cur_speed = 0;
|
2006-12-05 17:53:33 +00:00
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
CargoPayment *payment = v->cargo_payment;
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
for (; v != NULL; v = v->Next()) {
|
2007-05-12 09:09:10 +00:00
|
|
|
if (v->cargo_cap == 0) continue;
|
|
|
|
|
|
|
|
byte load_amount = EngInfo(v->engine_type)->load_amount;
|
2008-12-13 18:25:42 +00:00
|
|
|
|
|
|
|
/* The default loadamount for mail is 1/4 of the load amount for passengers */
|
2009-07-13 16:37:27 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT && !Aircraft::From(v)->IsNormalAircraft()) load_amount = (load_amount + 3) / 4;
|
2008-12-13 18:25:42 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.gradual_loading && HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_LOAD_AMOUNT)) {
|
2006-12-02 16:56:32 +00:00
|
|
|
uint16 cb_load_amount = GetVehicleCallback(CBID_VEHICLE_LOAD_AMOUNT, 0, 0, v->engine_type, v);
|
2008-04-21 14:33:33 +00:00
|
|
|
if (cb_load_amount != CALLBACK_FAILED && GB(cb_load_amount, 0, 8) != 0) load_amount = GB(cb_load_amount, 0, 8);
|
2006-12-02 16:56:32 +00:00
|
|
|
}
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2007-05-12 09:09:10 +00:00
|
|
|
GoodsEntry *ge = &st->goods[v->cargo_type];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-10 10:18:03 +00:00
|
|
|
if (HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) && (u->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
|
2007-06-22 11:58:59 +00:00
|
|
|
uint cargo_count = v->cargo.Count();
|
2008-05-29 15:13:28 +00:00
|
|
|
uint amount_unloaded = _settings_game.order.gradual_loading ? min(cargo_count, load_amount) : cargo_count;
|
2009-02-25 00:12:57 +00:00
|
|
|
bool remaining = false; // Are there cargo entities in this vehicle that can still be unloaded here?
|
|
|
|
bool accepted = false; // Is the cargo accepted by the station?
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
payment->SetCargo(v->cargo_type);
|
|
|
|
|
2008-04-07 20:03:46 +00:00
|
|
|
if (HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) && !(u->current_order.GetUnloadType() & OUFB_TRANSFER)) {
|
2007-06-22 11:58:59 +00:00
|
|
|
/* The cargo has reached it's final destination, the packets may now be destroyed */
|
2009-06-29 19:55:36 +00:00
|
|
|
remaining = v->cargo.MoveTo(NULL, amount_unloaded, CargoList::MTA_FINAL_DELIVERY, payment, last_visited);
|
2006-12-05 17:53:33 +00:00
|
|
|
|
|
|
|
result |= 1;
|
2009-02-25 00:12:57 +00:00
|
|
|
accepted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The !accepted || v->cargo.Count == cargo_count clause is there
|
|
|
|
* to make it possible to force unload vehicles at the station where
|
|
|
|
* they were loaded, but to not force unload the vehicle when the
|
|
|
|
* station is still accepting the cargo in the vehicle. It doesn't
|
|
|
|
* accept cargo that was loaded at the same station. */
|
2009-06-01 11:43:36 +00:00
|
|
|
if ((u->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) && (!accepted || v->cargo.Count() == cargo_count)) {
|
2009-06-29 19:55:36 +00:00
|
|
|
remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded, u->current_order.GetUnloadType() & OUFB_TRANSFER ? CargoList::MTA_TRANSFER : CargoList::MTA_UNLOAD, payment);
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(ge->acceptance_pickup, GoodsEntry::PICKUP);
|
2007-03-02 18:49:11 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
result |= 2;
|
2009-02-25 00:12:57 +00:00
|
|
|
} else if (!accepted) {
|
2007-05-12 09:09:10 +00:00
|
|
|
/* The order changed while unloading (unset unload/transfer) or the
|
2009-07-01 09:50:52 +00:00
|
|
|
* station does not accept our goods. */
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_CARGO_UNLOADING);
|
2009-07-01 09:50:52 +00:00
|
|
|
|
|
|
|
/* Say we loaded something, otherwise we'll think we didn't unload
|
|
|
|
* something and we didn't load something, so we must be finished
|
|
|
|
* at this station. Setting the unloaded means that we will get a
|
|
|
|
* retry for loading in the next cycle. */
|
|
|
|
anything_unloaded = true;
|
2007-05-12 09:09:10 +00:00
|
|
|
continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-11-17 08:52:47 +00:00
|
|
|
|
2007-05-12 09:09:10 +00:00
|
|
|
/* Deliver goods to the station */
|
|
|
|
st->time_since_unload = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-05-12 09:09:10 +00:00
|
|
|
unloading_time += amount_unloaded;
|
|
|
|
|
2007-05-13 21:24:58 +00:00
|
|
|
anything_unloaded = true;
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.gradual_loading && remaining) {
|
2008-01-10 13:13:18 +00:00
|
|
|
completely_emptied = false;
|
2007-05-12 09:09:10 +00:00
|
|
|
} else {
|
|
|
|
/* We have finished unloading (cargo count == 0) */
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_CARGO_UNLOADING);
|
2007-05-12 09:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-31 22:33:24 +00:00
|
|
|
|
2008-04-10 17:23:51 +00:00
|
|
|
/* Do not pick up goods when we have no-load set. */
|
|
|
|
if (u->current_order.GetLoadType() & OLFB_NO_LOAD) continue;
|
2006-12-02 16:56:32 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* update stats */
|
2007-05-12 09:09:10 +00:00
|
|
|
int t;
|
2006-02-06 09:18:04 +00:00
|
|
|
switch (u->type) {
|
2009-06-06 16:54:22 +00:00
|
|
|
case VEH_TRAIN: t = Train::From(u)->tcache.cached_max_speed; break;
|
2007-03-08 16:27:54 +00:00
|
|
|
case VEH_ROAD: t = u->max_speed / 2; break;
|
2006-02-06 09:18:04 +00:00
|
|
|
default: t = u->max_speed; break;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* if last speed is 0, we treat that as if no vehicle has ever visited the station. */
|
2006-02-06 09:18:04 +00:00
|
|
|
ge->last_speed = min(t, 255);
|
2007-06-21 14:32:27 +00:00
|
|
|
ge->last_age = _cur_year - u->build_year;
|
2007-08-20 20:29:22 +00:00
|
|
|
ge->days_since_pickup = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* If there's goods waiting at the station, and the vehicle
|
|
|
|
* has capacity for it, load it on the vehicle. */
|
2009-02-21 15:56:02 +00:00
|
|
|
int cap_left = v->cargo_cap - v->cargo.Count();
|
|
|
|
if (!ge->cargo.Empty() && cap_left > 0) {
|
|
|
|
uint cap = cap_left;
|
2007-06-22 11:58:59 +00:00
|
|
|
uint count = ge->cargo.Count();
|
2005-06-15 16:58:15 +00:00
|
|
|
|
2004-11-29 11:59:09 +00:00
|
|
|
/* Skip loading this vehicle if another train/vehicle is already handling
|
|
|
|
* the same cargo type at this station */
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.improved_load && cargo_left[v->cargo_type] <= 0) {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(cargo_not_full, v->cargo_type);
|
2007-05-02 18:29:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-11-29 11:59:09 +00:00
|
|
|
|
2007-05-14 20:12:32 +00:00
|
|
|
if (cap > count) cap = count;
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.gradual_loading) cap = min(cap, load_amount);
|
|
|
|
if (_settings_game.order.improved_load) {
|
2007-05-14 20:12:32 +00:00
|
|
|
/* Don't load stuff that is already 'reserved' for other vehicles */
|
2007-06-22 22:28:15 +00:00
|
|
|
cap = min((uint)cargo_left[v->cargo_type], cap);
|
2007-05-14 20:12:32 +00:00
|
|
|
cargo_left[v->cargo_type] -= cap;
|
|
|
|
}
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
if (v->cargo.Empty()) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
|
2007-05-14 20:12:32 +00:00
|
|
|
|
2004-11-17 08:52:47 +00:00
|
|
|
/* TODO: Regarding this, when we do gradual loading, we
|
|
|
|
* should first unload all vehicles and then start
|
|
|
|
* loading them. Since this will cause
|
|
|
|
* VEHICLE_TRIGGER_EMPTY to be called at the time when
|
|
|
|
* the whole vehicle chain is really totally empty, the
|
2008-01-10 13:13:18 +00:00
|
|
|
* completely_emptied assignment can then be safely
|
2004-11-17 08:52:47 +00:00
|
|
|
* removed; that's how TTDPatch behaves too. --pasky */
|
2008-01-10 13:13:18 +00:00
|
|
|
completely_emptied = false;
|
2006-12-05 17:53:33 +00:00
|
|
|
anything_loaded = true;
|
2004-11-17 08:52:47 +00:00
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
ge->cargo.MoveTo(&v->cargo, cap, CargoList::MTA_CARGO_LOAD, NULL, st->xy);
|
2007-03-02 18:49:11 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
st->time_since_load = 0;
|
2007-06-22 11:58:59 +00:00
|
|
|
st->last_vehicle_type = v->type;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-19 23:19:12 +00:00
|
|
|
StationAnimationTrigger(st, st->xy, STAT_ANIM_CARGO_TAKEN, v->cargo_type);
|
|
|
|
|
2007-08-26 13:55:36 +00:00
|
|
|
unloading_time += cap;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
result |= 2;
|
|
|
|
}
|
2007-05-13 21:24:58 +00:00
|
|
|
|
2009-02-21 15:56:02 +00:00
|
|
|
if (v->cargo.Count() >= v->cargo_cap) {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(cargo_full, v->cargo_type);
|
2007-05-13 21:24:58 +00:00
|
|
|
} else {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(cargo_not_full, v->cargo_type);
|
2007-05-13 21:24:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-10 13:13:18 +00:00
|
|
|
/* Only set completly_emptied, if we just unloaded all remaining cargo */
|
|
|
|
completely_emptied &= anything_unloaded;
|
|
|
|
|
2007-05-14 20:12:32 +00:00
|
|
|
/* We update these variables here, so gradual loading still fills
|
|
|
|
* all wagons at the same time instead of using the same 'improved'
|
|
|
|
* loading algorithm for the wagons (only fill wagon when there is
|
|
|
|
* enough to fill the previous wagons) */
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.improved_load && (u->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
|
2007-05-14 20:12:32 +00:00
|
|
|
/* Update left cargo */
|
2007-08-30 13:03:56 +00:00
|
|
|
for (v = u; v != NULL; v = v->Next()) {
|
2009-02-21 15:56:02 +00:00
|
|
|
int cap_left = v->cargo_cap - v->cargo.Count();
|
|
|
|
if (cap_left > 0) cargo_left[v->cargo_type] -= cap_left;
|
2007-05-14 20:12:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v = u;
|
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
if (!anything_unloaded) delete payment;
|
|
|
|
|
2007-05-13 21:24:58 +00:00
|
|
|
if (anything_loaded || anything_unloaded) {
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.gradual_loading) {
|
2007-05-13 21:24:58 +00:00
|
|
|
/* The time it takes to load one 'slice' of cargo or passengers depends
|
2009-03-14 18:16:29 +00:00
|
|
|
* on the vehicle type - the values here are those found in TTDPatch */
|
2007-05-13 21:24:58 +00:00
|
|
|
const uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
|
2006-12-02 16:56:32 +00:00
|
|
|
|
2007-05-13 21:24:58 +00:00
|
|
|
unloading_time = gradual_loading_wait_time[v->type];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool finished_loading = true;
|
2008-04-07 20:03:46 +00:00
|
|
|
if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) {
|
2008-04-09 18:26:19 +00:00
|
|
|
if (v->current_order.GetLoadType() == OLF_FULL_LOAD_ANY) {
|
2007-05-13 21:24:58 +00:00
|
|
|
/* if the aircraft carries passengers and is NOT full, then
|
|
|
|
* continue loading, no matter how much mail is in */
|
2009-02-21 15:56:02 +00:00
|
|
|
if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap > v->cargo.Count()) ||
|
2007-05-13 21:24:58 +00:00
|
|
|
(cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are stull non-full cargos
|
|
|
|
finished_loading = false;
|
|
|
|
}
|
|
|
|
} else if (cargo_not_full != 0) {
|
|
|
|
finished_loading = false;
|
2006-12-05 17:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-13 21:24:58 +00:00
|
|
|
unloading_time = 20;
|
|
|
|
|
|
|
|
SB(v->vehicle_flags, VF_LOADING_FINISHED, 1, finished_loading);
|
2006-12-02 16:56:32 +00:00
|
|
|
}
|
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_TRAIN) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Each platform tile is worth 2 rail vehicles. */
|
2009-06-06 16:54:22 +00:00
|
|
|
int overhang = Train::From(v)->tcache.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
|
2005-11-03 09:22:24 +00:00
|
|
|
if (overhang > 0) {
|
|
|
|
unloading_time <<= 1;
|
|
|
|
unloading_time += (overhang * unloading_time) / 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 22:04:49 +00:00
|
|
|
/* Calculate the loading indicator fill percent and display
|
|
|
|
* In the Game Menu do not display indicators
|
2008-05-29 15:13:28 +00:00
|
|
|
* If _settings_client.gui.loading_indicators == 2, show indicators (bool can be promoted to int as 0 or 1 - results in 2 > 0,1 )
|
2008-09-30 20:39:50 +00:00
|
|
|
* if _settings_client.gui.loading_indicators == 1, _local_company must be the owner or must be a spectator to show ind., so 1 > 0
|
2008-05-29 15:13:28 +00:00
|
|
|
* if _settings_client.gui.loading_indicators == 0, do not display indicators ... 0 is never greater than anything
|
2007-09-08 22:04:49 +00:00
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
if (_game_mode != GM_MENU && (_settings_client.gui.loading_indicators > (uint)(v->owner != _local_company && _local_company != COMPANY_SPECTATOR))) {
|
2007-06-22 18:28:44 +00:00
|
|
|
StringID percent_up_down = STR_NULL;
|
|
|
|
int percent = CalcPercentVehicleFilled(v, &percent_up_down);
|
2007-06-21 16:17:47 +00:00
|
|
|
if (v->fill_percent_te_id == INVALID_TE_ID) {
|
2007-06-22 18:28:44 +00:00
|
|
|
v->fill_percent_te_id = ShowFillingPercent(v->x_pos, v->y_pos, v->z_pos + 20, percent, percent_up_down);
|
2007-06-21 16:17:47 +00:00
|
|
|
} else {
|
2007-06-22 18:28:44 +00:00
|
|
|
UpdateFillingPercent(v->fill_percent_te_id, percent, percent_up_down);
|
2007-06-21 16:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-24 00:08:48 +00:00
|
|
|
/* Always wait at least 1, otherwise we'll wait 'infinitively' long. */
|
|
|
|
v->load_unload_time_rem = max(1, unloading_time);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-10 13:13:18 +00:00
|
|
|
if (completely_emptied) {
|
2004-11-17 08:52:47 +00:00
|
|
|
TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (result != 0) {
|
2008-04-24 09:55:20 +00:00
|
|
|
InvalidateWindow(GetWindowClassForVehicleType(v->type), v->owner);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2007-05-02 18:29:11 +00:00
|
|
|
|
2007-06-08 09:35:39 +00:00
|
|
|
st->MarkTilesDirty(true);
|
2007-05-02 18:29:11 +00:00
|
|
|
v->MarkDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-14 16:07:05 +00:00
|
|
|
/**
|
|
|
|
* Load/unload the vehicles in this station according to the order
|
|
|
|
* they entered.
|
|
|
|
* @param st the station to do the loading/unloading for
|
|
|
|
*/
|
|
|
|
void LoadUnloadStation(Station *st)
|
|
|
|
{
|
2009-06-28 14:29:58 +00:00
|
|
|
/* No vehicle is here... */
|
|
|
|
if (st->loading_vehicles.empty()) return;
|
|
|
|
|
2007-05-14 20:12:32 +00:00
|
|
|
int cargo_left[NUM_CARGO];
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
for (uint i = 0; i < NUM_CARGO; i++) cargo_left[i] = st->goods[i].cargo.Count();
|
2007-05-14 20:12:32 +00:00
|
|
|
|
2007-05-14 16:07:05 +00:00
|
|
|
std::list<Vehicle *>::iterator iter;
|
|
|
|
for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
|
|
|
|
Vehicle *v = *iter;
|
2007-05-14 20:12:32 +00:00
|
|
|
if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left);
|
2007-05-14 16:07:05 +00:00
|
|
|
}
|
2009-06-29 19:55:36 +00:00
|
|
|
|
|
|
|
/* Call the production machinery of industries */
|
|
|
|
const Industry * const *isend = _cargo_delivery_destinations.End();
|
|
|
|
for (Industry **iid = _cargo_delivery_destinations.Begin(); iid != isend; iid++) {
|
|
|
|
TriggerIndustryProduction(*iid);
|
|
|
|
}
|
|
|
|
_cargo_delivery_destinations.Clear();
|
2007-05-14 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void CompaniesMonthlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
CompaniesGenStatistics();
|
2008-07-26 14:58:08 +00:00
|
|
|
if (_settings_game.economy.inflation) AddInflation();
|
2008-09-30 20:39:50 +00:00
|
|
|
CompaniesPayInterest();
|
|
|
|
/* Reset the _current_company flag */
|
|
|
|
_current_company = OWNER_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
HandleEconomyFluctuations();
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void DoAcquireCompany(Company *c)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
Company *owner;
|
2007-01-10 18:56:51 +00:00
|
|
|
int i;
|
2007-06-18 21:44:47 +00:00
|
|
|
Money value;
|
2009-01-12 17:11:45 +00:00
|
|
|
CompanyID ci = c->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-07-18 16:26:51 +00:00
|
|
|
CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
|
2009-05-16 23:34:14 +00:00
|
|
|
cni->FillData(c, Company::Get(_current_company));
|
2008-07-18 16:26:51 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
SetDParam(0, STR_NEWS_COMPANY_MERGER_TITLE);
|
|
|
|
SetDParam(1, c->bankrupt_value == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION);
|
2008-07-18 16:26:51 +00:00
|
|
|
SetDParamStr(2, cni->company_name);
|
|
|
|
SetDParamStr(3, cni->other_company_name);
|
2008-09-30 20:39:50 +00:00
|
|
|
SetDParam(4, c->bankrupt_value);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddCompanyNewsItem(STR_NEWS_MESSAGE, NS_COMPANY_MERGER, cni);
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::BroadcastNewEvent(new AIEventCompanyMerger(ci, _current_company));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* original code does this a little bit differently */
|
2008-09-30 20:39:50 +00:00
|
|
|
ChangeNetworkOwner(ci, _current_company);
|
|
|
|
ChangeOwnershipOfCompanyItems(ci, _current_company);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->bankrupt_value == 0) {
|
2009-05-16 23:34:14 +00:00
|
|
|
owner = Company::Get(_current_company);
|
2008-09-30 20:39:50 +00:00
|
|
|
owner->current_loan += c->current_loan;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
value = CalculateCompanyValue(c) >> 2;
|
|
|
|
CompanyID old_company = _current_company;
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != 4; i++) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->share_owners[i] != COMPANY_SPECTATOR) {
|
|
|
|
_current_company = c->share_owners[i];
|
|
|
|
SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -value));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-30 20:39:50 +00:00
|
|
|
_current_company = old_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-06-10 22:05:01 +00:00
|
|
|
if (c->is_ai) AI::Stop(c->index);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
DeleteCompanyWindows(ci);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
|
|
|
|
InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
|
|
|
|
InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
|
|
|
|
InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0);
|
2008-07-18 16:40:29 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
delete c;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
extern int GetAmountOwnedBy(const Company *c, Owner owner);
|
2004-12-12 14:00:25 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Acquire shares in an opposing company.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-06 04:10:19 +00:00
|
|
|
* @param flags type of operation
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param p1 company to buy the shares from
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdBuyShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost cost(EXPENSES_OTHER);
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(p1);
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Check if buying shares is allowed (protection against modified clients)
|
|
|
|
* Cannot buy own shares */
|
2009-05-18 16:21:28 +00:00
|
|
|
if (c == NULL || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR;
|
2007-08-31 17:15:46 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* Protect new companies from hostile takeovers */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (_cur_year - c->inaugurated_year < 6) return_cmd_error(STR_PROTECTED);
|
2005-01-07 23:59:59 +00:00
|
|
|
|
2004-12-12 14:00:25 +00:00
|
|
|
/* Those lines are here for network-protection (clients can be slow) */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 0) return cost;
|
2005-01-07 23:59:59 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* We can not buy out a real company (temporarily). TODO: well, enable it obviously */
|
|
|
|
if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 1 && !c->is_ai) return cost;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
cost.AddCost(CalculateCompanyValue(c) >> 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-09-30 20:39:50 +00:00
|
|
|
OwnerByte *b = c->share_owners;
|
2005-05-12 00:11:37 +00:00
|
|
|
int i;
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
while (*b != COMPANY_SPECTATOR) b++; // share owners is guaranteed to contain at least one COMPANY_SPECTATOR
|
2008-09-30 20:39:50 +00:00
|
|
|
*b = _current_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
for (i = 0; c->share_owners[i] == _current_company;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (++i == 4) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->bankrupt_value = 0;
|
|
|
|
DoAcquireCompany(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-02-13 21:15:00 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Sell shares in an opposing company.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-06 04:10:19 +00:00
|
|
|
* @param flags type of operation
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param p1 company to sell the shares from
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSellShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(p1);
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Check if selling shares is allowed (protection against modified clients)
|
|
|
|
* Cannot sell own shares */
|
2009-05-18 16:21:28 +00:00
|
|
|
if (c == NULL || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR;
|
2007-08-31 17:15:46 +00:00
|
|
|
|
2004-12-14 18:17:24 +00:00
|
|
|
/* Those lines are here for network-protection (clients can be slow) */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (GetAmountOwnedBy(c, _current_company) == 0) return CommandCost();
|
2004-12-14 18:17:24 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* adjust it a little to make it less profitable to sell and buy */
|
2008-09-30 20:39:50 +00:00
|
|
|
Money cost = CalculateCompanyValue(c) >> 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
cost = -(cost - (cost >> 7));
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-09-30 20:39:50 +00:00
|
|
|
OwnerByte *b = c->share_owners;
|
|
|
|
while (*b != _current_company) b++; // share owners is guaranteed to contain company
|
|
|
|
*b = COMPANY_SPECTATOR;
|
2006-02-13 21:15:00 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-01-09 16:55:48 +00:00
|
|
|
return CommandCost(EXPENSES_OTHER, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Buy up another company.
|
|
|
|
* When a competing company is gone bankrupt you get the chance to purchase
|
|
|
|
* that company.
|
2008-09-30 20:39:50 +00:00
|
|
|
* @todo currently this only works for AI companies
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-06 04:10:19 +00:00
|
|
|
* @param flags type of operation
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param p1 company to buy up
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdBuyCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(p1);
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
/* Disable takeovers in multiplayer games */
|
2009-05-18 16:21:28 +00:00
|
|
|
if (c == NULL || _networking) return CMD_ERROR;
|
2007-04-01 10:55:31 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Do not allow companies to take over themselves */
|
2009-05-18 16:21:28 +00:00
|
|
|
if ((CompanyID)p1 == _current_company) return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!c->is_ai) return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-09-30 20:39:50 +00:00
|
|
|
DoAcquireCompany(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-09-30 20:39:50 +00:00
|
|
|
return CommandCost(EXPENSES_OTHER, c->bankrupt_value);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|