summaryrefslogtreecommitdiff
path: root/src/subsidy_base.h
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-07-01 18:45:05 +0000
committersmatz <smatz@openttd.org>2009-07-01 18:45:05 +0000
commit76a50ce94dc7c78d76c309d8a639591fed2a2da8 (patch)
tree342be7030b9760781f886bca89cbe01863985008 /src/subsidy_base.h
parentaaeb53066dff98cac2d66d1f02f545d14c9f650c (diff)
downloadopenttd-76a50ce94dc7c78d76c309d8a639591fed2a2da8.tar.xz
(svn r16714) -Codechange: use pool-like accessors for Subsidy
Diffstat (limited to 'src/subsidy_base.h')
-rw-r--r--src/subsidy_base.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/subsidy_base.h b/src/subsidy_base.h
new file mode 100644
index 000000000..d172adfa5
--- /dev/null
+++ b/src/subsidy_base.h
@@ -0,0 +1,77 @@
+/* $Id$ */
+
+/** @file subsidy_base.h Subsidy base class. */
+
+#ifndef SUBSIDY_BASE_H
+#define SUBSIDY_BASE_H
+
+#include "cargo_type.h"
+#include "company_type.h"
+
+/** Struct about subsidies, offered and awarded */
+struct Subsidy {
+ CargoID cargo_type; ///< Cargo type involved in this subsidy, CT_INVALID for invalid subsidy
+ byte age; ///< Subsidy age; < 12 is unawarded, >= 12 is awarded
+ uint16 from; ///< Index of source. Either TownID, IndustryID or StationID, when awarded
+ uint16 to; ///< Index of destination. Either TownID, IndustryID or StationID, when awarded
+
+ /**
+ * Determines index of this subsidy
+ * @return index (in the Subsidy::array array)
+ */
+ FORCEINLINE size_t 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();
+};
+
+#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(var) FOR_ALL_SUBSIDIES_FROM(var, 0)
+
+#endif /* SUBSIDY_BASE_H */