# ai_cargo.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "ai_cargo.hpp"
    00014 #include "../../cargotype.h"
    00015 #include "../../economy_func.h"
    00016 #include "../../core/bitmath_func.hpp"
    00017
    00018 /* static */ bool AICargo::IsValidCargo(CargoID cargo_type)
    00019 {
    00020   return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
    00021 }
    00022
    00023 /* static */ char *AICargo::GetCargoLabel(CargoID cargo_type)
    00024 {
    00025   if (!IsValidCargo(cargo_type)) return NULL;
    00026   const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
    00027
    00028   /* cargo->label is a uint32 packing a 4 character non-terminated string,
    00029    * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
    00030   char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
    00031   for (uint i = 0; i < sizeof(cargo->label); i++) {
    00032     cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
    00033   }
    00034   cargo_label[sizeof(cargo->label)] = '\0';
    00035   return cargo_label;
    00036 }
    00037
    00038 /* static */ bool AICargo::IsFreight(CargoID cargo_type)
    00039 {
    00040   if (!IsValidCargo(cargo_type)) return false;
    00041   const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
    00042   return cargo->is_freight;
    00043 }
    00044
    00045 /* static */ bool AICargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
    00046 {
    00047   if (!IsValidCargo(cargo_type)) return false;
    00048   return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
    00049 }
    00050
    00051 /* static */ AICargo::TownEffect AICargo::GetTownEffect(CargoID cargo_type)
    00052 {
    00053   if (!IsValidCargo(cargo_type)) return TE_NONE;
    00054
    00055   return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
    00056 }
    00057
    00058 /* static */ Money AICargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
    00059 {
    00060   if (!IsValidCargo(cargo_type)) return -1;
    00061   return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
    00062 }