economy.c
author truelight
Sun, 12 Dec 2004 14:00:25 +0000
changeset 599 80e49e323483
parent 555 eec6c0294435
child 630 fd3c36132342
permissions -rw-r--r--
(svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
because of the lag between commands (anyway, I hope)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     1
#include "stdafx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     2
#include "ttd.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 499
diff changeset
     3
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     4
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     6
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     7
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     8
#include "window.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include "gfx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "saveload.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "economy.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "industry.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "town.h"
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
    15
#include "network.h"
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 260
diff changeset
    16
#include "sound.h"
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
    17
#include "engine.h"
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
    18
#include "network_data.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
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
    20
void UpdatePlayerHouse(Player *p, uint score)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
	byte val;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
	uint tile = p->location_of_house;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
	if (tile == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
	(val = 128, score < 170) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
	(val+= 4, score < 350) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
	(val+= 4, score < 520) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
	(val+= 4, score < 720) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
	(val+= 4, true);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
/* house is already big enough */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
	if (val <= _map5[tile])
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
	_map5[tile + TILE_XY(0,0)] = val;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
	_map5[tile + TILE_XY(0,1)] = ++val;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
	_map5[tile + TILE_XY(1,0)] = ++val;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
	_map5[tile + TILE_XY(1,1)] = ++val;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
	MarkTileDirtyByTile(tile + TILE_XY(0,0));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
	MarkTileDirtyByTile(tile + TILE_XY(0,1));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
	MarkTileDirtyByTile(tile + TILE_XY(1,0));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
	MarkTileDirtyByTile(tile + TILE_XY(1,1));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
    49
int64 CalculateCompanyValue(Player *p) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	byte owner = p->index;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
    51
	int64 value;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
    52
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
		Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
		uint num = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
    56
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
		FOR_ALL_STATIONS(st) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
			if (st->xy != 0 && st->owner == owner) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
				uint facil = st->facilities;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
				do num += (facil&1); while (facil >>= 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
		value = num * _price.station_value * 25;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
		Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
		FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
			if (v->owner != owner)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
				continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
			if (v->type == VEH_Train ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
					v->type == VEH_Road ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
					(v->type == VEH_Aircraft && v->subtype<=2) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
					v->type == VEH_Ship) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
				value += v->value * 3 >> 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
	if (p->player_money > 0)
236
da0ae9d977e0 (svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents: 206
diff changeset
    83
		value += p->money64; // add real money value
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
    88
// if update is set to true, the economy is updated with this score
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
    89
//  (also the house is updated, should only be true in the on-tick event)
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
    90
int UpdateCompanyRatingAndValue(Player *p, bool update)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	byte owner = p->index;
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
    93
	int score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
    94
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
    95
	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
    96
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
/* Count vehicles */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
		Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
		int32 min_profit = MAX_INT;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
		uint num = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
		FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
			if (v->owner != owner)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
				continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
			if ((v->type == VEH_Train && v->subtype == 0) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
					v->type == VEH_Road ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
					(v->type == VEH_Aircraft && v->subtype<=2) ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
					v->type == VEH_Ship) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
				num++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
				if (v->age > 730) {
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   112
					if (min_profit > v->profit_last_year)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
						min_profit = v->profit_last_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
				}
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
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   118
		_score_part[owner][SCORE_VEHICLES] = num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
		if (min_profit != MAX_INT && min_profit > 0)
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   121
			_score_part[owner][SCORE_MIN_PROFIT] = min_profit;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
/* Count stations */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
		uint num = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
		Station *st;
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
		FOR_ALL_STATIONS(st) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
			if (st->xy != 0 && st->owner == owner) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
				int facil = st->facilities;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
				do num += facil&1; while (facil>>=1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
		}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   135
		_score_part[owner][SCORE_STATIONS] = num;
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
/* Generate statistics depending on recent income statistics */
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
		PlayerEconomyEntry *pee;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
		int numec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
		int32 min_income;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   143
		int32 max_income;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
		numec = min(p->num_valid_stat_ent, 12);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
		if (numec != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
			min_income = 0x7FFFFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
			max_income = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
			pee = p->old_economy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
			do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
				min_income = min(min_income, pee->income + pee->expenses);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
				max_income = max(max_income, pee->income + pee->expenses);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
			} while (++pee,--numec);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
			if (min_income > 0)
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   156
				_score_part[owner][SCORE_MIN_INCOME] = min_income;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   158
			_score_part[owner][SCORE_MAX_INCOME] = max_income;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
/* Generate score depending on amount of transported cargo */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
		PlayerEconomyEntry *pee;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
		int numec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
		uint32 total_delivered;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
		numec = min(p->num_valid_stat_ent, 4);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
		if (numec != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
			pee = p->old_economy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
			total_delivered = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
			do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
				total_delivered += pee->delivered_cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
			} while (++pee,--numec);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   176
			_score_part[owner][SCORE_DELIVERED] = total_delivered;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
	}
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   179
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
/* Generate score for variety of cargo */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
		uint cargo = p->cargo_types;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
		uint num = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
		do num += cargo&1; while (cargo>>=1);
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   185
		_score_part[owner][SCORE_CARGO] = num;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   186
		if (update)
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   187
			p->cargo_types = 0;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
/* Generate score for player money */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
		int32 money = p->player_money;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
		if (money > 0) {
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   194
			_score_part[owner][SCORE_MONEY] = money;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
		}
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 loan */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
	{
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   200
		_score_part[owner][SCORE_LOAN] = score_info[SCORE_LOAN].needed - p->current_loan;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   201
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   202
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   203
	// Now we calculate the score for each item..
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   204
	{
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   205
		int i;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   206
		int total_score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   207
		int s;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   208
		score = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   209
		for (i=0;i<NUM_SCORE;i++) {
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   210
			// Skip the total
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   211
			if (i == SCORE_TOTAL) continue;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   212
			// Check the score
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   213
			s = (_score_part[owner][i] >= score_info[i].needed) ? score_info[i].score : ((_score_part[owner][i] * score_info[i].score) / score_info[i].needed);
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   214
			if (s < 0) s = 0;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   215
			score += s;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   216
			total_score += score_info[i].score;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   217
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   218
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   219
		_score_part[owner][SCORE_TOTAL] = score;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   220
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   221
		// We always want the score scaled to SCORE_MAX (1000)
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   222
		if (total_score != SCORE_MAX)
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   223
			score = score * SCORE_MAX / total_score;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   226
	if (update) {
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   227
    	p->old_economy[0].performance_history = score;
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   228
    	UpdatePlayerHouse(p, score);
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   229
    	p->old_economy[0].company_value = CalculateCompanyValue(p);
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   230
    }
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   231
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   232
	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
   233
	return score;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
// use 255 as new_player to delete the player.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
void ChangeOwnershipOfPlayerItems(byte old_player, byte new_player)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
	byte old = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
	_current_player = old_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
	if (new_player == 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
		Subsidy *s;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   244
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
		for(s=_subsidies; s != endof(_subsidies); s++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
			if (s->cargo_type != 0xff && s->age >= 12) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
				Station *st = DEREF_STATION(s->to);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
				if (st->owner == old_player)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
					s->cargo_type = 0xff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
	// take care of rating in towns
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
		Town *t;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   257
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
		// if a player takes over, give the ratings to that player.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
		if (new_player != 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
			FOR_ALL_TOWNS(t) if (t->xy && HASBIT(t->have_ratings, old_player)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
				if (HASBIT(t->have_ratings, new_player)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
					// use max of the two ratings.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
					t->ratings[new_player] = max(t->ratings[new_player], t->ratings[old_player]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
					SETBIT(t->have_ratings, new_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
					t->ratings[new_player] = t->ratings[old_player];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
		// reset rating for the towns.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
		FOR_ALL_TOWNS(t) if (t->xy) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
			t->ratings[old_player] = 500;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
			CLRBIT(t->have_ratings, old_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   277
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
		int num_train = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
		int num_road = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
		int num_ship = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
		int num_aircraft = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
		Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
		// Determine Ids for the new vehicles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
		FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
			if (v->owner == new_player) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   288
				if (v->type == VEH_Train && v->subtype == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
					num_train++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
				else if (v->type == VEH_Road)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
					num_road++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
				else if (v->type == VEH_Ship)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
					num_ship++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
				else if (v->type == VEH_Aircraft && v->subtype <= 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   295
					num_aircraft++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
		FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
			if (v->owner == old_player && IS_BYTE_INSIDE(v->type, VEH_Train, VEH_Aircraft+1) ) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
				if (new_player == 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
					DeleteWindowById(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
					DeleteWindowById(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
					DeleteWindowById(WC_VEHICLE_ORDERS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
					DeleteVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
					v->owner = new_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
					if (v->type == VEH_Train && v->subtype == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
						v->unitnumber = ++num_train;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
					else if (v->type == VEH_Road)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
						v->unitnumber = ++num_road;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
					else if (v->type == VEH_Ship)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
						v->unitnumber = ++num_ship;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
					else if (v->type == VEH_Aircraft && v->subtype <= 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
						v->unitnumber = ++num_aircraft;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
	// Change ownership of tiles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
		uint tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
			ChangeTileOwner(tile, old_player, new_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
		} while (++tile != TILES_X * TILES_Y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
	// Change color of existing windows
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
	if (new_player != 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
		Window *w;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
		for(w=_windows; w != _last_window; w++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
			if (w->caption_color == old_player)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
				w->caption_color = new_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
	_current_player = old;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
	MarkWholeScreenDirty();
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
extern void DeletePlayerWindows(int pi);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
static void PlayersCheckBankrupt(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
	int owner;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
   348
	int64 val;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   350
	// 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
   351
	if (p->player_money >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
		p->quarters_of_bankrupcy = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
	p->quarters_of_bankrupcy++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
	owner = p->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   360
	switch (p->quarters_of_bankrupcy) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   361
		case 2:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   362
			AddNewsItem( (StringID)(owner + 16),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   363
				NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   364
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   365
		case 3: {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   366
			/* XXX - In multiplayer, should we ask other players if it wants to take
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   367
		          over when it is a human company? -- TrueLight */
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   368
			if (IS_HUMAN_PLAYER(owner)) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   369
				AddNewsItem( (StringID)(owner + 16),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   370
					NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   371
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   372
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   374
			// Check if the company has any value.. if not, declare it bankrupt
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   375
			//  right now
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   376
			val = CalculateCompanyValue(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   377
			if (val > 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   378
				p->bankrupt_value = val;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   379
				p->bankrupt_asked = 1 << owner; // Don't ask the owner
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   380
				p->bankrupt_timeout = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   381
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   382
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   383
			// Else, falltrue to case 4...
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   385
		case 4: {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   386
			// Close everything the owner has open
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   387
			DeletePlayerWindows(owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   389
//		Show bankrupt news
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   390
			SetDParam(0, p->name_1);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   391
			SetDParam(1, p->name_2);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   392
			AddNewsItem( (StringID)(owner + 16*3), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   393
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   394
			// If the player is human, and it is no network play, leave the player playing
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   395
			if (IS_HUMAN_PLAYER(owner) && !_networking) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   396
				p->bankrupt_asked = 255;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   397
				p->bankrupt_timeout = 0x456;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   398
			} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   399
				// If we are the server, make sure it is clear that his player is no
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   400
				//  longer with us!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   401
				if (IS_HUMAN_PLAYER(owner) && _network_server) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   402
					NetworkClientInfo *ci;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   403
					ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   404
					ci->client_playas = (byte)(OWNER_SPECTATOR + 1);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   405
					// Send the new info to all the clients
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   406
					NetworkUpdateClientInfo(_network_own_client_index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   407
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   408
				// Make sure the player no longer controls the company
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   409
				if (IS_HUMAN_PLAYER(owner) && owner == _local_player) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   410
					// Switch the player to spectator..
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   411
					_local_player = OWNER_SPECTATOR;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   412
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   413
				// Convert everything the player owns to NO_OWNER
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   414
				p->money64 = p->player_money = 100000000;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   415
				ChangeOwnershipOfPlayerItems(owner, 0xFF); // 255 is no owner
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   416
				// Register the player as not-active
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   417
				p->is_active = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   418
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
void DrawNewsBankrupcy(Window *w)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   427
	DrawNewsBorder(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
	p = DEREF_PLAYER(WP(w,news_d).ni->string_id & 15);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
	DrawPlayerFace(p->face, p->player_color, 2, 23);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
	GfxFillRect(3, 23, 3+91, 23+118, 0x4323);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   433
	SetDParam(0, p->president_name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   434
	SetDParam(1, p->president_name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
	DrawStringMultiCenter(49, 148, STR_7058_PRESIDENT, 94);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
	switch(WP(w,news_d).ni->string_id >> 4) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
	case 1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
		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
   441
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   442
		SetDParam(0, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   443
		SetDParam(1, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
			STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
		break;
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
	case 2: {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
		int32 price;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
		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
   456
		COPY_IN_DPARAM(0,WP(w,news_d).ni->params, 2);
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   457
		SetDParam(2, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   458
		SetDParam(3, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
		price = WP(w,news_d).ni->params[2];
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   460
		SetDParam(4, price);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
			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
   465
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
	case 3:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
		DrawStringCentered(w->width>>1, 1, STR_705C_BANKRUPT, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
		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
   472
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
			STR_705D_HAS_BEEN_CLOSED_DOWN_BY,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
			w->width - 101);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   477
		break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
	case 4:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
		DrawStringCentered(w->width>>1, 1, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED, 0);
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   481
		SetDParam(0, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   482
		SetDParam(1, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
		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
   484
		DrawStringMultiCenter(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
			((w->width - 101) >> 1) + 98,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
			90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
			STR_705F_STARTS_CONSTRUCTION_NEAR,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
			w->width - 101);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
	default:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
		NOT_REACHED();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
StringID GetNewsStringBankrupcy(NewsItem *ni)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
	Player *p = DEREF_PLAYER(ni->string_id & 0xF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
	switch(ni->string_id >> 4) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
	case 1:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   502
		SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   503
		SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   504
		SetDParam(2, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   505
		SetDParam(3, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
		return STR_02B6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
	case 2:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   508
		SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   509
		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
   510
		COPY_IN_DPARAM(2,ni->params, 2);
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   511
		SetDParam(4, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   512
		SetDParam(5, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
		COPY_IN_DPARAM(6,ni->params + 2, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
		return STR_02B6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
	case 3:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   516
		SetDParam(0, STR_705C_BANKRUPT);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   517
		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
   518
		COPY_IN_DPARAM(2,ni->params, 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
		return STR_02B6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
	case 4:
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   521
		SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   522
		SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   523
		SetDParam(2, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   524
		SetDParam(3, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
		COPY_IN_DPARAM(4,ni->params, 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
		return STR_02B6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
	default:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
		NOT_REACHED();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   530
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
	/* useless, but avoids compiler warning this way */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
	return 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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
static void PlayersGenStatistics()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	FOR_ALL_STATIONS(st) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
		if (st->xy != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
			_current_player = st->owner;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
			SET_EXPENSES_TYPE(EXPENSES_PROPERTY);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
			SubtractMoneyFromPlayer(_price.station_value >> 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
	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
   549
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
	FOR_ALL_PLAYERS(p) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
		if (p->is_active) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
			memmove(&p->old_economy, &p->cur_economy, sizeof(p->old_economy));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
			memset(&p->cur_economy, 0, sizeof(p->cur_economy));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
			if (p->num_valid_stat_ent != 24)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
				p->num_valid_stat_ent++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
116
ca4332a9ec1d (svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents: 33
diff changeset
   559
			UpdateCompanyRatingAndValue(p, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
			PlayersCheckBankrupt(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
			if (p->block_preview != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
				p->block_preview--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
	InvalidateWindow(WC_INCOME_GRAPH, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
	InvalidateWindow(WC_OPERATING_PROFIT, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
	InvalidateWindow(WC_DELIVERED_CARGO, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
	InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
	InvalidateWindow(WC_COMPANY_VALUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
	InvalidateWindow(WC_COMPANY_LEAGUE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
static void AddSingleInflation(int32 *value, uint16 *frac, int32 amt)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
	int64 tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
	int32 low;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
	tmp = BIGMULS(*value, amt);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
	*frac = (uint16)(low = (uint16)tmp + *frac);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
	*value += (int32)(tmp >> 16) + (low >> 16);
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
static void AddInflation()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
	int32 inf = _economy.infl_amount * 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
	for(i=0; i!=NUM_PRICES; i++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
		AddSingleInflation( (int32*)&_price + i, _price_frac + i, inf );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
	_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
   594
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
	if (_economy.max_loan + 50000 <= _economy.max_loan_unround)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
		_economy.max_loan += 50000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
	inf = _economy.infl_amount_pr * 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
	for(i=0; i!=NUM_CARGO; i++) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   600
		AddSingleInflation(
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
			_cargo_payment_rates + i,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
			_cargo_payment_rates_frac + i,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
			inf
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
		);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
	InvalidateWindowClasses(WC_VEHICLE_DETAILS);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
	InvalidateWindow(WC_PAYMENT_RATES, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
static void PlayersPayInterest()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
	int interest = _economy.interest_rate * 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
	FOR_ALL_PLAYERS(p) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
		if (!p->is_active)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
		_current_player = p->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
		SET_EXPENSES_TYPE(EXPENSES_LOAN_INT);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   623
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
		SubtractMoneyFromPlayer(BIGMULUS(p->current_loan, interest, 16));
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
		SET_EXPENSES_TYPE(EXPENSES_OTHER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
		SubtractMoneyFromPlayer(_price.station_value >> 2);
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
static void HandleEconomyFluctuations()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	if (_opt.diff.economy == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
	if (--_economy.fluct == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
		_economy.fluct = - (int)(Random()&3);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
		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
   639
	} else if (_economy.fluct == -12) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
		_economy.fluct = (Random()&255) + 312;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
		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
   642
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
static byte _price_category[NUM_PRICES] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
	0, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
	2, 2, 2, 2, 2, 2, 2, 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
	2, 2, 1, 1, 1, 1, 1, 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
	2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
static const int32 _price_base[NUM_PRICES] = {
9
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   656
	100,		// station_value
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   657
	100,		// build_rail
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   658
	95,			// build_road
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   659
	65,			// build_signals
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   660
	275,		// build_bridge
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   661
	600,		// build_train_depot
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   662
	500,		// build_road_depot
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   663
	700,		// build_ship_depot
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   664
	450,		// build_tunnel
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   665
	200,		// train_station_track
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   666
	180,		// train_station_length
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   667
	600,		// build_airport
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   668
	200,		// build_bus_station
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   669
	200,		// build_truck_station
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   670
	350,		// build_dock
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   671
	400000,	// build_railvehicle
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   672
	2000,		// build_railwagon
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   673
	700000,	// aircraft_base
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   674
	14000,	// roadveh_base
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   675
	65000,	// ship_base
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   676
	20,			// build_trees
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   677
	250,		// terraform
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   678
	20,			// clear_1
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   679
	40,			// purchase_land
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   680
	200,		// clear_2
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   681
	500,		// clear_3
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   682
	20,			// remove_trees
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   683
	-70,		// remove_rail
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   684
	10,			// remove_signals
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   685
	50,			// clear_bridge
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   686
	80,			// remove_train_depot
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   687
	80,			// remove_road_depot
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   688
	90,			// remove_ship_depot
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   689
	30,			// clear_tunnel
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   690
	10000,	// clear_water
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   691
	50,			// remove_rail_station
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   692
	30,			// remove_airport
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   693
	50,			// remove_bus_station
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   694
	50,			// remove_truck_station
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   695
	55,			// remove_dock
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   696
	1600,		// remove_house
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   697
	40,			// remove_road
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   698
	5600,		// running_rail[0] railroad
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   699
	5200,		// running_rail[1] monorail
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   700
	4800,		// running_rail[2] maglev
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   701
	9600,		// aircraft_running
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   702
	1600,		// roadveh_running
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   703
	5600,		// ship_running
b1ecf90eb6da (svn r10) Some documentation on _price_base array
dominik
parents: 4
diff changeset
   704
	1000000, // build_industry
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
void StartupEconomy()
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
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
	assert(sizeof(_price) == NUM_PRICES * sizeof(int32));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
	for(i=0; i!=NUM_PRICES; i++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
		int32 price = _price_base[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
		if (_price_category[i] != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
			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
   717
			if (mod < 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
				price = price * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
			} else if (mod > 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
				price = price * 9 >> 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   721
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
		((int32*)&_price)[i] = price;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
		_price_frac[i] = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
	_economy.interest_rate = _opt.diff.initial_interest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
	_economy.infl_amount = _opt.diff.initial_interest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
	_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
   730
	_economy.max_loan_unround = _economy.max_loan = _opt.diff.max_loan * 1000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
	_economy.fluct = (byte)(Random()) + 168;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
Pair SetupSubsidyDecodeParam(Subsidy *s, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
	int tile, tile2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
	Industry *i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
	Town *t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
	Pair tp;
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
	/* if mode is false, convert into plural */
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   743
	SetDParam(0, _cargoc.names_s[s->cargo_type] + (mode?0:32));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
	if (s->age < 12) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
		if (!(s->cargo_type == CT_PASSENGERS || s->cargo_type == CT_MAIL)) {
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   747
			SetDParam(1, STR_2029);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   748
			i = DEREF_INDUSTRY(s->from);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   749
			tile = i->xy;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   750
			SetDParam(2, i->town->townnametype);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   751
			SetDParam(3, i->town->townnameparts);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   753
			SetDParam(4, i->type + STR_4802_COAL_MINE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
			if (s->cargo_type != CT_GOODS && s->cargo_type != CT_FOOD) {
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   756
				SetDParam(5, STR_2029);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
				i = DEREF_INDUSTRY(s->to);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
				tile2 = i->xy;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   759
				SetDParam(8, i->type + STR_4802_COAL_MINE);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   760
				SetDParam(6, i->town->townnametype);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   761
				SetDParam(7, i->town->townnameparts);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
				t = DEREF_TOWN(s->to);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
				tile2 = t->xy;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   765
				SetDParam(5, t->townnametype);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   766
				SetDParam(6, t->townnameparts);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
			t = DEREF_TOWN(s->from);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   770
			tile = t->xy;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   771
			SetDParam(1, t->townnametype);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   772
			SetDParam(2, t->townnameparts);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   773
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
			t = DEREF_TOWN(s->to);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
			tile2 = t->xy;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   776
			SetDParam(3, t->townnametype);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   777
			SetDParam(4, t->townnameparts);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   778
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   779
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
		st = DEREF_STATION(s->from);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
		tile = st->xy;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   782
		SetDParam(1, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
		st = DEREF_STATION(s->to);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
		tile2 = st->xy;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
   786
		SetDParam(2, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   788
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   789
	tp.a = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   790
	tp.b = tile2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
	return tp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
void DeleteSubsidyWithIndustry(byte index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
	Subsidy *s;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   798
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
	for(s=_subsidies; s != endof(_subsidies); s++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
		if (s->cargo_type != 0xFF && s->age < 12 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
				s->cargo_type != CT_PASSENGERS && s->cargo_type != CT_MAIL &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
				(index == s->from || (s->cargo_type!=CT_GOODS && s->cargo_type!=CT_FOOD && index==s->to))) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
			s->cargo_type = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   805
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   808
void DeleteSubsidyWithStation(byte index)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
	bool dirty = false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   812
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
	for(s=_subsidies; s != endof(_subsidies); s++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
		if (s->cargo_type != 0xFF && s->age >= 12 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
				(s->from == index || s->to == index)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
			s->cargo_type = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
			dirty = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   819
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
	if (dirty)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
}
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
typedef struct FoundRoute {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
	uint distance;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
	byte cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
	void *from;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
	void *to;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
} FoundRoute;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
static void FindSubsidyPassengerRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
	Town *from,*to;
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
	fr->distance = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   838
	fr->from = from = DEREF_TOWN(RandomRange(_total_towns));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
	if (from->xy == 0 || from->population < 400)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   842
	fr->to = to = DEREF_TOWN(RandomRange(_total_towns));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
	if (from==to || to->xy == 0 || to->population < 400 || to->pct_pass_transported > 42)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
		return;
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
	fr->distance = GetTileDist(from->xy, to->xy);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
static void FindSubsidyCargoRoute(FoundRoute *fr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
	Industry *i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
	int trans, total;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
	byte cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
	fr->distance = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   857
	fr->from = i = DEREF_INDUSTRY(RandomRange(_total_industries));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
	if (i->xy == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
	// Randomize cargo type
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   862
	if (Random()&1 && i->produced_cargo[1] != 0xFF) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
		cargo = i->produced_cargo[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
		trans = i->pct_transported[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
		total = i->total_production[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
		cargo = i->produced_cargo[0];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
		trans = i->pct_transported[0];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
		total = i->total_production[0];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
	// Quit if no production in this industry
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
	//  or if the cargo type is passengers
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   874
	//  or if the pct transported is already large enough
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
	if (total == 0  || trans > 42 || cargo == 0xFF || cargo == CT_PASSENGERS)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
		return;
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
	fr->cargo = cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
	if (cargo == CT_GOODS || cargo == CT_FOOD) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   881
		// The destination is a town
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   882
		Town *t = DEREF_TOWN(RandomRange(_total_towns));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   883
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
		// Only want big towns
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
		if (t->xy == 0 || t->population < 900)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
		fr->distance = GetTileDist(i->xy, t->xy);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
		fr->to = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
		// The destination is an industry
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   891
		Industry *i2 = DEREF_INDUSTRY(RandomRange(_total_industries));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   892
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
		// The industry must accept the cargo
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   894
		if (i == i2 || i2->xy == 0 ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
				(cargo != i2->accepts_cargo[0] &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
				cargo != i2->accepts_cargo[1] &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
				cargo != i2->accepts_cargo[2]))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   899
		fr->distance = GetTileDist(i->xy, i2->xy);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
		fr->to = i2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   904
static bool CheckSubsidyDuplicate(Subsidy *s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
	Subsidy *ss;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
	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
   909
		if (s != ss &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   910
//				ss->age < 12 &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   911
				ss->from == s->from &&
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   912
				ss->to == s->to &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
				ss->cargo_type == s->cargo_type) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
			s->cargo_type = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
		}
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
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   921
void RemoteSubsidyAdd(Subsidy *s_new)
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   922
{
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   923
	Subsidy *s;
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   924
	Pair pair;
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   925
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   926
	// search the first free subsidy
240
f1efbac04112 (svn r241) -Fix: Trailing spaces :O
darkvater
parents: 239
diff changeset
   927
	for(s=_subsidies; s != endof(_subsidies); s++)
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   928
		if (s->cargo_type == 0xFF)
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   929
			break;
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   930
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   931
	memcpy(s,s_new,sizeof(Subsidy));
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   932
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   933
	pair = SetupSubsidyDecodeParam(s, 0);
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   934
	AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   935
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   936
	InvalidateWindow(WC_SUBSIDIES_LIST, 0);
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   937
}
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
   938
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
static void SubsidyMonthlyHandler()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
	uint n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
	FoundRoute fr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
	bool modified = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
	for(s=_subsidies; s != endof(_subsidies); s++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
		if (s->cargo_type == 0xFF)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
		if (s->age == 12-1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
			pair = SetupSubsidyDecodeParam(s, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
			AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, 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
   955
			s->cargo_type = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
		} else if (s->age == 2*12-1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
			st = DEREF_STATION(s->to);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
			if (st->owner == _local_player) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
				pair = SetupSubsidyDecodeParam(s, 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
				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
   962
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
			s->cargo_type = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
			modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
			s->age++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
	}
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
	// 25% chance to go on
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 541
diff changeset
   971
	if (CHANCE16(1,4)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
		// Find a free slot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
		s = _subsidies;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
		while (s->cargo_type != 0xFF) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
			if (++s == endof(_subsidies))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
				goto no_add;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
   978
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
		n = 1000;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
			FindSubsidyPassengerRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
				s->cargo_type = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
				s->from = ((Town*)fr.from)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
				s->to = ((Town*)fr.to)->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
				goto add_subsidy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
			FindSubsidyCargoRoute(&fr);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
			if (fr.distance <= 70) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
				s->cargo_type = fr.cargo;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
				s->from = (Industry*)fr.from - _industries;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
				s->to = (fr.cargo == CT_GOODS || fr.cargo == CT_FOOD) ? ((Town*)fr.to)->index : (Industry*)fr.to - _industries;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
	add_subsidy:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
				if (!CheckSubsidyDuplicate(s)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   995
					s->age = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
					pair = SetupSubsidyDecodeParam(s, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
					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
   998
					modified = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
			}
239
23958632a582 (svn r240) -Fix: desync on subsidy generation
signde
parents: 236
diff changeset
  1002
		} while (n--);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
no_add:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	if (modified)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
		InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
static const byte _subsidies_desc[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
	SLE_VAR(Subsidy,cargo_type,		SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
	SLE_VAR(Subsidy,age,					SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
	SLE_VAR(Subsidy,from,					SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
	SLE_VAR(Subsidy,to,						SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1017
static void Save_SUBS()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1022
	for(i=0; i!=lengthof(_subsidies); i++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
		s = &_subsidies[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
		if (s->cargo_type != 0xFF) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1025
			SlSetArrayIndex(i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1026
			SlObject(s, _subsidies_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1030
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1031
static void Load_SUBS()
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
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1034
	while ((index = SlIterateArray()) != -1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
		SlObject(&_subsidies[index], _subsidies_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, byte cargo_type)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1040
	int cargo = cargo_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
	byte f;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1042
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
	/* zero the distance if it's the bank and very short transport. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
	if (_opt.landscape == LT_NORMAL && cargo == CT_VALUABLES && dist < 10)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
		dist = 0;
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
	f = 255;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
	if (transit_days > _cargoc.transit_days_1[cargo]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
		transit_days -= _cargoc.transit_days_1[cargo];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
		f -= transit_days;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1051
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
		if (transit_days > _cargoc.transit_days_2[cargo]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
			transit_days -= _cargoc.transit_days_2[cargo];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
			if (f < transit_days)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
				f = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
			else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
				f -= transit_days;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
	if (f < 31) f = 31;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
	return BIGMULSS(dist * f * num_pieces, _cargo_payment_rates[cargo], 21);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1066
static void DeliverGoodsToIndustry(TileIndex xy, byte cargo_type, int num_pieces)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1067
{
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1068
	Industry *ind, *best;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
	int t, u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1070
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
	/* Check if there's an industry close to the station that accepts
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
	 * the cargo */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
	best = NULL;
33
431df1abc34b (svn r34) -Fix [990770] Cargo delivery area patch for bug #989322. If station sign was too far away, there were goods acceptance/delivery issues (Celstar)
darkvater
parents: 9
diff changeset
  1074
	u = _patches.station_spread + 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1075
	for(ind = _industries; ind != endof(_industries); ind++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
		if (ind->xy != 0 && (cargo_type == ind->accepts_cargo[0] || cargo_type
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
				 == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]) &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
				 ind->produced_cargo[0] != 0xFF &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
				 ind->produced_cargo[0] != cargo_type &&
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1080
				 (t=GetTileDist(ind->xy, xy)) < u) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
			u = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
			best = ind;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1084
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1085
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
	/* Found one? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
	if (best != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
		best->was_cargo_delivered = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
		best->cargo_waiting[0] = min(best->cargo_waiting[0] + num_pieces, 0xFFFF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1093
static bool CheckSubsidised(Station *from, Station *to, byte cargo_type)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1094
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
	Subsidy *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
	TileIndex xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
	Pair pair;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1098
	Player *p;
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
	// check if there is an already existing subsidy that applies to us
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
	for(s=_subsidies; s != endof(_subsidies); s++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1102
		if (s->cargo_type == cargo_type &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
				s->age >= 12 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1104
				s->from == from->index &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
				s->to == to->index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1106
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1108
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1109
	/* check if there's a new subsidy that applies.. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1110
	for(s=_subsidies; s != endof(_subsidies); s++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
		if (s->cargo_type == cargo_type && s->age < 12) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1112
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
			/* Check distance from source */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
			if (cargo_type == CT_PASSENGERS || cargo_type == CT_MAIL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
				xy = DEREF_TOWN(s->from)->xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
				xy = (DEREF_INDUSTRY(s->from))->xy;
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
			if (GetTileDist1D(xy, from->xy) > 9)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
				continue;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1121
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
			/* Check distance from dest */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
			if (cargo_type == CT_PASSENGERS || cargo_type == CT_MAIL || cargo_type == CT_GOODS || cargo_type == CT_FOOD) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
				xy = DEREF_TOWN(s->to)->xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
				xy = (DEREF_INDUSTRY(s->to))->xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
			if (GetTileDist1D(xy, to->xy) > 9)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
				continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
			/* 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
  1133
			s->age = 12;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1134
			s->from = from->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
			s->to = to->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
			/* Add a news item */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
			pair = SetupSubsidyDecodeParam(s, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
			INJECT_DPARAM(2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
			p = DEREF_PLAYER(_current_player);
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1142
			SetDParam(0, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1143
			SetDParam(1, p->name_2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
			AddNewsItem(
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1145
				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
  1146
				NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
				pair.a, pair.b);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1148
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
			InvalidateWindow(WC_SUBSIDIES_LIST, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1156
static int32 DeliverGoods(int num_pieces, byte cargo_type, byte source, byte dest, byte days_in_transit)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
	bool subsidised;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
	Station *s_from, *s_to;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1160
	int32 profit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
 	assert(num_pieces > 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1164
	// Update player statistics
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1165
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1166
		Player *p = DEREF_PLAYER(_current_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
		p->cur_economy.delivered_cargo += num_pieces;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
		SETBIT(p->cargo_types, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1169
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1170
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1171
	// Get station pointers.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
	s_from = DEREF_STATION(source);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
	s_to = DEREF_STATION(dest);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
	// Check if a subsidy applies.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
	subsidised = CheckSubsidised(s_from, s_to, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
	// Increase town's counter for some special goods types
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
		Town *t = s_to->town;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
		if (cargo_type == CT_FOOD) t->new_act_food += num_pieces;
4
cad62d5f9708 (svn r5) -Fix: townname generation of TTDLX savegames. All work
darkvater
parents: 0
diff changeset
  1182
		if (cargo_type == CT_STEEL)  t->new_act_water += num_pieces;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
	// Give the goods to the industry.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
	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
  1187
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
	// Determine profit
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1190
		int t = GetTileDist(s_from->xy, s_to->xy);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
		int r = num_pieces;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1192
		profit = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1193
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1194
			int u = min(r, 255);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1195
			r -= u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1196
			profit += GetTransportedGoodsIncome(u, t, days_in_transit, cargo_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1197
		} while (r != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1198
	}
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
	// Modify profit if a subsidy is in effect
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
	if (subsidised) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
		if (_opt.diff.subsidy_multiplier < 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
			/* 1.5x */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
			profit += profit >> 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
		} else if (_opt.diff.subsidy_multiplier == 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
			/* 2x */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1207
			profit *= 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
		} else if (_opt.diff.subsidy_multiplier == 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
			/* 3x */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
			profit *= 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
			/* 4x */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
			profit *= 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
	// Computers get 25% extra profit if they're intelligent.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
	if (_opt.diff.competitor_intelligence>=1 && !IS_HUMAN_PLAYER(_current_player))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
		profit += profit >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
	return profit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
523
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1224
/*
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1225
 * Returns true if Vehicle v should wait loading because other vehicle is
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1226
 * already loading the same cargo type
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1227
 * v = vehicle to load, u = GetFirstInChain(v)
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1228
 */
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1229
static bool LoadWait(const Vehicle *v, const Vehicle *u) {
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1230
	const Vehicle *w;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1231
	const Vehicle *x;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1232
	bool has_any_cargo = false;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1233
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1234
	if (!(u->current_order.flags & OF_FULL_LOAD)) return false;
523
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1235
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1236
	for (w = u; w != NULL; w = w->next) {
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1237
		if (w->cargo_count != 0) {
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1238
			if (v->cargo_type == w->cargo_type &&
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1239
					u->last_station_visited == w->cargo_source)
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1240
				return false;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1241
			has_any_cargo = true;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1242
		}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1243
	}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1244
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1245
	FOR_ALL_VEHICLES(x) {
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1246
		if ((x->type != VEH_Train || x->subtype == 0) && // for all locs
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1247
				u->last_station_visited == x->last_station_visited && // at the same station
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1248
				!(x->vehstatus & VS_STOPPED) && // not stopped
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1249
				x->current_order.type == OT_LOADING && // loading
523
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1250
				u != x) { // not itself
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1251
			bool other_has_any_cargo = false;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1252
			bool has_space_for_same_type = false;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1253
			bool other_has_same_type = false;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1254
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1255
			for (w = x; w != NULL; w = w->next) {
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1256
				if (w->cargo_count < w->cargo_cap && v->cargo_type == w->cargo_type)
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1257
					has_space_for_same_type = true;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1258
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1259
				if (w->cargo_count != 0) {
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1260
					if (v->cargo_type == w->cargo_type &&
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1261
							u->last_station_visited == w->cargo_source)
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1262
						other_has_same_type = true;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1263
					other_has_any_cargo = true;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1264
				}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1265
			}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1266
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1267
			if (has_space_for_same_type) {
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1268
				if (other_has_same_type) return true;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1269
				if (other_has_any_cargo && !has_any_cargo) return true;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1270
			}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1271
		}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1272
	}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1273
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1274
	return false;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1275
}
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1276
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
int LoadUnloadVehicle(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1278
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
	int profit = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
	int unloading_time = 20;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1281
	Vehicle *u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
	int result = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
	int last_visited;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1284
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1285
	GoodsEntry *ge;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
	int t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
	uint count, cap;
260
32fcaaf9f4ef (svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents: 240
diff changeset
  1288
	byte old_player;
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1289
	bool completely_empty = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1291
	assert(v->current_order.type == OT_LOADING);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
	v->cur_speed = 0;
260
32fcaaf9f4ef (svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents: 240
diff changeset
  1294
	old_player = _current_player;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
	_current_player = v->owner;
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
	st = DEREF_STATION(last_visited = v->last_station_visited);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1298
499
509765981930 (svn r792) Small cleanup: Kill a goto
tron
parents: 445
diff changeset
  1299
	for (; v != NULL; v = v->next) {
509765981930 (svn r792) Small cleanup: Kill a goto
tron
parents: 445
diff changeset
  1300
		if (v->cargo_cap == 0) continue;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1301
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1302
		ge = &st->goods[v->cargo_type];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1304
		/* unload? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1305
		if (v->cargo_count != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1306
			if (v->cargo_source != (byte)last_visited && ge->waiting_acceptance & 0x8000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1307
				// deliver goods to the station
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1308
				unloading_time += v->cargo_count; /* TTDBUG: bug in original TTD */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1309
				profit += DeliverGoods(v->cargo_count, v->cargo_type, v->cargo_source, last_visited, v->cargo_days);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1310
				result |= 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
				v->cargo_count = 0;
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1312
			} else if (u->current_order.flags & OF_UNLOAD) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1313
				/* unload goods and let it wait at the station */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
				st->time_since_unload = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1315
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
				if ((t=ge->waiting_acceptance & 0xFFF) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
					// No goods waiting at station
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
					ge->enroute_time = v->cargo_days;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
					ge->enroute_from = v->cargo_source;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1320
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
					// Goods already waiting at station. Set counters to the worst value.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1322
					if (v->cargo_days >= ge->enroute_time)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1323
						ge->enroute_time = v->cargo_days;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
					if ((byte)last_visited != ge->enroute_from)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1325
						ge->enroute_from = v->cargo_source;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1326
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
				// Update amount of waiting cargo
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1328
				ge->waiting_acceptance = (ge->waiting_acceptance &~0xFFF) | min(v->cargo_count + t, 0xFFF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1329
				result |= 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1330
				v->cargo_count = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
			}
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1332
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1333
			if (v->cargo_count != 0)
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1334
				completely_empty = false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1335
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1336
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1337
		/* don't pick up goods that we unloaded */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
  1338
		if (u->current_order.flags & OF_UNLOAD) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1339
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
		/* update stats */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
		ge->days_since_pickup = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
		t = u->max_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
		if (u->type == VEH_Road) t >>=1;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1344
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
		// if last speed is 0, we treat that as if no vehicle has ever visited the station.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1346
		ge->last_speed = t < 255 ? t : 255;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1347
		ge->last_age = _cur_year - v->build_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1349
		// If there's goods waiting at the station, and the vehicle
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
		//  has capacity for it, load it on the vehicle.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
		if ((count=ge->waiting_acceptance & 0xFFF) != 0 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
				(cap = v->cargo_cap - v->cargo_count) != 0) {
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1353
			if (v->cargo_count == 0)
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1354
				TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1355
523
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1356
			/* Skip loading this vehicle if another train/vehicle is already handling
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1357
			 * the same cargo type at this station */
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1358
			if (_patches.improved_load && LoadWait(v,u)) continue;
1184a22723ad (svn r853) -Feature: Implement improved vehicle loading algorithm
tron
parents: 507
diff changeset
  1359
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1360
			/* TODO: Regarding this, when we do gradual loading, we
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1361
			 * should first unload all vehicles and then start
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1362
			 * loading them. Since this will cause
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1363
			 * VEHICLE_TRIGGER_EMPTY to be called at the time when
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1364
			 * the whole vehicle chain is really totally empty, the
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1365
			 * @completely_empty assignment can then be safely
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1366
			 * removed; that's how TTDPatch behaves too. --pasky */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1367
			completely_empty = false;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1368
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1369
			if (cap > count) cap = count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
			v->cargo_count += cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1371
			ge->waiting_acceptance -= cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
			unloading_time += cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
			st->time_since_load = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 147
diff changeset
  1374
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1375
			// And record the source of the cargo, and the days in travel.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
			v->cargo_source = ge->enroute_from;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1377
			v->cargo_days = ge->enroute_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
			result |= 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1379
			st->last_vehicle = v->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1380
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1381
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1383
	v = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1384
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
	if (v->type == VEH_Train) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
		int num = - (int)GetStationPlatforms(st, v->tile) * 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1387
		do num++; while ( (v=v->next) != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
		if (num > 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1389
			unloading_time <<=1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1390
			unloading_time += num * unloading_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1391
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1392
		v = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1393
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1394
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1395
	v->load_unload_time_rem = unloading_time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1396
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1397
	if (completely_empty) {
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1398
		TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1399
	}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 337
diff changeset
  1400
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1401
	if (result != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1402
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1403
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1404
		if (result & 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1405
			InvalidateWindow(WC_STATION_VIEW, last_visited);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1406
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1407
		if (profit != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1408
			v->profit_this_year += profit;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1409
			SubtractMoneyFromPlayer(-profit);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
			if (_current_player == _local_player)
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 534
diff changeset
  1412
				SndPlayVehicleFx(SND_14_CASHTILL, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1413
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1414
			ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, -profit);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1416
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1417
260
32fcaaf9f4ef (svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents: 240
diff changeset
  1418
	_current_player = old_player;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1419
	return result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1420
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
void PlayersMonthlyLoop()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1423
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1424
	PlayersGenStatistics();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1425
	if (_patches.inflation)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1426
		AddInflation();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
	PlayersPayInterest();
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 200
diff changeset
  1428
	// Reset the _current_player flag
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 200
diff changeset
  1429
	_current_player = OWNER_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1430
	HandleEconomyFluctuations();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1431
	SubsidyMonthlyHandler();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1432
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
static void DoAcquireCompany(Player *p)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
	Player *owner;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
	int i,pi;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
  1438
	int64 value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1440
	SetDParam(0, p->name_1);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1441
	SetDParam(1, p->name_2);
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 523
diff changeset
  1442
	SetDParam(2, p->bankrupt_value);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
	AddNewsItem( (StringID)(_current_player + 16*2), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1444
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1445
	// original code does this a little bit differently
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1446
	pi = p->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
	ChangeOwnershipOfPlayerItems(pi, _current_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1449
	if (p->bankrupt_value == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1450
		owner = DEREF_PLAYER(_current_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1451
		owner->current_loan += p->current_loan;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1452
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1453
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1454
	value = CalculateCompanyValue(p) >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1455
	for(i=0; i!=4; i++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1456
		if (p->share_owners[i] != 0xFF) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1457
			owner = DEREF_PLAYER(p->share_owners[i]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1458
			owner->money64 += value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1459
			owner->yearly_expenses[0][EXPENSES_OTHER] += value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1460
			UpdatePlayerMoney32(owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1461
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1462
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1463
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1464
	p->is_active = false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1465
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1466
	DeletePlayerWindows(pi);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1468
599
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1469
extern int GetAmountOwnedBy(Player *p, byte owner);
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1470
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1471
int32 CmdBuyShareInCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1472
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1473
	Player *p;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
  1474
	int64 cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1475
	byte *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1476
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1477
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1478
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1479
	p = DEREF_PLAYER(p1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1480
599
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1481
	/* Those lines are here for network-protection (clients can be slow) */
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1482
	if (GetAmountOwnedBy(p, OWNER_SPECTATOR) == 0)
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1483
		return 0;
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1484
	/* We can not buy out a real player in networking */
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1485
	if (GetAmountOwnedBy(p, OWNER_SPECTATOR) == 1 && !p->is_ai)
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1486
		return 0;
80e49e323483 (svn r1023) -Fix: [Network] [ 1083692 ] You can no longer buy out a company in MP
truelight
parents: 555
diff changeset
  1487
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
	cost = CalculateCompanyValue(p) >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
		b = p->share_owners;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1491
		while (*b != 0xFF) b++; /* share owners is guaranteed to contain at least one 0xFF */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
		*b = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1494
		for(i=0;p->share_owners[i] == _current_player;) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1495
			if (++i == 4) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1496
				p->bankrupt_value = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1497
				DoAcquireCompany(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1498
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1499
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1500
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1501
		InvalidateWindow(WC_COMPANY, (int)p1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1502
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1503
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1504
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1505
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1506
int32 CmdSellShareInCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
	Player *p;
200
03b8104d1479 (svn r201) -Fix: [1025836] Company values bigger dan int32 were put to negative
truelight
parents: 193
diff changeset
  1509
	int64 cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
	byte *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
	p = DEREF_PLAYER(p1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
	/* 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
  1516
	cost = CalculateCompanyValue(p) >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
	cost = -(cost - (cost >> 7));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
		b = p->share_owners;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
		while (*b != _current_player) b++; /* share owners is guaranteed to contain player */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
		*b = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
		InvalidateWindow(WC_COMPANY, (int)p1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1525
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
int32 CmdBuyCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1530
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
	p = DEREF_PLAYER(p1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1533
	if (flags & 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1534
		DoAcquireCompany(p);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
	return p->bankrupt_value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
// Prices
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1540
static void SaveLoad_PRIC()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1541
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1542
	SlArray(&_price, NUM_PRICES, SLE_INT32);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1543
	SlArray(&_price_frac, NUM_PRICES, SLE_UINT16);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
// Cargo payment rates
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
static void SaveLoad_CAPR()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1548
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
	SlArray(&_cargo_payment_rates, NUM_CARGO, SLE_INT32);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1550
	SlArray(&_cargo_payment_rates_frac, NUM_CARGO, SLE_UINT16);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
static const byte _economy_desc[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
	SLE_VAR(Economy,max_loan,						SLE_INT32),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
	SLE_VAR(Economy,max_loan_unround,		SLE_INT32),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
	SLE_VAR(Economy,fluct,							SLE_FILE_I16 | SLE_VAR_I32),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
	SLE_VAR(Economy,interest_rate,			SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1558
	SLE_VAR(Economy,infl_amount,				SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
	SLE_VAR(Economy,infl_amount_pr,			SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
// Economy variables
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
static void SaveLoad_ECMY()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1566
	SlObject(&_economy, &_economy_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
const ChunkHandler _economy_chunk_handlers[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
	{ '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
  1571
	{ 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1572
	{ 'SUBS', Save_SUBS,			Load_SUBS, CH_ARRAY},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1573
	{ 'ECMY', SaveLoad_ECMY, SaveLoad_ECMY, CH_RIFF | CH_LAST},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1574
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1575
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1576
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1577
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1578