-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudioScripts.lua
More file actions
1102 lines (934 loc) · 31.9 KB
/
Copy pathAudioScripts.lua
File metadata and controls
1102 lines (934 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- **MINOS AUDIO**
AudioState = {}
OnAnyLoad{
function( triggerArgs )
DeferredPlayVoiceLines = {}
if MusicName ~= nil and MusicId == nil then
-- Out of sync (presumably from a load)
local restoreTrackName = MusicName
local restoreMusicSection = MusicSection
MusicName = nil
MusicSection = nil
MusicPlayer( restoreTrackName )
SetMusicSection( restoreMusicSection )
if MusicId ~= nil then
if MusicActiveStems ~= nil then
SetSoundCueValue({ Names = MusicActiveStems, Id = MusicId, Value = 1 })
end
if MusicMutedStems ~= nil then
SetSoundCueValue({ Names = MusicMutedStems, Id = MusicId, Value = 0 })
end
else
MusicActiveStems = nil
MusicMutedStems = nil
end
if MusicPlayerTrackPaused and MusicPlayerTrackData[MusicName] ~= nil then
PauseMusic()
end
end
if AmbientTrackName ~= nil and AmbientMusicSource ~= nil and AmbientMusicId == nil then
-- Out of sync (presumably from a load)
local restoreTrackName = AmbientTrackName
AmbientTrackName = nil
wait(0.02) -- Need to wait for potential stored targets to spawn
local source = ActiveEnemies[AmbientMusicSource.ObjectId]
if source ~= nil then
MusicianMusic( source, { TrackName = restoreTrackName } )
if CurrentDeathAreaRoom ~= nil then
if CurrentDeathAreaRoom.AmbientMusicParams ~= nil then
for param, value in pairs( CurrentDeathAreaRoom.AmbientMusicParams ) do
SetSoundCueValue({ Id = AmbientMusicId, Name = param, Value = value, Duration = 0.5 })
end
end
SetVolume({ Id = AmbientMusicId, Value = CurrentDeathAreaRoom.AmbientMusicVolume, Duration = 0.5 })
end
end
end
end
}
function DeferredAudioScripts()
for index, params in ipairs(DeferredPlayVoiceLines) do
thread( PlayVoiceLinesReal, params[1], params[2], params[3], params[4] )
end
DeferredPlayVoiceLines = {}
end
-- **Music**
function MusicPlayerEvent( source, args )
MusicPlayer( args.TrackName, args.MusicInfo, args.DestinationId )
end
function MusicPlayer( trackName, musicInfo, destinationId )
if trackName == nil then
return false
end
if MusicName ~= nil and MusicName == trackName then
-- Don't play an identical track that's already playing
-- But do still update the source if it is being changed
SetSoundSource({ Id = MusicId, DestinationId = destinationId })
return false
end
if MusicId ~= nil then
-- Quick cut any music still playing
StopSound({ Id = MusicId, Duration = 0.25 })
MusicId = nil
end
if StoppingMusicId ~= nil then
-- Quick cut any music still fading out
StopSound({ Id = StoppingMusicId, Duration = 0.25 })
StoppingMusicId = nil
end
MusicName = trackName
MusicId = PlaySound({ Name = MusicName, AddCallbacks = true, Id = destinationId })
SetDefaultMusicParams( MusicName, MusicId )
if musicInfo ~= nil then
musicInfo.Id = MusicId
musicInfo.Name = MusicName
end
if SecretMusicId ~= nil then
-- Secret music has priority and is mutually exclusive so this must wait
SetVolume({ Id = MusicId, Value = 0.0, Duration = 0.0 })
PauseSound({ Id = MusicId, Duration = 0.0 })
end
return true
end
function SecretMusicPlayer( trackName, musicInfo )
if trackName == nil then
return false
end
if SecretMusicName == trackName then
-- Don't play an identical track that's already playing
return
end
if MusicId ~= nil then
-- Quick cut any music still playing
PauseMusic()
end
if SecretMusicId ~= nil then
-- Quick cut any music still playing
StopSound({ Id = SecretMusicId, Duration = 0.25 })
end
SecretMusicName = trackName
SecretMusicId = PlaySound({ Name = SecretMusicName, AddCallbacks = true })
SetDefaultMusicParams( SecretMusicName, SecretMusicId )
if musicInfo ~= nil then
musicInfo.Id = SecretMusicId
musicInfo.Name = SecretMusicName
end
return true
end
function StopSecretMusic( smoothStop )
if SecretMusicId == nil then
return
end
if smoothStop then
EndMusic( SecretMusicId, SecretMusicName )
else
StopSound({ Id = SecretMusicId, Duration = 0.25 })
end
SecretMusicId = nil
SecretMusicName = nil
end
function ChaosBassStart()
SetSoundCueValue({ Names = { "ChaosBass" }, Id = SecretMusicId, Value = 1, Duration = 0.5 })
end
function ChaosBassStop()
SetSoundCueValue({ Names = { "ChaosBass" }, Id = SecretMusicId, Value = 0, Duration = 3 })
end
function SingingPresentation( source, ars )
if source.SingingFx ~= nil then
CreateAnimation({ Name = source.SingingFx, DestinationId = source.ObjectId, OffsetX = source.SingingAnimOffsetX or source.AnimOffsetX, OffsetZ = source.AnimOffsetZ, Group = "Combat_UI_World" })
end
if source.SingingAnimation ~= nil then
SetAnimation({ Name = source.SingingAnimation, DestinationId = source.ObjectId })
end
if source.PartnerSingingAnimation ~= nil and source.PartnerObjectId ~= nil then
SetAnimation({ Name = source.PartnerSingingAnimation, DestinationId = source.PartnerObjectId })
end
end
function MusicianMusic( source, args )
if CurrentRun.BlockAmbientMusic then
return
end
CurrentRun.EventState[source.ObjectId] = { FunctionName = "SingingPresentation", Args = args }
SingingPresentation( source, args )
if args.TrackName == AmbientTrackName then
-- Don't play an identical track that's already playing
-- But do still update the source if it is being changed
SetSoundSource({ Id = AmbientMusicId, DestinationId = source.ObjectId })
AmbientMusicSource = source
return
end
if AmbientMusicId ~= nil then
-- Quick cut the previously playing id
StopSound({ Id = AmbientMusicId, Duration = 0.25 })
AmbientMusicId = nil
end
--Shake({ Id = source.ObjectId, Distance = 1, Speed = 3, Duration = 9999, Angle = 0 })
AmbientMusicSource = source
AmbientMusicId = PlaySound({ Name = args.TrackName, Id = source.ObjectId })
SetSoundCueValue({ Names = { "Vocals", }, Id = AmbientMusicId, Value = 1 })
AmbientTrackName = args.TrackName
SetVolume({ Id = AmbientMusicId, Value = 1 })
if args.TrackOffsetMin ~= nil then
SetSoundPosition({ Id = AmbientMusicId, Position = RandomFloat( args.TrackOffsetMin, args.TrackOffsetMax ) })
end
-- Workaround for FMOD bug, after a long play-session VO played in 2D can become inaudible. Pausing and unpausing the sound fixes it.
thread( PauseUnpauseSoundWorkaround, AmbientMusicId )
end
-- Workaround for FMOD bug, after a long play-session VO played in 2D can become inaudible. Pausing and unpausing the sound fixes it.
function PauseUnpauseSoundWorkaround( soundId )
wait( 0.03 )
PauseSound({ Id = soundId, Duration = 0 })
ResumeSound({ Id = soundId, Duration = 0 })
end
function StopMusicianMusic( source, args )
StopSound({ Id = AmbientMusicId, Duration = args.Duration or 0.2 })
AmbientMusicId = nil
AmbientTrackName = nil
if source ~= nil and source.ObjectId ~= nil then
CurrentRun.EventState[source.ObjectId] = nil
end
end
function SetDefaultMusicParams( trackName, musicId )
SetSoundCueValue({ Names = { "Keys" }, Id = musicId, Value = 1 })
SetSoundCueValue({ Names = { "Guitar", "Drums", "Bass" }, Id = musicId, Value = 1 })
SetMusicSection( 1, musicId )
end
function RandomStemMixer( currentRoom, musicId )
if musicId == nil then
return
end
if currentRoom.IgnoreStemMixer then
return
end
local musicSetup = RandomInt( 1, currentRoom.RandomStemMixerOptions or 3 )
if musicSetup == 1 then
-- guitar, bass, drums
SetSoundCueValue({ Names = { "Guitar" }, Id = musicId, Value = 1, Duration = 2.5 })
SetSoundCueValue({ Names = { "Bass" }, Id = musicId, Value = 1, Duration = 2.5 })
SetSoundCueValue({ Names = { "Drums" }, Id = musicId, Value = 1, Duration = 0.25 })
elseif musicSetup == 2 then
-- drums only
SetSoundCueValue({ Names = { "Guitar" }, Id = musicId, Value = 0, Duration = 10 })
SetSoundCueValue({ Names = { "Bass" }, Id = musicId, Value = 0, Duration = 10 })
SetSoundCueValue({ Names = { "Drums" }, Id = musicId, Value = 1, Duration = 0.25 })
elseif musicSetup == 3 then
-- bass and drums only
SetSoundCueValue({ Names = { "Guitar" }, Id = musicId, Value = 0, Duration = 10 })
SetSoundCueValue({ Names = { "Bass" }, Id = musicId, Value = 1, Duration = 2.5 })
SetSoundCueValue({ Names = { "Drums" }, Id = musicId, Value = 1, Duration = 0.25 })
else
-- guitar and drums only
SetSoundCueValue({ Names = { "Guitar" }, Id = musicId, Value = 1, Duration = 10 })
SetSoundCueValue({ Names = { "Bass" }, Id = musicId, Value = 0, Duration = 2.5 })
SetSoundCueValue({ Names = { "Drums" }, Id = musicId, Value = 1, Duration = 0.25 })
end
end
function MusicMixer( mixArgs )
if mixArgs == nil then
return
end
if mixArgs.MusicMixerRequirements ~= nil and not IsGameStateEligible( CurrentRun, mixArgs, mixArgs.MusicMixerRequirements ) then
return
end
wait( mixArgs.MusicStartDelay )
if mixArgs.PlayBiomeMusic then
local biomeName = CurrentRun.CurrentRoom.RoomSetName
local biomeMusicTracks = MusicTrackData[biomeName]
if biomeMusicTracks ~= nil then
if BiomeMusicPlayCounts[biomeName] == nil then
BiomeMusicPlayCounts[biomeName] = 0
end
local trackIndex = (BiomeMusicPlayCounts[biomeName] % #biomeMusicTracks) + 1
local trackData = biomeMusicTracks[trackIndex]
MusicPlayer( trackData.Name, trackData )
BiomeMusicPlayCounts[biomeName] = BiomeMusicPlayCounts[biomeName] + 1
end
end
if MusicId == nil then
return
end
if mixArgs.MusicActiveStems ~= nil then
MusicActiveStems = mixArgs.MusicActiveStems
SetSoundCueValue({ Names = mixArgs.MusicActiveStems, Id = MusicId, Value = 1, Duration = 0.75 })
end
if mixArgs.MusicMutedStems ~= nil then
MusicMutedStems = mixArgs.MusicMutedStems
SetSoundCueValue({ Names = mixArgs.MusicMutedStems, Id = MusicId, Value = 0, Duration = mixArgs.MusicMutedStemsDuration or 0.75 })
end
if mixArgs.MusicSection ~= nil then
SetMusicSection( mixArgs.MusicSection )
end
if mixArgs.UseRoomMusicSection ~= nil and CurrentRun.CurrentRoom.MusicSection ~= nil then
SetMusicSection( CurrentRun.CurrentRoom.MusicSection )
end
end
function CheckMusicEvents( currentRun, musicEvents )
if musicEvents == nil then
return
end
for k, musicEvent in ipairs( musicEvents ) do
if IsGameStateEligible( currentRun, musicEvent, musicEvent.GameStateRequirements ) then
if musicEvent.EndMusic then
EndMusic()
end
thread( MusicMixer, musicEvent )
end
end
end
function StartBossRoomMusic()
SetMusicSection( 2 )
local activeStemTable = { "Guitar", "Bass", "Drums" }
SetSoundCueValue({ Names = activeStemTable, Id = MusicId, Value = 1, Duration = 0.75 })
end
function EndMusic( musicId, musicName, hardStopTime )
if musicId == nil then
musicId = MusicId
end
if musicName == nil then
musicName = MusicName
end
if hardStopTime == nil then
hardStopTime = 20
end
if musicId == nil then
return
end
SetMusicSection( 10, musicId )
if hardStopTime ~= nil then
StopSound({ Id = musicId, Value = 0.0, Duration = hardStopTime })
StoppingMusicId = musicId
end
if musicId == MusicId then
MusicId = nil
MusicName = nil
MusicSection = nil
end
end
function PauseMusic()
PauseSound({ Id = MusicId, Duration = 0.2 })
end
function ResumeMusic( duration, delay )
if MusicId == nil then
return
end
wait( delay )
ResumeSound({ Id = MusicId, Duration = duration or 0.2 })
end
function PauseAmbientMusic()
PauseSound({ Id = AmbientMusicId, Duration = 0.2 })
PauseSound({ Id = SecretMusicId, Duration = 0.2 })
end
function ResumeAmbientMusic()
ResumeSound({ Id = AmbientMusicId, Duration = 0.2 })
SetVolume({ Id = AmbientMusicId, Value = 1.0 })
ResumeSound({ Id = SecretMusicId, Duration = 0.2 })
end
function SetMusicSection( section, musicId )
if section == nil then
return
end
if musicId == nil then
musicId = MusicId
end
SetSoundCueValue({ Names = { "Section", }, Id = musicId, Value = section })
if musicId == MusicId then
MusicSection = section
MusicSectionStartDepth = CurrentRun.RunDepthCache
end
end
function SetAdvancedTooltipMixing( value )
value = value or 0
SetSoundCueValue({ Id = GetMixingId({}), Names = { "Wagon" }, Value = value })
end
function IsMusicPlaying()
if MusicId ~= nil and MusicId > 0 then
return true
end
return false
end
-- Music Marker logic
OnMusicMarker{ "Marker End",
function( triggerArgs )
local markerName = triggerArgs.name
if markerName == "Marker End" then
notifyExistingWaiters( "MusicTrackEnded" )
end
end
}
OnMusicMarker{ "Shout",
function( triggerArgs )
if not AllowShout then
return
end
if MusicName ~= "/Music/MusicExploration4_MC" and MusicName ~= "/Music/MusicHadesReset_MC" then
return
end
if CurrentRun.Hero.LastKillTime == nil or _worldTime > CurrentRun.Hero.LastKillTime + 20 then
return
end
if IsEmpty( RequiredKillEnemies ) then
return
end
PlaySound({ Name = "/SFX/Shout1" })
AllowShout = false
end
}
-- **AMBIENCE**
function StartRoomAmbience( currentRun, currentRoom )
local newTrackName = nil
if currentRoom.Encounter then
newTrackName = currentRoom.Encounter.Ambience
end
newTrackName = newTrackName or currentRoom.Ambience
if newTrackName == "None" then
-- Silence requested
StopSound({ Id = AmbienceId, Duration = 0.5 })
AmbienceId = nil
AmbienceName = nil
elseif newTrackName ~= nil then
-- Specific track requested
if newTrackName ~= AmbienceName then
StopSound({ Id = AmbienceId, Duration = 0.5 })
AmbienceId = PlaySound({ Name = newTrackName, Duration = 0.5 })
AmbienceName = newTrackName
end
else
-- Nothing requested, use default biome track
local ambienceTrackName = nil
local biomeAmbienceTracks = AmbienceTracks[currentRoom.RoomSetName]
if biomeAmbienceTracks ~= nil then
local trackIndex = (GetCompletedRuns() % #biomeAmbienceTracks) + 1
local trackData = biomeAmbienceTracks[trackIndex]
if trackData.Name ~= nil and trackData.Name ~= AmbienceName then
StopSound({ Id = AmbienceId, Duration = 0.5 })
AmbienceId = PlaySound({ Name = trackData.Name, Duration = 0.5 })
AmbienceName = trackData.Name
end
if trackData.ReverbValue ~= nil then
SetAudioEffectState({ Name = "Reverb", Value = trackData.ReverbValue })
end
end
end
if currentRoom.ReverbValue ~= nil then
SetAudioEffectState({ Name = "Reverb", Value = currentRoom.ReverbValue })
end
end
function EndAmbience( duration )
StopSound({ Id = AmbienceId, Duration = duration or 0.2 })
AmbienceId = nil
StopSound({ Id = AmbientMusicId, Duration = duration or 0.2 })
AmbientMusicId = nil
AmbientTrackName = nil
end
-- **VO**
-- Minos Voice Debug
OnKeyPressed{ "Alt V", Name = "ToggleVoice",
function( triggerArgs )
if CurrentRun.CurrentRoom.IntroVO ~= nil then
PlayRemainingSpeech( CurrentRun.CurrentRoom.IntroVO )
end
end
}
function AudioInit()
PlayedRandomLines = PlayedRandomLines or {}
AllowShout = true
BiomeMusicPlayCounts = BiomeMusicPlayCounts or {}
end
function PlayRandomEligibleVoiceLines( voiceLineSets )
while not IsEmpty( voiceLineSets ) do
local voiceLines = RemoveRandomValue( voiceLineSets )
local playedSomething = PlayVoiceLines( voiceLines, true )
if playedSomething then
return
end
end
end
function PlayFirstEligibleVoiceLines( voiceLineSets )
local highestIndex = GetHighestIndex( voiceLineSets )
for index = 1, highestIndex do
local voiceLines = voiceLineSets[index]
if voiceLines ~= nil then
local playedSomething = PlayVoiceLines( voiceLines, true )
if playedSomething then
return
end
end
end
end
function PlayVoiceLines( voiceLines, neverQueue, source, args )
if args ~= nil and args.Defer then
for k, v in pairs(DeferredPlayVoiceLines) do
if v[1] == voiceLines then
--DebugPrint({ Text = "voice lines play request de-duped"})
return
end
end
table.insert( DeferredPlayVoiceLines, { voiceLines, neverQueue, source, args } )
return
end
return PlayVoiceLinesReal( voiceLines, neverQueue, source, args )
end
function PlayVoiceLinesReal( voiceLines, neverQueue, source, args )
if voiceLines == nil then
return
end
args = args or {}
source = GetLineSource( voiceLines, source, args )
if source == nil then
-- Never play a line if the source doesn't exist
return
end
if not IsVoiceLineEligible( CurrentRun, voiceLines, nil, nil, source, args ) then
-- First check requirements of the whole set
if voiceLines.PlayedNothingFunctionName ~= nil then
local playedNothingFunction = _G[voiceLines.PlayedNothingFunctionName]
if playedNothingFunction ~= nil then
playedNothingFunction( source, voiceLines.PlayedNothingFunctionArgs )
end
end
return
end
if source.PlayingVoiceLines then
if voiceLines.Queue == "Interrupt" then
-- Play as normal
else
if neverQueue then
--DebugPrint({ Text = "Skipped voiceLines on "..GetTableString( source.Name, source ) })
return
end
if source.QueuedVoiceLines == nil then
source.QueuedVoiceLines = {}
end
table.insert( source.QueuedVoiceLines, voiceLines )
--DebugPrint({ Text = "Queued voiceLines on "..GetTableString( source.Name, source ) })
return
end
end
local playedSomething = false
local parentLine = voiceLines
source.PlayingVoiceLines = true
if PlayVoiceLine( voiceLines, nil, nil, source, args ) then
playedSomething = true
else
if voiceLines.PlayedNothingFunctionName ~= nil then
local playedNothingFunction = _G[voiceLines.PlayedNothingFunctionName]
if playedNothingFunction ~= nil then
playedNothingFunction( source, voiceLines.PlayedNothingFunctionArgs )
end
end
end
source.PlayingVoiceLines = false
if not IsEmpty( source.QueuedVoiceLines ) then
local nextVoiceLines = RemoveFirstValue( source.QueuedVoiceLines )
if PlayVoiceLines( nextVoiceLines, false, source, args ) then
playedSomething = true
end
end
return playedSomething
end
function GetLineSource( line, source, args )
if line.ObjectType ~= nil then
local typeIds = GetIdsByType({ Name = line.ObjectType })
if IsEmpty( typeIds ) then
return nil
end
local objectId = typeIds[1]
source = ActiveEnemies[objectId]
if source == nil then
return nil
else
if line.RequiredSourceValue ~= nil and not source[line.RequiredSourceValue] then
return nil
end
if line.RequiredSourceValueFalse ~= nil and source[line.RequiredSourceValueFalse] then
return nil
end
return source
end
end
if line.UsePlayerSource then
return CurrentRun.Hero
end
if line.Source ~= nil then
return line.Source
end
if source ~= nil then
return source
end
return CurrentRun.Hero
end
function PlayVoiceLine( line, prevLine, parentLine, source, args )
local playedSomething = false
if parentLine == nil then
parentLine = line
end
args = args or {}
args.ThreadName = line.ThreadName or args.ThreadName
args.Queue = line.Queue or args.Queue
args.BreakIfPlayed = line.BreakIfPlayed or args.BreakIfPlayed
args.PlayOnceContext = line.PlayOnceContext or args.PlayOnceContext
args.SubtitleMinDistance = line.SubtitleMinDistance or args.SubtitleMinDistance
args.Actor = line.Actor or args.Actor
args.AllowTalkOverTextLines = line.AllowTalkOverTextLines or args.AllowTalkOverTextLines
-- By default, the player object is the ListenerId, though could be something else
args.ListenerId = line.ListenerId or args.ListenerId or CurrentRun.Hero.ObjectId
source = GetLineSource( line, source, args )
if source == nil then
-- Never play a line if the source doesn't exist
return
end
if line.SetFlagTrue ~= nil then
GameState.Flags[line.SetFlagTrue] = true
end
if line.SetFlagFalse ~= nil then
GameState.Flags[line.SetFlagFalse] = false
end
if line.TriggerCooldowns ~= nil then
for k, cooldownName in pairs( line.TriggerCooldowns ) do
TriggerCooldown( cooldownName )
end
end
-- Play this line
if line.Cue ~= nil then
if args.OnPlayedSomethingFunctionName ~= nil and not args.PlayedSomething then
local onPlayedSomethingFunction = _G[args.OnPlayedSomethingFunctionName]
if onPlayedSomethingFunction ~= nil then
thread( onPlayedSomethingFunction, source, args.OnPlayedSomethingFunctionArgs )
end
end
if line.PreLineThreadedFunctionName ~= nil then
local preLineThreadedFunction = _G[line.PreLineThreadedFunctionName]
thread(preLineThreadedFunction, source, line.PreLineThreadedFunctionArgs )
end
wait( line.PreLineWait or parentLine.PreLineWait, args.ThreadName )
local preLineAnim = line.PreLineAnim or parentLine.PreLineAnim
if preLineAnim ~= nil then
SetAnimation({ Name = preLineAnim, DestinationId = source.ObjectId })
end
local playedSpeechId = 0
local useSubtitles = false
if not source.Mute then
if args.SubtitleMinDistance then
local dist = GetDistance({ Id = args.ListenerId, DestinationId = source.ObjectId })
if dist > args.SubtitleMinDistance then
useSubtitles = false
else
useSubtitles = true
end
else
useSubtitles = true
end
if line.NoTarget or parentLine.NoTarget then
playedSpeechId = PlaySpeech({ Name = line.Cue, Queue = args.Queue, SubtitleColor = source.SubtitleColor, UseSubtitles = useSubtitles, Actor = args.Actor })
elseif line.SkipAnim or parentLine.SkipAnim then
playedSpeechId = PlaySpeechCueFromSource( line.Cue, source, false, args.Queue, useSubtitles, source.SubtitleColor, args )
else
playedSpeechId = PlaySpeechCueFromSource( line.Cue, source, true, args.Queue, useSubtitles, source.SubtitleColor, args )
end
end
if line.UseOcclusion then
SetSoundCueValue({ Id = playedSpeechId, Names = { "VoiceOcclusion" }, Value = 1.0, Duration = 0.01 })
end
if playedSpeechId > 0 then
prevLine = line
LastLinePlayed = line.Cue
table.insert( CurrentRun.CurrentRoom.VoiceLinesPlayed, line.Cue )
playedSomething = true
args.PlayedSomething = true
-- @refactor The SpeechRecord recording is pretty scattered / redundant
SpeechRecord[line.Cue] = true
CurrentRun.SpeechRecord[line.Cue] = true
if args.PlayOnceContext ~= nil then
GameState.SpeechRecordContexts[args.PlayOnceContext] = GameState.SpeechRecordContexts[args.PlayOnceContext] or {}
GameState.SpeechRecordContexts[args.PlayOnceContext][line.Cue] = true
end
-- Intentionally leaving this on raw data for now to be wiped out on load
line.LastPlayTime = _worldTime
parentLine.LastPlayTime = _worldTime
waitUntil( line.Cue )
wait( line.PostLineWait or parentLine.PostLineWait, args.ThreadName )
if line.PostLineFunctionName ~= nil then
local postLineFunction = _G[line.PostLineFunctionName]
postLineFunction( source, line.PostLineFunctionArgs )
end
if args.BreakIfPlayed then
return playedSomething
end
else
--DebugAssert({ Condition = playedSpeechId > 0, Text = "Speech failed to play: "..line.Cue })
end
end
-- Play sublines
if line.RandomRemaining then
local eligibleUnplayedLines = {}
local allEligibleLines = {}
for k, subLine in ipairs( line ) do
if IsVoiceLineEligible( CurrentRun, subLine, prevLine, line, source, args ) then
table.insert( allEligibleLines, subLine )
if not PlayedRandomLines[subLine.Cue] then
table.insert( eligibleUnplayedLines, subLine )
end
end
end
if not IsEmpty( allEligibleLines ) then
local randomLine = nil
if IsEmpty( eligibleUnplayedLines ) then
-- All lines played, start the record over
for k, subLine in ipairs( line ) do
PlayedRandomLines[subLine.Cue] = nil
end
randomLine = GetRandomValue( allEligibleLines )
else
randomLine = GetRandomValue( eligibleUnplayedLines )
end
PlayedRandomLines[randomLine.Cue] = true
-- Effectively pass down by value rather than reference
local subLineArgs = ShallowCopyTable( args )
if PlayVoiceLine( randomLine, prevLine, line, source, subLineArgs ) then
prevLine = randomLine
playedSomething = true
args.PlayedSomething = true
if args.BreakIfPlayed or randomLine.BreakIfPlayed or subLineArgs.BreakIfPlayed then
return playedSomething
end
end
end
else
for k, subLine in ipairs( line ) do
if IsVoiceLineEligible( CurrentRun, subLine, prevLine, line, source, args ) then
-- Effectively pass down by value rather than reference
local subLineArgs = ShallowCopyTable( args )
if PlayVoiceLine( subLine, prevLine, line, source, subLineArgs ) then
prevLine = subLine
playedSomething = true
args.PlayedSomething = true
if args.BreakIfPlayed or subLine.BreakIfPlayed or subLineArgs.BreakIfPlayed then
return playedSomething
end
end
end
end
end
return playedSomething
end
function CleanUpCurrentSpeechId( cue, speechId, source, animation )
if speechId == nil then
return
end
waitUntil( cue )
if animation ~= nil then
if StopStatusAnimation( source, animation ) and not source.BlockStatusAnimations and ConfigOptionCache.ShowUIAnimations and ConfigOptionCache.ShowSpeechBubble then
local endAnimation = animation.."_End"
CreateAnimation({ Name = endAnimation, DestinationId = source.ObjectId, OffsetX = source.AnimOffsetX, OffsetZ = source.AnimOffsetZ })
end
end
if CurrentSpeechId == speechId then
CurrentSpeechId = nil
end
end
function PlaySpeechCue( cue, source, animation, queue, useSubtitles, subtitleColor, args )
if cue == nil or cue == "" then
return 0
end
args = args or {}
local sourceId = nil
if source ~= nil then
sourceId = source.ObjectId
end
local speechId = PlaySpeech({ Name = cue, Id = sourceId, Queue = queue, UseSubtitles = useSubtitles, SubtitleColor = subtitleColor, Actor = args.Actor })
if speechId > 0 then
if source ~= nil and animation ~= nil and ConfigOptionCache.ShowUIAnimations and ConfigOptionCache.ShowSpeechBubble then
PlayStatusAnimation( source, { Animation = animation } )
end
CurrentSpeechId = speechId
-- @refactor The SpeechRecord recording is pretty scattered / redundant
SpeechRecord[cue] = true
CurrentRun.SpeechRecord[cue] = true
thread( CleanUpCurrentSpeechId, cue, speechId, source, animation )
end
return speechId
end
function PlaySpeechCueFromSource( cue, source, useDefaultAnim, queue, useSubtitles, subtitleColor, args )
if PlayingTextLines and not args.AllowTalkOverTextLines then
return 0
end
if source == nil then
source = CurrentRun.Hero
end
if useDefaultAnim == nil then
useDefaultAnim = true
end
local anim = nil
if useDefaultAnim then
anim = StatusAnimations.Speaking
end
return PlaySpeechCue( cue, source, anim, queue, useSubtitles, subtitleColor, args )
end
function IsVoiceLineEligible( currentRun, line, prevLine, parentLine, source, args )
if line == nil then
return false
end
args = args or {}
if source ~= nil then
if line.RequiresFalseCharmed ~= nil and source.Charmed then
return false
end
if line.RequiresCharmed ~= nil and not source.Charmed then
return false
end
if line.RequiredSourceValue ~= nil and not source[line.RequiredSourceValue] then
return false
end
if line.RequiredSourceValueFalse ~= nil and source[line.RequiredSourceValueFalse] then
return false
end
end
if args ~= nil and args.ElapsedTime ~= nil then
if line.RequiredMinElapsedTime ~= nil and args.ElapsedTime < line.RequiredMinElapsedTime then
return false
end
if line.RequiredMaxElapsedTime ~= nil and args.ElapsedTime > line.RequiredMaxElapsedTime then
return false
end
end
if line.ExplicitRequirements or ( parentLine ~= nil and parentLine.ExplicitRequirements ) then
if line.GameStateRequirements ~= nil and not IsGameStateEligible( currentRun, line, line.GameStateRequirements ) then
return false
end
else
if not IsGameStateEligible( currentRun, line, line.GameStateRequirements ) then
return false
end
end
if line.PlayOnceFromTableThisRun then
for k, subLine in ipairs( line ) do
if subLine.Cue ~= nil and currentRun.SpeechRecord[subLine.Cue] then
return false
end
end
end
if line.SuccessiveChanceToPlay ~= nil then
local anyLinePlayed = false
for k, subLine in ipairs( line ) do
if subLine.Cue ~= nil and SpeechRecord[subLine.Cue] then
anyLinePlayed = true
break
end
end
if anyLinePlayed and not RandomChance( line.SuccessiveChanceToPlay ) then
return false
end
end
if line.SuccessiveChanceToPlayAll ~= nil then
local allLinesPlayed = true
for k, subLine in ipairs( line ) do
if subLine.Cue ~= nil and not SpeechRecord[subLine.Cue] then
allLinesPlayed = false
break
end
end
if allLinesPlayed and not RandomChance( line.SuccessiveChanceToPlayAll ) then
return false
end
end
if line.Cue ~= nil then
if line.PlayOnce or ( parentLine ~= nil and parentLine.PlayOnce ) then
if args.PlayOnceContext ~= nil then
DebugPrint({ Text = "checking context = "..tostring(args.PlayOnceContext) })
GameState.SpeechRecordContexts[args.PlayOnceContext] = GameState.SpeechRecordContexts[args.PlayOnceContext] or {}
if GameState.SpeechRecordContexts[args.PlayOnceContext][line.Cue] then
return false
end
elseif SpeechRecord[line.Cue] then
return false
end
end
if line.PlayOnceThisRun or ( parentLine ~= nil and parentLine.PlayOnceThisRun ) then