cargotype.cpp
Go to the documentation of this file.00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "newgrf_cargo.h"
00008 #include "cargotype.h"
00009 #include "core/bitmath_func.hpp"
00010
00011 #include "table/sprites.h"
00012 #include "table/strings.h"
00013 #include "table/cargo_const.h"
00014
00015 CargoSpec _cargo[NUM_CARGO];
00016
00017 static const byte INVALID_CARGO = 0xFF;
00018
00019
00020 uint32 _cargo_mask;
00021
00022
00023 void SetupCargoForClimate(LandscapeID l)
00024 {
00025 assert(l < lengthof(_default_climate_cargo));
00026
00027
00028 memset(_cargo, 0, sizeof(_cargo));
00029 for (CargoID i = 0; i < lengthof(_cargo); i++) _cargo[i].bitnum = INVALID_CARGO;
00030
00031 _cargo_mask = 0;
00032
00033 for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
00034 CargoLabel cl = _default_climate_cargo[l][i];
00035
00036
00037 if (cl < lengthof(_default_cargo)) {
00038
00039 _cargo[i] = _default_cargo[cl];
00040 if (_cargo[i].bitnum != INVALID_CARGO) SetBit(_cargo_mask, i);
00041 continue;
00042 }
00043
00044
00045
00046 for (uint j = 0; j < lengthof(_default_cargo); j++) {
00047 if (_default_cargo[j].label == cl) {
00048 _cargo[i] = _default_cargo[j];
00049
00050
00051 SetBit(_cargo_mask, i);
00052 break;
00053 }
00054 }
00055 }
00056 }
00057
00058
00059 const CargoSpec *GetCargo(CargoID c)
00060 {
00061 assert(c < lengthof(_cargo));
00062 return &_cargo[c];
00063 }
00064
00065
00066 bool CargoSpec::IsValid() const
00067 {
00068 return bitnum != INVALID_CARGO;
00069 }
00070
00071
00072 CargoID GetCargoIDByLabel(CargoLabel cl)
00073 {
00074 for (CargoID c = 0; c < lengthof(_cargo); c++) {
00075 if (_cargo[c].bitnum == INVALID_CARGO) continue;
00076 if (_cargo[c].label == cl) return c;
00077 }
00078
00079
00080 return CT_INVALID;
00081 }
00082
00083
00088 CargoID GetCargoIDByBitnum(uint8 bitnum)
00089 {
00090 if (bitnum == INVALID_CARGO) return CT_INVALID;
00091
00092 for (CargoID c = 0; c < lengthof(_cargo); c++) {
00093 if (_cargo[c].bitnum == bitnum) return c;
00094 }
00095
00096
00097 return CT_INVALID;
00098 }
00099