peter1138@6091: /* $Id$ */ peter1138@6091: rubidium@9111: /** @file cargotype.cpp Implementation of cargos. */ belugas@6123: peter1138@6091: #include "stdafx.h" peter1138@6091: #include "openttd.h" peter1138@6091: #include "newgrf_cargo.h" peter1138@6091: #include "cargotype.h" rubidium@8113: #include "core/bitmath_func.hpp" peter1138@6091: rubidium@8264: #include "table/sprites.h" rubidium@8264: #include "table/strings.h" peter1138@6091: #include "table/cargo_const.h" peter1138@6091: peter1138@6359: CargoSpec _cargo[NUM_CARGO]; peter1138@6091: peter1138@6091: static const byte INVALID_CARGO = 0xFF; peter1138@6091: peter1138@6148: /* Bitmask of cargo types available */ peter1138@6113: uint32 _cargo_mask; peter1138@6113: peter1138@6091: peter1138@6091: void SetupCargoForClimate(LandscapeID l) peter1138@6091: { peter1138@6091: assert(l < lengthof(_default_climate_cargo)); peter1138@6091: peter1138@6091: /* Reset and disable all cargo types */ peter1138@6091: memset(_cargo, 0, sizeof(_cargo)); peter1138@6091: for (CargoID i = 0; i < lengthof(_cargo); i++) _cargo[i].bitnum = INVALID_CARGO; peter1138@6091: peter1138@6113: _cargo_mask = 0; peter1138@6113: peter1138@6091: for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) { peter1138@6091: CargoLabel cl = _default_climate_cargo[l][i]; peter1138@6091: peter1138@6640: /* Bzzt: check if cl is just an index into the cargo table */ peter1138@6640: if (cl < lengthof(_default_cargo)) { peter1138@6640: /* Copy the indexed cargo */ peter1138@6640: _cargo[i] = _default_cargo[cl]; peter1138@8195: if (_cargo[i].bitnum != INVALID_CARGO) SetBit(_cargo_mask, i); peter1138@6640: continue; peter1138@6640: } peter1138@6640: peter1138@6091: /* Loop through each of the default cargo types to see if peter1138@6091: * the label matches */ peter1138@6091: for (uint j = 0; j < lengthof(_default_cargo); j++) { peter1138@6091: if (_default_cargo[j].label == cl) { peter1138@6091: _cargo[i] = _default_cargo[j]; peter1138@6113: peter1138@6148: /* Populate the available cargo mask */ skidd13@7931: SetBit(_cargo_mask, i); peter1138@6091: break; peter1138@6091: } peter1138@6091: } peter1138@6091: } peter1138@6091: } peter1138@6091: peter1138@6091: peter1138@6091: const CargoSpec *GetCargo(CargoID c) peter1138@6091: { peter1138@6091: assert(c < lengthof(_cargo)); peter1138@6091: return &_cargo[c]; peter1138@6091: } peter1138@6091: peter1138@6113: peter1138@6122: bool CargoSpec::IsValid() const peter1138@6122: { peter1138@6122: return bitnum != INVALID_CARGO; peter1138@6122: } peter1138@6143: peter1138@6143: peter1138@6143: CargoID GetCargoIDByLabel(CargoLabel cl) peter1138@6143: { peter1138@6143: for (CargoID c = 0; c < lengthof(_cargo); c++) { peter1138@6360: if (_cargo[c].bitnum == INVALID_CARGO) continue; peter1138@6143: if (_cargo[c].label == cl) return c; peter1138@6143: } peter1138@6143: peter1138@6143: /* No matching label was found, so it is invalid */ peter1138@6143: return CT_INVALID; peter1138@6143: } peter1138@6460: peter1138@6460: peter1138@6460: /** Find the CargoID of a 'bitnum' value. peter1138@6460: * @param bitnum 'bitnum' to find. peter1138@6460: * @return First CargoID with the given bitnum, or CT_INVALID if not found. peter1138@6460: */ peter1138@6460: CargoID GetCargoIDByBitnum(uint8 bitnum) peter1138@6460: { peter1138@6460: if (bitnum == INVALID_CARGO) return CT_INVALID; peter1138@6460: peter1138@6460: for (CargoID c = 0; c < lengthof(_cargo); c++) { peter1138@6460: if (_cargo[c].bitnum == bitnum) return c; peter1138@6460: } peter1138@6460: peter1138@6460: /* No matching label was found, so it is invalid */ peter1138@6460: return CT_INVALID; peter1138@6460: } peter1138@6460: