ai_cargo.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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 bool AICargo::IsValidCargo(CargoID cargo_type)
00019 {
00020 return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
00021 }
00022
00023 char *AICargo::GetCargoLabel(CargoID cargo_type)
00024 {
00025 if (!IsValidCargo(cargo_type)) return NULL;
00026 const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
00027
00028
00029
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 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 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 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 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 }