Skip to content

Commit

Permalink
[ENHANCEMENT] A better way to limit variations for characters
Browse files Browse the repository at this point in the history
  • Loading branch information
AppleHair authored and charlesisfeline committed Jul 5, 2024
1 parent 70cf389 commit 820d0bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
5 changes: 5 additions & 0 deletions source/funkin/data/song/SongData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class SongMetadata implements ICloneable<SongMetadata>

public var timeChanges:Array<SongTimeChange>;

@:optional
@:default(funkin.util.Constants.DEFAULT_CHARACTER)
public var campaignCharacter:String;

/**
* Defaults to `Constants.DEFAULT_VARIATION`. Populated later.
*/
Expand All @@ -86,6 +90,7 @@ class SongMetadata implements ICloneable<SongMetadata>
this.playData.stage = 'mainStage';
this.playData.noteStyle = Constants.DEFAULT_NOTE_STYLE;
this.generatedBy = SongRegistry.DEFAULT_GENERATEDBY;
this.campaignCharacter = Constants.DEFAULT_CHARACTER;
// Variation ID.
this.variation = (variation == null) ? Constants.DEFAULT_VARIATION : variation;
}
Expand Down
30 changes: 19 additions & 11 deletions source/funkin/play/song/Song.hx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class Song implements IPlayStateScriptedClass implements IRegistryEntry<SongMeta
// key = variation id, value = metadata
final _metadata:Map<String, SongMetadata>;
final difficulties:Map<String, SongDifficulty>;
// key = character id, value = allowed variation ids
final charVariations:Map<String, Array<String>>;

/**
* The list of variations a song has.
Expand Down Expand Up @@ -150,7 +152,13 @@ class Song implements IPlayStateScriptedClass implements IRegistryEntry<SongMeta

_data = _fetchData(id);

_metadata = _data == null ? [] : [Constants.DEFAULT_VARIATION => _data];
_metadata = new Map<String, SongMetadata>();
charVariations = new Map<String, Array<String>>();
if (_data != null)
{
_metadata.set(Constants.DEFAULT_VARIATION, _data);
charVariations.set(_data.campaignCharacter, [Constants.DEFAULT_VARIATION]);
}

if (_data != null && _data.playData != null)
{
Expand All @@ -160,6 +168,15 @@ class Song implements IPlayStateScriptedClass implements IRegistryEntry<SongMeta
if (variMeta != null)
{
_metadata.set(variMeta.variation, variMeta);
var variChar:Null<Array<String>> = charVariations.get(variMeta.campaignCharacter);
if (variChar != null)
{
variChar.push(variMeta.variation);
}
else
{
charVariations.set(variMeta.campaignCharacter, [variMeta.variation]);
}
trace(' Loaded variation: $vari');
}
else
Expand Down Expand Up @@ -428,16 +445,7 @@ class Song implements IPlayStateScriptedClass implements IRegistryEntry<SongMeta
public function getVariationsByCharId(?charId:String):Array<String>
{
if (charId == null) charId = Constants.DEFAULT_CHARACTER;

if (variations.contains(charId))
{
return [charId];
}
else
{
// TODO: How to exclude character variations while keeping other custom variations?
return variations;
}
return charVariations.get(charId) ?? [];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions source/funkin/ui/freeplay/FreeplayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,7 @@ class FreeplayState extends MusicBeatSubState
FlxG.log.warn('WARN: could not find song with id (${grpCapsules.members[curSelected].songData.songId})');
return;
}
var targetVariation:String = targetSong.getFirstValidVariation(currentDifficulty);
var targetVariation:String = targetSong.getFirstValidVariation(currentDifficulty, targetSong.getVariationsByCharId(currentCharacter));

// TODO: This line of code makes me sad, but you can't really fix it without a breaking migration.
var suffixedDifficulty = (targetVariation != Constants.DEFAULT_VARIATION
Expand Down Expand Up @@ -1717,7 +1717,7 @@ class FreeplayState extends MusicBeatSubState
return;
}
var targetDifficultyId:String = currentDifficulty;
var targetVariation:String = targetSong.getFirstValidVariation(targetDifficultyId);
var targetVariation:String = targetSong.getFirstValidVariation(targetDifficultyId, targetSong.getVariationsByCharId(currentCharacter));
PlayStatePlaylist.campaignId = cap.songData.levelId;

var targetDifficulty:SongDifficulty = targetSong.getDifficulty(targetDifficultyId, targetVariation);
Expand Down

0 comments on commit 820d0bc

Please sign in to comment.