script_cargo.cpp

Go to the documentation of this file.
00001 /* $Id: script_cargo.cpp 23355 2011-11-29 23:15:35Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "script_cargo.hpp"
00014 #include "../../cargotype.h"
00015 #include "../../economy_func.h"
00016 #include "../../core/bitmath_func.hpp"
00017 
00018 /* static */ bool ScriptCargo::IsValidCargo(CargoID cargo_type)
00019 {
00020   return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
00021 }
00022 
00023 /* static */ bool ScriptCargo::IsValidTownEffect(TownEffect towneffect_type)
00024 {
00025   return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END);
00026 }
00027 
00028 /* static */ char *ScriptCargo::GetCargoLabel(CargoID cargo_type)
00029 {
00030   if (!IsValidCargo(cargo_type)) return NULL;
00031   const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
00032 
00033   /* cargo->label is a uint32 packing a 4 character non-terminated string,
00034    * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
00035   char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
00036   for (uint i = 0; i < sizeof(cargo->label); i++) {
00037     cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
00038   }
00039   cargo_label[sizeof(cargo->label)] = '\0';
00040   return cargo_label;
00041 }
00042 
00043 /* static */ bool ScriptCargo::IsFreight(CargoID cargo_type)
00044 {
00045   if (!IsValidCargo(cargo_type)) return false;
00046   const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
00047   return cargo->is_freight;
00048 }
00049 
00050 /* static */ bool ScriptCargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
00051 {
00052   if (!IsValidCargo(cargo_type)) return false;
00053   return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
00054 }
00055 
00056 /* static */ ScriptCargo::TownEffect ScriptCargo::GetTownEffect(CargoID cargo_type)
00057 {
00058   if (!IsValidCargo(cargo_type)) return TE_NONE;
00059 
00060   return (ScriptCargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
00061 }
00062 
00063 /* static */ Money ScriptCargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
00064 {
00065   if (!IsValidCargo(cargo_type)) return -1;
00066   return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
00067 }