src/cargotype.cpp
author bjarni
Mon, 28 May 2007 21:40:40 +0000
changeset 6735 dd792fed65e2
parent 6640 dac8f508fcaa
child 7931 b0a46cd92225
permissions -rw-r--r--
(svn r9967) -Fix (r9938): autoreplace would in certain conditions move dualheaded engines in a train (usually to the rear)
-Change: moving an engine in between the two ends of a dualheaded engine will now move the rear dualheaded engine to the front of the newly added engine (instead of moving the new engine to the rear of the rear dualheaded engine)
This can make a difference if there are wagons in the train
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
     1
/* $Id$ */
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
     2
6123
04eb770ec17e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6122
diff changeset
     3
/** @file cargotype.cpp */
04eb770ec17e (svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6122
diff changeset
     4
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
     5
#include "stdafx.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
     6
#include "openttd.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
     7
#include "macros.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
     8
#include "table/sprites.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
     9
#include "table/strings.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    10
#include "newgrf_cargo.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    11
#include "cargotype.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    12
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    13
#include "table/cargo_const.h"
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    14
6359
8527262c4cf8 (svn r9411) -Codechange: Add support for loading of newcargo data.
peter1138
parents: 6148
diff changeset
    15
CargoSpec _cargo[NUM_CARGO];
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    16
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    17
static const byte INVALID_CARGO = 0xFF;
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    18
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6143
diff changeset
    19
/* Bitmask of cargo types available */
6113
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6091
diff changeset
    20
uint32 _cargo_mask;
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6091
diff changeset
    21
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    22
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    23
void SetupCargoForClimate(LandscapeID l)
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    24
{
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    25
	assert(l < lengthof(_default_climate_cargo));
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    26
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    27
	/* Reset and disable all cargo types */
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    28
	memset(_cargo, 0, sizeof(_cargo));
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    29
	for (CargoID i = 0; i < lengthof(_cargo); i++) _cargo[i].bitnum = INVALID_CARGO;
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    30
6113
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6091
diff changeset
    31
	_cargo_mask = 0;
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6091
diff changeset
    32
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    33
	for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    34
		CargoLabel cl = _default_climate_cargo[l][i];
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    35
6640
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    36
		/* Bzzt: check if cl is just an index into the cargo table */
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    37
		if (cl < lengthof(_default_cargo)) {
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    38
			/* Copy the indexed cargo */
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    39
			_cargo[i] = _default_cargo[cl];
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    40
			SETBIT(_cargo_mask, i);
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    41
			continue;
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    42
		}
dac8f508fcaa (svn r9871) -Fix (r8826): Some cargo costs were wrong for different climates. Multiple definitions of the same cargo type are sometimes required.
peter1138
parents: 6460
diff changeset
    43
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    44
		/* Loop through each of the default cargo types to see if
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    45
		 * the label matches */
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    46
		for (uint j = 0; j < lengthof(_default_cargo); j++) {
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    47
			if (_default_cargo[j].label == cl) {
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    48
				_cargo[i] = _default_cargo[j];
6113
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6091
diff changeset
    49
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6143
diff changeset
    50
				/* Populate the available cargo mask */
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6143
diff changeset
    51
				SETBIT(_cargo_mask, i);
6091
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    52
				break;
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    53
			}
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    54
		}
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    55
	}
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    56
}
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    57
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    58
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    59
const CargoSpec *GetCargo(CargoID c)
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    60
{
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    61
	assert(c < lengthof(_cargo));
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    62
	return &_cargo[c];
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    63
}
c8827d9ae04a (svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
diff changeset
    64
6113
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6091
diff changeset
    65
6122
3359af5a1f54 (svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents: 6113
diff changeset
    66
bool CargoSpec::IsValid() const
3359af5a1f54 (svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents: 6113
diff changeset
    67
{
3359af5a1f54 (svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents: 6113
diff changeset
    68
	return bitnum != INVALID_CARGO;
3359af5a1f54 (svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents: 6113
diff changeset
    69
}
6143
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    70
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    71
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    72
CargoID GetCargoIDByLabel(CargoLabel cl)
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    73
{
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    74
	for (CargoID c = 0; c < lengthof(_cargo); c++) {
6360
2ed2ef7ae2ed (svn r9412) -Fix (r8886): When looking up a cargo type by label skip disabled cargos
peter1138
parents: 6359
diff changeset
    75
		if (_cargo[c].bitnum == INVALID_CARGO) continue;
6143
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    76
		if (_cargo[c].label == cl) return c;
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    77
	}
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    78
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    79
	/* No matching label was found, so it is invalid */
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    80
	return CT_INVALID;
dbca54e64e01 (svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents: 6123
diff changeset
    81
}
6460
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    82
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    83
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    84
/** Find the CargoID of a 'bitnum' value.
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    85
 * @param bitnum 'bitnum' to find.
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    86
 * @return First CargoID with the given bitnum, or CT_INVALID if not found.
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    87
 */
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    88
CargoID GetCargoIDByBitnum(uint8 bitnum)
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    89
{
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    90
	if (bitnum == INVALID_CARGO) return CT_INVALID;
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    91
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    92
	for (CargoID c = 0; c < lengthof(_cargo); c++) {
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    93
		if (_cargo[c].bitnum == bitnum) return c;
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    94
	}
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    95
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    96
	/* No matching label was found, so it is invalid */
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    97
	return CT_INVALID;
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    98
}
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6360
diff changeset
    99