src/cargotype.cpp
author bjarni
Wed, 28 Feb 2007 00:33:40 +0000
branchcpp_gui
changeset 6268 4b5241e5dd10
child 6303 84c215fc8eb8
permissions -rw-r--r--
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
/* $Id$ */

/** @file cargotype.cpp */

#include "stdafx.h"
#include "openttd.h"
#include "macros.h"
#include "table/sprites.h"
#include "table/strings.h"
#include "newgrf_cargo.h"
#include "cargotype.h"

#include "table/cargo_const.h"

static CargoSpec _cargo[NUM_CARGO];

static const byte INVALID_CARGO = 0xFF;

/* Bitmask of cargo types available */
uint32 _cargo_mask;


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;

	_cargo_mask = 0;

	for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
		CargoLabel cl = _default_climate_cargo[l][i];

		/* 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];

				/* Populate the available cargo mask */
				SETBIT(_cargo_mask, i);
				break;
			}
		}
	}
}


const CargoSpec *GetCargo(CargoID c)
{
	assert(c < lengthof(_cargo));
	return &_cargo[c];
}


bool CargoSpec::IsValid() const
{
	return bitnum != INVALID_CARGO;
}


CargoID GetCargoIDByLabel(CargoLabel cl)
{
	for (CargoID c = 0; c < lengthof(_cargo); c++) {
		if (_cargo[c].label == cl) return c;
	}

	/* No matching label was found, so it is invalid */
	return CT_INVALID;
}