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