annotate src/economy.cpp @ 17213:4111445c818e draft

(svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
author rubidium <rubidium@openttd.org>
date Fri, 04 Feb 2011 10:31:40 +0000
parents c62cecf4dade
children 46deeeb7a878
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1 /* $Id$ */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2
12778
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12756
diff changeset
3 /*
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12756
diff changeset
4 * This file is part of OpenTTD.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12756
diff changeset
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12756
diff changeset
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12756
diff changeset
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12756
diff changeset
8 */
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12756
diff changeset
9
9111
d48433370037 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium <rubidium@openttd.org>
parents: 9058
diff changeset
10 /** @file economy.cpp Handling of the economy. */
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
11
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
12 #include "stdafx.h"
10208
39cf8eebfda5 (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium <rubidium@openttd.org>
parents: 10207
diff changeset
13 #include "company_func.h"
8116
9cc845deddfe (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium <rubidium@openttd.org>
parents: 8114
diff changeset
14 #include "command_func.h"
12826
c2f3eaf3b353 (svn r17316) -Codechange: use Industry::GetByTile() instead of GetIndustryByTile()
smatz <smatz@openttd.org>
parents: 12778
diff changeset
15 #include "industry.h"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
16 #include "town.h"
8763
d6e363672edb (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium <rubidium@openttd.org>
parents: 8626
diff changeset
17 #include "news_func.h"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
18 #include "network/network.h"
9428
ba7cdcf21f4c (svn r13343) -Codechange: reorder/move variable/functions in the network headers so that nothing from the network directory needs to include basically all network headers.
rubidium <rubidium@openttd.org>
parents: 9413
diff changeset
19 #include "network/network_func.h"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
20 #include "vehicle_gui.h"
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents: 10647
diff changeset
21 #include "ai/ai.hpp"
5854
b52317eb405c (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater <Darkvater@openttd.org>
parents: 5683
diff changeset
22 #include "aircraft.h"
17152
c62cecf4dade (svn r21890) -Cleanup: remove some unneeded includes
rubidium <rubidium@openttd.org>
parents: 16923
diff changeset
23 #include "newgrf_engine.h"
c62cecf4dade (svn r21890) -Cleanup: remove some unneeded includes
rubidium <rubidium@openttd.org>
parents: 16923
diff changeset
24 #include "engine_base.h"
c62cecf4dade (svn r21890) -Cleanup: remove some unneeded includes
rubidium <rubidium@openttd.org>
parents: 16923
diff changeset
25 #include "ground_vehicle.hpp"
13466
a6e569a5cc14 (svn r17976) -Codechange: Move CargoClass to cargotype.h and clean up including of newgrf_cargo.h
frosch <frosch@openttd.org>
parents: 13325
diff changeset
26 #include "newgrf_cargo.h"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
27 #include "newgrf_sound.h"
7229
39b4e2291424 (svn r10508) -Codechange: allow customizable animation schemes for industries.
rubidium <rubidium@openttd.org>
parents: 7199
diff changeset
28 #include "newgrf_industrytiles.h"
9003
ac23e012c9d7 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138 <peter1138@openttd.org>
parents: 8885
diff changeset
29 #include "newgrf_station.h"
14620
a2d95ade0e87 (svn r19197) -Codechange: introduce animation callbacks for airport tiles
yexo <yexo@openttd.org>
parents: 14353
diff changeset
30 #include "newgrf_airporttiles.h"
15744
f9e8032ac213 (svn r20411) -Codechange: rename unmovables as quite a lot of them are actually movable; e.g. HQ and owned land are pretty movable.
rubidium <rubidium@openttd.org>
parents: 15729
diff changeset
31 #include "object.h"
6643
18d58b36b9b3 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium <rubidium@openttd.org>
parents: 6639
diff changeset
32 #include "group.h"
8114
2d6af5d7a142 (svn r11675) -Codechange: split the string types from the string functions.
rubidium <rubidium@openttd.org>
parents: 8106
diff changeset
33 #include "strings_func.h"
8131
e300ac8001ae (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents: 8121
diff changeset
34 #include "functions.h"
e300ac8001ae (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents: 8121
diff changeset
35 #include "window_func.h"
8140
fb8a05d579da (svn r11702) -Codechange: move all date related stuff to date*.
rubidium <rubidium@openttd.org>
parents: 8139
diff changeset
36 #include "date_func.h"
8144
d18c8a0bb638 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium <rubidium@openttd.org>
parents: 8140
diff changeset
37 #include "vehicle_func.h"
8157
019833e42fda (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium <rubidium@openttd.org>
parents: 8155
diff changeset
38 #include "sound_func.h"
9058
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
39 #include "autoreplace_func.h"
10208
39cf8eebfda5 (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium <rubidium@openttd.org>
parents: 10207
diff changeset
40 #include "company_gui.h"
10158
c429fccf1aea (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz <smatz@openttd.org>
parents: 10146
diff changeset
41 #include "signs_base.h"
12297
39ea618c207e (svn r16714) -Codechange: use pool-like accessors for Subsidy
smatz <smatz@openttd.org>
parents: 12294
diff changeset
42 #include "subsidy_base.h"
11997
e2187548a5e4 (svn r16403) -Codechange: move code related to subsidies to separate file
smatz <smatz@openttd.org>
parents: 11987
diff changeset
43 #include "subsidy_func.h"
12228
240adc64d01a (svn r16643) -Codechange: replace GetStationByTile() by Station::GetByTile()
smatz <smatz@openttd.org>
parents: 12143
diff changeset
44 #include "station_base.h"
12475
c133b51890f4 (svn r16912) -Codechange: split waypoint.h in waypoint_base.h and waypoint_func.h
rubidium <rubidium@openttd.org>
parents: 12467
diff changeset
45 #include "waypoint_base.h"
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
46 #include "economy_base.h"
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
47 #include "core/pool_func.hpp"
14258
a899d4e5ee1a (svn r18809) -Codechange/Cleanup: remove unneeded headers from some files, if a header require a header make it include that header
rubidium <rubidium@openttd.org>
parents: 14213
diff changeset
48 #include "newgrf.h"
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
49 #include "core/backup_type.hpp"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
50
8264
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8254
diff changeset
51 #include "table/strings.h"
12593
73b580ddf33f (svn r17036) -Codechange: Split price bases from economy.cpp to table/pricebase.h.
frosch <frosch@openttd.org>
parents: 12496
diff changeset
52 #include "table/pricebase.h"
8264
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8254
diff changeset
53
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
54
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
55 /* Initialize the cargo payment-pool */
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
56 CargoPaymentPool _cargo_payment_pool("CargoPayment");
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
57 INSTANTIATE_POOL_METHODS(CargoPayment)
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
58
7926
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
59 /**
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
60 * Multiply two integer values and shift the results to right.
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
61 *
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
62 * This function multiplies two integer values. The result is
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
63 * shifted by the amount of shift to right.
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
64 *
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
65 * @param a The first integer
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
66 * @param b The second integer
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
67 * @param shift The amount to shift the value to right.
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
68 * @return The shifted result
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
69 */
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
70 static inline int32 BigMulS(const int32 a, const int32 b, const uint8 shift)
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
71 {
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
72 return (int32)((int64)a * (int64)b >> shift);
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
73 }
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
74
10450
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
75 typedef SmallVector<Industry *, 16> SmallIndustryList;
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
76
15601
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
77 /**
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
78 * Score info, values used for computing the detailed performance rating.
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
79 */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
80 const ScoreInfo _score_info[] = {
15601
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
81 { 120, 100}, // SCORE_VEHICLES
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
82 { 80, 100}, // SCORE_STATIONS
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
83 { 10000, 100}, // SCORE_MIN_PROFIT
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
84 { 50000, 50}, // SCORE_MIN_INCOME
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
85 { 100000, 100}, // SCORE_MAX_INCOME
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
86 { 40000, 400}, // SCORE_DELIVERED
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
87 { 8, 50}, // SCORE_CARGO
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
88 {10000000, 50}, // SCORE_MONEY
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
89 { 250000, 50}, // SCORE_LOAN
4d1a28b41236 (svn r20262) -Codechange: Remove unused ScoreInfo::id field, and doxyment the data structures.
alberth <alberth@openttd.org>
parents: 15552
diff changeset
90 { 0, 0} // SCORE_TOTAL
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
91 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
92
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
93 int _score_part[MAX_COMPANIES][SCORE_END];
8116
9cc845deddfe (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium <rubidium@openttd.org>
parents: 8114
diff changeset
94 Economy _economy;
9cc845deddfe (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium <rubidium@openttd.org>
parents: 8114
diff changeset
95 Prices _price;
8119
b6ec923e9fa8 (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium <rubidium@openttd.org>
parents: 8116
diff changeset
96 Money _additional_cash_required;
13743
c84d609b6877 (svn r18268) -Feature: [NewGRF] Make price base multipliers related to vehicles only apply to the GRF locally, if it defines engines of that type itself.
frosch <frosch@openttd.org>
parents: 13741
diff changeset
97 static PriceMultipliers _price_base_multiplier;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
98
14353
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
99 /**
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
100 * Calculate the value of the company. That is the value of all
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
101 * assets (vehicles, stations, etc) and money minus the loan,
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
102 * except when including_loan is \c false which is useful when
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
103 * we want to calculate the value for bankruptcy.
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
104 * @param c the company to get the value of.
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
105 * @param including_loan include the loan in the company value.
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
106 * @return the value of the company.
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
107 */
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
108 Money CalculateCompanyValue(const Company *c, bool including_loan)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
109 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
110 Owner owner = c->index;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
111
7781
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
112 Station *st;
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
113 uint num = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
114
7781
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
115 FOR_ALL_STATIONS(st) {
12319
712bfd1aca6c (svn r16736) -Codechange: give some station enums a name and use that instead of 'byte'.
rubidium <rubidium@openttd.org>
parents: 12297
diff changeset
116 if (st->owner == owner) num += CountBits((byte)st->facilities);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
117 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
118
15652
354307d245e5 (svn r20317) -Codechange: Move variable declaration to their first use.
alberth <alberth@openttd.org>
parents: 15623
diff changeset
119 Money value = num * _price[PR_STATION_VALUE] * 25;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
120
7781
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
121 Vehicle *v;
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
122 FOR_ALL_VEHICLES(v) {
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
123 if (v->owner != owner) continue;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
124
7781
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
125 if (v->type == VEH_TRAIN ||
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
126 v->type == VEH_ROAD ||
12384
d4b73a406cf3 (svn r16814) -Codechange: make IsNormalAircraft() member of Aircraft
smatz <smatz@openttd.org>
parents: 12383
diff changeset
127 (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft()) ||
7781
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
128 v->type == VEH_SHIP) {
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
129 value += v->value * 3 >> 1;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
130 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
131 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
132
6953
41fd36025784 (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium <rubidium@openttd.org>
parents: 6952
diff changeset
133 /* Add real money value */
14353
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
134 if (including_loan) value -= c->current_loan;
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
135 value += c->money;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
136
7781
67423408cf20 (svn r11331) -Fix: do not misuse CommandCost for overflow safe stuff as Money supports that now too.
rubidium <rubidium@openttd.org>
parents: 7763
diff changeset
137 return max(value, (Money)1);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
138 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
139
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
140 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
141 * if update is set to true, the economy is updated with this score
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
142 * (also the house is updated, should only be true in the on-tick event)
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
143 * @param update the economy with calculated score
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
144 * @param c company been evaluated
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
145 * @return actual score of this company
15623
c62577640878 (svn r20286) -Codechange: Unify end of doxygen comments.
frosch <frosch@openttd.org>
parents: 15620
diff changeset
146 *
c62577640878 (svn r20286) -Codechange: Unify end of doxygen comments.
frosch <frosch@openttd.org>
parents: 15620
diff changeset
147 */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
148 int UpdateCompanyRatingAndValue(Company *c, bool update)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
149 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
150 Owner owner = c->index;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
151 int score = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
152
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
153 memset(_score_part[owner], 0, sizeof(_score_part[owner]));
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
154
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
155 /* Count vehicles */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
156 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
157 Vehicle *v;
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6957
diff changeset
158 Money min_profit = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
159 bool min_profit_first = true;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
160 uint num = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
161
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
162 FOR_ALL_VEHICLES(v) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
163 if (v->owner != owner) continue;
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
164 if (IsCompanyBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) {
12991
d3df6e84bf71 (svn r17485) -Change [FS2459]: make the performance ratings harder to exploit; only count profitable vehicles and recently serviced stations.
rubidium <rubidium@openttd.org>
parents: 12956
diff changeset
165 if (v->profit_last_year > 0) num++; // For the vehicle score only count profitable vehicles
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
166 if (v->age > 730) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
167 /* Find the vehicle with the lowest amount of profit */
8614
36bb76188638 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz <smatz@openttd.org>
parents: 8556
diff changeset
168 if (min_profit_first || min_profit > v->profit_last_year) {
36bb76188638 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz <smatz@openttd.org>
parents: 8556
diff changeset
169 min_profit = v->profit_last_year;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
170 min_profit_first = false;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
171 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
172 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
173 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
174 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
175
8614
36bb76188638 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz <smatz@openttd.org>
parents: 8556
diff changeset
176 min_profit >>= 8; // remove the fract part
36bb76188638 (svn r12197) -Fix [FS#1788](r12134): show correct last year profit when the train had negative income
smatz <smatz@openttd.org>
parents: 8556
diff changeset
177
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
178 _score_part[owner][SCORE_VEHICLES] = num;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
179 /* Don't allow negative min_profit to show */
15552
683f2dbe75bb (svn r20211) -Codechange: Indented code should have curly braces around it.
alberth <alberth@openttd.org>
parents: 15511
diff changeset
180 if (min_profit > 0) {
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6957
diff changeset
181 _score_part[owner][SCORE_MIN_PROFIT] = ClampToI32(min_profit);
15552
683f2dbe75bb (svn r20211) -Codechange: Indented code should have curly braces around it.
alberth <alberth@openttd.org>
parents: 15511
diff changeset
182 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
183 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
184
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
185 /* Count stations */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
186 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
187 uint num = 0;
10647
592ae9307430 (svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents: 10571
diff changeset
188 const Station *st;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
189
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
190 FOR_ALL_STATIONS(st) {
12991
d3df6e84bf71 (svn r17485) -Change [FS2459]: make the performance ratings harder to exploit; only count profitable vehicles and recently serviced stations.
rubidium <rubidium@openttd.org>
parents: 12956
diff changeset
191 /* Only count stations that are actually serviced */
d3df6e84bf71 (svn r17485) -Change [FS2459]: make the performance ratings harder to exploit; only count profitable vehicles and recently serviced stations.
rubidium <rubidium@openttd.org>
parents: 12956
diff changeset
192 if (st->owner == owner && (st->time_since_load <= 20 || st->time_since_unload <= 20)) num += CountBits((byte)st->facilities);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
193 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
194 _score_part[owner][SCORE_STATIONS] = num;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
195 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
196
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
197 /* Generate statistics depending on recent income statistics */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
198 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
199 int numec = min(c->num_valid_stat_ent, 12);
6953
41fd36025784 (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium <rubidium@openttd.org>
parents: 6952
diff changeset
200 if (numec != 0) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
201 const CompanyEconomyEntry *cee = c->old_economy;
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
202 Money min_income = cee->income + cee->expenses;
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
203 Money max_income = cee->income + cee->expenses;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
204
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
205 do {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
206 min_income = min(min_income, cee->income + cee->expenses);
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
207 max_income = max(max_income, cee->income + cee->expenses);
11361
02313cc6114e (svn r15711) -Codechange: lots of whitespace cleanup/fixes
rubidium <rubidium@openttd.org>
parents: 11234
diff changeset
208 } while (++cee, --numec);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
209
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
210 if (min_income > 0) {
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6957
diff changeset
211 _score_part[owner][SCORE_MIN_INCOME] = ClampToI32(min_income);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
212 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
213
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6957
diff changeset
214 _score_part[owner][SCORE_MAX_INCOME] = ClampToI32(max_income);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
215 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
216 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
217
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
218 /* Generate score depending on amount of transported cargo */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
219 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
220 const CompanyEconomyEntry *cee;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
221 int numec;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
222 uint32 total_delivered;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
223
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
224 numec = min(c->num_valid_stat_ent, 4);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
225 if (numec != 0) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
226 cee = c->old_economy;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
227 total_delivered = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
228 do {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
229 total_delivered += cee->delivered_cargo;
11361
02313cc6114e (svn r15711) -Codechange: lots of whitespace cleanup/fixes
rubidium <rubidium@openttd.org>
parents: 11234
diff changeset
230 } while (++cee, --numec);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
231
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
232 _score_part[owner][SCORE_DELIVERED] = total_delivered;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
233 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
234 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
235
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
236 /* Generate score for variety of cargo */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
237 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
238 uint num = CountBits(c->cargo_types);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
239 _score_part[owner][SCORE_CARGO] = num;
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
240 if (update) c->cargo_types = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
241 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
242
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
243 /* Generate score for company's money */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
244 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
245 if (c->money > 0) {
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
246 _score_part[owner][SCORE_MONEY] = ClampToI32(c->money);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
247 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
248 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
249
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
250 /* Generate score for loan */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
251 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
252 _score_part[owner][SCORE_LOAN] = ClampToI32(_score_info[SCORE_LOAN].needed - c->current_loan);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
253 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
254
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
255 /* Now we calculate the score for each item.. */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
256 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
257 int total_score = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
258 int s;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
259 score = 0;
5587
c44c070c5032 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium <rubidium@openttd.org>
parents: 5584
diff changeset
260 for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
261 /* Skip the total */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
262 if (i == SCORE_TOTAL) continue;
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
263 /* Check the score */
7922
ff1975ced735 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13 <skidd13@openttd.org>
parents: 7860
diff changeset
264 s = Clamp(_score_part[owner][i], 0, _score_info[i].needed) * _score_info[i].score / _score_info[i].needed;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
265 score += s;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
266 total_score += _score_info[i].score;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
267 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
268
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
269 _score_part[owner][SCORE_TOTAL] = score;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
270
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
271 /* We always want the score scaled to SCORE_MAX (1000) */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
272 if (total_score != SCORE_MAX) score = score * SCORE_MAX / total_score;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
273 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
274
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
275 if (update) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
276 c->old_economy[0].performance_history = score;
15676
87f2261652e1 (svn r20341) -Codechange: pass the tile of the HQ instead of the company to UpdateCompanyHQ
rubidium <rubidium@openttd.org>
parents: 15655
diff changeset
277 UpdateCompanyHQ(c->location_of_HQ, score);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
278 c->old_economy[0].company_value = CalculateCompanyValue(c);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
279 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
280
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
281 SetWindowDirty(WC_PERFORMANCE_DETAIL, 0);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
282 return score;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
283 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
284
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
285 /* use INVALID_OWNER as new_owner to delete the company. */
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
286 void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
287 {
15035
cd12f24222ac (svn r19651) -Fix [FS#3745]: when a company is sold, move connected clients to spectators
smatz <smatz@openttd.org>
parents: 15002
diff changeset
288 #ifdef ENABLE_NETWORK
cd12f24222ac (svn r19651) -Fix [FS#3745]: when a company is sold, move connected clients to spectators
smatz <smatz@openttd.org>
parents: 15002
diff changeset
289 /* In all cases, make spectators of clients connected to that company */
cd12f24222ac (svn r19651) -Fix [FS#3745]: when a company is sold, move connected clients to spectators
smatz <smatz@openttd.org>
parents: 15002
diff changeset
290 if (_networking) NetworkClientsToSpectators(old_owner);
cd12f24222ac (svn r19651) -Fix [FS#3745]: when a company is sold, move connected clients to spectators
smatz <smatz@openttd.org>
parents: 15002
diff changeset
291 #endif /* ENABLE_NETWORK */
cd12f24222ac (svn r19651) -Fix [FS#3745]: when a company is sold, move connected clients to spectators
smatz <smatz@openttd.org>
parents: 15002
diff changeset
292
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
293 Town *t;
15292
e2c62d70dfcf (svn r19931) -Fix (r19914): Convert assertion in Backup<> destructor into DEBUG() output. It was triggered on exceptions, especially when aborting world generation.
frosch <frosch@openttd.org>
parents: 15279
diff changeset
294 Backup<CompanyByte> cur_company(_current_company, old_owner, FILE_LINE);
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
295
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
296 assert(old_owner != new_owner);
6405
3f3b30a14c26 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar <celestar@openttd.org>
parents: 6403
diff changeset
297
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
298 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
299 Company *c;
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
300 uint i;
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
301
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
302 /* See if the old_owner had shares in other companies */
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
303 FOR_ALL_COMPANIES(c) {
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
304 for (i = 0; i < 4; i++) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
305 if (c->share_owners[i] == old_owner) {
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
306 /* Sell his shares */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
307 CommandCost res = DoCommand(0, c->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
14136
54dfd3720fa1 (svn r18683) -Fix: it's not an other
rubidium <rubidium@openttd.org>
parents: 13998
diff changeset
308 /* Because we are in a DoCommand, we can't just execute another one and
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
309 * expect the money to be removed. We need to do it ourself! */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
310 SubtractMoneyFromCompany(res);
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
311 }
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
312 }
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
313 }
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
314
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
315 /* Sell all the shares that people have on this company */
15292
e2c62d70dfcf (svn r19931) -Fix (r19914): Convert assertion in Backup<> destructor into DEBUG() output. It was triggered on exceptions, especially when aborting world generation.
frosch <frosch@openttd.org>
parents: 15279
diff changeset
316 Backup<CompanyByte> cur_company2(_current_company, FILE_LINE);
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11730
diff changeset
317 c = Company::Get(old_owner);
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
318 for (i = 0; i < 4; i++) {
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
319 cur_company2.Change(c->share_owners[i]);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
320 if (_current_company != INVALID_OWNER) {
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
321 /* Sell the shares */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
322 CommandCost res = DoCommand(0, old_owner, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
14136
54dfd3720fa1 (svn r18683) -Fix: it's not an other
rubidium <rubidium@openttd.org>
parents: 13998
diff changeset
323 /* Because we are in a DoCommand, we can't just execute another one and
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
324 * expect the money to be removed. We need to do it ourself! */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
325 SubtractMoneyFromCompany(res);
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
326 }
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
327 }
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
328 cur_company2.Restore();
6403
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
329 }
8c022dabd50e (svn r9533) -Fix [FS#274]: when a company is removed (either via auto-clean, bankrupt, or take over), sell all the shares he has first, then sell the shares all people have on this company, and then remove the company.
truelight <truelight@openttd.org>
parents: 6369
diff changeset
330
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
331 /* Temporarily increase the company's money, to be sure that
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
332 * removing his/her property doesn't fail because of lack of money.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
333 * Not too drastically though, because it could overflow */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
334 if (new_owner == INVALID_OWNER) {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11730
diff changeset
335 Company::Get(old_owner)->money = UINT64_MAX >> 2; // jackpot ;p
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
336 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
337
12658
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
338 Subsidy *s;
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
339 FOR_ALL_SUBSIDIES(s) {
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
340 if (s->awarded == old_owner) {
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
341 if (new_owner == INVALID_OWNER) {
12669
406f4ae1e2ca (svn r17124) -Codechange: store subsidies in a pool (instead of an array)
smatz <smatz@openttd.org>
parents: 12658
diff changeset
342 delete s;
12658
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
343 } else {
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
344 s->awarded = new_owner;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
345 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
346 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
347 }
12669
406f4ae1e2ca (svn r17124) -Codechange: store subsidies in a pool (instead of an array)
smatz <smatz@openttd.org>
parents: 12658
diff changeset
348 if (new_owner == INVALID_OWNER) RebuildSubsidisedSourceAndDestinationCache();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
349
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
350 /* Take care of rating in towns */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
351 FOR_ALL_TOWNS(t) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
352 /* If a company takes over, give the ratings to that company. */
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
353 if (new_owner != INVALID_OWNER) {
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
354 if (HasBit(t->have_ratings, old_owner)) {
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
355 if (HasBit(t->have_ratings, new_owner)) {
11368
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11361
diff changeset
356 /* use max of the two ratings. */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
357 t->ratings[new_owner] = max(t->ratings[new_owner], t->ratings[old_owner]);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
358 } else {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
359 SetBit(t->have_ratings, new_owner);
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
360 t->ratings[new_owner] = t->ratings[old_owner];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
361 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
362 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
363 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
364
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
365 /* Reset the ratings for the old owner */
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
366 t->ratings[old_owner] = RATING_INITIAL;
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
367 ClrBit(t->have_ratings, old_owner);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
368 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
369
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
370 {
10498
730117b172e1 (svn r14753) -Fix (r1): after buying a company, one could have more vehicles with the same UnitID
smatz <smatz@openttd.org>
parents: 10495
diff changeset
371 FreeUnitIDGenerator unitidgen[] = {
730117b172e1 (svn r14753) -Fix (r1): after buying a company, one could have more vehicles with the same UnitID
smatz <smatz@openttd.org>
parents: 10495
diff changeset
372 FreeUnitIDGenerator(VEH_TRAIN, new_owner), FreeUnitIDGenerator(VEH_ROAD, new_owner),
730117b172e1 (svn r14753) -Fix (r1): after buying a company, one could have more vehicles with the same UnitID
smatz <smatz@openttd.org>
parents: 10495
diff changeset
373 FreeUnitIDGenerator(VEH_SHIP, new_owner), FreeUnitIDGenerator(VEH_AIRCRAFT, new_owner)
730117b172e1 (svn r14753) -Fix (r1): after buying a company, one could have more vehicles with the same UnitID
smatz <smatz@openttd.org>
parents: 10495
diff changeset
374 };
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
375
10498
730117b172e1 (svn r14753) -Fix (r1): after buying a company, one could have more vehicles with the same UnitID
smatz <smatz@openttd.org>
parents: 10495
diff changeset
376 Vehicle *v;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
377 FOR_ALL_VEHICLES(v) {
10492
28fa021b003c (svn r14747) -Codechange: remove DeleteVehicleChain in favour of the vehicle destructor as that was already doing the same for a few vehicle types.
rubidium <rubidium@openttd.org>
parents: 10479
diff changeset
378 if (v->owner == old_owner && IsCompanyBuildableVehicleType(v->type)) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
379 if (new_owner == INVALID_OWNER) {
10492
28fa021b003c (svn r14747) -Codechange: remove DeleteVehicleChain in favour of the vehicle destructor as that was already doing the same for a few vehicle types.
rubidium <rubidium@openttd.org>
parents: 10479
diff changeset
380 if (v->Previous() == NULL) delete v;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
381 } else {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
382 v->owner = new_owner;
11085
8da1855e9f14 (svn r15428) -Codechange: consistently use colour instead of having both color and colour.
rubidium <rubidium@openttd.org>
parents: 11056
diff changeset
383 v->colourmap = PAL_NONE;
12383
3c7e93b91c73 (svn r16813) -Codechange: make IsEngineCountable() member of Vehicle
smatz <smatz@openttd.org>
parents: 12319
diff changeset
384 if (v->IsEngineCountable()) Company::Get(new_owner)->num_engines[v->engine_type]++;
10498
730117b172e1 (svn r14753) -Fix (r1): after buying a company, one could have more vehicles with the same UnitID
smatz <smatz@openttd.org>
parents: 10495
diff changeset
385 if (v->IsPrimaryVehicle()) v->unitnumber = unitidgen[v->type].NextID();
16871
39e28608198f (svn r21605) -Fix [FS#4324]: crash due to cargo payments belonging to an non-existing company
rubidium <rubidium@openttd.org>
parents: 16788
diff changeset
386
39e28608198f (svn r21605) -Fix [FS#4324]: crash due to cargo payments belonging to an non-existing company
rubidium <rubidium@openttd.org>
parents: 16788
diff changeset
387 /* Invalidate the vehicle's cargo payment "owner cache". */
39e28608198f (svn r21605) -Fix [FS#4324]: crash due to cargo payments belonging to an non-existing company
rubidium <rubidium@openttd.org>
parents: 16788
diff changeset
388 if (v->cargo_payment != NULL) v->cargo_payment->owner = NULL;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
389 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
390 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
391 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
392 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
393
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
394 /* Change ownership of tiles */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
395 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
396 TileIndex tile = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
397 do {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
398 ChangeTileOwner(tile, old_owner, new_owner);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
399 } while (++tile != MapSize());
8200
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
400
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
401 if (new_owner != INVALID_OWNER) {
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
402 /* Update all signals because there can be new segment that was owned by two companies
8342
441cda3cfbdb (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz <smatz@openttd.org>
parents: 8306
diff changeset
403 * and signals were not propagated
441cda3cfbdb (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz <smatz@openttd.org>
parents: 8306
diff changeset
404 * Similiar with crossings - it is needed to bar crossings that weren't before
441cda3cfbdb (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz <smatz@openttd.org>
parents: 8306
diff changeset
405 * because of different owner of crossing and approaching train */
8200
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
406 tile = 0;
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
407
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
408 do {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
409 if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, new_owner) && HasSignals(tile)) {
8200
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
410 TrackBits tracks = GetTrackBits(tile);
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
411 do { // there may be two tracks with signals for TRACK_BIT_HORZ and TRACK_BIT_VERT
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
412 Track track = RemoveFirstTrack(&tracks);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
413 if (HasSignalOnTrack(tile, track)) AddTrackToSignalBuffer(tile, track, new_owner);
8200
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
414 } while (tracks != TRACK_BIT_NONE);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
415 } else if (IsLevelCrossingTile(tile) && IsTileOwner(tile, new_owner)) {
8342
441cda3cfbdb (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz <smatz@openttd.org>
parents: 8306
diff changeset
416 UpdateLevelCrossing(tile);
8200
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
417 }
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
418 } while (++tile != MapSize());
8347
a83dd50f52f9 (svn r11913) -Fix (r11871): update signals after company bankrupt and disaster too
smatz <smatz@openttd.org>
parents: 8342
diff changeset
419 }
8306
ea2c123b3655 (svn r11871) -Fix [FS#1074]: do not update signals after each tile when building/removing a large block of track/signals/station
smatz <smatz@openttd.org>
parents: 8302
diff changeset
420
8347
a83dd50f52f9 (svn r11913) -Fix (r11871): update signals after company bankrupt and disaster too
smatz <smatz@openttd.org>
parents: 8342
diff changeset
421 /* update signals in buffer */
a83dd50f52f9 (svn r11913) -Fix (r11871): update signals after company bankrupt and disaster too
smatz <smatz@openttd.org>
parents: 8342
diff changeset
422 UpdateSignalsInBuffer();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
423 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
424
11234
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
425 /* convert owner of stations (including deleted ones, but excluding buoys) */
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
426 Station *st;
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
427 FOR_ALL_STATIONS(st) {
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
428 if (st->owner == old_owner) {
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
429 /* if a company goes bankrupt, set owner to OWNER_NONE so the sign doesn't disappear immediately
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
430 * also, drawing station window would cause reading invalid company's colour */
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
431 st->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
432 }
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
433 }
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
434
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
435 /* do the same for waypoints (we need to do this here so deleted waypoints are converted too) */
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
436 Waypoint *wp;
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
437 FOR_ALL_WAYPOINTS(wp) {
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
438 if (wp->owner == old_owner) {
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
439 wp->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
440 }
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
441 }
6ebdffe6e5e6 (svn r15588) -Fix: change owner of waypoints and deleted stations when merging companies or when a company benkrupts
smatz <smatz@openttd.org>
parents: 11221
diff changeset
442
9058
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
443 /* In all cases clear replace engine rules.
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
444 * Even if it was copied, it could interfere with new owner's rules */
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11730
diff changeset
445 RemoveAllEngineReplacementForCompany(Company::Get(old_owner));
9058
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
446
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
447 if (new_owner == INVALID_OWNER) {
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
448 RemoveAllGroupsForCompany(old_owner);
9058
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
449 } else {
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
450 Group *g;
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
451 FOR_ALL_GROUPS(g) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
452 if (g->owner == old_owner) g->owner = new_owner;
9058
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
453 }
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
454 }
c73011639858 (svn r12906) -Fix: vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (before new player start)
smatz <smatz@openttd.org>
parents: 9043
diff changeset
455
10158
c429fccf1aea (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz <smatz@openttd.org>
parents: 10146
diff changeset
456 Sign *si;
c429fccf1aea (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz <smatz@openttd.org>
parents: 10146
diff changeset
457 FOR_ALL_SIGNS(si) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
458 if (si->owner == old_owner) si->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
10158
c429fccf1aea (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz <smatz@openttd.org>
parents: 10146
diff changeset
459 }
c429fccf1aea (svn r14348) -Fix (r1667): signs were not updated on company bankrupcy/sell, they have colour of invalid player
smatz <smatz@openttd.org>
parents: 10146
diff changeset
460
11085
8da1855e9f14 (svn r15428) -Codechange: consistently use colour instead of having both color and colour.
rubidium <rubidium@openttd.org>
parents: 11056
diff changeset
461 /* Change colour of existing windows */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
462 if (new_owner != INVALID_OWNER) ChangeWindowOwner(old_owner, new_owner);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
463
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
464 cur_company.Restore();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
465
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
466 MarkWholeScreenDirty();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
467 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
468
15653
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
469 /**
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
470 * Check for bankruptcy of a company. Called every three months.
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
471 * @param c Company to check.
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
472 */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
473 static void CompanyCheckBankrupt(Company *c)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
474 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
475 /* If the company has money again, it does not go bankrupt */
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
476 if (c->money >= 0) {
12853
a1d8c582ef59 (svn r17343) -Codechange: it's bankruptcy, not bankrupcy, according to my Oxford Dictionary of English and Merriam-Webster.
rubidium <rubidium@openttd.org>
parents: 12852
diff changeset
477 c->quarters_of_bankruptcy = 0;
12852
c8f80b97fd61 (svn r17342) -Fix: reset bankruptcy checks when bankruptcy has been averted and don't set an unused timeout with a magic number
rubidium <rubidium@openttd.org>
parents: 12851
diff changeset
478 c->bankrupt_asked = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
479 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
480 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
481
12853
a1d8c582ef59 (svn r17343) -Codechange: it's bankruptcy, not bankrupcy, according to my Oxford Dictionary of English and Merriam-Webster.
rubidium <rubidium@openttd.org>
parents: 12852
diff changeset
482 c->quarters_of_bankruptcy++;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
483
12853
a1d8c582ef59 (svn r17343) -Codechange: it's bankruptcy, not bankrupcy, according to my Oxford Dictionary of English and Merriam-Webster.
rubidium <rubidium@openttd.org>
parents: 12852
diff changeset
484 switch (c->quarters_of_bankruptcy) {
10495
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
485 case 0:
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
486 case 1:
9658
27697e657d2e (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium <rubidium@openttd.org>
parents: 9652
diff changeset
487 break;
27697e657d2e (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium <rubidium@openttd.org>
parents: 9652
diff changeset
488
17213
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
489 case 2: {
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
490 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
491 cni->FillData(c);
11730
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11376
diff changeset
492 SetDParam(0, STR_NEWS_COMPANY_IN_TROUBLE_TITLE);
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11376
diff changeset
493 SetDParam(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION);
9658
27697e657d2e (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium <rubidium@openttd.org>
parents: 9652
diff changeset
494 SetDParamStr(2, cni->company_name);
12632
e41943276802 (svn r17075) -Codechange: rename ~750 strings to be more uniform with their relatives
rubidium <rubidium@openttd.org>
parents: 12615
diff changeset
495 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_TROUBLE, cni);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents: 10647
diff changeset
496 AI::BroadcastNewEvent(new AIEventCompanyInTrouble(c->index));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
497 break;
17213
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
498 }
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
499
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
500 case 3: {
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
501 /* Check if the company has any value.. if not, declare it bankrupt
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
502 * right now */
14353
098ac3e84f3b (svn r18908) -Fix [FS#3561]: on bankruptcy the company value did include the loan and as such the value at which you bought the company was too low
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
503 Money val = CalculateCompanyValue(c, false);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
504 if (val > 0) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
505 c->bankrupt_value = val;
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
506 c->bankrupt_asked = 1 << c->index; // Don't ask the owner
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
507 c->bankrupt_timeout = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
508 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
509 }
15655
0be6e9a4ca0b (svn r20320) -Doc: Small Doxygen and normal comment fixes, and an missed addition.
alberth <alberth@openttd.org>
parents: 15654
diff changeset
510 /* FALL THROUGH to case 4... */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
511 }
10495
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
512 default:
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
513 case 4:
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
514 if (!_networking && _local_company == c->index) {
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
515 /* If we are in offline mode, leave the company playing. Eg. there
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
516 * is no THE-END, otherwise mark the client as spectator to make sure
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
517 * he/she is no long in control of this company. However... when you
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
518 * join another company (cheat) the "unowned" company can bankrupt. */
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
519 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
520 break;
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
521 }
dd7d08e0ff23 (svn r14750) -Fix [FS#1993]: in an MP game in SP mode no company would go bankrupt. Furthermore companies that passed the "bankrupt" period (4 quarters) wouldn't go bankrupt when loading the game back in MP. Now any company that is in MP or not "currently controlled by the player" in SP will bankrupt.
rubidium <rubidium@openttd.org>
parents: 10493
diff changeset
522
17213
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
523 /* Actually remove the company. */
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
524 DoCommand(0, 2 | (c->index << 16), 0, DC_EXEC, CMD_COMPANY_CTRL);
4111445c818e (svn r21952) -Codechange: unify the company clearing code for bankruptcy with the command to remove companies
rubidium <rubidium@openttd.org>
parents: 17152
diff changeset
525 break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
526 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
527 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
528
15653
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
529 /**
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
530 * Update the finances of all companies.
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
531 * Pay for the stations, update the history graph, update ratings and company values, and deal with bankruptcy.
930fc2895ced (svn r20318) -Doc: Doxygen additions.
alberth <alberth@openttd.org>
parents: 15652
diff changeset
532 */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
533 static void CompaniesGenStatistics()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
534 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
535 Station *st;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
536
15292
e2c62d70dfcf (svn r19931) -Fix (r19914): Convert assertion in Backup<> destructor into DEBUG() output. It was triggered on exceptions, especially when aborting world generation.
frosch <frosch@openttd.org>
parents: 15279
diff changeset
537 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
538 FOR_ALL_STATIONS(st) {
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
539 cur_company.Change(st->owner);
13486
f2535c5098a0 (svn r18005) -Codechange: Convert the Prices struct into an array and an enum.
frosch <frosch@openttd.org>
parents: 13466
diff changeset
540 CommandCost cost(EXPENSES_PROPERTY, _price[PR_STATION_VALUE] >> 1);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
541 SubtractMoneyFromCompany(cost);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
542 }
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
543 cur_company.Restore();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
544
15552
683f2dbe75bb (svn r20211) -Codechange: Indented code should have curly braces around it.
alberth <alberth@openttd.org>
parents: 15511
diff changeset
545 if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_month)) return;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
546
15652
354307d245e5 (svn r20317) -Codechange: Move variable declaration to their first use.
alberth <alberth@openttd.org>
parents: 15623
diff changeset
547 Company *c;
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
548 FOR_ALL_COMPANIES(c) {
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
549 memmove(&c->old_economy[1], &c->old_economy[0], sizeof(c->old_economy) - sizeof(c->old_economy[0]));
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
550 c->old_economy[0] = c->cur_economy;
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
551 memset(&c->cur_economy, 0, sizeof(c->cur_economy));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
552
12615
5f44b757d251 (svn r17058) -Codechange: magic numbers removal.
belugas <belugas@openttd.org>
parents: 12594
diff changeset
553 if (c->num_valid_stat_ent != MAX_HISTORY_MONTHS) c->num_valid_stat_ent++;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
554
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
555 UpdateCompanyRatingAndValue(c, true);
12046
da0c682ff327 (svn r16453) -Fix: Move some code around so a company is no longer used after deleting it
yexo <yexo@openttd.org>
parents: 12010
diff changeset
556 if (c->block_preview != 0) c->block_preview--;
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
557 CompanyCheckBankrupt(c);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
558 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
559
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
560 SetWindowDirty(WC_INCOME_GRAPH, 0);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
561 SetWindowDirty(WC_OPERATING_PROFIT, 0);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
562 SetWindowDirty(WC_DELIVERED_CARGO, 0);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
563 SetWindowDirty(WC_PERFORMANCE_HISTORY, 0);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
564 SetWindowDirty(WC_COMPANY_VALUE, 0);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
565 SetWindowDirty(WC_COMPANY_LEAGUE, 0);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
566 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
567
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
568 /**
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
569 * Add monthly inflation
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
570 * @param check_year Shall the inflation get stopped after 170 years?
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
571 */
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
572 void AddInflation(bool check_year)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
573 {
7244
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
574 /* The cargo payment inflation differs from the normal inflation, so the
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
575 * relative amount of money you make with a transport decreases slowly over
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
576 * the 170 years. After a few hundred years we reach a level in which the
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
577 * games will become unplayable as the maximum income will be less than
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
578 * the minimum running cost.
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
579 *
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
580 * Furthermore there are a lot of inflation related overflows all over the
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
581 * place. Solving them is hardly possible because inflation will always
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
582 * reach the overflow threshold some day. So we'll just perform the
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
583 * inflation mechanism during the first 170 years (the amount of years that
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
584 * one had in the original TTD) and stop doing the inflation after that
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
585 * because it only causes problems that can't be solved nicely and the
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
586 * inflation doesn't add anything after that either; it even makes playing
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
587 * it impossible due to the diverging cost and income rates.
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
588 */
9717
57549f1e5133 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138 <peter1138@openttd.org>
parents: 9659
diff changeset
589 if (check_year && (_cur_year - _settings_game.game_creation.starting_year) >= (ORIGINAL_MAX_YEAR - ORIGINAL_BASE_YEAR)) return;
7244
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
590
6119
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
591 /* Approximation for (100 + infl_amount)% ** (1 / 12) - 100%
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
592 * scaled by 65536
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
593 * 12 -> months per year
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
594 * This is only a good approxiamtion for small values
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
595 */
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
596 _economy.inflation_prices += min((_economy.inflation_prices * _economy.infl_amount * 54) >> 16, MAX_INFLATION);
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
597 _economy.inflation_payment += min((_economy.inflation_payment * _economy.infl_amount_pr * 54) >> 16, MAX_INFLATION);
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
598 }
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
599
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
600 /**
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
601 * Computes all prices, payments and maximum loan.
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
602 */
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
603 void RecomputePrices()
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
604 {
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
605 /* Setup maximum loan */
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
606 _economy.max_loan = (_settings_game.difficulty.max_loan * _economy.inflation_prices >> 16) / 50000 * 50000;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
607
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
608 /* Setup price bases */
13489
afa5c1a70640 (svn r18008) -Codechange: Rename NUM_PRICES to PR_END, and use the Price enum some more.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
609 for (Price i = PR_BEGIN; i < PR_END; i++) {
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
610 Money price = _price_base_specs[i].start_price;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
611
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
612 /* Apply difficulty settings */
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
613 uint mod = 1;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
614 switch (_price_base_specs[i].category) {
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
615 case PCAT_RUNNING:
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
616 mod = _settings_game.difficulty.vehicle_costs;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
617 break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
618
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
619 case PCAT_CONSTRUCTION:
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
620 mod = _settings_game.difficulty.construction_cost;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
621 break;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
622
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
623 default: break;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
624 }
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
625 if (mod < 1) {
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
626 price = price * 3 >> 2;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
627 } else if (mod > 1) {
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
628 price = price * 9 >> 3;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
629 }
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
630
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
631 /* Apply inflation */
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
632 price = (int64)price * _economy.inflation_prices;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
633
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
634 /* Apply newgrf modifiers, and remove fractional part of inflation */
13497
a378bbea4ddb (svn r18016) -Codechange: Move the arbitrary basecost multiplier offset (8) to newgrf loading and make the internal state zero-based instead.
frosch <frosch@openttd.org>
parents: 13489
diff changeset
635 int shift = _price_base_multiplier[i] - 16;
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
636 if (shift >= 0) {
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
637 price <<= shift;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
638 } else {
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
639 price >>= -shift;
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
640 }
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
641
14695
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
642 /* Make sure the price does not get reduced to zero.
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
643 * Zero breaks quite a few commands that use a zero
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
644 * cost to see whether something got changed or not
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
645 * and based on that cause an error. When the price
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
646 * is zero that fails even when things are done. */
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
647 if (price == 0) {
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
648 price = Clamp(_price_base_specs[i].start_price, -1, 1);
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
649 /* No base price should be zero, but be sure. */
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
650 assert(price != 0);
4d9574aa8b7a (svn r19277) -Fix [FS#3646]: [NewGRF] Ensure prices can't be set to zero. Zero prices break a lot of the internal logic to determine whether something has been done.
rubidium <rubidium@openttd.org>
parents: 14620
diff changeset
651 }
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
652 /* Store value */
13489
afa5c1a70640 (svn r18008) -Codechange: Rename NUM_PRICES to PR_END, and use the Price enum some more.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
653 _price[i] = price;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
654 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
655
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
656 /* Setup cargo payment */
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
657 CargoSpec *cs;
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
658 FOR_ALL_CARGOSPECS(cs) {
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
659 cs->current_payment = ((int64)cs->initial_payment * _economy.inflation_payment) >> 16;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
660 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
661
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
662 SetWindowClassesDirty(WC_BUILD_VEHICLE);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
663 SetWindowClassesDirty(WC_REPLACE_VEHICLE);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
664 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
16923
047786b50538 (svn r21658) -Fix [FS#4351]: cargo payment window wasn't properly invalidated when payment changed
rubidium <rubidium@openttd.org>
parents: 16871
diff changeset
665 InvalidateWindowData(WC_PAYMENT_RATES, 0);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
666 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
667
15655
0be6e9a4ca0b (svn r20320) -Doc: Small Doxygen and normal comment fixes, and an missed addition.
alberth <alberth@openttd.org>
parents: 15654
diff changeset
668 /** Let all companies pay the monthly interest on their loan. */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
669 static void CompaniesPayInterest()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
670 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
671 const Company *c;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
672
15292
e2c62d70dfcf (svn r19931) -Fix (r19914): Convert assertion in Backup<> destructor into DEBUG() output. It was triggered on exceptions, especially when aborting world generation.
frosch <frosch@openttd.org>
parents: 15279
diff changeset
673 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
674 FOR_ALL_COMPANIES(c) {
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
675 cur_company.Change(c->index);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
676
10568
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
677 /* Over a year the paid interest should be "loan * interest percentage",
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
678 * but... as that number is likely not dividable by 12 (pay each month),
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
679 * one needs to account for that in the monthly fee calculations.
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
680 * To easily calculate what one should pay "this" month, you calculate
15604
ca959eb5d431 (svn r20266) -Doc: Mostly typo fixes, a few doxygen-related improvements.
alberth <alberth@openttd.org>
parents: 15601
diff changeset
681 * what (total) should have been paid up to this month and you subtract
10568
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
682 * whatever has been paid in the previous months. This will mean one month
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
683 * it'll be a bit more and the other it'll be a bit less than the average
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
684 * monthly fee, but on average it will be exact. */
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
685 Money yearly_fee = c->current_loan * _economy.interest_rate / 100;
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
686 Money up_to_previous_month = yearly_fee * _cur_month / 12;
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
687 Money up_to_this_month = yearly_fee * (_cur_month + 1) / 12;
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
688
35171d182079 (svn r14825) -Fix: pay the correct amount of interest instead of a few percent too little a year
rubidium <rubidium@openttd.org>
parents: 10550
diff changeset
689 SubtractMoneyFromCompany(CommandCost(EXPENSES_LOAN_INT, up_to_this_month - up_to_previous_month));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
690
13486
f2535c5098a0 (svn r18005) -Codechange: Convert the Prices struct into an array and an enum.
frosch <frosch@openttd.org>
parents: 13466
diff changeset
691 SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, _price[PR_STATION_VALUE] >> 2));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
692 }
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
693 cur_company.Restore();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
694 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
695
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6227
diff changeset
696 static void HandleEconomyFluctuations()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
697 {
12933
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
698 if (_settings_game.difficulty.economy != 0) {
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
699 /* When economy is Fluctuating, decrease counter */
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
700 _economy.fluct--;
16440
c776a99ce938 (svn r21156) -Codechange: Introduce EconomyIsInRecession().
alberth <alberth@openttd.org>
parents: 16429
diff changeset
701 } else if (EconomyIsInRecession()) {
12933
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
702 /* When it's Steady and we are in recession, end it now */
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
703 _economy.fluct = -12;
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
704 } else {
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
705 /* No need to do anything else in other cases */
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
706 return;
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
707 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
708
12933
23e2eb186b4d (svn r17426) -Fix (r1): economy recession would never end when economy is set to Steady while in recession
smatz <smatz@openttd.org>
parents: 12927
diff changeset
709 if (_economy.fluct == 0) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
710 _economy.fluct = -(int)GB(Random(), 0, 2);
12010
d0bf08a7c260 (svn r16416) -Fix [FS#2912]: Rework deleting of news when referenced vehicles/stations/industries are deleted.
frosch <frosch@openttd.org>
parents: 12001
diff changeset
711 AddNewsItem(STR_NEWS_BEGIN_OF_RECESSION, NS_ECONOMY);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
712 } else if (_economy.fluct == -12) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
713 _economy.fluct = GB(Random(), 0, 8) + 312;
12010
d0bf08a7c260 (svn r16416) -Fix [FS#2912]: Rework deleting of news when referenced vehicles/stations/industries are deleted.
frosch <frosch@openttd.org>
parents: 12001
diff changeset
714 AddNewsItem(STR_NEWS_END_OF_RECESSION, NS_ECONOMY);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
715 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
716 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
717
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
718
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
719 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
720 * Reset changes to the price base multipliers.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
721 */
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6227
diff changeset
722 void ResetPriceBaseMultipliers()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
723 {
13497
a378bbea4ddb (svn r18016) -Codechange: Move the arbitrary basecost multiplier offset (8) to newgrf loading and make the internal state zero-based instead.
frosch <frosch@openttd.org>
parents: 13489
diff changeset
724 memset(_price_base_multiplier, 0, sizeof(_price_base_multiplier));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
725 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
726
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
727 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
728 * Change a price base by the given factor.
13497
a378bbea4ddb (svn r18016) -Codechange: Move the arbitrary basecost multiplier offset (8) to newgrf loading and make the internal state zero-based instead.
frosch <frosch@openttd.org>
parents: 13489
diff changeset
729 * The price base is altered by factors of two.
a378bbea4ddb (svn r18016) -Codechange: Move the arbitrary basecost multiplier offset (8) to newgrf loading and make the internal state zero-based instead.
frosch <frosch@openttd.org>
parents: 13489
diff changeset
730 * NewBaseCost = OldBaseCost * 2^n
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
731 * @param price Index of price base to change.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
732 * @param factor Amount to change by.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
733 */
13497
a378bbea4ddb (svn r18016) -Codechange: Move the arbitrary basecost multiplier offset (8) to newgrf loading and make the internal state zero-based instead.
frosch <frosch@openttd.org>
parents: 13489
diff changeset
734 void SetPriceBaseMultiplier(Price price, int factor)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
735 {
13489
afa5c1a70640 (svn r18008) -Codechange: Rename NUM_PRICES to PR_END, and use the Price enum some more.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
736 assert(price < PR_END);
13497
a378bbea4ddb (svn r18016) -Codechange: Move the arbitrary basecost multiplier offset (8) to newgrf loading and make the internal state zero-based instead.
frosch <frosch@openttd.org>
parents: 13489
diff changeset
737 _price_base_multiplier[price] = Clamp(factor, MIN_PRICE_MODIFIER, MAX_PRICE_MODIFIER);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
738 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
739
10146
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
740 /**
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
741 * Initialize the variables that will maintain the daily industry change system.
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
742 * @param init_counter specifies if the counter is required to be initialized
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
743 */
10571
b6779abf2e96 (svn r14828) -Codechange: move most of save/load-specific code to separate files
smatz <smatz@openttd.org>
parents: 10568
diff changeset
744 void StartupIndustryDailyChanges(bool init_counter)
10146
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
745 {
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
746 uint map_size = MapLogX() + MapLogY();
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
747 /* After getting map size, it needs to be scaled appropriately and divided by 31,
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
748 * which stands for the days in a month.
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
749 * Using just 31 will make it so that a monthly reset (based on the real number of days of that month)
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
750 * would not be needed.
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
751 * Since it is based on "fractionnal parts", the leftover days will not make much of a difference
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
752 * on the overall total number of changes performed */
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
753 _economy.industry_daily_increment = (1 << map_size) / 31;
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
754
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
755 if (init_counter) {
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
756 /* A new game or a savegame from an older version will require the counter to be initialized */
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
757 _economy.industry_daily_change_counter = 0;
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
758 }
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
759 }
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
760
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6227
diff changeset
761 void StartupEconomy()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
762 {
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
763 _economy.interest_rate = _settings_game.difficulty.initial_interest;
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
764 _economy.infl_amount = _settings_game.difficulty.initial_interest;
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
765 _economy.infl_amount_pr = max(0, _settings_game.difficulty.initial_interest - 1);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
766 _economy.fluct = GB(Random(), 0, 8) + 168;
10146
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
767
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
768 /* Set up prices */
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
769 RecomputePrices();
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
770
10146
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
771 StartupIndustryDailyChanges(true); // As we are starting a new game, initialize the counter too
f5ef198f917d (svn r14332) -Fix[FS#1885]: Balance the monthly random industry changes, by introducing a daily random industry change.
belugas <belugas@openttd.org>
parents: 10080
diff changeset
772
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
773 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
774
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
775 /**
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
776 * Resets economy to initial values
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
777 */
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
778 void InitializeEconomy()
9717
57549f1e5133 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138 <peter1138@openttd.org>
parents: 9659
diff changeset
779 {
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
780 _economy.inflation_prices = _economy.inflation_payment = 1 << 16;
9717
57549f1e5133 (svn r13836) -Fix [FS#2074]: Automatically recalculate inflation if NewGRFs are changed and cargo types are added, so that cargo payment rates are correct.
peter1138 <peter1138@openttd.org>
parents: 9659
diff changeset
781 }
8626
e6ad3a9cdfcb (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138 <peter1138@openttd.org>
parents: 8624
diff changeset
782
13486
f2535c5098a0 (svn r18005) -Codechange: Convert the Prices struct into an array and an enum.
frosch <frosch@openttd.org>
parents: 13466
diff changeset
783 /**
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
784 * Determine a certain price
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
785 * @param index Price base
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
786 * @param cost_factor Price factor
13743
c84d609b6877 (svn r18268) -Feature: [NewGRF] Make price base multipliers related to vehicles only apply to the GRF locally, if it defines engines of that type itself.
frosch <frosch@openttd.org>
parents: 13741
diff changeset
787 * @param grf_file NewGRF to use local price multipliers from.
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
788 * @param shift Extra bit shifting after the computation
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
789 * @return Price
13486
f2535c5098a0 (svn r18005) -Codechange: Convert the Prices struct into an array and an enum.
frosch <frosch@openttd.org>
parents: 13466
diff changeset
790 */
13743
c84d609b6877 (svn r18268) -Feature: [NewGRF] Make price base multipliers related to vehicles only apply to the GRF locally, if it defines engines of that type itself.
frosch <frosch@openttd.org>
parents: 13741
diff changeset
791 Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
8626
e6ad3a9cdfcb (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138 <peter1138@openttd.org>
parents: 8624
diff changeset
792 {
13489
afa5c1a70640 (svn r18008) -Codechange: Rename NUM_PRICES to PR_END, and use the Price enum some more.
frosch <frosch@openttd.org>
parents: 13486
diff changeset
793 if (index >= PR_END) return 0;
8626
e6ad3a9cdfcb (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138 <peter1138@openttd.org>
parents: 8624
diff changeset
794
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
795 Money cost = _price[index] * cost_factor;
13743
c84d609b6877 (svn r18268) -Feature: [NewGRF] Make price base multipliers related to vehicles only apply to the GRF locally, if it defines engines of that type itself.
frosch <frosch@openttd.org>
parents: 13741
diff changeset
796 if (grf_file != NULL) shift += grf_file->price_base_multipliers[index];
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
797
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
798 if (shift >= 0) {
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
799 cost <<= shift;
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
800 } else {
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
801 cost >>= -shift;
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
802 }
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
803
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13497
diff changeset
804 return cost;
8626
e6ad3a9cdfcb (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138 <peter1138@openttd.org>
parents: 8624
diff changeset
805 }
e6ad3a9cdfcb (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138 <peter1138@openttd.org>
parents: 8624
diff changeset
806
6954
3aa54e38394f (svn r10209) -Fix/Codechange: forgot a few changes of int32 -> Money in the previous commit
rubidium <rubidium@openttd.org>
parents: 6953
diff changeset
807 Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
808 {
12415
6a77d1df56e2 (svn r16849) -Codechange: replace GetCargo() by CargoSpec::Get()
smatz <smatz@openttd.org>
parents: 12384
diff changeset
809 const CargoSpec *cs = CargoSpec::Get(cargo_type);
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
810 if (!cs->IsValid()) {
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
811 /* User changed newgrfs and some vehicle still carries some cargo which is no longer available. */
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
812 return 0;
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
813 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
814
6458
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
815 /* Use callback to calculate cargo profit, if available */
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7926
diff changeset
816 if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) {
6458
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
817 uint32 var18 = min(dist, 0xFFFF) | (min(num_pieces, 0xFF) << 16) | (transit_days << 24);
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
818 uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs);
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
819 if (callback != CALLBACK_FAILED) {
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
820 int result = GB(callback, 0, 14);
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
821
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
822 /* Simulate a 15 bit signed value */
13048
f9399c64435f (svn r17546) -Fix (r9617): sign extending of profit calculation didn't work
rubidium <rubidium@openttd.org>
parents: 13044
diff changeset
823 if (HasBit(callback, 14)) result -= 0x4000;
6458
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
824
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
825 /* "The result should be a signed multiplier that gets multiplied
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
826 * by the amount of cargo moved and the price factor, then gets
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
827 * divided by 8192." */
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
828 return result * num_pieces * cs->current_payment / 8192;
6458
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
829 }
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
830 }
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
831
7328
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
832 static const int MIN_TIME_FACTOR = 31;
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
833 static const int MAX_TIME_FACTOR = 255;
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
834
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
835 const int days1 = cs->transit_days[0];
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
836 const int days2 = cs->transit_days[1];
10417
844e13224bd1 (svn r14670) -Codechange: use better readable (I hope) and branchless (for some archs/compilers) code for cargo value computation
smatz <smatz@openttd.org>
parents: 10297
diff changeset
837 const int days_over_days1 = max( transit_days - days1, 0);
844e13224bd1 (svn r14670) -Codechange: use better readable (I hope) and branchless (for some archs/compilers) code for cargo value computation
smatz <smatz@openttd.org>
parents: 10297
diff changeset
838 const int days_over_days2 = max(days_over_days1 - days2, 0);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
839
7328
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
840 /*
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
841 * The time factor is calculated based on the time it took
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
842 * (transit_days) compared two cargo-depending values. The
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
843 * range is divided into three parts:
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
844 *
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
845 * - constant for fast transits
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
846 * - linear decreasing with time with a slope of -1 for medium transports
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
847 * - linear decreasing with time with a slope of -2 for slow transports
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
848 *
35cf803fe4b9 (svn r10691) -Codechange [FS#509]: simplify GetTransportedGoodsIncome to make it more obvious and less hidden what actually happens. Based on a patch by rfalke.
rubidium <rubidium@openttd.org>
parents: 7287
diff changeset
849 */
10417
844e13224bd1 (svn r14670) -Codechange: use better readable (I hope) and branchless (for some archs/compilers) code for cargo value computation
smatz <smatz@openttd.org>
parents: 10297
diff changeset
850 const int time_factor = max(MAX_TIME_FACTOR - days_over_days1 - days_over_days2, MIN_TIME_FACTOR);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
851
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
852 return BigMulS(dist * time_factor * num_pieces, cs->current_payment, 21);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
853 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
854
12267
fe4287ad4243 (svn r16682) -Codechange: make (static) industry list used to trigger the industries' production compile unit static instead of passing it around as pointer
rubidium <rubidium@openttd.org>
parents: 12266
diff changeset
855 /** The industries we've currently brought cargo to. */
fe4287ad4243 (svn r16682) -Codechange: make (static) industry list used to trigger the industries' production compile unit static instead of passing it around as pointer
rubidium <rubidium@openttd.org>
parents: 12266
diff changeset
856 static SmallIndustryList _cargo_delivery_destinations;
12239
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
857
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
858 /**
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
859 * Transfer goods from station to industry.
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
860 * All cargo is delivered to the nearest (Manhattan) industry to the station sign, which is inside the acceptance rectangle and actually accepts the cargo.
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
861 * @param st The station that accepted the cargo
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
862 * @param cargo_type Type of cargo delivered
13070
1c4c4ae8d716 (svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents: 13067
diff changeset
863 * @param num_pieces Amount of cargo delivered
1c4c4ae8d716 (svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents: 13067
diff changeset
864 * @param source The source of the cargo
12943
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
865 * @return actually accepted pieces of cargo
12239
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
866 */
12944
75ae91d09b03 (svn r17437) -Change: don't accept cargo produced in the same industry (generalise and improve the check used only for valuables)
smatz <smatz@openttd.org>
parents: 12943
diff changeset
867 static uint DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, uint num_pieces, IndustryID source)
12239
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
868 {
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
869 /* Find the nearest industrytile to the station sign inside the catchment area, whose industry accepts the cargo.
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
870 * This fails in three cases:
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
871 * 1) The station accepts the cargo because there are enough houses around it accepting the cargo.
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
872 * 2) The industries in the catchment area temporarily reject the cargo, and the daily station loop has not yet updated station acceptance.
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
873 * 3) The results of callbacks CBID_INDUSTRY_REFUSE_CARGO and CBID_INDTILE_CARGO_ACCEPTANCE are inconsistent. (documented behaviour)
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
874 */
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
875
12943
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
876 uint accepted = 0;
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
877
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
878 for (uint i = 0; i < st->industries_near.Length() && num_pieces != 0; i++) {
12239
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
879 Industry *ind = st->industries_near[i];
12944
75ae91d09b03 (svn r17437) -Change: don't accept cargo produced in the same industry (generalise and improve the check used only for valuables)
smatz <smatz@openttd.org>
parents: 12943
diff changeset
880 if (ind->index == source) continue;
75ae91d09b03 (svn r17437) -Change: don't accept cargo produced in the same industry (generalise and improve the check used only for valuables)
smatz <smatz@openttd.org>
parents: 12943
diff changeset
881
12239
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
882 uint cargo_index;
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
883 for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
884 if (cargo_type == ind->accepts_cargo[cargo_index]) break;
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
885 }
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
886 /* Check if matching cargo has been found */
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
887 if (cargo_index >= lengthof(ind->accepts_cargo)) continue;
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
888
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
889 /* Check if industry temporarily refuses acceptance */
15729
f6078460c5b1 (svn r20396) -Codechange: introduce a helper function to test whether an industry temporarily refues to accept some cargo
yexo <yexo@openttd.org>
parents: 15726
diff changeset
890 if (IndustryTemporarilyRefusesCargo(ind, cargo_type)) continue;
7165
c3b231638b80 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium <rubidium@openttd.org>
parents: 7124
diff changeset
891
12267
fe4287ad4243 (svn r16682) -Codechange: make (static) industry list used to trigger the industries' production compile unit static instead of passing it around as pointer
rubidium <rubidium@openttd.org>
parents: 12266
diff changeset
892 /* Insert the industry into _cargo_delivery_destinations, if not yet contained */
fe4287ad4243 (svn r16682) -Codechange: make (static) industry list used to trigger the industries' production compile unit static instead of passing it around as pointer
rubidium <rubidium@openttd.org>
parents: 12266
diff changeset
893 _cargo_delivery_destinations.Include(ind);
12239
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
894
12956
3a0bbffd1a58 (svn r17449) -Fix (r17436): MSVC compile warning about comparing signed and unsigned values
rubidium <rubidium@openttd.org>
parents: 12946
diff changeset
895 uint amount = min(num_pieces, 0xFFFFU - ind->incoming_cargo_waiting[cargo_index]);
12943
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
896 ind->incoming_cargo_waiting[cargo_index] += amount;
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
897 num_pieces -= amount;
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
898 accepted += amount;
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
899 }
12239
b1d30d9d4025 (svn r16654) -Codechange: cache industries in station's coverage area instead of searching them everytime payment is made
smatz <smatz@openttd.org>
parents: 12228
diff changeset
900
12943
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
901 return accepted;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
902 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
903
10450
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
904 /**
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
905 * Delivers goods to industries/towns and calculates the payment
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
906 * @param num_pieces amount of cargo delivered
13070
1c4c4ae8d716 (svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents: 13067
diff changeset
907 * @param cargo_type the type of cargo that is delivered
10450
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
908 * @param dest Station the cargo has been unloaded
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
909 * @param source_tile The origin of the cargo for distance calculation
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
910 * @param days_in_transit Travel time
12266
c9e7d3ec5c1a (svn r16681) -Codechange: only resolve the company once for vehicle payment instead of each time a cargo packet is delivered
rubidium <rubidium@openttd.org>
parents: 12265
diff changeset
911 * @param company The company delivering the cargo
12658
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
912 * @param src_type Type of source of cargo (industry, town, headquarters)
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
913 * @param src Index of source of cargo
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
914 * @return Revenue for delivering cargo
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
915 * @note The cargo is just added to the stockpile of the industry. It is due to the caller to trigger the industry's production machinery
10450
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
916 */
12658
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
917 static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID dest, TileIndex source_tile, byte days_in_transit, Company *company, SourceType src_type, SourceID src)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
918 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
919 assert(num_pieces > 0);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
920
12658
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
921 const Station *st = Station::Get(dest);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
922
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
923 /* Give the goods to the industry. */
12944
75ae91d09b03 (svn r17437) -Change: don't accept cargo produced in the same industry (generalise and improve the check used only for valuables)
smatz <smatz@openttd.org>
parents: 12943
diff changeset
924 uint accepted = DeliverGoodsToIndustry(st, cargo_type, num_pieces, src_type == ST_INDUSTRY ? src : INVALID_INDUSTRY);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
925
13092
d755cf7553ef (svn r17592) -Fix [FS#3212](r17436): force all cargo being accepted when industry tiles accept it but industry itself doesn't
smatz <smatz@openttd.org>
parents: 13089
diff changeset
926 /* If this cargo type is always accepted, accept all */
13089
559dafab3ecb (svn r17589) -Codechange: rename town_acc to always_accepted
smatz <smatz@openttd.org>
parents: 13079
diff changeset
927 if (HasBit(st->always_accepted, cargo_type)) accepted = num_pieces;
12946
560c26f15688 (svn r17439) -Fix (r17436): you weren't paid for cargo delivered to houses and headquarters anymore
smatz <smatz@openttd.org>
parents: 12944
diff changeset
928
13995
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
929 /* Update company statistics */
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
930 company->cur_economy.delivered_cargo += accepted;
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
931 if (accepted > 0) SetBit(company->cargo_types, cargo_type);
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
932
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
933 /* Increase town's counter for some special goods types */
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
934 const CargoSpec *cs = CargoSpec::Get(cargo_type);
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
935 if (cs->town_effect == TE_FOOD) st->town->new_act_food += accepted;
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
936 if (cs->town_effect == TE_WATER) st->town->new_act_water += accepted;
bc8b836e1fb9 (svn r18537) -Fix (r17436): Also do not account cargo in statistics, if it was not accepted.
frosch <frosch@openttd.org>
parents: 13804
diff changeset
937
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
938 /* Determine profit */
12943
049765007dc0 (svn r17436) -Change: pay only for cargo actually delivered, not for all cargo unloaded at station (can differ with 'stockpiling' industries)
smatz <smatz@openttd.org>
parents: 12942
diff changeset
939 Money profit = GetTransportedGoodsIncome(accepted, DistanceManhattan(source_tile, st->xy), days_in_transit, cargo_type);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
940
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
941 /* Modify profit if a subsidy is in effect */
12658
b75d8c6681bd (svn r17113) -Change [FS#265][FS#2094][FS#2589]: apply the subsidy when subsidy's destination is in station's catchment area and cargo packets originate from subsidy's source
smatz <smatz@openttd.org>
parents: 12652
diff changeset
942 if (CheckSubsidised(cargo_type, company->index, src_type, src, st)) {
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
943 switch (_settings_game.difficulty.subsidy_multiplier) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
944 case 0: profit += profit >> 1; break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
945 case 1: profit *= 2; break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
946 case 2: profit *= 3; break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
947 default: profit *= 4; break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
948 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
949 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
950
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
951 return profit;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
952 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
953
6559
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
954 /**
10450
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
955 * Inform the industry about just delivered cargo
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
956 * DeliverGoodsToIndustry() silently incremented incoming_cargo_waiting, now it is time to do something with the new cargo.
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
957 * @param i The industry to process
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
958 */
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
959 static void TriggerIndustryProduction(Industry *i)
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
960 {
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
961 const IndustrySpec *indspec = GetIndustrySpec(i->type);
13036
8e2a0e8fa275 (svn r17534) -Codechange: unify the naming of callback masks/flags
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
962 uint16 callback = indspec->callback_mask;
10450
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
963
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
964 i->was_cargo_delivered = true;
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
965 i->last_cargo_accepted_at = _date;
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
966
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
967 if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
968 if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) {
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
969 IndustryProductionCallback(i, 0);
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
970 } else {
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
971 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
10450
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
972 }
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
973 } else {
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
974 for (uint cargo_index = 0; cargo_index < lengthof(i->incoming_cargo_waiting); cargo_index++) {
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
975 uint cargo_waiting = i->incoming_cargo_waiting[cargo_index];
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
976 if (cargo_waiting == 0) continue;
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
977
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
978 i->produced_cargo_waiting[0] = min(i->produced_cargo_waiting[0] + (cargo_waiting * indspec->input_cargo_multiplier[cargo_index][0] / 256), 0xFFFF);
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
979 i->produced_cargo_waiting[1] = min(i->produced_cargo_waiting[1] + (cargo_waiting * indspec->input_cargo_multiplier[cargo_index][1] / 256), 0xFFFF);
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
980
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
981 i->incoming_cargo_waiting[cargo_index] = 0;
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
982 }
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
983 }
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
984
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
985 TriggerIndustry(i, INDUSTRY_TRIGGER_RECEIVED_CARGO);
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
986 StartStopIndustryTileAnimation(i, IAT_INDUSTRY_RECEIVED_CARGO);
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
987 }
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
988
e3e68b2c143c (svn r14705) -Fix [FS#2460]: First transfer the whole load of a vehicle chain to industries before triggering any processing.
frosch <frosch@openttd.org>
parents: 10419
diff changeset
989 /**
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
990 * Makes us a new cargo payment helper.
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
991 * @param front The front of the train
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
992 */
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
993 CargoPayment::CargoPayment(Vehicle *front) :
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
994 front(front),
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
995 current_station(front->last_station_visited)
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
996 {
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
997 }
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
998
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
999 CargoPayment::~CargoPayment()
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1000 {
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1001 if (this->CleaningPool()) return;
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1002
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1003 this->front->cargo_payment = NULL;
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1004
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1005 if (this->visual_profit == 0) return;
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1006
15292
e2c62d70dfcf (svn r19931) -Fix (r19914): Convert assertion in Backup<> destructor into DEBUG() output. It was triggered on exceptions, especially when aborting world generation.
frosch <frosch@openttd.org>
parents: 15279
diff changeset
1007 Backup<CompanyByte> cur_company(_current_company, this->front->owner, FILE_LINE);
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1008
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1009 SubtractMoneyFromCompany(CommandCost(this->front->GetExpenseType(true), -this->route_profit));
12278
492621950e1a (svn r16693) -Fix [FS#3004] (r16883): cargo income was not added to the vehicle's profit.
rubidium <rubidium@openttd.org>
parents: 12268
diff changeset
1010 this->front->profit_this_year += this->visual_profit << 8;
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1011
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1012 if (this->route_profit != 0) {
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1013 if (IsLocalCompany() && !PlayVehicleSound(this->front, VSE_LOAD_UNLOAD)) {
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1014 SndPlayVehicleFx(SND_14_CASHTILL, this->front);
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1015 }
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1016
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1017 ShowCostOrIncomeAnimation(this->front->x_pos, this->front->y_pos, this->front->z_pos, -this->visual_profit);
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1018 } else {
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1019 ShowFeederIncomeAnimation(this->front->x_pos, this->front->y_pos, this->front->z_pos, this->visual_profit);
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1020 }
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1021
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
1022 cur_company.Restore();
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1023 }
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1024
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1025 /**
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1026 * Handle payment for final delivery of the given cargo packet.
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1027 * @param cp The cargo packet to pay for.
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1028 * @param count The number of packets to pay for.
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1029 */
13213
cbc7f8fe50ee (svn r17720) -Codechange: guard the CargoPacket variables that are cached in CargoLists so they cannot be written from outside the CargoList class (based on patch by fonsinchen)
rubidium <rubidium@openttd.org>
parents: 13092
diff changeset
1030 void CargoPayment::PayFinalDelivery(const CargoPacket *cp, uint count)
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1031 {
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1032 if (this->owner == NULL) {
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1033 this->owner = Company::Get(this->front->owner);
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1034 }
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1035
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1036 /* Handle end of route payment */
13325
2cac520633fc (svn r17834) -Fix [FS#3274] (r17808): you got paid a bit too much... ofcourse the index of the source station generally doesn't equal the location of said station.
rubidium <rubidium@openttd.org>
parents: 13299
diff changeset
1037 Money profit = DeliverGoods(count, this->ct, this->current_station, cp->SourceStationXY(), cp->DaysInTransit(), this->owner, cp->SourceSubsidyType(), cp->SourceSubsidyID());
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1038 this->route_profit += profit;
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1039
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1040 /* The vehicle's profit is whatever route profit there is minus feeder shares. */
13213
cbc7f8fe50ee (svn r17720) -Codechange: guard the CargoPacket variables that are cached in CargoLists so they cannot be written from outside the CargoList class (based on patch by fonsinchen)
rubidium <rubidium@openttd.org>
parents: 13092
diff changeset
1041 this->visual_profit += profit - cp->FeederShare();
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1042 }
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1043
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1044 /**
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1045 * Handle payment for transfer of the given cargo packet.
13213
cbc7f8fe50ee (svn r17720) -Codechange: guard the CargoPacket variables that are cached in CargoLists so they cannot be written from outside the CargoList class (based on patch by fonsinchen)
rubidium <rubidium@openttd.org>
parents: 13092
diff changeset
1046 * @param cp The cargo packet to pay for; actual payment won't be made!.
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1047 * @param count The number of packets to pay for.
13213
cbc7f8fe50ee (svn r17720) -Codechange: guard the CargoPacket variables that are cached in CargoLists so they cannot be written from outside the CargoList class (based on patch by fonsinchen)
rubidium <rubidium@openttd.org>
parents: 13092
diff changeset
1048 * @return The amount of money paid for the transfer.
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1049 */
13213
cbc7f8fe50ee (svn r17720) -Codechange: guard the CargoPacket variables that are cached in CargoLists so they cannot be written from outside the CargoList class (based on patch by fonsinchen)
rubidium <rubidium@openttd.org>
parents: 13092
diff changeset
1050 Money CargoPayment::PayTransfer(const CargoPacket *cp, uint count)
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1051 {
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1052 Money profit = GetTransportedGoodsIncome(
15654
22d1a4dbf678 (svn r20319) -Codechange: Align comments, add indent to a function call.
alberth <alberth@openttd.org>
parents: 15653
diff changeset
1053 count,
22d1a4dbf678 (svn r20319) -Codechange: Align comments, add indent to a function call.
alberth <alberth@openttd.org>
parents: 15653
diff changeset
1054 /* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
22d1a4dbf678 (svn r20319) -Codechange: Align comments, add indent to a function call.
alberth <alberth@openttd.org>
parents: 15653
diff changeset
1055 DistanceManhattan(cp->LoadedAtXY(), Station::Get(this->current_station)->xy),
22d1a4dbf678 (svn r20319) -Codechange: Align comments, add indent to a function call.
alberth <alberth@openttd.org>
parents: 15653
diff changeset
1056 cp->DaysInTransit(),
22d1a4dbf678 (svn r20319) -Codechange: Align comments, add indent to a function call.
alberth <alberth@openttd.org>
parents: 15653
diff changeset
1057 this->ct);
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1058
14155
96be446af436 (svn r18703) -Feature-ish: allow virtually paying a percentage of the leg profit in feeder chains. This to give the user a better chance to get a feeder system without "losses".
rubidium <rubidium@openttd.org>
parents: 14136
diff changeset
1059 profit = profit * _settings_game.economy.feeder_payment_share / 100;
96be446af436 (svn r18703) -Feature-ish: allow virtually paying a percentage of the leg profit in feeder chains. This to give the user a better chance to get a feeder system without "losses".
rubidium <rubidium@openttd.org>
parents: 14136
diff changeset
1060
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1061 this->visual_profit += profit; // accumulate transfer profits for whole vehicle
13213
cbc7f8fe50ee (svn r17720) -Codechange: guard the CargoPacket variables that are cached in CargoLists so they cannot be written from outside the CargoList class (based on patch by fonsinchen)
rubidium <rubidium@openttd.org>
parents: 13092
diff changeset
1062 return profit; // account for the (virtual) profit already made for the cargo packet
12268
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1063 }
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1064
3a97d4e6f918 (svn r16683) -Codechange: move the cargo payment handling to a seperate class. Based on ideas of fonso/fonsinchen.
rubidium <rubidium@openttd.org>
parents: 12267
diff changeset
1065 /**
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1066 * Prepare the vehicle to be unloaded.
6559
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1067 * @param front_v the vehicle to be unloaded
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1068 */
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1069 void PrepareUnload(Vehicle *front_v)
6559
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1070 {
6565
547b0171c3a9 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium <rubidium@openttd.org>
parents: 6559
diff changeset
1071 /* At this moment loading cannot be finished */
7929
4d13dbed828c (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7928
diff changeset
1072 ClrBit(front_v->vehicle_flags, VF_LOADING_FINISHED);
6565
547b0171c3a9 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium <rubidium@openttd.org>
parents: 6559
diff changeset
1073
547b0171c3a9 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium <rubidium@openttd.org>
parents: 6559
diff changeset
1074 /* Start unloading in at the first possible moment */
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14169
diff changeset
1075 front_v->load_unload_ticks = 1;
6565
547b0171c3a9 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium <rubidium@openttd.org>
parents: 6559
diff changeset
1076
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1077 if ((front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1078 for (Vehicle *v = front_v; v != NULL; v = v->Next()) {
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1079 if (v->cargo_cap > 0 && !v->cargo.Empty()) {
7931
44ff7a6d801f (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7929
diff changeset
1080 SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
6559
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1081 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1082 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1083 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1084
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1085 assert(front_v->cargo_payment == NULL);
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1086 front_v->cargo_payment = new CargoPayment(front_v);
6559
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1087 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1088
6580
e1cdf0b1265b (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium <rubidium@openttd.org>
parents: 6565
diff changeset
1089 /**
e1cdf0b1265b (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium <rubidium@openttd.org>
parents: 6565
diff changeset
1090 * Loads/unload the vehicle if possible.
e1cdf0b1265b (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium <rubidium@openttd.org>
parents: 6565
diff changeset
1091 * @param v the vehicle to be (un)loaded
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1092 * @param cargo_left the amount of each cargo type that is
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1093 * virtually left on the platform to be
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1094 * picked up by another vehicle when all
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1095 * previous vehicles have loaded.
6580
e1cdf0b1265b (svn r9794) -Codechange: refactor the question whether a vehicle can be loaded into LoadUnloadVehicle instead of duplicating it for each vehicle.
rubidium <rubidium@openttd.org>
parents: 6565
diff changeset
1096 */
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1097 static void LoadUnloadVehicle(Vehicle *v, int *cargo_left)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1098 {
8837
bdaf8866039c (svn r12585) -Fix (r12584): assumption that assertions were enabled during compilation/testing was flawed.
rubidium <rubidium@openttd.org>
parents: 8814
diff changeset
1099 assert(v->current_order.IsType(OT_LOADING));
12756
7e6255798f4e (svn r17222) -Fix [FS#3129, FS#3130]: with time tables vehicles would stay in the 'loading' state after they have finished loading
rubidium <rubidium@openttd.org>
parents: 12669
diff changeset
1100
6616
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1101 /* We have not waited enough time till the next round of loading/unloading */
16729
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1102 if (v->load_unload_ticks != 0) {
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1103 if (_settings_game.order.improved_load && (v->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1104 /* 'Reserve' this cargo for this vehicle, because we were first. */
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7474
diff changeset
1105 for (; v != NULL; v = v->Next()) {
11194
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1106 int cap_left = v->cargo_cap - v->cargo.Count();
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1107 if (cap_left > 0) cargo_left[v->cargo_type] -= cap_left;
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1108 }
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1109 }
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1110 return;
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1111 }
6616
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1112
8231
7002fda5bb72 (svn r11794) -Fix [FS#1632]: reversing a train when loading at a station with an adjacent station in the same axis crashed.
glx <glx@openttd.org>
parents: 8230
diff changeset
1113 StationID last_visited = v->last_station_visited;
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11730
diff changeset
1114 Station *st = Station::Get(last_visited);
8231
7002fda5bb72 (svn r11794) -Fix [FS#1632]: reversing a train when loading at a station with an adjacent station in the same axis crashed.
glx <glx@openttd.org>
parents: 8230
diff changeset
1115
7002fda5bb72 (svn r11794) -Fix [FS#1632]: reversing a train when loading at a station with an adjacent station in the same axis crashed.
glx <glx@openttd.org>
parents: 8230
diff changeset
1116 if (v->type == VEH_TRAIN && (!IsTileType(v->tile, MP_STATION) || GetStationIndex(v->tile) != st->index)) {
6844
70d60b0104c1 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium <rubidium@openttd.org>
parents: 6823
diff changeset
1117 /* The train reversed in the station. Take the "easy" way
70d60b0104c1 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium <rubidium@openttd.org>
parents: 6823
diff changeset
1118 * out and let the train just leave as it always did. */
7931
44ff7a6d801f (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7929
diff changeset
1119 SetBit(v->vehicle_flags, VF_LOADING_FINISHED);
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14169
diff changeset
1120 v->load_unload_ticks = 1;
6844
70d60b0104c1 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium <rubidium@openttd.org>
parents: 6823
diff changeset
1121 return;
70d60b0104c1 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium <rubidium@openttd.org>
parents: 6823
diff changeset
1122 }
70d60b0104c1 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium <rubidium@openttd.org>
parents: 6823
diff changeset
1123
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1124 int unloading_time = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1125 Vehicle *u = v;
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1126 bool dirty_vehicle = false;
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1127 bool dirty_station = false;
6565
547b0171c3a9 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium <rubidium@openttd.org>
parents: 6559
diff changeset
1128
8241
4648f777da55 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch <frosch@openttd.org>
parents: 8238
diff changeset
1129 bool completely_emptied = true;
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1130 bool anything_unloaded = false;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1131 bool anything_loaded = false;
16147
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1132 uint32 full_load_amount = 0;
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1133 uint32 cargo_not_full = 0;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1134 uint32 cargo_full = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1135
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1136 v->cur_speed = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1137
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1138 CargoPayment *payment = v->cargo_payment;
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1139
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7474
diff changeset
1140 for (; v != NULL; v = v->Next()) {
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1141 if (v->cargo_cap == 0) continue;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1142
13218
03a409fa4c29 (svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents: 13213
diff changeset
1143 const Engine *e = Engine::Get(v->engine_type);
03a409fa4c29 (svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents: 13213
diff changeset
1144 byte load_amount = e->info.load_amount;
10419
e723ec01b3a6 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch <frosch@openttd.org>
parents: 10417
diff changeset
1145
e723ec01b3a6 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch <frosch@openttd.org>
parents: 10417
diff changeset
1146 /* The default loadamount for mail is 1/4 of the load amount for passengers */
15050
73f4e0e490b7 (svn r19670) -Codechange: Add CeilDiv() and RoundDiv() to simplify integer divisions with rounding.
frosch <frosch@openttd.org>
parents: 15038
diff changeset
1147 if (v->type == VEH_AIRCRAFT && !Aircraft::From(v)->IsNormalAircraft()) load_amount = CeilDiv(load_amount, 4);
10419
e723ec01b3a6 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch <frosch@openttd.org>
parents: 10417
diff changeset
1148
13218
03a409fa4c29 (svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents: 13213
diff changeset
1149 if (_settings_game.order.gradual_loading && HasBit(e->info.callback_mask, CBM_VEHICLE_LOAD_AMOUNT)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1150 uint16 cb_load_amount = GetVehicleCallback(CBID_VEHICLE_LOAD_AMOUNT, 0, 0, v->engine_type, v);
9020
94bb527c979f (svn r12819) -Codechange: handle more NewGRFs in the same way as TTDP does it, i.e. testing the low bits for 0xFF or 0 instead of all bits.
rubidium <rubidium@openttd.org>
parents: 9003
diff changeset
1151 if (cb_load_amount != CALLBACK_FAILED && GB(cb_load_amount, 0, 8) != 0) load_amount = GB(cb_load_amount, 0, 8);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1152 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1153
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1154 GoodsEntry *ge = &st->goods[v->cargo_type];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1155
8883
23eea6824c4b (svn r12650) -Feature: ability to force a vehicle to not load at a station.
rubidium <rubidium@openttd.org>
parents: 8873
diff changeset
1156 if (HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) && (u->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
7010
f0f87c5a431e (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents: 7002
diff changeset
1157 uint cargo_count = v->cargo.Count();
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1158 uint amount_unloaded = _settings_game.order.gradual_loading ? min(cargo_count, load_amount) : cargo_count;
11221
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1159 bool remaining = false; // Are there cargo entities in this vehicle that can still be unloaded here?
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1160 bool accepted = false; // Is the cargo accepted by the station?
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1161
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1162 payment->SetCargo(v->cargo_type);
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1163
8855
2acce887e7ec (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium <rubidium@openttd.org>
parents: 8843
diff changeset
1164 if (HasBit(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE) && !(u->current_order.GetUnloadType() & OUFB_TRANSFER)) {
15002
baf0e12ad2b4 (svn r19614) -Codechange: "it's" => "its" where appropriate
smatz <smatz@openttd.org>
parents: 14695
diff changeset
1165 /* The cargo has reached its final destination, the packets may now be destroyed */
13291
07fc7c78b30b (svn r17800) -Codechange: first steps into making CargoList a template
rubidium <rubidium@openttd.org>
parents: 13218
diff changeset
1166 remaining = v->cargo.MoveTo<StationCargoList>(NULL, amount_unloaded, VehicleCargoList::MTA_FINAL_DELIVERY, payment, last_visited);
7010
f0f87c5a431e (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents: 7002
diff changeset
1167
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1168 dirty_vehicle = true;
11221
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1169 accepted = true;
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1170 }
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1171
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1172 /* The !accepted || v->cargo.Count == cargo_count clause is there
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1173 * to make it possible to force unload vehicles at the station where
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1174 * they were loaded, but to not force unload the vehicle when the
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1175 * station is still accepting the cargo in the vehicle. It doesn't
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1176 * accept cargo that was loaded at the same station. */
12082
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 12046
diff changeset
1177 if ((u->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) && (!accepted || v->cargo.Count() == cargo_count)) {
13291
07fc7c78b30b (svn r17800) -Codechange: first steps into making CargoList a template
rubidium <rubidium@openttd.org>
parents: 13218
diff changeset
1178 remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded, u->current_order.GetUnloadType() & OUFB_TRANSFER ? VehicleCargoList::MTA_TRANSFER : VehicleCargoList::MTA_UNLOAD, payment);
16429
7d45364dd30d (svn r21145) -Fix: station list wasn't updated when a new cargo got a rating
rubidium <rubidium@openttd.org>
parents: 16383
diff changeset
1179 if (!HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP)) {
7d45364dd30d (svn r21145) -Fix: station list wasn't updated when a new cargo got a rating
rubidium <rubidium@openttd.org>
parents: 16383
diff changeset
1180 InvalidateWindowData(WC_STATION_LIST, last_visited);
7d45364dd30d (svn r21145) -Fix: station list wasn't updated when a new cargo got a rating
rubidium <rubidium@openttd.org>
parents: 16383
diff changeset
1181 SetBit(ge->acceptance_pickup, GoodsEntry::PICKUP);
7d45364dd30d (svn r21145) -Fix: station list wasn't updated when a new cargo got a rating
rubidium <rubidium@openttd.org>
parents: 16383
diff changeset
1182 }
6198
a2cdd211d091 (svn r8978) -Feature: Rewrite of transfer system.
richk <richk@openttd.org>
parents: 6190
diff changeset
1183
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1184 dirty_vehicle = dirty_station = true;
11221
6205ed0337b2 (svn r15574) -Fix [FS#2680]: force unload not working when trying to force unload at the station where you received the cargo
rubidium <rubidium@openttd.org>
parents: 11194
diff changeset
1185 } else if (!accepted) {
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1186 /* The order changed while unloading (unset unload/transfer) or the
12290
3ba19b02f30e (svn r16707) -Fix [FS#3007] (r16693): under some circumstances vehicles would skip loading as they thought nothing could be unloaded or loaded.
rubidium <rubidium@openttd.org>
parents: 12289
diff changeset
1187 * station does not accept our goods. */
7929
4d13dbed828c (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7928
diff changeset
1188 ClrBit(v->vehicle_flags, VF_CARGO_UNLOADING);
12290
3ba19b02f30e (svn r16707) -Fix [FS#3007] (r16693): under some circumstances vehicles would skip loading as they thought nothing could be unloaded or loaded.
rubidium <rubidium@openttd.org>
parents: 12289
diff changeset
1189
3ba19b02f30e (svn r16707) -Fix [FS#3007] (r16693): under some circumstances vehicles would skip loading as they thought nothing could be unloaded or loaded.
rubidium <rubidium@openttd.org>
parents: 12289
diff changeset
1190 /* Say we loaded something, otherwise we'll think we didn't unload
3ba19b02f30e (svn r16707) -Fix [FS#3007] (r16693): under some circumstances vehicles would skip loading as they thought nothing could be unloaded or loaded.
rubidium <rubidium@openttd.org>
parents: 12289
diff changeset
1191 * something and we didn't load something, so we must be finished
3ba19b02f30e (svn r16707) -Fix [FS#3007] (r16693): under some circumstances vehicles would skip loading as they thought nothing could be unloaded or loaded.
rubidium <rubidium@openttd.org>
parents: 12289
diff changeset
1192 * at this station. Setting the unloaded means that we will get a
3ba19b02f30e (svn r16707) -Fix [FS#3007] (r16693): under some circumstances vehicles would skip loading as they thought nothing could be unloaded or loaded.
rubidium <rubidium@openttd.org>
parents: 12289
diff changeset
1193 * retry for loading in the next cycle. */
3ba19b02f30e (svn r16707) -Fix [FS#3007] (r16693): under some circumstances vehicles would skip loading as they thought nothing could be unloaded or loaded.
rubidium <rubidium@openttd.org>
parents: 12289
diff changeset
1194 anything_unloaded = true;
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1195 continue;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1196 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1197
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1198 /* Deliver goods to the station */
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1199 st->time_since_unload = 0;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1200
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1201 unloading_time += amount_unloaded;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1202
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1203 anything_unloaded = true;
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1204 if (_settings_game.order.gradual_loading && remaining) {
8241
4648f777da55 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch <frosch@openttd.org>
parents: 8238
diff changeset
1205 completely_emptied = false;
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1206 } else {
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1207 /* We have finished unloading (cargo count == 0) */
7929
4d13dbed828c (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7928
diff changeset
1208 ClrBit(v->vehicle_flags, VF_CARGO_UNLOADING);
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1209 }
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1210
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1211 continue;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1212 }
5888
11683122ef51 (svn r8501) -Fix (r7377) [FS#539]: Keep track of how much cargo has been paid for, so that cargo cannot be paid for more than once.
maedhros <maedhros@openttd.org>
parents: 5854
diff changeset
1213
15827
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1214 /* Do not pick up goods when we have no-load set or loading is stopped. */
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1215 if (u->current_order.GetLoadType() & OLFB_NO_LOAD || HasBit(u->vehicle_flags, VF_STOP_LOADING)) continue;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1216
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1217 /* update stats */
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1218 int t;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1219 switch (u->type) {
16382
198b969abcda (svn r21098) -Codechange: Ships now store their max speed in the cache instead of recalculating it every time.
terkhen <terkhen@openttd.org>
parents: 16381
diff changeset
1220 case VEH_TRAIN: /* FALL THROUGH */
198b969abcda (svn r21098) -Codechange: Ships now store their max speed in the cache instead of recalculating it every time.
terkhen <terkhen@openttd.org>
parents: 16381
diff changeset
1221 case VEH_SHIP:
198b969abcda (svn r21098) -Codechange: Ships now store their max speed in the cache instead of recalculating it every time.
terkhen <terkhen@openttd.org>
parents: 16381
diff changeset
1222 t = u->vcache.cached_max_speed;
198b969abcda (svn r21098) -Codechange: Ships now store their max speed in the cache instead of recalculating it every time.
terkhen <terkhen@openttd.org>
parents: 16381
diff changeset
1223 break;
16383
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1224
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1225 case VEH_ROAD:
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1226 t = u->vcache.cached_max_speed / 2;
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1227 break;
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1228
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1229 case VEH_AIRCRAFT:
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1230 t = Aircraft::From(u)->GetSpeedOldUnits(); // Convert to old units.
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1231 break;
6dd97e032309 (svn r21099) -Codechange: Store road vehicle max speed in the vehicle cache.
terkhen <terkhen@openttd.org>
parents: 16382
diff changeset
1232
13052
6dba8bf1c036 (svn r17550) -Fix (r8973): Aircraft are good enough, don't make them even better.
frosch <frosch@openttd.org>
parents: 13048
diff changeset
1233 default: NOT_REACHED();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1234 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1235
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
1236 /* if last speed is 0, we treat that as if no vehicle has ever visited the station. */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1237 ge->last_speed = min(t, 255);
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6957
diff changeset
1238 ge->last_age = _cur_year - u->build_year;
7458
7e4d907043ef (svn r10953) -Fix [FS#1139, FS#1143]: stations would not get a rating when there were already two stations with a "more than default" rating as no cargo would be moved to the station to be picked up.
rubidium <rubidium@openttd.org>
parents: 7447
diff changeset
1239 ge->days_since_pickup = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1240
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
1241 /* If there's goods waiting at the station, and the vehicle
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
1242 * has capacity for it, load it on the vehicle. */
11194
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1243 int cap_left = v->cargo_cap - v->cargo.Count();
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1244 if (!ge->cargo.Empty() && cap_left > 0) {
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1245 uint cap = cap_left;
7010
f0f87c5a431e (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents: 7002
diff changeset
1246 uint count = ge->cargo.Count();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1247
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1248 /* Skip loading this vehicle if another train/vehicle is already handling
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1249 * the same cargo type at this station */
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1250 if (_settings_game.order.improved_load && cargo_left[v->cargo_type] <= 0) {
7931
44ff7a6d801f (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7929
diff changeset
1251 SetBit(cargo_not_full, v->cargo_type);
6565
547b0171c3a9 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium <rubidium@openttd.org>
parents: 6559
diff changeset
1252 continue;
547b0171c3a9 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium <rubidium@openttd.org>
parents: 6559
diff changeset
1253 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1254
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1255 if (cap > count) cap = count;
15827
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1256 if (_settings_game.order.gradual_loading) {
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1257 cap = min(cap, load_amount);
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1258 cap_left = min(cap_left, load_amount);
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1259 }
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1260 if (_settings_game.order.improved_load) {
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1261 /* Don't load stuff that is already 'reserved' for other vehicles */
7023
1997a5b53cec (svn r10287) -Fix (r10266): some surprises from that (almost always) complaining compiler...
rubidium <rubidium@openttd.org>
parents: 7014
diff changeset
1262 cap = min((uint)cargo_left[v->cargo_type], cap);
15827
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1263 count = cargo_left[v->cargo_type];
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1264 cargo_left[v->cargo_type] -= cap;
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1265 }
16147
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1266
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1267 /* Store whether the maximum possible load amount was loaded or not.*/
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1268 if (count >= (uint)cap_left) {
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1269 SetBit(full_load_amount, v->cargo_type);
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1270 } else {
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1271 ClrBit(full_load_amount, v->cargo_type);
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1272 }
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1273
7010
f0f87c5a431e (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents: 7002
diff changeset
1274 if (v->cargo.Empty()) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1275
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1276 /* TODO: Regarding this, when we do gradual loading, we
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1277 * should first unload all vehicles and then start
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1278 * loading them. Since this will cause
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1279 * VEHICLE_TRIGGER_EMPTY to be called at the time when
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1280 * the whole vehicle chain is really totally empty, the
8241
4648f777da55 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch <frosch@openttd.org>
parents: 8238
diff changeset
1281 * completely_emptied assignment can then be safely
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1282 * removed; that's how TTDPatch behaves too. --pasky */
8241
4648f777da55 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch <frosch@openttd.org>
parents: 8238
diff changeset
1283 completely_emptied = false;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1284 anything_loaded = true;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1285
13291
07fc7c78b30b (svn r17800) -Codechange: first steps into making CargoList a template
rubidium <rubidium@openttd.org>
parents: 13218
diff changeset
1286 ge->cargo.MoveTo(&v->cargo, cap, StationCargoList::MTA_CARGO_LOAD, NULL, st->xy);
6198
a2cdd211d091 (svn r8978) -Feature: Rewrite of transfer system.
richk <richk@openttd.org>
parents: 6190
diff changeset
1287
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1288 st->time_since_load = 0;
7010
f0f87c5a431e (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents: 7002
diff changeset
1289 st->last_vehicle_type = v->type;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1290
15935
a4fa352275f4 (svn r20623) -Codechange: unify the storing of animation related information
rubidium <rubidium@openttd.org>
parents: 15827
diff changeset
1291 TriggerStationAnimation(st, st->xy, SAT_CARGO_TAKEN, v->cargo_type);
14620
a2d95ade0e87 (svn r19197) -Codechange: introduce animation callbacks for airport tiles
yexo <yexo@openttd.org>
parents: 14353
diff changeset
1292 AirportAnimationTrigger(st, AAT_STATION_CARGO_TAKEN, v->cargo_type);
9003
ac23e012c9d7 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138 <peter1138@openttd.org>
parents: 8885
diff changeset
1293
7474
7b35fd1cc99d (svn r10981) -Fix [FS#1156] (r10970): stations gave ratings for accepted cargo too.
rubidium <rubidium@openttd.org>
parents: 7458
diff changeset
1294 unloading_time += cap;
7b35fd1cc99d (svn r10981) -Fix [FS#1156] (r10970): stations gave ratings for accepted cargo too.
rubidium <rubidium@openttd.org>
parents: 7458
diff changeset
1295
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1296 dirty_vehicle = dirty_station = true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1297 }
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1298
11194
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1299 if (v->cargo.Count() >= v->cargo_cap) {
7931
44ff7a6d801f (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7929
diff changeset
1300 SetBit(cargo_full, v->cargo_type);
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1301 } else {
7931
44ff7a6d801f (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7929
diff changeset
1302 SetBit(cargo_not_full, v->cargo_type);
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1303 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1304 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1305
15604
ca959eb5d431 (svn r20266) -Doc: Mostly typo fixes, a few doxygen-related improvements.
alberth <alberth@openttd.org>
parents: 15601
diff changeset
1306 /* Only set completely_emptied, if we just unloaded all remaining cargo */
8241
4648f777da55 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch <frosch@openttd.org>
parents: 8238
diff changeset
1307 completely_emptied &= anything_unloaded;
4648f777da55 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch <frosch@openttd.org>
parents: 8238
diff changeset
1308
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1309 /* We update these variables here, so gradual loading still fills
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1310 * all wagons at the same time instead of using the same 'improved'
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1311 * loading algorithm for the wagons (only fill wagon when there is
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1312 * enough to fill the previous wagons) */
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1313 if (_settings_game.order.improved_load && (u->current_order.GetLoadType() & OLFB_FULL_LOAD)) {
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1314 /* Update left cargo */
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7474
diff changeset
1315 for (v = u; v != NULL; v = v->Next()) {
11194
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1316 int cap_left = v->cargo_cap - v->cargo.Count();
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1317 if (cap_left > 0) cargo_left[v->cargo_type] -= cap_left;
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1318 }
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1319 }
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1320
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1321 v = u;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1322
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1323 if (!anything_unloaded) delete payment;
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1324
15827
56dcb448bdfb (svn r20506) -Change: Vehicles will now stop loading after a load cycle that loaded less than possible, unless it's a full load order. This should improve behaviour with gradual loading and cargo continuously trickling in.
michi_cc <michi_cc@openttd.org>
parents: 15744
diff changeset
1325 ClrBit(u->vehicle_flags, VF_STOP_LOADING);
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1326 if (anything_loaded || anything_unloaded) {
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1327 if (_settings_game.order.gradual_loading) {
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1328 /* The time it takes to load one 'slice' of cargo or passengers depends
11361
02313cc6114e (svn r15711) -Codechange: lots of whitespace cleanup/fixes
rubidium <rubidium@openttd.org>
parents: 11234
diff changeset
1329 * on the vehicle type - the values here are those found in TTDPatch */
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1330 const uint gradual_loading_wait_time[] = { 40, 20, 10, 20 };
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1331
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1332 unloading_time = gradual_loading_wait_time[v->type];
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1333 }
16147
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1334 /* We loaded less cargo than possible for all cargo types and it's not full
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1335 * load and we're not supposed to wait any longer: stop loading. */
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1336 if (!anything_unloaded && full_load_amount == 0 && !(v->current_order.GetLoadType() & OLFB_FULL_LOAD) &&
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1337 (!_settings_game.order.timetabling || v->current_order_time >= (uint)max(v->current_order.wait_time - v->lateness_counter, 0))) {
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1338 SetBit(v->vehicle_flags, VF_STOP_LOADING);
2c2d2a90fd54 (svn r20843) -Fix [FS#2534] (r20506): Make sure (gradual) loading is properly terminated for consists with multiple cargo types. Don't stop loading if the timetabled wait is not over yet. (Steve-N)
michi_cc <michi_cc@openttd.org>
parents: 15944
diff changeset
1339 }
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1340 } else {
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1341 bool finished_loading = true;
8855
2acce887e7ec (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium <rubidium@openttd.org>
parents: 8843
diff changeset
1342 if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) {
8873
9780f2a6cd20 (svn r12640) -Codechange: let GetLoadType make a difference between full load and full load any based on the patch setting instead of using the patch setting directly.
rubidium <rubidium@openttd.org>
parents: 8855
diff changeset
1343 if (v->current_order.GetLoadType() == OLF_FULL_LOAD_ANY) {
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1344 /* if the aircraft carries passengers and is NOT full, then
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1345 * continue loading, no matter how much mail is in */
11194
1c56a71f2bb1 (svn r15547) -Codechange: Eliminate all == and != comparisons between v->cargo_cap and v->cargo.Count() to improve behaviour wrt. broken/incompatible grfs.
frosch <frosch@openttd.org>
parents: 11090
diff changeset
1346 if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap > v->cargo.Count()) ||
15655
0be6e9a4ca0b (svn r20320) -Doc: Small Doxygen and normal comment fixes, and an missed addition.
alberth <alberth@openttd.org>
parents: 15654
diff changeset
1347 (cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are still non-full cargoes
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1348 finished_loading = false;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1349 }
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1350 } else if (cargo_not_full != 0) {
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1351 finished_loading = false;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1352 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1353 }
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1354 unloading_time = 20;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1355
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1356 SB(v->vehicle_flags, VF_LOADING_FINISHED, 1, finished_loading);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1357 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1358
6259
4a39d6291d58 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
1359 if (v->type == VEH_TRAIN) {
6125
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
1360 /* Each platform tile is worth 2 rail vehicles. */
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16729
diff changeset
1361 int overhang = v->GetGroundVehicleCache()->cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1362 if (overhang > 0) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1363 unloading_time <<= 1;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1364 unloading_time += (overhang * unloading_time) / 8;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1365 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1366 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1367
7543
2e26b732a29b (svn r11063) -Codechange: make it possible for people to view the loading indicators of everyone. Patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7520
diff changeset
1368 /* Calculate the loading indicator fill percent and display
2e26b732a29b (svn r11063) -Codechange: make it possible for people to view the loading indicators of everyone. Patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7520
diff changeset
1369 * In the Game Menu do not display indicators
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1370 * If _settings_client.gui.loading_indicators == 2, show indicators (bool can be promoted to int as 0 or 1 - results in 2 > 0,1 )
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1371 * if _settings_client.gui.loading_indicators == 1, _local_company must be the owner or must be a spectator to show ind., so 1 > 0
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9359
diff changeset
1372 * if _settings_client.gui.loading_indicators == 0, do not display indicators ... 0 is never greater than anything
7543
2e26b732a29b (svn r11063) -Codechange: make it possible for people to view the loading indicators of everyone. Patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7520
diff changeset
1373 */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1374 if (_game_mode != GM_MENU && (_settings_client.gui.loading_indicators > (uint)(v->owner != _local_company && _local_company != COMPANY_SPECTATOR))) {
7014
d03670858cef (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight <truelight@openttd.org>
parents: 7010
diff changeset
1375 StringID percent_up_down = STR_NULL;
d03670858cef (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight <truelight@openttd.org>
parents: 7010
diff changeset
1376 int percent = CalcPercentVehicleFilled(v, &percent_up_down);
6998
13ea0d5f8acf (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight <truelight@openttd.org>
parents: 6991
diff changeset
1377 if (v->fill_percent_te_id == INVALID_TE_ID) {
7014
d03670858cef (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight <truelight@openttd.org>
parents: 7010
diff changeset
1378 v->fill_percent_te_id = ShowFillingPercent(v->x_pos, v->y_pos, v->z_pos + 20, percent, percent_up_down);
6998
13ea0d5f8acf (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight <truelight@openttd.org>
parents: 6991
diff changeset
1379 } else {
7014
d03670858cef (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight <truelight@openttd.org>
parents: 7010
diff changeset
1380 UpdateFillingPercent(v->fill_percent_te_id, percent, percent_up_down);
6998
13ea0d5f8acf (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight <truelight@openttd.org>
parents: 6991
diff changeset
1381 }
13ea0d5f8acf (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight <truelight@openttd.org>
parents: 6991
diff changeset
1382 }
13ea0d5f8acf (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight <truelight@openttd.org>
parents: 6991
diff changeset
1383
12496
99f1ec9a1940 (svn r16933) -Fix [FS#3054] (r16694): vehicles would wait "very long" when they had nothing to unload and gradual loading was disabled.
rubidium <rubidium@openttd.org>
parents: 12475
diff changeset
1384 /* Always wait at least 1, otherwise we'll wait 'infinitively' long. */
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14169
diff changeset
1385 v->load_unload_ticks = max(1, unloading_time);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1386
8241
4648f777da55 (svn r11805) -Fix [FS#1620]: VEHICLE_TRIGGER_EMPTY was triggered continuously while train waiting in station.
frosch <frosch@openttd.org>
parents: 8238
diff changeset
1387 if (completely_emptied) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1388 TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1389 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1390
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1391 if (dirty_vehicle) {
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
1392 SetWindowDirty(GetWindowClassForVehicleType(v->type), v->owner);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
1393 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1394 v->MarkDirty();
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1395 }
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1396 if (dirty_station) {
6823
afa9c0a81fda (svn r10062) -Codechange: Don't redraw all station tiles when cargo is added or removed if the station has no custom graphics.
peter1138 <peter1138@openttd.org>
parents: 6819
diff changeset
1397 st->MarkTilesDirty(true);
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1398 SetWindowDirty(WC_STATION_VIEW, last_visited);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1399 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1400 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1401
6616
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1402 /**
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1403 * Load/unload the vehicles in this station according to the order
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1404 * they entered.
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1405 * @param st the station to do the loading/unloading for
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1406 */
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1407 void LoadUnloadStation(Station *st)
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1408 {
12265
10c7609e2d4b (svn r16680) -Codechange: don't go through all the station unloading stuff when there is no vehicle at the station
rubidium <rubidium@openttd.org>
parents: 12239
diff changeset
1409 /* No vehicle is here... */
10c7609e2d4b (svn r16680) -Codechange: don't go through all the station unloading stuff when there is no vehicle at the station
rubidium <rubidium@openttd.org>
parents: 12239
diff changeset
1410 if (st->loading_vehicles.empty()) return;
10c7609e2d4b (svn r16680) -Codechange: don't go through all the station unloading stuff when there is no vehicle at the station
rubidium <rubidium@openttd.org>
parents: 12239
diff changeset
1411
16729
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1412 Vehicle *last_loading = NULL;
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1413 std::list<Vehicle *>::iterator iter;
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1414
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1415 /* Check if anything will be loaded at all. Otherwise we don't need to reserve either. */
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1416 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1417 Vehicle *v = *iter;
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1418
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1419 if ((v->vehstatus & (VS_STOPPED | VS_CRASHED))) continue;
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1420
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1421 assert(v->load_unload_ticks != 0);
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1422 if (--v->load_unload_ticks == 0) last_loading = v;
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1423 }
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1424
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1425 /* We only need to reserve and load/unload up to the last loading vehicle.
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1426 * Anything else will be forgotten anyway after returning from this function.
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1427 *
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1428 * Especially this means we do _not_ need to reserve cargo for a single
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1429 * consist in a station which is not allowed to load yet because its
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1430 * load_unload_ticks is still not 0.
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1431 */
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1432 if (last_loading == NULL) return;
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1433
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1434 int cargo_left[NUM_CARGO];
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1435
7010
f0f87c5a431e (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents: 7002
diff changeset
1436 for (uint i = 0; i < NUM_CARGO; i++) cargo_left[i] = st->goods[i].cargo.Count();
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1437
6616
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1438 for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) {
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1439 Vehicle *v = *iter;
6618
a0ebb1506656 (svn r9838) -Fix: make "improved loading" a proper improved loading instead of loading one (semi-)random vehicle at a time:
rubidium <rubidium@openttd.org>
parents: 6616
diff changeset
1440 if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left);
16729
235fa32934bf (svn r21462) -Codechange [FS#3689]: don't do unnecessary cargo reservations in while loading/unloading vehicles. It is only needed when at least one vehicle is loading; all other times it is a waste of effort. Roughly halves the time it takes to perform loading/unloading of vehicles (fonsinchen)
rubidium <rubidium@openttd.org>
parents: 16440
diff changeset
1441 if (v == last_loading) break;
6616
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1442 }
12279
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1443
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1444 /* Call the production machinery of industries */
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1445 const Industry * const *isend = _cargo_delivery_destinations.End();
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1446 for (Industry **iid = _cargo_delivery_destinations.Begin(); iid != isend; iid++) {
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1447 TriggerIndustryProduction(*iid);
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1448 }
329b186db8cd (svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
rubidium <rubidium@openttd.org>
parents: 12278
diff changeset
1449 _cargo_delivery_destinations.Clear();
6616
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1450 }
ce5601a32593 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium <rubidium@openttd.org>
parents: 6611
diff changeset
1451
15616
d76890d16808 (svn r20279) -Doc: Doxygen additions/improvements.
alberth <alberth@openttd.org>
parents: 15604
diff changeset
1452 /**
d76890d16808 (svn r20279) -Doc: Doxygen additions/improvements.
alberth <alberth@openttd.org>
parents: 15604
diff changeset
1453 * Monthly update of the economic data (of the companies as well as economic fluctuations).
d76890d16808 (svn r20279) -Doc: Doxygen additions/improvements.
alberth <alberth@openttd.org>
parents: 15604
diff changeset
1454 */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1455 void CompaniesMonthlyLoop()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1456 {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1457 CompaniesGenStatistics();
12940
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
1458 if (_settings_game.economy.inflation) {
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
1459 AddInflation();
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
1460 RecomputePrices();
1b090e0c4f93 (svn r17433) -Codechange: Store cumulated inflation in savegame and compute all prices from that instead of storing all prices separately.
frosch <frosch@openttd.org>
parents: 12933
diff changeset
1461 }
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1462 CompaniesPayInterest();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1463 HandleEconomyFluctuations();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1464 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1465
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1466 static void DoAcquireCompany(Company *c)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1467 {
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents: 10647
diff changeset
1468 CompanyID ci = c->index;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1469
9658
27697e657d2e (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium <rubidium@openttd.org>
parents: 9652
diff changeset
1470 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11730
diff changeset
1471 cni->FillData(c, Company::Get(_current_company));
9658
27697e657d2e (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium <rubidium@openttd.org>
parents: 9652
diff changeset
1472
11730
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11376
diff changeset
1473 SetDParam(0, STR_NEWS_COMPANY_MERGER_TITLE);
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11376
diff changeset
1474 SetDParam(1, c->bankrupt_value == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION);
9658
27697e657d2e (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium <rubidium@openttd.org>
parents: 9652
diff changeset
1475 SetDParamStr(2, cni->company_name);
27697e657d2e (svn r13730) -Fix: make a copy of the names for news messages about the deletion of companies as the removal of a company could lead to wrong names in the news messages.
rubidium <rubidium@openttd.org>
parents: 9652
diff changeset
1476 SetDParamStr(3, cni->other_company_name);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1477 SetDParam(4, c->bankrupt_value);
12632
e41943276802 (svn r17075) -Codechange: rename ~750 strings to be more uniform with their relatives
rubidium <rubidium@openttd.org>
parents: 12615
diff changeset
1478 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_MERGER, cni);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents: 10647
diff changeset
1479 AI::BroadcastNewEvent(new AIEventCompanyMerger(ci, _current_company));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1480
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1481 ChangeOwnershipOfCompanyItems(ci, _current_company);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1482
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1483 if (c->bankrupt_value == 0) {
15652
354307d245e5 (svn r20317) -Codechange: Move variable declaration to their first use.
alberth <alberth@openttd.org>
parents: 15623
diff changeset
1484 Company *owner = Company::Get(_current_company);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1485 owner->current_loan += c->current_loan;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1486 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1487
15652
354307d245e5 (svn r20317) -Codechange: Move variable declaration to their first use.
alberth <alberth@openttd.org>
parents: 15623
diff changeset
1488 Money value = CalculateCompanyValue(c) >> 2;
15292
e2c62d70dfcf (svn r19931) -Fix (r19914): Convert assertion in Backup<> destructor into DEBUG() output. It was triggered on exceptions, especially when aborting world generation.
frosch <frosch@openttd.org>
parents: 15279
diff changeset
1489 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
15652
354307d245e5 (svn r20317) -Codechange: Move variable declaration to their first use.
alberth <alberth@openttd.org>
parents: 15623
diff changeset
1490 for (int i = 0; i != 4; i++) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1491 if (c->share_owners[i] != COMPANY_SPECTATOR) {
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
1492 cur_company.Change(c->share_owners[i]);
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1493 SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -value));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1494 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1495 }
15279
4e1862aebea2 (svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
frosch <frosch@openttd.org>
parents: 15050
diff changeset
1496 cur_company.Restore();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1497
12143
20abba1d6d18 (svn r16559) -Codechange: introduce Company::IsValidAiID() and Company::IsValidHumanID(), don't use IsHumanCompany() where possible
smatz <smatz@openttd.org>
parents: 12114
diff changeset
1498 if (c->is_ai) AI::Stop(c->index);
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents: 10647
diff changeset
1499
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1500 DeleteCompanyWindows(ci);
9297
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1501 InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1502 InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1503 InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1504 InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0);
9659
14e241fadc66 (svn r13731) -Codechange: make a pool of the array of players.
rubidium <rubidium@openttd.org>
parents: 9658
diff changeset
1505
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1506 delete c;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1507 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1508
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1509 extern int GetAmountOwnedBy(const Company *c, Owner owner);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1510
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
1511 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
1512 * Acquire shares in an opposing company.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1513 * @param tile unused
6432
fc703c6dc92c (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas <belugas@openttd.org>
parents: 6405
diff changeset
1514 * @param flags type of operation
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1515 * @param p1 company to buy the shares from
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1516 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1517 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1518 * @return the cost of this operation or an error
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1519 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1520 CommandCost CmdBuyShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1521 {
8230
5b61305fcdd4 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium <rubidium@openttd.org>
parents: 8224
diff changeset
1522 CommandCost cost(EXPENSES_OTHER);
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1523 CompanyID target_company = (CompanyID)p1;
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1524 Company *c = Company::GetIfValid(target_company);
11949
c89301974eb8 (svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents: 11924
diff changeset
1525
11368
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11361
diff changeset
1526 /* Check if buying shares is allowed (protection against modified clients)
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11361
diff changeset
1527 * Cannot buy own shares */
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1528 if (c == NULL || !_settings_game.economy.allow_shares || _current_company == target_company) return CMD_ERROR;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1529
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1530 /* Protect new companies from hostile takeovers */
12632
e41943276802 (svn r17075) -Codechange: rename ~750 strings to be more uniform with their relatives
rubidium <rubidium@openttd.org>
parents: 12615
diff changeset
1531 if (_cur_year - c->inaugurated_year < 6) return_cmd_error(STR_ERROR_PROTECTED);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1532
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1533 /* Those lines are here for network-protection (clients can be slow) */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1534 if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 0) return cost;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1535
15726
88dadfb384f8 (svn r20393) -Fix [FS#3993]: Prevent taking over competitors when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15725
diff changeset
1536 if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 1) {
88dadfb384f8 (svn r20393) -Fix [FS#3993]: Prevent taking over competitors when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15725
diff changeset
1537 if (!c->is_ai) return cost; // We can not buy out a real company (temporarily). TODO: well, enable it obviously.
88dadfb384f8 (svn r20393) -Fix [FS#3993]: Prevent taking over competitors when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15725
diff changeset
1538
88dadfb384f8 (svn r20393) -Fix [FS#3993]: Prevent taking over competitors when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15725
diff changeset
1539 if (GetAmountOwnedBy(c, _current_company) == 3 && !MayCompanyTakeOver(_current_company, target_company)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
88dadfb384f8 (svn r20393) -Fix [FS#3993]: Prevent taking over competitors when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15725
diff changeset
1540 }
88dadfb384f8 (svn r20393) -Fix [FS#3993]: Prevent taking over competitors when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15725
diff changeset
1541
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1542
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1543 cost.AddCost(CalculateCompanyValue(c) >> 2);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1544 if (flags & DC_EXEC) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1545 OwnerByte *b = c->share_owners;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1546
11368
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11361
diff changeset
1547 while (*b != COMPANY_SPECTATOR) b++; // share owners is guaranteed to contain at least one COMPANY_SPECTATOR
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1548 *b = _current_company;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1549
15652
354307d245e5 (svn r20317) -Codechange: Move variable declaration to their first use.
alberth <alberth@openttd.org>
parents: 15623
diff changeset
1550 for (int i = 0; c->share_owners[i] == _current_company;) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1551 if (++i == 4) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1552 c->bankrupt_value = 0;
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1553 DoAcquireCompany(c);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1554 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1555 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1556 }
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1557 SetWindowDirty(WC_COMPANY, target_company);
16269
f1578c16487c (svn r20970) -Add: company change notification to remote admins (dihedral)
rubidium <rubidium@openttd.org>
parents: 16164
diff changeset
1558 CompanyAdminUpdate(c);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1559 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1560 return cost;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1561 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1562
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
1563 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
1564 * Sell shares in an opposing company.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1565 * @param tile unused
6432
fc703c6dc92c (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas <belugas@openttd.org>
parents: 6405
diff changeset
1566 * @param flags type of operation
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1567 * @param p1 company to sell the shares from
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1568 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1569 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1570 * @return the cost of this operation or an error
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1571 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1572 CommandCost CmdSellShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1573 {
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1574 CompanyID target_company = (CompanyID)p1;
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1575 Company *c = Company::GetIfValid(target_company);
11949
c89301974eb8 (svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents: 11924
diff changeset
1576
11368
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11361
diff changeset
1577 /* Check if selling shares is allowed (protection against modified clients)
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11361
diff changeset
1578 * Cannot sell own shares */
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1579 if (c == NULL || !_settings_game.economy.allow_shares || _current_company == target_company) return CMD_ERROR;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1580
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1581 /* Those lines are here for network-protection (clients can be slow) */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1582 if (GetAmountOwnedBy(c, _current_company) == 0) return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1583
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1584 /* adjust it a little to make it less profitable to sell and buy */
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1585 Money cost = CalculateCompanyValue(c) >> 2;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1586 cost = -(cost - (cost >> 7));
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1587
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1588 if (flags & DC_EXEC) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1589 OwnerByte *b = c->share_owners;
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1590 while (*b != _current_company) b++; // share owners is guaranteed to contain company
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1591 *b = COMPANY_SPECTATOR;
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1592 SetWindowDirty(WC_COMPANY, target_company);
16269
f1578c16487c (svn r20970) -Add: company change notification to remote admins (dihedral)
rubidium <rubidium@openttd.org>
parents: 16164
diff changeset
1593 CompanyAdminUpdate(c);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1594 }
8230
5b61305fcdd4 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium <rubidium@openttd.org>
parents: 8224
diff changeset
1595 return CommandCost(EXPENSES_OTHER, cost);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1596 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1597
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
1598 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15616
diff changeset
1599 * Buy up another company.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1600 * When a competing company is gone bankrupt you get the chance to purchase
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1601 * that company.
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1602 * @todo currently this only works for AI companies
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1603 * @param tile unused
6432
fc703c6dc92c (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas <belugas@openttd.org>
parents: 6405
diff changeset
1604 * @param flags type of operation
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1605 * @param p1 company to buy up
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1606 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1607 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1608 * @return the cost of this operation or an error
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1609 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1610 CommandCost CmdBuyCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1611 {
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1612 CompanyID target_company = (CompanyID)p1;
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1613 Company *c = Company::GetIfValid(target_company);
12855
7c31785657c1 (svn r17345) -Fix [FS#2769]: one wasn't offered to take over bankrupt companies anymore; caused by the introduction NoAI, although NewAI had the same problem too.
rubidium <rubidium@openttd.org>
parents: 12853
diff changeset
1614 if (c == NULL) return CMD_ERROR;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1615
12855
7c31785657c1 (svn r17345) -Fix [FS#2769]: one wasn't offered to take over bankrupt companies anymore; caused by the introduction NoAI, although NewAI had the same problem too.
rubidium <rubidium@openttd.org>
parents: 12853
diff changeset
1616 /* Disable takeovers when not asked */
7c31785657c1 (svn r17345) -Fix [FS#2769]: one wasn't offered to take over bankrupt companies anymore; caused by the introduction NoAI, although NewAI had the same problem too.
rubidium <rubidium@openttd.org>
parents: 12853
diff changeset
1617 if (!HasBit(c->bankrupt_asked, _current_company)) return CMD_ERROR;
7c31785657c1 (svn r17345) -Fix [FS#2769]: one wasn't offered to take over bankrupt companies anymore; caused by the introduction NoAI, although NewAI had the same problem too.
rubidium <rubidium@openttd.org>
parents: 12853
diff changeset
1618
7c31785657c1 (svn r17345) -Fix [FS#2769]: one wasn't offered to take over bankrupt companies anymore; caused by the introduction NoAI, although NewAI had the same problem too.
rubidium <rubidium@openttd.org>
parents: 12853
diff changeset
1619 /* Disable taking over the local company in single player */
7c31785657c1 (svn r17345) -Fix [FS#2769]: one wasn't offered to take over bankrupt companies anymore; caused by the introduction NoAI, although NewAI had the same problem too.
rubidium <rubidium@openttd.org>
parents: 12853
diff changeset
1620 if (!_networking && _local_company == c->index) return CMD_ERROR;
6405
3f3b30a14c26 (svn r9541) -Codechange: Safeguard the company-buying routines so that the buying player and the bought player need to be two different entities
celestar <celestar@openttd.org>
parents: 6403
diff changeset
1621
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1622 /* Do not allow companies to take over themselves */
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1623 if (target_company == _current_company) return CMD_ERROR;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1624
15725
597cad644741 (svn r20392) -Fix [FS#3993]: Prevent buying bankrupt companies when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15676
diff changeset
1625 /* Disable taking over when not allowed. */
597cad644741 (svn r20392) -Fix [FS#3993]: Prevent buying bankrupt companies when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15676
diff changeset
1626 if (!MayCompanyTakeOver(_current_company, target_company)) return CMD_ERROR;
597cad644741 (svn r20392) -Fix [FS#3993]: Prevent buying bankrupt companies when you'd get too many vehicles.
alberth <alberth@openttd.org>
parents: 15676
diff changeset
1627
13044
1f4a5608aba5 (svn r17542) -Fix: don't access variables in the company struct after it has been deleted
yexo <yexo@openttd.org>
parents: 13036
diff changeset
1628 /* Get the cost here as the company is deleted in DoAcquireCompany. */
1f4a5608aba5 (svn r17542) -Fix: don't access variables in the company struct after it has been deleted
yexo <yexo@openttd.org>
parents: 13036
diff changeset
1629 CommandCost cost(EXPENSES_OTHER, c->bankrupt_value);
1f4a5608aba5 (svn r17542) -Fix: don't access variables in the company struct after it has been deleted
yexo <yexo@openttd.org>
parents: 13036
diff changeset
1630
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1631 if (flags & DC_EXEC) {
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10158
diff changeset
1632 DoAcquireCompany(c);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1633 }
13044
1f4a5608aba5 (svn r17542) -Fix: don't access variables in the company struct after it has been deleted
yexo <yexo@openttd.org>
parents: 13036
diff changeset
1634 return cost;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1635 }