src/economy.cpp
author rubidium
Mon, 18 Jun 2007 21:00:14 +0000
changeset 6952 b19643469024
parent 6950 14ecb0acdfb4
child 6953 230d1e3ac86c
permissions -rw-r--r--
(svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
     3
/** @file economy.cpp */
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1884
diff changeset
     6
#include "openttd.h"
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents: 2272
diff changeset
     7
#include "currency.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#include "functions.h"
6453
226bcddeba32 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6432
diff changeset
     9
#include "landscape.h"
1309
4403a69da4f8 (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
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 499
diff changeset
    11
#include "table/strings.h"
2218
2132596a35c0 (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
04ca2cd69420 (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"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5426
diff changeset
    25
#include "network/network.h"
337
cbe0c766c947 (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
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
    27
#include "engine.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5426
diff changeset
    28
#include "network/network_data.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    29
#include "variables.h"
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    30
#include "vehicle_gui.h"
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2306
diff changeset
    31
#include "ai/ai.h"
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2642
diff changeset
    32
#include "train.h"
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6844
diff changeset
    33
#include "roadveh.h"
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5683
diff changeset
    34
#include "aircraft.h"
2962
f0a49b646c48 (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2952
diff changeset
    35
#include "newgrf_engine.h"
4701
9e83e5ae6de7 (svn r6611) - Newsounds:
peter1138
parents: 4621
diff changeset
    36
#include "newgrf_sound.h"
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
    37
#include "newgrf_callbacks.h"
3386
6d088afdabf4 (svn r4194) -Codechange: Renamed UpdatePlayerHouse to UpdateCompanyHQ because the rest of the code calls it Comapany HQ. Moved it to unmovable_cmd.c because CompanyHQ is an unmovable and has nothing to do with economy.
celestar
parents: 3347
diff changeset
    38
#include "unmovable.h"
4261
28670f743746 (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4260
diff changeset
    39
#include "date.h"
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
    40
#include "cargotype.h"
6190
01a2b579b668 (svn r8969) -Codechange: rework of the player face bits.
rubidium
parents: 6175
diff changeset
    41
#include "player_face.h"
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6639
diff changeset
    42
#include "group.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
    44
/* Score info */
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    45
const ScoreInfo _score_info[] = {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    46
	{ SCORE_VEHICLES,        120, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    47
	{ SCORE_STATIONS,         80, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    48
	{ SCORE_MIN_PROFIT,    10000, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    49
	{ SCORE_MIN_INCOME,    50000,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    50
	{ SCORE_MAX_INCOME,   100000, 100 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    51
	{ SCORE_DELIVERED,     40000, 400 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    52
	{ SCORE_CARGO,             8,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    53
	{ SCORE_MONEY,      10000000,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    54
	{ SCORE_LOAN,         250000,  50 },
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
    55
	{ SCORE_TOTAL,             0,   0 }
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    56
};
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    57
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    58
int _score_part[MAX_PLAYERS][SCORE_END];
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
    59
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
    60
int64 CalculateCompanyValue(const Player* p)
2475
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
    61
{
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
    62
	PlayerID owner = p->index;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
    63
	int64 value;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
    64
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
		Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
		uint num = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
    68
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
		FOR_ALL_STATIONS(st) {
6879
6f2927f456d7 (svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.
maedhros
parents: 6876
diff changeset
    70
			if (st->owner == owner) num += CountBitsSet(st->facilities);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
		value = num * _price.station_value * 25;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
		Vehicle *v;
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
		FOR_ALL_VEHICLES(v) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    80
			if (v->owner != owner) continue;
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    81
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
    82
			if (v->type == VEH_TRAIN ||
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
    83
					v->type == VEH_ROAD ||
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
    84
					(v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) ||
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
    85
					v->type == VEH_SHIP) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
				value += v->value * 3 >> 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
6952
b19643469024 (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 6950
diff changeset
    91
	value += p->player_money - p->current_loan; // add real money value
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
    92
5601
d58f82901b2f (svn r8055) -Codechange: Replace the different max, dmax, maxu whatever macros by a simple template function max(), that requires two arguments of the same type. While I'm at it change a variable called "max" to "maxval" in a function that calls max().
celestar
parents: 5587
diff changeset
    93
	return max(value, 1LL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
    96
/** if update is set to true, the economy is updated with this score
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
    97
 *  (also the house is updated, should only be true in the on-tick event)
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
    98
 * @param update the economy with calculated score
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
    99
 * @param p player been evaluated
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   100
 * @return actual score of this player
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   101
 * */
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
   102
int UpdateCompanyRatingAndValue(Player *p, bool update)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
	byte owner = p->index;
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   105
	int score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   106
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   107
	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
   108
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
/* Count vehicles */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
		Vehicle *v;
2829
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   112
		int32 min_profit = 0;
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   113
		bool min_profit_first = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
		uint num = 0;
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
		FOR_ALL_VEHICLES(v) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   117
			if (v->owner != owner) continue;
6773
bc98b0b16ec4 (svn r10009) -Codechange: Add and use Vehicle::IsPrimaryVehicle to replace individual checks depending on the vehicle type.
maedhros
parents: 6698
diff changeset
   118
			if (IsPlayerBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
				num++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
				if (v->age > 730) {
2829
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   121
					/* Find the vehicle with the lowest amount of profit */
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   122
					if (min_profit_first == true) {
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   123
						min_profit = v->profit_last_year;
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   124
						min_profit_first = false;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   125
					} else if (min_profit > v->profit_last_year) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
						min_profit = v->profit_last_year;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   127
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   132
		_score_part[owner][SCORE_VEHICLES] = num;
2829
fd54aa44d6e0 (svn r3377) -Fix: [ FS#24 ] Min. profit in detail performance rating gave you full points if you had no vehicles, and was limited to 20k. Solved both. (Partly based on Graphite his patch)
truelight
parents: 2815
diff changeset
   133
		/* Don't allow negative min_profit to show */
1407
7d3702835802 (svn r1911) Put back an "if" i accidently removed in r1898
tron
parents: 1394
diff changeset
   134
		if (min_profit > 0)
7d3702835802 (svn r1911) Put back an "if" i accidently removed in r1898
tron
parents: 1394
diff changeset
   135
			_score_part[owner][SCORE_MIN_PROFIT] = min_profit;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
/* Count stations */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
		uint num = 0;
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   141
		const Station* st;
0
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
		FOR_ALL_STATIONS(st) {
6879
6f2927f456d7 (svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.
maedhros
parents: 6876
diff changeset
   144
			if (st->owner == owner) num += CountBitsSet(st->facilities);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
		}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   146
		_score_part[owner][SCORE_STATIONS] = num;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
/* Generate statistics depending on recent income statistics */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
	{
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   151
		const PlayerEconomyEntry* pee;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
		int numec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
		int32 min_income;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   154
		int32 max_income;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
		numec = min(p->num_valid_stat_ent, 12);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
		if (numec != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
			min_income = 0x7FFFFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
			max_income = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
			pee = p->old_economy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
			do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
				min_income = min(min_income, pee->income + pee->expenses);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
				max_income = max(max_income, pee->income + pee->expenses);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
			} while (++pee,--numec);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
			if (min_income > 0)
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   167
				_score_part[owner][SCORE_MIN_INCOME] = min_income;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   169
			_score_part[owner][SCORE_MAX_INCOME] = max_income;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
		}
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
/* Generate score depending on amount of transported cargo */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
	{
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   175
		const PlayerEconomyEntry* pee;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		int numec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
		uint32 total_delivered;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
		numec = min(p->num_valid_stat_ent, 4);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
		if (numec != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
			pee = p->old_economy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
			total_delivered = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
			do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
				total_delivered += pee->delivered_cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
			} while (++pee,--numec);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   187
			_score_part[owner][SCORE_DELIVERED] = total_delivered;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
	}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   190
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
/* Generate score for variety of cargo */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
	{
6879
6f2927f456d7 (svn r10122) -Codechange: Add a CountBitsSet function and use it to replace some less efficient loops.
maedhros
parents: 6876
diff changeset
   193
		uint num = CountBitsSet(p->cargo_types);
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   194
		_score_part[owner][SCORE_CARGO] = num;
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   195
		if (update) p->cargo_types = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
	}
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
/* Generate score for player money */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
		int32 money = p->player_money;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
		if (money > 0) {
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   202
			_score_part[owner][SCORE_MONEY] = 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
	{
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
   208
		_score_part[owner][SCORE_LOAN] = _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
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
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;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   216
		for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   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;
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   219
			/*  Check the score */
2261
d3554e5d3e86 (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) ?
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2218
diff changeset
   221
				_score_info[i].score :
3033
9cba043eb38f (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
d3554e5d3e86 (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
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   230
		/*  We always want the score scaled to SCORE_MAX (1000) */
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3017
diff changeset
   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
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   235
		p->old_economy[0].performance_history = score;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   236
		UpdateCompanyHQ(p, score);
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
   237
		p->old_economy[0].company_value = CalculateCompanyValue(p);
d4d440dd8925 (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
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   244
/*  use PLAYER_SPECTATOR as new_player to delete the player. */
1797
fa156be69235 (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
abfa5fac015f (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   247
	Town *t;
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   248
	PlayerID old = _current_player;
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   249
6405
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
   250
	assert(old_player != new_player);
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
   251
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   252
	{
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   253
		Player *p;
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   254
		uint i;
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   255
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   256
		/* See if the old_player had shares in other companies */
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   257
		_current_player = old_player;
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   258
		FOR_ALL_PLAYERS(p) {
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   259
			for (i = 0; i < 4; i++) {
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   260
				if (p->share_owners[i] == old_player) {
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   261
					/* Sell his shares */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
   262
					CommandCost res = DoCommand(0, p->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   263
					/* Because we are in a DoCommand, we can't just execute an other one and
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   264
					 *  expect the money to be removed. We need to do it ourself! */
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   265
					SubtractMoneyFromPlayer(res);
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   266
				}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   267
			}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   268
		}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   269
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   270
		/* Sell all the shares that people have on this company */
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   271
		p = GetPlayer(old_player);
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   272
		for (i = 0; i < 4; i++) {
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   273
			_current_player = p->share_owners[i];
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   274
			if (_current_player != PLAYER_SPECTATOR) {
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   275
				/* Sell the shares */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
   276
				CommandCost res = DoCommand(0, old_player, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
6403
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   277
				/* Because we are in a DoCommand, we can't just execute an other one and
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   278
				 *  expect the money to be removed. We need to do it ourself! */
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   279
				SubtractMoneyFromPlayer(res);
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   280
			}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   281
		}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   282
	}
56a00bf0531b (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight
parents: 6369
diff changeset
   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
c638c6441121 (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
106ed18a7675 (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.
106ed18a7675 (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
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   289
	if (new_player == PLAYER_SPECTATOR) {
6952
b19643469024 (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 6950
diff changeset
   290
		GetPlayer(old_player)->player_money = MAX_UVALUE(uint64) >> 2; // jackpot ;p
4260
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   291
	}
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   292
4848
45f848b46222 (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
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   296
		for (s = _subsidies; s != endof(_subsidies); s++) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   297
			if (s->cargo_type != CT_INVALID && s->age >= 12) {
4077
d4d440dd8925 (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
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   303
	/* Take care of rating in towns */
5284
abfa5fac015f (svn r7433) -Fix (r2301): Town ratings were not reset when a company went bankrupt.
peter1138
parents: 5251
diff changeset
   304
	FOR_ALL_TOWNS(t) {
abfa5fac015f (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. */
abfa5fac015f (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
66105d4f6e83 (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
c638c6441121 (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)) {
c638c6441121 (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.
c638c6441121 (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]);
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   311
				} else {
c638c6441121 (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);
c638c6441121 (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
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   314
				}
4260
c638c6441121 (svn r5886) -Fix [FS#273]: Incomplete removal of player owned property due to lack of money.
Darkvater
parents: 4077
diff changeset
   315
			}
5284
abfa5fac015f (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
abfa5fac015f (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 */
abfa5fac015f (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;
abfa5fac015f (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
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
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
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   333
				switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   334
					case VEH_TRAIN:    if (IsFrontEngine(v)) num_train++; break;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6844
diff changeset
   335
					case VEH_ROAD:     if (IsRoadVehFront(v)) num_road++; break;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   336
					case VEH_SHIP:     num_ship++; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   337
					case VEH_AIRCRAFT: if (IsNormalAircraft(v)) num_aircraft++; break;
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1793
diff changeset
   338
					default: break;
fa156be69235 (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) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   344
			if (v->owner == old_player && IS_BYTE_INSIDE(v->type, VEH_TRAIN, VEH_AIRCRAFT + 1)) {
4848
45f848b46222 (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);
6876
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   349
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   350
					if (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && IsFreeWagon(v))) {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   351
						switch (v->type) {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   352
							default: NOT_REACHED();
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   353
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   354
							case VEH_TRAIN: {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   355
								Vehicle *u = v;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   356
								do {
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   357
									Vehicle *next = GetNextVehicle(u);
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   358
									DeleteVehicle(u);
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   359
									u = next;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   360
								} while (u != NULL);
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   361
							} break;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   362
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   363
							case VEH_ROAD:
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   364
							case VEH_SHIP:
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   365
								DeleteVehicle(v);
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   366
								break;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   367
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   368
							case VEH_AIRCRAFT:
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   369
								DeleteVehicleChain(v);
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   370
								break;
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
diff changeset
   371
						}
22305fa54101 (svn r10117) -Fix [FS#863]: When deleting the vehicles of bankrupt players, delete trains as
maedhros
parents: 6857
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;
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6639
diff changeset
   375
					v->group_id = DEFAULT_GROUP;
4621
47100a78dbd3 (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
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   377
					switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   378
						case VEH_TRAIN:    if (IsFrontEngine(v)) v->unitnumber = ++num_train; break;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6844
diff changeset
   379
						case VEH_ROAD:     if (IsRoadVehFront(v)) v->unitnumber = ++num_road; break;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   380
						case VEH_SHIP:     v->unitnumber = ++num_ship; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   381
						case VEH_AIRCRAFT: if (IsNormalAircraft(v)) v->unitnumber = ++num_aircraft; break;
6621
441559124aea (svn r9841) -Codechange: add a little more type strictness to the vehicle types.
rubidium
parents: 6620
diff changeset
   382
						default: NOT_REACHED();
2989
916f9443345f (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
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
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
fa156be69235 (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
6a1444534f62 (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
5a601eb01735 (svn r7138) -Fix: [vehicle list windows] fixed a rare crash where having some (not all) vehicle list windows open for a player, that goes bankrupt would crash the game
bjarni
parents: 5067
diff changeset
   397
	/* Change color of existing windows */
5a601eb01735 (svn r7138) -Fix: [vehicle list windows] fixed a rare crash where having some (not all) vehicle list windows open for a player, that goes bankrupt would crash the game
bjarni
parents: 5067
diff changeset
   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
6262
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   405
static void ChangeNetworkOwner(PlayerID current_player, PlayerID new_player)
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   406
{
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   407
#ifdef ENABLE_NETWORK
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   408
	if (!_networking) return;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   409
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   410
	if (current_player == _local_player) {
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   411
		_network_playas = new_player;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   412
		SetLocalPlayer(new_player);
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   413
	}
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   414
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   415
	if (!_network_server) return;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   416
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   417
	/* The server has to handle all administrative issues, for example
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   418
	* updating and notifying all clients of what has happened */
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   419
	NetworkTCPSocketHandler *cs;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   420
	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   421
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   422
	/* The server has just changed from player */
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   423
	if (current_player == ci->client_playas) {
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   424
		ci->client_playas = new_player;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   425
		NetworkUpdateClientInfo(NETWORK_SERVER_INDEX);
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   426
	}
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   427
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   428
	/* Find all clients that were in control of this company, and mark them as new_player */
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   429
	FOR_ALL_CLIENTS(cs) {
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   430
		ci = DEREF_CLIENT_INFO(cs);
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   431
		if (current_player == ci->client_playas) {
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   432
			ci->client_playas = new_player;
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   433
			NetworkUpdateClientInfo(ci->client_index);
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   434
		}
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   435
	}
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   436
#endif /* ENABLE_NETWORK */
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   437
}
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   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
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2469
diff changeset
   441
	PlayerID owner;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
   442
	int64 val;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   444
	/*  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
   445
	if (p->player_money >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
		p->quarters_of_bankrupcy = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
		return;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
	p->quarters_of_bankrupcy++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
	owner = p->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   454
	switch (p->quarters_of_bankrupcy) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   455
		case 2:
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   456
			AddNewsItem( (StringID)(owner | NB_BTROUBLE),
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   457
				NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   458
			break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   459
		case 3: {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   460
			/* XXX - In multiplayer, should we ask other players if it wants to take
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   461
		          over when it is a human company? -- TrueLight */
4845
72bbb6e2694e (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4814
diff changeset
   462
			if (IsHumanPlayer(owner)) {
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   463
				AddNewsItem( (StringID)(owner | NB_BTROUBLE),
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   464
					NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   465
				break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   466
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   468
			/* Check if the company has any value.. if not, declare it bankrupt
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   469
			 *  right now */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   470
			val = CalculateCompanyValue(p);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   471
			if (val > 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   472
				p->bankrupt_value = val;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   473
				p->bankrupt_asked = 1 << owner; // Don't ask the owner
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   474
				p->bankrupt_timeout = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   475
				break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   476
			}
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   477
			/* Else, falltrue to case 4... */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   479
		case 4: {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   480
			/* Close everything the owner has open */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   481
			DeletePlayerWindows(owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   482
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   483
			/* Show bankrupt news */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   484
			SetDParam(0, p->name_1);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   485
			SetDParam(1, p->name_2);
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   486
			AddNewsItem( (StringID)(owner | NB_BBANKRUPT), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   487
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   488
			if (IsHumanPlayer(owner)) {
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   489
				/* XXX - If we are in offline mode, leave the player playing. Eg. there
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   490
				 * is no THE-END, otherwise mark the player as spectator to make sure
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   491
				 * he/she is no long in control of this company */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   492
				if (!_networking) {
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   493
					p->bankrupt_asked = 0xFF;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   494
					p->bankrupt_timeout = 0x456;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   495
					break;
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   496
				}
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   497
6262
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
   498
				ChangeNetworkOwner(owner, PLAYER_SPECTATOR);
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   499
			}
687
0e42e732ad78 (svn r1128) -Fix: [Network] Going bankrupt of human-players was corrupted in networkgames
truelight
parents: 679
diff changeset
   500
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   501
			/* Remove the player */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   502
			ChangeOwnershipOfPlayerItems(owner, PLAYER_SPECTATOR);
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   503
			/* Register the player as not-active */
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   504
			p->is_active = false;
2395
d1629f64d157 (svn r2921) -Codechange: moved all AI-code to 1 central place (ai/ai.c)
truelight
parents: 2306
diff changeset
   505
5067
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   506
			if (!IsHumanPlayer(owner) && (!_networking || _network_server) && _ai.enabled)
f3816e74ad32 (svn r7125) -Fix: Several errors/glitches related to multiplayer and bankrupcy, mainly such a
Darkvater
parents: 4880
diff changeset
   507
				AI_PlayerDied(owner);
0
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
void DrawNewsBankrupcy(Window *w)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   516
	DrawNewsBorder(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   518
	p = GetPlayer((PlayerID)GB(WP(w,news_d).ni->string_id, 0, 4));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
	DrawPlayerFace(p->face, p->player_color, 2, 23);
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5665
diff changeset
   520
	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
   521
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   522
	SetDParam(0, p->president_name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   523
	SetDParam(1, p->president_name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
	DrawStringMultiCenter(49, 148, STR_7058_PRESIDENT, 94);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   527
	switch (WP(w,news_d).ni->string_id & 0xF0) {
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   528
	case NB_BTROUBLE:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
		DrawStringCentered(w->width>>1, 1, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   531
		SetDParam(0, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   532
		SetDParam(1, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
			STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   541
	case NB_BMERGER: {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
		int32 price;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
		DrawStringCentered(w->width>>1, 1, STR_7059_TRANSPORT_COMPANY_MERGER, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
		COPY_IN_DPARAM(0,WP(w,news_d).ni->params, 2);
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   546
		SetDParam(2, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   547
		SetDParam(3, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
		price = WP(w,news_d).ni->params[2];
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   549
		SetDParam(4, price);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
			price==0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   558
	case NB_BBANKRUPT:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
		DrawStringCentered(w->width>>1, 1, STR_705C_BANKRUPT, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
		COPY_IN_DPARAM(0,WP(w,news_d).ni->params, 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
			STR_705D_HAS_BEEN_CLOSED_DOWN_BY,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
			w->width - 101);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   566
		break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   568
	case NB_BNEWCOMPANY:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
		DrawStringCentered(w->width>>1, 1, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED, 0);
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   570
		SetDParam(0, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   571
		SetDParam(1, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
		COPY_IN_DPARAM(2,WP(w,news_d).ni->params, 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
			STR_705F_STARTS_CONSTRUCTION_NEAR,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
	default:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
		NOT_REACHED();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2425
diff changeset
   585
StringID GetNewsStringBankrupcy(const NewsItem *ni)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   587
	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
   588
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   589
	switch (ni->string_id & 0xF0) {
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   590
	case NB_BTROUBLE:
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   591
		SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   592
		SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   593
		SetDParam(2, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   594
		SetDParam(3, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
		return STR_02B6;
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   596
	case NB_BMERGER:
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   597
		SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   598
		SetDParam(1, STR_705A_HAS_BEEN_SOLD_TO_FOR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
		COPY_IN_DPARAM(2,ni->params, 2);
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   600
		SetDParam(4, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   601
		SetDParam(5, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
		COPY_IN_DPARAM(6,ni->params + 2, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
		return STR_02B6;
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   604
	case NB_BBANKRUPT:
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   605
		SetDParam(0, STR_705C_BANKRUPT);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   606
		SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
		COPY_IN_DPARAM(2,ni->params, 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
		return STR_02B6;
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
   609
	case NB_BNEWCOMPANY:
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   610
		SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   611
		SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   612
		SetDParam(2, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   613
		SetDParam(3, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
		COPY_IN_DPARAM(4,ni->params, 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
		return STR_02B6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	default:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
		NOT_REACHED();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   619
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
	/* useless, but avoids compiler warning this way */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   624
static void PlayersGenStatistics()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
	FOR_ALL_STATIONS(st) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   630
		_current_player = st->owner;
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   631
		SET_EXPENSES_TYPE(EXPENSES_PROPERTY);
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   632
		SubtractMoneyFromPlayer(_price.station_value >> 1);
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
	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
   636
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
	FOR_ALL_PLAYERS(p) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
		if (p->is_active) {
5426
d3d5733c5e3a (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5378
diff changeset
   640
			memmove(&p->old_economy[1], &p->old_economy[0], sizeof(p->old_economy) - sizeof(p->old_economy[0]));
d3d5733c5e3a (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5378
diff changeset
   641
			p->old_economy[0] = p->cur_economy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
			memset(&p->cur_economy, 0, sizeof(p->cur_economy));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
5426
d3d5733c5e3a (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5378
diff changeset
   644
			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
   645
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   646
			UpdateCompanyRatingAndValue(p, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
			PlayersCheckBankrupt(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
5426
d3d5733c5e3a (svn r7630) -Fix: At the end of the quarter the old economy numbers were shifted up, and
Darkvater
parents: 5378
diff changeset
   649
			if (p->block_preview != 0) p->block_preview--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
	InvalidateWindow(WC_INCOME_GRAPH, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
	InvalidateWindow(WC_OPERATING_PROFIT, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
	InvalidateWindow(WC_DELIVERED_CARGO, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
	InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
	InvalidateWindow(WC_COMPANY_VALUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
	InvalidateWindow(WC_COMPANY_LEAGUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
static void AddSingleInflation(int32 *value, uint16 *frac, int32 amt)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
{
6119
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   663
	int64 tmp = (int64)*value * amt + *frac;
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   664
	*frac   = GB(tmp, 0, 16);
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   665
	*value += tmp >> 16;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   668
static void AddInflation()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   669
{
6119
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   670
	/* Approximation for (100 + infl_amount)% ** (1 / 12) - 100%
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   671
	 * scaled by 65536
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   672
	 * 12 -> months per year
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   673
	 * This is only a good approxiamtion for small values
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   674
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
	int32 inf = _economy.infl_amount * 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
6119
bfe2e29619ba (svn r8855) -Fix
tron
parents: 6111
diff changeset
   677
	for (uint i = 0; i != NUM_PRICES; i++) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
   678
		AddSingleInflation((int32*)&_price + i, _price_frac + i, inf);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
	_economy.max_loan_unround += BIGMULUS(_economy.max_loan_unround, inf, 16);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   682
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
	if (_economy.max_loan + 50000 <= _economy.max_loan_unround)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
		_economy.max_loan += 50000;
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
	inf = _economy.infl_amount_pr * 54;
6350
04b19f551aec (svn r9388) -Codechange: variable scope and type, and standardify all CargoID loops.
peter1138
parents: 6315
diff changeset
   687
	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
   688
		AddSingleInflation(
1884
804ff5f94ed0 (svn r2390) - Codechange: Fix some warnings on GCC 4.0.0
hackykid
parents: 1881
diff changeset
   689
			(int32*)_cargo_payment_rates + i,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
			_cargo_payment_rates_frac + i,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
			inf
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
		);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
1098
dac6a13ff91d (svn r1599) fix: autoreplace window now updates when inflation alters prices
bjarni
parents: 1093
diff changeset
   696
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
	InvalidateWindowClasses(WC_VEHICLE_DETAILS);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
	InvalidateWindow(WC_PAYMENT_RATES, 0);
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
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   701
static void PlayersPayInterest()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
{
2548
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2544
diff changeset
   703
	const Player* p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	int interest = _economy.interest_rate * 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
	FOR_ALL_PLAYERS(p) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
   707
		if (!p->is_active) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
		_current_player = p->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
		SET_EXPENSES_TYPE(EXPENSES_LOAN_INT);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   711
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   712
		SubtractMoneyFromPlayer(CommandCost(BIGMULUS(p->current_loan, interest, 16)));
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
		SET_EXPENSES_TYPE(EXPENSES_OTHER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
		SubtractMoneyFromPlayer(_price.station_value >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   719
static void HandleEconomyFluctuations()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
   721
	if (_opt.diff.economy == 0) return;
0
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
	if (--_economy.fluct == 0) {
2642
1d79b44ccbc9 (svn r3184) GB/SB
tron
parents: 2639
diff changeset
   724
		_economy.fluct = -(int)GB(Random(), 0, 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
		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
   726
	} else if (_economy.fluct == -12) {
2642
1d79b44ccbc9 (svn r3184) GB/SB
tron
parents: 2639
diff changeset
   727
		_economy.fluct = GB(Random(), 0, 8) + 312;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
		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
   729
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
static byte _price_category[NUM_PRICES] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
	0, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
	2, 2, 1, 1, 1, 1, 1, 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
	2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
static const int32 _price_base[NUM_PRICES] = {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   743
	    100, ///< station_value
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   744
	    100, ///< build_rail
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   745
	     95, ///< build_road
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   746
	     65, ///< build_signals
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   747
	    275, ///< build_bridge
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   748
	    600, ///< build_train_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   749
	    500, ///< build_road_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   750
	    700, ///< build_ship_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   751
	    450, ///< build_tunnel
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   752
	    200, ///< train_station_track
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   753
	    180, ///< train_station_length
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   754
	    600, ///< build_airport
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   755
	    200, ///< build_bus_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   756
	    200, ///< build_truck_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   757
	    350, ///< build_dock
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   758
	 400000, ///< build_railvehicle
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   759
	   2000, ///< build_railwagon
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   760
	 700000, ///< aircraft_base
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   761
	  14000, ///< roadveh_base
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   762
	  65000, ///< ship_base
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   763
	     20, ///< build_trees
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   764
	    250, ///< terraform
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   765
	     20, ///< clear_1
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   766
	     40, ///< purchase_land
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   767
	    200, ///< clear_2
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   768
	    500, ///< clear_3
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   769
	     20, ///< remove_trees
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   770
	    -70, ///< remove_rail
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   771
	     10, ///< remove_signals
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   772
	     50, ///< clear_bridge
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   773
	     80, ///< remove_train_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   774
	     80, ///< remove_road_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   775
	     90, ///< remove_ship_depot
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   776
	     30, ///< clear_tunnel
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   777
	  10000, ///< clear_water
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   778
	     50, ///< remove_rail_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   779
	     30, ///< remove_airport
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   780
	     50, ///< remove_bus_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   781
	     50, ///< remove_truck_station
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   782
	     55, ///< remove_dock
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   783
	   1600, ///< remove_house
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   784
	     40, ///< remove_road
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   785
	   5600, ///< running_rail[0] railroad
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   786
	   5200, ///< running_rail[1] monorail
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   787
	   4800, ///< running_rail[2] maglev
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   788
	   9600, ///< aircraft_running
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   789
	   1600, ///< roadveh_running
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   790
	   5600, ///< ship_running
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   791
	1000000, ///< build_industry
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   794
static byte price_base_multiplier[NUM_PRICES];
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   795
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   796
/**
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   797
 * Reset changes to the price base multipliers.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   798
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   799
void ResetPriceBaseMultipliers()
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   800
{
2508
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   801
	uint i;
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   802
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   803
	/* 8 means no multiplier. */
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   804
	for (i = 0; i < NUM_PRICES; i++)
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   805
		price_base_multiplier[i] = 8;
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   806
}
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   807
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   808
/**
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   809
 * Change a price base by the given factor.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   810
 * The price base is altered by factors of two, with an offset of 8.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   811
 * NewBaseCost = OldBaseCost * 2^(n-8)
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   812
 * @param price Index of price base to change.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   813
 * @param factor Amount to change by.
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   814
 */
2508
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   815
void SetPriceBaseMultiplier(uint price, byte factor)
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   816
{
2508
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   817
	assert(price < NUM_PRICES);
1926793f2977 (svn r3034) -NewGRF: Improve error checking of setting price bases.
peter1138
parents: 2506
diff changeset
   818
	price_base_multiplier[price] = factor;
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   819
}
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   820
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
   821
void StartupEconomy()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
	assert(sizeof(_price) == NUM_PRICES * sizeof(int32));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   827
	for (i = 0; i != NUM_PRICES; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
		int32 price = _price_base[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
		if (_price_category[i] != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
			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
   831
			if (mod < 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
				price = price * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
			} else if (mod > 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
				price = price * 9 >> 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
		}
2506
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   837
		if (price_base_multiplier[i] > 8) {
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   838
			price <<= price_base_multiplier[i] - 8;
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   839
		} else {
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   840
			price >>= 8 - price_base_multiplier[i];
eca87f30e05a (svn r3032) -NewGRF, Feature: Add support for changing base prices.
peter1138
parents: 2504
diff changeset
   841
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
		((int32*)&_price)[i] = price;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
		_price_frac[i] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   846
	_economy.interest_rate = _opt.diff.initial_interest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
	_economy.infl_amount = _opt.diff.initial_interest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
	_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
   849
	_economy.max_loan_unround = _economy.max_loan = _opt.diff.max_loan * 1000;
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2070
diff changeset
   850
	_economy.fluct = GB(Random(), 0, 8) + 168;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2587
diff changeset
   853
Pair SetupSubsidyDecodeParam(const Subsidy* s, bool mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
{
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   855
	TileIndex tile;
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
   856
	TileIndex tile2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
	Pair tp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
2272
8b32b48b8579 (svn r2794) Fix a comment and get rid of some parentheses using DeMorgan's law
tron
parents: 2261
diff changeset
   859
	/* if mode is false, use the singular form */
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
   860
	const CargoSpec *cs = GetCargo(s->cargo_type);
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
   861
	SetDParam(0, mode ? cs->name_plural : cs->name);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
	if (s->age < 12) {
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   864
		if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL) {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   865
			SetDParam(1, STR_INDUSTRY);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   866
			SetDParam(2, s->from);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   867
			tile = GetIndustry(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   869
			if (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD) {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   870
				SetDParam(4, STR_INDUSTRY);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   871
				SetDParam(5, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   872
				tile2 = GetIndustry(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
			} else {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   874
				SetDParam(4, STR_TOWN);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   875
				SetDParam(5, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   876
				tile2 = GetTown(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   878
		} else {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   879
			SetDParam(1, STR_TOWN);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   880
			SetDParam(2, s->from);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   881
			tile = GetTown(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   883
			SetDParam(4, STR_TOWN);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   884
			SetDParam(5, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   885
			tile2 = GetTown(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
	} else {
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   888
		SetDParam(1, s->from);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   889
		tile = GetStation(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
2070
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   891
		SetDParam(2, s->to);
47b1f29d4671 (svn r2580) Change: Added {INDUSTRY} command for printing industry names instead of the old {TOWN} {STRING} way.
ludde
parents: 2063
diff changeset
   892
		tile2 = GetStation(s->to)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   894
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
	tp.a = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
	tp.b = tile2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
	return tp;
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
5378
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   901
void DeleteSubsidyWithTown(TownID index)
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   902
{
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   903
	Subsidy *s;
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   904
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   905
	for (s = _subsidies; s != endof(_subsidies); s++) {
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   906
		if (s->cargo_type != CT_INVALID && s->age < 12) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   907
			const CargoSpec *cs = GetCargo(s->cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   908
			if (((cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) && (index == s->from || index == s->to)) ||
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   909
				((cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) && index == s->to)) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   910
				s->cargo_type = CT_INVALID;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   911
			}
5378
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   912
		}
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   913
	}
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   914
}
5c26eb069095 (svn r7563) -Fix (FS#468): removing towns in the scenario editor could leave subsidies with that town as source/destination, resulting in an assertion when displaying the Subsidies Window.
rubidium
parents: 5376
diff changeset
   915
4330
cf31daa8b321 (svn r6005) -Cleanup: introduce IndustryID and use it
rubidium
parents: 4329
diff changeset
   916
void DeleteSubsidyWithIndustry(IndustryID index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   918
	Subsidy *s;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   919
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   920
	for (s = _subsidies; s != endof(_subsidies); s++) {
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   921
		if (s->cargo_type != CT_INVALID && s->age < 12) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   922
			const CargoSpec *cs = GetCargo(s->cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   923
			if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL &&
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   924
				(index == s->from || (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD && index == s->to))) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   925
				s->cargo_type = CT_INVALID;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   926
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   928
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
4330
cf31daa8b321 (svn r6005) -Cleanup: introduce IndustryID and use it
rubidium
parents: 4329
diff changeset
   931
void DeleteSubsidyWithStation(StationID index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
	bool dirty = false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   935
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
   936
	for (s = _subsidies; s != endof(_subsidies); s++) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   937
		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
   938
				(s->from == index || s->to == index)) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
   939
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
			dirty = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   942
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
	if (dirty)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   948
struct FoundRoute {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	uint distance;
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
   950
	CargoID cargo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
	void *from;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
	void *to;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   953
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
static void FindSubsidyPassengerRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
	Town *from,*to;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
	fr->distance = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   961
	fr->from = from = GetRandomTown();
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   962
	if (from == NULL || from->population < 400) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   964
	fr->to = to = GetRandomTown();
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   965
	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
   966
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
   968
	fr->distance = DistanceManhattan(from->xy, to->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
static void FindSubsidyCargoRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
	Industry *i;
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
   974
	const IndustrySpec *ind;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
	int trans, total;
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
   976
	CargoID cargo;
0
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
	fr->distance = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   980
	fr->from = i = GetRandomIndustry();
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
   981
	if (i == NULL) return;
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
   982
	ind = GetIndustrySpec(i->type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   984
	/* Randomize cargo type */
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
   985
	if (HASBIT(Random(), 0) && ind->produced_cargo[1] != CT_INVALID) {
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
   986
		cargo = ind->produced_cargo[1];
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
   987
		trans = i->last_month_pct_transported[1];
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
   988
		total = i->last_month_production[1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
	} else {
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
   990
		cargo = ind->produced_cargo[0];
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
   991
		trans = i->last_month_pct_transported[0];
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6773
diff changeset
   992
		total = i->last_month_production[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   995
	/* Quit if no production in this industry
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   996
	 * or if the cargo type is passengers
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
   997
	 * or if the pct transported is already large enough */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   998
	if (total == 0 || trans > 42 || cargo == CT_INVALID) return;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
   999
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1000
	const CargoSpec *cs = GetCargo(cargo);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1001
	if (cs->town_effect == TE_PASSENGERS) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
	fr->cargo = cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1005
	if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1006
		/*  The destination is a town */
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1007
		Town *t = GetRandomTown();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1008
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1009
		/* Only want big towns */
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1010
		if (t == NULL || t->population < 900) return;
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  1011
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
  1012
		fr->distance = DistanceManhattan(i->xy, t->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
		fr->to = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
	} else {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1015
		/* The destination is an industry */
4356
ec4e13f21cd0 (svn r6057) -Codechange: made a function GetRandomXXX, that _always_ returns a valid XXX, unless there are none to pick from. Then NULL is returned.
truelight
parents: 4354
diff changeset
  1016
		Industry *i2 = GetRandomIndustry();
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1017
		if (i2 == NULL) {
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1018
			return;
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1019
		}
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1020
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1021
		ind = GetIndustrySpec(i2->type);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1022
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1023
		/* The industry must accept the cargo */
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1024
		if (i == i2 ||
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1025
				(cargo != ind->accepts_cargo[0] &&
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1026
				cargo != ind->accepts_cargo[1] &&
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1027
				cargo != ind->accepts_cargo[2])) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
			return;
6636
fec086b5c340 (svn r9867) -Codechange: Remove data duplication. The exact same values can be found in the industry spec, so take it from there instead.
belugas
parents: 6635
diff changeset
  1029
		}
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1098
diff changeset
  1030
		fr->distance = DistanceManhattan(i->xy, i2->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1031
		fr->to = i2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1032
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1034
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1035
static bool CheckSubsidyDuplicate(Subsidy *s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2587
diff changeset
  1037
	const Subsidy* ss;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1039
	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
  1040
		if (s != ss &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1041
				ss->from == s->from &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1042
				ss->to == s->to &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
				ss->cargo_type == s->cargo_type) {
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1044
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
  1051
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1052
static void SubsidyMonthlyHandler()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
	uint n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
	bool modified = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1061
	for (s = _subsidies; s != endof(_subsidies); s++) {
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1062
		if (s->cargo_type == CT_INVALID) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
		if (s->age == 12-1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
			pair = SetupSubsidyDecodeParam(s, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1066
			AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1067
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
		} else if (s->age == 2*12-1) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1070
			st = GetStation(s->to);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
			if (st->owner == _local_player) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
				pair = SetupSubsidyDecodeParam(s, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
				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
  1074
			}
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1075
			s->cargo_type = CT_INVALID;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
			s->age++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1080
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1082
	/* 25% chance to go on */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
  1083
	if (CHANCE16(1,4)) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1084
		/*  Find a free slot*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1085
		s = _subsidies;
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1086
		while (s->cargo_type != CT_INVALID) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
			if (++s == endof(_subsidies))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
				goto no_add;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1090
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
		n = 1000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1093
			FindSubsidyPassengerRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1094
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
				s->cargo_type = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
				s->from = ((Town*)fr.from)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
				s->to = ((Town*)fr.to)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1098
				goto add_subsidy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1099
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
			FindSubsidyCargoRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1102
				s->cargo_type = fr.cargo;
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1103
				s->from = ((Industry*)fr.from)->index;
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1104
				{
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1105
					const CargoSpec *cs = GetCargo(fr.cargo);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1106
					s->to = (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) ? ((Town*)fr.to)->index : ((Industry*)fr.to)->index;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1107
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1108
	add_subsidy:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1109
				if (!CheckSubsidyDuplicate(s)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1110
					s->age = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
					pair = SetupSubsidyDecodeParam(s, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
					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
  1113
					modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
			}
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
  1117
		} while (n--);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
no_add:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
	if (modified)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1797
diff changeset
  1124
static const SaveLoad _subsidies_desc[] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1125
	    SLE_VAR(Subsidy, cargo_type, SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1126
	    SLE_VAR(Subsidy, age,        SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1127
	SLE_CONDVAR(Subsidy, from,       SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1128
	SLE_CONDVAR(Subsidy, from,       SLE_UINT16,                5, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1129
	SLE_CONDVAR(Subsidy, to,         SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1130
	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
  1131
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1134
static void Save_SUBS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1139
	for (i = 0; i != lengthof(_subsidies); i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
		s = &_subsidies[i];
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2436
diff changeset
  1141
		if (s->cargo_type != CT_INVALID) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
			SlSetArrayIndex(i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
			SlObject(s, _subsidies_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1148
static void Load_SUBS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
	while ((index = SlIterateArray()) != -1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
		SlObject(&_subsidies[index], _subsidies_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
  1155
int32 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
  1156
{
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
  1157
	const CargoSpec *cs = GetCargo(cargo_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
	byte f;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
6458
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1160
	/* Use callback to calculate cargo profit, if available */
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1161
	if (HASBIT(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) {
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1162
		uint32 var18 = min(dist, 0xFFFF) | (min(num_pieces, 0xFF) << 16) | (transit_days << 24);
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1163
		uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs);
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1164
		if (callback != CALLBACK_FAILED) {
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1165
			int result = GB(callback, 0, 14);
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1166
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1167
			/* Simulate a 15 bit signed value */
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1168
			if (HASBIT(callback, 14)) result = 0x4000 - result;
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1169
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1170
			/* "The result should be a signed multiplier that gets multiplied
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1171
			 * by the amount of cargo moved and the price factor, then gets
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1172
			 * divided by 8192." */
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1173
			return result * num_pieces * _cargo_payment_rates[cargo_type] / 8192;
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1174
		}
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1175
	}
de9ef7bf3ea5 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138
parents: 6453
diff changeset
  1176
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
	/* zero the distance if it's the bank and very short transport. */
6357
f0f5e7d1713c (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6350
diff changeset
  1178
	if (_opt.landscape == LT_TEMPERATE && cs->label == 'VALU' && dist < 10)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
		dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
	f = 255;
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
  1182
	if (transit_days > cs->transit_days[0]) {
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
  1183
		transit_days -= cs->transit_days[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
		f -= transit_days;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1185
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
  1186
		if (transit_days > cs->transit_days[1]) {
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents: 5998
diff changeset
  1187
			transit_days -= cs->transit_days[1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1189
			if (f < transit_days) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1190
				f = 0;
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1191
			} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1192
				f -= transit_days;
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1193
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1194
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1195
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1196
	if (f < 31) f = 31;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1197
6369
50323446966b (svn r9427) -Codechange: remove redundant variable
peter1138
parents: 6357
diff changeset
  1198
	return BIGMULSS(dist * f * num_pieces, _cargo_payment_rates[cargo_type], 21);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1199
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
  1201
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
  1202
{
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1203
	Industry *best = NULL;
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1204
	Industry *ind;
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1205
	const IndustrySpec *indspec;
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1206
	uint best_dist;
6639
d9e4445b518d (svn r9870) -Codechange: Silence two compiler warnings and give proper type to the "type" member of industry struct
belugas
parents: 6636
diff changeset
  1207
	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
  1208
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1209
	/* Check if there's an industry close to the station that accepts the cargo
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1210
	 * XXX - Think of something better to
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1211
	 *       1) Only deliver to industries which are withing the catchment radius
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1212
	 *       2) Distribute between industries if more then one is present */
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1213
	best_dist = (_patches.station_spread + 8) * 2;
830
36c1366367e4 (svn r1301) -Codechange: _industries finally has FOR_ALL_INDUSTRIES too
truelight
parents: 820
diff changeset
  1214
	FOR_ALL_INDUSTRIES(ind) {
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1215
		indspec = GetIndustrySpec(ind->type);
6639
d9e4445b518d (svn r9870) -Codechange: Silence two compiler warnings and give proper type to the "type" member of industry struct
belugas
parents: 6636
diff changeset
  1216
		uint i;
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1217
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1218
		if (indspec->produced_cargo[0] == CT_INVALID) continue;
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1219
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1220
		for (i = 0; i < lengthof(indspec->accepts_cargo); i++) {
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1221
			if (cargo_type == indspec->accepts_cargo[i] &&
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1222
					(indspec->input_cargo_multiplier[i][0] != 0 || indspec->input_cargo_multiplier[i][1] != 0)) {
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1223
				break;
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1224
			}
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1225
		}
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1226
6639
d9e4445b518d (svn r9870) -Codechange: Silence two compiler warnings and give proper type to the "type" member of industry struct
belugas
parents: 6636
diff changeset
  1227
		/* Check if matching cargo has been found */
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1228
		if (i == lengthof(indspec->accepts_cargo)) continue;
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1229
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1230
		uint dist = DistanceManhattan(ind->xy, xy);
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1231
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1232
		if (dist < best_dist) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
			best = ind;
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1234
			best_dist = dist;
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1235
			accepted_cargo_index = i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1236
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1237
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1239
	/* Found one? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
	if (best != NULL) {
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1241
		indspec = GetIndustrySpec(best->type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1242
		best->was_cargo_delivered = true;
6635
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1243
		best->cargo_waiting[0] = min(best->cargo_waiting[0] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][0] / 256), 0xFFFF);
73064d1771ed (svn r9866) -Codechange: remove the technical one-input-cargo-creates-one-output-cargo limit in the factories, sawmills, steelmills and the ones I'm forgetting to mention right now. Use of this functionality will come later.
rubidium
parents: 6621
diff changeset
  1244
		best->cargo_waiting[1] = min(best->cargo_waiting[1] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][1] / 256), 0xFFFF);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1246
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3047
diff changeset
  1248
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
  1249
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
	TileIndex xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1255
	/* check if there is an already existing subsidy that applies to us */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1256
	for (s = _subsidies; s != endof(_subsidies); s++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
		if (s->cargo_type == cargo_type &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
				s->age >= 12 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
				s->from == from->index &&
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1260
				s->to == to->index) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
			return true;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1262
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
	/* check if there's a new subsidy that applies.. */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1266
	for (s = _subsidies; s != endof(_subsidies); s++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
		if (s->cargo_type == cargo_type && s->age < 12) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1268
			/* Check distance from source */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1269
			const CargoSpec *cs = GetCargo(cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1270
			if (cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1271
				xy = GetTown(s->from)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
			} else {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1273
				xy = (GetIndustry(s->from))->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
			}
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1275
			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
  1276
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
			/* Check distance from dest */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1278
			switch (cs->town_effect) {
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1279
				case TE_PASSENGERS:
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1280
				case TE_MAIL:
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1281
				case TE_GOODS:
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1282
				case TE_FOOD:
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1283
					xy = GetTown(s->to)->xy;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1284
					break;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1285
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1286
				default:
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1287
					xy = GetIndustry(s->to)->xy;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1288
					break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1289
			}
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1290
			if (DistanceMax(xy, to->xy) > 9) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1291
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
			/* 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
  1293
			s->age = 12;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
			s->from = from->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
			s->to = to->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1296
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1297
			/* Add a news item */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1298
			pair = SetupSubsidyDecodeParam(s, 0);
1309
4403a69da4f8 (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
  1299
			InjectDParam(2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1300
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1935
diff changeset
  1301
			p = GetPlayer(_current_player);
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1302
			SetDParam(0, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1303
			SetDParam(1, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1304
			AddNewsItem(
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1305
				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
  1306
				NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0),
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1307
				pair.a, pair.b
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1308
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1309
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1310
			InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1312
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1313
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1315
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
5683
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1317
static int32 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
  1318
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
	bool subsidised;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1320
	Station *s_from, *s_to;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
	int32 profit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1322
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3655
diff changeset
  1323
	assert(num_pieces > 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1325
	/* Update player statistics */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1326
	{
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1935
diff changeset
  1327
		Player *p = GetPlayer(_current_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1328
		p->cur_economy.delivered_cargo += num_pieces;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1329
		SETBIT(p->cargo_types, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1330
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1332
	/* Get station pointers. */
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1333
	s_from = GetStation(source);
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 863
diff changeset
  1334
	s_to = GetStation(dest);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1335
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1336
	/* Check if a subsidy applies. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1337
	subsidised = CheckSubsidised(s_from, s_to, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1339
	/* Increase town's counter for some special goods types */
6315
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1340
	const CargoSpec *cs = GetCargo(cargo_type);
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1341
	if (cs->town_effect == TE_FOOD) s_to->town->new_act_food += num_pieces;
6fae8409b635 (svn r9257) -Codechange: Use cargo's town effect property for determining subsidy source/target type.
peter1138
parents: 6262
diff changeset
  1342
	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
  1343
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1344
	/* Give the goods to the industry. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
	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
  1346
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1347
	/* Determine profit */
5683
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1348
	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
  1349
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1350
	/* Modify profit if a subsidy is in effect */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
	if (subsidised) {
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1352
		switch (_opt.diff.subsidy_multiplier) {
3655
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1353
			case 0:  profit += profit >> 1; break;
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1354
			case 1:  profit *= 2; break;
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1355
			case 2:  profit *= 3; break;
f91f341a613e (svn r4570) - Fix: subsidy calculation was too high, broken in r3564
peter1138
parents: 3613
diff changeset
  1356
			default: profit *= 4; break;
0
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1360
	return profit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1361
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1363
/**
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1364
 * Performs the vehicle payment _and_ marks the vehicle to be unloaded.
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1365
 * @param front_v the vehicle to be unloaded
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1366
 */
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1367
void VehiclePayment(Vehicle *front_v)
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1368
{
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1369
	int result = 0;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1370
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1371
	int profit = 0;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1372
	int total_veh_profit = 0;         // accumulates the profit across the vehicle chain (used by trains)
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1373
	int32 route_profit = 0;           // the grand total amount for the route. A-D of transfer chain A-B-C-D
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1374
	int virtual_profit = 0;           // virtual profit of one vehicle element for feeder systems
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1375
	int virtual_profit_total = 0;     // virtual profit for entire vehicle chain
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1376
	int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1377
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1378
	int all_vehicles_cargo_feeder_share = front_v->cargo_feeder_share; // used to hold transfer value of complete vehicle chain - used by trains
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1379
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1380
	StationID last_visited = front_v->last_station_visited;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1381
	Station *st = GetStation(last_visited);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1382
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1383
	/* The owner of the train wants to be paid */
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1384
	PlayerID old_player = _current_player;
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1385
	_current_player = front_v->owner;
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1386
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1387
	/* At this moment loading cannot be finished */
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1388
	CLRBIT(front_v->vehicle_flags, VF_LOADING_FINISHED);
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1389
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1390
	/* Start unloading in at the first possible moment */
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1391
	front_v->load_unload_time_rem = 1;
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1392
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1393
	for (Vehicle *v = front_v; v != NULL; v = v->next) {
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1394
		/* No cargo to unload */
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1395
		if (v->cargo_cap == 0 || v->cargo_count == 0) continue;
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1396
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1397
		/* All cargo has already been paid for, no need to pay again */
6882
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1398
		if (v->cargo_count == v->cargo_paid_for) {
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1399
			SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1400
			continue;
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1401
		}
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1402
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1403
		GoodsEntry *ge = &st->goods[v->cargo_type];
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1404
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1405
		if (v->cargo_source != last_visited &&
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1406
				HASBIT(ge->waiting_acceptance, 15) &&
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1407
				(front_v->current_order.flags & OF_TRANSFER) == 0) {
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1408
			/* Deliver goods to the station */
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1409
			st->time_since_unload = 0;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1410
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1411
			/* handle end of route payment */
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1412
			profit += DeliverGoods(v->cargo_count - v->cargo_paid_for, v->cargo_type, v->cargo_source, last_visited, v->cargo_source_xy, v->cargo_days);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1413
			v->cargo_paid_for        = v->cargo_count;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1414
			route_profit             = profit;                                   // display amount paid for final route delivery, A-D of a chain A-B-C-D
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1415
			total_veh_profit         = profit - all_vehicles_cargo_feeder_share; // whole vehicle is not payed for transfers picked up earlier
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1416
			total_cargo_feeder_share = -all_vehicles_cargo_feeder_share;         // total of transfer fees in vehicle chain needs to be zero at end of unload
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1417
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1418
			v->cargo_feeder_share = 0;   // clear transfer cost per vehicle
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1419
			result |= 1;
6882
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1420
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1421
			SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1422
		} else if (front_v->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1423
			if ((front_v->current_order.flags & OF_TRANSFER) != 0) {
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1424
				virtual_profit = GetTransportedGoodsIncome(
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1425
					v->cargo_count - v->cargo_paid_for,
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1426
					/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1427
					DistanceManhattan(v->cargo_loaded_at_xy, GetStation(last_visited)->xy),
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1428
					v->cargo_days,
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1429
					v->cargo_type);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1430
6601
eac63c966a9e (svn r9815) -Fix (r9761) [FS#769): transfers weren't accounted in the vehicle profits.
rubidium
parents: 6580
diff changeset
  1431
				front_v->profit_this_year += virtual_profit;
eac63c966a9e (svn r9815) -Fix (r9761) [FS#769): transfers weren't accounted in the vehicle profits.
rubidium
parents: 6580
diff changeset
  1432
				ge->feeder_profit         += v->cargo_feeder_share + virtual_profit; // transfer cargo transfer fees to station
eac63c966a9e (svn r9815) -Fix (r9761) [FS#769): transfers weren't accounted in the vehicle profits.
rubidium
parents: 6580
diff changeset
  1433
				total_cargo_feeder_share  -= v->cargo_feeder_share; // accumulate deduction of feeder shares
eac63c966a9e (svn r9815) -Fix (r9761) [FS#769): transfers weren't accounted in the vehicle profits.
rubidium
parents: 6580
diff changeset
  1434
				v->cargo_feeder_share      = 0;                     // clear transfer cost
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1435
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1436
				/* keep total of cargo unloaded (pending) for accurate cargoshare calculation on load */
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1437
				SB(ge->unload_pending, 0, 12, GB(ge->unload_pending, 0, 12) + v->cargo_count);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1438
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1439
				virtual_profit_total += virtual_profit;   // accumulate transfer profits for whole vehicle
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1440
				v->cargo_paid_for = v->cargo_count;       // record how much of the cargo has been paid for to eliminate double counting
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1441
			}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1442
			result |= 2;
6882
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1443
10263ea04cf3 (svn r10125) -Fix [FS#865]: under some circumstances the wagons of a train didn't get loaded properly.
rubidium
parents: 6879
diff changeset
  1444
			SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1445
		}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1446
	}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1447
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1448
	/* Ensure a negative total is only applied to the vehicle if there is value to reduce. */
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1449
	front_v->cargo_feeder_share = max(front_v->cargo_feeder_share + total_cargo_feeder_share, 0);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1450
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1451
	if (virtual_profit_total > 0) {
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1452
		ShowFeederIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, virtual_profit_total);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1453
	}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1454
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1455
	if (route_profit != 0) {
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1456
		front_v->profit_this_year += total_veh_profit;
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1457
		SubtractMoneyFromPlayer(-route_profit);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1458
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1459
		if (IsLocalPlayer() && !PlayVehicleSound(front_v, VSE_LOAD_UNLOAD)) {
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1460
			SndPlayVehicleFx(SND_14_CASHTILL, front_v);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1461
		}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1462
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1463
		ShowCostOrIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, -total_veh_profit);
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1464
	}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1465
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1466
	_current_player = old_player;
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1467
}
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1468
6580
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1469
/**
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1470
 * Loads/unload the vehicle if possible.
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1471
 * @param v the vehicle to be (un)loaded
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1472
 * @param cargo_left the amount of each cargo type that is
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1473
 *                   virtually left on the platform to be
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1474
 *                   picked up by another vehicle when all
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1475
 *                   previous vehicles have loaded.
6580
8ba94d9af221 (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium
parents: 6565
diff changeset
  1476
 */
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1477
static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1478
{
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1479
	assert(v->current_order.type == OT_LOADING);
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1480
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1481
	/* We have not waited enough time till the next round of loading/unloading */
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1482
	if (--v->load_unload_time_rem != 0) {
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1483
		if (_patches.improved_load && HASBIT(v->current_order.flags, OFB_FULL_LOAD)) {
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1484
			/* 'Reserve' this cargo for this vehicle, because we were first. */
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1485
			for (; v != NULL; v = v->next) {
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1486
				if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo_count;
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1487
			}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1488
		}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1489
		return;
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1490
	}
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1491
6844
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1492
	if (v->type == VEH_TRAIN && !IsTileType(v->tile, MP_STATION)) {
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1493
		/* The train reversed in the station. Take the "easy" way
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1494
		 * out and let the train just leave as it always did. */
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1495
		SETBIT(v->vehicle_flags, VF_LOADING_FINISHED);
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1496
		return;
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1497
	}
ee73775d49d2 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium
parents: 6823
diff changeset
  1498
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1499
	int unloading_time = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1500
	Vehicle *u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1501
	int result = 0;
6620
4ecf216888d3 (svn r9840) -Fix (r9838): MSVC is complaining about signedness again (Belugas).
rubidium
parents: 6618
diff changeset
  1502
	int cap;
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1503
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1504
	bool completely_empty  = true;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1505
	bool anything_unloaded = false;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1506
	bool anything_loaded   = false;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1507
	uint32 cargo_not_full  = 0;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1508
	uint32 cargo_full      = 0;
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1509
	int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
	v->cur_speed = 0;
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1512
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1513
	StationID last_visited = v->last_station_visited;
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1514
	Station *st = GetStation(last_visited);
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1515
499
b224c29812bc (svn r792) Small cleanup: Kill a goto
tron
parents: 445
diff changeset
  1516
	for (; v != NULL; v = v->next) {
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1517
		if (v->cargo_cap == 0) continue;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1518
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1519
		byte load_amount = EngInfo(v->engine_type)->load_amount;
6227
e38177bd77c4 (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: 6199
diff changeset
  1520
		if (_patches.gradual_loading && HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_LOAD_AMOUNT)) {
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1521
			uint16 cb_load_amount = GetVehicleCallback(CBID_VEHICLE_LOAD_AMOUNT, 0, 0, v->engine_type, v);
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1522
			if (cb_load_amount != CALLBACK_FAILED) load_amount = cb_load_amount & 0xFF;
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1523
		}
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1524
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1525
		GoodsEntry *ge = &st->goods[v->cargo_type];
6620
4ecf216888d3 (svn r9840) -Fix (r9838): MSVC is complaining about signedness again (Belugas).
rubidium
parents: 6618
diff changeset
  1526
		int count = GB(ge->waiting_acceptance, 0, 12);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1527
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1528
		if (HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING)) {
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1529
			uint16 amount_unloaded = _patches.gradual_loading ? min(v->cargo_count, load_amount) : v->cargo_count;
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1530
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1531
			if (v->cargo_source != last_visited && ge->waiting_acceptance & 0x8000 && !(u->current_order.flags & OF_TRANSFER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
				result |= 1;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2630
diff changeset
  1533
			} else if (u->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1534
				if (count == 0) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1535
					/* No goods waiting at station */
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1536
					ge->enroute_time    = v->cargo_days;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1537
					ge->enroute_from    = v->cargo_source;
5683
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1538
					ge->enroute_from_xy = v->cargo_source_xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
				} else {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1540
					/* Goods already waiting at station. Set counters to the worst value. */
5683
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1541
					if (v->cargo_days >= ge->enroute_time) ge->enroute_time = v->cargo_days;
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1542
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1543
					if (last_visited != ge->enroute_from) {
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1544
						ge->enroute_from    = v->cargo_source;
5683
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1545
						ge->enroute_from_xy = v->cargo_source_xy;
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1546
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
				}
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1548
				/* Update amount of waiting cargo. There is, however, no sense in
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1549
				 * updating the count variable because this vehicle will not be
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1550
				 * able to take the cargo. */
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1551
				SB(ge->waiting_acceptance, 0, 12, min(amount_unloaded + count, 0xFFF));
4814
8e8f3b110279 (svn r6738) - Fix (r2441): Only apply the virtual transfer profit if the order is a transfer order, rather than to any unload order. This fixes an issue where the AI doesn't know that a route is unprofitable.
peter1138
parents: 4701
diff changeset
  1552
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1553
				/* if there is not enough to unload from pending, ensure it does not go -ve
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1554
				 * else deduct amount actually unloaded from unload_pending */
6199
707e6a239878 (svn r8979) -Fix (r8978): unsigned vs signed comparisons.
rubidium
parents: 6198
diff changeset
  1555
				SB(ge->unload_pending, 0, 12, max(GB(ge->unload_pending, 0, 12) - amount_unloaded, 0U));
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1556
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
				result |= 2;
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1558
			} else {
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1559
				/* The order changed while unloading (unset unload/transfer) or the
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1560
				 * station does not accept goods anymore. */
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1561
				CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1562
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
			}
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1564
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1565
			/* Deliver goods to the station */
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1566
			st->time_since_unload = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1567
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1568
			unloading_time += amount_unloaded;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1569
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1570
			v->cargo_count -= amount_unloaded;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1571
			v->cargo_paid_for -= min(amount_unloaded, v->cargo_paid_for);
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1572
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1573
			anything_unloaded = true;
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1574
			if (_patches.gradual_loading && v->cargo_count != 0) {
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1575
				completely_empty = false;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1576
			} else {
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1577
				/* We have finished unloading (cargo count == 0) */
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1578
				CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1579
			}
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1580
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1581
			continue;
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1582
		}
5888
6fb161a15a3c (svn r8501) -Fix (r7377) [FS#539]: Keep track of how much cargo has been paid for, so that cargo cannot be paid for more than once.
maedhros
parents: 5854
diff changeset
  1583
6fb161a15a3c (svn r8501) -Fix (r7377) [FS#539]: Keep track of how much cargo has been paid for, so that cargo cannot be paid for more than once.
maedhros
parents: 5854
diff changeset
  1584
		/* We cannot have paid for more cargo than there is on board. */
6fb161a15a3c (svn r8501) -Fix (r7377) [FS#539]: Keep track of how much cargo has been paid for, so that cargo cannot be paid for more than once.
maedhros
parents: 5854
diff changeset
  1585
		assert(v->cargo_paid_for <= v->cargo_count);
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1586
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1587
		/* Do not pick up goods that we unloaded */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1588
		if (u->current_order.flags & OF_UNLOAD) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
		/* update stats */
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1591
		int t;
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1592
		switch (u->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1593
			case VEH_TRAIN: t = u->u.rail.cached_max_speed; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1594
			case VEH_ROAD:  t = u->max_speed / 2;           break;
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1595
			default:        t = u->max_speed;               break;
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1596
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1597
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1598
		/* if last speed is 0, we treat that as if no vehicle has ever visited the station. */
6609
5db3cdab0591 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium
parents: 6601
diff changeset
  1599
		ge->days_since_pickup = 0;
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
  1600
		ge->last_speed = min(t, 255);
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
  1601
		ge->last_age = _cur_year - v->build_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1602
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1603
		/* If there's goods waiting at the station, and the vehicle
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1604
		 * has capacity for it, load it on the vehicle. */
2504
5ae89f643cdc (svn r3030) More work for GB/SB, this time concerning the waiting_acceptance attribute of stations
tron
parents: 2498
diff changeset
  1605
		if (count != 0 &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1606
				(cap = v->cargo_cap - v->cargo_count) != 0) {
1935
164d58b9137c (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1905
diff changeset
  1607
523
8871819c9afd (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1608
			/* Skip loading this vehicle if another train/vehicle is already handling
8871819c9afd (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1609
			 * the same cargo type at this station */
6698
611e079b7cd1 (svn r9930) -Fix (r9838): obiwan could cause vehicles to way a long time (2.5 years) at stations.
rubidium
parents: 6643
diff changeset
  1610
			if (_patches.improved_load && cargo_left[v->cargo_type] <= 0) {
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1611
				SETBIT(cargo_not_full, v->cargo_type);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1612
				continue;
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1613
			}
523
8871819c9afd (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1614
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1615
			if (cap > count) cap = count;
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1616
			if (_patches.gradual_loading) cap = min(cap, load_amount);
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1617
			if (_patches.improved_load) {
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1618
				/* Don't load stuff that is already 'reserved' for other vehicles */
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1619
				cap = min(cargo_left[v->cargo_type], cap);
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1620
				cargo_left[v->cargo_type] -= cap;
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1621
			}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1622
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1623
			if (v->cargo_count == 0) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1624
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1625
			/* TODO: Regarding this, when we do gradual loading, we
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1626
			 * should first unload all vehicles and then start
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1627
			 * loading them. Since this will cause
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1628
			 * VEHICLE_TRIGGER_EMPTY to be called at the time when
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1629
			 * the whole vehicle chain is really totally empty, the
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1630
			 * completely_empty assignment can then be safely
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1631
			 * removed; that's how TTDPatch behaves too. --pasky */
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1632
			completely_empty = false;
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1633
			anything_loaded = true;
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1634
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1635
			/* cargoshare is proportioned by the amount due to unload
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1636
			 * Otherwise, with gradual loading, 100% of credits would be taken immediately,
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1637
			 * even if the cargo volume represents a tiny percent of the whole.
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1638
			 * ge->unload_pending holds the amount that has been credited, but has not yet been unloaded.
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1639
			 */
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1640
			int cargoshare = cap * 10000 / (ge->waiting_acceptance + ge->unload_pending);
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1641
			int feeder_profit_share = ge->feeder_profit * cargoshare / 10000;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
			v->cargo_count += cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
			ge->waiting_acceptance -= cap;
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1644
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1645
			total_cargo_feeder_share += feeder_profit_share;    // store cost for later payment when cargo unloaded
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1646
			v->cargo_loaded_at_xy = st->xy;                     // retains location of where the cargo was picked up for intermediate payments
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1647
1935
164d58b9137c (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1905
diff changeset
  1648
			ge->feeder_profit -= feeder_profit_share;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1649
			unloading_time += cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1650
			st->time_since_load = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1651
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1652
			/* And record the source of the cargo, and the days in travel. */
2815
dc557fb1f477 (svn r3363) -Fix: 'fixed' the transfer problem. This doesn't really fix it, but gives a bit more fair amount of money. Can only be fixed correctly with CargoPackets as suggested in branch.
truelight
parents: 2684
diff changeset
  1653
			v->cargo_source = ge->enroute_from;
5683
b027e5d5b4cd (svn r8144) -Fix: [FS#163] When a station is removed, vehicles do not get excessive payment any longer, as the origin TILE is now stored as long as the origin STATION for the transported cargos. Basically this is only a temporary fix until cargopackets are implemented, but it fixes one of the oldest known bugs (Special Thanks to Darkvater for lots of testing)
celestar
parents: 5668
diff changeset
  1654
			v->cargo_source_xy = ge->enroute_from_xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1655
			v->cargo_days = ge->enroute_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1656
			result |= 2;
3580
14bf6e7715c8 (svn r4466) -Fix: (FS#71) Game no longer crashes when the last vehicle serving a station has been deleted.
celestar
parents: 3491
diff changeset
  1657
			st->last_vehicle_type = v->type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1658
		}
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1659
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1660
		if (v->cargo_count == v->cargo_cap) {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1661
			SETBIT(cargo_full, v->cargo_type);
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1662
		} else {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1663
			SETBIT(cargo_not_full, v->cargo_type);
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1664
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1665
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1666
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1667
	/* We update these variables here, so gradual loading still fills
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1668
	 * all wagons at the same time instead of using the same 'improved'
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1669
	 * loading algorithm for the wagons (only fill wagon when there is
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1670
	 * enough to fill the previous wagons) */
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1671
	if (_patches.improved_load && HASBIT(u->current_order.flags, OFB_FULL_LOAD)) {
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1672
		/* Update left cargo */
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1673
		for (v = u; v != NULL; v = v->next) {
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1674
			if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo_count;
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1675
		}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1676
	}
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1677
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1678
	v = u;
1935
164d58b9137c (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1905
diff changeset
  1679
6559
354d355736ab (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium
parents: 6500
diff changeset
  1680
	v->cargo_feeder_share += total_cargo_feeder_share;
6198
5e278ec7c0f9 (svn r8978) -Feature: Rewrite of transfer system.
richk
parents: 6190
diff changeset
  1681
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1682
	if (anything_loaded || anything_unloaded) {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1683
		if (_patches.gradual_loading) {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1684
			/* The time it takes to load one 'slice' of cargo or passengers depends
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1685
			* on the vehicle type - the values here are those found in TTDPatch */
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1686
			const uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1687
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1688
			unloading_time = gradual_loading_wait_time[v->type];
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1689
		}
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1690
	} else {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1691
		bool finished_loading = true;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1692
		if (HASBIT(v->current_order.flags, OFB_FULL_LOAD)) {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1693
			if (_patches.full_load_any) {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1694
				/* if the aircraft carries passengers and is NOT full, then
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1695
				 * continue loading, no matter how much mail is in */
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1696
				if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap != v->cargo_count) ||
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1697
						(cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are stull non-full cargos
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1698
					finished_loading = false;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1699
				}
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1700
			} else if (cargo_not_full != 0) {
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1701
				finished_loading = false;
5251
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1702
			}
f4123dca9ec2 (svn r7377) -Fix (r7326): With gradual loading, make sure cargo is only paid for once, and always notify the station the vehicle was present. (Maedhros)
peter1138
parents: 5220
diff changeset
  1703
		}
6611
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1704
		unloading_time = 20;
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1705
9ebe17f772ba (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium
parents: 6609
diff changeset
  1706
		SB(v->vehicle_flags, VF_LOADING_FINISHED, 1, finished_loading);
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5077
diff changeset
  1707
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1708
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1709
	if (v->type == VEH_TRAIN) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1710
		/* Each platform tile is worth 2 rail vehicles. */
5998
2bfbade143ac (svn r8709) -Fix/Codechange: Rename the function GetStationPlatforms into GetPlatformLength because that is what it really does. Overload it because there is already a GetPlatformLength (one gives the length of the whole platform, the other gives the remaining length in a given direction). Turned both functions into methods of Station. While messing around with it, fix a problem where loading times for overhanging trains are miscomputed.
celestar
parents: 5955
diff changeset
  1711
		int overhang = v->u.rail.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
2587
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2548
diff changeset
  1712
		if (overhang > 0) {
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2548
diff changeset
  1713
			unloading_time <<= 1;
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2548
diff changeset
  1714
			unloading_time += (overhang * unloading_time) / 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1715
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1716
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1718
	v->load_unload_time_rem = unloading_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1719
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1720
	if (completely_empty) {
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1721
		TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1722
	}
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1723
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1724
	if (result != 0) {
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1725
		InvalidateWindow(v->GetVehicleListWindowClass(), v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1726
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1727
6823
1f2075619543 (svn r10062) -Codechange: Don't redraw all station tiles when cargo is added or removed if the station has no custom graphics.
peter1138
parents: 6819
diff changeset
  1728
		st->MarkTilesDirty(true);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6559
diff changeset
  1729
		v->MarkDirty();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1730
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2829
diff changeset
  1731
		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
  1732
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1733
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1734
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1735
/**
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1736
 * Load/unload the vehicles in this station according to the order
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1737
 * they entered.
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1738
 * @param st the station to do the loading/unloading for
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1739
 */
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1740
void LoadUnloadStation(Station *st)
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1741
{
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1742
	int cargo_left[NUM_CARGO];
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1743
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1744
	for (uint i = 0; i < NUM_CARGO; i++) cargo_left[i] = GB(st->goods[i].waiting_acceptance, 0, 12);
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1745
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1746
	std::list<Vehicle *>::iterator iter;
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1747
	for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1748
		Vehicle *v = *iter;
6618
094779076306 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium
parents: 6616
diff changeset
  1749
		if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left);
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1750
	}
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1751
}
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  1752
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1753
void PlayersMonthlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1754
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1755
	PlayersGenStatistics();
4293
4b7006c1b5eb (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4286
diff changeset
  1756
	if (_patches.inflation && _cur_year < MAX_YEAR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
		AddInflation();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1758
	PlayersPayInterest();
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1759
	/* Reset the _current_player flag */
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 200
diff changeset
  1760
	_current_player = OWNER_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1761
	HandleEconomyFluctuations();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1762
	SubsidyMonthlyHandler();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1763
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1764
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
static void DoAcquireCompany(Player *p)
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
	Player *owner;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1768
	int i;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
  1769
	int64 value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1770
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1771
	SetDParam(0, p->name_1);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1772
	SetDParam(1, p->name_2);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1773
	SetDParam(2, p->bankrupt_value);
4873
11880cbbc9f6 (svn r6803) -Codechange: Substitute magic numbers by an enum for the news windows
Darkvater
parents: 4854
diff changeset
  1774
	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
  1775
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1776
	/* original code does this a little bit differently */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1777
	PlayerID pi = p->index;
6262
312d9c28da04 (svn r9071) -Fix [FS#459]: when in networking an other player takes over an AI which is also controlled by a player, move the player with the company to the company who bought out the AI
truelight
parents: 6259
diff changeset
  1778
	ChangeNetworkOwner(pi, _current_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1779
	ChangeOwnershipOfPlayerItems(pi, _current_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1780
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1781
	if (p->bankrupt_value == 0) {
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1935
diff changeset
  1782
		owner = GetPlayer(_current_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1783
		owner->current_loan += p->current_loan;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
	value = CalculateCompanyValue(p) >> 2;
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1787
	for (i = 0; i != 4; i++) {
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
  1788
		if (p->share_owners[i] != PLAYER_SPECTATOR) {
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1935
diff changeset
  1789
			owner = GetPlayer(p->share_owners[i]);
6952
b19643469024 (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 6950
diff changeset
  1790
			owner->player_money += value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
			owner->yearly_expenses[0][EXPENSES_OTHER] += value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1794
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1795
	p->is_active = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
	DeletePlayerWindows(pi);
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4423
diff changeset
  1798
	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
  1799
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1800
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1801
extern int GetAmountOwnedBy(const Player *p, PlayerID owner);
599
30da691a5561 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1802
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1803
/** Acquire shares in an opposing company.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
  1804
 * @param tile unused
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6405
diff changeset
  1805
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1806
 * @param p1 player to buy the shares from
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1807
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1808
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
  1809
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
  1810
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1811
	Player *p;
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
  1812
	CommandCost cost;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1813
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1814
	/* Check if buying shares is allowed (protection against modified clients */
4850
93095755db8c (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4848
diff changeset
  1815
	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
  1816
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1817
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1818
	p = GetPlayer((PlayerID)p1);
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
  1819
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1820
	/* Protect new companies from hostile takeovers */
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
  1821
	if (_cur_year - p->inaugurated_year < 6) return_cmd_error(STR_7080_PROTECTED);
930
ab42e283749d (svn r1418) -Feature: [1098254] (dis)Allow Shares. Add patch options to allow buying/selling of shares (Hackykid)
darkvater
parents: 919
diff changeset
  1822
599
30da691a5561 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1823
	/* Those lines are here for network-protection (clients can be slow) */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1824
	if (GetAmountOwnedBy(p, PLAYER_SPECTATOR) == 0) return cost;
930
ab42e283749d (svn r1418) -Feature: [1098254] (dis)Allow Shares. Add patch options to allow buying/selling of shares (Hackykid)
darkvater
parents: 919
diff changeset
  1825
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1826
	/* We can not buy out a real player (temporarily). TODO: well, enable it obviously */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1827
	if (GetAmountOwnedBy(p, PLAYER_SPECTATOR) == 1 && !p->is_ai) return cost;
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 997
diff changeset
  1828
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1829
	cost.AddCost(CalculateCompanyValue(p) >> 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1830
	if (flags & DC_EXEC) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1831
		PlayerByte* b = p->share_owners;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1832
		int i;
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1833
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
  1834
		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
  1835
		*b = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1836
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1837
		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
  1838
			if (++i == 4) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1839
				p->bankrupt_value = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1840
				DoAcquireCompany(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1841
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1842
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1843
		}
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1844
		InvalidateWindow(WC_COMPANY, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1846
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1847
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1848
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1849
/** Sell shares in an opposing company.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
  1850
 * @param tile unused
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6405
diff changeset
  1851
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1852
 * @param p1 player to sell the shares from
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1853
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1854
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
  1855
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
  1856
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
	Player *p;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
  1858
	int64 cost;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1859
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1860
	/* Check if buying shares is allowed (protection against modified clients */
4850
93095755db8c (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4848
diff changeset
  1861
	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
  1862
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1863
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1864
	p = GetPlayer((PlayerID)p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
653
989ae721eb8b (svn r1086) -Fix: [Network] [ 1084774 ] Fixed quick-share-sell bug
truelight
parents: 630
diff changeset
  1866
	/* Those lines are here for network-protection (clients can be slow) */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1867
	if (GetAmountOwnedBy(p, _current_player) == 0) return CommandCost();
653
989ae721eb8b (svn r1086) -Fix: [Network] [ 1084774 ] Fixed quick-share-sell bug
truelight
parents: 630
diff changeset
  1868
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1869
	/* 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
  1870
	cost = CalculateCompanyValue(p) >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
	cost = -(cost - (cost >> 7));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1873
	if (flags & DC_EXEC) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1874
		PlayerByte* b = p->share_owners;
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1875
		while (*b != _current_player) b++; // share owners is guaranteed to contain player
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
  1876
		*b = PLAYER_SPECTATOR;
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 2989
diff changeset
  1877
		InvalidateWindow(WC_COMPANY, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1878
	}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1879
	return CommandCost((int32)cost);
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
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1882
/** Buy up another company.
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1883
 * When a competing company is gone bankrupt you get the chance to purchase
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1884
 * that company.
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1885
 * @todo currently this only works for AI players
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
  1886
 * @param tile unused
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6405
diff changeset
  1887
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1888
 * @param p1 player/company to buy up
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1889
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1890
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6882
diff changeset
  1891
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
  1892
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1893
	Player *p;
6405
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
  1894
	PlayerID pid = (PlayerID)p1;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1895
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1896
	/* Disable takeovers in multiplayer games */
6405
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
  1897
	if (!IsValidPlayer(pid) || _networking) return CMD_ERROR;
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
  1898
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
  1899
	/* Do not allow players to take over themselves */
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
  1900
	if (pid == _current_player) return CMD_ERROR;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1901
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1902
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
6405
aa7d8578cce9 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar
parents: 6403
diff changeset
  1903
	p = GetPlayer(pid);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1904
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1905
	if (!p->is_ai) return CMD_ERROR;
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1906
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1707
diff changeset
  1907
	if (flags & DC_EXEC) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1908
		DoAcquireCompany(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1909
	}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
  1910
	return CommandCost(p->bankrupt_value);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1911
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1912
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1913
/** Prices */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1914
static void SaveLoad_PRIC()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1915
{
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1916
	SlArray(&_price,      NUM_PRICES, SLE_INT32);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1917
	SlArray(&_price_frac, NUM_PRICES, SLE_UINT16);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1918
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1919
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1920
/** Cargo payment rates */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1921
static void SaveLoad_CAPR()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1922
{
6463
e986fe39dfe7 (svn r9638) -Feature: Increase cargo types from 12 to 32 and enable newcargo flag in NewGRF loader.
peter1138
parents: 6458
diff changeset
  1923
	uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO;
e986fe39dfe7 (svn r9638) -Feature: Increase cargo types from 12 to 32 and enable newcargo flag in NewGRF loader.
peter1138
parents: 6458
diff changeset
  1924
	SlArray(&_cargo_payment_rates,      num_cargo, SLE_INT32);
e986fe39dfe7 (svn r9638) -Feature: Increase cargo types from 12 to 32 and enable newcargo flag in NewGRF loader.
peter1138
parents: 6458
diff changeset
  1925
	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
  1926
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1927
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1797
diff changeset
  1928
static const SaveLoad _economy_desc[] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1929
	SLE_VAR(Economy, max_loan,         SLE_INT32),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1930
	SLE_VAR(Economy, max_loan_unround, SLE_INT32),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1931
	SLE_VAR(Economy, fluct,            SLE_FILE_I16 | SLE_VAR_I32),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1932
	SLE_VAR(Economy, interest_rate,    SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1933
	SLE_VAR(Economy, infl_amount,      SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1934
	SLE_VAR(Economy, infl_amount_pr,   SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1935
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1936
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1937
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 6119
diff changeset
  1938
/** Economy variables */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6227
diff changeset
  1939
static void SaveLoad_ECMY()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1940
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1797
diff changeset
  1941
	SlObject(&_economy, _economy_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1942
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1943
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1944
extern const ChunkHandler _economy_chunk_handlers[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1945
	{ '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
  1946
	{ 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4330
diff changeset
  1947
	{ 'SUBS', Save_SUBS,     Load_SUBS,     CH_ARRAY},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1948
	{ 'ECMY', SaveLoad_ECMY, SaveLoad_ECMY, CH_RIFF | CH_LAST},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1949
};