2007-02-20 23:09:21 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file cargotype.cpp Implementation of cargos. */
|
2007-02-23 12:50:43 +01:00
|
|
|
|
2007-02-20 23:09:21 +01:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "newgrf_cargo.h"
|
|
|
|
#include "cargotype.h"
|
2007-12-21 20:21:21 +01:00
|
|
|
#include "core/bitmath_func.hpp"
|
2007-02-20 23:09:21 +01:00
|
|
|
|
2008-01-13 02:21:35 +01:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
2007-02-20 23:09:21 +01:00
|
|
|
#include "table/cargo_const.h"
|
|
|
|
|
2009-07-16 22:40:06 +02:00
|
|
|
CargoSpec CargoSpec::array[NUM_CARGO];
|
2007-02-20 23:09:21 +01:00
|
|
|
|
2007-02-25 00:36:40 +01:00
|
|
|
/* Bitmask of cargo types available */
|
2007-02-22 23:09:51 +01:00
|
|
|
uint32 _cargo_mask;
|
|
|
|
|
2007-02-20 23:09:21 +01:00
|
|
|
|
|
|
|
void SetupCargoForClimate(LandscapeID l)
|
|
|
|
{
|
|
|
|
assert(l < lengthof(_default_climate_cargo));
|
|
|
|
|
|
|
|
/* Reset and disable all cargo types */
|
2009-07-16 22:40:06 +02:00
|
|
|
memset(CargoSpec::array, 0, sizeof(CargoSpec::array));
|
|
|
|
for (CargoID i = 0; i < lengthof(CargoSpec::array); i++) CargoSpec::Get(i)->bitnum = INVALID_CARGO;
|
2007-02-20 23:09:21 +01:00
|
|
|
|
2007-02-22 23:09:51 +01:00
|
|
|
_cargo_mask = 0;
|
|
|
|
|
2007-02-20 23:09:21 +01:00
|
|
|
for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
|
|
|
|
CargoLabel cl = _default_climate_cargo[l][i];
|
|
|
|
|
2007-05-18 23:50:32 +02:00
|
|
|
/* Bzzt: check if cl is just an index into the cargo table */
|
|
|
|
if (cl < lengthof(_default_cargo)) {
|
|
|
|
/* Copy the indexed cargo */
|
2009-07-16 21:00:13 +02:00
|
|
|
CargoSpec *cargo = CargoSpec::Get(i);
|
|
|
|
*cargo = _default_cargo[cl];
|
|
|
|
if (cargo->bitnum != INVALID_CARGO) SetBit(_cargo_mask, i);
|
2007-05-18 23:50:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-02-20 23:09:21 +01: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) {
|
2009-07-16 21:00:13 +02:00
|
|
|
*CargoSpec::Get(i) = _default_cargo[j];
|
2007-02-22 23:09:51 +01:00
|
|
|
|
2007-02-25 00:36:40 +01:00
|
|
|
/* Populate the available cargo mask */
|
2007-11-20 14:35:54 +01:00
|
|
|
SetBit(_cargo_mask, i);
|
2007-02-20 23:09:21 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-24 20:36:47 +01:00
|
|
|
CargoID GetCargoIDByLabel(CargoLabel cl)
|
|
|
|
{
|
2009-07-16 23:37:59 +02:00
|
|
|
const CargoSpec *cs;
|
2009-07-16 22:40:06 +02:00
|
|
|
FOR_ALL_CARGOSPECS(cs) {
|
|
|
|
if (cs->label == cl) return cs->Index();
|
2007-02-24 20:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No matching label was found, so it is invalid */
|
|
|
|
return CT_INVALID;
|
|
|
|
}
|
2007-04-13 21:32:18 +02: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;
|
|
|
|
|
2009-07-16 23:37:59 +02:00
|
|
|
const CargoSpec *cs;
|
2009-07-16 22:40:06 +02:00
|
|
|
FOR_ALL_CARGOSPECS(cs) {
|
|
|
|
if (cs->bitnum == bitnum) return cs->Index();
|
2007-04-13 21:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No matching label was found, so it is invalid */
|
|
|
|
return CT_INVALID;
|
|
|
|
}
|
|
|
|
|