author | truebrain |
Thu, 12 Jun 2008 21:07:25 +0000 | |
branch | noai |
changeset 10943 | 5f5a5dd407d8 |
parent 10455 | 22c441f5adf9 |
permissions | -rw-r--r-- |
6417
26acff62d001
(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$ */ |
26acff62d001
(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 |
|
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
9724
diff
changeset
|
3 |
/** @file cargotype.cpp Implementation of cargos. */ |
6449
e520244dc71e
(svn r8859) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents:
6448
diff
changeset
|
4 |
|
6417
26acff62d001
(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" |
26acff62d001
(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" |
26acff62d001
(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 "newgrf_cargo.h" |
26acff62d001
(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 "cargotype.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
9 |
#include "core/bitmath_func.hpp" |
6417
26acff62d001
(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 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
11 |
#include "table/sprites.h" |
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
12 |
#include "table/strings.h" |
6417
26acff62d001
(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" |
26acff62d001
(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 |
|
9517 | 15 |
CargoSpec _cargo[NUM_CARGO]; |
6417
26acff62d001
(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 |
|
26acff62d001
(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; |
26acff62d001
(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 |
|
6474
24e7ceeb25f5
(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:
6469
diff
changeset
|
19 |
/* Bitmask of cargo types available */ |
6439
99a5807852b1
(svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents:
6417
diff
changeset
|
20 |
uint32 _cargo_mask; |
99a5807852b1
(svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents:
6417
diff
changeset
|
21 |
|
6417
26acff62d001
(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 |
|
26acff62d001
(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) |
26acff62d001
(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 |
{ |
26acff62d001
(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)); |
26acff62d001
(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 |
|
26acff62d001
(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 */ |
26acff62d001
(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)); |
26acff62d001
(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; |
26acff62d001
(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 |
|
6439
99a5807852b1
(svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents:
6417
diff
changeset
|
31 |
_cargo_mask = 0; |
99a5807852b1
(svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents:
6417
diff
changeset
|
32 |
|
6417
26acff62d001
(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++) { |
26acff62d001
(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]; |
26acff62d001
(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 |
|
9624 | 36 |
/* Bzzt: check if cl is just an index into the cargo table */ |
37 |
if (cl < lengthof(_default_cargo)) { |
|
38 |
/* Copy the indexed cargo */ |
|
39 |
_cargo[i] = _default_cargo[cl]; |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
40 |
if (_cargo[i].bitnum != INVALID_CARGO) SetBit(_cargo_mask, i); |
9624 | 41 |
continue; |
42 |
} |
|
43 |
||
6417
26acff62d001
(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 |
26acff62d001
(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 */ |
26acff62d001
(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++) { |
26acff62d001
(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) { |
26acff62d001
(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]; |
6439
99a5807852b1
(svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents:
6417
diff
changeset
|
49 |
|
6474
24e7ceeb25f5
(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:
6469
diff
changeset
|
50 |
/* Populate the available cargo mask */ |
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9624
diff
changeset
|
51 |
SetBit(_cargo_mask, i); |
6417
26acff62d001
(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; |
26acff62d001
(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 |
} |
26acff62d001
(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 |
} |
26acff62d001
(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 |
} |
26acff62d001
(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 |
} |
26acff62d001
(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 |
|
26acff62d001
(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 |
|
26acff62d001
(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) |
26acff62d001
(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 |
{ |
26acff62d001
(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)); |
26acff62d001
(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]; |
26acff62d001
(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 |
} |
26acff62d001
(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 |
|
6439
99a5807852b1
(svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents:
6417
diff
changeset
|
65 |
|
6448
a749c78812a7
(svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents:
6439
diff
changeset
|
66 |
bool CargoSpec::IsValid() const |
a749c78812a7
(svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents:
6439
diff
changeset
|
67 |
{ |
a749c78812a7
(svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents:
6439
diff
changeset
|
68 |
return bitnum != INVALID_CARGO; |
a749c78812a7
(svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
peter1138
parents:
6439
diff
changeset
|
69 |
} |
6469
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
70 |
|
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
71 |
|
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
72 |
CargoID GetCargoIDByLabel(CargoLabel cl) |
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
73 |
{ |
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
74 |
for (CargoID c = 0; c < lengthof(_cargo); c++) { |
9517 | 75 |
if (_cargo[c].bitnum == INVALID_CARGO) continue; |
6469
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
76 |
if (_cargo[c].label == cl) return c; |
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
77 |
} |
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
78 |
|
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
79 |
/* No matching label was found, so it is invalid */ |
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
80 |
return CT_INVALID; |
05a2e97144ec
(svn r8886) -Codechange: (NewGRF) Add (partial) cargo translation table support, applied to action 3s only.
peter1138
parents:
6449
diff
changeset
|
81 |
} |
9599 | 82 |
|
83 |
||
84 |
/** Find the CargoID of a 'bitnum' value. |
|
85 |
* @param bitnum 'bitnum' to find. |
|
86 |
* @return First CargoID with the given bitnum, or CT_INVALID if not found. |
|
87 |
*/ |
|
88 |
CargoID GetCargoIDByBitnum(uint8 bitnum) |
|
89 |
{ |
|
90 |
if (bitnum == INVALID_CARGO) return CT_INVALID; |
|
91 |
||
92 |
for (CargoID c = 0; c < lengthof(_cargo); c++) { |
|
93 |
if (_cargo[c].bitnum == bitnum) return c; |
|
94 |
} |
|
95 |
||
96 |
/* No matching label was found, so it is invalid */ |
|
97 |
return CT_INVALID; |
|
98 |
} |
|
99 |