2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_cargo.cpp Implementation of ScriptCargo. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_cargo.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../../economy_func.h"
|
|
|
|
#include "../../core/bitmath_func.hpp"
|
2014-03-10 22:18:53 +00:00
|
|
|
#include "../../settings_type.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCargo::IsValidCargo(CargoID cargo_type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-07-16 19:00:13 +00:00
|
|
|
return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCargo::IsValidTownEffect(TownEffect towneffect_type)
|
2011-11-23 16:05:19 +00:00
|
|
|
{
|
2011-11-23 16:29:34 +00:00
|
|
|
return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END);
|
2011-11-23 16:05:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ char *ScriptCargo::GetCargoLabel(CargoID cargo_type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidCargo(cargo_type)) return NULL;
|
2009-07-16 19:00:13 +00:00
|
|
|
const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/* cargo->label is a uint32 packing a 4 character non-terminated string,
|
|
|
|
* like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
|
|
|
|
char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
|
|
|
|
for (uint i = 0; i < sizeof(cargo->label); i++) {
|
|
|
|
cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
|
|
|
|
}
|
|
|
|
cargo_label[sizeof(cargo->label)] = '\0';
|
|
|
|
return cargo_label;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCargo::IsFreight(CargoID cargo_type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidCargo(cargo_type)) return false;
|
2009-07-16 19:00:13 +00:00
|
|
|
const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
|
2009-01-12 17:11:45 +00:00
|
|
|
return cargo->is_freight;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptCargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidCargo(cargo_type)) return false;
|
|
|
|
return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptCargo::TownEffect ScriptCargo::GetTownEffect(CargoID cargo_type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidCargo(cargo_type)) return TE_NONE;
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return (ScriptCargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptCargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidCargo(cargo_type)) return -1;
|
|
|
|
return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
|
|
|
|
}
|
2014-03-10 22:18:53 +00:00
|
|
|
|
|
|
|
/* static */ ScriptCargo::DistributionType ScriptCargo::GetDistributionType(CargoID cargo_type)
|
|
|
|
{
|
|
|
|
if (!ScriptCargo::IsValidCargo(cargo_type)) return INVALID_DISTRIBUTION_TYPE;
|
|
|
|
return (ScriptCargo::DistributionType)_settings_game.linkgraph.GetDistributionType(cargo_type);
|
|
|
|
}
|