The Battle for Wesnoth  1.17.0-dev
soundsource.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2021
3  by Karol Nowak <grzywacz@sul.uni.lodz.pl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <map>
19 
20 #include "generic_event.hpp"
21 #include "map/location.hpp"
22 
23 class config;
24 class display;
25 
26 namespace soundsource {
27 
28 class sourcespec;
29 class manager;
30 
31 /*
32  * Sound source is an object on a map (a location) which has one or more
33  * sounds effects associated with it, which are played randomly and with
34  * appropriate delays, when sound emitting object is visible on screen.
35  */
37  unsigned int last_played_;
39  int chance_;
40  int loops_;
41  const unsigned int id_;
42  int range_;
46  std::string files_;
47  std::vector<map_location> locations_;
48 
49  // Last assigned id; this can, of course, overflow, but I'd
50  // never expect to see 4 billions sound sources being created...
51  static unsigned int last_id;
52 
53 public:
54  // min_delay is a minimum time in seconds, which must pass before
55  // this sound source can be played again if it remains visible
56  //
57  // chance is a chance ;-) (in %) that the sound source will emit
58  // sound every second after the delay has passed or once the source
59  // becomes visible
60  positional_source(const sourcespec &spec);
62 
63  bool is_global() const;
64 
65  void update(unsigned int time, const display &disp);
66  void update_positions(unsigned int time, const display &disp);
67 
68  int calculate_volume(const map_location &loc, const display &disp);
69 
70  /**
71  * Serializes attributes as WML config.
72  * @param cfg A reference to a [sound_source] tag object.
73  */
74  void write_config(config& cfg) const;
75 };
76 
77 class manager : public events::observer
78 {
79 
80  typedef std::map<std::string, std::unique_ptr<positional_source>> positional_source_map;
82  typedef positional_source_map::const_iterator positional_source_const_iterator;
83 
84  positional_source_map sources_;
85  const display &disp_;
86 
87 public:
88  manager(const display &disp);
89  ~manager();
90 
91  // event interface
92  void handle_generic_event(const std::string &event_name);
93 
94  // add or replace a soundsource
95  void add(const sourcespec &source);
96  void remove(const std::string &id);
97  sourcespec get(const std::string &id);
98  bool contains(const std::string& id);
99  void update();
100 
101  // checks which sound sources are visible
102  void update_positions();
103 
104  /**
105  * Serializes information into cfg as new children of key
106  * "sound_source", appended to existing content.
107  */
108  void write_sourcespecs(config& cfg) const;
109 };
110 
111 /**
112  * Sound source info class.
113  * Encapsulates sound source parameters, so that they're easier to pass
114  * around/extend/read.
115  */
117 {
118  const std::string id_;
119  std::string files_;
120 
122  int chance_;
123 
124  int loops_;
125  int range_;
129 
130  std::vector<map_location> locations_;
131 
132 public:
133  /** Parameter-list constructor. */
134  sourcespec(const std::string& id, const std::string& files, int min_delay, int chance) :
135  id_(id),
136  files_(files),
137  min_delay_(min_delay),
138  chance_(chance),
139  loops_(0),
140  range_(3),
141  faderange_(14),
142  check_fogged_(false),
143  check_shrouded_(false),
144  locations_()
145  {}
146 
147  /** WML constructor. */
148  sourcespec(const config& cfg);
149 
150  /**
151  * Serializes information into cfg as a new (appended)
152  * child of key "sound_source".
153  */
154  void write(config& cfg) const;
155 
156  int loops() const { return loops_; }
157 
158  void set_loops(int value) {
159  loops_ = value;
160  }
161 
162  bool check_fogged() const { return check_fogged_; }
163  bool check_shrouded() const { return check_shrouded_; }
164 
165  void set_check_fogged(bool value) {
166  check_fogged_ = value;
167  }
168 
169  void set_check_shrouded(bool value) {
170  check_shrouded_ = value;
171  }
172 
173  const std::vector<map_location>& get_locations() const {
174  return locations_;
175  }
176 
177  void set_locations(const std::vector<map_location>& locs) {
178  locations_ = locs;
179  }
180 
181  int full_range() const { return range_; }
182 
183  void set_full_range(int value) {
184  range_ = value;
185  }
186 
187  int fade_range() const { return faderange_; }
188 
189  void set_fade_range(int value) {
190  faderange_ = value;
191  }
192 
193  int minimum_delay() const { return min_delay_; }
194 
195  void set_minimum_delay(int value) {
196  min_delay_ = value;
197  }
198 
199  int chance() const { return chance_; }
200 
201  void set_chance(int value) {
202  chance_ = value;
203  }
204 
205  const std::string& id() const { return id_; }
206 
207  const std::string& files() const { return files_; }
208 
209  void set_files(const std::string& f) {
210  files_ = f;
211  }
212 };
213 
214 } // namespace soundsource
int calculate_volume(const map_location &loc, const display &disp)
bool contains(const pane::item &item, const std::string &tag, const text_box &text_box)
A filter testing whether a search string is used in a text.
Definition: filter.hpp:64
void set_fade_range(int value)
void set_minimum_delay(int value)
const std::vector< map_location > & get_locations() const
const std::string & id() const
positional_source_map::iterator positional_source_iterator
Definition: soundsource.hpp:81
void update(unsigned int time, const display &disp)
std::vector< map_location > locations_
void write(std::ostream &out, const configr_of &cfg, unsigned int level)
Definition: parser.cpp:764
positional_source_map sources_
Definition: soundsource.hpp:84
void write_config(config &cfg) const
Serializes attributes as WML config.
void set_full_range(int value)
bool check_shrouded() const
const std::string & files() const
void set_loops(int value)
void update_positions(unsigned int time, const display &disp)
std::map< std::string, std::unique_ptr< positional_source > > positional_source_map
Definition: soundsource.hpp:80
Encapsulates the map of the game.
Definition: location.hpp:38
void set_check_shrouded(bool value)
std::vector< map_location > locations_
Definition: soundsource.hpp:47
Sound source info class.
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
positional_source(const sourcespec &spec)
sourcespec(const std::string &id, const std::string &files, int min_delay, int chance)
Parameter-list constructor.
void set_files(const std::string &f)
void set_chance(int value)
#define f
const display & disp_
Definition: soundsource.hpp:85
const std::string id_
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
void set_check_fogged(bool value)
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
positional_source_map::const_iterator positional_source_const_iterator
Definition: soundsource.hpp:82
static unsigned int last_id
Definition: soundsource.hpp:51
void set_locations(const std::vector< map_location > &locs)