src/economy.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 10419 d9a1fa739477
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9058
diff changeset
     3
/** @file economy.cpp Handling of the economy. */
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1884
diff changeset
     6
#include "openttd.h"
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents: 2272
diff changeset
     7
#include "currency.h"
6453
226bcddeba32 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6432
diff changeset
     8
#include "landscape.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
     9
#include "company_base.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    10
#include "company_func.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    11
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "saveload.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "industry.h"
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
    14
#include "industry_map.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "town.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8626
diff changeset
    16
#include "news_func.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5426
diff changeset
    17
#include "network/network.h"
9428
1ba05b499957 (svn r13343) -Codechange: reorder/move variable/functions in the network headers so that nothing from the network directory needs to include basically all network headers.
rubidium
parents: 9413
diff changeset
    18
#include "network/network_func.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    19
#include "variables.h"
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    20
#include "vehicle_gui.h"
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2306
diff changeset
    21
#include "ai/ai.h"
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2642
diff changeset
    22
#include "train.h"
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6844
diff changeset
    23
#include "roadveh.h"
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5683
diff changeset
    24
#include "aircraft.h"
2962
f0a49b646c48 (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2952
diff changeset
    25
#include "newgrf_engine.h"
4701
9e83e5ae6de7 (svn r6611) - Newsounds:
peter1138
parents: 4621
diff changeset
    26
#include "newgrf_sound.h"
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
    27
#include "newgrf_callbacks.h"
7168
25e07ccb4a3d (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7167
diff changeset
    28
#include "newgrf_industries.h"
7229
a5f262f6df1b (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7199
diff changeset
    29
#include "newgrf_industrytiles.h"
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8885
diff changeset
    30
#include "newgrf_station.h"
3386
6d088afdabf4 (svn r4194) -Codechange: Renamed UpdatePlayerHouse to UpdateCompanyHQ because the rest of the code calls it Comapany HQ. Moved it to unmovable_cmd.c because CompanyHQ is an unmovable and has nothing to do with economy.
celestar
parents: 3347
diff changeset
    31
#include "unmovable.h"
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
    32
#include "cargotype.h"
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6639
diff changeset
    33
#include "group.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8106
diff changeset
    34
#include "strings_func.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
    35
#include "tile_cmd.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8121
diff changeset
    36
#include "functions.h"
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8121
diff changeset
    37
#include "window_func.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    38
#include "date_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    39
#include "vehicle_func.h"
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8155
diff changeset
    40
#include "sound_func.h"
8200
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
    41
#include "track_type.h"
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
    42
#include "track_func.h"
8342
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8306
diff changeset
    43
#include "road_func.h"
8200
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
    44
#include "rail_map.h"
8238
d47a86c79603 (svn r11802) -Fix [FS#716]: do not crash trains when leaving depot to a very long track
smatz
parents: 8231
diff changeset
    45
#include "signal_func.h"
8224
c5a64d87cc54 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8200
diff changeset
    46
#include "gfx_func.h"
9058
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
    47
#include "autoreplace_func.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    48
#include "company_gui.h"
10158
8f570dcb0997 (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz
parents: 10146
diff changeset
    49
#include "signs_base.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    51
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    52
#include "table/sprites.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    53
7926
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    54
/**
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    55
 * Multiply two integer values and shift the results to right.
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    56
 *
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    57
 * This function multiplies two integer values. The result is
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    58
 * shifted by the amount of shift to right.
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    59
 *
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    60
 * @param a The first integer
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    61
 * @param b The second integer
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    62
 * @param shift The amount to shift the value to right.
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    63
 * @return The shifted result
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    64
 */
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    65
static inline int32 BigMulS(const int32 a, const int32 b, const uint8 shift)
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    66
{
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    67
	return (int32)((int64)a * (int64)b >> shift);
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    68
}
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    69
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    70
/**
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    71
 * Multiply two unsigned integers and shift the results to right.
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    72
 *
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    73
 * This function multiplies two unsigned integers. The result is
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    74
 * shifted by the amount of shift to right.
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    75
 *
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    76
 * @param a The first unsigned integer
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    77
 * @param b The second unsigned integer
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    78
 * @param shift The amount to shift the value to right.
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    79
 * @return The shifted result
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    80
 */
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    81
static inline uint32 BigMulSU(const uint32 a, const uint32 b, const uint8 shift)
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    82
{
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    83
	return (uint32)((uint64)a * (uint64)b >> shift);
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    84
}
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
    85
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
    86
/* Score info */
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    87
const ScoreInfo _score_info[] = {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    88
	{ SCORE_VEHICLES,        120, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    89
	{ SCORE_STATIONS,         80, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    90
	{ SCORE_MIN_PROFIT,    10000, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    91
	{ SCORE_MIN_INCOME,    50000,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    92
	{ SCORE_MAX_INCOME,   100000, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    93
	{ SCORE_DELIVERED,     40000, 400 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    94
	{ SCORE_CARGO,             8,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    95
	{ SCORE_MONEY,      10000000,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    96
	{ SCORE_LOAN,         250000,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    97
	{ SCORE_TOTAL,             0,   0 }
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    98
};
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    99
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   100
int _score_part[MAX_COMPANIES][SCORE_END];
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
   101
Economy _economy;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   102
Subsidy _subsidies[MAX_COMPANIES];
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
   103
Prices _price;
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
   104
uint16 _price_frac[NUM_PRICES];
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
   105
Money  _cargo_payment_rates[NUM_CARGO];
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
   106
uint16 _cargo_payment_rates_frac[NUM_CARGO];
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
   107
Money _additional_cash_required;
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
   108
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   109
Money CalculateCompanyValue(const Company *c)
2475
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
   110
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   111
	Owner owner = c->index;
7781
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   112
	Money value = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   113
7781
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   114
	Station *st;
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   115
	uint num = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   116
7781
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   117
	FOR_ALL_STATIONS(st) {
7832
60035eb28f08 (svn r11382) -Codechange: renamed COUNTBITS to CountBits, as it is no longer a macro (skidd13)
truelight
parents: 7824
diff changeset
   118
		if (st->owner == owner) num += CountBits(st->facilities);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
7781
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   121
	value += num * _price.station_value * 25;
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   122
7781
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   123
	Vehicle *v;
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   124
	FOR_ALL_VEHICLES(v) {
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   125
		if (v->owner != owner) continue;
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   126
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   127
		if (v->type == VEH_TRAIN ||
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   128
				v->type == VEH_ROAD ||
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   129
				(v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) ||
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   130
				v->type == VEH_SHIP) {
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   131
			value += v->value * 3 >> 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   135
	/* Add real money value */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   136
	value -= c->current_loan;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   137
	value += c->money;
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
   138
7781
bceee111342f (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium
parents: 7763
diff changeset
   139
	return max(value, (Money)1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   142
/** if update is set to true, the economy is updated with this score
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   143
 *  (also the house is updated, should only be true in the on-tick event)
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   144
 * @param update the economy with calculated score
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   145
 * @param c company been evaluated
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   146
 * @return actual score of this company
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   147
 * */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   148
int UpdateCompanyRatingAndValue(Company *c, bool update)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   150
	Owner owner = c->index;
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   151
	int score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   152
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   153
	memset(_score_part[owner], 0, sizeof(_score_part[owner]));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   155
	/* Count vehicles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
		Vehicle *v;
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
   158
		Money min_profit = 0;
2829
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   159
		bool min_profit_first = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
		uint num = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
		FOR_ALL_VEHICLES(v) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   163
			if (v->owner != owner) continue;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   164
			if (IsCompanyBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
				num++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
				if (v->age > 730) {
2829
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   167
					/* Find the vehicle with the lowest amount of profit */
8614
4c67a883c4c4 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents: 8556
diff changeset
   168
					if (min_profit_first || min_profit > v->profit_last_year) {
4c67a883c4c4 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents: 8556
diff changeset
   169
						min_profit = v->profit_last_year;
2829
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   170
						min_profit_first = false;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   171
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
8614
4c67a883c4c4 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents: 8556
diff changeset
   176
		min_profit >>= 8; // remove the fract part
4c67a883c4c4 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz
parents: 8556
diff changeset
   177
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   178
		_score_part[owner][SCORE_VEHICLES] = num;
2829
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   179
		/* Don't allow negative min_profit to show */
1407
7d3702835802 (svn r1911) Put back an "if" i accidently removed in r1898
tron
parents: 1394
diff changeset
   180
		if (min_profit > 0)
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
   181
			_score_part[owner][SCORE_MIN_PROFIT] = ClampToI32(min_profit);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   184
	/* Count stations */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
		uint num = 0;
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   187
		const Station* st;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
		FOR_ALL_STATIONS(st) {
7832
60035eb28f08 (svn r11382) -Codechange: renamed COUNTBITS to CountBits, as it is no longer a macro (skidd13)
truelight
parents: 7824
diff changeset
   190
			if (st->owner == owner) num += CountBits(st->facilities);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
		}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   192
		_score_part[owner][SCORE_STATIONS] = num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   195
	/* Generate statistics depending on recent income statistics */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
	{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   197
		int numec = min(c->num_valid_stat_ent, 12);
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   198
		if (numec != 0) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   199
			const CompanyEconomyEntry *cee = c->old_economy;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   200
			Money min_income = cee->income + cee->expenses;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   201
			Money max_income = cee->income + cee->expenses;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
			do {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   204
				min_income = min(min_income, cee->income + cee->expenses);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   205
				max_income = max(max_income, cee->income + cee->expenses);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   206
			} while (++cee,--numec);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   208
			if (min_income > 0) {
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
   209
				_score_part[owner][SCORE_MIN_INCOME] = ClampToI32(min_income);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   210
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
   212
			_score_part[owner][SCORE_MAX_INCOME] = ClampToI32(max_income);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   216
	/* Generate score depending on amount of transported cargo */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
	{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   218
		const CompanyEconomyEntry *cee;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
		int numec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
		uint32 total_delivered;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   222
		numec = min(c->num_valid_stat_ent, 4);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
		if (numec != 0) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   224
			cee = c->old_economy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
			total_delivered = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
			do {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   227
				total_delivered += cee->delivered_cargo;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   228
			} while (++cee,--numec);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   230
			_score_part[owner][SCORE_DELIVERED] = total_delivered;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
	}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   233
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   234
	/* Generate score for variety of cargo */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
	{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   236
		uint num = CountBits(c->cargo_types);
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   237
		_score_part[owner][SCORE_CARGO] = num;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   238
		if (update) c->cargo_types = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   241
	/* Generate score for company's money */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
	{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   243
		if (c->money > 0) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   244
			_score_part[owner][SCORE_MONEY] = ClampToI32(c->money);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   248
	/* Generate score for loan */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
	{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   250
		_score_part[owner][SCORE_LOAN] = ClampToI32(_score_info[SCORE_LOAN].needed - c->current_loan);
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   251
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   252
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   253
	/* Now we calculate the score for each item.. */
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   254
	{
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   255
		int total_score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   256
		int s;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   257
		score = 0;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   258
		for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   259
			/* Skip the total */
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   260
			if (i == SCORE_TOTAL) continue;
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   261
			/*  Check the score */
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7860
diff changeset
   262
			s = Clamp(_score_part[owner][i], 0, _score_info[i].needed) * _score_info[i].score / _score_info[i].needed;
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   263
			score += s;
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
   264
			total_score += _score_info[i].score;
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   265
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   266
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   267
		_score_part[owner][SCORE_TOTAL] = score;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   268
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   269
		/*  We always want the score scaled to SCORE_MAX (1000) */
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   270
		if (total_score != SCORE_MAX) score = score * SCORE_MAX / total_score;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   273
	if (update) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   274
		c->old_economy[0].performance_history = score;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   275
		UpdateCompanyHQ(c, score);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   276
		c->old_economy[0].company_value = CalculateCompanyValue(c);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   277
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   278
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   279
	InvalidateWindow(WC_PERFORMANCE_DETAIL, 0);
147
d0c8100d18cf (svn r148) -Feature: Company HQ can now be moved somewhere else (cost 1% of company value). Water floods HQ.
darkvater
parents: 116
diff changeset
   280
	return score;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   283
/*  use INVALID_OWNER as new_owner to delete the company. */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   284
void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
{
5284
abfa5fac015f (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   286
	Town *t;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   287
	CompanyID old = _current_company;
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   288
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   289
	assert(old_owner != new_owner);
6405
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
   290
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   291
	{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   292
		Company *c;
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   293
		uint i;
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   294
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   295
		/* See if the old_owner had shares in other companies */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   296
		_current_company = old_owner;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   297
		FOR_ALL_COMPANIES(c) {
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   298
			for (i = 0; i < 4; i++) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   299
				if (c->share_owners[i] == old_owner) {
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   300
					/* Sell his shares */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   301
					CommandCost res = DoCommand(0, c->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   302
					/* Because we are in a DoCommand, we can't just execute an other one and
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   303
					 *  expect the money to be removed. We need to do it ourself! */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   304
					SubtractMoneyFromCompany(res);
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   305
				}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   306
			}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   307
		}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   308
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   309
		/* Sell all the shares that people have on this company */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   310
		c = GetCompany(old_owner);
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   311
		for (i = 0; i < 4; i++) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   312
			_current_company = c->share_owners[i];
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   313
			if (_current_company != INVALID_OWNER) {
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   314
				/* Sell the shares */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   315
				CommandCost res = DoCommand(0, old_owner, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   316
				/* Because we are in a DoCommand, we can't just execute an other one and
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   317
				 *  expect the money to be removed. We need to do it ourself! */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   318
				SubtractMoneyFromCompany(res);
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   319
			}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   320
		}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   321
	}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   322
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   323
	_current_company = old_owner;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   325
	/* Temporarily increase the company's money, to be sure that
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4540
diff changeset
   326
	 * removing his/her property doesn't fail because of lack of money.
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4540
diff changeset
   327
	 * Not too drastically though, because it could overflow */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   328
	if (new_owner == INVALID_OWNER) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   329
		GetCompany(old_owner)->money = UINT64_MAX >> 2; // jackpot ;p
4260
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   330
	}
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   331
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   332
	if (new_owner == INVALID_OWNER) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
		Subsidy *s;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   334
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   335
		for (s = _subsidies; s != endof(_subsidies); s++) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   336
			if (s->cargo_type != CT_INVALID && s->age >= 12) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   337
				if (GetStation(s->to)->owner == old_owner) s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   342
	/* Take care of rating in towns */
5284
abfa5fac015f (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   343
	FOR_ALL_TOWNS(t) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   344
		/* If a company takes over, give the ratings to that company. */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   345
		if (new_owner != INVALID_OWNER) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   346
			if (HasBit(t->have_ratings, old_owner)) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   347
				if (HasBit(t->have_ratings, new_owner)) {
4260
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   348
					// use max of the two ratings.
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   349
					t->ratings[new_owner] = max(t->ratings[new_owner], t->ratings[old_owner]);
4260
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   350
				} else {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   351
					SetBit(t->have_ratings, new_owner);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   352
					t->ratings[new_owner] = t->ratings[old_owner];
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   353
				}
4260
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   354
			}
5284
abfa5fac015f (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   355
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   356
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   357
		/* Reset the ratings for the old owner */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   358
		t->ratings[old_owner] = RATING_INITIAL;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   359
		ClrBit(t->have_ratings, old_owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   361
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
		int num_train = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
		int num_road = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
		int num_ship = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
		int num_aircraft = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
		Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   369
		/*  Determine Ids for the new vehicles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
		FOR_ALL_VEHICLES(v) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   371
			if (v->owner == new_owner) {
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   372
				switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   373
					case VEH_TRAIN:    if (IsFrontEngine(v)) num_train++; break;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6844
diff changeset
   374
					case VEH_ROAD:     if (IsRoadVehFront(v)) num_road++; break;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   375
					case VEH_SHIP:     num_ship++; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   376
					case VEH_AIRCRAFT: if (IsNormalAircraft(v)) num_aircraft++; break;
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   377
					default: break;
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   378
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
		FOR_ALL_VEHICLES(v) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   383
			if (v->owner == old_owner && IsInsideMM(v->type, VEH_TRAIN, VEH_AIRCRAFT + 1)) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   384
				if (new_owner == INVALID_OWNER) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
					DeleteWindowById(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
					DeleteWindowById(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
					DeleteWindowById(WC_VEHICLE_ORDERS, v->index);
6876
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   388
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   389
					if (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && IsFreeWagon(v))) {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   390
						switch (v->type) {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   391
							default: NOT_REACHED();
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   392
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   393
							case VEH_TRAIN: {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   394
								Vehicle *u = v;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   395
								do {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   396
									Vehicle *next = GetNextVehicle(u);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7329
diff changeset
   397
									delete u;
6876
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   398
									u = next;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   399
								} while (u != NULL);
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   400
							} break;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   401
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   402
							case VEH_ROAD:
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   403
							case VEH_SHIP:
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7329
diff changeset
   404
								delete v;
6876
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   405
								break;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   406
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   407
							case VEH_AIRCRAFT:
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   408
								DeleteVehicleChain(v);
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   409
								break;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   410
						}
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   411
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
				} else {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   413
					v->owner = new_owner;
8186
6ebffac017be (svn r11749) -Fix (r11352): when a bankrupted company is bought, reset vehicle color mapping so the vehicles use the new owner color
glx
parents: 8157
diff changeset
   414
					v->colormap = PAL_NONE;
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6639
diff changeset
   415
					v->group_id = DEFAULT_GROUP;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   416
					if (IsEngineCountable(v)) GetCompany(new_owner)->num_engines[v->engine_type]++;
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   417
					switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   418
						case VEH_TRAIN:    if (IsFrontEngine(v)) v->unitnumber = ++num_train; break;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6844
diff changeset
   419
						case VEH_ROAD:     if (IsRoadVehFront(v)) v->unitnumber = ++num_road; break;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   420
						case VEH_SHIP:     v->unitnumber = ++num_ship; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   421
						case VEH_AIRCRAFT: if (IsNormalAircraft(v)) v->unitnumber = ++num_aircraft; break;
6621
441559124aea (svn r9841) -Codechange: add a little more type strictness to the vehicle types.
rubidium
parents: 6620
diff changeset
   422
						default: NOT_REACHED();
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   423
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   429
	/*  Change ownership of tiles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
	{
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   431
		TileIndex tile = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
		do {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   433
			ChangeTileOwner(tile, old_owner, new_owner);
863
6a1444534f62 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 830
diff changeset
   434
		} while (++tile != MapSize());
8200
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   435
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   436
		if (new_owner != INVALID_OWNER) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   437
			/* Update all signals because there can be new segment that was owned by two companies
8342
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8306
diff changeset
   438
			 * and signals were not propagated
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8306
diff changeset
   439
			 * Similiar with crossings - it is needed to bar crossings that weren't before
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8306
diff changeset
   440
			 * because of different owner of crossing and approaching train */
8200
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   441
			tile = 0;
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   442
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   443
			do {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   444
				if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, new_owner) && HasSignals(tile)) {
8200
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   445
					TrackBits tracks = GetTrackBits(tile);
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   446
					do { // there may be two tracks with signals for TRACK_BIT_HORZ and TRACK_BIT_VERT
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   447
						Track track = RemoveFirstTrack(&tracks);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   448
						if (HasSignalOnTrack(tile, track)) AddTrackToSignalBuffer(tile, track, new_owner);
8200
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   449
					} while (tracks != TRACK_BIT_NONE);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   450
				} else if (IsLevelCrossingTile(tile) && IsTileOwner(tile, new_owner)) {
8342
bf710180e7ce (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz
parents: 8306
diff changeset
   451
					UpdateLevelCrossing(tile);
8200
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   452
				}
633e55a3ca3d (svn r11763) -Fix: update signal states after merging a company
smatz
parents: 8186
diff changeset
   453
			} while (++tile != MapSize());
8347
7edfa9e0c8ad (svn r11913) -Fix (r11871): update signals after company bankrupt and disaster too
smatz
parents: 8342
diff changeset
   454
		}
8306
22e1344c5457 (svn r11871) -Fix [FS#1074]: do not update signals after each tile when building/removing a large block of track/signals/station
smatz
parents: 8302
diff changeset
   455
8347
7edfa9e0c8ad (svn r11913) -Fix (r11871): update signals after company bankrupt and disaster too
smatz
parents: 8342
diff changeset
   456
		/* update signals in buffer */
7edfa9e0c8ad (svn r11913) -Fix (r11871): update signals after company bankrupt and disaster too
smatz
parents: 8342
diff changeset
   457
		UpdateSignalsInBuffer();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
9058
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   460
	/* In all cases clear replace engine rules.
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   461
	 * Even if it was copied, it could interfere with new owner's rules */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   462
	RemoveAllEngineReplacementForCompany(GetCompany(old_owner));
9058
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   463
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   464
	if (new_owner == INVALID_OWNER) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   465
		RemoveAllGroupsForCompany(old_owner);
9058
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   466
	} else {
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   467
		Group *g;
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   468
		FOR_ALL_GROUPS(g) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   469
			if (g->owner == old_owner) g->owner = new_owner;
9058
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   470
		}
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   471
	}
25cfd71b68e6 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz
parents: 9043
diff changeset
   472
10158
8f570dcb0997 (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz
parents: 10146
diff changeset
   473
	Sign *si;
8f570dcb0997 (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz
parents: 10146
diff changeset
   474
	FOR_ALL_SIGNS(si) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   475
		if (si->owner == old_owner) si->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
10158
8f570dcb0997 (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz
parents: 10146
diff changeset
   476
	}
8f570dcb0997 (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz
parents: 10146
diff changeset
   477
5077
5a601eb01735 (svn r7138) -Fix: [vehicle list windows] fixed a rare crash where having some (not all) vehicle list windows open for a player, that goes bankrupt would crash the game
bjarni
parents: 5067
diff changeset
   478
	/* Change color of existing windows */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   479
	if (new_owner != INVALID_OWNER) ChangeWindowOwner(old_owner, new_owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   481
	_current_company = old;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   482
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
	MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   486
static void ChangeNetworkOwner(Owner current_owner, Owner new_owner)
6262
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   487
{
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   488
#ifdef ENABLE_NETWORK
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   489
	if (!_networking) return;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   490
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   491
	if (current_owner == _local_company) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   492
		_network_playas = new_owner;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   493
		SetLocalCompany(new_owner);
6262
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   494
	}
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   495
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   496
	if (!_network_server) return;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   497
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   498
	NetworkServerChangeOwner(current_owner, new_owner);
6262
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   499
#endif /* ENABLE_NETWORK */
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   500
}
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   501
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   502
static void CompanyCheckBankrupt(Company *c)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   504
	/*  If the company has money again, it does not go bankrupt */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   505
	if (c->money >= 0) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   506
		c->quarters_of_bankrupcy = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   510
	c->quarters_of_bankrupcy++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   512
	CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   513
	cni->FillData(c);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   515
	switch (c->quarters_of_bankrupcy) {
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   516
		default:
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   517
			free(cni);
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   518
			break;
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   519
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   520
		case 2:
9199
ce9fbdff6581 (svn r13065) -Codechange: remove the need for the news string callbacks. Patch by Cirdan.
rubidium
parents: 9158
diff changeset
   521
			SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
ce9fbdff6581 (svn r13065) -Codechange: remove the need for the news string callbacks. Patch by Cirdan.
rubidium
parents: 9158
diff changeset
   522
			SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   523
			SetDParamStr(2, cni->company_name);
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   524
			AddNewsItem(STR_02B6, NS_COMPANY_TROUBLE, 0, 0, cni);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   525
			break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   526
		case 3: {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   527
			/* XXX - In multiplayer, should we ask other companies if it wants to take
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   528
		          over when it is a human company? -- TrueLight */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   529
			if (IsHumanCompany(c->index)) {
9199
ce9fbdff6581 (svn r13065) -Codechange: remove the need for the news string callbacks. Patch by Cirdan.
rubidium
parents: 9158
diff changeset
   530
				SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
ce9fbdff6581 (svn r13065) -Codechange: remove the need for the news string callbacks. Patch by Cirdan.
rubidium
parents: 9158
diff changeset
   531
				SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   532
				SetDParamStr(2, cni->company_name);
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   533
				AddNewsItem(STR_02B6, NS_COMPANY_TROUBLE, 0, 0, cni);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   534
				break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   535
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   537
			/* Check if the company has any value.. if not, declare it bankrupt
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   538
			 *  right now */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   539
			Money val = CalculateCompanyValue(c);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   540
			if (val > 0) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   541
				c->bankrupt_value = val;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   542
				c->bankrupt_asked = 1 << c->index; // Don't ask the owner
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   543
				c->bankrupt_timeout = 0;
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   544
				free(cni);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   545
				break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   546
			}
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   547
			/* Else, falltrue to case 4... */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   549
		case 4: {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   550
			/* Close everything the owner has open */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   551
			DeleteCompanyWindows(c->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   553
			/* Show bankrupt news */
9199
ce9fbdff6581 (svn r13065) -Codechange: remove the need for the news string callbacks. Patch by Cirdan.
rubidium
parents: 9158
diff changeset
   554
			SetDParam(0, STR_705C_BANKRUPT);
ce9fbdff6581 (svn r13065) -Codechange: remove the need for the news string callbacks. Patch by Cirdan.
rubidium
parents: 9158
diff changeset
   555
			SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   556
			SetDParamStr(2, cni->company_name);
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
   557
			AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, 0, cni);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   558
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   559
			if (IsHumanCompany(c->index)) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   560
				/* XXX - If we are in offline mode, leave the company playing. Eg. there
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   561
				 * is no THE-END, otherwise mark the client as spectator to make sure
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   562
				 * he/she is no long in control of this company */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   563
				if (!_networking) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   564
					c->bankrupt_asked = 0xFF;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   565
					c->bankrupt_timeout = 0x456;
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   566
					break;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   567
				}
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   568
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   569
				ChangeNetworkOwner(c->index, COMPANY_SPECTATOR);
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   570
			}
687
0e42e732ad78 (svn r1128) -Fix: [Network] Going bankrupt of human-players was corrupted in networkgames
truelight
parents: 679
diff changeset
   571
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   572
			/* Remove the company */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   573
			ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   574
			/* Register the company as not-active */
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2306
diff changeset
   575
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   576
			if (!IsHumanCompany(c->index) && (!_networking || _network_server) && _ai.enabled)
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   577
				AI_CompanyDied(c->index);
9659
187142ff9b6c (svn r13731) -Codechange: make a pool of the array of players.
rubidium
parents: 9658
diff changeset
   578
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   579
			delete c;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   584
static void CompaniesGenStatistics()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
	Station *st;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   587
	Company *c;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
	FOR_ALL_STATIONS(st) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   590
		_current_company = st->owner;
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   591
		CommandCost cost(EXPENSES_PROPERTY, _price.station_value >> 1);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   592
		SubtractMoneyFromCompany(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
10080
4ac7070a5b0b (svn r14254) -Codechange: Code style and breathing room
belugas
parents: 9950
diff changeset
   595
	if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_month))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   598
	FOR_ALL_COMPANIES(c) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   599
		memmove(&c->old_economy[1], &c->old_economy[0], sizeof(c->old_economy) - sizeof(c->old_economy[0]));
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   600
		c->old_economy[0] = c->cur_economy;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   601
		memset(&c->cur_economy, 0, sizeof(c->cur_economy));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   603
		if (c->num_valid_stat_ent != 24) c->num_valid_stat_ent++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   605
		UpdateCompanyRatingAndValue(c, true);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   606
		CompanyCheckBankrupt(c);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   608
		if (c->block_preview != 0) c->block_preview--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
	InvalidateWindow(WC_INCOME_GRAPH, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
	InvalidateWindow(WC_OPERATING_PROFIT, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
	InvalidateWindow(WC_DELIVERED_CARGO, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
	InvalidateWindow(WC_COMPANY_VALUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	InvalidateWindow(WC_COMPANY_LEAGUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   619
static void AddSingleInflation(Money *value, uint16 *frac, int32 amt)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
{
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   621
	/* Is it safe to add inflation ? */
6957
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6955
diff changeset
   622
	if ((INT64_MAX / amt) < (*value + 1)) {
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6955
diff changeset
   623
		*value = INT64_MAX / amt;
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   624
		*frac = 0;
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   625
	} else {
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   626
		int64 tmp = (int64)*value * amt + *frac;
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   627
		*frac   = GB(tmp, 0, 16);
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   628
		*value += tmp >> 16;
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   629
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
9717
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   632
static void AddInflation(bool check_year = true)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
{
7244
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   634
	/* The cargo payment inflation differs from the normal inflation, so the
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   635
	 * relative amount of money you make with a transport decreases slowly over
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   636
	 * the 170 years. After a few hundred years we reach a level in which the
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   637
	 * games will become unplayable as the maximum income will be less than
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   638
	 * the minimum running cost.
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   639
	 *
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   640
	 * Furthermore there are a lot of inflation related overflows all over the
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   641
	 * place. Solving them is hardly possible because inflation will always
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   642
	 * reach the overflow threshold some day. So we'll just perform the
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   643
	 * inflation mechanism during the first 170 years (the amount of years that
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   644
	 * one had in the original TTD) and stop doing the inflation after that
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   645
	 * because it only causes problems that can't be solved nicely and the
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   646
	 * inflation doesn't add anything after that either; it even makes playing
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   647
	 * it impossible due to the diverging cost and income rates.
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   648
	 */
9717
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   649
	if (check_year && (_cur_year - _settings_game.game_creation.starting_year) >= (ORIGINAL_MAX_YEAR - ORIGINAL_BASE_YEAR)) return;
7244
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   650
6119
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   651
	/* Approximation for (100 + infl_amount)% ** (1 / 12) - 100%
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   652
	 * scaled by 65536
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   653
	 * 12 -> months per year
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   654
	 * This is only a good approxiamtion for small values
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   655
	 */
9438
e6aa429e06fc (svn r13356) -Codechange: Incorrect documentation and variable type for inflation rates
peter1138
parents: 9428
diff changeset
   656
	int32 inf = _economy.infl_amount * 54;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
6119
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   658
	for (uint i = 0; i != NUM_PRICES; i++) {
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   659
		AddSingleInflation((Money*)&_price + i, _price_frac + i, inf);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
7244
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   662
	AddSingleInflation(&_economy.max_loan_unround, &_economy.max_loan_unround_fract, inf);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   663
7244
8e6293476b5c (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7229
diff changeset
   664
	if (_economy.max_loan + 50000 <= _economy.max_loan_unround) _economy.max_loan += 50000;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
	inf = _economy.infl_amount_pr * 54;
6350
04b19f551aec (svn r9388) -Codechange: variable scope and type, and standardify all CargoID loops.
peter1138
parents: 6315
diff changeset
   667
	for (CargoID i = 0; i < NUM_CARGO; i++) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   668
		AddSingleInflation(
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   669
			(Money*)_cargo_payment_rates + i,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
			_cargo_payment_rates_frac + i,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
			inf
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   672
		);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   673
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
1098
dac6a13ff91d (svn r1599) fix: autoreplace window now updates when inflation alters prices
bjarni
parents: 1093
diff changeset
   676
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
	InvalidateWindowClasses(WC_VEHICLE_DETAILS);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
	InvalidateWindow(WC_PAYMENT_RATES, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   681
static void CompaniesPayInterest()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   683
	const Company *c;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
	int interest = _economy.interest_rate * 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   686
	FOR_ALL_COMPANIES(c) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   687
		_current_company = c->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   688
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   689
		SubtractMoneyFromCompany(CommandCost(EXPENSES_LOAN_INT, (Money)BigMulSU(c->current_loan, interest, 16)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
   691
		SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, _price.station_value >> 2));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   695
static void HandleEconomyFluctuations()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
   697
	if (_settings_game.difficulty.economy == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
	if (--_economy.fluct == 0) {
2642
1d79b44ccbc9 (svn r3184) GB/SB
tron
parents: 2639
diff changeset
   700
		_economy.fluct = -(int)GB(Random(), 0, 2);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9199
diff changeset
   701
		AddNewsItem(STR_7073_WORLD_RECESSION_FINANCIAL, NS_ECONOMY, 0, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
	} else if (_economy.fluct == -12) {
2642
1d79b44ccbc9 (svn r3184) GB/SB
tron
parents: 2639
diff changeset
   703
		_economy.fluct = GB(Random(), 0, 8) + 312;
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9199
diff changeset
   704
		AddNewsItem(STR_7074_RECESSION_OVER_UPTURN_IN, NS_ECONOMY, 0, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
static byte _price_category[NUM_PRICES] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
	0, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
	2, 2, 1, 1, 1, 1, 1, 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
	2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   718
static const Money _price_base[NUM_PRICES] = {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   719
	    100, ///< station_value
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   720
	    100, ///< build_rail
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   721
	     95, ///< build_road
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   722
	     65, ///< build_signals
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   723
	    275, ///< build_bridge
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   724
	    600, ///< build_train_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   725
	    500, ///< build_road_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   726
	    700, ///< build_ship_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   727
	    450, ///< build_tunnel
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   728
	    200, ///< train_station_track
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   729
	    180, ///< train_station_length
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   730
	    600, ///< build_airport
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   731
	    200, ///< build_bus_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   732
	    200, ///< build_truck_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   733
	    350, ///< build_dock
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   734
	 400000, ///< build_railvehicle
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   735
	   2000, ///< build_railwagon
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   736
	 700000, ///< aircraft_base
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   737
	  14000, ///< roadveh_base
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   738
	  65000, ///< ship_base
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   739
	     20, ///< build_trees
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   740
	    250, ///< terraform
7977
4a4e11757be5 (svn r11533) -Codechange: Rename some unclear clear_x prices member names.
belugas
parents: 7967
diff changeset
   741
	     20, ///< clear_grass
4a4e11757be5 (svn r11533) -Codechange: Rename some unclear clear_x prices member names.
belugas
parents: 7967
diff changeset
   742
	     40, ///< clear_roughland
4a4e11757be5 (svn r11533) -Codechange: Rename some unclear clear_x prices member names.
belugas
parents: 7967
diff changeset
   743
	    200, ///< clear_rocks
4a4e11757be5 (svn r11533) -Codechange: Rename some unclear clear_x prices member names.
belugas
parents: 7967
diff changeset
   744
	    500, ///< clear_fields
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   745
	     20, ///< remove_trees
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   746
	    -70, ///< remove_rail
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   747
	     10, ///< remove_signals
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   748
	     50, ///< clear_bridge
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   749
	     80, ///< remove_train_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   750
	     80, ///< remove_road_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   751
	     90, ///< remove_ship_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   752
	     30, ///< clear_tunnel
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   753
	  10000, ///< clear_water
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   754
	     50, ///< remove_rail_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   755
	     30, ///< remove_airport
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   756
	     50, ///< remove_bus_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   757
	     50, ///< remove_truck_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   758
	     55, ///< remove_dock
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   759
	   1600, ///< remove_house
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   760
	     40, ///< remove_road
8624
01b74528e7f6 (svn r12207) -Change: rail running costs are engine class, not rail type
peter1138
parents: 8614
diff changeset
   761
	   5600, ///< running_rail[0] steam
01b74528e7f6 (svn r12207) -Change: rail running costs are engine class, not rail type
peter1138
parents: 8614
diff changeset
   762
	   5200, ///< running_rail[1] diesel
01b74528e7f6 (svn r12207) -Change: rail running costs are engine class, not rail type
peter1138
parents: 8614
diff changeset
   763
	   4800, ///< running_rail[2] electric
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   764
	   9600, ///< aircraft_running
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   765
	   1600, ///< roadveh_running
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   766
	   5600, ///< ship_running
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   767
	1000000, ///< build_industry
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   770
static byte price_base_multiplier[NUM_PRICES];
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   771
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   772
/**
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   773
 * Reset changes to the price base multipliers.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   774
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   775
void ResetPriceBaseMultipliers()
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   776
{
2508
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   777
	uint i;
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   778
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   779
	/* 8 means no multiplier. */
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   780
	for (i = 0; i < NUM_PRICES; i++)
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   781
		price_base_multiplier[i] = 8;
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   782
}
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   783
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   784
/**
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   785
 * Change a price base by the given factor.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   786
 * The price base is altered by factors of two, with an offset of 8.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   787
 * NewBaseCost = OldBaseCost * 2^(n-8)
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   788
 * @param price Index of price base to change.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   789
 * @param factor Amount to change by.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   790
 */
2508
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   791
void SetPriceBaseMultiplier(uint price, byte factor)
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   792
{
2508
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   793
	assert(price < NUM_PRICES);
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   794
	price_base_multiplier[price] = factor;
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   795
}
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   796
10146
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   797
/**
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   798
 * Initialize the variables that will maintain the daily industry change system.
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   799
 * @param init_counter specifies if the counter is required to be initialized
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   800
 */
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   801
static void StartupIndustryDailyChanges(bool init_counter)
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   802
{
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   803
	uint map_size = MapLogX() + MapLogY();
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   804
	/* After getting map size, it needs to be scaled appropriately and divided by 31,
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   805
	 * which stands for the days in a month.
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   806
	 * Using just 31 will make it so that a monthly reset (based on the real number of days of that month)
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   807
	 * would not be needed.
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   808
	 * Since it is based on "fractionnal parts", the leftover days will not make much of a difference
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   809
	 * on the overall total number of changes performed */
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   810
	_economy.industry_daily_increment = (1 << map_size) / 31;
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   811
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   812
	if (init_counter) {
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   813
		/* A new game or a savegame from an older version will require the counter to be initialized */
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   814
		_economy.industry_daily_change_counter = 0;
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   815
	}
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   816
}
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   817
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   818
void StartupEconomy()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   822
	assert(sizeof(_price) == NUM_PRICES * sizeof(Money));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   824
	for (i = 0; i != NUM_PRICES; i++) {
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   825
		Money price = _price_base[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
		if (_price_category[i] != 0) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
   827
			uint mod = _price_category[i] == 1 ? _settings_game.difficulty.vehicle_costs : _settings_game.difficulty.construction_cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
			if (mod < 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
				price = price * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
			} else if (mod > 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
				price = price * 9 >> 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
		}
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   834
		if (price_base_multiplier[i] > 8) {
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   835
			price <<= price_base_multiplier[i] - 8;
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   836
		} else {
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   837
			price >>= 8 - price_base_multiplier[i];
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   838
		}
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
   839
		((Money*)&_price)[i] = price;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
		_price_frac[i] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
   843
	_economy.interest_rate = _settings_game.difficulty.initial_interest;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
   844
	_economy.infl_amount = _settings_game.difficulty.initial_interest;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
   845
	_economy.infl_amount_pr = max(0, _settings_game.difficulty.initial_interest - 1);
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
   846
	_economy.max_loan_unround = _economy.max_loan = _settings_game.difficulty.max_loan;
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2070
diff changeset
   847
	_economy.fluct = GB(Random(), 0, 8) + 168;
10146
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   848
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   849
	StartupIndustryDailyChanges(true); // As we are starting a new game, initialize the counter too
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
   850
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
9717
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   853
void ResetEconomy()
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   854
{
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   855
	/* Test if resetting the economy is needed. */
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   856
	bool needed = false;
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   857
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   858
	for (CargoID c = 0; c < NUM_CARGO; c++) {
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   859
		const CargoSpec *cs = GetCargo(c);
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   860
		if (!cs->IsValid()) continue;
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   861
		if (_cargo_payment_rates[c] == 0) {
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   862
			needed = true;
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   863
			break;
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   864
		}
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   865
	}
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   866
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   867
	if (!needed) return;
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   868
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   869
	/* Remember old unrounded maximum loan value. NewGRF has the ability
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   870
	 * to change all the other inflation affected base costs. */
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   871
	Money old_value = _economy.max_loan_unround;
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   872
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   873
	/* Reset the economy */
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   874
	StartupEconomy();
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   875
	InitializeLandscapeVariables(false);
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   876
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   877
	/* Reapply inflation, ignoring the year */
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   878
	while (old_value > _economy.max_loan_unround) {
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   879
		AddInflation(false);
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   880
	}
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
   881
}
8626
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   882
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   883
Money GetPriceByIndex(uint8 index)
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   884
{
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   885
	if (index > NUM_PRICES) return 0;
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   886
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   887
	return ((Money*)&_price)[index];
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   888
}
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   889
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8624
diff changeset
   890
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2587
diff changeset
   891
Pair SetupSubsidyDecodeParam(const Subsidy* s, bool mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
{
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   893
	TileIndex tile;
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   894
	TileIndex tile2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
	Pair tp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
2272
8b32b48b8579 (svn r2794) Fix a comment and get rid of some parentheses using DeMorgan's law
tron
parents: 2261
diff changeset
   897
	/* if mode is false, use the singular form */
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
   898
	const CargoSpec *cs = GetCargo(s->cargo_type);
7287
88e6b96765d6 (svn r10628) -Fix (r10606,FS#1055): Revert r10606 and fix the plural problem another way.
peter1138
parents: 7266
diff changeset
   899
	SetDParam(0, mode ? cs->name : cs->name_single);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
	if (s->age < 12) {
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   902
		if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL) {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   903
			SetDParam(1, STR_INDUSTRY);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   904
			SetDParam(2, s->from);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   905
			tile = GetIndustry(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   907
			if (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD) {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   908
				SetDParam(4, STR_INDUSTRY);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   909
				SetDParam(5, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   910
				tile2 = GetIndustry(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
			} else {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   912
				SetDParam(4, STR_TOWN);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   913
				SetDParam(5, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   914
				tile2 = GetTown(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
		} else {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   917
			SetDParam(1, STR_TOWN);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   918
			SetDParam(2, s->from);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   919
			tile = GetTown(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   921
			SetDParam(4, STR_TOWN);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   922
			SetDParam(5, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   923
			tile2 = GetTown(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
	} else {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   926
		SetDParam(1, s->from);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   927
		tile = GetStation(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   929
		SetDParam(2, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   930
		tile2 = GetStation(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
	tp.a = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
	tp.b = tile2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
	return tp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   938
5378
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   939
void DeleteSubsidyWithTown(TownID index)
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   940
{
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   941
	Subsidy *s;
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   942
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   943
	for (s = _subsidies; s != endof(_subsidies); s++) {
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   944
		if (s->cargo_type != CT_INVALID && s->age < 12) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   945
			const CargoSpec *cs = GetCargo(s->cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   946
			if (((cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) && (index == s->from || index == s->to)) ||
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   947
				((cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) && index == s->to)) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   948
				s->cargo_type = CT_INVALID;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   949
			}
5378
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   950
		}
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   951
	}
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   952
}
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   953
4330
cf31daa8b321 (svn r6005) -Cleanup: introduce IndustryID and use it
rubidium
parents: 4329
diff changeset
   954
void DeleteSubsidyWithIndustry(IndustryID index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
	Subsidy *s;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   957
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   958
	for (s = _subsidies; s != endof(_subsidies); s++) {
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   959
		if (s->cargo_type != CT_INVALID && s->age < 12) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   960
			const CargoSpec *cs = GetCargo(s->cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   961
			if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL &&
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   962
				(index == s->from || (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD && index == s->to))) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   963
				s->cargo_type = CT_INVALID;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   964
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   966
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
4330
cf31daa8b321 (svn r6005) -Cleanup: introduce IndustryID and use it
rubidium
parents: 4329
diff changeset
   969
void DeleteSubsidyWithStation(StationID index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
	bool dirty = false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   973
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   974
	for (s = _subsidies; s != endof(_subsidies); s++) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   975
		if (s->cargo_type != CT_INVALID && s->age >= 12 &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
				(s->from == index || s->to == index)) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   977
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
			dirty = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   980
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
	if (dirty)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   986
struct FoundRoute {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
	uint distance;
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
   988
	CargoID cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
	void *from;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
	void *to;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   991
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
static void FindSubsidyPassengerRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   995
	Town *from,*to;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10208
diff changeset
   997
	fr->distance = UINT_MAX;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   999
	fr->from = from = GetRandomTown();
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1000
	if (from == NULL || from->population < 400) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1002
	fr->to = to = GetRandomTown();
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1003
	if (from == to || to == NULL || to->population < 400 || to->pct_pass_transported > 42)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
  1006
	fr->distance = DistanceManhattan(from->xy, to->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
static void FindSubsidyCargoRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
	Industry *i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
	int trans, total;
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
  1013
	CargoID cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10208
diff changeset
  1015
	fr->distance = UINT_MAX;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1017
	fr->from = i = GetRandomIndustry();
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1018
	if (i == NULL) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1020
	/* Randomize cargo type */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7926
diff changeset
  1021
	if (HasBit(Random(), 0) && i->produced_cargo[1] != CT_INVALID) {
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7642
diff changeset
  1022
		cargo = i->produced_cargo[1];
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
  1023
		trans = i->last_month_pct_transported[1];
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
  1024
		total = i->last_month_production[1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1025
	} else {
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7642
diff changeset
  1026
		cargo = i->produced_cargo[0];
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
  1027
		trans = i->last_month_pct_transported[0];
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
  1028
		total = i->last_month_production[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1030
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1031
	/* Quit if no production in this industry
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1032
	 * or if the cargo type is passengers
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1033
	 * or if the pct transported is already large enough */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1034
	if (total == 0 || trans > 42 || cargo == CT_INVALID) return;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1035
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1036
	const CargoSpec *cs = GetCargo(cargo);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1037
	if (cs->town_effect == TE_PASSENGERS) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
	fr->cargo = cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1040
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1041
	if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1042
		/*  The destination is a town */
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1043
		Town *t = GetRandomTown();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1044
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1045
		/* Only want big towns */
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1046
		if (t == NULL || t->population < 900) return;
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1047
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
  1048
		fr->distance = DistanceManhattan(i->xy, t->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
		fr->to = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
	} else {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1051
		/* The destination is an industry */
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1052
		Industry *i2 = GetRandomIndustry();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1053
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1054
		/* The industry must accept the cargo */
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7642
diff changeset
  1055
		if (i2 == NULL || i == i2 ||
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7642
diff changeset
  1056
				(cargo != i2->accepts_cargo[0] &&
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7642
diff changeset
  1057
				cargo != i2->accepts_cargo[1] &&
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7642
diff changeset
  1058
				cargo != i2->accepts_cargo[2])) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
			return;
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1060
		}
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
  1061
		fr->distance = DistanceManhattan(i->xy, i2->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
		fr->to = i2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1066
static bool CheckSubsidyDuplicate(Subsidy *s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1067
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2587
diff changeset
  1068
	const Subsidy* ss;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1070
	for (ss = _subsidies; ss != endof(_subsidies); ss++) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1071
		if (s != ss &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1072
				ss->from == s->from &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1073
				ss->to == s->to &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
				ss->cargo_type == s->cargo_type) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1075
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1080
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
  1082
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1083
static void SubsidyMonthlyHandler()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1084
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1085
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
	uint n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
	bool modified = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1092
	for (s = _subsidies; s != endof(_subsidies); s++) {
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1093
		if (s->cargo_type == CT_INVALID) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1094
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
		if (s->age == 12-1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
			pair = SetupSubsidyDecodeParam(s, 1);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9199
diff changeset
  1097
			AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, NS_SUBSIDIES, pair.a, pair.b);
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1098
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1099
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
		} else if (s->age == 2*12-1) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1101
			st = GetStation(s->to);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1102
			if (st->owner == _local_company) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
				pair = SetupSubsidyDecodeParam(s, 1);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9199
diff changeset
  1104
				AddNewsItem(STR_202F_SUBSIDY_WITHDRAWN_SERVICE, NS_SUBSIDIES, pair.a, pair.b);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
			}
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1106
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1108
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1109
			s->age++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1110
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1113
	/* 25% chance to go on */
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7954
diff changeset
  1114
	if (Chance16(1,4)) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1115
		/*  Find a free slot*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
		s = _subsidies;
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1117
		while (s->cargo_type != CT_INVALID) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
			if (++s == endof(_subsidies))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
				goto no_add;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1121
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
		n = 1000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
			FindSubsidyPassengerRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
				s->cargo_type = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
				s->from = ((Town*)fr.from)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
				s->to = ((Town*)fr.to)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
				goto add_subsidy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
			FindSubsidyCargoRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
				s->cargo_type = fr.cargo;
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1134
				s->from = ((Industry*)fr.from)->index;
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1135
				{
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1136
					const CargoSpec *cs = GetCargo(fr.cargo);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1137
					s->to = (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) ? ((Town*)fr.to)->index : ((Industry*)fr.to)->index;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1138
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
	add_subsidy:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
				if (!CheckSubsidyDuplicate(s)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
					s->age = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
					pair = SetupSubsidyDecodeParam(s, 0);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9199
diff changeset
  1143
					AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NS_SUBSIDIES, pair.a, pair.b);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
					modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
			}
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
  1148
		} while (n--);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
no_add:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
	if (modified)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1797
diff changeset
  1155
static const SaveLoad _subsidies_desc[] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1156
	    SLE_VAR(Subsidy, cargo_type, SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1157
	    SLE_VAR(Subsidy, age,        SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1158
	SLE_CONDVAR(Subsidy, from,       SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1159
	SLE_CONDVAR(Subsidy, from,       SLE_UINT16,                5, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1160
	SLE_CONDVAR(Subsidy, to,         SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1161
	SLE_CONDVAR(Subsidy, to,         SLE_UINT16,                5, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1164
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1165
static void Save_SUBS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1166
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1169
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1170
	for (i = 0; i != lengthof(_subsidies); i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1171
		s = &_subsidies[i];
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1172
		if (s->cargo_type != CT_INVALID) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
			SlSetArrayIndex(i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
			SlObject(s, _subsidies_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1179
static void Load_SUBS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
	while ((index = SlIterateArray()) != -1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
		SlObject(&_subsidies[index], _subsidies_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
6954
76ad0c521767 (svn r10209) -Fix/Codechange: forgot a few changes of int32 -> Money in the previous commit
rubidium
parents: 6953
diff changeset
  1186
Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
{
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
  1188
	const CargoSpec *cs = GetCargo(cargo_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
6458
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1190
	/* Use callback to calculate cargo profit, if available */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7926
diff changeset
  1191
	if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) {
6458
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1192
		uint32 var18 = min(dist, 0xFFFF) | (min(num_pieces, 0xFF) << 16) | (transit_days << 24);
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1193
		uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs);
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1194
		if (callback != CALLBACK_FAILED) {
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1195
			int result = GB(callback, 0, 14);
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1196
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1197
			/* Simulate a 15 bit signed value */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7926
diff changeset
  1198
			if (HasBit(callback, 14)) result = 0x4000 - result;
6458
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1199
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1200
			/* "The result should be a signed multiplier that gets multiplied
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1201
			 * by the amount of cargo moved and the price factor, then gets
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1202
			 * divided by 8192." */
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1203
			return result * num_pieces * _cargo_payment_rates[cargo_type] / 8192;
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1204
		}
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1205
	}
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1206
7328
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1207
	/* zero the distance (thus income) if it's the bank and very short transport. */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1208
	if (_settings_game.game_creation.landscape == LT_TEMPERATE && cs->label == 'VALU' && dist < 10) return 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1209
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
7328
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1211
	static const int MIN_TIME_FACTOR = 31;
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1212
	static const int MAX_TIME_FACTOR = 255;
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1213
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1214
	const int days1 = cs->transit_days[0];
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1215
	const int days2 = cs->transit_days[1];
10417
a1a609566853 (svn r14670) -Codechange: use better readable (I hope) and branchless (for some archs/compilers) code for cargo value computation
smatz
parents: 10297
diff changeset
  1216
	const int days_over_days1 = max(   transit_days - days1, 0);
a1a609566853 (svn r14670) -Codechange: use better readable (I hope) and branchless (for some archs/compilers) code for cargo value computation
smatz
parents: 10297
diff changeset
  1217
	const int days_over_days2 = max(days_over_days1 - days2, 0);
7328
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1218
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1219
	/*
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1220
	 * The time factor is calculated based on the time it took
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1221
	 * (transit_days) compared two cargo-depending values. The
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1222
	 * range is divided into three parts:
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1223
	 *
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1224
	 *  - constant for fast transits
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1225
	 *  - linear decreasing with time with a slope of -1 for medium transports
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1226
	 *  - linear decreasing with time with a slope of -2 for slow transports
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1227
	 *
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1228
	 */
10417
a1a609566853 (svn r14670) -Codechange: use better readable (I hope) and branchless (for some archs/compilers) code for cargo value computation
smatz
parents: 10297
diff changeset
  1229
	const int time_factor = max(MAX_TIME_FACTOR - days_over_days1 - days_over_days2, MIN_TIME_FACTOR);
7328
c97074186530 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium
parents: 7287
diff changeset
  1230
7926
01d19beae592 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13
parents: 7922
diff changeset
  1231
	return BigMulS(dist * time_factor * num_pieces, _cargo_payment_rates[cargo_type], 21);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1234
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1235
struct FindIndustryToDeliverData {
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1236
	const Rect *rect;            ///< Station acceptance rectangle
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1237
	CargoID cargo_type;          ///< Cargo type that was delivered
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1238
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1239
	Industry *ind;               ///< Returns found industry
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1240
	const IndustrySpec *indspec; ///< Spec of ind
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1241
	uint cargo_index;            ///< Index of cargo_type in acceptance list of ind
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1242
};
7199
e65a28455ee3 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
rubidium
parents: 7195
diff changeset
  1243
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1244
static bool FindIndustryToDeliver(TileIndex ind_tile, void *user_data)
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1245
{
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1246
	FindIndustryToDeliverData *callback_data = (FindIndustryToDeliverData *)user_data;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1247
	const Rect *rect = callback_data->rect;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1248
	CargoID cargo_type = callback_data->cargo_type;
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1249
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1250
	/* Only process industry tiles */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1251
	if (!IsTileType(ind_tile, MP_INDUSTRY)) return false;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1252
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1253
	/* Only process tiles in the station acceptance rectangle */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1254
	int x = TileX(ind_tile);
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1255
	int y = TileY(ind_tile);
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1256
	if (x < rect->left || x > rect->right || y < rect->top || y > rect->bottom) return false;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1257
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1258
	Industry *ind = GetIndustryByTile(ind_tile);
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1259
	const IndustrySpec *indspec = GetIndustrySpec(ind->type);
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1260
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1261
	uint cargo_index;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1262
	for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1263
		if (cargo_type == ind->accepts_cargo[cargo_index]) break;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1264
	}
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1265
	/* Check if matching cargo has been found */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1266
	if (cargo_index >= lengthof(ind->accepts_cargo)) return false;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1267
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1268
	/* Check if industry temporarly refuses acceptance */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1269
	if (HasBit(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) {
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1270
		uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->xy);
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1271
		if (res == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1274
	/* Found industry accepting the cargo */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1275
	callback_data->ind = ind;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1276
	callback_data->indspec = indspec;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1277
	callback_data->cargo_index = cargo_index;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1278
	return true;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1279
}
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1280
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1281
/**
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1282
 * Transfer goods from station to industry.
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1283
 * All cargo is delivered to the nearest (Manhattan) industry to the station sign, which is inside the acceptance rectangle and actually accepts the cargo.
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1284
 * @param st The station that accepted the cargo
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1285
 * @param cargo_type Type of cargo delivered
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1286
 * @param nun_pieces Amount of cargo delivered
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1287
 */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1288
static void DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, int num_pieces)
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1289
{
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1290
	if (st->rect.IsEmpty()) return;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1292
	/* Compute acceptance rectangle */
10297
e17a18bb827f (svn r14536) -Fix (r14530): Do not expect uints to become negative.
frosch
parents: 10291
diff changeset
  1293
	int catchment_radius = st->GetCatchmentRadius();
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1294
	Rect rect = {
10297
e17a18bb827f (svn r14536) -Fix (r14530): Do not expect uints to become negative.
frosch
parents: 10291
diff changeset
  1295
		max<int>(st->rect.left   - catchment_radius, 0),
e17a18bb827f (svn r14536) -Fix (r14530): Do not expect uints to become negative.
frosch
parents: 10291
diff changeset
  1296
		max<int>(st->rect.top    - catchment_radius, 0),
e17a18bb827f (svn r14536) -Fix (r14530): Do not expect uints to become negative.
frosch
parents: 10291
diff changeset
  1297
		min<int>(st->rect.right  + catchment_radius, MapMaxX()),
e17a18bb827f (svn r14536) -Fix (r14530): Do not expect uints to become negative.
frosch
parents: 10291
diff changeset
  1298
		min<int>(st->rect.bottom + catchment_radius, MapMaxY())
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1299
	};
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1300
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1301
	/* Compute maximum extent of acceptance rectangle wrt. station sign */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1302
	TileIndex start_tile = st->xy;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1303
	uint max_radius = max(
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1304
		max(DistanceManhattan(start_tile, TileXY(rect.left , rect.top)), DistanceManhattan(start_tile, TileXY(rect.left , rect.bottom))),
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1305
		max(DistanceManhattan(start_tile, TileXY(rect.right, rect.top)), DistanceManhattan(start_tile, TileXY(rect.right, rect.bottom)))
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1306
	);
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1307
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1308
	FindIndustryToDeliverData callback_data;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1309
	callback_data.rect = &rect;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1310
	callback_data.cargo_type = cargo_type;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1311
	callback_data.ind = NULL;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1312
	callback_data.indspec = NULL;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1313
	callback_data.cargo_index = 0;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1314
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1315
	/* Find the nearest industrytile to the station sign inside the catchment area, whose industry accepts the cargo.
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1316
	 * This fails in three cases:
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1317
	 *  1) The station accepts the cargo because there are enough houses around it accepting the cargo.
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1318
	 *  2) The industries in the catchment area temporarily reject the cargo, and the daily station loop has not yet updated station acceptance.
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1319
	 *  3) The results of callbacks CBID_INDUSTRY_REFUSE_CARGO and CBID_INDTILE_CARGO_ACCEPTANCE are inconsistent. (documented behaviour)
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1320
	 */
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1321
	if (CircularTileSearch(&start_tile, 2 * max_radius + 1, FindIndustryToDeliver, &callback_data)) {
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1322
		Industry *best = callback_data.ind;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1323
		const IndustrySpec *indspec = callback_data.indspec;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1324
		uint accepted_cargo_index = callback_data.cargo_index;
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1325
		assert(best != NULL && indspec != NULL);
7165
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7124
diff changeset
  1326
		uint16 callback = indspec->callback_flags;
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
  1327
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1328
		best->was_cargo_delivered = true;
7186
818eba1f20d5 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7168
diff changeset
  1329
		best->last_cargo_accepted_at = _date;
7165
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7124
diff changeset
  1330
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7926
diff changeset
  1331
		if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
7165
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7124
diff changeset
  1332
			best->incoming_cargo_waiting[accepted_cargo_index] = min(num_pieces + best->incoming_cargo_waiting[accepted_cargo_index], 0xFFFF);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7926
diff changeset
  1333
			if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) {
7195
8c728b5fef43 (svn r10473) -Codechange: make the industry "window", the one that shows when you click on an industry, more flexible to allow easier integration with newindustries.
rubidium
parents: 7186
diff changeset
  1334
				IndustryProductionCallback(best, 0);
8c728b5fef43 (svn r10473) -Codechange: make the industry "window", the one that shows when you click on an industry, more flexible to allow easier integration with newindustries.
rubidium
parents: 7186
diff changeset
  1335
			} else {
8c728b5fef43 (svn r10473) -Codechange: make the industry "window", the one that shows when you click on an industry, more flexible to allow easier integration with newindustries.
rubidium
parents: 7186
diff changeset
  1336
				InvalidateWindow(WC_INDUSTRY_VIEW, best->index);
8c728b5fef43 (svn r10473) -Codechange: make the industry "window", the one that shows when you click on an industry, more flexible to allow easier integration with newindustries.
rubidium
parents: 7186
diff changeset
  1337
			}
7165
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7124
diff changeset
  1338
		} else {
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7124
diff changeset
  1339
			best->produced_cargo_waiting[0] = min(best->produced_cargo_waiting[0] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][0] / 256), 0xFFFF);
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7124
diff changeset
  1340
			best->produced_cargo_waiting[1] = min(best->produced_cargo_waiting[1] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][1] / 256), 0xFFFF);
fb78336b205f (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7124
diff changeset
  1341
		}
7229
a5f262f6df1b (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7199
diff changeset
  1342
7860
5cdd0d914013 (svn r11410) -Codechange: implement random triggers for industries.
rubidium
parents: 7832
diff changeset
  1343
		TriggerIndustry(best, INDUSTRY_TRIGGER_RECEIVED_CARGO);
7229
a5f262f6df1b (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7199
diff changeset
  1344
		StartStopIndustryTileAnimation(best, IAT_INDUSTRY_RECEIVED_CARGO);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1346
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1347
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
  1348
static bool CheckSubsidised(Station *from, Station *to, CargoID cargo_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1349
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
	TileIndex xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1354
	/* check if there is an already existing subsidy that applies to us */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1355
	for (s = _subsidies; s != endof(_subsidies); s++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1356
		if (s->cargo_type == cargo_type &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1357
				s->age >= 12 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
				s->from == from->index &&
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1359
				s->to == to->index) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1360
			return true;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1361
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1363
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1364
	/* check if there's a new subsidy that applies.. */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1365
	for (s = _subsidies; s != endof(_subsidies); s++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1366
		if (s->cargo_type == cargo_type && s->age < 12) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1367
			/* Check distance from source */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1368
			const CargoSpec *cs = GetCargo(cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1369
			if (cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1370
				xy = GetTown(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1371
			} else {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1372
				xy = (GetIndustry(s->from))->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
			}
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1374
			if (DistanceMax(xy, from->xy) > 9) continue;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1375
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
			/* Check distance from dest */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1377
			switch (cs->town_effect) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1378
				case TE_PASSENGERS:
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1379
				case TE_MAIL:
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1380
				case TE_GOODS:
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1381
				case TE_FOOD:
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1382
					xy = GetTown(s->to)->xy;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1383
					break;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1384
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1385
				default:
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1386
					xy = GetIndustry(s->to)->xy;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1387
					break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
			}
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1389
			if (DistanceMax(xy, to->xy) > 9) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1390
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1391
			/* Found a subsidy, change the values to indicate that it's in use */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1392
			s->age = 12;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1393
			s->from = from->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1394
			s->to = to->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1395
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1396
			/* Add a news item */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1397
			pair = SetupSubsidyDecodeParam(s, 0);
7124
8ef9caaddce5 (svn r10397) -Fix [FS#969]: subsidy awarded messages broken due to {COMPANY} now taking 1 (not 2) parameters. also remove an unnecessary dereference.
peter1138
parents: 7067
diff changeset
  1398
			InjectDParam(1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1399
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1400
			SetDParam(0, _current_company);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1401
			AddNewsItem(
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1402
				STR_2031_SERVICE_SUBSIDY_AWARDED + _settings_game.difficulty.subsidy_multiplier,
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9199
diff changeset
  1403
				NS_SUBSIDIES,
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1404
				pair.a, pair.b
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1405
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1406
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1407
			InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1408
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1409
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1412
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1413
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
  1414
static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID source, StationID dest, TileIndex source_tile, byte days_in_transit)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1416
	bool subsidised;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1417
	Station *s_from, *s_to;
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
  1418
	Money profit;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1419
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
  1420
	assert(num_pieces > 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1422
	/* Update company statistics */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1423
	{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1424
		Company *c = GetCompany(_current_company);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1425
		c->cur_economy.delivered_cargo += num_pieces;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1426
		SetBit(c->cargo_types, cargo_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1429
	/* Get station pointers. */
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1430
	s_from = GetStation(source);
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1431
	s_to = GetStation(dest);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1432
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1433
	/* Check if a subsidy applies. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
	subsidised = CheckSubsidised(s_from, s_to, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1436
	/* Increase town's counter for some special goods types */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1437
	const CargoSpec *cs = GetCargo(cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1438
	if (cs->town_effect == TE_FOOD) s_to->town->new_act_food += num_pieces;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1439
	if (cs->town_effect == TE_WATER) s_to->town->new_act_water += num_pieces;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1440
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1441
	/* Give the goods to the industry. */
10291
2c8bf5a13a1b (svn r14530) -Fix [FS#2138]: Do not deliver cargo to industries not inside station catchment area.
frosch
parents: 10236
diff changeset
  1442
	DeliverGoodsToIndustry(s_to, cargo_type, num_pieces);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1443
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1444
	/* Determine profit */
5683
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1445
	profit = GetTransportedGoodsIncome(num_pieces, DistanceManhattan(source_tile, s_to->xy), days_in_transit, cargo_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1446
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1447
	/* Modify profit if a subsidy is in effect */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
	if (subsidised) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1449
		switch (_settings_game.difficulty.subsidy_multiplier) {
3655
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1450
			case 0:  profit += profit >> 1; break;
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1451
			case 1:  profit *= 2; break;
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1452
			case 2:  profit *= 3; break;
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1453
			default: profit *= 4; break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1454
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1455
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1456
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1457
	return profit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1458
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1459
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1460
/**
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1461
 * Performs the vehicle payment _and_ marks the vehicle to be unloaded.
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1462
 * @param front_v the vehicle to be unloaded
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1463
 */
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1464
void VehiclePayment(Vehicle *front_v)
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1465
{
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1466
	int result = 0;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1467
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1468
	Money vehicle_profit = 0; // Money paid to the train
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1469
	Money route_profit   = 0; // The grand total amount for the route. A-D of transfer chain A-B-C-D
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1470
	Money virtual_profit = 0; // The virtual profit for entire vehicle chain
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1471
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1472
	StationID last_visited = front_v->last_station_visited;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1473
	Station *st = GetStation(last_visited);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1474
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1475
	/* The owner of the train wants to be paid */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1476
	CompanyID old_company = _current_company;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1477
	_current_company = front_v->owner;
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1478
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1479
	/* At this moment loading cannot be finished */
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1480
	ClrBit(front_v->vehicle_flags, VF_LOADING_FINISHED);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1481
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1482
	/* Start unloading in at the first possible moment */
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1483
	front_v->load_unload_time_rem = 1;
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1484
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7474
diff changeset
  1485
	for (Vehicle *v = front_v; v != NULL; v = v->Next()) {
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1486
		/* No cargo to unload */
8883
baee554a9449 (svn r12650) -Feature: ability to force a vehicle to not load at a station.
rubidium
parents: 8873
diff changeset
  1487
		if (v->cargo_cap == 0 || v->cargo.Empty() || front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) continue;
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1488
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1489
		/* All cargo has already been paid for, no need to pay again */
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1490
		if (!v->cargo.UnpaidCargo()) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1491
			SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
6882
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1492
			continue;
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1493
		}
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1494
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1495
		GoodsEntry *ge = &st->goods[v->cargo_type];
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1496
		const CargoList::List *cargos = v->cargo.Packets();
6882
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1497
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1498
		for (CargoList::List::const_iterator it = cargos->begin(); it != cargos->end(); it++) {
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1499
			CargoPacket *cp = *it;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1500
			if (!cp->paid_for &&
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1501
					cp->source != last_visited &&
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7926
diff changeset
  1502
					HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) &&
8855
6d841d5d4789 (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents: 8843
diff changeset
  1503
					(front_v->current_order.GetUnloadType() & OUFB_TRANSFER) == 0) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1504
				/* Deliver goods to the station */
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1505
				st->time_since_unload = 0;
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1506
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1507
				/* handle end of route payment */
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1508
				Money profit = DeliverGoods(cp->count, v->cargo_type, cp->source, last_visited, cp->source_xy, cp->days_in_transit);
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1509
				cp->paid_for = true;
7436
2ef100150423 (svn r10857) -Fix: feeder-income was broken; substract the feeder-costs from the vehicle-income, not from the route-income (tnx to RichK for all the info)
truelight
parents: 7398
diff changeset
  1510
				route_profit   += profit; // display amount paid for final route delivery, A-D of a chain A-B-C-D
2ef100150423 (svn r10857) -Fix: feeder-income was broken; substract the feeder-costs from the vehicle-income, not from the route-income (tnx to RichK for all the info)
truelight
parents: 7398
diff changeset
  1511
				vehicle_profit += profit - cp->feeder_share;                    // whole vehicle is not payed for transfers picked up earlier
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1512
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1513
				result |= 1;
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1514
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1515
				SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
8855
6d841d5d4789 (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents: 8843
diff changeset
  1516
			} else if (front_v->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) {
6d841d5d4789 (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents: 8843
diff changeset
  1517
				if (!cp->paid_for && (front_v->current_order.GetUnloadType() & OUFB_TRANSFER) != 0) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1518
					Money profit = GetTransportedGoodsIncome(
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1519
						cp->count,
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1520
						/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1521
						DistanceManhattan(cp->loaded_at_xy, GetStation(last_visited)->xy),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1522
						cp->days_in_transit,
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1523
						v->cargo_type);
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1524
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8424
diff changeset
  1525
					front_v->profit_this_year += profit << 8;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1526
					virtual_profit   += profit; // accumulate transfer profits for whole vehicle
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1527
					cp->feeder_share += profit; // account for the (virtual) profit already made for the cargo packet
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1528
					cp->paid_for      = true;   // record that the cargo has been paid for to eliminate double counting
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1529
				}
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1530
				result |= 2;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1531
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1532
				SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1533
			}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1534
		}
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1535
		v->cargo.InvalidateCache();
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1536
	}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1537
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1538
	if (virtual_profit > 0) {
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1539
		ShowFeederIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, virtual_profit);
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1540
	}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1541
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1542
	if (route_profit != 0) {
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8424
diff changeset
  1543
		front_v->profit_this_year += vehicle_profit << 8;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1544
		SubtractMoneyFromCompany(CommandCost(front_v->GetExpenseType(true), -route_profit));
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1545
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1546
		if (IsLocalCompany() && !PlayVehicleSound(front_v, VSE_LOAD_UNLOAD)) {
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1547
			SndPlayVehicleFx(SND_14_CASHTILL, front_v);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1548
		}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1549
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1550
		ShowCostOrIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, -vehicle_profit);
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1551
	}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1552
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1553
	_current_company = old_company;
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1554
}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1555
6580
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1556
/**
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1557
 * Loads/unload the vehicle if possible.
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1558
 * @param v the vehicle to be (un)loaded
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1559
 * @param cargo_left the amount of each cargo type that is
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1560
 *                   virtually left on the platform to be
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1561
 *                   picked up by another vehicle when all
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1562
 *                   previous vehicles have loaded.
6580
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1563
 */
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1564
static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
{
8837
72125f871eb4 (svn r12585) -Fix (r12584): assumption that assertions were enabled during compilation/testing was flawed.
rubidium
parents: 8814
diff changeset
  1566
	assert(v->current_order.IsType(OT_LOADING));
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1567
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1568
	/* We have not waited enough time till the next round of loading/unloading */
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1569
	if (--v->load_unload_time_rem != 0) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1570
		if (_settings_game.order.improved_load && (v->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1571
			/* 'Reserve' this cargo for this vehicle, because we were first. */
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7474
diff changeset
  1572
			for (; v != NULL; v = v->Next()) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1573
				if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo.Count();
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1574
			}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1575
		}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1576
		return;
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1577
	}
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1578
8231
d98af7a3fecb (svn r11794) -Fix [FS#1632]: reversing a train when loading at a station with an adjacent station in the same axis crashed.
glx
parents: 8230
diff changeset
  1579
	StationID last_visited = v->last_station_visited;
d98af7a3fecb (svn r11794) -Fix [FS#1632]: reversing a train when loading at a station with an adjacent station in the same axis crashed.
glx
parents: 8230
diff changeset
  1580
	Station *st = GetStation(last_visited);
d98af7a3fecb (svn r11794) -Fix [FS#1632]: reversing a train when loading at a station with an adjacent station in the same axis crashed.
glx
parents: 8230
diff changeset
  1581
d98af7a3fecb (svn r11794) -Fix [FS#1632]: reversing a train when loading at a station with an adjacent station in the same axis crashed.
glx
parents: 8230
diff changeset
  1582
	if (v->type == VEH_TRAIN && (!IsTileType(v->tile, MP_STATION) || GetStationIndex(v->tile) != st->index)) {
6844
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1583
		/* The train reversed in the station. Take the "easy" way
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1584
		 * out and let the train just leave as it always did. */
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1585
		SetBit(v->vehicle_flags, VF_LOADING_FINISHED);
6844
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1586
		return;
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1587
	}
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1588
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1589
	int unloading_time = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
	Vehicle *u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1591
	int result = 0;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1592
	uint cap;
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1593
8241
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1594
	bool completely_emptied = true;
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1595
	bool anything_unloaded = false;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1596
	bool anything_loaded   = false;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1597
	uint32 cargo_not_full  = 0;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1598
	uint32 cargo_full      = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1599
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1600
	v->cur_speed = 0;
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1601
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7474
diff changeset
  1602
	for (; v != NULL; v = v->Next()) {
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1603
		if (v->cargo_cap == 0) continue;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1604
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1605
		byte load_amount = EngInfo(v->engine_type)->load_amount;
10419
d9a1fa739477 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch
parents: 10417
diff changeset
  1606
d9a1fa739477 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch
parents: 10417
diff changeset
  1607
		/* The default loadamount for mail is 1/4 of the load amount for passengers */
d9a1fa739477 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch
parents: 10417
diff changeset
  1608
		if (v->type == VEH_AIRCRAFT && !IsNormalAircraft(v)) load_amount = (load_amount + 3) / 4;
d9a1fa739477 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch
parents: 10417
diff changeset
  1609
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1610
		if (_settings_game.order.gradual_loading && HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_LOAD_AMOUNT)) {
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1611
			uint16 cb_load_amount = GetVehicleCallback(CBID_VEHICLE_LOAD_AMOUNT, 0, 0, v->engine_type, v);
9020
8174bbda9abe (svn r12819) -Codechange: handle more NewGRFs in the same way as TTDP does it, i.e. testing the low bits for 0xFF or 0 instead of all bits.
rubidium
parents: 9003
diff changeset
  1612
			if (cb_load_amount != CALLBACK_FAILED && GB(cb_load_amount, 0, 8) != 0) load_amount = GB(cb_load_amount, 0, 8);
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1613
		}
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1614
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1615
		GoodsEntry *ge = &st->goods[v->cargo_type];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1616
8883
baee554a9449 (svn r12650) -Feature: ability to force a vehicle to not load at a station.
rubidium
parents: 8873
diff changeset
  1617
		if (HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) && (u->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1618
			uint cargo_count = v->cargo.Count();
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1619
			uint amount_unloaded = _settings_game.order.gradual_loading ? min(cargo_count, load_amount) : cargo_count;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1620
			bool remaining; // Are there cargo entities in this vehicle that can still be unloaded here?
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1621
8855
6d841d5d4789 (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents: 8843
diff changeset
  1622
			if (HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) && !(u->current_order.GetUnloadType() & OUFB_TRANSFER)) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1623
				/* The cargo has reached it's final destination, the packets may now be destroyed */
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1624
				remaining = v->cargo.MoveTo(NULL, amount_unloaded, CargoList::MTA_FINAL_DELIVERY, last_visited);
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1625
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
				result |= 1;
8855
6d841d5d4789 (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents: 8843
diff changeset
  1627
			} else if (u->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1628
				remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded);
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1629
				SetBit(ge->acceptance_pickup, GoodsEntry::PICKUP);
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1630
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1631
				result |= 2;
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1632
			} else {
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1633
				/* The order changed while unloading (unset unload/transfer) or the
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1634
				 * station does not accept goods anymore. */
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1635
				ClrBit(v->vehicle_flags, VF_CARGO_UNLOADING);
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1636
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1637
			}
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1638
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1639
			/* Deliver goods to the station */
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1640
			st->time_since_unload = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1641
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1642
			unloading_time += amount_unloaded;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1643
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1644
			anything_unloaded = true;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1645
			if (_settings_game.order.gradual_loading && remaining) {
8241
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1646
				completely_emptied = false;
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1647
			} else {
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1648
				/* We have finished unloading (cargo count == 0) */
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1649
				ClrBit(v->vehicle_flags, VF_CARGO_UNLOADING);
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1650
			}
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1651
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1652
			continue;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1653
		}
5888
6fb161a15a3c (svn r8501) -Fix (r7377) [FS#539]: Keep track of how much cargo has been paid for, so that cargo cannot be paid for more than once.
maedhros
parents: 5854
diff changeset
  1654
8885
647cda0e9312 (svn r12652) -Codechange: rework the order GUI a little more to make it a little more clear that 'transfer' is just an unload type.
rubidium
parents: 8883
diff changeset
  1655
		/* Do not pick up goods when we have no-load set. */
647cda0e9312 (svn r12652) -Codechange: rework the order GUI a little more to make it a little more clear that 'transfer' is just an unload type.
rubidium
parents: 8883
diff changeset
  1656
		if (u->current_order.GetLoadType() & OLFB_NO_LOAD) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1657
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1658
		/* update stats */
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1659
		int t;
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1660
		switch (u->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1661
			case VEH_TRAIN: t = u->u.rail.cached_max_speed; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1662
			case VEH_ROAD:  t = u->max_speed / 2;           break;
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1663
			default:        t = u->max_speed;               break;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1664
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1665
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1666
		/* if last speed is 0, we treat that as if no vehicle has ever visited the station. */
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1667
		ge->last_speed = min(t, 255);
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
  1668
		ge->last_age = _cur_year - u->build_year;
7458
5ffdb894e08c (svn r10953) -Fix [FS#1139, FS#1143]: stations would not get a rating when there were already two stations with a "more than default" rating as no cargo would be moved to the station to be picked up.
rubidium
parents: 7447
diff changeset
  1669
		ge->days_since_pickup = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1670
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1671
		/* If there's goods waiting at the station, and the vehicle
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1672
		 * has capacity for it, load it on the vehicle. */
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1673
		if (!ge->cargo.Empty() &&
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1674
				(cap = v->cargo_cap - v->cargo.Count()) != 0) {
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1675
			uint count = ge->cargo.Count();
1935
164d58b9137c (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1905
diff changeset
  1676
523
8871819c9afd (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1677
			/* Skip loading this vehicle if another train/vehicle is already handling
8871819c9afd (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1678
			 * the same cargo type at this station */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1679
			if (_settings_game.order.improved_load && cargo_left[v->cargo_type] <= 0) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1680
				SetBit(cargo_not_full, v->cargo_type);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1681
				continue;
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1682
			}
523
8871819c9afd (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1683
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1684
			if (cap > count) cap = count;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1685
			if (_settings_game.order.gradual_loading) cap = min(cap, load_amount);
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1686
			if (_settings_game.order.improved_load) {
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1687
				/* Don't load stuff that is already 'reserved' for other vehicles */
7023
7ecc645ba712 (svn r10287) -Fix (r10266): some surprises from that (almost always) complaining compiler...
rubidium
parents: 7014
diff changeset
  1688
				cap = min((uint)cargo_left[v->cargo_type], cap);
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1689
				cargo_left[v->cargo_type] -= cap;
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1690
			}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1691
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1692
			if (v->cargo.Empty()) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1693
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1694
			/* TODO: Regarding this, when we do gradual loading, we
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1695
			 * should first unload all vehicles and then start
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1696
			 * loading them. Since this will cause
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1697
			 * VEHICLE_TRIGGER_EMPTY to be called at the time when
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1698
			 * the whole vehicle chain is really totally empty, the
8241
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1699
			 * completely_emptied assignment can then be safely
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1700
			 * removed; that's how TTDPatch behaves too. --pasky */
8241
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1701
			completely_emptied = false;
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1702
			anything_loaded = true;
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1703
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1704
			ge->cargo.MoveTo(&v->cargo, cap, CargoList::MTA_CARGO_LOAD, st->xy);
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1705
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1706
			st->time_since_load = 0;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1707
			st->last_vehicle_type = v->type;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1708
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8885
diff changeset
  1709
			StationAnimationTrigger(st, st->xy, STAT_ANIM_CARGO_TAKEN, v->cargo_type);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8885
diff changeset
  1710
7474
1daa825ba893 (svn r10981) -Fix [FS#1156] (r10970): stations gave ratings for accepted cargo too.
rubidium
parents: 7458
diff changeset
  1711
			unloading_time += cap;
1daa825ba893 (svn r10981) -Fix [FS#1156] (r10970): stations gave ratings for accepted cargo too.
rubidium
parents: 7458
diff changeset
  1712
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1713
			result |= 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1714
		}
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1715
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1716
		if (v->cargo.Count() == v->cargo_cap) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1717
			SetBit(cargo_full, v->cargo_type);
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1718
		} else {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
  1719
			SetBit(cargo_not_full, v->cargo_type);
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1720
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1722
8241
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1723
	/* Only set completly_emptied, if we just unloaded all remaining cargo */
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1724
	completely_emptied &= anything_unloaded;
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1725
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1726
	/* We update these variables here, so gradual loading still fills
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1727
	 * all wagons at the same time instead of using the same 'improved'
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1728
	 * loading algorithm for the wagons (only fill wagon when there is
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1729
	 * enough to fill the previous wagons) */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1730
	if (_settings_game.order.improved_load && (u->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1731
		/* Update left cargo */
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7474
diff changeset
  1732
		for (v = u; v != NULL; v = v->Next()) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1733
			if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo.Count();
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1734
		}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1735
	}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1736
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1737
	v = u;
1935
164d58b9137c (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1905
diff changeset
  1738
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1739
	if (anything_loaded || anything_unloaded) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1740
		if (_settings_game.order.gradual_loading) {
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1741
			/* The time it takes to load one 'slice' of cargo or passengers depends
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1742
			* on the vehicle type - the values here are those found in TTDPatch */
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1743
			const uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1744
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1745
			unloading_time = gradual_loading_wait_time[v->type];
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1746
		}
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1747
	} else {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1748
		bool finished_loading = true;
8855
6d841d5d4789 (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents: 8843
diff changeset
  1749
		if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) {
8873
d66baf98e3eb (svn r12640) -Codechange: let GetLoadType make a difference between full load and full load any based on the patch setting instead of using the patch setting directly.
rubidium
parents: 8855
diff changeset
  1750
			if (v->current_order.GetLoadType() == OLF_FULL_LOAD_ANY) {
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1751
				/* if the aircraft carries passengers and is NOT full, then
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1752
				 * continue loading, no matter how much mail is in */
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1753
				if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap != v->cargo.Count()) ||
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1754
						(cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are stull non-full cargos
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1755
					finished_loading = false;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1756
				}
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1757
			} else if (cargo_not_full != 0) {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1758
				finished_loading = false;
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1759
			}
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1760
		}
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1761
		unloading_time = 20;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1762
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1763
		SB(v->vehicle_flags, VF_LOADING_FINISHED, 1, finished_loading);
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1764
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1766
	if (v->type == VEH_TRAIN) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1767
		/* Each platform tile is worth 2 rail vehicles. */
5998
2bfbade143ac (svn r8709) -Fix/Codechange: Rename the function GetStationPlatforms into GetPlatformLength because that is what it really does. Overload it because there is already a GetPlatformLength (one gives the length of the whole platform, the other gives the remaining length in a given direction). Turned both functions into methods of Station. While messing around with it, fix a problem where loading times for overhanging trains are miscomputed.
celestar
parents: 5955
diff changeset
  1768
		int overhang = v->u.rail.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
2587
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2548
diff changeset
  1769
		if (overhang > 0) {
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2548
diff changeset
  1770
			unloading_time <<= 1;
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2548
diff changeset
  1771
			unloading_time += (overhang * unloading_time) / 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1773
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1774
7543
e55bda077bea (svn r11063) -Codechange: make it possible for people to view the loading indicators of everyone. Patch by SmatZ.
rubidium
parents: 7520
diff changeset
  1775
	/* Calculate the loading indicator fill percent and display
e55bda077bea (svn r11063) -Codechange: make it possible for people to view the loading indicators of everyone. Patch by SmatZ.
rubidium
parents: 7520
diff changeset
  1776
	 * In the Game Menu do not display indicators
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1777
	 * If _settings_client.gui.loading_indicators == 2, show indicators (bool can be promoted to int as 0 or 1 - results in 2 > 0,1 )
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1778
	 * if _settings_client.gui.loading_indicators == 1, _local_company must be the owner or must be a spectator to show ind., so 1 > 0
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9359
diff changeset
  1779
	 * if _settings_client.gui.loading_indicators == 0, do not display indicators ... 0 is never greater than anything
7543
e55bda077bea (svn r11063) -Codechange: make it possible for people to view the loading indicators of everyone. Patch by SmatZ.
rubidium
parents: 7520
diff changeset
  1780
	 */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1781
	if (_game_mode != GM_MENU && (_settings_client.gui.loading_indicators > (uint)(v->owner != _local_company && _local_company != COMPANY_SPECTATOR))) {
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1782
		StringID percent_up_down = STR_NULL;
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1783
		int percent = CalcPercentVehicleFilled(v, &percent_up_down);
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6991
diff changeset
  1784
		if (v->fill_percent_te_id == INVALID_TE_ID) {
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1785
			v->fill_percent_te_id = ShowFillingPercent(v->x_pos, v->y_pos, v->z_pos + 20, percent, percent_up_down);
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6991
diff changeset
  1786
		} else {
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1787
			UpdateFillingPercent(v->fill_percent_te_id, percent, percent_up_down);
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6991
diff changeset
  1788
		}
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6991
diff changeset
  1789
	}
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6991
diff changeset
  1790
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
	v->load_unload_time_rem = unloading_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
8241
b5c1a0a74838 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch
parents: 8238
diff changeset
  1793
	if (completely_emptied) {
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1794
		TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1795
	}
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1796
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
	if (result != 0) {
9043
eb4f06b99146 (svn r12862) -Codechange: reduce code duplication for VehicleType -> WindowClass lookup
smatz
parents: 9020
diff changeset
  1798
		InvalidateWindow(GetWindowClassForVehicleType(v->type), v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1800
6823
1f2075619543 (svn r10062) -Codechange: Don't redraw all station tiles when cargo is added or removed if the station has no custom graphics.
peter1138
parents: 6819
diff changeset
  1801
		st->MarkTilesDirty(true);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1802
		v->MarkDirty();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1804
		if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1805
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1806
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1808
/**
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1809
 * Load/unload the vehicles in this station according to the order
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1810
 * they entered.
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1811
 * @param st the station to do the loading/unloading for
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1812
 */
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1813
void LoadUnloadStation(Station *st)
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1814
{
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1815
	int cargo_left[NUM_CARGO];
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1816
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7002
diff changeset
  1817
	for (uint i = 0; i < NUM_CARGO; i++) cargo_left[i] = st->goods[i].cargo.Count();
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1818
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1819
	std::list<Vehicle *>::iterator iter;
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1820
	for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1821
		Vehicle *v = *iter;
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1822
		if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left);
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1823
	}
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1824
}
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1825
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1826
void CompaniesMonthlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1828
	CompaniesGenStatistics();
9717
b7806a165a75 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138
parents: 9659
diff changeset
  1829
	if (_settings_game.economy.inflation) AddInflation();
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1830
	CompaniesPayInterest();
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1831
	/* Reset the _current_company flag */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1832
	_current_company = OWNER_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1833
	HandleEconomyFluctuations();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1834
	SubsidyMonthlyHandler();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1835
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1836
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1837
static void DoAcquireCompany(Company *c)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1838
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1839
	Company *owner;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1840
	int i;
6953
230d1e3ac86c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 6952
diff changeset
  1841
	Money value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1842
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  1843
	CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1844
	cni->FillData(c, GetCompany(_current_company));
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  1845
9199
ce9fbdff6581 (svn r13065) -Codechange: remove the need for the news string callbacks. Patch by Cirdan.
rubidium
parents: 9158
diff changeset
  1846
	SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1847
	SetDParam(1, c->bankrupt_value == 0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR);
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  1848
	SetDParamStr(2, cni->company_name);
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  1849
	SetDParamStr(3, cni->other_company_name);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1850
	SetDParam(4, c->bankrupt_value);
9658
f5c4d3c04b5d (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium
parents: 9652
diff changeset
  1851
	AddNewsItem(STR_02B6, NS_COMPANY_MERGER, 0, 0, cni);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1852
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1853
	/* original code does this a little bit differently */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1854
	CompanyID ci = c->index;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1855
	ChangeNetworkOwner(ci, _current_company);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1856
	ChangeOwnershipOfCompanyItems(ci, _current_company);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1858
	if (c->bankrupt_value == 0) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1859
		owner = GetCompany(_current_company);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1860
		owner->current_loan += c->current_loan;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1861
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1863
	value = CalculateCompanyValue(c) >> 2;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1864
	CompanyID old_company = _current_company;
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1865
	for (i = 0; i != 4; i++) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1866
		if (c->share_owners[i] != COMPANY_SPECTATOR) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1867
			_current_company = c->share_owners[i];
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1868
			SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -value));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1869
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1870
	}
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1871
	_current_company = old_company;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1873
	DeleteCompanyWindows(ci);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9281
diff changeset
  1874
	InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9281
diff changeset
  1875
	InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9281
diff changeset
  1876
	InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9281
diff changeset
  1877
	InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0);
9659
187142ff9b6c (svn r13731) -Codechange: make a pool of the array of players.
rubidium
parents: 9658
diff changeset
  1878
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1879
	delete c;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1880
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1881
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1882
extern int GetAmountOwnedBy(const Company *c, Owner owner);
599
30da691a5561 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1883
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1884
/** Acquire shares in an opposing company.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
  1885
 * @param tile unused
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6405
diff changeset
  1886
 * @param flags type of operation
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1887
 * @param p1 company to buy the shares from
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1888
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1889
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
  1890
CommandCost CmdBuyShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1891
{
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
  1892
	CommandCost cost(EXPENSES_OTHER);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1893
7503
3f0b9663c72e (svn r11018) -Fix [FS#1169]: Disallow buying/selling shares in your own company or a bankrupted/non-existant company.
rubidium
parents: 7492
diff changeset
  1894
	/* Check if buying shares is allowed (protection against modified clients) */
3f0b9663c72e (svn r11018) -Fix [FS#1169]: Disallow buying/selling shares in your own company or a bankrupted/non-existant company.
rubidium
parents: 7492
diff changeset
  1895
	/* Cannot buy own shares */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1896
	if (!IsValidCompanyID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR;
7503
3f0b9663c72e (svn r11018) -Fix [FS#1169]: Disallow buying/selling shares in your own company or a bankrupted/non-existant company.
rubidium
parents: 7492
diff changeset
  1897
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1898
	Company *c = GetCompany((CompanyID)p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1900
	/* Protect new companies from hostile takeovers */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1901
	if (_cur_year - c->inaugurated_year < 6) return_cmd_error(STR_PROTECTED);
930
ab42e283749d (svn r1418) -Feature: [1098254] (dis)Allow Shares. Add patch options to allow buying/selling of shares (Hackykid)
darkvater
parents: 919
diff changeset
  1902
599
30da691a5561 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1903
	/* Those lines are here for network-protection (clients can be slow) */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1904
	if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 0) return cost;
930
ab42e283749d (svn r1418) -Feature: [1098254] (dis)Allow Shares. Add patch options to allow buying/selling of shares (Hackykid)
darkvater
parents: 919
diff changeset
  1905
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1906
	/* We can not buy out a real company (temporarily). TODO: well, enable it obviously */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1907
	if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 1 && !c->is_ai) return cost;
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
  1908
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1909
	cost.AddCost(CalculateCompanyValue(c) >> 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1910
	if (flags & DC_EXEC) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1911
		OwnerByte *b = c->share_owners;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1912
		int i;
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1913
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1914
		while (*b != COMPANY_SPECTATOR) b++; /* share owners is guaranteed to contain at least one COMPANY_SPECTATOR */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1915
		*b = _current_company;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1916
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1917
		for (i = 0; c->share_owners[i] == _current_company;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1918
			if (++i == 4) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1919
				c->bankrupt_value = 0;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1920
				DoAcquireCompany(c);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1921
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1922
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1923
		}
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1924
		InvalidateWindow(WC_COMPANY, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1926
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1927
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1928
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1929
/** Sell shares in an opposing company.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
  1930
 * @param tile unused
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6405
diff changeset
  1931
 * @param flags type of operation
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1932
 * @param p1 company to sell the shares from
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1933
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1934
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
  1935
CommandCost CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1936
{
7503
3f0b9663c72e (svn r11018) -Fix [FS#1169]: Disallow buying/selling shares in your own company or a bankrupted/non-existant company.
rubidium
parents: 7492
diff changeset
  1937
	/* Check if selling shares is allowed (protection against modified clients) */
3f0b9663c72e (svn r11018) -Fix [FS#1169]: Disallow buying/selling shares in your own company or a bankrupted/non-existant company.
rubidium
parents: 7492
diff changeset
  1938
	/* Cannot sell own shares */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1939
	if (!IsValidCompanyID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR;
7503
3f0b9663c72e (svn r11018) -Fix [FS#1169]: Disallow buying/selling shares in your own company or a bankrupted/non-existant company.
rubidium
parents: 7492
diff changeset
  1940
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1941
	Company *c = GetCompany((CompanyID)p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1942
653
989ae721eb8b (svn r1086) -Fix: [Network] [ 1084774 ] Fixed quick-share-sell bug
truelight
parents: 630
diff changeset
  1943
	/* Those lines are here for network-protection (clients can be slow) */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1944
	if (GetAmountOwnedBy(c, _current_company) == 0) return CommandCost();
653
989ae721eb8b (svn r1086) -Fix: [Network] [ 1084774 ] Fixed quick-share-sell bug
truelight
parents: 630
diff changeset
  1945
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1946
	/* adjust it a little to make it less profitable to sell and buy */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1947
	Money cost = CalculateCompanyValue(c) >> 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1948
	cost = -(cost - (cost >> 7));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1949
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1950
	if (flags & DC_EXEC) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1951
		OwnerByte *b = c->share_owners;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1952
		while (*b != _current_company) b++; // share owners is guaranteed to contain company
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1953
		*b = COMPANY_SPECTATOR;
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1954
		InvalidateWindow(WC_COMPANY, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1955
	}
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
  1956
	return CommandCost(EXPENSES_OTHER, cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1957
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1958
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1959
/** Buy up another company.
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1960
 * When a competing company is gone bankrupt you get the chance to purchase
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1961
 * that company.
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1962
 * @todo currently this only works for AI companies
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
  1963
 * @param tile unused
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6405
diff changeset
  1964
 * @param flags type of operation
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1965
 * @param p1 company to buy up
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1966
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1967
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
  1968
CommandCost CmdBuyCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1969
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1970
	CompanyID cid = (CompanyID)p1;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1971
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1972
	/* Disable takeovers in multiplayer games */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1973
	if (!IsValidCompanyID(cid) || _networking) return CMD_ERROR;
6405
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
  1974
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1975
	/* Do not allow companies to take over themselves */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1976
	if (cid == _current_company) return CMD_ERROR;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1977
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1978
	Company *c = GetCompany(cid);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1979
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1980
	if (!c->is_ai) return CMD_ERROR;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1981
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1982
	if (flags & DC_EXEC) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1983
		DoAcquireCompany(c);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1984
	}
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10158
diff changeset
  1985
	return CommandCost(EXPENSES_OTHER, c->bankrupt_value);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1987
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1988
/** Prices */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1989
static void SaveLoad_PRIC()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1990
{
6955
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6954
diff changeset
  1991
	int vt = CheckSavegameVersion(65) ? (SLE_FILE_I32 | SLE_VAR_I64) : SLE_INT64;
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6954
diff changeset
  1992
	SlArray(&_price,      NUM_PRICES, vt);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1993
	SlArray(&_price_frac, NUM_PRICES, SLE_UINT16);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1994
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1995
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1996
/** Cargo payment rates */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1997
static void SaveLoad_CAPR()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1998
{
6463
e986fe39dfe7 (svn r9638) -Feature: Increase cargo types from 12 to 32 and enable newcargo flag in NewGRF loader.
peter1138
parents: 6458
diff changeset
  1999
	uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO;
6955
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6954
diff changeset
  2000
	int vt = CheckSavegameVersion(65) ? (SLE_FILE_I32 | SLE_VAR_I64) : SLE_INT64;
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6954
diff changeset
  2001
	SlArray(&_cargo_payment_rates,      num_cargo, vt);
6463
e986fe39dfe7 (svn r9638) -Feature: Increase cargo types from 12 to 32 and enable newcargo flag in NewGRF loader.
peter1138
parents: 6458
diff changeset
  2002
	SlArray(&_cargo_payment_rates_frac, num_cargo, SLE_UINT16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2003
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1797
diff changeset
  2005
static const SaveLoad _economy_desc[] = {
10146
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2006
	SLE_CONDVAR(Economy, max_loan,                      SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2007
	SLE_CONDVAR(Economy, max_loan,                      SLE_INT64,                  65, SL_MAX_VERSION),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2008
	SLE_CONDVAR(Economy, max_loan_unround,              SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2009
	SLE_CONDVAR(Economy, max_loan_unround,              SLE_INT64,                  65, SL_MAX_VERSION),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2010
	SLE_CONDVAR(Economy, max_loan_unround_fract,        SLE_UINT16,                 70, SL_MAX_VERSION),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2011
	    SLE_VAR(Economy, fluct,                         SLE_INT16),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2012
	    SLE_VAR(Economy, interest_rate,                 SLE_UINT8),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2013
	    SLE_VAR(Economy, infl_amount,                   SLE_UINT8),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2014
	    SLE_VAR(Economy, infl_amount_pr,                SLE_UINT8),
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2015
	SLE_CONDVAR(Economy, industry_daily_change_counter, SLE_UINT32,                102, SL_MAX_VERSION),
6955
da3431cb25a9 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 6954
diff changeset
  2016
	    SLE_END()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2018
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  2019
/** Economy variables */
10146
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2020
static void Save_ECMY()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1797
diff changeset
  2022
	SlObject(&_economy, _economy_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2023
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2024
10146
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2025
/** Economy variables */
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2026
static void Load_ECMY()
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2027
{
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2028
	SlObject(&_economy, _economy_desc);
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2029
	StartupIndustryDailyChanges(CheckSavegameVersion(102));  // old savegames will need to be initialized
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2030
}
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2031
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2032
extern const ChunkHandler _economy_chunk_handlers[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
	{ 'PRIC', SaveLoad_PRIC, SaveLoad_PRIC, CH_RIFF | CH_AUTO_LENGTH},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2034
	{ 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  2035
	{ 'SUBS', Save_SUBS,     Load_SUBS,     CH_ARRAY},
10146
cfea97f96067 (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas
parents: 10080
diff changeset
  2036
	{ 'ECMY', Save_ECMY,     Load_ECMY,     CH_RIFF | CH_LAST},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2037
};