forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoteDataUtil.cpp
3110 lines (2796 loc) · 89.4 KB
/
NoteDataUtil.cpp
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
#include "global.h"
#include "NoteDataUtil.h"
#include "NoteData.h"
#include "RageUtil.h"
#include "RageLog.h"
#include "PlayerOptions.h"
#include "Song.h"
#include "Style.h"
#include "GameState.h"
#include "RadarValues.h"
#include "TimingData.h"
#include <utility>
#include <array>
using std::vector;
// TODO: Remove these constants that aren't time signature-aware
static const int BEATS_PER_MEASURE = 4;
static const int ROWS_PER_MEASURE = ROWS_PER_BEAT * BEATS_PER_MEASURE;
NoteType NoteDataUtil::GetSmallestNoteTypeForMeasure( const NoteData &nd, int iMeasureIndex )
{
const int iMeasureStartIndex = iMeasureIndex * ROWS_PER_MEASURE;
const int iMeasureEndIndex = (iMeasureIndex+1) * ROWS_PER_MEASURE;
return NoteDataUtil::GetSmallestNoteTypeInRange( nd, iMeasureStartIndex, iMeasureEndIndex );
}
NoteType NoteDataUtil::GetSmallestNoteTypeInRange( const NoteData &n, int iStartIndex, int iEndIndex )
{
// probe to find the smallest note type
FOREACH_ENUM(NoteType, nt)
{
float fBeatSpacing = NoteTypeToBeat( nt );
int iRowSpacing = std::lrint( fBeatSpacing * ROWS_PER_BEAT );
bool bFoundSmallerNote = false;
// for each index in this measure
FOREACH_NONEMPTY_ROW_ALL_TRACKS_RANGE( n, i, iStartIndex, iEndIndex )
{
if( i % iRowSpacing == 0 )
continue; // skip
if( !n.IsRowEmpty(i) )
{
bFoundSmallerNote = true;
break;
}
}
if( bFoundSmallerNote )
continue; // searching the next NoteType
else
return nt; // stop searching. We found the smallest NoteType
}
return NoteType_Invalid; // well-formed notes created in the editor should never get here
}
static void LoadFromSMNoteDataStringWithPlayer( NoteData& out, const std::string &sSMNoteData, int start,
int len, PlayerNumber pn, int iNumTracks )
{
/* Don't allocate memory for the entire string, nor per measure. Instead, use the in-place
* partial string split twice. By maintaining begin and end pointers to each measure line
* we can perform this without copying the string at all. */
int size = -1;
const int end = start + len;
vector<std::pair<const char *, const char *> > aMeasureLines;
for( unsigned m = 0; true; ++m )
{
/* XXX Ignoring empty seems wrong for measures. It means that ",,," is treated as
* "," where I would expect most people would want 2 empty measures. ",\n,\n,"
* would do as I would expect. */
Rage::split_in_place( sSMNoteData, ",", start, size, end, Rage::EmptyEntries::skip ); // Ignore empty is important.
if( start == end )
{
break;
}
// Partial string split.
int measureLineStart = start, measureLineSize = -1;
const int measureEnd = start + size;
aMeasureLines.clear();
for(;;)
{
// Ignore empty is clearly important here.
Rage::split_in_place( sSMNoteData, "\n", measureLineStart, measureLineSize, measureEnd, Rage::EmptyEntries::skip );
if( measureLineStart == measureEnd )
{
break;
}
//std::string &line = sSMNoteData.substr( measureLineStart, measureLineSize );
const char *beginLine = sSMNoteData.data() + measureLineStart;
const char *endLine = beginLine + measureLineSize;
while( beginLine < endLine && strchr("\r\n\t ", *beginLine) )
{
++beginLine;
}
while( endLine > beginLine && strchr("\r\n\t ", *(endLine - 1)) )
{
--endLine;
}
if( beginLine < endLine ) // nonempty
{
aMeasureLines.push_back( std::pair<const char *, const char *>(beginLine, endLine) );
}
}
for( unsigned l=0; l<aMeasureLines.size(); l++ )
{
const char *p = aMeasureLines[l].first;
const char *const beginLine = p;
const char *const endLine = aMeasureLines[l].second;
const float fPercentIntoMeasure = l/(float)aMeasureLines.size();
const float fBeat = (m + fPercentIntoMeasure) * BEATS_PER_MEASURE;
const int iIndex = BeatToNoteRow( fBeat );
int iTrack = 0;
while( iTrack < iNumTracks && p < endLine )
{
TapNote tn;
char ch = *p;
switch( ch )
{
case '0': tn = TAP_EMPTY; break;
case '1': tn = TAP_ORIGINAL_TAP; break;
case '2':
case '4':
// case 'N': // minefield
tn = ch == '2' ? TAP_ORIGINAL_HOLD_HEAD : TAP_ORIGINAL_ROLL_HEAD;
/*
// upcoming code for minefields -aj
switch(ch)
{
case '2': tn = TAP_ORIGINAL_HOLD_HEAD; break;
case '4': tn = TAP_ORIGINAL_ROLL_HEAD; break;
case 'N': tn = TAP_ORIGINAL_MINE_HEAD; break;
}
*/
/* Set the hold note to have infinite length. We'll clamp
* it when we hit the tail. */
tn.iDuration = MAX_NOTE_ROW;
break;
case '3':
{
// This is the end of a hold. Search for the beginning.
int iHeadRow;
if( !out.IsHoldNoteAtRow( iTrack, iIndex, &iHeadRow ) )
{
int n = intptr_t(endLine) - intptr_t(beginLine);
LOG->Warn( "Unmatched 3 in \"%.*s\"", n, beginLine );
}
else
{
out.FindTapNote( iTrack, iHeadRow )->second.iDuration = iIndex - iHeadRow;
}
// This won't write tn, but keep parsing normally anyway.
break;
}
// case 'm':
// Don't be loose with the definition. Use only 'M' since
// that's what we've been writing to disk. -Chris
case 'M': tn = TAP_ORIGINAL_MINE; break;
// case 'A': tn = TAP_ORIGINAL_ATTACK; break;
case 'K': tn = TAP_ORIGINAL_AUTO_KEYSOUND; break;
case 'L': tn = TAP_ORIGINAL_LIFT; break;
case 'F': tn = TAP_ORIGINAL_FAKE; break;
// case 'I': tn = TAP_ORIGINAL_ITEM; break;
default:
/* Invalid data. We don't want to assert, since there might
* simply be invalid data in an .SM, and we don't want to die
* due to invalid data. We should probably check for this when
* we load SM data for the first time ... */
// FAIL_M("Invalid data in SM");
tn = TAP_EMPTY;
break;
}
p++;
// We won't scan past the end of the line so these are safe to do.
#if 0
// look for optional attack info (e.g. "{tipsy,50% drunk:15.2}")
if( *p == '{' )
{
p++;
char szModifiers[256] = "";
float fDurationSeconds = 0;
if( sscanf( p, "%255[^:]:%f}", szModifiers, &fDurationSeconds ) == 2 ) // not fatal if this fails due to malformed data
{
tn.type = TapNoteType_Attack;
tn.sAttackModifiers = szModifiers;
tn.fAttackDurationSeconds = fDurationSeconds;
}
// skip past the '}'
while( p < endLine )
{
if( *(p++) == '}' )
{
break;
}
}
}
#endif
// look for optional keysound index (e.g. "[123]")
if( *p == '[' )
{
p++;
int iKeysoundIndex = 0;
if( 1 == sscanf( p, "%d]", &iKeysoundIndex ) ) // not fatal if this fails due to malformed data
tn.iKeysoundIndex = iKeysoundIndex;
// skip past the ']'
while( p < endLine )
{
if( *(p++) == ']' )
{
break;
}
}
}
#if 0
// look for optional item name (e.g. "<potion>"),
// where the name in the <> is a Lua function defined elsewhere
// (Data/ItemTypes.lua, perhaps?) -aj
if( *p == '<' )
{
p++;
// skip past the '>'
while( p < endLine )
{
if( *(p++) == '>' )
{
break;
}
}
}
#endif
/* Optimization: if we pass TAP_EMPTY, NoteData will do a search
* to remove anything in this position. We know that there's nothing
* there, so avoid the search. */
if( tn.type != TapNoteType_Empty && ch != '3' )
{
tn.pn = pn;
out.SetTapNote( iTrack, iIndex, tn );
}
iTrack++;
}
}
}
// Make sure we don't have any hold notes that didn't find a tail.
for( int t=0; t<out.GetNumTracks(); t++ )
{
NoteData::iterator begin = out.begin( t );
NoteData::iterator lEnd = out.end( t );
while( begin != lEnd )
{
NoteData::iterator next = Increment( begin );
const TapNote &tn = begin->second;
if( tn.type == TapNoteType_HoldHead && tn.iDuration == MAX_NOTE_ROW )
{
int iRow = begin->first;
LOG->UserLog( "", "", "While loading .sm/.ssc note data, there was an unmatched 2 at beat %f", NoteRowToBeat(iRow) );
out.RemoveTapNote( t, begin );
}
begin = next;
}
}
out.RevalidateATIs(vector<int>(), false);
}
void NoteDataUtil::LoadFromSMNoteDataString( NoteData &out, const std::string &sSMNoteData_, bool bComposite )
{
// Load note data
std::string sSMNoteData;
std::string::size_type iIndexCommentStart = 0;
std::string::size_type iIndexCommentEnd = 0;
std::string::size_type origSize = sSMNoteData_.size();
const char *p = sSMNoteData_.data();
sSMNoteData.reserve( origSize );
while( (iIndexCommentStart = sSMNoteData_.find("//", iIndexCommentEnd)) != std::string::npos )
{
sSMNoteData.append( p, iIndexCommentStart - iIndexCommentEnd );
p += iIndexCommentStart - iIndexCommentEnd;
iIndexCommentEnd = sSMNoteData_.find( "\n", iIndexCommentStart );
iIndexCommentEnd = (iIndexCommentEnd == std::string::npos ? origSize : iIndexCommentEnd+1);
p += iIndexCommentEnd - iIndexCommentStart;
}
sSMNoteData.append( p, origSize - iIndexCommentEnd );
// Clear notes, but keep the same number of tracks.
int iNumTracks = out.GetNumTracks();
out.Init();
out.SetNumTracks( iNumTracks );
if( !bComposite )
{
LoadFromSMNoteDataStringWithPlayer( out, sSMNoteData, 0, sSMNoteData.size(),
PLAYER_INVALID, iNumTracks );
return;
}
int start = 0, size = -1;
vector<NoteData> vParts;
FOREACH_PlayerNumber( pn )
{
Rage::split_in_place( sSMNoteData, "&", start, size, Rage::EmptyEntries::include );
if( unsigned(start) == sSMNoteData.size() )
break;
vParts.push_back( NoteData() );
NoteData &nd = vParts.back();
nd.SetNumTracks( iNumTracks );
LoadFromSMNoteDataStringWithPlayer( nd, sSMNoteData, start, size, pn, iNumTracks );
}
CombineCompositeNoteData( out, vParts );
out.RevalidateATIs(vector<int>(), false);
}
void NoteDataUtil::InsertHoldTails( NoteData &inout )
{
for( int t=0; t < inout.GetNumTracks(); t++ )
{
NoteData::iterator begin = inout.begin(t), end = inout.end(t);
for( ; begin != end; ++begin )
{
int iRow = begin->first;
const TapNote &tn = begin->second;
if( tn.type != TapNoteType_HoldHead )
continue;
TapNote tail = tn;
tail.type = TapNoteType_HoldTail;
/* If iDuration is 0, we'd end up overwriting the head with the tail
* (and invalidating our iterator). Empty hold notes aren't valid. */
ASSERT( tn.iDuration != 0 );
inout.SetTapNote( t, iRow + tn.iDuration, tail );
}
}
}
void NoteDataUtil::GetSMNoteDataString( const NoteData &in, std::string &sRet )
{
using std::max;
// Get note data
vector<NoteData> parts;
float fLastBeat = -1.0f;
SplitCompositeNoteData( in, parts );
for (auto &nd: parts)
{
InsertHoldTails( nd );
fLastBeat = max( fLastBeat, nd.GetLastBeat() );
}
int iLastMeasure = int( fLastBeat/BEATS_PER_MEASURE );
sRet = "";
for (auto nd = parts.begin(); nd != parts.end(); ++nd)
{
if( nd != parts.begin() )
sRet.append( "&\n" );
for( int m = 0; m <= iLastMeasure; ++m ) // foreach measure
{
if( m )
sRet.append( 1, ',' );
sRet += fmt::sprintf(" // measure %d\n", m);
NoteType nt = GetSmallestNoteTypeForMeasure( *nd, m );
int iRowSpacing;
if( nt == NoteType_Invalid )
iRowSpacing = 1;
else
iRowSpacing = std::lrint( NoteTypeToBeat(nt) * ROWS_PER_BEAT );
// (verify first)
// iRowSpacing = BeatToNoteRow( NoteTypeToBeat(nt) );
const int iMeasureStartRow = m * ROWS_PER_MEASURE;
const int iMeasureLastRow = (m+1) * ROWS_PER_MEASURE - 1;
for( int r=iMeasureStartRow; r<=iMeasureLastRow; r+=iRowSpacing )
{
for( int t = 0; t < nd->GetNumTracks(); ++t )
{
const TapNote &tn = nd->GetTapNote(t, r);
char c;
switch( tn.type )
{
case TapNoteType_Empty: c = '0'; break;
case TapNoteType_Tap: c = '1'; break;
case TapNoteType_HoldHead:
switch( tn.subType )
{
case TapNoteSubType_Hold: c = '2'; break;
case TapNoteSubType_Roll: c = '4'; break;
//case TapNoteSubType_Mine: c = 'N'; break;
default:
FAIL_M(fmt::sprintf("Invalid tap note subtype: %i", tn.subType));
}
break;
case TapNoteType_HoldTail: c = '3'; break;
case TapNoteType_Mine: c = 'M'; break;
case TapNoteType_Attack: c = 'A'; break;
case TapNoteType_AutoKeysound: c = 'K'; break;
case TapNoteType_Lift: c = 'L'; break;
case TapNoteType_Fake: c = 'F'; break;
default:
c = '\0';
FAIL_M(fmt::sprintf("Invalid tap note type: %i", tn.type));
}
sRet.append( 1, c );
if( tn.type == TapNoteType_Attack )
{
sRet.append( fmt::sprintf("{%s:%.2f}", tn.sAttackModifiers.c_str(),
tn.fAttackDurationSeconds) );
}
// hey maybe if we have TapNoteType_Item we can do things here.
if( tn.iKeysoundIndex >= 0 )
sRet.append( fmt::sprintf("[%d]",tn.iKeysoundIndex) );
}
sRet.append( 1, '\n' );
}
}
}
}
void NoteDataUtil::SplitCompositeNoteData( const NoteData &in, vector<NoteData> &out )
{
if( !in.IsComposite() )
{
out.push_back( in );
return;
}
FOREACH_PlayerNumber( pn )
{
out.push_back( NoteData() );
out.back().SetNumTracks( in.GetNumTracks() );
}
for( int t = 0; t < in.GetNumTracks(); ++t )
{
for( NoteData::const_iterator iter = in.begin(t); iter != in.end(t); ++iter )
{
int row = iter->first;
TapNote tn = iter->second;
/*
XXX: This code is (hopefully) a temporary hack to make sure that
routine charts don't have any notes without players assigned to them.
I suspect this is due to a related bug that these problems were
occuring to begin with, but at this time, I am unsure how to deal with it.
Hopefully this hack can be removed soon. -- Jason "Wolfman2000" Felds
*/
const Style *curStyle = GAMESTATE->GetCurrentStyle(PLAYER_INVALID);
if( (curStyle == nullptr || curStyle->m_StyleType == StyleType_TwoPlayersSharedSides )
&& int( tn.pn ) > NUM_PlayerNumber )
{
tn.pn = PLAYER_1;
}
unsigned index = int( tn.pn );
ASSERT_M( index < NUM_PlayerNumber, fmt::sprintf("We have a note not assigned to a player. The note in question is on beat %f, column %i.", NoteRowToBeat(row), t + 1) );
tn.pn = PLAYER_INVALID;
out[index].SetTapNote( t, row, tn );
}
}
}
void NoteDataUtil::CombineCompositeNoteData( NoteData &out, const vector<NoteData> &in )
{
using std::min;
for (auto &nd: in)
{
const int iMaxTracks = min( out.GetNumTracks(), nd.GetNumTracks() );
for( int track = 0; track < iMaxTracks; ++track )
{
for( auto i = nd.begin(track); i != nd.end(track); ++i )
{
int row = i->first;
if( out.IsHoldNoteAtRow(track, i->first) )
continue;
if( i->second.type == TapNoteType_HoldHead )
out.AddHoldNote( track, row, row + i->second.iDuration, i->second );
else
out.SetTapNote( track, row, i->second );
}
}
}
out.RevalidateATIs(vector<int>(), false);
}
void NoteDataUtil::LoadTransformedSlidingWindow( const NoteData &in, NoteData &out, int iNewNumTracks )
{
// reset all notes
out.Init();
if( in.GetNumTracks() > iNewNumTracks )
{
// Use a different algorithm for reducing tracks.
LoadOverlapped( in, out, iNewNumTracks );
return;
}
out.SetNumTracks( iNewNumTracks );
if( in.GetNumTracks() == 0 )
return; // nothing to do and don't AV below
int iCurTrackOffset = 0;
int iTrackOffsetMin = 0;
int iTrackOffsetMax = abs( iNewNumTracks - in.GetNumTracks() );
int bOffsetIncreasing = true;
int iLastMeasure = 0;
int iMeasuresSinceChange = 0;
FOREACH_NONEMPTY_ROW_ALL_TRACKS( in, r )
{
const int iMeasure = r / ROWS_PER_MEASURE;
if( iMeasure != iLastMeasure )
++iMeasuresSinceChange;
if( iMeasure != iLastMeasure && iMeasuresSinceChange >= 4 ) // adjust sliding window every 4 measures at most
{
// See if there is a hold crossing the beginning of this measure
bool bHoldCrossesThisMeasure = false;
for( int t=0; t<in.GetNumTracks(); t++ )
{
if( in.IsHoldNoteAtRow( t, r-1 ) &&
in.IsHoldNoteAtRow( t, r ) )
{
bHoldCrossesThisMeasure = true;
break;
}
}
// adjust offset
if( !bHoldCrossesThisMeasure )
{
iMeasuresSinceChange = 0;
iCurTrackOffset += bOffsetIncreasing ? 1 : -1;
if( iCurTrackOffset == iTrackOffsetMin || iCurTrackOffset == iTrackOffsetMax )
bOffsetIncreasing ^= true;
iCurTrackOffset = Rage::clamp( iCurTrackOffset, iTrackOffsetMin, iTrackOffsetMax );
}
}
iLastMeasure = iMeasure;
// copy notes in this measure
for( int t=0; t<in.GetNumTracks(); t++ )
{
int iOldTrack = t;
int iNewTrack = (iOldTrack + iCurTrackOffset) % iNewNumTracks;
TapNote tn = in.GetTapNote( iOldTrack, r );
tn.pn= PLAYER_INVALID;
out.SetTapNote( iNewTrack, r, tn );
}
}
out.RevalidateATIs(vector<int>(), false);
}
void PlaceAutoKeysound( NoteData &out, int row, TapNote akTap )
{
int iEmptyTrack = -1;
int iEmptyRow = row;
int iNewNumTracks = out.GetNumTracks();
bool bFoundEmptyTrack = false;
int iRowsToLook[3] = {0, -1, 1};
for( int j = 0; j < 3; j ++ )
{
int r = iRowsToLook[j] + row;
if( r < 0 )
continue;
for( int i = 0; i < iNewNumTracks; ++i )
{
if ( out.GetTapNote(i, r) == TAP_EMPTY && !out.IsHoldNoteAtRow(i, r) )
{
iEmptyTrack = i;
iEmptyRow = r;
bFoundEmptyTrack = true;
break;
}
}
if( bFoundEmptyTrack )
break;
}
if( iEmptyTrack != -1 )
{
akTap.type = TapNoteType_AutoKeysound;
out.SetTapNote( iEmptyTrack, iEmptyRow, akTap );
}
}
void NoteDataUtil::LoadOverlapped( const NoteData &in, NoteData &out, int iNewNumTracks )
{
out.SetNumTracks( iNewNumTracks );
/* Keep track of the last source track that put a tap into each destination track,
* and the row of that tap. Then, if two rows are trying to put taps into the
* same row within the shift threshold, shift the newcomer source row. */
int LastSourceTrack[MAX_NOTE_TRACKS];
int LastSourceRow[MAX_NOTE_TRACKS];
int DestRow[MAX_NOTE_TRACKS];
for( int tr = 0; tr < MAX_NOTE_TRACKS; ++tr )
{
LastSourceTrack[tr] = -1;
LastSourceRow[tr] = -MAX_NOTE_ROW;
DestRow[tr] = tr;
wrap( DestRow[tr], iNewNumTracks );
}
const int ShiftThreshold = BeatToNoteRow(1);
FOREACH_NONEMPTY_ROW_ALL_TRACKS( in, row )
{
for( int iTrackFrom = 0; iTrackFrom < in.GetNumTracks(); ++iTrackFrom )
{
TapNote tnFrom = in.GetTapNote( iTrackFrom, row );
if( tnFrom.type == TapNoteType_Empty || tnFrom.type == TapNoteType_AutoKeysound )
continue;
tnFrom.pn= PLAYER_INVALID;
// If this is a hold note, find the end.
int iEndIndex = row;
if( tnFrom.type == TapNoteType_HoldHead )
iEndIndex = row + tnFrom.iDuration;
int &iTrackTo = DestRow[iTrackFrom];
if( LastSourceTrack[iTrackTo] != iTrackFrom )
{
if( iEndIndex - LastSourceRow[iTrackTo] < ShiftThreshold )
{
/* This destination track is in use by a different source
* track. Use the least-recently-used track. */
for( int DestTrack = 0; DestTrack < iNewNumTracks; ++DestTrack )
if( LastSourceRow[DestTrack] < LastSourceRow[iTrackTo] )
iTrackTo = DestTrack;
}
// If it's still in use, then we just don't have an available track.
if( iEndIndex - LastSourceRow[iTrackTo] < ShiftThreshold )
{
// If it has a keysound, put it in autokeysound track.
if( tnFrom.iKeysoundIndex >= 0 )
{
TapNote akTap = tnFrom;
PlaceAutoKeysound( out, row, akTap );
}
continue;
}
}
LastSourceTrack[iTrackTo] = iTrackFrom;
LastSourceRow[iTrackTo] = iEndIndex;
out.SetTapNote( iTrackTo, row, tnFrom );
if( tnFrom.type == TapNoteType_HoldHead )
{
const TapNote &tnTail = in.GetTapNote( iTrackFrom, iEndIndex );
out.SetTapNote( iTrackTo, iEndIndex, tnTail );
}
}
// find empty track for autokeysounds in 2 next rows, so you can hear most autokeysounds
for( int iTrackFrom = 0; iTrackFrom < in.GetNumTracks(); ++iTrackFrom )
{
const TapNote &tnFrom = in.GetTapNote( iTrackFrom, row );
if( tnFrom.type != TapNoteType_AutoKeysound )
continue;
PlaceAutoKeysound( out, row, tnFrom );
}
}
out.RevalidateATIs(vector<int>(), false);
}
int FindLongestOverlappingHoldNoteForAnyTrack( const NoteData &in, int iRow )
{
using std::max;
int iMaxTailRow = -1;
for( int t=0; t<in.GetNumTracks(); t++ )
{
const TapNote &tn = in.GetTapNote( t, iRow );
if( tn.type == TapNoteType_HoldHead )
iMaxTailRow = max( iMaxTailRow, iRow + tn.iDuration );
}
return iMaxTailRow;
}
// For every row in "in" with a tap or hold on any track, enable the specified tracks in "out".
void LightTransformHelper( const NoteData &in, NoteData &out, const vector<int> &aiTracks )
{
for (auto &track: aiTracks)
{
ASSERT_M( track < out.GetNumTracks(), fmt::sprintf("%i, %i", track, out.GetNumTracks()) );
}
FOREACH_NONEMPTY_ROW_ALL_TRACKS( in, r )
{
/* If any row starts a hold note, find the end of the hold note, and keep searching
* until we've extended to the end of the latest overlapping hold note. */
int iHoldStart = r;
int iHoldEnd = -1;
for(;;)
{
int iMaxTailRow = FindLongestOverlappingHoldNoteForAnyTrack( in, r );
if( iMaxTailRow == -1 )
{
break;
}
iHoldEnd = iMaxTailRow;
r = iMaxTailRow;
}
if( iHoldEnd != -1 )
{
// If we found a hold note, add it to all tracks.
for (auto t: aiTracks)
{
out.AddHoldNote( t, iHoldStart, iHoldEnd, TAP_ORIGINAL_HOLD_HEAD );
}
continue;
}
if( in.IsRowEmpty(r) )
continue;
// Enable every track in the output.
for (auto t: aiTracks)
{
out.SetTapNote( t, r, TAP_ORIGINAL_TAP );
}
}
}
// For every track enabled in "in", enable all tracks in "out".
void NoteDataUtil::LoadTransformedLights( const NoteData &in, NoteData &out, int iNewNumTracks )
{
// reset all notes
out.Init();
out.SetNumTracks( iNewNumTracks );
vector<int> aiTracks;
for( int i = 0; i < out.GetNumTracks(); ++i )
aiTracks.push_back( i );
LightTransformHelper( in, out, aiTracks );
}
// This transform is specific to StepsType_lights_cabinet.
#include "LightsManager.h" // for LIGHT_*
void NoteDataUtil::LoadTransformedLightsFromTwo( const NoteData &marquee, const NoteData &bass, NoteData &out )
{
ASSERT( marquee.GetNumTracks() >= 4 );
ASSERT( bass.GetNumTracks() >= 1 );
/* For each track in "marquee", enable a track in the marquee lights.
* This will reinit out. */
{
NoteData transformed_marquee;
transformed_marquee.CopyAll( marquee );
Wide( transformed_marquee );
const int iOriginalTrackToTakeFrom[NUM_CabinetLight] = { 0, 1, 2, 3, -1, -1 };
out.LoadTransformed( transformed_marquee, NUM_CabinetLight, iOriginalTrackToTakeFrom );
}
// For each track in "bass", enable the bass lights.
{
vector<int> aiTracks;
aiTracks.push_back( LIGHT_BASS_LEFT );
aiTracks.push_back( LIGHT_BASS_RIGHT );
LightTransformHelper( bass, out, aiTracks );
}
// Delete all mines.
NoteDataUtil::RemoveMines( out );
}
// This kickbox_limb enum should not be used anywhere outside the
// Autogenkickbox function. -Kyz
enum kickbox_limb
{
left_foot, left_fist, right_fist, right_foot, num_kickbox_limbs, invalid_limb= -1
};
void NoteDataUtil::AutogenKickbox(const NoteData& in, NoteData& out, const TimingData& timing, StepsType out_type, int nonrandom_seed)
{
// Each limb has its own list of tracks it is used for. This allows
// abstract handling of the different styles.
// By convention, the lower panels are pushed first. This gives the upper
// panels a higher index, which is mnemonically useful.
vector<vector<int> > limb_tracks(num_kickbox_limbs);
bool have_feet= true;
switch(out_type)
{
case StepsType_kickbox_human:
out.SetNumTracks(4);
limb_tracks[left_foot].push_back(0);
limb_tracks[left_fist].push_back(1);
limb_tracks[right_fist].push_back(2);
limb_tracks[right_foot].push_back(3);
break;
case StepsType_kickbox_quadarm:
out.SetNumTracks(4);
have_feet= false;
limb_tracks[left_fist].push_back(1);
limb_tracks[left_fist].push_back(0);
limb_tracks[right_fist].push_back(2);
limb_tracks[right_fist].push_back(3);
break;
case StepsType_kickbox_insect:
out.SetNumTracks(6);
limb_tracks[left_foot].push_back(0);
limb_tracks[left_fist].push_back(2);
limb_tracks[left_fist].push_back(1);
limb_tracks[right_fist].push_back(3);
limb_tracks[right_fist].push_back(4);
limb_tracks[right_foot].push_back(5);
break;
case StepsType_kickbox_arachnid:
out.SetNumTracks(8);
limb_tracks[left_foot].push_back(0);
limb_tracks[left_foot].push_back(1);
limb_tracks[left_fist].push_back(3);
limb_tracks[left_fist].push_back(2);
limb_tracks[right_fist].push_back(4);
limb_tracks[right_fist].push_back(5);
limb_tracks[right_foot].push_back(7);
limb_tracks[right_foot].push_back(6);
break;
DEFAULT_FAIL(out_type);
}
// prev_limb_panels keeps track of which panel in the track list the limb
// hit last.
vector<size_t> prev_limb_panels(num_kickbox_limbs, 0);
vector<int> panel_repeat_counts(num_kickbox_limbs, 0);
vector<int> panel_repeat_goals(num_kickbox_limbs, 0);
RandomGen rnd(nonrandom_seed);
kickbox_limb prev_limb_used= invalid_limb;
// Kicks are only allowed if there is enough setup/recovery time.
float kick_recover_time= GAMESTATE->GetAutoGenFarg(0);
if(kick_recover_time <= 0.0f)
{
kick_recover_time= .25f;
}
float prev_note_time= -1.0f;
int rows_done= 0;
#define RAND_FIST ((rnd() % 2) ? left_fist : right_fist)
#define RAND_FOOT ((rnd() % 2) ? left_foot : right_foot)
FOREACH_NONEMPTY_ROW_ALL_TRACKS(in, r)
{
// Arbitrary: Drop everything except taps and hold heads out entirely,
// convert holds to tap just the head.
bool has_valid_tapnote= false;
for(int t= 0; t < in.GetNumTracks(); ++t)
{
const TapNote& tn= in.GetTapNote(t, r);
if(tn.type == TapNoteType_Tap || tn.type == TapNoteType_HoldHead)
{
has_valid_tapnote= true;
break;
}
}
if(!has_valid_tapnote) { continue; }
int next_row= r;
bool next_has_valid= false;
while(!next_has_valid)
{
if(!in.GetNextTapNoteRowForAllTracks(next_row))
{
next_row= -1;
next_has_valid= true;
}
else
{
for(int t= 0; t < in.GetNumTracks(); ++t)
{
const TapNote& tn= in.GetTapNote(t, next_row);
if(tn.type == TapNoteType_Tap || tn.type == TapNoteType_HoldHead)
{
next_has_valid= true;
break;
}
}
}
}
float this_note_time= timing.GetElapsedTimeFromBeat(NoteRowToBeat(r));
float next_note_time= timing.GetElapsedTimeFromBeat(NoteRowToBeat(next_row));
kickbox_limb this_limb= invalid_limb;
switch(prev_limb_used)
{
case invalid_limb:
// First limb is arbitrarily always a fist.
this_limb= RAND_FIST;
break;
case left_foot:
case right_foot:
// Multiple kicks in a row are allowed if they're on the same foot.
// Allow the last note to be a kick.
// Switch feet if there's enough time.
if(next_note_time - this_note_time > kick_recover_time * 2.0f)
{
this_limb= prev_limb_used == left_foot ? right_foot : left_foot;
}
else if((next_note_time - this_note_time > kick_recover_time * .5f ||
next_note_time < 0.0f) && (rnd() % 2))
{
this_limb= prev_limb_used;
}
else
{
this_limb= RAND_FIST;
}
break;
case left_fist:
case right_fist:
if(this_note_time - prev_note_time > kick_recover_time &&
(next_note_time - this_note_time > kick_recover_time ||
next_note_time < 0.0f) && have_feet)
{
this_limb= RAND_FOOT;
}
else
{
// Alternate fists.
this_limb= prev_limb_used == left_fist ? right_fist : left_fist;
}
break;
default:
break;
}
size_t this_panel= prev_limb_panels[this_limb];
if(panel_repeat_counts[this_limb] + 1 > panel_repeat_goals[this_limb])
{
// Use a different panel.
this_panel= (this_panel + 1) % limb_tracks[this_limb].size();
panel_repeat_counts[this_limb]= 0;
panel_repeat_goals[this_limb]= (rnd() % 8) + 1;
}
out.SetTapNote(limb_tracks[this_limb][this_panel], r, TAP_ORIGINAL_TAP);
++panel_repeat_counts[this_limb];
prev_limb_panels[this_limb]= this_panel;
prev_note_time= this_note_time;
prev_limb_used= this_limb;
++rows_done;
}
out.RevalidateATIs(vector<int>(), false);
}
struct recent_note
{
int row;
int track;
recent_note()
:row(0), track(0) {}
recent_note(int r, int t)
:row(r), track(t) {}
};
// CalculateRadarValues has to delay some stuff until a row ends, but can
// only detect a row ending when it hits the next note. There isn't a note
// after the last row, so it also has to do the delayed stuff after exiting
// its loop. So this state structure exists to be passed to a function that
// can be called from both places to do the work. If this were Lua,
// DoRowEndRadarCalc would be a nested function. -Kyz
struct crv_state
{
bool judgable;
// hold_ends tracks where currently active holds will end, which is used
// to count the number of hands. -Kyz
vector<int> hold_ends;
// num_holds_on_curr_row saves us the work of tracking where holds started
// just to keep a jump of two holds from counting as a hand.
int num_holds_on_curr_row;