ai_cargo.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "ai_cargo.hpp"
00013 #include "../../cargotype.h"
00014 #include "../../economy_func.h"
00015 #include "../../core/bitmath_func.hpp"
00016
00017 bool AICargo::IsValidCargo(CargoID cargo_type)
00018 {
00019 return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
00020 }
00021
00022 char *AICargo::GetCargoLabel(CargoID cargo_type)
00023 {
00024 if (!IsValidCargo(cargo_type)) return NULL;
00025 const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
00026
00027
00028
00029 char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
00030 for (uint i = 0; i < sizeof(cargo->label); i++) {
00031 cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
00032 }
00033 cargo_label[sizeof(cargo->label)] = '\0';
00034 return cargo_label;
00035 }
00036
00037 bool AICargo::IsFreight(CargoID cargo_type)
00038 {
00039 if (!IsValidCargo(cargo_type)) return false;
00040 const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
00041 return cargo->is_freight;
00042 }
00043
00044 bool AICargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
00045 {
00046 if (!IsValidCargo(cargo_type)) return false;
00047 return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
00048 }
00049
00050 AICargo::TownEffect AICargo::GetTownEffect(CargoID cargo_type)
00051 {
00052 if (!IsValidCargo(cargo_type)) return TE_NONE;
00053
00054 return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
00055 }
00056
00057 Money AICargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
00058 {
00059 if (!IsValidCargo(cargo_type)) return -1;
00060 return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
00061 }