2007-02-20 22:09:21 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file cargotype.cpp Implementation of cargos. */
|
2007-02-23 11:50:43 +00:00
|
|
|
|
2007-02-20 22:09:21 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "newgrf_cargo.h"
|
|
|
|
#include "cargotype.h"
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
2007-02-20 22:09:21 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
2007-02-20 22:09:21 +00:00
|
|
|
#include "table/cargo_const.h"
|
|
|
|
|
2007-03-22 23:19:40 +00:00
|
|
|
CargoSpec _cargo[NUM_CARGO];
|
2007-02-20 22:09:21 +00:00
|
|
|
|
|
|
|
static const byte INVALID_CARGO = 0xFF;
|
|
|
|
|
2007-02-24 23:36:40 +00:00
|
|
|
/* Bitmask of cargo types available */
|
2007-02-22 22:09:51 +00:00
|
|
|
uint32 _cargo_mask;
|
|
|
|
|
2007-02-20 22:09:21 +00:00
|
|
|
|
|
|
|
void SetupCargoForClimate(LandscapeID l)
|
|
|
|
{
|
|
|
|
assert(l < lengthof(_default_climate_cargo));
|
|
|
|
|
|
|
|
/* Reset and disable all cargo types */
|
|
|
|
memset(_cargo, 0, sizeof(_cargo));
|
|
|
|
for (CargoID i = 0; i < lengthof(_cargo); i++) _cargo[i].bitnum = INVALID_CARGO;
|
|
|
|
|
2007-02-22 22:09:51 +00:00
|
|
|
_cargo_mask = 0;
|
|
|
|
|
2007-02-20 22:09:21 +00:00
|
|
|
for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
|
|
|
|
CargoLabel cl = _default_climate_cargo[l][i];
|
|
|
|
|
2007-05-18 21:50:32 +00:00
|
|
|
/* Bzzt: check if cl is just an index into the cargo table */
|
|
|
|
if (cl < lengthof(_default_cargo)) {
|
|
|
|
/* Copy the indexed cargo */
|
|
|
|
_cargo[i] = _default_cargo[cl];
|
2008-01-04 17:25:53 +00:00
|
|
|
if (_cargo[i].bitnum != INVALID_CARGO) SetBit(_cargo_mask, i);
|
2007-05-18 21:50:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-02-20 22:09:21 +00:00
|
|
|
/* Loop through each of the default cargo types to see if
|
|
|
|
* the label matches */
|
|
|
|
for (uint j = 0; j < lengthof(_default_cargo); j++) {
|
|
|
|
if (_default_cargo[j].label == cl) {
|
|
|
|
_cargo[i] = _default_cargo[j];
|
2007-02-22 22:09:51 +00:00
|
|
|
|
2007-02-24 23:36:40 +00:00
|
|
|
/* Populate the available cargo mask */
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(_cargo_mask, i);
|
2007-02-20 22:09:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const CargoSpec *GetCargo(CargoID c)
|
|
|
|
{
|
|
|
|
assert(c < lengthof(_cargo));
|
|
|
|
return &_cargo[c];
|
|
|
|
}
|
|
|
|
|
2007-02-22 22:09:51 +00:00
|
|
|
|
2007-02-23 09:56:20 +00:00
|
|
|
bool CargoSpec::IsValid() const
|
|
|
|
{
|
|
|
|
return bitnum != INVALID_CARGO;
|
|
|
|
}
|
2007-02-24 19:36:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
CargoID GetCargoIDByLabel(CargoLabel cl)
|
|
|
|
{
|
|
|
|
for (CargoID c = 0; c < lengthof(_cargo); c++) {
|
2007-03-22 23:32:24 +00:00
|
|
|
if (_cargo[c].bitnum == INVALID_CARGO) continue;
|
2007-02-24 19:36:47 +00:00
|
|
|
if (_cargo[c].label == cl) return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No matching label was found, so it is invalid */
|
|
|
|
return CT_INVALID;
|
|
|
|
}
|
2007-04-13 19:32:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Find the CargoID of a 'bitnum' value.
|
|
|
|
* @param bitnum 'bitnum' to find.
|
|
|
|
* @return First CargoID with the given bitnum, or CT_INVALID if not found.
|
|
|
|
*/
|
|
|
|
CargoID GetCargoIDByBitnum(uint8 bitnum)
|
|
|
|
{
|
|
|
|
if (bitnum == INVALID_CARGO) return CT_INVALID;
|
|
|
|
|
|
|
|
for (CargoID c = 0; c < lengthof(_cargo); c++) {
|
|
|
|
if (_cargo[c].bitnum == bitnum) return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No matching label was found, so it is invalid */
|
|
|
|
return CT_INVALID;
|
|
|
|
}
|
|
|
|
|