src/economy.cpp
author rubidium
Mon, 20 Aug 2007 20:29:22 +0000
changeset 7954 774f46ff9444
parent 7943 5bd1fbc094c6
child 7970 204ece3a05d5
permissions -rw-r--r--
(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.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
     3
/** @file economy.cpp */
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1884
diff changeset
     6
#include "openttd.h"
2291
2b064aa97f91 (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"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#include "functions.h"
6949
72d11a1e1e60 (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: 6928
diff changeset
     9
#include "landscape.h"
1309
dab90d4cbf2d (svn r1813) Declare functions implemented in strings.c in their own shiny new header (though i think some of these function don't belong into strings.c)
tron
parents: 1308
diff changeset
    10
#include "strings.h" // XXX InjectDParam()
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 499
diff changeset
    11
#include "table/strings.h"
2218
abd47ef7d902 (svn r2736) -Codechange: De-mystified GfxDrawFillRect a bit, and used enums from table/sprites.h. You can now change the number of bits used for sprites and switches in the SpriteSetup enum and the rest should work automagically. Can be used to increase the number of active sprites to 2^19 in case there are no colortables (recolor sprites) in any newgrf. We should possibly move the the colortables to an own list, but how to detect them in a newgrf.
celestar
parents: 2186
diff changeset
    12
#include "table/sprites.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 653
diff changeset
    13
#include "map.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include "window.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#include "gfx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "saveload.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#include "economy.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
#include "industry.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
#include "town.h"
5720
cc0ceeafaa55 (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: 5677
diff changeset
    25
#include "network/network.h"
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 260
diff changeset
    26
#include "sound.h"
445
0e3fa3da3899 (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
    27
#include "engine.h"
5720
cc0ceeafaa55 (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: 5677
diff changeset
    28
#include "network/network_data.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    29
#include "variables.h"
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    30
#include "vehicle_gui.h"
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2306
diff changeset
    31
#include "ai/ai.h"
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2642
diff changeset
    32
#include "train.h"
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7340
diff changeset
    33
#include "roadveh.h"
6105
761d393829d6 (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: 5934
diff changeset
    34
#include "aircraft.h"
2962
dbd168a4703a (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
    35
#include "newgrf_engine.h"
4701
773b12043ef8 (svn r6611) - Newsounds:
peter1138
parents: 4621
diff changeset
    36
#include "newgrf_sound.h"
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
    37
#include "newgrf_callbacks.h"
7664
81c35390400e (svn r10442) -Codechange: implement the industry production callback.
rubidium
parents: 7663
diff changeset
    38
#include "newgrf_industries.h"
7725
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7695
diff changeset
    39
#include "newgrf_industrytiles.h"
3386
edda700ed8e5 (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
    40
#include "unmovable.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4260
diff changeset
    41
#include "date.h"
6417
26acff62d001 (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: 6324
diff changeset
    42
#include "cargotype.h"
6516
ee6d057b9850 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6501
diff changeset
    43
#include "player_face.h"
7139
4ae3ab180d05 (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: 7135
diff changeset
    44
#include "group.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
    46
/* Score info */
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    47
const ScoreInfo _score_info[] = {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    48
	{ SCORE_VEHICLES,        120, 100 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    49
	{ SCORE_STATIONS,         80, 100 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    50
	{ SCORE_MIN_PROFIT,    10000, 100 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    51
	{ SCORE_MIN_INCOME,    50000,  50 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    52
	{ SCORE_MAX_INCOME,   100000, 100 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    53
	{ SCORE_DELIVERED,     40000, 400 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    54
	{ SCORE_CARGO,             8,  50 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    55
	{ SCORE_MONEY,      10000000,  50 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    56
	{ SCORE_LOAN,         250000,  50 },
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    57
	{ SCORE_TOTAL,             0,   0 }
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    58
};
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    59
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    60
int _score_part[MAX_PLAYERS][SCORE_END];
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    61
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    62
Money CalculateCompanyValue(const Player* p)
2475
8443e1eefe1b (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
    63
{
8443e1eefe1b (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
    64
	PlayerID owner = p->index;
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    65
	/* Do a little nasty by using CommandCost, so we can use the "overflow" protection of CommandCost */
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    66
	CommandCost value;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
    67
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
		Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
		uint num = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
    71
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
		FOR_ALL_STATIONS(st) {
7375
e8639f73a615 (svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.
maedhros
parents: 7372
diff changeset
    73
			if (st->owner == owner) num += CountBitsSet(st->facilities);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    76
		value.AddCost(num * _price.station_value * 25);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
		Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
		FOR_ALL_VEHICLES(v) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    83
			if (v->owner != owner) continue;
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    84
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
    85
			if (v->type == VEH_TRAIN ||
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
    86
					v->type == VEH_ROAD ||
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
    87
					(v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) ||
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
    88
					v->type == VEH_SHIP) {
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    89
				value.AddCost(v->value * 3 >> 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    94
	/* Add real money value */
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    95
	value.AddCost(-p->current_loan);
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
    96
	value.AddCost(p->player_money);
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
    97
7451
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
    98
	return max(value.GetCost(), 1LL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   101
/** if update is set to true, the economy is updated with this score
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   102
 *  (also the house is updated, should only be true in the on-tick event)
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   103
 * @param update the economy with calculated score
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   104
 * @param p player been evaluated
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   105
 * @return actual score of this player
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   106
 * */
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
   107
int UpdateCompanyRatingAndValue(Player *p, bool update)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
	byte owner = p->index;
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   110
	int score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   111
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   112
	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
   113
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
/* Count vehicles */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
		Vehicle *v;
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   117
		Money min_profit = 0;
2829
e2dde3fe0bd9 (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
   118
		bool min_profit_first = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
		uint num = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
		FOR_ALL_VEHICLES(v) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   122
			if (v->owner != owner) continue;
7269
c7f39d91255e (svn r10009) -Codechange: Add and use Vehicle::IsPrimaryVehicle to replace individual checks depending on the vehicle type.
maedhros
parents: 7194
diff changeset
   123
			if (IsPlayerBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
				num++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
				if (v->age > 730) {
2829
e2dde3fe0bd9 (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
   126
					/* Find the vehicle with the lowest amount of profit */
e2dde3fe0bd9 (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
   127
					if (min_profit_first == true) {
e2dde3fe0bd9 (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
   128
						min_profit = v->profit_last_year;
e2dde3fe0bd9 (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
   129
						min_profit_first = false;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   130
					} else if (min_profit > v->profit_last_year) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
						min_profit = v->profit_last_year;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   132
					}
0
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
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   137
		_score_part[owner][SCORE_VEHICLES] = num;
2829
e2dde3fe0bd9 (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
   138
		/* Don't allow negative min_profit to show */
1407
bbcdd50795e3 (svn r1911) Put back an "if" i accidently removed in r1898
tron
parents: 1394
diff changeset
   139
		if (min_profit > 0)
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   140
			_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
   141
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
/* Count stations */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
		uint num = 0;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   146
		const Station* st;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
		FOR_ALL_STATIONS(st) {
7375
e8639f73a615 (svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.
maedhros
parents: 7372
diff changeset
   149
			if (st->owner == owner) num += CountBitsSet(st->facilities);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
		}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   151
		_score_part[owner][SCORE_STATIONS] = num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
/* Generate statistics depending on recent income statistics */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
	{
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   156
		int numec = min(p->num_valid_stat_ent, 12);
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   157
		if (numec != 0) {
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   158
			const PlayerEconomyEntry *pee = p->old_economy;
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   159
			Money min_income = pee->income + pee->expenses;
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   160
			Money max_income = pee->income + pee->expenses;
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
			do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
				min_income = min(min_income, pee->income + pee->expenses);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
				max_income = max(max_income, pee->income + pee->expenses);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
			} while (++pee,--numec);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
			if (min_income > 0)
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   168
				_score_part[owner][SCORE_MIN_INCOME] = ClampToI32(min_income);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   170
			_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
   171
		}
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
/* Generate score depending on amount of transported cargo */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
	{
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   176
		const PlayerEconomyEntry* pee;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
		int numec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
		uint32 total_delivered;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
		numec = min(p->num_valid_stat_ent, 4);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
		if (numec != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
			pee = p->old_economy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
			total_delivered = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
			do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
				total_delivered += pee->delivered_cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
			} while (++pee,--numec);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   188
			_score_part[owner][SCORE_DELIVERED] = total_delivered;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
	}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   191
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
/* Generate score for variety of cargo */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
	{
7375
e8639f73a615 (svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.
maedhros
parents: 7372
diff changeset
   194
		uint num = CountBitsSet(p->cargo_types);
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   195
		_score_part[owner][SCORE_CARGO] = num;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   196
		if (update) p->cargo_types = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
/* Generate score for player money */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
	{
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   201
		if (p->player_money > 0) {
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   202
			_score_part[owner][SCORE_MONEY] = ClampToI32(p->player_money);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
/* Generate score for loan */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	{
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   208
		_score_part[owner][SCORE_LOAN] = ClampToI32(_score_info[SCORE_LOAN].needed - p->current_loan);
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   209
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   210
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   211
	/* 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
   212
	{
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   213
		int total_score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   214
		int s;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   215
		score = 0;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   216
		for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   217
			/* Skip the total */
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   218
			if (i == SCORE_TOTAL) continue;
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   219
			/*  Check the score */
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
   220
			s = (_score_part[owner][i] >= _score_info[i].needed) ?
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
   221
				_score_info[i].score :
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   222
				_score_part[owner][i] * _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
   223
			if (s < 0) s = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   224
			score += s;
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
   225
			total_score += _score_info[i].score;
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   226
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   227
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   228
		_score_part[owner][SCORE_TOTAL] = score;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   229
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   230
		/*  We always want the score scaled to SCORE_MAX (1000) */
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   231
		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
   232
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   234
	if (update) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   235
		p->old_economy[0].performance_history = score;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   236
		UpdateCompanyHQ(p, score);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   237
		p->old_economy[0].company_value = CalculateCompanyValue(p);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   238
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   239
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   240
	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
   241
	return score;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   244
/*  use PLAYER_SPECTATOR as new_player to delete the player. */
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   245
void ChangeOwnershipOfPlayerItems(PlayerID old_player, PlayerID new_player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
{
5284
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   247
	Town *t;
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   248
	PlayerID old = _current_player;
6899
9fef750f15c5 (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: 6695
diff changeset
   249
6901
2201a832ae42 (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: 6899
diff changeset
   250
	assert(old_player != new_player);
2201a832ae42 (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: 6899
diff changeset
   251
6899
9fef750f15c5 (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: 6695
diff changeset
   252
	{
9fef750f15c5 (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: 6695
diff changeset
   253
		Player *p;
9fef750f15c5 (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: 6695
diff changeset
   254
		uint i;
9fef750f15c5 (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: 6695
diff changeset
   255
9fef750f15c5 (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: 6695
diff changeset
   256
		/* See if the old_player had shares in other companies */
9fef750f15c5 (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: 6695
diff changeset
   257
		_current_player = old_player;
9fef750f15c5 (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: 6695
diff changeset
   258
		FOR_ALL_PLAYERS(p) {
9fef750f15c5 (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: 6695
diff changeset
   259
			for (i = 0; i < 4; i++) {
9fef750f15c5 (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: 6695
diff changeset
   260
				if (p->share_owners[i] == old_player) {
9fef750f15c5 (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: 6695
diff changeset
   261
					/* Sell his shares */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7378
diff changeset
   262
					CommandCost res = DoCommand(0, p->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
6899
9fef750f15c5 (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: 6695
diff changeset
   263
					/* Because we are in a DoCommand, we can't just execute an other one and
9fef750f15c5 (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: 6695
diff changeset
   264
					 *  expect the money to be removed. We need to do it ourself! */
9fef750f15c5 (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: 6695
diff changeset
   265
					SubtractMoneyFromPlayer(res);
9fef750f15c5 (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: 6695
diff changeset
   266
				}
9fef750f15c5 (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: 6695
diff changeset
   267
			}
9fef750f15c5 (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: 6695
diff changeset
   268
		}
9fef750f15c5 (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: 6695
diff changeset
   269
9fef750f15c5 (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: 6695
diff changeset
   270
		/* Sell all the shares that people have on this company */
9fef750f15c5 (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: 6695
diff changeset
   271
		p = GetPlayer(old_player);
9fef750f15c5 (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: 6695
diff changeset
   272
		for (i = 0; i < 4; i++) {
9fef750f15c5 (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: 6695
diff changeset
   273
			_current_player = p->share_owners[i];
9fef750f15c5 (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: 6695
diff changeset
   274
			if (_current_player != PLAYER_SPECTATOR) {
9fef750f15c5 (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: 6695
diff changeset
   275
				/* Sell the shares */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7378
diff changeset
   276
				CommandCost res = DoCommand(0, old_player, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
6899
9fef750f15c5 (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: 6695
diff changeset
   277
				/* Because we are in a DoCommand, we can't just execute an other one and
9fef750f15c5 (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: 6695
diff changeset
   278
				 *  expect the money to be removed. We need to do it ourself! */
9fef750f15c5 (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: 6695
diff changeset
   279
				SubtractMoneyFromPlayer(res);
9fef750f15c5 (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: 6695
diff changeset
   280
			}
9fef750f15c5 (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: 6695
diff changeset
   281
		}
9fef750f15c5 (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: 6695
diff changeset
   282
	}
9fef750f15c5 (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: 6695
diff changeset
   283
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
	_current_player = old_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
4260
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   286
	/* Temporarily increase the player's money, to be sure that
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4540
diff changeset
   287
	 * removing his/her property doesn't fail because of lack of money.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4540
diff changeset
   288
	 * Not too drastically though, because it could overflow */
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   289
	if (new_player == PLAYER_SPECTATOR) {
7448
424ab8df8adb (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 7446
diff changeset
   290
		GetPlayer(old_player)->player_money = MAX_UVALUE(uint64) >> 2; // jackpot ;p
4260
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   291
	}
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   292
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   293
	if (new_player == PLAYER_SPECTATOR) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
		Subsidy *s;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   295
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   296
		for (s = _subsidies; s != endof(_subsidies); s++) {
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   297
			if (s->cargo_type != CT_INVALID && s->age >= 12) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   298
				if (GetStation(s->to)->owner == old_player) s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   303
	/* Take care of rating in towns */
5284
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   304
	FOR_ALL_TOWNS(t) {
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   305
		/* If a player takes over, give the ratings to that player. */
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   306
		if (new_player != PLAYER_SPECTATOR) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   307
			if (HASBIT(t->have_ratings, old_player)) {
4260
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   308
				if (HASBIT(t->have_ratings, new_player)) {
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   309
					// use max of the two ratings.
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   310
					t->ratings[new_player] = max(t->ratings[new_player], t->ratings[old_player]);
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   311
				} else {
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   312
					SETBIT(t->have_ratings, new_player);
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   313
					t->ratings[new_player] = t->ratings[old_player];
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   314
				}
4260
8a9824f6e501 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   315
			}
5284
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   316
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   317
5284
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   318
		/* Reset the ratings for the old player */
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   319
		t->ratings[old_player] = 500;
3c42e8e18392 (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   320
		CLRBIT(t->have_ratings, old_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   322
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
		int num_train = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
		int num_road = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
		int num_ship = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
		int num_aircraft = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
		Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   330
		/*  Determine Ids for the new vehicles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
		FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
			if (v->owner == new_player) {
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   333
				switch (v->type) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   334
					case VEH_TRAIN:    if (IsFrontEngine(v)) num_train++; break;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7340
diff changeset
   335
					case VEH_ROAD:     if (IsRoadVehFront(v)) num_road++; break;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   336
					case VEH_SHIP:     num_ship++; break;
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   337
					case VEH_AIRCRAFT: if (IsNormalAircraft(v)) num_aircraft++; break;
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   338
					default: break;
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   339
				}
0
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
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
		FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   344
			if (v->owner == old_player && IS_BYTE_INSIDE(v->type, VEH_TRAIN, VEH_AIRCRAFT + 1)) {
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   345
				if (new_player == PLAYER_SPECTATOR) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
					DeleteWindowById(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
					DeleteWindowById(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
					DeleteWindowById(WC_VEHICLE_ORDERS, v->index);
7372
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   349
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   350
					if (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && IsFreeWagon(v))) {
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   351
						switch (v->type) {
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   352
							default: NOT_REACHED();
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   353
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   354
							case VEH_TRAIN: {
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   355
								Vehicle *u = v;
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   356
								do {
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   357
									Vehicle *next = GetNextVehicle(u);
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7825
diff changeset
   358
									delete u;
7372
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   359
									u = next;
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   360
								} while (u != NULL);
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   361
							} break;
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   362
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   363
							case VEH_ROAD:
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   364
							case VEH_SHIP:
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7825
diff changeset
   365
								delete v;
7372
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   366
								break;
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   367
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   368
							case VEH_AIRCRAFT:
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   369
								DeleteVehicleChain(v);
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   370
								break;
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   371
						}
c254cca76042 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 7353
diff changeset
   372
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
					v->owner = new_player;
7139
4ae3ab180d05 (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: 7135
diff changeset
   375
					v->group_id = DEFAULT_GROUP;
4621
79238ae21c0d (svn r6479) -Fix r6424: FS#348 Plane autoreplace glitch
bjarni
parents: 4549
diff changeset
   376
					if (IsEngineCountable(v)) GetPlayer(new_player)->num_engines[v->engine_type]++;
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   377
					switch (v->type) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   378
						case VEH_TRAIN:    if (IsFrontEngine(v)) v->unitnumber = ++num_train; break;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7340
diff changeset
   379
						case VEH_ROAD:     if (IsRoadVehFront(v)) v->unitnumber = ++num_road; break;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   380
						case VEH_SHIP:     v->unitnumber = ++num_ship; break;
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   381
						case VEH_AIRCRAFT: if (IsNormalAircraft(v)) v->unitnumber = ++num_aircraft; break;
7117
866a75f1ac5e (svn r9841) -Codechange: add a little more type strictness to the vehicle types.
rubidium
parents: 7116
diff changeset
   382
						default: NOT_REACHED();
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   383
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   389
	/*  Change ownership of tiles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
	{
1797
a19c4181f1b7 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   391
		TileIndex tile = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
			ChangeTileOwner(tile, old_player, new_player);
863
8d09f9331a80 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 830
diff changeset
   394
		} while (++tile != MapSize());
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
5077
587701a06b6a (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
   397
	/* Change color of existing windows */
587701a06b6a (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
   398
	if (new_player != PLAYER_SPECTATOR) ChangeWindowOwner(old_player, new_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	_current_player = old;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
	MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
6588
73d273b7b9fc (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: 6585
diff changeset
   405
static void ChangeNetworkOwner(PlayerID current_player, PlayerID new_player)
73d273b7b9fc (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: 6585
diff changeset
   406
{
73d273b7b9fc (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: 6585
diff changeset
   407
#ifdef ENABLE_NETWORK
73d273b7b9fc (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: 6585
diff changeset
   408
	if (!_networking) return;
73d273b7b9fc (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: 6585
diff changeset
   409
73d273b7b9fc (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: 6585
diff changeset
   410
	if (current_player == _local_player) {
73d273b7b9fc (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: 6585
diff changeset
   411
		_network_playas = new_player;
73d273b7b9fc (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: 6585
diff changeset
   412
		SetLocalPlayer(new_player);
73d273b7b9fc (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: 6585
diff changeset
   413
	}
73d273b7b9fc (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: 6585
diff changeset
   414
73d273b7b9fc (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: 6585
diff changeset
   415
	if (!_network_server) return;
73d273b7b9fc (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: 6585
diff changeset
   416
73d273b7b9fc (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: 6585
diff changeset
   417
	/* The server has to handle all administrative issues, for example
73d273b7b9fc (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: 6585
diff changeset
   418
	* updating and notifying all clients of what has happened */
73d273b7b9fc (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: 6585
diff changeset
   419
	NetworkTCPSocketHandler *cs;
73d273b7b9fc (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: 6585
diff changeset
   420
	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
73d273b7b9fc (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: 6585
diff changeset
   421
73d273b7b9fc (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: 6585
diff changeset
   422
	/* The server has just changed from player */
73d273b7b9fc (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: 6585
diff changeset
   423
	if (current_player == ci->client_playas) {
73d273b7b9fc (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: 6585
diff changeset
   424
		ci->client_playas = new_player;
73d273b7b9fc (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: 6585
diff changeset
   425
		NetworkUpdateClientInfo(NETWORK_SERVER_INDEX);
73d273b7b9fc (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: 6585
diff changeset
   426
	}
73d273b7b9fc (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: 6585
diff changeset
   427
73d273b7b9fc (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: 6585
diff changeset
   428
	/* Find all clients that were in control of this company, and mark them as new_player */
73d273b7b9fc (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: 6585
diff changeset
   429
	FOR_ALL_CLIENTS(cs) {
73d273b7b9fc (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: 6585
diff changeset
   430
		ci = DEREF_CLIENT_INFO(cs);
73d273b7b9fc (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: 6585
diff changeset
   431
		if (current_player == ci->client_playas) {
73d273b7b9fc (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: 6585
diff changeset
   432
			ci->client_playas = new_player;
73d273b7b9fc (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: 6585
diff changeset
   433
			NetworkUpdateClientInfo(ci->client_index);
73d273b7b9fc (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: 6585
diff changeset
   434
		}
73d273b7b9fc (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: 6585
diff changeset
   435
	}
73d273b7b9fc (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: 6585
diff changeset
   436
#endif /* ENABLE_NETWORK */
73d273b7b9fc (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: 6585
diff changeset
   437
}
73d273b7b9fc (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: 6585
diff changeset
   438
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
static void PlayersCheckBankrupt(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
{
2475
8443e1eefe1b (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
   441
	PlayerID owner;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   443
	/*  If the player has money again, it does not go bankrupt */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
	if (p->player_money >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
		p->quarters_of_bankrupcy = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
	p->quarters_of_bankrupcy++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
	owner = p->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   453
	switch (p->quarters_of_bankrupcy) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   454
		case 2:
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   455
			AddNewsItem( (StringID)(owner | NB_BTROUBLE),
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   456
				NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   457
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   458
		case 3: {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   459
			/* XXX - In multiplayer, should we ask other players if it wants to take
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   460
		          over when it is a human company? -- TrueLight */
4845
49105d3b5a9a (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4814
diff changeset
   461
			if (IsHumanPlayer(owner)) {
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   462
				AddNewsItem( (StringID)(owner | NB_BTROUBLE),
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   463
					NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   464
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   465
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   467
			/* Check if the company has any value.. if not, declare it bankrupt
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   468
			 *  right now */
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   469
			Money val = CalculateCompanyValue(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   470
			if (val > 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   471
				p->bankrupt_value = val;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   472
				p->bankrupt_asked = 1 << owner; // Don't ask the owner
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   473
				p->bankrupt_timeout = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   474
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   475
			}
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   476
			/* Else, falltrue to case 4... */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   478
		case 4: {
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   479
			/* Close everything the owner has open */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   480
			DeletePlayerWindows(owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   481
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   482
			/* Show bankrupt news */
7554
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
   483
			SetDParam(0, p->index);
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   484
			AddNewsItem( (StringID)(owner | NB_BBANKRUPT), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   485
5067
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   486
			if (IsHumanPlayer(owner)) {
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   487
				/* XXX - If we are in offline mode, leave the player playing. Eg. there
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   488
				 * is no THE-END, otherwise mark the player as spectator to make sure
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   489
				 * he/she is no long in control of this company */
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   490
				if (!_networking) {
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   491
					p->bankrupt_asked = 0xFF;
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   492
					p->bankrupt_timeout = 0x456;
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   493
					break;
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   494
				}
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   495
6588
73d273b7b9fc (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: 6585
diff changeset
   496
				ChangeNetworkOwner(owner, PLAYER_SPECTATOR);
5067
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   497
			}
687
e8d646a00eb1 (svn r1128) -Fix: [Network] Going bankrupt of human-players was corrupted in networkgames
truelight
parents: 679
diff changeset
   498
5067
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   499
			/* Remove the player */
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   500
			ChangeOwnershipOfPlayerItems(owner, PLAYER_SPECTATOR);
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   501
			/* Register the player as not-active */
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   502
			p->is_active = false;
2395
19b4da30806b (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2306
diff changeset
   503
5067
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   504
			if (!IsHumanPlayer(owner) && (!_networking || _network_server) && _ai.enabled)
870dfdcef898 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   505
				AI_PlayerDied(owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
	}
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
void DrawNewsBankrupcy(Window *w)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
{
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   512
	DrawNewsBorder(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   514
	const NewsItem *ni = WP(w, news_d).ni;
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   515
	Player *p = GetPlayer((PlayerID)GB(ni->string_id, 0, 4));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
	DrawPlayerFace(p->face, p->player_color, 2, 23);
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5916
diff changeset
   517
	GfxFillRect(3, 23, 3 + 91, 23 + 118, PALETTE_TO_STRUCT_GREY | (1 << USE_COLORTABLE));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
7554
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
   519
	SetDParam(0, p->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
	DrawStringMultiCenter(49, 148, STR_7058_PRESIDENT, 94);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   523
	switch (ni->string_id & 0xF0) {
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   524
	case NB_BTROUBLE:
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   525
		DrawStringCentered(w->width >> 1, 1, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
7554
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
   527
		SetDParam(0, p->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
			STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   536
	case NB_BMERGER:
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   537
		DrawStringCentered(w->width >> 1, 1, STR_7059_TRANSPORT_COMPANY_MERGER, 0);
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   538
		SetDParam(0, ni->params[0]);
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   539
		SetDParam(1, p->index);
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   540
		SetDParam(2, ni->params[1]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
			90,
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   544
			ni->params[1] == 0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   548
	case NB_BBANKRUPT:
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   549
		DrawStringCentered(w->width >> 1, 1, STR_705C_BANKRUPT, 0);
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   550
		SetDParam(0, ni->params[0]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
			STR_705D_HAS_BEEN_CLOSED_DOWN_BY,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
			w->width - 101);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   556
		break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   558
	case NB_BNEWCOMPANY:
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   559
		DrawStringCentered(w->width >> 1, 1, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED, 0);
7554
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
   560
		SetDParam(0, p->index);
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   561
		SetDParam(1, ni->params[0]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
			STR_705F_STARTS_CONSTRUCTION_NEAR,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
	default:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
		NOT_REACHED();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
2436
177cb6a8339f (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2425
diff changeset
   574
StringID GetNewsStringBankrupcy(const NewsItem *ni)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   576
	const Player *p = GetPlayer((PlayerID)GB(ni->string_id, 0, 4));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   578
	switch (ni->string_id & 0xF0) {
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   579
	case NB_BTROUBLE:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   580
		SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   581
		SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
7554
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
   582
		SetDParam(2, p->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
		return STR_02B6;
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   584
	case NB_BMERGER:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   585
		SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER);
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   586
		SetDParam(1, ni->params[1] == 0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR);
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   587
		SetDParam(2, ni->params[0]);
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   588
		SetDParam(3, p->index);
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   589
		SetDParam(4, ni->params[1]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
		return STR_02B6;
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   591
	case NB_BBANKRUPT:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   592
		SetDParam(0, STR_705C_BANKRUPT);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   593
		SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   594
		SetDParam(2, ni->params[0]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
		return STR_02B6;
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   596
	case NB_BNEWCOMPANY:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   597
		SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   598
		SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
7554
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
   599
		SetDParam(2, p->index);
7943
5bd1fbc094c6 (svn r10921) -Fix [FS#1130] (r10323): the company related news messages still thought that companies were described by two parameters instead of one.
rubidium
parents: 7932
diff changeset
   600
		SetDParam(3, ni->params[0]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
		return STR_02B6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
	default:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
		NOT_REACHED();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
   607
static void PlayersGenStatistics()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
	FOR_ALL_STATIONS(st) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   613
		_current_player = st->owner;
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   614
		SET_EXPENSES_TYPE(EXPENSES_PROPERTY);
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   615
		SubtractMoneyFromPlayer(_price.station_value >> 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	}
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
	if (!HASBIT(1<<0|1<<3|1<<6|1<<9, _cur_month))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
	FOR_ALL_PLAYERS(p) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
		if (p->is_active) {
5677
525f85bee8f9 (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5566
diff changeset
   623
			memmove(&p->old_economy[1], &p->old_economy[0], sizeof(p->old_economy) - sizeof(p->old_economy[0]));
525f85bee8f9 (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5566
diff changeset
   624
			p->old_economy[0] = p->cur_economy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
			memset(&p->cur_economy, 0, sizeof(p->cur_economy));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
5677
525f85bee8f9 (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5566
diff changeset
   627
			if (p->num_valid_stat_ent != 24) p->num_valid_stat_ent++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   629
			UpdateCompanyRatingAndValue(p, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
			PlayersCheckBankrupt(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
5677
525f85bee8f9 (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5566
diff changeset
   632
			if (p->block_preview != 0) p->block_preview--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
	InvalidateWindow(WC_INCOME_GRAPH, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
	InvalidateWindow(WC_OPERATING_PROFIT, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
	InvalidateWindow(WC_DELIVERED_CARGO, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
	InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
	InvalidateWindow(WC_COMPANY_VALUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
	InvalidateWindow(WC_COMPANY_LEAGUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   644
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
   645
{
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   646
	/* Is it safe to add inflation ? */
7453
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 7451
diff changeset
   647
	if ((INT64_MAX / amt) < (*value + 1)) {
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 7451
diff changeset
   648
		*value = INT64_MAX / amt;
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   649
		*frac = 0;
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   650
	} else {
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   651
		int64 tmp = (int64)*value * amt + *frac;
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   652
		*frac   = GB(tmp, 0, 16);
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   653
		*value += tmp >> 16;
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   654
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
   657
static void AddInflation()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
{
7740
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   659
	/* The cargo payment inflation differs from the normal inflation, so the
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   660
	 * relative amount of money you make with a transport decreases slowly over
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   661
	 * the 170 years. After a few hundred years we reach a level in which the
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   662
	 * games will become unplayable as the maximum income will be less than
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   663
	 * the minimum running cost.
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   664
	 *
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   665
	 * Furthermore there are a lot of inflation related overflows all over the
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   666
	 * place. Solving them is hardly possible because inflation will always
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   667
	 * reach the overflow threshold some day. So we'll just perform the
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   668
	 * inflation mechanism during the first 170 years (the amount of years that
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   669
	 * one had in the original TTD) and stop doing the inflation after that
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   670
	 * because it only causes problems that can't be solved nicely and the
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   671
	 * inflation doesn't add anything after that either; it even makes playing
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   672
	 * it impossible due to the diverging cost and income rates.
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   673
	 */
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   674
	if ((_cur_year - _patches.starting_year) >= (ORIGINAL_MAX_YEAR - ORIGINAL_BASE_YEAR)) return;
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   675
6445
6db2e90e8bcf (svn r8855) -Fix
tron
parents: 6437
diff changeset
   676
	/* Approximation for (100 + infl_amount)% ** (1 / 12) - 100%
6db2e90e8bcf (svn r8855) -Fix
tron
parents: 6437
diff changeset
   677
	 * scaled by 65536
6db2e90e8bcf (svn r8855) -Fix
tron
parents: 6437
diff changeset
   678
	 * 12 -> months per year
6db2e90e8bcf (svn r8855) -Fix
tron
parents: 6437
diff changeset
   679
	 * This is only a good approxiamtion for small values
6db2e90e8bcf (svn r8855) -Fix
tron
parents: 6437
diff changeset
   680
	 */
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   681
	Money inf = _economy.infl_amount * 54;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
6445
6db2e90e8bcf (svn r8855) -Fix
tron
parents: 6437
diff changeset
   683
	for (uint i = 0; i != NUM_PRICES; i++) {
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   684
		AddSingleInflation((Money*)&_price + i, _price_frac + i, inf);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
7740
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   687
	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
   688
7740
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
   689
	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
   690
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
	inf = _economy.infl_amount_pr * 54;
6676
30aea9ac89bc (svn r9388) -Codechange: variable scope and type, and standardify all CargoID loops.
peter1138
parents: 6641
diff changeset
   692
	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
   693
		AddSingleInflation(
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   694
			(Money*)_cargo_payment_rates + i,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
			_cargo_payment_rates_frac + i,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
			inf
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
		);
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
1098
481fbdea1569 (svn r1599) fix: autoreplace window now updates when inflation alters prices
bjarni
parents: 1093
diff changeset
   701
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
	InvalidateWindowClasses(WC_VEHICLE_DETAILS);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
	InvalidateWindow(WC_PAYMENT_RATES, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
   706
static void PlayersPayInterest()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
{
2548
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2544
diff changeset
   708
	const Player* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
	int interest = _economy.interest_rate * 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
	FOR_ALL_PLAYERS(p) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2630
diff changeset
   712
		if (!p->is_active) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
		_current_player = p->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
		SET_EXPENSES_TYPE(EXPENSES_LOAN_INT);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   716
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   717
		SubtractMoneyFromPlayer(CommandCost((Money)BIGMULUS(p->current_loan, interest, 16)));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
		SET_EXPENSES_TYPE(EXPENSES_OTHER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
		SubtractMoneyFromPlayer(_price.station_value >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   721
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
   724
static void HandleEconomyFluctuations()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2630
diff changeset
   726
	if (_opt.diff.economy == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
	if (--_economy.fluct == 0) {
2642
cb99ff2e49a7 (svn r3184) GB/SB
tron
parents: 2639
diff changeset
   729
		_economy.fluct = -(int)GB(Random(), 0, 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
		AddNewsItem(STR_7073_WORLD_RECESSION_FINANCIAL, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
	} else if (_economy.fluct == -12) {
2642
cb99ff2e49a7 (svn r3184) GB/SB
tron
parents: 2639
diff changeset
   732
		_economy.fluct = GB(Random(), 0, 8) + 312;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
		AddNewsItem(STR_7074_RECESSION_OVER_UPTURN_IN, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
static byte _price_category[NUM_PRICES] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
	0, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
	2, 2, 1, 1, 1, 1, 1, 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
	2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   747
static const Money _price_base[NUM_PRICES] = {
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   748
	    100, ///< station_value
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   749
	    100, ///< build_rail
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   750
	     95, ///< build_road
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   751
	     65, ///< build_signals
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   752
	    275, ///< build_bridge
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   753
	    600, ///< build_train_depot
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   754
	    500, ///< build_road_depot
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   755
	    700, ///< build_ship_depot
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   756
	    450, ///< build_tunnel
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   757
	    200, ///< train_station_track
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   758
	    180, ///< train_station_length
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   759
	    600, ///< build_airport
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   760
	    200, ///< build_bus_station
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   761
	    200, ///< build_truck_station
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   762
	    350, ///< build_dock
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   763
	 400000, ///< build_railvehicle
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   764
	   2000, ///< build_railwagon
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   765
	 700000, ///< aircraft_base
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   766
	  14000, ///< roadveh_base
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   767
	  65000, ///< ship_base
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   768
	     20, ///< build_trees
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   769
	    250, ///< terraform
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   770
	     20, ///< clear_1
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   771
	     40, ///< purchase_land
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   772
	    200, ///< clear_2
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   773
	    500, ///< clear_3
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   774
	     20, ///< remove_trees
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   775
	    -70, ///< remove_rail
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   776
	     10, ///< remove_signals
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   777
	     50, ///< clear_bridge
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   778
	     80, ///< remove_train_depot
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   779
	     80, ///< remove_road_depot
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   780
	     90, ///< remove_ship_depot
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   781
	     30, ///< clear_tunnel
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   782
	  10000, ///< clear_water
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   783
	     50, ///< remove_rail_station
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   784
	     30, ///< remove_airport
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   785
	     50, ///< remove_bus_station
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   786
	     50, ///< remove_truck_station
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   787
	     55, ///< remove_dock
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   788
	   1600, ///< remove_house
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   789
	     40, ///< remove_road
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   790
	   5600, ///< running_rail[0] railroad
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   791
	   5200, ///< running_rail[1] monorail
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   792
	   4800, ///< running_rail[2] maglev
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   793
	   9600, ///< aircraft_running
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   794
	   1600, ///< roadveh_running
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   795
	   5600, ///< ship_running
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   796
	1000000, ///< build_industry
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
2506
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   799
static byte price_base_multiplier[NUM_PRICES];
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   800
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   801
/**
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   802
 * Reset changes to the price base multipliers.
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   803
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
   804
void ResetPriceBaseMultipliers()
2506
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   805
{
2508
c1a1af1920ce (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   806
	uint i;
2506
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   807
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   808
	/* 8 means no multiplier. */
2506
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   809
	for (i = 0; i < NUM_PRICES; i++)
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   810
		price_base_multiplier[i] = 8;
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   811
}
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   812
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   813
/**
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   814
 * Change a price base by the given factor.
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   815
 * The price base is altered by factors of two, with an offset of 8.
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   816
 * NewBaseCost = OldBaseCost * 2^(n-8)
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   817
 * @param price Index of price base to change.
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   818
 * @param factor Amount to change by.
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   819
 */
2508
c1a1af1920ce (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   820
void SetPriceBaseMultiplier(uint price, byte factor)
2506
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   821
{
2508
c1a1af1920ce (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   822
	assert(price < NUM_PRICES);
c1a1af1920ce (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   823
	price_base_multiplier[price] = factor;
2506
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   824
}
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   825
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
   826
void StartupEconomy()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   830
	assert(sizeof(_price) == NUM_PRICES * sizeof(Money));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   832
	for (i = 0; i != NUM_PRICES; i++) {
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   833
		Money price = _price_base[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
		if (_price_category[i] != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
			uint mod = _price_category[i] == 1 ? _opt.diff.vehicle_costs : _opt.diff.construction_cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
			if (mod < 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
				price = price * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
			} else if (mod > 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
				price = price * 9 >> 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
		}
2506
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   842
		if (price_base_multiplier[i] > 8) {
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   843
			price <<= price_base_multiplier[i] - 8;
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   844
		} else {
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   845
			price >>= 8 - price_base_multiplier[i];
777ac2d9a98a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   846
		}
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
   847
		((Money*)&_price)[i] = price;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
		_price_frac[i] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
	_economy.interest_rate = _opt.diff.initial_interest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
	_economy.infl_amount = _opt.diff.initial_interest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
	_economy.infl_amount_pr = max(0, _opt.diff.initial_interest - 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
	_economy.max_loan_unround = _economy.max_loan = _opt.diff.max_loan * 1000;
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2070
diff changeset
   855
	_economy.fluct = GB(Random(), 0, 8) + 168;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2587
diff changeset
   858
Pair SetupSubsidyDecodeParam(const Subsidy* s, bool mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
{
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   860
	TileIndex tile;
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   861
	TileIndex tile2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
	Pair tp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
2272
fc701f21b2be (svn r2794) Fix a comment and get rid of some parentheses using DeMorgan's law
tron
parents: 2261
diff changeset
   864
	/* if mode is false, use the singular form */
6417
26acff62d001 (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: 6324
diff changeset
   865
	const CargoSpec *cs = GetCargo(s->cargo_type);
7783
467bf38f954f (svn r10628) -Fix (r10606,FS#1055): Revert r10606 and fix the plural problem another way.
peter1138
parents: 7762
diff changeset
   866
	SetDParam(0, mode ? cs->name : cs->name_single);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
	if (s->age < 12) {
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   869
		if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL) {
2070
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   870
			SetDParam(1, STR_INDUSTRY);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   871
			SetDParam(2, s->from);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   872
			tile = GetIndustry(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   874
			if (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD) {
2070
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   875
				SetDParam(4, STR_INDUSTRY);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   876
				SetDParam(5, s->to);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   877
				tile2 = GetIndustry(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   878
			} else {
2070
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   879
				SetDParam(4, STR_TOWN);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   880
				SetDParam(5, s->to);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   881
				tile2 = GetTown(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
		} else {
2070
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   884
			SetDParam(1, STR_TOWN);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   885
			SetDParam(2, s->from);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   886
			tile = GetTown(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
2070
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   888
			SetDParam(4, STR_TOWN);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   889
			SetDParam(5, s->to);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   890
			tile2 = GetTown(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
	} else {
2070
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   893
		SetDParam(1, s->from);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   894
		tile = GetStation(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
2070
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   896
		SetDParam(2, s->to);
26c657906f25 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   897
		tile2 = GetStation(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   899
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
	tp.a = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
	tp.b = tile2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
	return tp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
5566
02b1ecccc540 (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: 5564
diff changeset
   906
void DeleteSubsidyWithTown(TownID index)
02b1ecccc540 (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: 5564
diff changeset
   907
{
02b1ecccc540 (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: 5564
diff changeset
   908
	Subsidy *s;
02b1ecccc540 (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: 5564
diff changeset
   909
02b1ecccc540 (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: 5564
diff changeset
   910
	for (s = _subsidies; s != endof(_subsidies); s++) {
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   911
		if (s->cargo_type != CT_INVALID && s->age < 12) {
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   912
			const CargoSpec *cs = GetCargo(s->cargo_type);
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   913
			if (((cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) && (index == s->from || index == s->to)) ||
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   914
				((cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) && index == s->to)) {
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   915
				s->cargo_type = CT_INVALID;
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   916
			}
5566
02b1ecccc540 (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: 5564
diff changeset
   917
		}
02b1ecccc540 (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: 5564
diff changeset
   918
	}
02b1ecccc540 (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: 5564
diff changeset
   919
}
02b1ecccc540 (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: 5564
diff changeset
   920
4330
7306bda145ab (svn r6005) -Cleanup: introduce IndustryID and use it
rubidium
parents: 4329
diff changeset
   921
void DeleteSubsidyWithIndustry(IndustryID index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
	Subsidy *s;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   924
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   925
	for (s = _subsidies; s != endof(_subsidies); s++) {
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   926
		if (s->cargo_type != CT_INVALID && s->age < 12) {
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   927
			const CargoSpec *cs = GetCargo(s->cargo_type);
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   928
			if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL &&
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   929
				(index == s->from || (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD && index == s->to))) {
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   930
				s->cargo_type = CT_INVALID;
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
   931
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   933
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
4330
7306bda145ab (svn r6005) -Cleanup: introduce IndustryID and use it
rubidium
parents: 4329
diff changeset
   936
void DeleteSubsidyWithStation(StationID index)
0
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
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
	bool dirty = false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   940
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   941
	for (s = _subsidies; s != endof(_subsidies); s++) {
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   942
		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
   943
				(s->from == index || s->to == index)) {
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   944
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
			dirty = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   947
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	if (dirty)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   953
struct FoundRoute {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
	uint distance;
3344
d86c852715fa (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
   955
	CargoID cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
	void *from;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
	void *to;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   958
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
static void FindSubsidyPassengerRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
	Town *from,*to;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
	fr->distance = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
4356
bc52a48e2590 (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
   966
	fr->from = from = GetRandomTown();
bc52a48e2590 (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
   967
	if (from == NULL || from->population < 400) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
4356
bc52a48e2590 (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
   969
	fr->to = to = GetRandomTown();
bc52a48e2590 (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
   970
	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
   971
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
   973
	fr->distance = DistanceManhattan(from->xy, to->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
static void FindSubsidyCargoRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
	Industry *i;
7132
08abbae3073e (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: 7131
diff changeset
   979
	const IndustrySpec *ind;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
	int trans, total;
3344
d86c852715fa (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
   981
	CargoID cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
	fr->distance = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
4356
bc52a48e2590 (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
   985
	fr->from = i = GetRandomIndustry();
bc52a48e2590 (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
   986
	if (i == NULL) return;
7132
08abbae3073e (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: 7131
diff changeset
   987
	ind = GetIndustrySpec(i->type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
   989
	/* Randomize cargo type */
7132
08abbae3073e (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: 7131
diff changeset
   990
	if (HASBIT(Random(), 0) && ind->produced_cargo[1] != CT_INVALID) {
08abbae3073e (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: 7131
diff changeset
   991
		cargo = ind->produced_cargo[1];
7315
f1b2591ad082 (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: 7269
diff changeset
   992
		trans = i->last_month_pct_transported[1];
f1b2591ad082 (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: 7269
diff changeset
   993
		total = i->last_month_production[1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
	} else {
7132
08abbae3073e (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: 7131
diff changeset
   995
		cargo = ind->produced_cargo[0];
7315
f1b2591ad082 (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: 7269
diff changeset
   996
		trans = i->last_month_pct_transported[0];
f1b2591ad082 (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: 7269
diff changeset
   997
		total = i->last_month_production[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1000
	/* Quit if no production in this industry
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1001
	 * or if the cargo type is passengers
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1002
	 * or if the pct transported is already large enough */
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1003
	if (total == 0 || trans > 42 || cargo == CT_INVALID) return;
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1004
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1005
	const CargoSpec *cs = GetCargo(cargo);
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1006
	if (cs->town_effect == TE_PASSENGERS) return;
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
	fr->cargo = cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1010
	if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) {
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1011
		/*  The destination is a town */
4356
bc52a48e2590 (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
  1012
		Town *t = GetRandomTown();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1013
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1014
		/* Only want big towns */
4356
bc52a48e2590 (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
  1015
		if (t == NULL || t->population < 900) return;
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1016
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
  1017
		fr->distance = DistanceManhattan(i->xy, t->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
		fr->to = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
	} else {
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1020
		/* The destination is an industry */
4356
bc52a48e2590 (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
  1021
		Industry *i2 = GetRandomIndustry();
7132
08abbae3073e (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: 7131
diff changeset
  1022
		if (i2 == NULL) {
08abbae3073e (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: 7131
diff changeset
  1023
			return;
08abbae3073e (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: 7131
diff changeset
  1024
		}
08abbae3073e (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: 7131
diff changeset
  1025
08abbae3073e (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: 7131
diff changeset
  1026
		ind = GetIndustrySpec(i2->type);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1027
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1028
		/* The industry must accept the cargo */
7132
08abbae3073e (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: 7131
diff changeset
  1029
		if (i == i2 ||
08abbae3073e (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: 7131
diff changeset
  1030
				(cargo != ind->accepts_cargo[0] &&
08abbae3073e (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: 7131
diff changeset
  1031
				cargo != ind->accepts_cargo[1] &&
08abbae3073e (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: 7131
diff changeset
  1032
				cargo != ind->accepts_cargo[2])) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
			return;
7132
08abbae3073e (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: 7131
diff changeset
  1034
		}
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
  1035
		fr->distance = DistanceManhattan(i->xy, i2->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
		fr->to = i2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
	}
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
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1040
static bool CheckSubsidyDuplicate(Subsidy *s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2587
diff changeset
  1042
	const Subsidy* ss;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1044
	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
  1045
		if (s != ss &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1046
				ss->from == s->from &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1047
				ss->to == s->to &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
				ss->cargo_type == s->cargo_type) {
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1049
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
  1056
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
  1057
static void SubsidyMonthlyHandler()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
	uint n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
	bool modified = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1066
	for (s = _subsidies; s != endof(_subsidies); s++) {
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1067
		if (s->cargo_type == CT_INVALID) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
		if (s->age == 12-1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1070
			pair = SetupSubsidyDecodeParam(s, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
			AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1072
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
		} else if (s->age == 2*12-1) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1075
			st = GetStation(s->to);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
			if (st->owner == _local_player) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
				pair = SetupSubsidyDecodeParam(s, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
				AddNewsItem(STR_202F_SUBSIDY_WITHDRAWN_SERVICE, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
			}
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1080
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
			s->age++;
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1087
	/* 25% chance to go on */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
  1088
	if (CHANCE16(1,4)) {
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1089
		/*  Find a free slot*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
		s = _subsidies;
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1091
		while (s->cargo_type != CT_INVALID) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
			if (++s == endof(_subsidies))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1093
				goto no_add;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1094
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1095
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
		n = 1000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1098
			FindSubsidyPassengerRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1099
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
				s->cargo_type = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
				s->from = ((Town*)fr.from)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1102
				s->to = ((Town*)fr.to)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
				goto add_subsidy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1104
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
			FindSubsidyCargoRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1106
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
				s->cargo_type = fr.cargo;
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1108
				s->from = ((Industry*)fr.from)->index;
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1109
				{
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1110
					const CargoSpec *cs = GetCargo(fr.cargo);
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1111
					s->to = (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) ? ((Town*)fr.to)->index : ((Industry*)fr.to)->index;
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1112
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
	add_subsidy:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
				if (!CheckSubsidyDuplicate(s)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
					s->age = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
					pair = SetupSubsidyDecodeParam(s, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
					AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
					modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
			}
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
  1122
		} while (n--);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
no_add:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
	if (modified)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
1881
023a134a4b12 (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
  1129
static const SaveLoad _subsidies_desc[] = {
4344
5d0e40cd67b9 (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
  1130
	    SLE_VAR(Subsidy, cargo_type, SLE_UINT8),
5d0e40cd67b9 (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
  1131
	    SLE_VAR(Subsidy, age,        SLE_UINT8),
5d0e40cd67b9 (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
  1132
	SLE_CONDVAR(Subsidy, from,       SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
5d0e40cd67b9 (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
  1133
	SLE_CONDVAR(Subsidy, from,       SLE_UINT16,                5, SL_MAX_VERSION),
5d0e40cd67b9 (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
  1134
	SLE_CONDVAR(Subsidy, to,         SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
5d0e40cd67b9 (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
  1135
	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
  1136
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
  1139
static void Save_SUBS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1144
	for (i = 0; i != lengthof(_subsidies); i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
		s = &_subsidies[i];
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1146
		if (s->cargo_type != CT_INVALID) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
			SlSetArrayIndex(i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1148
			SlObject(s, _subsidies_desc);
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
  1153
static void Load_SUBS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1156
	while ((index = SlIterateArray()) != -1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
		SlObject(&_subsidies[index], _subsidies_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
7450
4034e2193a9e (svn r10209) -Fix/Codechange: forgot a few changes of int32 -> Money in the previous commit
rubidium
parents: 7449
diff changeset
  1160
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
  1161
{
6417
26acff62d001 (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: 6324
diff changeset
  1162
	const CargoSpec *cs = GetCargo(cargo_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
6954
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1164
	/* Use callback to calculate cargo profit, if available */
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1165
	if (HASBIT(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) {
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1166
		uint32 var18 = min(dist, 0xFFFF) | (min(num_pieces, 0xFF) << 16) | (transit_days << 24);
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1167
		uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs);
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1168
		if (callback != CALLBACK_FAILED) {
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1169
			int result = GB(callback, 0, 14);
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1170
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1171
			/* Simulate a 15 bit signed value */
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1172
			if (HASBIT(callback, 14)) result = 0x4000 - result;
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1173
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1174
			/* "The result should be a signed multiplier that gets multiplied
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1175
			 * by the amount of cargo moved and the price factor, then gets
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1176
			 * divided by 8192." */
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1177
			return result * num_pieces * _cargo_payment_rates[cargo_type] / 8192;
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1178
		}
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1179
	}
100c6cb85a9f (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6949
diff changeset
  1180
7824
b3984e6a6dd4 (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: 7783
diff changeset
  1181
	/* zero the distance (thus income) if it's the bank and very short transport. */
7825
62397322bde2 (svn r10692) -Fix (r10691): those good old days that LT_NORMAL was still called LT_NORMAL :)
rubidium
parents: 7824
diff changeset
  1182
	if (_opt.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
  1183
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
7824
b3984e6a6dd4 (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: 7783
diff changeset
  1185
	static const int MIN_TIME_FACTOR = 31;
b3984e6a6dd4 (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: 7783
diff changeset
  1186
	static const int MAX_TIME_FACTOR = 255;
b3984e6a6dd4 (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: 7783
diff changeset
  1187
b3984e6a6dd4 (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: 7783
diff changeset
  1188
	const int days1 = cs->transit_days[0];
b3984e6a6dd4 (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: 7783
diff changeset
  1189
	const int days2 = cs->transit_days[1];
b3984e6a6dd4 (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: 7783
diff changeset
  1190
	const int days_over_days1 = transit_days - days1;
b3984e6a6dd4 (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: 7783
diff changeset
  1191
b3984e6a6dd4 (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: 7783
diff changeset
  1192
	/*
b3984e6a6dd4 (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: 7783
diff changeset
  1193
	 * The time factor is calculated based on the time it took
b3984e6a6dd4 (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: 7783
diff changeset
  1194
	 * (transit_days) compared two cargo-depending values. The
b3984e6a6dd4 (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: 7783
diff changeset
  1195
	 * range is divided into three parts:
b3984e6a6dd4 (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: 7783
diff changeset
  1196
	 *
b3984e6a6dd4 (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: 7783
diff changeset
  1197
	 *  - constant for fast transits
b3984e6a6dd4 (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: 7783
diff changeset
  1198
	 *  - linear decreasing with time with a slope of -1 for medium transports
b3984e6a6dd4 (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: 7783
diff changeset
  1199
	 *  - linear decreasing with time with a slope of -2 for slow transports
b3984e6a6dd4 (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: 7783
diff changeset
  1200
	 *
b3984e6a6dd4 (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: 7783
diff changeset
  1201
	 */
b3984e6a6dd4 (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: 7783
diff changeset
  1202
	int time_factor;
b3984e6a6dd4 (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: 7783
diff changeset
  1203
	if (days_over_days1 <= 0) {
b3984e6a6dd4 (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: 7783
diff changeset
  1204
		time_factor = MAX_TIME_FACTOR;
b3984e6a6dd4 (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: 7783
diff changeset
  1205
	} else if (days_over_days1 <= days2) {
b3984e6a6dd4 (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: 7783
diff changeset
  1206
		time_factor = MAX_TIME_FACTOR - days_over_days1;
b3984e6a6dd4 (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: 7783
diff changeset
  1207
	} else {
b3984e6a6dd4 (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: 7783
diff changeset
  1208
		time_factor = MAX_TIME_FACTOR - 2 * days_over_days1 + days2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
7824
b3984e6a6dd4 (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: 7783
diff changeset
  1211
	if (time_factor < MIN_TIME_FACTOR) time_factor = MIN_TIME_FACTOR;
b3984e6a6dd4 (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: 7783
diff changeset
  1212
b3984e6a6dd4 (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: 7783
diff changeset
  1213
	return BIGMULSS(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
  1214
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
3344
d86c852715fa (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
  1216
static void DeliverGoodsToIndustry(TileIndex xy, CargoID cargo_type, int num_pieces)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
{
7131
d4904a021b9e (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: 7117
diff changeset
  1218
	Industry *best = NULL;
d4904a021b9e (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: 7117
diff changeset
  1219
	Industry *ind;
d4904a021b9e (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: 7117
diff changeset
  1220
	const IndustrySpec *indspec;
d4904a021b9e (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: 7117
diff changeset
  1221
	uint best_dist;
7135
8078f2b614e4 (svn r9870) -Codechange: Silence two compiler warnings and give proper type to the "type" member of industry struct
belugas
parents: 7132
diff changeset
  1222
	uint accepted_cargo_index = 0;  ///< unlikely value, just for warning removing
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1224
	/* Check if there's an industry close to the station that accepts the cargo
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1225
	 * XXX - Think of something better to
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1226
	 *       1) Only deliver to industries which are withing the catchment radius
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1227
	 *       2) Distribute between industries if more then one is present */
7131
d4904a021b9e (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: 7117
diff changeset
  1228
	best_dist = (_patches.station_spread + 8) * 2;
830
8114845804c9 (svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents: 820
diff changeset
  1229
	FOR_ALL_INDUSTRIES(ind) {
7131
d4904a021b9e (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: 7117
diff changeset
  1230
		indspec = GetIndustrySpec(ind->type);
7135
8078f2b614e4 (svn r9870) -Codechange: Silence two compiler warnings and give proper type to the "type" member of industry struct
belugas
parents: 7132
diff changeset
  1231
		uint i;
3017
915fae59d5e0 (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
  1232
7131
d4904a021b9e (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: 7117
diff changeset
  1233
		for (i = 0; i < lengthof(indspec->accepts_cargo); i++) {
7695
b15538ba7152 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
rubidium
parents: 7691
diff changeset
  1234
			if (cargo_type == indspec->accepts_cargo[i]) break;
7131
d4904a021b9e (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: 7117
diff changeset
  1235
		}
d4904a021b9e (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: 7117
diff changeset
  1236
7135
8078f2b614e4 (svn r9870) -Codechange: Silence two compiler warnings and give proper type to the "type" member of industry struct
belugas
parents: 7132
diff changeset
  1237
		/* Check if matching cargo has been found */
7131
d4904a021b9e (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: 7117
diff changeset
  1238
		if (i == lengthof(indspec->accepts_cargo)) continue;
d4904a021b9e (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: 7117
diff changeset
  1239
7695
b15538ba7152 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
rubidium
parents: 7691
diff changeset
  1240
		if (HASBIT(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) {
b15538ba7152 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
rubidium
parents: 7691
diff changeset
  1241
			uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->xy);
b15538ba7152 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
rubidium
parents: 7691
diff changeset
  1242
			if (res == 0) continue;
b15538ba7152 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
rubidium
parents: 7691
diff changeset
  1243
		}
b15538ba7152 (svn r10477) -Codechange: add some callbacks to customise the acceptance of industries.
rubidium
parents: 7691
diff changeset
  1244
7131
d4904a021b9e (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: 7117
diff changeset
  1245
		uint dist = DistanceManhattan(ind->xy, xy);
d4904a021b9e (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: 7117
diff changeset
  1246
d4904a021b9e (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: 7117
diff changeset
  1247
		if (dist < best_dist) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
			best = ind;
7131
d4904a021b9e (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: 7117
diff changeset
  1249
			best_dist = dist;
d4904a021b9e (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: 7117
diff changeset
  1250
			accepted_cargo_index = i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
	/* Found one? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
	if (best != NULL) {
7131
d4904a021b9e (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: 7117
diff changeset
  1256
		indspec = GetIndustrySpec(best->type);
7661
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7620
diff changeset
  1257
		uint16 callback = indspec->callback_flags;
7682
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
  1258
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
		best->was_cargo_delivered = true;
7682
263dbeceba12 (svn r10460) -Codechange: add some more variables needed for newindustries regarding to the creation of the industries.
rubidium
parents: 7664
diff changeset
  1260
		best->last_cargo_accepted_at = _date;
7661
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7620
diff changeset
  1261
7662
60f877c832d9 (svn r10440) -Fix: CBM_IND defines bits to be set, not bitsets.
rubidium
parents: 7661
diff changeset
  1262
		if (HASBIT(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HASBIT(callback, CBM_IND_PRODUCTION_256_TICKS)) {
7661
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7620
diff changeset
  1263
			best->incoming_cargo_waiting[accepted_cargo_index] = min(num_pieces + best->incoming_cargo_waiting[accepted_cargo_index], 0xFFFF);
7691
64063937da38 (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: 7682
diff changeset
  1264
			if (HASBIT(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) {
64063937da38 (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: 7682
diff changeset
  1265
				IndustryProductionCallback(best, 0);
64063937da38 (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: 7682
diff changeset
  1266
			} else {
64063937da38 (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: 7682
diff changeset
  1267
				InvalidateWindow(WC_INDUSTRY_VIEW, best->index);
64063937da38 (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: 7682
diff changeset
  1268
			}
7661
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7620
diff changeset
  1269
		} else {
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7620
diff changeset
  1270
			best->produced_cargo_waiting[0] = min(best->produced_cargo_waiting[0] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][0] / 256), 0xFFFF);
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7620
diff changeset
  1271
			best->produced_cargo_waiting[1] = min(best->produced_cargo_waiting[1] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][1] / 256), 0xFFFF);
40c7a63ad9a1 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium
parents: 7620
diff changeset
  1272
		}
7725
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7695
diff changeset
  1273
e3892b38aa14 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium
parents: 7695
diff changeset
  1274
		StartStopIndustryTileAnimation(best, IAT_INDUSTRY_RECEIVED_CARGO);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
3344
d86c852715fa (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
  1278
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
  1279
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1281
	TileIndex xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1284
	/* check if there is an already existing subsidy that applies to us */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1285
	for (s = _subsidies; s != endof(_subsidies); s++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
		if (s->cargo_type == cargo_type &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
				s->age >= 12 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1288
				s->from == from->index &&
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1289
				s->to == to->index) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
			return true;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1291
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
	/* check if there's a new subsidy that applies.. */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1295
	for (s = _subsidies; s != endof(_subsidies); s++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1296
		if (s->cargo_type == cargo_type && s->age < 12) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1297
			/* Check distance from source */
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1298
			const CargoSpec *cs = GetCargo(cargo_type);
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1299
			if (cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1300
				xy = GetTown(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1301
			} else {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1302
				xy = (GetIndustry(s->from))->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
			}
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1304
			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
  1305
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1306
			/* Check distance from dest */
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1307
			switch (cs->town_effect) {
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1308
				case TE_PASSENGERS:
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1309
				case TE_MAIL:
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1310
				case TE_GOODS:
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1311
				case TE_FOOD:
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1312
					xy = GetTown(s->to)->xy;
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1313
					break;
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1314
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1315
				default:
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1316
					xy = GetIndustry(s->to)->xy;
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1317
					break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
			}
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1319
			if (DistanceMax(xy, to->xy) > 9) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1320
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
			/* 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
  1322
			s->age = 12;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1323
			s->from = from->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
			s->to = to->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1325
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1326
			/* Add a news item */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
			pair = SetupSubsidyDecodeParam(s, 0);
7620
9501bd894ab2 (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: 7563
diff changeset
  1328
			InjectDParam(1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1329
7620
9501bd894ab2 (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: 7563
diff changeset
  1330
			SetDParam(0, _current_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
			AddNewsItem(
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1332
				STR_2031_SERVICE_SUBSIDY_AWARDED + _opt.diff.subsidy_multiplier,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1333
				NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0),
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1334
				pair.a, pair.b
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1335
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1336
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1337
			InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1339
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
  1344
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
  1345
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1346
	bool subsidised;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1347
	Station *s_from, *s_to;
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
  1348
	Money profit;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1349
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
  1350
	assert(num_pieces > 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1352
	/* Update player statistics */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
	{
1962
51ee4f459268 (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1935
diff changeset
  1354
		Player *p = GetPlayer(_current_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
		p->cur_economy.delivered_cargo += num_pieces;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1356
		SETBIT(p->cargo_types, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1357
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1359
	/* Get station pointers. */
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1360
	s_from = GetStation(source);
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1361
	s_to = GetStation(dest);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1363
	/* Check if a subsidy applies. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1364
	subsidised = CheckSubsidised(s_from, s_to, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1365
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1366
	/* Increase town's counter for some special goods types */
6641
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1367
	const CargoSpec *cs = GetCargo(cargo_type);
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1368
	if (cs->town_effect == TE_FOOD) s_to->town->new_act_food += num_pieces;
b195931de7fa (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6588
diff changeset
  1369
	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
  1370
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1371
	/* Give the goods to the industry. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
	DeliverGoodsToIndustry(s_to->xy, cargo_type, num_pieces);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1373
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1374
	/* Determine profit */
5934
9f91cc4e2b17 (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: 5919
diff changeset
  1375
	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
  1376
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1377
	/* Modify profit if a subsidy is in effect */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
	if (subsidised) {
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1379
		switch (_opt.diff.subsidy_multiplier) {
3655
ed8ce86fb219 (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1380
			case 0:  profit += profit >> 1; break;
ed8ce86fb219 (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1381
			case 1:  profit *= 2; break;
ed8ce86fb219 (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1382
			case 2:  profit *= 3; break;
ed8ce86fb219 (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1383
			default: profit *= 4; break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1384
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1387
	return profit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1389
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1390
/**
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1391
 * Performs the vehicle payment _and_ marks the vehicle to be unloaded.
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1392
 * @param front_v the vehicle to be unloaded
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1393
 */
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1394
void VehiclePayment(Vehicle *front_v)
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1395
{
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1396
	int result = 0;
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1397
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1398
	Money vehicle_profit = 0; // Money paid to the train
e52d89f5c7c1 (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: 7498
diff changeset
  1399
	Money route_profit   = 0; // The grand total amount for the route. A-D of transfer chain A-B-C-D
e52d89f5c7c1 (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: 7498
diff changeset
  1400
	Money virtual_profit = 0; // The virtual profit for entire vehicle chain
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1401
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1402
	StationID last_visited = front_v->last_station_visited;
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1403
	Station *st = GetStation(last_visited);
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1404
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1405
	/* The owner of the train wants to be paid */
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1406
	PlayerID old_player = _current_player;
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1407
	_current_player = front_v->owner;
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1408
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1409
	/* At this moment loading cannot be finished */
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1410
	CLRBIT(front_v->vehicle_flags, VF_LOADING_FINISHED);
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1411
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1412
	/* Start unloading in at the first possible moment */
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1413
	front_v->load_unload_time_rem = 1;
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1414
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1415
	for (Vehicle *v = front_v; v != NULL; v = v->next) {
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1416
		/* No cargo to unload */
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1417
		if (v->cargo_cap == 0 || v->cargo.Empty()) continue;
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1418
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1419
		/* All cargo has already been paid for, no need to pay again */
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1420
		if (!v->cargo.UnpaidCargo()) {
7378
bc4ce52b1932 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 7375
diff changeset
  1421
			SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
bc4ce52b1932 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 7375
diff changeset
  1422
			continue;
bc4ce52b1932 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 7375
diff changeset
  1423
		}
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1424
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1425
		GoodsEntry *ge = &st->goods[v->cargo_type];
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1426
		const CargoList::List *cargos = v->cargo.Packets();
7378
bc4ce52b1932 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 7375
diff changeset
  1427
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1428
		for (CargoList::List::const_iterator it = cargos->begin(); it != cargos->end(); it++) {
e52d89f5c7c1 (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: 7498
diff changeset
  1429
			CargoPacket *cp = *it;
e52d89f5c7c1 (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: 7498
diff changeset
  1430
			if (!cp->paid_for &&
e52d89f5c7c1 (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: 7498
diff changeset
  1431
					cp->source != last_visited &&
e52d89f5c7c1 (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: 7498
diff changeset
  1432
					ge->acceptance &&
e52d89f5c7c1 (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: 7498
diff changeset
  1433
					(front_v->current_order.flags & OF_TRANSFER) == 0) {
e52d89f5c7c1 (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: 7498
diff changeset
  1434
				/* Deliver goods to the station */
e52d89f5c7c1 (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: 7498
diff changeset
  1435
				st->time_since_unload = 0;
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1436
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1437
				/* handle end of route payment */
e52d89f5c7c1 (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: 7498
diff changeset
  1438
				Money profit = DeliverGoods(cp->count, v->cargo_type, cp->source, last_visited, cp->source_xy, cp->days_in_transit);
e52d89f5c7c1 (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: 7498
diff changeset
  1439
				cp->paid_for = true;
7932
5eb8816a872a (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: 7894
diff changeset
  1440
				route_profit   += profit; // display amount paid for final route delivery, A-D of a chain A-B-C-D
5eb8816a872a (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: 7894
diff changeset
  1441
				vehicle_profit += profit - cp->feeder_share;                    // whole vehicle is not payed for transfers picked up earlier
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1442
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1443
				result |= 1;
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1444
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1445
				SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
e52d89f5c7c1 (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: 7498
diff changeset
  1446
			} else if (front_v->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
e52d89f5c7c1 (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: 7498
diff changeset
  1447
				if (!cp->paid_for && (front_v->current_order.flags & OF_TRANSFER) != 0) {
e52d89f5c7c1 (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: 7498
diff changeset
  1448
					Money profit = GetTransportedGoodsIncome(
e52d89f5c7c1 (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: 7498
diff changeset
  1449
						cp->count,
e52d89f5c7c1 (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: 7498
diff changeset
  1450
						/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
e52d89f5c7c1 (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: 7498
diff changeset
  1451
						DistanceManhattan(cp->loaded_at_xy, GetStation(last_visited)->xy),
e52d89f5c7c1 (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: 7498
diff changeset
  1452
						cp->days_in_transit,
e52d89f5c7c1 (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: 7498
diff changeset
  1453
						v->cargo_type);
e52d89f5c7c1 (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: 7498
diff changeset
  1454
e52d89f5c7c1 (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: 7498
diff changeset
  1455
					front_v->profit_this_year += profit;
e52d89f5c7c1 (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: 7498
diff changeset
  1456
					virtual_profit   += profit; // accumulate transfer profits for whole vehicle
e52d89f5c7c1 (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: 7498
diff changeset
  1457
					cp->feeder_share += profit; // account for the (virtual) profit already made for the cargo packet
e52d89f5c7c1 (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: 7498
diff changeset
  1458
					cp->paid_for      = true;   // record that the cargo has been paid for to eliminate double counting
e52d89f5c7c1 (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: 7498
diff changeset
  1459
				}
e52d89f5c7c1 (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: 7498
diff changeset
  1460
				result |= 2;
e52d89f5c7c1 (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: 7498
diff changeset
  1461
e52d89f5c7c1 (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: 7498
diff changeset
  1462
				SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1463
			}
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1464
		}
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1465
		v->cargo.InvalidateCache();
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1466
	}
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1467
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1468
	if (virtual_profit > 0) {
e52d89f5c7c1 (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: 7498
diff changeset
  1469
		ShowFeederIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, virtual_profit);
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1470
	}
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1471
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1472
	if (route_profit != 0) {
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1473
		front_v->profit_this_year += vehicle_profit;
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1474
		SubtractMoneyFromPlayer(-route_profit);
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1475
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1476
		if (IsLocalPlayer() && !PlayVehicleSound(front_v, VSE_LOAD_UNLOAD)) {
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1477
			SndPlayVehicleFx(SND_14_CASHTILL, front_v);
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1478
		}
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1479
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1480
		ShowCostOrIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, -vehicle_profit);
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1481
	}
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1482
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1483
	_current_player = old_player;
7055
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1484
}
9118fc525e42 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6996
diff changeset
  1485
7076
3bc104d72eef (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 7061
diff changeset
  1486
/**
3bc104d72eef (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 7061
diff changeset
  1487
 * Loads/unload the vehicle if possible.
3bc104d72eef (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 7061
diff changeset
  1488
 * @param v the vehicle to be (un)loaded
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1489
 * @param cargo_left the amount of each cargo type that is
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1490
 *                   virtually left on the platform to be
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1491
 *                   picked up by another vehicle when all
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1492
 *                   previous vehicles have loaded.
7076
3bc104d72eef (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 7061
diff changeset
  1493
 */
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1494
static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1495
{
7112
fcac9e881b43 (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: 7107
diff changeset
  1496
	assert(v->current_order.type == OT_LOADING);
fcac9e881b43 (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: 7107
diff changeset
  1497
fcac9e881b43 (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: 7107
diff changeset
  1498
	/* We have not waited enough time till the next round of loading/unloading */
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1499
	if (--v->load_unload_time_rem != 0) {
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1500
		if (_patches.improved_load && HASBIT(v->current_order.flags, OFB_FULL_LOAD)) {
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1501
			/* 'Reserve' this cargo for this vehicle, because we were first. */
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1502
			for (; v != NULL; v = v->next) {
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1503
				if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo.Count();
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1504
			}
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1505
		}
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1506
		return;
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1507
	}
7112
fcac9e881b43 (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: 7107
diff changeset
  1508
7340
0ac181725a79 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 7319
diff changeset
  1509
	if (v->type == VEH_TRAIN && !IsTileType(v->tile, MP_STATION)) {
0ac181725a79 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 7319
diff changeset
  1510
		/* The train reversed in the station. Take the "easy" way
0ac181725a79 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 7319
diff changeset
  1511
		 * out and let the train just leave as it always did. */
0ac181725a79 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 7319
diff changeset
  1512
		SETBIT(v->vehicle_flags, VF_LOADING_FINISHED);
0ac181725a79 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 7319
diff changeset
  1513
		return;
0ac181725a79 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 7319
diff changeset
  1514
	}
0ac181725a79 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 7319
diff changeset
  1515
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1516
	int unloading_time = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
	Vehicle *u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
	int result = 0;
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1519
	uint cap;
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1520
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1521
	bool completely_empty  = true;
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1522
	bool anything_unloaded = false;
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1523
	bool anything_loaded   = false;
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1524
	uint32 cargo_not_full  = 0;
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1525
	uint32 cargo_full      = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
	v->cur_speed = 0;
5251
3be755f9ab99 (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
  1528
6524
44e22a9b2c97 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6516
diff changeset
  1529
	StationID last_visited = v->last_station_visited;
44e22a9b2c97 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6516
diff changeset
  1530
	Station *st = GetStation(last_visited);
44e22a9b2c97 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6516
diff changeset
  1531
499
509765981930 (svn r792) Small cleanup: Kill a goto
tron
parents: 445
diff changeset
  1532
	for (; v != NULL; v = v->next) {
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1533
		if (v->cargo_cap == 0) continue;
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1534
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1535
		byte load_amount = EngInfo(v->engine_type)->load_amount;
6553
85486d7fa259 (svn r9029) -Fix (r7326): Only call the gradual loading amount callback if the bit for it is set in the vehicle's callbackmask.
maedhros
parents: 6525
diff changeset
  1536
		if (_patches.gradual_loading && HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_LOAD_AMOUNT)) {
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1537
			uint16 cb_load_amount = GetVehicleCallback(CBID_VEHICLE_LOAD_AMOUNT, 0, 0, v->engine_type, v);
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1538
			if (cb_load_amount != CALLBACK_FAILED) load_amount = cb_load_amount & 0xFF;
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1539
		}
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1540
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1541
		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
  1542
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1543
		if (HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING)) {
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1544
			uint cargo_count = v->cargo.Count();
e52d89f5c7c1 (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: 7498
diff changeset
  1545
			uint amount_unloaded = _patches.gradual_loading ? min(cargo_count, load_amount) : cargo_count;
e52d89f5c7c1 (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: 7498
diff changeset
  1546
			bool remaining; // Are there cargo entities in this vehicle that can still be unloaded here?
5251
3be755f9ab99 (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
  1547
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1548
			if (ge->acceptance && !(u->current_order.flags & OF_TRANSFER)) {
e52d89f5c7c1 (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: 7498
diff changeset
  1549
				/* The cargo has reached it's final destination, the packets may now be destroyed */
e52d89f5c7c1 (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: 7498
diff changeset
  1550
				remaining = v->cargo.MoveTo(NULL, amount_unloaded, CargoList::MTA_FINAL_DELIVERY, last_visited);
e52d89f5c7c1 (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: 7498
diff changeset
  1551
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
				result |= 1;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1553
			} else if (u->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1554
				remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded);
6524
44e22a9b2c97 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6516
diff changeset
  1555
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
				result |= 2;
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1557
			} else {
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1558
				/* The order changed while unloading (unset unload/transfer) or the
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1559
				 * station does not accept goods anymore. */
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1560
				CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1561
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
			}
445
0e3fa3da3899 (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
  1563
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1564
			/* Deliver goods to the station */
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1565
			st->time_since_unload = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1566
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1567
			unloading_time += amount_unloaded;
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1568
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1569
			anything_unloaded = true;
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1570
			if (_patches.gradual_loading && remaining) {
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1571
				completely_empty = false;
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1572
			} else {
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1573
				/* We have finished unloading (cargo count == 0) */
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1574
				CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1575
			}
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1576
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1577
			continue;
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1578
		}
6139
e80b90da75e5 (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: 6105
diff changeset
  1579
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1580
		/* Do not pick up goods that we unloaded */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1581
		if (u->current_order.flags & OF_UNLOAD) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1583
		/* update stats */
7105
1287fa220aec (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 7097
diff changeset
  1584
		int t;
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1585
		switch (u->type) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1586
			case VEH_TRAIN: t = u->u.rail.cached_max_speed; break;
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1587
			case VEH_ROAD:  t = u->max_speed / 2;           break;
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1588
			default:        t = u->max_speed;               break;
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1589
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1590
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1591
		/* if last speed is 0, we treat that as if no vehicle has ever visited the station. */
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1592
		ge->last_speed = min(t, 255);
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
  1593
		ge->last_age = _cur_year - u->build_year;
7954
774f46ff9444 (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: 7943
diff changeset
  1594
		ge->days_since_pickup = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1596
		/* If there's goods waiting at the station, and the vehicle
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1597
		 * has capacity for it, load it on the vehicle. */
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1598
		if (!ge->cargo.Empty() &&
e52d89f5c7c1 (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: 7498
diff changeset
  1599
				(cap = v->cargo_cap - v->cargo.Count()) != 0) {
e52d89f5c7c1 (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: 7498
diff changeset
  1600
			uint count = ge->cargo.Count();
1935
f43f062c9498 (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1905
diff changeset
  1601
523
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1602
			/* Skip loading this vehicle if another train/vehicle is already handling
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1603
			 * the same cargo type at this station */
7194
b9f7550dcf17 (svn r9930) -Fix (r9838): obiwan could cause vehicles to way a long time (2.5 years) at stations.
rubidium
parents: 7139
diff changeset
  1604
			if (_patches.improved_load && cargo_left[v->cargo_type] <= 0) {
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1605
				SETBIT(cargo_not_full, v->cargo_type);
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1606
				continue;
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1607
			}
523
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1608
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1609
			if (cap > count) cap = count;
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1610
			if (_patches.gradual_loading) cap = min(cap, load_amount);
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1611
			if (_patches.improved_load) {
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1612
				/* Don't load stuff that is already 'reserved' for other vehicles */
7519
66445d420d02 (svn r10287) -Fix (r10266): some surprises from that (almost always) complaining compiler...
rubidium
parents: 7510
diff changeset
  1613
				cap = min((uint)cargo_left[v->cargo_type], cap);
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1614
				cargo_left[v->cargo_type] -= cap;
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1615
			}
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1616
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1617
			if (v->cargo.Empty()) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1618
445
0e3fa3da3899 (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
  1619
			/* TODO: Regarding this, when we do gradual loading, we
0e3fa3da3899 (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
  1620
			 * should first unload all vehicles and then start
0e3fa3da3899 (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
  1621
			 * loading them. Since this will cause
0e3fa3da3899 (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
  1622
			 * VEHICLE_TRIGGER_EMPTY to be called at the time when
0e3fa3da3899 (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
  1623
			 * the whole vehicle chain is really totally empty, the
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1624
			 * completely_empty assignment can then be safely
445
0e3fa3da3899 (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
  1625
			 * removed; that's how TTDPatch behaves too. --pasky */
0e3fa3da3899 (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
  1626
			completely_empty = false;
5251
3be755f9ab99 (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
  1627
			anything_loaded = true;
445
0e3fa3da3899 (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
  1628
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1629
			ge->cargo.MoveTo(&v->cargo, cap, CargoList::MTA_CARGO_LOAD, st->xy);
6524
44e22a9b2c97 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6516
diff changeset
  1630
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1631
			unloading_time += cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1632
			st->time_since_load = 0;
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1633
			st->last_vehicle_type = v->type;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1634
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
			result |= 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1636
		}
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1637
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1638
		if (v->cargo.Count() == v->cargo_cap) {
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1639
			SETBIT(cargo_full, v->cargo_type);
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1640
		} else {
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1641
			SETBIT(cargo_not_full, v->cargo_type);
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1642
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1645
	/* We update these variables here, so gradual loading still fills
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1646
	 * all wagons at the same time instead of using the same 'improved'
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1647
	 * loading algorithm for the wagons (only fill wagon when there is
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1648
	 * enough to fill the previous wagons) */
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1649
	if (_patches.improved_load && HASBIT(u->current_order.flags, OFB_FULL_LOAD)) {
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1650
		/* Update left cargo */
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1651
		for (v = u; v != NULL; v = v->next) {
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1652
			if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo.Count();
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1653
		}
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1654
	}
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1655
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1656
	v = u;
1935
f43f062c9498 (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1905
diff changeset
  1657
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1658
	if (anything_loaded || anything_unloaded) {
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1659
		if (_patches.gradual_loading) {
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1660
			/* The time it takes to load one 'slice' of cargo or passengers depends
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1661
			* on the vehicle type - the values here are those found in TTDPatch */
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1662
			const uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1663
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1664
			unloading_time = gradual_loading_wait_time[v->type];
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1665
		}
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1666
	} else {
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1667
		bool finished_loading = true;
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1668
		if (HASBIT(v->current_order.flags, OFB_FULL_LOAD)) {
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1669
			if (_patches.full_load_any) {
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1670
				/* if the aircraft carries passengers and is NOT full, then
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1671
				 * continue loading, no matter how much mail is in */
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1672
				if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap != v->cargo.Count()) ||
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1673
						(cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are stull non-full cargos
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1674
					finished_loading = false;
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1675
				}
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1676
			} else if (cargo_not_full != 0) {
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1677
				finished_loading = false;
5251
3be755f9ab99 (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
  1678
			}
3be755f9ab99 (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
  1679
		}
7107
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1680
		unloading_time = 20;
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1681
c8e507a51696 (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 7105
diff changeset
  1682
		SB(v->vehicle_flags, VF_LOADING_FINISHED, 1, finished_loading);
5211
651c9272ad22 (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1683
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1684
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  1685
	if (v->type == VEH_TRAIN) {
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1686
		/* Each platform tile is worth 2 rail vehicles. */
6324
ebf3649bd88f (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: 6206
diff changeset
  1687
		int overhang = v->u.rail.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
2587
d554e2a03afa (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
  1688
		if (overhang > 0) {
d554e2a03afa (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
  1689
			unloading_time <<= 1;
d554e2a03afa (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
  1690
			unloading_time += (overhang * unloading_time) / 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1693
7494
99eac2a2cd8b (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 7487
diff changeset
  1694
	/* Calculate the loading indicator fill percent and display */
99eac2a2cd8b (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 7487
diff changeset
  1695
	if (_patches.loading_indicators && _game_mode != GM_MENU && v->owner == _local_player) {
7510
acb7cfe27b60 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7506
diff changeset
  1696
		StringID percent_up_down = STR_NULL;
acb7cfe27b60 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7506
diff changeset
  1697
		int percent = CalcPercentVehicleFilled(v, &percent_up_down);
7494
99eac2a2cd8b (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 7487
diff changeset
  1698
		if (v->fill_percent_te_id == INVALID_TE_ID) {
7510
acb7cfe27b60 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7506
diff changeset
  1699
			v->fill_percent_te_id = ShowFillingPercent(v->x_pos, v->y_pos, v->z_pos + 20, percent, percent_up_down);
7494
99eac2a2cd8b (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 7487
diff changeset
  1700
		} else {
7510
acb7cfe27b60 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7506
diff changeset
  1701
			UpdateFillingPercent(v->fill_percent_te_id, percent, percent_up_down);
7494
99eac2a2cd8b (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 7487
diff changeset
  1702
		}
99eac2a2cd8b (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 7487
diff changeset
  1703
	}
99eac2a2cd8b (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 7487
diff changeset
  1704
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1705
	v->load_unload_time_rem = unloading_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1706
445
0e3fa3da3899 (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
  1707
	if (completely_empty) {
0e3fa3da3899 (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
  1708
		TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
0e3fa3da3899 (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
  1709
	}
0e3fa3da3899 (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
  1710
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1711
	if (result != 0) {
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1712
		InvalidateWindow(v->GetVehicleListWindowClass(), v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1713
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1714
7319
f884be171f04 (svn r10062) -Codechange: Don't redraw all station tiles when cargo is added or removed if the station has no custom graphics.
peter1138
parents: 7315
diff changeset
  1715
		st->MarkTilesDirty(true);
7061
81d3a68972b5 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 7055
diff changeset
  1716
		v->MarkDirty();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1718
		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
  1719
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1720
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
7112
fcac9e881b43 (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: 7107
diff changeset
  1722
/**
fcac9e881b43 (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: 7107
diff changeset
  1723
 * Load/unload the vehicles in this station according to the order
fcac9e881b43 (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: 7107
diff changeset
  1724
 * they entered.
fcac9e881b43 (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: 7107
diff changeset
  1725
 * @param st the station to do the loading/unloading for
fcac9e881b43 (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: 7107
diff changeset
  1726
 */
fcac9e881b43 (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: 7107
diff changeset
  1727
void LoadUnloadStation(Station *st)
fcac9e881b43 (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: 7107
diff changeset
  1728
{
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1729
	int cargo_left[NUM_CARGO];
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1730
7506
e52d89f5c7c1 (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: 7498
diff changeset
  1731
	for (uint i = 0; i < NUM_CARGO; i++) cargo_left[i] = st->goods[i].cargo.Count();
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1732
7112
fcac9e881b43 (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: 7107
diff changeset
  1733
	std::list<Vehicle *>::iterator iter;
fcac9e881b43 (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: 7107
diff changeset
  1734
	for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
fcac9e881b43 (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: 7107
diff changeset
  1735
		Vehicle *v = *iter;
7114
665294e21ae8 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 7112
diff changeset
  1736
		if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left);
7112
fcac9e881b43 (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: 7107
diff changeset
  1737
	}
fcac9e881b43 (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: 7107
diff changeset
  1738
}
fcac9e881b43 (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: 7107
diff changeset
  1739
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
  1740
void PlayersMonthlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1741
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1742
	PlayersGenStatistics();
4293
2c24234a7aec (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4286
diff changeset
  1743
	if (_patches.inflation && _cur_year < MAX_YEAR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1744
		AddInflation();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1745
	PlayersPayInterest();
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1746
	/* Reset the _current_player flag */
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 200
diff changeset
  1747
	_current_player = OWNER_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1748
	HandleEconomyFluctuations();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1749
	SubsidyMonthlyHandler();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1750
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1751
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
static void DoAcquireCompany(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1754
	Player *owner;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1755
	int i;
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1756
	Money value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
7554
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
  1758
	SetDParam(0, p->index);
076c42f44a83 (svn r10323) -Codechange: reference company name, number and player (president) name
peter1138
parents: 7519
diff changeset
  1759
	SetDParam(1, p->bankrupt_value);
4873
7cb19235cf60 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
  1760
	AddNewsItem( (StringID)(_current_player | NB_BMERGER), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1761
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1762
	/* original code does this a little bit differently */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1763
	PlayerID pi = p->index;
6588
73d273b7b9fc (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: 6585
diff changeset
  1764
	ChangeNetworkOwner(pi, _current_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
	ChangeOwnershipOfPlayerItems(pi, _current_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1766
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1767
	if (p->bankrupt_value == 0) {
1962
51ee4f459268 (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1935
diff changeset
  1768
		owner = GetPlayer(_current_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1769
		owner->current_loan += p->current_loan;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1770
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1771
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
	value = CalculateCompanyValue(p) >> 2;
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1773
	PlayerID old_player = _current_player;
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1774
	for (i = 0; i != 4; i++) {
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
  1775
		if (p->share_owners[i] != PLAYER_SPECTATOR) {
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1776
			SET_EXPENSES_TYPE(EXPENSES_OTHER);
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1777
			_current_player = p->share_owners[i];
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1778
			SubtractMoneyFromPlayer(CommandCost(-value));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1779
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1780
	}
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1781
	_current_player = old_player;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1782
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1783
	p->is_active = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1784
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1785
	DeletePlayerWindows(pi);
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4423
diff changeset
  1786
	RebuildVehicleLists(); //Updates the open windows to add the newly acquired vehicles to the lists
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1788
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1789
extern int GetAmountOwnedBy(const Player *p, PlayerID owner);
599
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1790
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1791
/** Acquire shares in an opposing company.
3491
4c8427796c64 (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
  1792
 * @param tile unused
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6901
diff changeset
  1793
 * @param flags type of operation
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1794
 * @param p1 player to buy the shares from
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1795
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1796
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7378
diff changeset
  1797
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
  1798
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
	Player *p;
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7378
diff changeset
  1800
	CommandCost cost;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1801
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1802
	/* Check if buying shares is allowed (protection against modified clients */
4850
b4e9be22945f (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4848
diff changeset
  1803
	if (!IsValidPlayer((PlayerID)p1) || !_patches.allow_shares) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1804
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1805
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1806
	p = GetPlayer((PlayerID)p1);
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
  1807
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1808
	/* Protect new companies from hostile takeovers */
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
  1809
	if (_cur_year - p->inaugurated_year < 6) return_cmd_error(STR_7080_PROTECTED);
930
524b02923ede (svn r1418) -Feature: [1098254] (dis)Allow Shares. Add patch options to allow buying/selling of shares (Hackykid)
darkvater
parents: 919
diff changeset
  1810
599
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1811
	/* Those lines are here for network-protection (clients can be slow) */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1812
	if (GetAmountOwnedBy(p, PLAYER_SPECTATOR) == 0) return cost;
930
524b02923ede (svn r1418) -Feature: [1098254] (dis)Allow Shares. Add patch options to allow buying/selling of shares (Hackykid)
darkvater
parents: 919
diff changeset
  1813
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1814
	/* We can not buy out a real player (temporarily). TODO: well, enable it obviously */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1815
	if (GetAmountOwnedBy(p, PLAYER_SPECTATOR) == 1 && !p->is_ai) return cost;
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
  1816
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1817
	cost.AddCost(CalculateCompanyValue(p) >> 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1818
	if (flags & DC_EXEC) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1819
		PlayerByte* b = p->share_owners;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1820
		int i;
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1821
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
  1822
		while (*b != PLAYER_SPECTATOR) b++; /* share owners is guaranteed to contain at least one PLAYER_SPECTATOR */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1823
		*b = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1824
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1825
		for (i = 0; p->share_owners[i] == _current_player;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1826
			if (++i == 4) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
				p->bankrupt_value = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1828
				DoAcquireCompany(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1829
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1830
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1831
		}
3017
915fae59d5e0 (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
  1832
		InvalidateWindow(WC_COMPANY, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1833
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1834
	return cost;
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
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1837
/** Sell shares in an opposing company.
3491
4c8427796c64 (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
  1838
 * @param tile unused
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6901
diff changeset
  1839
 * @param flags type of operation
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1840
 * @param p1 player to sell the shares from
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1841
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1842
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7378
diff changeset
  1843
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
  1844
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
	Player *p;
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1846
	Money cost;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1847
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1848
	/* Check if buying shares is allowed (protection against modified clients */
4850
b4e9be22945f (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4848
diff changeset
  1849
	if (!IsValidPlayer((PlayerID)p1) || !_patches.allow_shares) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1850
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1851
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1852
	p = GetPlayer((PlayerID)p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
653
3c902c647791 (svn r1086) -Fix: [Network] [ 1084774 ] Fixed quick-share-sell bug
truelight
parents: 630
diff changeset
  1854
	/* Those lines are here for network-protection (clients can be slow) */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1855
	if (GetAmountOwnedBy(p, _current_player) == 0) return CommandCost();
653
3c902c647791 (svn r1086) -Fix: [Network] [ 1084774 ] Fixed quick-share-sell bug
truelight
parents: 630
diff changeset
  1856
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
	/* adjust it a little to make it less profitable to sell and buy */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1858
	cost = CalculateCompanyValue(p) >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1859
	cost = -(cost - (cost >> 7));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1860
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1861
	if (flags & DC_EXEC) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1862
		PlayerByte* b = p->share_owners;
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1863
		while (*b != _current_player) b++; // share owners is guaranteed to contain player
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
  1864
		*b = PLAYER_SPECTATOR;
3017
915fae59d5e0 (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
  1865
		InvalidateWindow(WC_COMPANY, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1866
	}
7449
5cedaf2c861c (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium
parents: 7448
diff changeset
  1867
	return CommandCost(cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1869
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1870
/** Buy up another company.
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1871
 * When a competing company is gone bankrupt you get the chance to purchase
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1872
 * that company.
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1873
 * @todo currently this only works for AI players
3491
4c8427796c64 (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
  1874
 * @param tile unused
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6901
diff changeset
  1875
 * @param flags type of operation
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1876
 * @param p1 player/company to buy up
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1877
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1878
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7378
diff changeset
  1879
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
  1880
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1881
	Player *p;
6901
2201a832ae42 (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: 6899
diff changeset
  1882
	PlayerID pid = (PlayerID)p1;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1883
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1884
	/* Disable takeovers in multiplayer games */
6901
2201a832ae42 (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: 6899
diff changeset
  1885
	if (!IsValidPlayer(pid) || _networking) return CMD_ERROR;
2201a832ae42 (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: 6899
diff changeset
  1886
2201a832ae42 (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: 6899
diff changeset
  1887
	/* Do not allow players to take over themselves */
2201a832ae42 (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: 6899
diff changeset
  1888
	if (pid == _current_player) return CMD_ERROR;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1889
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1890
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
6901
2201a832ae42 (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: 6899
diff changeset
  1891
	p = GetPlayer(pid);
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1892
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1893
	if (!p->is_ai) return CMD_ERROR;
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1894
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1895
	if (flags & DC_EXEC) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1896
		DoAcquireCompany(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1897
	}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
  1898
	return CommandCost(p->bankrupt_value);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1900
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1901
/** Prices */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
  1902
static void SaveLoad_PRIC()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1903
{
7451
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1904
	int vt = CheckSavegameVersion(65) ? (SLE_FILE_I32 | SLE_VAR_I64) : SLE_INT64;
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1905
	SlArray(&_price,      NUM_PRICES, vt);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1906
	SlArray(&_price_frac, NUM_PRICES, SLE_UINT16);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1907
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1908
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1909
/** Cargo payment rates */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
  1910
static void SaveLoad_CAPR()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1911
{
6959
0066ab0d9b93 (svn r9638) -Feature: Increase cargo types from 12 to 32 and enable newcargo flag in NewGRF loader.
peter1138
parents: 6954
diff changeset
  1912
	uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO;
7451
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1913
	int vt = CheckSavegameVersion(65) ? (SLE_FILE_I32 | SLE_VAR_I64) : SLE_INT64;
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1914
	SlArray(&_cargo_payment_rates,      num_cargo, vt);
6959
0066ab0d9b93 (svn r9638) -Feature: Increase cargo types from 12 to 32 and enable newcargo flag in NewGRF loader.
peter1138
parents: 6954
diff changeset
  1915
	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
  1916
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1917
1881
023a134a4b12 (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
  1918
static const SaveLoad _economy_desc[] = {
7451
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1919
	SLE_CONDVAR(Economy, max_loan,         SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1920
	SLE_CONDVAR(Economy, max_loan,         SLE_INT64,                  65, SL_MAX_VERSION),
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1921
	SLE_CONDVAR(Economy, max_loan_unround, SLE_FILE_I32 | SLE_VAR_I64,  0, 64),
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1922
	SLE_CONDVAR(Economy, max_loan_unround, SLE_INT64,                  65, SL_MAX_VERSION),
7740
31ef7e9cc0c0 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium
parents: 7725
diff changeset
  1923
	SLE_CONDVAR(Economy, max_loan_unround_fract, SLE_UINT16,           70, SL_MAX_VERSION),
7451
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1924
	    SLE_VAR(Economy, fluct,            SLE_FILE_I16 | SLE_VAR_I32),
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1925
	    SLE_VAR(Economy, interest_rate,    SLE_UINT8),
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1926
	    SLE_VAR(Economy, infl_amount,      SLE_UINT8),
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1927
	    SLE_VAR(Economy, infl_amount_pr,   SLE_UINT8),
82101e591f90 (svn r10210) -Codechange: make all money related variables 64 bits, so overflowing them should become a little harder.
rubidium
parents: 7450
diff changeset
  1928
	    SLE_END()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1929
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1930
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6445
diff changeset
  1931
/** Economy variables */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6553
diff changeset
  1932
static void SaveLoad_ECMY()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1933
{
1881
023a134a4b12 (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
  1934
	SlObject(&_economy, _economy_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1935
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1936
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1937
extern const ChunkHandler _economy_chunk_handlers[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1938
	{ '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
  1939
	{ 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
4344
5d0e40cd67b9 (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
  1940
	{ 'SUBS', Save_SUBS,     Load_SUBS,     CH_ARRAY},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1941
	{ 'ECMY', SaveLoad_ECMY, SaveLoad_ECMY, CH_RIFF | CH_LAST},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1942
};