# cargotype.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "cargotype.h"
    00014 #include "core/bitmath_func.hpp"
    00015 #include "newgrf_cargo.h"
    00016
    00017 #include "table/sprites.h"
    00018 #include "table/strings.h"
    00019 #include "table/cargo_const.h"
    00020
    00021 CargoSpec CargoSpec::array[NUM_CARGO];
    00022
    00026 uint32 _cargo_mask;
    00027
    00031 void SetupCargoForClimate(LandscapeID l)
    00032 {
    00033   assert(l < lengthof(_default_climate_cargo));
    00034
    00035   /* Reset and disable all cargo types */
    00036   memset(CargoSpec::array, 0, sizeof(CargoSpec::array));
    00037   for (CargoID i = 0; i < lengthof(CargoSpec::array); i++) CargoSpec::Get(i)->bitnum = INVALID_CARGO;
    00038
    00039   _cargo_mask = 0;
    00040
    00041   for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
    00042     CargoLabel cl = _default_climate_cargo[l][i];
    00043
    00044     /* Bzzt: check if cl is just an index into the cargo table */
    00045     if (cl < lengthof(_default_cargo)) {
    00046       /* Copy the indexed cargo */
    00047       CargoSpec *cargo = CargoSpec::Get(i);
    00048       *cargo = _default_cargo[cl];
    00049       if (cargo->bitnum != INVALID_CARGO) SetBit(_cargo_mask, i);
    00050       continue;
    00051     }
    00052
    00053     /* Loop through each of the default cargo types to see if
    00054      * the label matches */
    00055     for (uint j = 0; j < lengthof(_default_cargo); j++) {
    00056       if (_default_cargo[j].label == cl) {
    00057         *CargoSpec::Get(i) = _default_cargo[j];
    00058
    00059         /* Populate the available cargo mask */
    00060         SetBit(_cargo_mask, i);
    00061         break;
    00062       }
    00063     }
    00064   }
    00065 }
    00066
    00071 CargoID GetCargoIDByLabel(CargoLabel cl)
    00072 {
    00073   const CargoSpec *cs;
    00074   FOR_ALL_CARGOSPECS(cs) {
    00075     if (cs->label == cl) return cs->Index();
    00076   }
    00077
    00078   /* No matching label was found, so it is invalid */
    00079   return CT_INVALID;
    00080 }
    00081
    00082
    00087 CargoID GetCargoIDByBitnum(uint8 bitnum)
    00088 {
    00089   if (bitnum == INVALID_CARGO) return CT_INVALID;
    00090
    00091   const CargoSpec *cs;
    00092   FOR_ALL_CARGOSPECS(cs) {
    00093     if (cs->bitnum == bitnum) return cs->Index();
    00094   }
    00095
    00096   /* No matching label was found, so it is invalid */
    00097   return CT_INVALID;
    00098 }
    00099
    00103 SpriteID CargoSpec::GetCargoIcon() const
    00104 {
    00105   SpriteID sprite = this->sprite;
    00106   if (sprite == 0xFFFF) {
    00107     /* A value of 0xFFFF indicates we should draw a custom icon */
    00108     sprite = GetCustomCargoSprite(this);
    00109   }
    00110
    00111   if (sprite == 0) sprite = SPR_CARGO_GOODS;
    00112
    00113   return sprite;
    00114 }
    00115