1 #ifndef _ResourcePool_h_
2 #define _ResourcePool_h_
3
4 #include "../universe/EnumsFwd.h"
5 #include "../util/Export.h"
6
7 #include <boost/signals2/signal.hpp>
8 #include <boost/serialization/nvp.hpp>
9 #include <boost/serialization/version.hpp>
10
11 #include <vector>
12 #include <set>
13
14 /** The ResourcePool class keeps track of an empire's stockpile and production
15 * of a particular resource (eg. research, industry). */
16 class FO_COMMON_API ResourcePool {
17 public:
18 /** \name Structors */ //@{
19 ResourcePool(ResourceType type);
20 //@}
21
22 /** \name Accessors */ //@{
23 const std::vector<int>& ObjectIDs() const; ///< returns UniverseObject IDs in this ResourcePool
24 float Stockpile() const; ///< returns current stockpiled amount of resource
25
26 float TotalOutput() const; ///< returns amount of resource being generated by all ResourceCenters
27 std::map<std::set<int>, float> Output() const; ///< returns the sets of groups of objects that can share resources, and the amount of this pool's resource that each group is generating this turn
28 float GroupOutput(int object_id) const; ///< returns amount of resource being generated by resource sharing group that contains the object with id \a object_id
29
30 float TargetOutput() const;
31 float GroupTargetOutput(int object_id) const;
32
33 float TotalAvailable() const; ///< returns amount of resource immediately available = output + stockpile from all ResourceCenters, ignoring limitations of connections between centers
34 /** Returns the sets of groups of objects that can share resources, and the
35 * amount of this pool's resource that each group has available */
36 std::map<std::set<int>, float> Available() const;
37 float GroupAvailable(int object_id) const; ///< returns amount of resource available in resource sharing group that contains the object with id \a object_id
38
39 std::string Dump() const;
40 //@}
41
42 /** \name Mutators */ //@{
43 /** emitted after updating production, or called externally to indicate
44 * that stockpile and change need to be refreshed. */
45 mutable boost::signals2::signal<void ()> ChangedSignal;
46
47 void SetObjects(const std::vector<int>& object_ids);
48 /** specifies which sets systems can share resources. any two sets should
49 * have no common systems. */
50 void SetConnectedSupplyGroups(const std::set<std::set<int>>& connected_system_groups);
51
52 void SetStockpile(float d); ///< sets current sockpiled amount of resource
53
54 void Update(); ///< recalculates total resource production
55 //@}
56
57 private:
58 ResourcePool(); ///< default ctor needed for serialization
59
60 std::vector<int> m_object_ids; ///< IDs of objects to consider in this pool
61 std::set<std::set<int>> m_connected_system_groups; ///< sets of systems between and in which objects can share this pool's resource
62 std::map<std::set<int>, float> m_connected_object_groups_resource_output; ///< cached map from connected group of objects that can share resources, to how much resource is output by ResourceCenters in the group. regenerated during update from other state information.
63 std::map<std::set<int>, float> m_connected_object_groups_resource_target_output; ///< cached map from connected group of objects that can share resources, to how much resource would, if all meters equaled their target meters, be output by ResourceCenters in the group. regenerated during update from other state information.
64 float m_stockpile = 0.0f; ///< current stockpiled amount of resource
65 ResourceType m_type; ///< what kind of resource does this pool hold?
66
67 friend class boost::serialization::access;
68 template <typename Archive>
69 void serialize(Archive& ar, const unsigned int version);
70 };
71
72
73 BOOST_CLASS_VERSION(ResourcePool, 1)
74
75 // template implementations
76 template <typename Archive>
serialize(Archive & ar,const unsigned int version)77 void ResourcePool::serialize(Archive& ar, const unsigned int version)
78 {
79 ar & BOOST_SERIALIZATION_NVP(m_type)
80 & BOOST_SERIALIZATION_NVP(m_object_ids)
81 & BOOST_SERIALIZATION_NVP(m_stockpile);
82 if (version < 1) {
83 int dummy = -1;
84 ar & boost::serialization::make_nvp("m_stockpile_object_id", dummy);
85 }
86 ar & BOOST_SERIALIZATION_NVP(m_connected_system_groups);
87 }
88
89 #endif // _ResourcePool_h_
90