summaryrefslogtreecommitdiff
path: root/src/subsidy_base.h
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-08-08 20:53:36 +0000
committersmatz <smatz@openttd.org>2009-08-08 20:53:36 +0000
commit13e23141b418214db51e1463bb3696a4e6d87129 (patch)
tree3b092f293fd5fdc1c1dd17120ba874d919824bf0 /src/subsidy_base.h
parent8d809d5f4c21afbed4bbeb0388d42a9c5718a755 (diff)
downloadopenttd-13e23141b418214db51e1463bb3696a4e6d87129.tar.xz
(svn r17124) -Codechange: store subsidies in a pool (instead of an array)
Diffstat (limited to 'src/subsidy_base.h')
-rw-r--r--src/subsidy_base.h68
1 files changed, 11 insertions, 57 deletions
diff --git a/src/subsidy_base.h b/src/subsidy_base.h
index 4367334cb..015c773cc 100644
--- a/src/subsidy_base.h
+++ b/src/subsidy_base.h
@@ -8,9 +8,13 @@
#include "cargo_type.h"
#include "company_type.h"
#include "subsidy_type.h"
+#include "core/pool_type.hpp"
+
+typedef Pool<Subsidy, SubsidyID, 1, MAX_COMPANIES> SubsidyPool;
+extern SubsidyPool _subsidy_pool;
/** Struct about subsidies, offered and awarded */
-struct Subsidy {
+struct Subsidy : SubsidyPool::PoolItem<&_subsidy_pool> {
CargoID cargo_type; ///< Cargo type involved in this subsidy, CT_INVALID for invalid subsidy
byte remaining; ///< Remaining months when this subsidy is valid
CompanyByte awarded; ///< Subsidy is awarded to this company; INVALID_COMPANY if it's not awarded to anyone
@@ -20,6 +24,11 @@ struct Subsidy {
SourceID dst; ///< Index of destination. Either TownID or IndustryID
/**
+ * We need an (empty) constructor so struct isn't zeroed (as C++ standard states)
+ */
+ FORCEINLINE Subsidy() { }
+
+ /**
* Tests whether this subsidy has been awarded to someone
* @return is this subsidy awarded?
*/
@@ -29,60 +38,6 @@ struct Subsidy {
}
void AwardTo(CompanyID company);
-
- /**
- * Determines index of this subsidy
- * @return index (in the Subsidy::array array)
- */
- FORCEINLINE SubsidyID Index() const
- {
- return this - Subsidy::array;
- }
-
- /**
- * Tests for validity of this subsidy
- * @return is this subsidy valid?
- */
- FORCEINLINE bool IsValid() const
- {
- return this->cargo_type != CT_INVALID;
- }
-
-
- static Subsidy array[MAX_COMPANIES]; ///< Array holding all subsidies
-
- /**
- * Total number of subsidies, both valid and invalid
- * @return length of Subsidy::array
- */
- static FORCEINLINE size_t GetArraySize()
- {
- return lengthof(Subsidy::array);
- }
-
- /**
- * Tests whether given index is an index of valid subsidy
- * @param index index to check
- * @return can this index be used to access a valid subsidy?
- */
- static FORCEINLINE bool IsValidID(size_t index)
- {
- return index < Subsidy::GetArraySize() && Subsidy::Get(index)->IsValid();
- }
-
- /**
- * Returns pointer to subsidy with given index
- * @param index index of subsidy
- * @return pointer to subsidy with given index
- */
- static FORCEINLINE Subsidy *Get(size_t index)
- {
- assert(index < Subsidy::GetArraySize());
- return &Subsidy::array[index];
- }
-
- static Subsidy *AllocateItem();
- static void Clean();
};
/** Constants related to subsidies */
@@ -95,8 +50,7 @@ enum {
SUBSIDY_MAX_DISTANCE = 70, ///< Max. length of subsidised route (DistanceManhattan)
};
-#define FOR_ALL_SUBSIDIES_FROM(var, start) for (size_t subsidy_index = start; var = NULL, subsidy_index < Subsidy::GetArraySize(); subsidy_index++) \
- if ((var = Subsidy::Get(subsidy_index))->IsValid())
+#define FOR_ALL_SUBSIDIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Subsidy, subsidy_index, var, start)
#define FOR_ALL_SUBSIDIES(var) FOR_ALL_SUBSIDIES_FROM(var, 0)
#endif /* SUBSIDY_BASE_H */