Mercurial > hg > openttd
comparison src/subsidy_base.h @ 12297:39ea618c207e draft
(svn r16714) -Codechange: use pool-like accessors for Subsidy
author | smatz <smatz@openttd.org> |
---|---|
date | Wed, 01 Jul 2009 18:45:05 +0000 |
parents | |
children | e8f5aa4ce79b |
comparison
equal
deleted
inserted
replaced
12296:c7095580548d | 12297:39ea618c207e |
---|---|
1 /* $Id$ */ | |
2 | |
3 /** @file subsidy_base.h Subsidy base class. */ | |
4 | |
5 #ifndef SUBSIDY_BASE_H | |
6 #define SUBSIDY_BASE_H | |
7 | |
8 #include "cargo_type.h" | |
9 #include "company_type.h" | |
10 | |
11 /** Struct about subsidies, offered and awarded */ | |
12 struct Subsidy { | |
13 CargoID cargo_type; ///< Cargo type involved in this subsidy, CT_INVALID for invalid subsidy | |
14 byte age; ///< Subsidy age; < 12 is unawarded, >= 12 is awarded | |
15 uint16 from; ///< Index of source. Either TownID, IndustryID or StationID, when awarded | |
16 uint16 to; ///< Index of destination. Either TownID, IndustryID or StationID, when awarded | |
17 | |
18 /** | |
19 * Determines index of this subsidy | |
20 * @return index (in the Subsidy::array array) | |
21 */ | |
22 FORCEINLINE size_t Index() const | |
23 { | |
24 return this - Subsidy::array; | |
25 } | |
26 | |
27 /** | |
28 * Tests for validity of this subsidy | |
29 * @return is this subsidy valid? | |
30 */ | |
31 FORCEINLINE bool IsValid() const | |
32 { | |
33 return this->cargo_type != CT_INVALID; | |
34 } | |
35 | |
36 | |
37 static Subsidy array[MAX_COMPANIES]; ///< Array holding all subsidies | |
38 | |
39 /** | |
40 * Total number of subsidies, both valid and invalid | |
41 * @return length of Subsidy::array | |
42 */ | |
43 static FORCEINLINE size_t GetArraySize() | |
44 { | |
45 return lengthof(Subsidy::array); | |
46 } | |
47 | |
48 /** | |
49 * Tests whether given index is an index of valid subsidy | |
50 * @param index index to check | |
51 * @return can this index be used to access a valid subsidy? | |
52 */ | |
53 static FORCEINLINE bool IsValidID(size_t index) | |
54 { | |
55 return index < Subsidy::GetArraySize() && Subsidy::Get(index)->IsValid(); | |
56 } | |
57 | |
58 /** | |
59 * Returns pointer to subsidy with given index | |
60 * @param index index of subsidy | |
61 * @return pointer to subsidy with given index | |
62 */ | |
63 static FORCEINLINE Subsidy *Get(size_t index) | |
64 { | |
65 assert(index < Subsidy::GetArraySize()); | |
66 return &Subsidy::array[index]; | |
67 } | |
68 | |
69 static Subsidy *AllocateItem(); | |
70 static void Clean(); | |
71 }; | |
72 | |
73 #define FOR_ALL_SUBSIDIES_FROM(var, start) for (size_t subsidy_index = start; var = NULL, subsidy_index < Subsidy::GetArraySize(); subsidy_index++) \ | |
74 if ((var = Subsidy::Get(subsidy_index))->IsValid()) | |
75 #define FOR_ALL_SUBSIDIES(var) FOR_ALL_SUBSIDIES_FROM(var, 0) | |
76 | |
77 #endif /* SUBSIDY_BASE_H */ |