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/>.
8 /** @file script_cargo.cpp Implementation of ScriptCargo. */
10 #include "../../stdafx.h"
11 #include "script_cargo.hpp"
12 #include "../../economy_func.h"
13 #include "../../core/alloc_func.hpp"
14 #include "../../core/bitmath_func.hpp"
15 #include "../../strings_func.h"
16 #include "../../settings_type.h"
17 #include "table/strings.h"
19 #include "../../safeguards.h"
21 /* static */ bool ScriptCargo::IsValidCargo(CargoID cargo_type
)
23 return (cargo_type
< NUM_CARGO
&& ::CargoSpec::Get(cargo_type
)->IsValid());
26 /* static */ bool ScriptCargo::IsValidTownEffect(TownEffect towneffect_type
)
28 return (towneffect_type
>= (TownEffect
)TAE_BEGIN
&& towneffect_type
< (TownEffect
)TAE_END
);
31 /* static */ std::optional
<std::string
> ScriptCargo::GetName(CargoID cargo_type
)
33 if (!IsValidCargo(cargo_type
)) return std::nullopt
;
35 ::SetDParam(0, 1ULL << cargo_type
);
36 return GetString(STR_JUST_CARGO_LIST
);
39 /* static */ std::optional
<std::string
> ScriptCargo::GetCargoLabel(CargoID cargo_type
)
41 if (!IsValidCargo(cargo_type
)) return std::nullopt
;
42 const CargoSpec
*cargo
= ::CargoSpec::Get(cargo_type
);
44 /* cargo->label is a uint32_t packing a 4 character non-terminated string,
45 * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
46 std::string cargo_label
;
47 for (uint i
= 0; i
< sizeof(cargo
->label
); i
++) {
48 cargo_label
.push_back(GB(cargo
->label
.base(), (uint8_t)(sizeof(cargo
->label
) - i
- 1) * 8, 8));
53 /* static */ bool ScriptCargo::IsFreight(CargoID cargo_type
)
55 if (!IsValidCargo(cargo_type
)) return false;
56 const CargoSpec
*cargo
= ::CargoSpec::Get(cargo_type
);
57 return cargo
->is_freight
;
60 /* static */ bool ScriptCargo::HasCargoClass(CargoID cargo_type
, CargoClass cargo_class
)
62 if (!IsValidCargo(cargo_type
)) return false;
63 return ::IsCargoInClass(cargo_type
, (::CargoClass
)cargo_class
);
66 /* static */ ScriptCargo::TownEffect
ScriptCargo::GetTownEffect(CargoID cargo_type
)
68 if (!IsValidCargo(cargo_type
)) return TE_NONE
;
70 return (ScriptCargo::TownEffect
)::CargoSpec::Get(cargo_type
)->town_acceptance_effect
;
73 /* static */ Money
ScriptCargo::GetCargoIncome(CargoID cargo_type
, SQInteger distance
, SQInteger days_in_transit
)
75 if (!IsValidCargo(cargo_type
)) return -1;
77 distance
= Clamp
<SQInteger
>(distance
, 0, UINT32_MAX
);
79 return ::GetTransportedGoodsIncome(1, distance
, Clamp(days_in_transit
* 2 / 5, 0, UINT16_MAX
), cargo_type
);
82 /* static */ ScriptCargo::DistributionType
ScriptCargo::GetDistributionType(CargoID cargo_type
)
84 if (!ScriptCargo::IsValidCargo(cargo_type
)) return INVALID_DISTRIBUTION_TYPE
;
85 return (ScriptCargo::DistributionType
)_settings_game
.linkgraph
.GetDistributionType(cargo_type
);
88 /* static */ SQInteger
ScriptCargo::GetWeight(CargoID cargo_type
, SQInteger amount
)
90 if (!IsValidCargo(cargo_type
)) return -1;
92 amount
= Clamp
<SQInteger
>(amount
, 0, UINT32_MAX
);
94 return ::CargoSpec::Get(cargo_type
)->WeightOfNUnits(amount
);