@@ -5,6 +5,10 @@ using namespace RTE;
5
5
ConcreteClassInfo (DynamicSongSection, Entity, 50 );
6
6
ConcreteClassInfo (DynamicSong, Entity, 50 );
7
7
8
+ const std::unordered_map<std::string, DynamicSongSection::SoundContainerSelectionCycleMode> DynamicSongSection::c_SoundContainerSelectionCycleModeMap = {
9
+ {" randomnorepeat" , SoundContainerSelectionCycleMode::RANDOMNOREPEAT},
10
+ {" shuffle" , SoundContainerSelectionCycleMode::SHUFFLE}};
11
+
8
12
DynamicSongSection::DynamicSongSection () {
9
13
Clear ();
10
14
}
@@ -21,8 +25,13 @@ DynamicSongSection::~DynamicSongSection() {
21
25
void DynamicSongSection::Clear () {
22
26
m_TransitionSoundContainers.clear ();
23
27
m_LastTransitionSoundContainerIndex = -1 ;
28
+ m_TransitionShuffleIndices.clear ();
29
+
24
30
m_SoundContainers.clear ();
25
31
m_LastSoundContainerIndex = -1 ;
32
+ m_ShuffleIndices.clear ();
33
+
34
+ m_SoundContainerSelectionCycleMode = RANDOMNOREPEAT;
26
35
m_SectionType = " Default" ;
27
36
}
28
37
@@ -34,16 +43,16 @@ int DynamicSongSection::Create(const DynamicSongSection& reference) {
34
43
soundContainer.Create (referenceSoundContainer);
35
44
m_TransitionSoundContainers.push_back (soundContainer);
36
45
}
37
-
38
46
m_LastTransitionSoundContainerIndex = reference.m_LastTransitionSoundContainerIndex ;
39
-
47
+
40
48
for (const SoundContainer& referenceSoundContainer: reference.m_SoundContainers ) {
41
49
SoundContainer soundContainer;
42
50
soundContainer.Create (referenceSoundContainer);
43
51
m_SoundContainers.push_back (soundContainer);
44
52
}
45
-
46
53
m_LastSoundContainerIndex = reference.m_LastSoundContainerIndex ;
54
+
55
+ m_SoundContainerSelectionCycleMode = reference.m_SoundContainerSelectionCycleMode ;
47
56
m_SectionType = reference.m_SectionType ;
48
57
49
58
return 0 ;
@@ -62,11 +71,32 @@ int DynamicSongSection::ReadProperty(const std::string_view& propName, Reader& r
62
71
reader >> soundContainerToAdd;
63
72
m_SoundContainers.push_back (soundContainerToAdd);
64
73
});
74
+ MatchProperty (" SoundContainerSelectionCycleMode" , {
75
+ std::string soundContainerSelectionCycleModeString = reader.ReadPropValue ();
76
+ if (c_SoundContainerSelectionCycleModeMap.find (soundContainerSelectionCycleModeString) != c_SoundContainerSelectionCycleModeMap.end ()) {
77
+ m_SoundContainerSelectionCycleMode = c_SoundContainerSelectionCycleModeMap.find (soundContainerSelectionCycleModeString)->second ;
78
+ } else {
79
+ try {
80
+ m_SoundContainerSelectionCycleMode = static_cast <SoundContainerSelectionCycleMode>(std::stoi (soundContainerSelectionCycleModeString));
81
+ } catch (const std::exception&) {
82
+ reader.ReportError (" Tried to set non-existent SoundContainerSelectionCycleMode " + soundContainerSelectionCycleModeString);
83
+ }
84
+ }
85
+ });
65
86
MatchProperty (" SectionType" , { reader >> m_SectionType; });
66
87
67
88
EndPropertyList;
68
89
}
69
90
91
+ void DynamicSongSection::SaveSoundContainerSelectionCycleMode (Writer& writer, SoundContainerSelectionCycleMode soundContainerSelectionCycleMode) {
92
+ auto cycleModeMapEntry = std::find_if (c_SoundContainerSelectionCycleModeMap.begin (), c_SoundContainerSelectionCycleModeMap.end (), [&soundContainerSelectionCycleMode = soundContainerSelectionCycleMode](auto element) { return element.second == soundContainerSelectionCycleMode; });
93
+ if (cycleModeMapEntry != c_SoundContainerSelectionCycleModeMap.end ()) {
94
+ writer << cycleModeMapEntry->first ;
95
+ } else {
96
+ RTEAbort (" Tried to write invalid SoundContainerSelectionCycleMode when saving DynamicSongSection." );
97
+ }
98
+ }
99
+
70
100
int DynamicSongSection::Save (Writer& writer) const {
71
101
Entity::Save (writer);
72
102
@@ -86,6 +116,8 @@ int DynamicSongSection::Save(Writer& writer) const {
86
116
}
87
117
writer.NewProperty (" LastSoundContainerIndex" );
88
118
writer << m_LastSoundContainerIndex;
119
+ writer.NewProperty (" SoundContainerSelectionCycleMode" );
120
+ SaveSoundContainerSelectionCycleMode (writer, m_SoundContainerSelectionCycleMode);
89
121
writer.NewProperty (" SectionType" );
90
122
writer << m_SectionType;
91
123
@@ -100,16 +132,31 @@ SoundContainer& DynamicSongSection::SelectTransitionSoundContainer() {
100
132
return m_TransitionSoundContainers[0 ];
101
133
}
102
134
103
- std::vector<unsigned int > validIndices;
104
- for (unsigned int i = 0 ; i < m_TransitionSoundContainers.size (); i++) {
105
- if (i != m_LastTransitionSoundContainerIndex) {
106
- validIndices.push_back (i);
135
+ if (m_TransitionShuffleIndices.empty ()) {
136
+ for (unsigned int i = 0 ; i <= m_TransitionSoundContainers.size () - 1 ; i++ ) {
137
+ m_TransitionShuffleIndices.push_back (i);
107
138
}
108
139
}
109
140
110
- unsigned int randomIndex = validIndices[RandomNum (0 , static_cast <int >(validIndices.size ()) - 1 )];
111
- m_LastTransitionSoundContainerIndex = randomIndex;
112
- return m_TransitionSoundContainers[randomIndex];
141
+ switch (m_SoundContainerSelectionCycleMode) {
142
+ case RANDOMNOREPEAT: {
143
+ std::vector<unsigned int > validIndices;
144
+ for (unsigned int i = 0 ; i < m_TransitionSoundContainers.size (); i++) {
145
+ if (i != m_LastTransitionSoundContainerIndex) {
146
+ validIndices.push_back (i);
147
+ }
148
+ }
149
+
150
+ unsigned int randomIndex = validIndices[RandomNum (0 , static_cast <int >(validIndices.size ()) - 1 )];
151
+ m_LastTransitionSoundContainerIndex = randomIndex;
152
+ return m_TransitionSoundContainers[randomIndex];
153
+ }
154
+ case SHUFFLE: {
155
+ unsigned int selectedIndex = m_TransitionShuffleIndices[RandomNum (0 , static_cast <int >(m_TransitionShuffleIndices.size ()) - 1 )];
156
+ m_TransitionShuffleIndices.erase (m_TransitionShuffleIndices.begin () + selectedIndex);
157
+ return m_TransitionSoundContainers[selectedIndex];
158
+ }
159
+ }
113
160
}
114
161
115
162
SoundContainer& DynamicSongSection::SelectSoundContainer () {
@@ -119,16 +166,31 @@ SoundContainer& DynamicSongSection::SelectSoundContainer() {
119
166
return m_SoundContainers[0 ];
120
167
}
121
168
122
- std::vector<unsigned int > validIndices;
123
- for (unsigned int i = 0 ; i < m_SoundContainers.size (); i++) {
124
- if (i != m_LastSoundContainerIndex) {
125
- validIndices.push_back (i);
169
+ if (m_ShuffleIndices.empty ()) {
170
+ for (unsigned int i = 0 ; i <= m_SoundContainers.size () - 1 ; i++ ) {
171
+ m_ShuffleIndices.push_back (i);
126
172
}
127
173
}
128
174
129
- unsigned int randomIndex = validIndices[RandomNum (0 , static_cast <int >(validIndices.size ()) - 1 )];
130
- m_LastSoundContainerIndex = randomIndex;
131
- return m_SoundContainers[randomIndex];
175
+ switch (m_SoundContainerSelectionCycleMode) {
176
+ case RANDOMNOREPEAT: {
177
+ std::vector<unsigned int > validIndices;
178
+ for (unsigned int i = 0 ; i < m_SoundContainers.size (); i++) {
179
+ if (i != m_LastSoundContainerIndex) {
180
+ validIndices.push_back (i);
181
+ }
182
+ }
183
+
184
+ unsigned int randomIndex = validIndices[RandomNum (0 , static_cast <int >(validIndices.size ()) - 1 )];
185
+ m_LastSoundContainerIndex = randomIndex;
186
+ return m_SoundContainers[randomIndex];
187
+ }
188
+ case SHUFFLE: {
189
+ unsigned int selectedIndex = m_ShuffleIndices[RandomNum (0 , static_cast <int >(m_ShuffleIndices.size ()) - 1 )];
190
+ m_ShuffleIndices.erase (m_ShuffleIndices.begin () + selectedIndex);
191
+ return m_SoundContainers[selectedIndex];
192
+ }
193
+ }
132
194
}
133
195
134
196
DynamicSong::DynamicSong () {
0 commit comments