The Battle for Wesnoth  1.19.0-dev
soundsource.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2024
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 
30 /*
31  * Sound source is an object on a map (a location) which has one or more
32  * sounds effects associated with it, which are played randomly and with
33  * appropriate delays, when sound emitting object is visible on screen.
34  */
36  unsigned int last_played_;
38  int chance_;
39  int loops_;
40  const unsigned int id_;
41  int range_;
45  std::string files_;
46  std::vector<map_location> locations_;
47 
48  // Last assigned id; this can, of course, overflow, but I'd
49  // never expect to see 4 billions sound sources being created...
50  static unsigned int last_id;
51 
52 public:
53  // min_delay is a minimum time in seconds, which must pass before
54  // this sound source can be played again if it remains visible
55  //
56  // chance is a chance ;-) (in %) that the sound source will emit
57  // sound every second after the delay has passed or once the source
58  // becomes visible
59  positional_source(const sourcespec &spec);
61 
62  bool is_global() const;
63 
64  void update(unsigned int time, const display &disp);
65  void update_positions(unsigned int time, const display &disp);
66 
67  int calculate_volume(const map_location &loc, const display &disp);
68 
69  /**
70  * Serializes attributes as WML config.
71  * @param cfg A reference to a [sound_source] tag object.
72  */
73  void write_config(config& cfg) const;
74 };
75 
76 class manager : public events::observer
77 {
78 
79  typedef std::map<std::string, std::unique_ptr<positional_source>> positional_source_map;
81  typedef positional_source_map::const_iterator positional_source_const_iterator;
82 
84  const display &disp_;
85 
86 public:
87  manager(const display &disp);
88  ~manager();
89 
90  // event interface
91  void handle_generic_event(const std::string &event_name);
92 
93  // add or replace a soundsource
94  void add(const sourcespec &source);
95  void remove(const std::string &id);
96  sourcespec get(const std::string &id);
97  bool contains(const std::string& id);
98  void update();
99 
100  // checks which sound sources are visible
101  void update_positions();
102 
103  /**
104  * Serializes information into cfg as new children of key
105  * "sound_source", appended to existing content.
106  */
107  void write_sourcespecs(config& cfg) const;
108 };
109 
110 /**
111  * Sound source info class.
112  * Encapsulates sound source parameters, so that they're easier to pass
113  * around/extend/read.
114  */
116 {
117  const std::string id_;
118  std::string files_;
119 
121  int chance_;
122 
123  int loops_;
124  int range_;
128 
129  std::vector<map_location> locations_;
130 
131 public:
132  /** Parameter-list constructor. */
133  sourcespec(const std::string& id, const std::string& files, int min_delay, int chance) :
134  id_(id),
135  files_(files),
136  min_delay_(min_delay),
137  chance_(chance),
138  loops_(0),
139  range_(3),
140  faderange_(14),
141  check_fogged_(false),
142  check_shrouded_(false),
143  locations_()
144  {}
145 
146  /** WML constructor. */
147  sourcespec(const config& cfg);
148 
149  /**
150  * Serializes information into cfg as a new (appended)
151  * child of key "sound_source".
152  */
153  void write(config& cfg) const;
154 
155  int loops() const { return loops_; }
156 
157  void set_loops(int value) {
158  loops_ = value;
159  }
160 
161  bool check_fogged() const { return check_fogged_; }
162  bool check_shrouded() const { return check_shrouded_; }
163 
164  void set_check_fogged(bool value) {
165  check_fogged_ = value;
166  }
167 
168  void set_check_shrouded(bool value) {
169  check_shrouded_ = value;
170  }
171 
172  const std::vector<map_location>& get_locations() const {
173  return locations_;
174  }
175 
176  void set_locations(const std::vector<map_location>& locs) {
177  locations_ = locs;
178  }
179 
180  int full_range() const { return range_; }
181 
182  void set_full_range(int value) {
183  range_ = value;
184  }
185 
186  int fade_range() const { return faderange_; }
187 
188  void set_fade_range(int value) {
189  faderange_ = value;
190  }
191 
192  int minimum_delay() const { return min_delay_; }
193 
194  void set_minimum_delay(int value) {
195  min_delay_ = value;
196  }
197 
198  int chance() const { return chance_; }
199 
200  void set_chance(int value) {
201  chance_ = value;
202  }
203 
204  const std::string& id() const { return id_; }
205 
206  const std::string& files() const { return files_; }
207 
208  void set_files(const std::string& f) {
209  files_ = f;
210  }
211 };
212 
213 } // namespace soundsource
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:81
void handle_generic_event(const std::string &event_name)
Definition: soundsource.cpp:44
sourcespec get(const std::string &id)
Definition: soundsource.cpp:55
positional_source_map sources_
Definition: soundsource.hpp:83
const display & disp_
Definition: soundsource.hpp:84
positional_source_map::iterator positional_source_iterator
Definition: soundsource.hpp:80
manager(const display &disp)
Definition: soundsource.cpp:30
void write_sourcespecs(config &cfg) const
Serializes information into cfg as new children of key "sound_source", appended to existing content.
Definition: soundsource.cpp:99
void add(const sourcespec &source)
Definition: soundsource.cpp:50
positional_source_map::const_iterator positional_source_const_iterator
Definition: soundsource.hpp:81
bool contains(const std::string &id)
Definition: soundsource.cpp:76
void remove(const std::string &id)
Definition: soundsource.cpp:65
std::map< std::string, std::unique_ptr< positional_source > > positional_source_map
Definition: soundsource.hpp:79
void write_config(config &cfg) const
Serializes attributes as WML config.
std::vector< map_location > locations_
Definition: soundsource.hpp:46
int calculate_volume(const map_location &loc, const display &disp)
void update(unsigned int time, const display &disp)
void update_positions(unsigned int time, const display &disp)
positional_source(const sourcespec &spec)
static unsigned int last_id
Definition: soundsource.hpp:50
Sound source info class.
void set_check_fogged(bool value)
sourcespec(const std::string &id, const std::string &files, int min_delay, int chance)
Parameter-list constructor.
const std::vector< map_location > & get_locations() const
void write(config &cfg) const
Serializes information into cfg as a new (appended) child of key "sound_source".
void set_check_shrouded(bool value)
const std::string id_
void set_locations(const std::vector< map_location > &locs)
void set_loops(int value)
const std::string & files() const
std::vector< map_location > locations_
void set_minimum_delay(int value)
void set_files(const std::string &f)
void set_chance(int value)
void set_fade_range(int value)
bool check_shrouded() const
const std::string & id() const
void set_full_range(int value)
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
Encapsulates the map of the game.
Definition: location.hpp:38
#define f