1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
8 /** @file script_cargo.cpp Implementation of ScriptCargo. */
9
10 #include "../../stdafx.h"
11 #include "script_cargo.hpp"
12 #include "../../economy_func.h"
13 #include "../../core/bitmath_func.hpp"
14 #include "../../strings_func.h"
15 #include "../../settings_type.h"
16 #include "table/strings.h"
17
18 #include "../../safeguards.h"
19
IsValidCargo(CargoID cargo_type)20 /* static */ bool ScriptCargo::IsValidCargo(CargoID cargo_type)
21 {
22 return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
23 }
24
IsValidTownEffect(TownEffect towneffect_type)25 /* static */ bool ScriptCargo::IsValidTownEffect(TownEffect towneffect_type)
26 {
27 return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END);
28 }
29
GetName(CargoID cargo_type)30 /* static */ char *ScriptCargo::GetName(CargoID cargo_type)
31 {
32 if (!IsValidCargo(cargo_type)) return nullptr;
33
34 ::SetDParam(0, 1ULL << cargo_type);
35 return GetString(STR_JUST_CARGO_LIST);
36 }
37
GetCargoLabel(CargoID cargo_type)38 /* static */ char *ScriptCargo::GetCargoLabel(CargoID cargo_type)
39 {
40 if (!IsValidCargo(cargo_type)) return nullptr;
41 const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
42
43 /* cargo->label is a uint32 packing a 4 character non-terminated string,
44 * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
45 char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
46 for (uint i = 0; i < sizeof(cargo->label); i++) {
47 cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
48 }
49 cargo_label[sizeof(cargo->label)] = '\0';
50 return cargo_label;
51 }
52
IsFreight(CargoID cargo_type)53 /* static */ bool ScriptCargo::IsFreight(CargoID cargo_type)
54 {
55 if (!IsValidCargo(cargo_type)) return false;
56 const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
57 return cargo->is_freight;
58 }
59
HasCargoClass(CargoID cargo_type,CargoClass cargo_class)60 /* static */ bool ScriptCargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
61 {
62 if (!IsValidCargo(cargo_type)) return false;
63 return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
64 }
65
GetTownEffect(CargoID cargo_type)66 /* static */ ScriptCargo::TownEffect ScriptCargo::GetTownEffect(CargoID cargo_type)
67 {
68 if (!IsValidCargo(cargo_type)) return TE_NONE;
69
70 return (ScriptCargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
71 }
72
GetCargoIncome(CargoID cargo_type,uint32 distance,uint32 days_in_transit)73 /* static */ Money ScriptCargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
74 {
75 if (!IsValidCargo(cargo_type)) return -1;
76 return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
77 }
78
GetDistributionType(CargoID cargo_type)79 /* static */ ScriptCargo::DistributionType ScriptCargo::GetDistributionType(CargoID cargo_type)
80 {
81 if (!ScriptCargo::IsValidCargo(cargo_type)) return INVALID_DISTRIBUTION_TYPE;
82 return (ScriptCargo::DistributionType)_settings_game.linkgraph.GetDistributionType(cargo_type);
83 }
84