ai_cargo.cpp

Go to the documentation of this file.
00001 /* $Id: ai_cargo.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "ai_cargo.hpp"
00006 #include "../../cargotype.h"
00007 #include "../../economy_func.h"
00008 #include "../../core/alloc_func.hpp"
00009 #include "../../core/bitmath_func.hpp"
00010 #include "../../newgrf_cargo.h"
00011 
00012 /* static */ bool AICargo::IsValidCargo(CargoID cargo_type)
00013 {
00014   return (cargo_type < NUM_CARGO && ::GetCargo(cargo_type)->IsValid());
00015 }
00016 
00017 /* static */ char *AICargo::GetCargoLabel(CargoID cargo_type)
00018 {
00019   if (!IsValidCargo(cargo_type)) return NULL;
00020   const CargoSpec *cargo = ::GetCargo(cargo_type);
00021 
00022   /* cargo->label is a uint32 packing a 4 character non-terminated string,
00023    * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
00024   char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
00025   for (uint i = 0; i < sizeof(cargo->label); i++) {
00026     cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
00027   }
00028   cargo_label[sizeof(cargo->label)] = '\0';
00029   return cargo_label;
00030 }
00031 
00032 /* static */ bool AICargo::IsFreight(CargoID cargo_type)
00033 {
00034   if (!IsValidCargo(cargo_type)) return false;
00035   const CargoSpec *cargo = ::GetCargo(cargo_type);
00036   return cargo->is_freight;
00037 }
00038 
00039 /* static */ bool AICargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
00040 {
00041   if (!IsValidCargo(cargo_type)) return false;
00042   return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
00043 }
00044 
00045 /* static */ AICargo::TownEffect AICargo::GetTownEffect(CargoID cargo_type)
00046 {
00047   if (!IsValidCargo(cargo_type)) return TE_NONE;
00048 
00049   return (AICargo::TownEffect)GetCargo(cargo_type)->town_effect;
00050 }
00051 
00052 /* static */ Money AICargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
00053 {
00054   if (!IsValidCargo(cargo_type)) return -1;
00055   return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
00056 }

Generated on Wed Jun 3 19:05:08 2009 for OpenTTD by  doxygen 1.5.6