annotate src/economy.cpp @ 15511:fbbbb2791756 draft

(svn r20164) -Fix [FS#3870]: inconsistencies w.r.t. to km/h vs km-ish/h as "base" unit for aircraft speeds
author rubidium <rubidium@openttd.org>
date Fri, 16 Jul 2010 17:45:34 +0000
parents e2c62d70dfcf
children 683f2dbe75bb
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"
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11956
diff changeset
23 #include "train.h"
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
24 #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
25 #include "newgrf_engine.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
26 #include "newgrf_sound.h"
7168
f0216e74951d (svn r10442) -Codechange: implement the industry production callback.
rubidium <rubidium@openttd.org>
parents: 7167
diff changeset
27 #include "newgrf_industries.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"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
31 #include "unmovable.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"
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
49 #include "engine_base.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
50 #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
51
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
52 #include "table/strings.h"
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 #include "table/sprites.h"
12593
73b580ddf33f (svn r17036) -Codechange: Split price bases from economy.cpp to table/pricebase.h.
frosch <frosch@openttd.org>
parents: 12496
diff changeset
54 #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
55
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
56
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 /* 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
58 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
59 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
60
7926
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 * 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
63 *
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
64 * 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
65 * 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
66 *
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
67 * @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
68 * @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
69 * @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
70 * @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
71 */
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
72 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
73 {
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
74 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
75 }
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
76
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
77 /**
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
78 * Multiply two unsigned integers 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
79 *
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
80 * This function multiplies two unsigned integers. 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
81 * 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
82 *
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
83 * @param a The first unsigned integer
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
84 * @param b The second unsigned integer
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
85 * @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
86 * @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
87 */
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
88 static inline uint32 BigMulSU(const uint32 a, const uint32 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
89 {
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
90 return (uint32)((uint64)a * (uint64)b >> shift);
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
91 }
e8ce9f6469d1 (svn r11479) -Codechange: Move the BIGMUL functions to the file of their usage
skidd13 <skidd13@openttd.org>
parents: 7922
diff changeset
92
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
93 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
94
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
95 /* Score info */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
96 const ScoreInfo _score_info[] = {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
97 { SCORE_VEHICLES, 120, 100 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
98 { SCORE_STATIONS, 80, 100 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
99 { SCORE_MIN_PROFIT, 10000, 100 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
100 { SCORE_MIN_INCOME, 50000, 50 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
101 { SCORE_MAX_INCOME, 100000, 100 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
102 { SCORE_DELIVERED, 40000, 400 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
103 { SCORE_CARGO, 8, 50 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
104 { SCORE_MONEY, 10000000, 50 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
105 { SCORE_LOAN, 250000, 50 },
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
106 { SCORE_TOTAL, 0, 0 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
107 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
108
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
109 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
110 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
111 Prices _price;
8119
b6ec923e9fa8 (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium <rubidium@openttd.org>
parents: 8116
diff changeset
112 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
113 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
114
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
115 /**
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
116 * 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
117 * 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
118 * 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
119 * 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
120 * @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
121 * @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
122 * @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
123 */
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
124 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
125 {
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
126 Owner owner = c->index;
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
127 Money value = 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
128
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
129 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
130 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
131
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
132 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
133 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
134 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
135
13486
f2535c5098a0 (svn r18005) -Codechange: Convert the Prices struct into an array and an enum.
frosch <frosch@openttd.org>
parents: 13466
diff changeset
136 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
137
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
138 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
139 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
140 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
141
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
142 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
143 v->type == VEH_ROAD ||
12384
d4b73a406cf3 (svn r16814) -Codechange: make IsNormalAircraft() member of Aircraft
smatz <smatz@openttd.org>
parents: 12383
diff changeset
144 (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
145 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
146 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
147 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
148 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
149
6953
41fd36025784 (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium <rubidium@openttd.org>
parents: 6952
diff changeset
150 /* 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
151 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
152 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
153
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
154 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
155 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
156
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
157 /** if update is set to true, the economy is updated with this score
46d001ebacf6 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6119
diff changeset
158 * (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
159 * @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
160 * @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
161 * @return actual score of this company
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
162 * */
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
163 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
164 {
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
165 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
166 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
167
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
168 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
169
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
170 /* 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
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 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
173 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
174 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
175 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
176
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
177 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
178 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
179 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
180 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
181 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
182 /* 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
183 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
184 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
185 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
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 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
188 }
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
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
191 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
192
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 _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
194 /* Don't allow negative min_profit to show */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
195 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
196 _score_part[owner][SCORE_MIN_PROFIT] = ClampToI32(min_profit);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
197 }
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 /* 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
200 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
201 uint num = 0;
10647
592ae9307430 (svn r14949) -Cleanup: pointer coding style
rubidium <rubidium@openttd.org>
parents: 10571
diff changeset
202 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
203
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
204 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
205 /* 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
206 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
207 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
208 _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
209 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
210
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
211 /* 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
212 {
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
213 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
214 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
215 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
216 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
217 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
218
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
219 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
220 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
221 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
222 } 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
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 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
225 _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
226 }
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
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
228 _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
229 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
230 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
231
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
232 /* 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
233 {
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
234 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
235 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
236 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
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 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
239 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
240 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
241 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
242 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
243 total_delivered += cee->delivered_cargo;
11361
02313cc6114e (svn r15711) -Codechange: lots of whitespace cleanup/fixes
rubidium <rubidium@openttd.org>
parents: 11234
diff changeset
244 } 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
245
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
246 _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
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 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
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 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
253 _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
254 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
255 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
256
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
257 /* 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
258 {
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
259 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
260 _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
261 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
262 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
263
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
264 /* 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
265 {
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
266 _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
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
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
269 /* 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
270 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
271 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
272 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
273 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
274 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
275 /* 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
276 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
277 /* 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
278 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
279 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
280 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
281 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
282
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
283 _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
284
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
285 /* 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
286 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
287 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
288
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
289 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
290 c->old_economy[0].performance_history = score;
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
291 UpdateCompanyHQ(c, score);
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
292 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
293 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
294
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
295 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
296 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
297 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
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 /* 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
300 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
301 {
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
302 #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
303 /* 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
304 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
305 #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
306
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
307 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
308 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
309
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 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
311
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
312 {
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
313 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
314 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
315
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
316 /* 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
317 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
318 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
319 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
320 /* 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
321 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
322 /* 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
323 * 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
324 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
325 }
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 }
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
328
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 /* 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
330 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
331 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
332 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
333 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
334 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
335 /* 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
336 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
337 /* 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
338 * 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
339 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
340 }
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
341 }
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
342 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
343 }
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
344
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
345 /* 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
346 * 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
347 * 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
348 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
349 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
350 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
351
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
352 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
353 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
354 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
355 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
356 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
357 } 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
358 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
359 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
360 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
361 }
12669
406f4ae1e2ca (svn r17124) -Codechange: store subsidies in a pool (instead of an array)
smatz <smatz@openttd.org>
parents: 12658
diff changeset
362 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
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 /* 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
365 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
366 /* 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
367 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
368 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
369 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
370 /* 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
371 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
372 } 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
373 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
374 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
375 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
376 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
377 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
378
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 /* 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
380 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
381 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
382 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
383
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
384 {
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 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
386 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
387 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
388 };
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
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
390 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
391 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
392 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
393 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
394 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
395 } 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
396 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
397 v->colourmap = PAL_NONE;
12383
3c7e93b91c73 (svn r16813) -Codechange: make IsEngineCountable() member of Vehicle
smatz <smatz@openttd.org>
parents: 12319
diff changeset
398 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
399 if (v->IsPrimaryVehicle()) v->unitnumber = unitidgen[v->type].NextID();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
400 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
401 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
402 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
403 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
404
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
405 /* 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
406 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
407 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
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 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
410 } while (++tile != MapSize());
8200
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
411
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
412 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
413 /* 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
414 * 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
415 * 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
416 * 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
417 tile = 0;
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
418
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
419 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
420 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
421 TrackBits tracks = GetTrackBits(tile);
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
422 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
423 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
424 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
425 } 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
426 } 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
427 UpdateLevelCrossing(tile);
8200
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
428 }
f7f49622b3e0 (svn r11763) -Fix: update signal states after merging a company
smatz <smatz@openttd.org>
parents: 8186
diff changeset
429 } 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
430 }
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
431
8347
a83dd50f52f9 (svn r11913) -Fix (r11871): update signals after company bankrupt and disaster too
smatz <smatz@openttd.org>
parents: 8342
diff changeset
432 /* 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
433 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
434 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
435
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
436 /* 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
437 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
438 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
439 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
440 /* 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
441 * 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
442 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
443 }
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
444 }
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
445
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
446 /* 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
447 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
448 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
449 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
450 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
451 }
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
452 }
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
453
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
454 /* 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
455 * 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
456 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
457
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 (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
459 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
460 } 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
461 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
462 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
463 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
464 }
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
465 }
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
466
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
467 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
468 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
469 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
470 }
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
471
11085
8da1855e9f14 (svn r15428) -Codechange: consistently use colour instead of having both color and colour.
rubidium <rubidium@openttd.org>
parents: 11056
diff changeset
472 /* 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
473 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
474
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
475 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
476
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
477 MarkWholeScreenDirty();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
478 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
479
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
480 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
481 {
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
482 /* 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
483 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
484 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
485 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
486 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
487 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
488
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
489 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
490
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
491 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(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
492 cni->FillData(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
493
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
494 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
495 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
496 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
497 free(cni);
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
498 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
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 2:
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
501 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
502 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
503 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
504 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
505 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
506 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
507 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
508 /* 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
509 * 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
510 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
511 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
512 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
513 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
514 c->bankrupt_timeout = 0;
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
515 free(cni);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
516 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
517 }
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
518 /* Else, falltrue 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
519 }
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
520 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
521 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
522 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
523 /* 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
524 * 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
525 * 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
526 * 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
527 c->bankrupt_asked = MAX_UVALUE(CompanyMask);
12851
a398faeb024e (svn r17341) -Fix: memory leak when trying to bankrupt the local company
rubidium <rubidium@openttd.org>
parents: 12826
diff changeset
528 free(cni);
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
529 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
530 }
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
531
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
532 /* Close everything the owner has open */
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 DeleteCompanyWindows(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
534
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
535 /* Show bankrupt news */
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
536 SetDParam(0, STR_NEWS_COMPANY_BANKRUPT_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
537 SetDParam(1, STR_NEWS_COMPANY_BANKRUPT_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
538 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
539 AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_BANKRUPT, cni);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
540
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
541 ChangeOwnershipOfCompanyItems(c->index, INVALID_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
542
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
543 if (c->is_ai) AI::Stop(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
544
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
545 CompanyID c_index = c->index;
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
546 delete c;
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
547 AI::BroadcastNewEvent(new AIEventCompanyBankrupt(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
548 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
549 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
550
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
551 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
552 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
553 Station *st;
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
554 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
555
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
556 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
557 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
558 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
559 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
560 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
561 }
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
562 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
563
10080
1ea2b6087381 (svn r14254) -Codechange: Code style and breathing room
belugas <belugas@openttd.org>
parents: 9950
diff changeset
564 if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_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
565 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
566
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
567 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
568 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
569 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
570 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
571
12615
5f44b757d251 (svn r17058) -Codechange: magic numbers removal.
belugas <belugas@openttd.org>
parents: 12594
diff changeset
572 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
573
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
574 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
575 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
576 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
577 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
578
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
579 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
580 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
581 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
582 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
583 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
584 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
585 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
586
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
587 /**
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
588 * 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
589 * @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
590 */
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
591 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
592 {
7244
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
593 /* 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
594 * 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
595 * 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
596 * 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
597 * the minimum running cost.
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
598 *
00f3d8da22e4 (svn r10541) -Fix [FS#1028]: cargo payment rates overflow.
rubidium <rubidium@openttd.org>
parents: 7229
diff changeset
599 * 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
600 * 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
601 * 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
602 * 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
603 * 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
604 * 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
605 * 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
606 * 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
607 */
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
608 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
609
6119
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
610 /* Approximation for (100 + infl_amount)% ** (1 / 12) - 100%
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
611 * scaled by 65536
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
612 * 12 -> months per year
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
613 * This is only a good approxiamtion for small values
f319793f0549 (svn r8855) -Fix
tron <tron@openttd.org>
parents: 6111
diff changeset
614 */
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
615 _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
616 _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
617 }
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
618
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 /**
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 * 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
621 */
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 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
623 {
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 /* 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
625 _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
626
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 /* 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
628 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
629 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
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 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
632 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
633 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
634 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
635 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
636 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
637
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
638 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
639 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
640 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
641
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
642 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
643 }
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
644 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
645 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
646 } 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
647 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
648 }
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
649
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
650 /* 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
651 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
652
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
653 /* 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
654 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
655 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
656 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
657 } 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
658 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
659 }
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
660
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
661 /* 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
662 * 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
663 * 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
664 * 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
665 * 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
666 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
667 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
668 /* 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
669 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
670 }
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
671 /* 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
672 _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
673 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
674
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
675 /* Setup cargo payment */
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
676 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
677 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
678 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
679 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
680
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
681 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
682 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
683 SetWindowClassesDirty(WC_VEHICLE_DETAILS);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 12991
diff changeset
684 SetWindowDirty(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
685 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
686
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
687 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
688 {
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
689 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
690
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
691 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
692 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
693 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
694
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
695 /* 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
696 * 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
697 * 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
698 * To easily calculate what one should pay "this" month, you calculate
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
699 * what (total) should have been paid up to this month and you substract
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
700 * 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
701 * 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
702 * 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
703 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
704 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
705 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
706
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
707 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
708
13486
f2535c5098a0 (svn r18005) -Codechange: Convert the Prices struct into an array and an enum.
frosch <frosch@openttd.org>
parents: 13466
diff changeset
709 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
710 }
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
711 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
712 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
713
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6227
diff changeset
714 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
715 {
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
716 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
717 /* 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
718 _economy.fluct--;
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
719 } else if (_economy.fluct <= 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
720 /* 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
721 _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
722 } 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
723 /* 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
724 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
725 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
726
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
727 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
728 _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
729 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
730 } 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
731 _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
732 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
733 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
734 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
735
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
736
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
737 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
738 * 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
739 */
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6227
diff changeset
740 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
741 {
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
742 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
743 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
744
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
745 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
746 * 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
747 * 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
748 * 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
749 * @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
750 * @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
751 */
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
752 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
753 {
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
754 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
755 _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
756 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
757
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
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 * 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
760 * @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
761 */
10571
b6779abf2e96 (svn r14828) -Codechange: move most of save/load-specific code to separate files
smatz <smatz@openttd.org>
parents: 10568
diff changeset
762 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
763 {
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
764 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
765 /* 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
766 * 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
767 * 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
768 * 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
769 * 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
770 * 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
771 _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
772
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
773 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
774 /* 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
775 _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
776 }
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
777 }
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
778
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6227
diff changeset
779 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
780 {
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
781 _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
782 _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
783 _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
784 _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
785
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
786 /* 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
787 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
788
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
789 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
790
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
791 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
792
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
793 /**
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
794 * 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
795 */
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
796 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
797 {
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
798 _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
799 }
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
800
13486
f2535c5098a0 (svn r18005) -Codechange: Convert the Prices struct into an array and an enum.
frosch <frosch@openttd.org>
parents: 13466
diff changeset
801 /**
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
802 * 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
803 * @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
804 * @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
805 * @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
806 * @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
807 * @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
808 */
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
809 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
810 {
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
811 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
812
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
813 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
814 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
815
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
816 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
817 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
818 } 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
819 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
820 }
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
821
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
822 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
823 }
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
824
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
825 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
826 {
12415
6a77d1df56e2 (svn r16849) -Codechange: replace GetCargo() by CargoSpec::Get()
smatz <smatz@openttd.org>
parents: 12384
diff changeset
827 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
828 if (!cs->IsValid()) {
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
829 /* 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
830 return 0;
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
831 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
832
6458
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
833 /* 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
834 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
835 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
836 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
837 if (callback != CALLBACK_FAILED) {
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
838 int result = GB(callback, 0, 14);
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
839
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
840 /* 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
841 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
842
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
843 /* "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
844 * 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
845 * divided by 8192." */
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
846 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
847 }
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
848 }
57cfff4ac608 (svn r9617) -Codechange: Implement callback for cargo profit calculation
peter1138 <peter1138@openttd.org>
parents: 6453
diff changeset
849
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
850 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
851 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
852
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
853 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
854 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
855 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
856 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
857
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
858 /*
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
859 * 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
860 * (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
861 * 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
862 *
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
863 * - 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
864 * - 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
865 * - 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
866 *
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
867 */
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
868 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
869
12942
e762f0b0f0b1 (svn r17435) -Codechange: Move _cargo_payment_rates[] to CargoSpec::current_payment.
frosch <frosch@openttd.org>
parents: 12940
diff changeset
870 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
871 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
872
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
873 /** 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
874 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
875
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
876 /**
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
877 * 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
878 * 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
879 * @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
880 * @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
881 * @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
882 * @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
883 * @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
884 */
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
885 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
886 {
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 /* 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
888 * 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
889 * 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
890 * 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
891 * 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
892 */
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
893
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
894 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
895
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 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
897 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
898 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
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 const IndustrySpec *indspec = GetIndustrySpec(ind->type);
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
901
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
902 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
903 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
904 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
905 }
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
906 /* 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
907 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
908
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
909 /* Check if industry temporarily refuses acceptance */
13036
8e2a0e8fa275 (svn r17534) -Codechange: unify the naming of callback masks/flags
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
910 if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
14169
8f89e4b14a05 (svn r18717) -Codechange: use TileArea in industry instead of three separate variables. Also make use of TileArea functions for determining the 'width' and 'height' of an industry.
rubidium <rubidium@openttd.org>
parents: 14155
diff changeset
911 uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->location.tile);
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
912 if (res == 0) 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
913 }
7165
c3b231638b80 (svn r10439) -Codechange: initial steps for customized industry productions.
rubidium <rubidium@openttd.org>
parents: 7124
diff changeset
914
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
915 /* 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
916 _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
917
12956
3a0bbffd1a58 (svn r17449) -Fix (r17436): MSVC compile warning about comparing signed and unsigned values
rubidium <rubidium@openttd.org>
parents: 12946
diff changeset
918 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
919 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
920 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
921 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
922 }
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
923
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
924 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
925 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
926
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
927 /**
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
928 * 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
929 * @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
930 * @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
931 * @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
932 * @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
933 * @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
934 * @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
935 * @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
936 * @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
937 * @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
938 * @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
939 */
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
940 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
941 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
942 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
943
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
944 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
945
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
946 /* 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
947 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
948
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
949 /* 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
950 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
951
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
952 /* 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
953 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
954 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
955
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
956 /* 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
957 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
958 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
959 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
960
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
961 /* 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
962 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
963
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
964 /* 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
965 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
966 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
967 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
968 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
969 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
970 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
971 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
972 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
973
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
974 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
975 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
976
6559
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
977 /**
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
978 * 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
979 * 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
980 * @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
981 */
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 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
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 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
985 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
986
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 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
988 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
989
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
990 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
991 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
992 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
993 } 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
994 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
995 }
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
996 } 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
997 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
998 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
999 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
1000
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
1001 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
1002 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
1003
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
1004 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
1005 }
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
1006 }
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
1007
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
1008 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
1009 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
1010 }
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
1011
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
1012 /**
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
1013 * 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
1014 * @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
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 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
1017 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
1018 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
1019 {
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
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
1022 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
1023 {
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
1024 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
1025
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
1026 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
1027
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
1028 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
1029
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
1030 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
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 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
1033 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
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 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
1036 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
1037 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
1038 }
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 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
1041 } 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
1042 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
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
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
1045 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
1046 }
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
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
1048 /**
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 * 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
1050 * @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
1051 * @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
1052 */
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
1053 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
1054 {
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
1055 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
1056 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
1057 }
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
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
1059 /* 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
1060 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
1061 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
1062
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 /* 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
1064 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
1065 }
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
1066
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
1067 /**
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
1068 * 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
1069 * @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
1070 * @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
1071 * @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
1072 */
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
1073 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
1074 {
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
1075 Money profit = GetTransportedGoodsIncome(
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
1076 count,
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
1077 /* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
13299
366acecbae41 (svn r17808) -Codechange: remove write 'access' from all of CargoPacket's variables
rubidium <rubidium@openttd.org>
parents: 13291
diff changeset
1078 DistanceManhattan(cp->LoadedAtXY(), Station::Get(this->current_station)->xy),
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
1079 cp->DaysInTransit(),
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
1080 this->ct);
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
1081
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
1082 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
1083
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
1084 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
1085 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
1086 }
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
1087
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
1088 /**
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
1089 * 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
1090 * @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
1091 */
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
1092 void PrepareUnload(Vehicle *front_v)
6559
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1093 {
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
1094 /* 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
1095 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
1096
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
1097 /* 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
1098 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
1099
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
1100 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
1101 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
1102 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
1103 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
1104 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1105 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1106 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1107
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
1108 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
1109 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
1110 }
b680e3433ca2 (svn r9761) -Codechange: refactor cargo payment out of LoadUnloadVehicle.
rubidium <rubidium@openttd.org>
parents: 6500
diff changeset
1111
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
1112 /**
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
1113 * 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
1114 * @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
1115 * @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
1116 * 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
1117 * 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
1118 * 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
1119 */
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
1120 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
1121 {
8837
bdaf8866039c (svn r12585) -Fix (r12584): assumption that assertions were enabled during compilation/testing was flawed.
rubidium <rubidium@openttd.org>
parents: 8814
diff changeset
1122 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
1123
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
1124 assert(v->load_unload_ticks != 0);
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
1125
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
1126 /* We have not waited enough time till the next round of loading/unloading */
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
1127 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
1128 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
1129 /* '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
1130 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
1131 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
1132 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
1133 }
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
1134 }
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
1135 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
1136 }
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
1137
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
1138 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
1139 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
1140
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
1141 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
1142 /* 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
1143 * 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
1144 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
1145 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
1146 return;
70d60b0104c1 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium <rubidium@openttd.org>
parents: 6823
diff changeset
1147 }
70d60b0104c1 (svn r10084) -Fix [FS#855]: reversing a train when loading at a station crashed.
rubidium <rubidium@openttd.org>
parents: 6823
diff changeset
1148
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1149 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
1150 Vehicle *u = v;
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1151 bool dirty_vehicle = false;
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1152 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
1153
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
1154 bool completely_emptied = true;
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1155 bool anything_unloaded = false;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1156 bool anything_loaded = false;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1157 uint32 cargo_not_full = 0;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1158 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
1159
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1160 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
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 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
1163
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
1164 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
1165 if (v->cargo_cap == 0) continue;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1166
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
1167 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
1168 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
1169
e723ec01b3a6 (svn r14672) -Fix [FS#2444]: Property 7 and callback 12 were broken for aircraft.
frosch <frosch@openttd.org>
parents: 10417
diff changeset
1170 /* 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
1171 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
1172
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
1173 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
1174 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
1175 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
1176 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1177
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1178 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
1179
8883
23eea6824c4b (svn r12650) -Feature: ability to force a vehicle to not load at a station.
rubidium <rubidium@openttd.org>
parents: 8873
diff changeset
1180 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
1181 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
1182 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
1183 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
1184 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
1185
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
1186 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
1187
8855
2acce887e7ec (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium <rubidium@openttd.org>
parents: 8843
diff changeset
1188 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
1189 /* 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
1190 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
1191
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1192 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
1193 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
1194 }
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
1195
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
1196 /* 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
1197 * 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
1198 * 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
1199 * 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
1200 * 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
1201 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
1202 remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded, u->current_order.GetUnloadType() & OUFB_TRANSFER ? VehicleCargoList::MTA_TRANSFER : VehicleCargoList::MTA_UNLOAD, payment);
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
1203 SetBit(ge->acceptance_pickup, GoodsEntry::PICKUP);
6198
a2cdd211d091 (svn r8978) -Feature: Rewrite of transfer system.
richk <richk@openttd.org>
parents: 6190
diff changeset
1204
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1205 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
1206 } else if (!accepted) {
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1207 /* 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
1208 * 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
1209 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
1210
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
1211 /* 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
1212 * 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
1213 * 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
1214 * 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
1215 anything_unloaded = true;
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1216 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
1217 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1218
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1219 /* Deliver goods to the station */
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1220 st->time_since_unload = 0;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1221
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1222 unloading_time += amount_unloaded;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1223
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1224 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
1225 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
1226 completely_emptied = false;
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1227 } else {
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1228 /* 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
1229 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
1230 }
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1231
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1232 continue;
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1233 }
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
1234
8885
2d7e1a978a1a (svn r12652) -Codechange: rework the order GUI a little more to make it a little more clear that 'transfer' is just an unload type.
rubidium <rubidium@openttd.org>
parents: 8883
diff changeset
1235 /* Do not pick up goods when we have no-load set. */
2d7e1a978a1a (svn r12652) -Codechange: rework the order GUI a little more to make it a little more clear that 'transfer' is just an unload type.
rubidium <rubidium@openttd.org>
parents: 8883
diff changeset
1236 if (u->current_order.GetLoadType() & OLFB_NO_LOAD) 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
1237
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1238 /* update stats */
6609
e78f5516e040 (svn r9829) -Codechange: more refactoring of the unloading algorithm.
rubidium <rubidium@openttd.org>
parents: 6601
diff changeset
1239 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
1240 switch (u->type) {
13052
6dba8bf1c036 (svn r17550) -Fix (r8973): Aircraft are good enough, don't make them even better.
frosch <frosch@openttd.org>
parents: 13048
diff changeset
1241 case VEH_TRAIN: t = Train::From(u)->tcache.cached_max_speed; break;
6dba8bf1c036 (svn r17550) -Fix (r8973): Aircraft are good enough, don't make them even better.
frosch <frosch@openttd.org>
parents: 13048
diff changeset
1242 case VEH_ROAD: t = u->max_speed / 2; break;
6dba8bf1c036 (svn r17550) -Fix (r8973): Aircraft are good enough, don't make them even better.
frosch <frosch@openttd.org>
parents: 13048
diff changeset
1243 case VEH_SHIP: t = u->max_speed; break;
15511
fbbbb2791756 (svn r20164) -Fix [FS#3870]: inconsistencies w.r.t. to km/h vs km-ish/h as "base" unit for aircraft speeds
rubidium <rubidium@openttd.org>
parents: 15292
diff changeset
1244 case VEH_AIRCRAFT: t = u->max_speed * 10 / 128; break; // convert to old units
13052
6dba8bf1c036 (svn r17550) -Fix (r8973): Aircraft are good enough, don't make them even better.
frosch <frosch@openttd.org>
parents: 13048
diff changeset
1245 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
1246 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1247
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
1248 /* 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
1249 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
1250 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
1251 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
1252
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
1253 /* 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
1254 * 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
1255 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
1256 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
1257 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
1258 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
1259
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1260 /* 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
1261 * 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
1262 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
1263 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
1264 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
1265 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1266
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
1267 if (cap > count) cap = 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
1268 if (_settings_game.order.gradual_loading) cap = min(cap, load_amount);
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
1269 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
1270 /* 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
1271 cap = min((uint)cargo_left[v->cargo_type], cap);
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
1272 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
1273 }
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
1274
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
1275 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
1276
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1277 /* 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
1278 * 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
1279 * 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
1280 * 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
1281 * 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
1282 * 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
1283 * 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
1284 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
1285 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
1286
13291
07fc7c78b30b (svn r17800) -Codechange: first steps into making CargoList a template
rubidium <rubidium@openttd.org>
parents: 13218
diff changeset
1287 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
1288
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1289 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
1290 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
1291
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
1292 StationAnimationTrigger(st, st->xy, STAT_ANIM_CARGO_TAKEN, v->cargo_type);
14620
a2d95ade0e87 (svn r19197) -Codechange: introduce animation callbacks for airport tiles
yexo <yexo@openttd.org>
parents: 14353
diff changeset
1293 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
1294
7474
7b35fd1cc99d (svn r10981) -Fix [FS#1156] (r10970): stations gave ratings for accepted cargo too.
rubidium <rubidium@openttd.org>
parents: 7458
diff changeset
1295 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
1296
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1297 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
1298 }
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1299
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
1300 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
1301 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
1302 } 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
1303 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
1304 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1305 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1306
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 /* Only set completly_emptied, if we just unloaded all remaining cargo */
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 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
1309
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
1310 /* 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
1311 * 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
1312 * 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
1313 * 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
1314 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
1315 /* 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
1316 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
1317 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
1318 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
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 }
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
1321
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1322 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
1323
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
1324 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
1325
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 }
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1334 } else {
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1335 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
1336 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
1337 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
1338 /* 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
1339 * 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
1340 if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap > v->cargo.Count()) ||
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1341 (cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are stull non-full cargos
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1342 finished_loading = false;
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1343 }
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1344 } else if (cargo_not_full != 0) {
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1345 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
1346 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1347 }
6611
461107df1c8f (svn r9831) -Codechange: more refactoring of the loading/unloading.
rubidium <rubidium@openttd.org>
parents: 6609
diff changeset
1348 unloading_time = 20;
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 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
1351 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1352
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
1353 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
1354 /* Each platform tile is worth 2 rail vehicles. */
12114
f020ec6be498 (svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
smatz <smatz@openttd.org>
parents: 12082
diff changeset
1355 int overhang = Train::From(v)->tcache.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
1356 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
1357 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
1358 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
1359 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1360 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1361
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
1362 /* 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
1363 * 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
1364 * 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
1365 * 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
1366 * 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
1367 */
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
1368 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
1369 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
1370 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
1371 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
1372 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
1373 } 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
1374 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
1375 }
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
1376 }
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
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
1378 /* 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
1379 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
1380
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
1381 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
1382 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
1383 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1384
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1385 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
1386 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
1387 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
1388 v->MarkDirty();
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1389 }
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1390 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
1391 st->MarkTilesDirty(true);
13998
3d6fd0265851 (svn r18540) -Codechange: resulting in better name for 'result'.
frosch <frosch@openttd.org>
parents: 13995
diff changeset
1392 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
1393 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1394 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1395
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
1396 /**
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
1397 * 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
1398 * 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
1399 * @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
1400 */
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
1401 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
1402 {
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
1403 /* 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
1404 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
1405
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
1406 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
1407
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
1408 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
1409
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
1410 std::list<Vehicle *>::iterator 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
1411 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
1412 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
1413 if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left);
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
1414 }
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
1415
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
1416 /* 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
1417 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
1418 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
1419 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
1420 }
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
1421 _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
1422 }
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
1423
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
1424 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
1425 {
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
1426 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
1427 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
1428 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
1429 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
1430 }
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
1431 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
1432 HandleEconomyFluctuations();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1433 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1434
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
1435 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
1436 {
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
1437 Company *owner;
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
1438 int i;
6953
41fd36025784 (svn r10208) -Codechange: replace int32 with Money where appropriate.
rubidium <rubidium@openttd.org>
parents: 6952
diff changeset
1439 Money value;
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
1440 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
1441
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
1442 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
1443 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
1444
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
1445 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
1446 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
1447 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
1448 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
1449 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
1450 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
1451 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
1452
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
1453 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
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 if (c->bankrupt_value == 0) {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11730
diff changeset
1456 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
1457 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
1458 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1459
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
1460 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
1461 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
1462 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
1463 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
1464 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
1465 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
1466 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1467 }
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
1468 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
1469
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
1470 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
1471
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
1472 DeleteCompanyWindows(ci);
9297
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1473 InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1474 InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1475 InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9281
diff changeset
1476 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
1477
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
1478 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
1479 }
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 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
1482
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1483 /** Acquire shares in an opposing company.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1484 * @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
1485 * @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
1486 * @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
1487 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1488 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1489 * @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
1490 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1491 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
1492 {
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
1493 CommandCost cost(EXPENSES_OTHER);
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1494 CompanyID target_company = (CompanyID)p1;
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1495 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
1496
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
1497 /* 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
1498 * Cannot buy own shares */
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1499 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
1500
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1501 /* 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
1502 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
1503
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1504 /* 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
1505 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
1506
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
1507 /* We can not buy out a real company (temporarily). TODO: well, enable it obviously */
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
1508 if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 1 && !c->is_ai) 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
1509
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
1510 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
1511 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
1512 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
1513 int i;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1514
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
1515 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
1516 *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
1517
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
1518 for (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
1519 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
1520 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
1521 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
1522 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1523 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1524 }
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1525 SetWindowDirty(WC_COMPANY, target_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
1526 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1527 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
1528 }
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 /** Sell shares in an opposing company.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1531 * @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
1532 * @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
1533 * @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
1534 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1535 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1536 * @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
1537 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1538 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
1539 {
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1540 CompanyID target_company = (CompanyID)p1;
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1541 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
1542
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
1543 /* 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
1544 * Cannot sell own shares */
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1545 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
1546
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1547 /* 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
1548 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
1549
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1550 /* 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
1551 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
1552 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
1553
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1554 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
1555 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
1556 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
1557 *b = COMPANY_SPECTATOR;
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1558 SetWindowDirty(WC_COMPANY, target_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
1559 }
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
1560 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
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
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1563 /** Buy up another company.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1564 * 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
1565 * 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
1566 * @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
1567 * @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
1568 * @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
1569 * @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
1570 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1571 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1572 * @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
1573 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1574 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
1575 {
15038
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1576 CompanyID target_company = (CompanyID)p1;
80e8104bac11 (svn r19655) -Codechange: Reduce usage magic p1 parameter.
frosch <frosch@openttd.org>
parents: 15035
diff changeset
1577 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
1578 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
1579
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
1580 /* 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
1581 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
1582
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
1583 /* 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
1584 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
1585
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
1586 /* 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
1587 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
1588
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
1589 /* 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
1590 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
1591
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1592 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
1593 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
1594 }
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
1595 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
1596 }