peter1138@6417: /* $Id$ */ peter1138@6417: belugas@6449: /** @file cargotype.cpp */ belugas@6449: peter1138@6417: #include "stdafx.h" peter1138@6417: #include "openttd.h" peter1138@6417: #include "newgrf_cargo.h" peter1138@6417: #include "cargotype.h" rubidium@9723: #include "core/bitmath_func.hpp" peter1138@6417: rubidium@9724: #include "table/sprites.h" rubidium@9724: #include "table/strings.h" peter1138@6417: #include "table/cargo_const.h" peter1138@6417: truelight@9517: 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: glx@9624: /* Bzzt: check if cl is just an index into the cargo table */ glx@9624: if (cl < lengthof(_default_cargo)) { glx@9624: /* Copy the indexed cargo */ glx@9624: _cargo[i] = _default_cargo[cl]; rubidium@9723: if (_cargo[i].bitnum != INVALID_CARGO) SetBit(_cargo_mask, i); glx@9624: continue; glx@9624: } glx@9624: 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 */ rubidium@9722: 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++) { truelight@9517: 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: } rubidium@9599: rubidium@9599: rubidium@9599: /** Find the CargoID of a 'bitnum' value. rubidium@9599: * @param bitnum 'bitnum' to find. rubidium@9599: * @return First CargoID with the given bitnum, or CT_INVALID if not found. rubidium@9599: */ rubidium@9599: CargoID GetCargoIDByBitnum(uint8 bitnum) rubidium@9599: { rubidium@9599: if (bitnum == INVALID_CARGO) return CT_INVALID; rubidium@9599: rubidium@9599: for (CargoID c = 0; c < lengthof(_cargo); c++) { rubidium@9599: if (_cargo[c].bitnum == bitnum) return c; rubidium@9599: } rubidium@9599: rubidium@9599: /* No matching label was found, so it is invalid */ rubidium@9599: return CT_INVALID; rubidium@9599: } rubidium@9599: