(svn r27985) -Codechange: Convert VA2 switches into ones with non-overlapping ranges...
[openttd.git] / src / script / api / script_cargo.cpp
bloba2643f897ffaaed68d402c315c62d2d86d755913
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
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.
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.
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/>.
8 */
10 /** @file script_cargo.cpp Implementation of ScriptCargo. */
12 #include "../../stdafx.h"
13 #include "script_cargo.hpp"
14 #include "../../economy_func.h"
15 #include "../../core/bitmath_func.hpp"
16 #include "../../settings_type.h"
18 #include "../../safeguards.h"
20 /* static */ bool ScriptCargo::IsValidCargo(CargoID cargo_type)
22 return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
25 /* static */ bool ScriptCargo::IsValidTownEffect(TownEffect towneffect_type)
27 return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END);
30 /* static */ char *ScriptCargo::GetCargoLabel(CargoID cargo_type)
32 if (!IsValidCargo(cargo_type)) return NULL;
33 const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
35 /* cargo->label is a uint32 packing a 4 character non-terminated string,
36 * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
37 char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
38 for (uint i = 0; i < sizeof(cargo->label); i++) {
39 cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
41 cargo_label[sizeof(cargo->label)] = '\0';
42 return cargo_label;
45 /* static */ bool ScriptCargo::IsFreight(CargoID cargo_type)
47 if (!IsValidCargo(cargo_type)) return false;
48 const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
49 return cargo->is_freight;
52 /* static */ bool ScriptCargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
54 if (!IsValidCargo(cargo_type)) return false;
55 return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
58 /* static */ ScriptCargo::TownEffect ScriptCargo::GetTownEffect(CargoID cargo_type)
60 if (!IsValidCargo(cargo_type)) return TE_NONE;
62 return (ScriptCargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
65 /* static */ Money ScriptCargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
67 if (!IsValidCargo(cargo_type)) return -1;
68 return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
71 /* static */ ScriptCargo::DistributionType ScriptCargo::GetDistributionType(CargoID cargo_type)
73 if (!ScriptCargo::IsValidCargo(cargo_type)) return INVALID_DISTRIBUTION_TYPE;
74 return (ScriptCargo::DistributionType)_settings_game.linkgraph.GetDistributionType(cargo_type);