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