cargotype.cpp

Go to the documentation of this file.
00001 /* $Id: cargotype.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "stdafx.h"
00006 #include "newgrf_cargo.h"
00007 #include "cargotype.h"
00008 #include "core/bitmath_func.hpp"
00009 
00010 #include "table/sprites.h"
00011 #include "table/strings.h"
00012 #include "table/cargo_const.h"
00013 
00014 CargoSpec _cargo[NUM_CARGO];
00015 
00016 static const byte INVALID_CARGO = 0xFF;
00017 
00018 /* Bitmask of cargo types available */
00019 uint32 _cargo_mask;
00020 
00021 
00022 void SetupCargoForClimate(LandscapeID l)
00023 {
00024   assert(l < lengthof(_default_climate_cargo));
00025 
00026   /* Reset and disable all cargo types */
00027   memset(_cargo, 0, sizeof(_cargo));
00028   for (CargoID i = 0; i < lengthof(_cargo); i++) _cargo[i].bitnum = INVALID_CARGO;
00029 
00030   _cargo_mask = 0;
00031 
00032   for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
00033     CargoLabel cl = _default_climate_cargo[l][i];
00034 
00035     /* Bzzt: check if cl is just an index into the cargo table */
00036     if (cl < lengthof(_default_cargo)) {
00037       /* Copy the indexed cargo */
00038       _cargo[i] = _default_cargo[cl];
00039       if (_cargo[i].bitnum != INVALID_CARGO) SetBit(_cargo_mask, i);
00040       continue;
00041     }
00042 
00043     /* Loop through each of the default cargo types to see if
00044      * the label matches */
00045     for (uint j = 0; j < lengthof(_default_cargo); j++) {
00046       if (_default_cargo[j].label == cl) {
00047         _cargo[i] = _default_cargo[j];
00048 
00049         /* Populate the available cargo mask */
00050         SetBit(_cargo_mask, i);
00051         break;
00052       }
00053     }
00054   }
00055 }
00056 
00057 
00058 const CargoSpec *GetCargo(CargoID c)
00059 {
00060   assert(c < lengthof(_cargo));
00061   return &_cargo[c];
00062 }
00063 
00064 
00065 bool CargoSpec::IsValid() const
00066 {
00067   return bitnum != INVALID_CARGO;
00068 }
00069 
00070 
00071 CargoID GetCargoIDByLabel(CargoLabel cl)
00072 {
00073   for (CargoID c = 0; c < lengthof(_cargo); c++) {
00074     if (_cargo[c].bitnum == INVALID_CARGO) continue;
00075     if (_cargo[c].label == cl) return c;
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   for (CargoID c = 0; c < lengthof(_cargo); c++) {
00092     if (_cargo[c].bitnum == bitnum) return c;
00093   }
00094 
00095   /* No matching label was found, so it is invalid */
00096   return CT_INVALID;
00097 }
00098 

Generated on Tue Jul 21 18:48:22 2009 for OpenTTD by  doxygen 1.5.6