Debian Codegraph @

editor/AmbientSoundPattern.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // AmbientSoundPattern.cpp : implementation file
20 //
21
22 #include "stdafx.h"
23 #include "editor.h"
24 #include "AmbientSoundPattern.h"
25 #include "AmbientSoundElement.h"
26 #include "EditLineDialog.h"
27 #include "ambient.h"
28 #include "ssl_lib.h"
29 #include "pserror.h"
30 #include "gamefilepage.h"
31 #include "ddio.h"
32 #include "mem.h"
33
34 #ifdef _DEBUG
35 #define new DEBUG_NEW
36 #undef THIS_FILE
37 static char THIS_FILE[] = __FILE__;
38 #endif
39
40 /////////////////////////////////////////////////////////////////////////////
41 // CAmbientSoundPattern dialog
42
43 CAmbientSoundPattern::CAmbientSoundPattern(CWnd *pParent /*=NULL*/) : CDialog(CAmbientSoundPattern::IDD, pParent) {
44 //{{AFX_DATA_INIT(CAmbientSoundPattern)
45 // NOTE: the ClassWizard will add member initialization here
46 //}}AFX_DATA_INIT
47 }
48
49 void CAmbientSoundPattern::DoDataExchange(CDataExchange *pDX) {
50 CDialog::DoDataExchange(pDX);
51 //{{AFX_DATA_MAP(CAmbientSoundPattern)
52 // NOTE: the ClassWizard will add DDX and DDV calls here
53 //}}AFX_DATA_MAP
54 }
55
56 BEGIN_MESSAGE_MAP(CAmbientSoundPattern, CDialog)
57 //{{AFX_MSG_MAP(CAmbientSoundPattern)
58 ON_CBN_SELENDOK(IDC_ASP_LIST, OnSelendokASPList)
59 ON_BN_CLICKED(IDC_ASP_CHECK_IN, OnASPCheckIn)
60 ON_BN_CLICKED(IDC_ASP_DELETE, OnASPDelete)
61 ON_BN_CLICKED(IDC_ASP_EDIT_ELEMENT, OnASPEditElement)
62 ON_BN_CLICKED(IDC_ASP_LOCK, OnASPLock)
63 ON_BN_CLICKED(IDC_ASP_NEW, OnASPNew)
64 ON_BN_CLICKED(IDC_ASP_NEW_ELEMENT, OnASPNewElement)
65 ON_BN_CLICKED(IDC_ASP_RENAME, OnASPRename)
66 ON_LBN_SELCHANGE(IDC_ASP_ELEMENT_LIST, OnSelchangeASPElementList)
67 ON_BN_CLICKED(IDC_ASP_DELETE_ELEMENT, OnASPDeleteElement)
68 ON_BN_CLICKED(IDC_ASP_UNDO_LOCK, OnASPUndoLock)
69 ON_EN_KILLFOCUS(IDC_ASP_MAX_DELAY, OnKillfocusASPMaxDelay)
70 ON_EN_KILLFOCUS(IDC_ASP_MIN_DELAY, OnKillfocusASPMinDelay)
71 //}}AFX_MSG_MAP
72 END_MESSAGE_MAP()
73
74 /////////////////////////////////////////////////////////////////////////////
75 // CAmbientSoundPattern message handlers
76
77 BOOL CAmbientSoundPattern::OnInitDialog() {
78 CDialog::OnInitDialog();
79
80 m_current_asp = m_current_element = -1;
81
82 m_locked = (mng_FindTrackLock(AMBIENT_FILE_NAME, PAGETYPE_GAMEFILE) != -1);
83
84 UpdateDialog();
85
86 return TRUE; // return TRUE unless you set the focus to a control
87 // EXCEPTION: OCX Property Pages should return FALSE
88 }
89
90 void CAmbientSoundPattern::UpdateDialog() {
91 int i;
92
93 // Build the ambient sound list
94 CComboBox *asplist = (CComboBox *)GetDlgItem(IDC_ASP_LIST);
95 asplist->ResetContent();
96 for (i = 0; i < Num_ambient_sound_patterns; i++)
97 if (Ambient_sound_patterns[i].name[0]) {
98 asplist->AddString(Ambient_sound_patterns[i].name);
99 if (m_current_asp == -1)
100 m_current_asp = i;
101 }
102 if (m_current_asp != -1)
103 asplist->SelectString(-1, Ambient_sound_patterns[m_current_asp].name);
104
105 // Write out elements for current ASP
106 CListBox *aselist = (CListBox *)GetDlgItem(IDC_ASP_ELEMENT_LIST);
107 char tbuf[100];
108 asp *asp = &Ambient_sound_patterns[m_current_asp];
109 aselist->ResetContent();
110 for (i = 0; i < asp->num_sounds; i++) {
111 ase *ase = &asp->sounds[i];
112 sprintf(tbuf, "%s: vol=%.2f,%.2f prob=%d", Sounds[ase->handle].name, ase->min_volume, ase->max_volume,
113 ase->probability);
114 aselist->AddString(tbuf);
115 }
116
117 // Write out the delay values
118 CEdit *ebox = (CEdit *)GetDlgItem(IDC_ASP_MIN_DELAY);
119 char buf[20];
120 sprintf(buf, "%.1f", asp->min_delay);
121 ebox->SetWindowText(buf);
122
123 ebox = (CEdit *)GetDlgItem(IDC_ASP_MAX_DELAY);
124 sprintf(buf, "%.1f", asp->max_delay);
125 ebox->SetWindowText(buf);
126
127 // Select current element
128 if (aselist->GetCount() > 0)
129 aselist->SetCurSel(m_current_element);
130
131 // Enable/disable editing buttons
132 ((CButton *)GetDlgItem(IDC_ASP_DELETE))->EnableWindow(m_locked);
133 ((CButton *)GetDlgItem(IDC_ASP_EDIT_ELEMENT))->EnableWindow(m_locked);
134 ((CButton *)GetDlgItem(IDC_ASP_NEW))->EnableWindow(m_locked);
135 ((CButton *)GetDlgItem(IDC_ASP_NEW_ELEMENT))->EnableWindow(m_locked);
136 ((CButton *)GetDlgItem(IDC_ASP_RENAME))->EnableWindow(m_locked);
137 ((CButton *)GetDlgItem(IDC_ASP_DELETE_ELEMENT))->EnableWindow(m_locked);
138 ((CButton *)GetDlgItem(IDC_ASP_MIN_DELAY))->EnableWindow(m_locked);
139 ((CButton *)GetDlgItem(IDC_ASP_MAX_DELAY))->EnableWindow(m_locked);
140
141 // Enable/disable locking buttons
142 ((CButton *)GetDlgItem(IDC_ASP_CHECK_IN))->EnableWindow(m_locked);
143 ((CButton *)GetDlgItem(IDC_ASP_UNDO_LOCK))->EnableWindow(m_locked);
144 ((CButton *)GetDlgItem(IDC_ASP_LOCK))->EnableWindow(!m_locked);
145 }
146
147 void CAmbientSoundPattern::OnSelendokASPList() {
148 char name[200];
149
150 CComboBox *asplist = (CComboBox *)GetDlgItem(IDC_ASP_LIST);
151
152 asplist->GetLBText(asplist->GetCurSel(), name);
153 m_current_asp = FindAmbientSoundPattern(name);
154
155 if (Ambient_sound_patterns[m_current_asp].num_sounds)
156 m_current_element = 0;
157 else
158 m_current_element = -1;
159
160 UpdateDialog();
161 }
162
163 void CAmbientSoundPattern::OnASPRename() {
164 if (!InputString(Ambient_sound_patterns[m_current_asp].name, sizeof(Ambient_sound_patterns[m_current_asp].name),
165 "Ambient Sound Pattern", "Enter a new name"))
166 return;
167
168 UpdateDialog();
169 }
170
171 void CAmbientSoundPattern::OnSelchangeASPElementList() {
172 CListBox *lb = (CListBox *)GetDlgItem(IDC_ASP_ELEMENT_LIST);
173
174 m_current_element = lb->GetCurSel();
175 }
176
177 void CAmbientSoundPattern::OnASPDelete() {
178 if (OutrageMessageBox(MBOX_YESNO, "Do you really want to delete the ambient sound pattern '%s'?",
179 AmbientSoundPatternName(m_current_asp)) == IDYES) {
180 asp *asp = &Ambient_sound_patterns[m_current_asp];
181
182 asp->name[0] = 0; // null string for name
183 mem_free(asp->sounds);
184 asp->num_sounds = 0;
185 m_current_asp = -1;
186
187 UpdateDialog();
188 }
189 }
190
191 void CAmbientSoundPattern::OnASPNew() {
192 asp *asp = &Ambient_sound_patterns[Num_ambient_sound_patterns];
193
194 if (InputString(asp->name, sizeof(asp->name), "Ambient Sound Pattern", "Enter a name for your new pattern"))
195 if (asp->name[0]) {
196 asp->num_sounds = 0;
197 asp->min_delay = asp->max_delay = 0.0;
198 m_current_asp = Num_ambient_sound_patterns++;
199 UpdateDialog();
200 }
201 }
202
203 void CAmbientSoundPattern::OnASPNewElement() {
204 asp *asp = &Ambient_sound_patterns[m_current_asp];
205 ase new_element;
206
207 new_element.handle = -1;
208 new_element.min_volume = new_element.max_volume = 1.0;
209 new_element.probability = 50;
210
211 CAmbientSoundElement dlg(&new_element);
212
213 theApp.pause();
214 dlg.DoModal();
215 theApp.resume();
216
217 if (new_element.handle != -1) {
218
219 ase *new_sounds = (ase *)mem_malloc(sizeof(*new_sounds) * (asp->num_sounds + 1));
220
221 for (int s = 0; s < asp->num_sounds; s++)
222 new_sounds[s] = asp->sounds[s];
223
224 mem_free(asp->sounds);
225
226 asp->sounds = new_sounds;
227 asp->sounds[s] = new_element;
228
229 asp->num_sounds++;
230
231 m_current_element = s;
232
233 UpdateDialog();
234 }
235 }
236
237 void CAmbientSoundPattern::OnASPEditElement() {
238 if (m_current_element > -1) {
239 CAmbientSoundElement dlg(&Ambient_sound_patterns[m_current_asp].sounds[m_current_element]);
240
241 theApp.pause();
242 dlg.DoModal();
243 theApp.resume();
244
245 UpdateDialog();
246 }
247 }
248
249 void CAmbientSoundPattern::OnASPDeleteElement() {
250 asp *asp = &Ambient_sound_patterns[m_current_asp];
251 ase *new_sounds;
252
253 if (m_current_element > -1) {
254 if (OutrageMessageBox(MBOX_YESNO, "Do you really want to delete sound '%s' from pattern '%s'?",
255 Sounds[asp->sounds[m_current_element].handle].name, asp->name) == IDYES) {
256 new_sounds = (ase *)mem_malloc(sizeof(*new_sounds) * (asp->num_sounds - 1));
257
258 for (int s = 0; s < m_current_element; s++)
259 new_sounds[s] = asp->sounds[s];
260
261 for (; s < asp->num_sounds - 1; s++)
262 new_sounds[s] = asp->sounds[s + 1];
263
264 mem_free(asp->sounds);
265
266 asp->sounds = new_sounds;
267
268 asp->num_sounds--;
269
270 UpdateDialog();
271 }
272 }
273 }
274
275 void CAmbientSoundPattern::OnKillfocusASPMinDelay() {
276 asp *asp = &Ambient_sound_patterns[m_current_asp];
277
278 CEdit *ebox;
279 char buf[20];
280
281 ebox = (CEdit *)GetDlgItem(IDC_ASP_MIN_DELAY);
282 ebox->GetWindowText(buf, sizeof(buf));
283
284 asp->min_delay = atof(buf);
285
286 UpdateDialog();
287 }
288
289 void CAmbientSoundPattern::OnKillfocusASPMaxDelay() {
290 asp *asp = &Ambient_sound_patterns[m_current_asp];
291
292 CEdit *ebox;
293 char buf[20];
294
295 ebox = (CEdit *)GetDlgItem(IDC_ASP_MAX_DELAY);
296 ebox->GetWindowText(buf, sizeof(buf));
297
298 asp->max_delay = atof(buf);
299
300 UpdateDialog();
301 }
302
303 void CAmbientSoundPattern::OnCancel() {
304 // Re-read old data
305 ReadAmbientData();
306
307 CDialog::OnCancel();
308 }
309
310 void CAmbientSoundPattern::OnOK() {
311 // Verify probabilities
312 for (int p = 0; p < Num_ambient_sound_patterns; p++) {
313 asp *asp = &Ambient_sound_patterns[p];
314 if (asp->num_sounds) {
315 int prob = 0;
316
317 for (int s = 0; s < asp->num_sounds; s++)
318 prob += asp->sounds[s].probability;
319
320 if (prob != 100) {
321 OutrageMessageBox("Error: the probabilities for pattern '%s' don't add up to 100.", asp->name);
322 return;
323 }
324 }
325 }
326
327 // Update the data
328 WriteAmbientData();
329
330 CDialog::OnOK();
331 }
332
333 void CAmbientSoundPattern::OnASPLock() {
334 int n;
335 mngs_Pagelock temp_pl;
336 mngs_gamefile_page gamefilepage;
337 int r;
338
339 // Find where it is in the list
340 n = FindGamefileName(AMBIENT_FILE_NAME);
341
342 if (n == -1) {
343 Int3(); // Get Matt or Jason - AMBIENT file name is no longer in the table file!
344 return;
345 }
346
347 if (!mng_MakeLocker())
348 return;
349
350 // Make sure it can be locked
351 strcpy(temp_pl.name, AMBIENT_FILE_NAME);
352 temp_pl.pagetype = PAGETYPE_GAMEFILE;
353
354 r = mng_CheckIfPageLocked(&temp_pl);
355 if (r == 2) {
356 int answer;
357 answer = OutrageMessageBox(MBOX_YESNO, "This page is not even in the table file, or the database maybe corrupt. "
358 "Override to 'Unlocked'? (Select NO if you don't know what you're doing)");
359 if (answer == IDYES) {
360 strcpy(temp_pl.holder, "UNLOCKED");
361 if (!mng_ReplacePagelock(temp_pl.name, &temp_pl))
362 MessageBox(ErrorString, "Error!");
363 }
364 } else if (r < 0)
365 OutrageMessageBox(ErrorString);
366 else if (r == 1)
367 OutrageMessageBox(InfoString);
368 else {
369
370 // Everything is ok. Tell the network we're locking it and get a copy to
371 // our local drive
372 strcpy(temp_pl.holder, TableUser);
373
374 // Search thru the net pagefile and get a new copy in RAM in case anyone
375 // changed it since we started the editor
376 if (mng_FindSpecificGamefilePage(temp_pl.name, &gamefilepage, 0)) {
377
378 if (mng_AssignGamefilePageToGamefile(&gamefilepage, n)) {
379 if (!mng_ReplacePage(Gamefiles[n].name, Gamefiles[n].name, n, PAGETYPE_GAMEFILE, 1)) {
380 OutrageMessageBox("There was problem writing that page locally!");
381 mng_EraseLocker();
382 return;
383 } else {
384
385 if (!mng_ReplacePagelock(temp_pl.name, &temp_pl)) {
386 MessageBox(ErrorString, "Error!");
387 mng_EraseLocker();
388 return;
389 }
390 }
391 OutrageMessageBox("Gamefile locked.");
392 } else
393 OutrageMessageBox(
394 "There was a problem loading this gamefile. You might encounter problems in dealing with it. Good luck!");
395
396 mng_AllocTrackLock(Gamefiles[n].name, PAGETYPE_GAMEFILE);
397 UpdateDialog();
398 } else
399 OutrageMessageBox("Couldn't find that gamefile in the table file!");
400 }
401
402 mng_EraseLocker();
403
404 // Read the data again in case we just got a newer file
405 ReadAmbientData();
406
407 m_locked = 1;
408
409 UpdateDialog();
410 }
411
412 void CAmbientSoundPattern::OnASPCheckIn() {
413 // Write the updated data to the file
414 WriteAmbientData();
415
416 // Find where it is in the files index
417 int n = FindGamefileName(AMBIENT_FILE_NAME);
418
419 if (n == -1) {
420 Int3(); // Get Matt or Jason - AMBIENT file name is no longer in the table file!
421 return;
422 }
423
424 mngs_Pagelock temp_pl;
425 int r;
426
427 // Lock the network table file
428 if (!mng_MakeLocker())
429 return;
430
431 // Make sure we own this gamefile
432 strcpy(temp_pl.name, Gamefiles[n].name);
433 temp_pl.pagetype = PAGETYPE_GAMEFILE;
434
435 r = mng_CheckIfPageOwned(&temp_pl, TableUser);
436 if (r < 0)
437 OutrageMessageBox(ErrorString);
438 else if (r == 0)
439 OutrageMessageBox(InfoString);
440 else {
441 // Change the pagelock state to UNLOCKED
442 strcpy(temp_pl.holder, "UNLOCKED");
443 if (!mng_ReplacePagelock(temp_pl.name, &temp_pl)) {
444 MessageBox(ErrorString, "Error!");
445 mng_EraseLocker();
446 return;
447 } else {
448 // Now actually replace the copy on the net with our local one
449
450 if (!mng_ReplacePage(Gamefiles[n].name, Gamefiles[n].name, n, PAGETYPE_GAMEFILE, 0))
451 OutrageMessageBox(ErrorString);
452 else {
453 // Save this gamefile file to the network for all
454
455 char destname[100], srcname[100];
456
457 ddio_MakePath(srcname, LocalD3Dir, "data", Gamefiles[n].dir_name, Gamefiles[n].name, NULL);
458 ddio_MakePath(destname, NetD3Dir, "data", Gamefiles[n].dir_name, Gamefiles[n].name, NULL);
459
460 cf_CopyFile(destname, srcname);
461
462 OutrageMessageBox("File checked in.");
463
464 // Delete it from local pagefile if its there
465 int dret = mng_DeletePage(Gamefiles[n].name, PAGETYPE_GAMEFILE, 1);
466 ASSERT(dret == 1);
467 mng_EraseLocker();
468
469 // Free the tracklock
470 int p = mng_FindTrackLock(Gamefiles[n].name, PAGETYPE_GAMEFILE);
471 ASSERT(p != -1);
472 mng_FreeTrackLock(p);
473 }
474 }
475 }
476
477 m_locked = 0;
478 UpdateDialog();
479 }
480
481 void CAmbientSoundPattern::OnASPUndoLock() {
482 int tl;
483 mngs_Pagelock pl;
484 mngs_gamefile_page page;
485
486 // Should have this item locked
487 if ((tl = mng_FindTrackLock(AMBIENT_FILE_NAME, PAGETYPE_GAMEFILE)) == -1)
488 return;
489
490 if (OutrageMessageBox(MBOX_YESNO,
491 "Are you sure you want to undo your lock and lose any changes you may have made?") != IDYES)
492 return;
493
494 if (!mng_MakeLocker())
495 return;
496
497 // Find where it is in the files index
498 int n = FindGamefileName(AMBIENT_FILE_NAME);
499
500 if (n == -1) {
501 Int3(); // Get Matt or Jason - AMBIENT file name is no longer in the table file!
502 return;
503 }
504
505 strcpy(pl.name, AMBIENT_FILE_NAME);
506 pl.pagetype = PAGETYPE_GAMEFILE;
507
508 mng_FreeTrackLock(tl);
509
510 // Delete local page
511 if (!mng_DeletePage(AMBIENT_FILE_NAME, PAGETYPE_GAMEFILE, 1)) {
512 mprintf(0, ErrorString);
513 Int3();
514 }
515
516 // Get old data from net
517 if (!mng_FindSpecificGamefilePage(pl.name, &page))
518 Int3();
519
520 // Copy old get into our local array
521 if (!mng_AssignGamefilePageToGamefile(&page, n))
522 Int3();
523
524 // We're done
525 mng_EraseLocker();
526
527 // Re-read old data
528 ReadAmbientData();
529
530 m_current_asp = -1;
531 m_locked = 0;
532
533 UpdateDialog();
534 }