SimulationCraft
SimulationCraft is a tool to explore combat mechanics in the popular MMO RPG World of Warcraft (tm).
pet_spawner.hpp
1 // ==========================================================================
2 // Dedmonwakeen's Raid DPS/TPS Simulator.
3 // Send questions to [email protected]
4 // ==========================================================================
5 #ifndef PET_SPAWNER_HPP
6 #define PET_SPAWNER_HPP
7 
8 #include "config.hpp"
9 #include "spawner_base.hpp"
10 #include "player/player.hpp"
11 
12 namespace spawner
13 {
14 enum pet_spawn_type
15 {
16  PET_SPAWN_DYNAMIC,
17  PET_SPAWN_PERSISTENT
18 };
19 
21 enum pet_replacement_strategy
22 {
23  NO_REPLACE,
24  REPLACE_OLDEST,
25  REPLACE_RANDOM,
26 };
27 
55 template<typename T, typename O = player_t>
57 {
58 public:
59  using create_fn_t = std::function<T*(O*)>;
60  using apply_fn_t = std::function<void(T*)>;
61  using check_arg_fn_t = std::function<bool(const T*)>;
62  using check_fn_t = std::function<bool(void)>;
63  using expr_fn_t = std::function<std::unique_ptr<expr_t>(util::span<const util::string_view>)>;
64 
65 private:
66  // Creation phase determines what components of the init process are run on the created pet
67  enum create_phase
68  {
69  PHASE_COMBAT,
70  PHASE_MERGE,
71  PHASE_INIT
72  };
73 
75  unsigned m_max_pets;
77  create_fn_t m_creator;
79  check_fn_t m_check_create;
81  std::vector<T*> m_pets, m_active_pets, m_inactive_pets;
83  timespan_t m_duration;
85  pet_spawn_type m_type;
87  pet_replacement_strategy m_replacement_strategy;
88 
89  // Callbacks
90 
92  std::vector<apply_fn_t> m_event_create;
93 
94  // Expressions
95  std::unordered_map<std::string, expr_fn_t> m_expressions;
96 
97  // Iteration uptime calculation
98  timespan_t m_cumulative_uptime;
99  timespan_t m_spawn_time;
100 
101  // Bookkeeping optimization
102 
104  bool m_dirty;
106  size_t m_active;
108  T* m_initial_pet;
109 
110  // Internal helper methods
111 
113  T* create_pet( create_phase phase );
114 
116  void update_state();
117 
119  T* replacement_pet();
120 public:
121  pet_spawner_t( util::string_view id, O* p, pet_spawn_type st = PET_SPAWN_DYNAMIC );
122  pet_spawner_t( util::string_view id, O* p, unsigned max_pets,
123  pet_spawn_type st = PET_SPAWN_DYNAMIC );
124  pet_spawner_t( util::string_view id, O* p, unsigned max_pets, const create_fn_t& creator,
125  pet_spawn_type st = PET_SPAWN_DYNAMIC );
126  pet_spawner_t( util::string_view id, O* p, const create_fn_t& creator,
127  pet_spawn_type st = PET_SPAWN_DYNAMIC );
128 
131  std::vector<T*> spawn( timespan_t duration = timespan_t::min(), unsigned n = 0 );
133  std::vector<T*> spawn( unsigned n );
135  size_t despawn();
137  bool despawn( T* obj );
139  size_t despawn( const std::vector<T*>& obj );
141  size_t despawn( const check_arg_fn_t& fn );
142 
144  void extend_expiration( timespan_t adjustment );
145 
147  size_t n_active_pets() const;
149  size_t n_active_pets( const check_arg_fn_t& fn );
151  size_t n_inactive_pets() const;
153  size_t n_pets() const;
155  size_t max_pets() const;
157  timespan_t duration() const;
158 
159  // Iteration
160  typename std::vector<T*>::iterator begin();
161  typename std::vector<T*>::iterator end();
162  typename std::vector<T*>::const_iterator cbegin();
163  typename std::vector<T*>::const_iterator cend();
164 
166  pet_spawner_t<T, O>& set_max_pets( unsigned v );
168  pet_spawner_t<T, O>& set_creation_callback( const create_fn_t& fn );
170  pet_spawner_t<T, O>& set_creation_check_callback( const check_fn_t& fn );
172  pet_spawner_t<T, O>& set_creation_event_callback( const apply_fn_t& fn );
180  pet_spawner_t<T, O>& add_expression( const std::string& name, const expr_fn_t& fn );
181 
183  pet_spawner_t<T, O>& set_replacement_strategy( pet_replacement_strategy new_ );
184 
185  // Access
186 
188  std::vector<T*> pets();
190  std::vector<const T*> pets() const;
192  std::vector<T*> active_pets();
194  T* pet( size_t index = 0 ) const;
196  T* active_pet( size_t index = 0 );
198  T* active_pet( const check_arg_fn_t& fn );
200  T* active_pet_max_remains( const check_arg_fn_t& fn = check_arg_fn_t() );
202  T* active_pet_min_remains( const check_arg_fn_t& fn = check_arg_fn_t() );
203 
204  // Pure virtual implementations
205 
207  void merge( base_actor_spawner_t* other ) override;
208 
210  void create_persistent_actors() override;
211 
213  std::unique_ptr<expr_t> create_expression( util::span<const util::string_view> expr, util::string_view full_expression_str ) override;
214 
216  timespan_t iteration_uptime() const override;
217 
219  void reset() override;
220 
222  void datacollection_end() override;
223 };
224 } // Namespace spawner ends
225 
226 #include "pet_spawner_impl.hpp"
227 
228 #endif /* PET_SPAWNER_HPP */
Definition: spawner_base.hpp:29
T * active_pet_min_remains(const check_arg_fn_t &fn=check_arg_fn_t())
Acccess the active pet with lowest remaining time left using an optional unary constraint.
Definition: pet_spawner_impl.hpp:473
void reset() override
Reset internal state.
Definition: pet_spawner_impl.hpp:658
pet_spawner_t< T, O > & add_expression(const std::string &name, const expr_fn_t &fn)
Registers custom expressions for the pet type.
Definition: pet_spawner_impl.hpp:167
void merge(base_actor_spawner_t *other) override
Merge dynamic pet information.
Definition: pet_spawner_impl.hpp:530
pet_spawner_t< T, O > & set_max_pets(unsigned v)
Sets the maximum number of active pets.
Definition: pet_spawner_impl.hpp:139
void datacollection_end() override
Collect statistical data.
Definition: pet_spawner_impl.hpp:665
void create_persistent_actors() override
Creates persistent pet objects during pet creation.
Definition: pet_spawner_impl.hpp:578
size_t n_active_pets() const
Number of active pets of type T.
Definition: pet_spawner_impl.hpp:77
pet_spawner_t< T, O > & set_replacement_strategy(pet_replacement_strategy new_)
Sets the replacement strategy of spawned pets if maximum is reached.
Definition: pet_spawner_impl.hpp:171
Definition: spell_data.hpp:398
pet_spawner_t< T, O > & set_creation_callback(const create_fn_t &fn)
Sets the creation callback for the pet.
Definition: pet_spawner_impl.hpp:143
size_t n_inactive_pets() const
Number of inactive pets of type T.
Definition: pet_spawner_impl.hpp:97
T * active_pet(size_t index=0)
Access the zero-indexth active pet, nullptr if no pet of index active.
Definition: pet_spawner_impl.hpp:446
pet_spawner_t< T, O > & set_default_duration(const spell_data_t *spell)
Set default duration based on a spell.
Definition: pet_spawner_impl.hpp:155
std::vector< T * > pets()
All pets.
Definition: pet_spawner_impl.hpp:434
std::unique_ptr< expr_t > create_expression(util::span< const util::string_view > expr, util::string_view full_expression_str) override
Returns a pet-related expression.
Definition: pet_spawner_impl.hpp:598
T * active_pet_max_remains(const check_arg_fn_t &fn=check_arg_fn_t())
Acccess the active pet with highest remaining time left using an optional unary constraint.
Definition: pet_spawner_impl.hpp:496
void extend_expiration(timespan_t adjustment)
Adjust the expiration time of active pets of type T.
Definition: pet_spawner_impl.hpp:248
T * pet(size_t index=0) const
Access the zero-indexth created pet, nullptr if no pets are created.
Definition: pet_spawner_impl.hpp:519
timespan_t iteration_uptime() const override
Returns total uptime of the spawned pet type.
Definition: pet_spawner_impl.hpp:652
size_t max_pets() const
Maximum number of active pets.
Definition: pet_spawner_impl.hpp:105
Class for representing InGame time.
Definition: timespan.hpp:37
timespan_t duration() const
Default spawning duration.
Definition: pet_spawner_impl.hpp:109
pet_spawner_t< T, O > & set_creation_check_callback(const check_fn_t &fn)
Set creation check callback for persistent pets. Dynamic spawns will always be created.
Definition: pet_spawner_impl.hpp:147
Definition: actor.hpp:17
std::vector< T * > active_pets()
All currently active pets.
Definition: pet_spawner_impl.hpp:426
A wrapper object to enable dynamic pet spawns in simulationcraft.
Definition: pet_spawner.hpp:56
size_t n_pets() const
Total number of active and inactive pets of type T.
Definition: pet_spawner_impl.hpp:101
std::vector< T * > spawn(timespan_t duration=timespan_t::min(), unsigned n=0)
Spawn n new pets (defaults, 1 for dynamic, max_pets for persistent), return spawned pets as a vector ...
Definition: pet_spawner_impl.hpp:348
pet_spawner_t< T, O > & set_creation_event_callback(const apply_fn_t &fn)
Set (add) creation callback. Creation callbacks will be invoked for every new pet.
Definition: pet_spawner_impl.hpp:151
size_t despawn()
Despawn all active pets, return number of pets despawned.
Definition: pet_spawner_impl.hpp:177