Skip to content

Commit

Permalink
Fix ReplaySnapshot issue with returning garbage values
Browse files Browse the repository at this point in the history
returning a pointer to a local variable is a bad idea, this should work better
  • Loading branch information
poco0317 committed Nov 7, 2019
1 parent d7e77d3 commit 075b994
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
40 changes: 21 additions & 19 deletions src/Etterna/Models/Misc/PlayerAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ PlayerAI::SetUpSnapshotMap(NoteData* pNoteData, set<int> validNoterows)

continue; // retry the iteration (it++ is moved to below)
}
ReplaySnapshot* rs = GetReplaySnapshotForNoterow(r);
auto rs = GetReplaySnapshotForNoterow(r);
for (auto& trr : it->second) {
if (trr.type == TapNoteType_Mine) {
cws -= 8.f;
Expand All @@ -571,7 +571,7 @@ PlayerAI::SetUpSnapshotMap(NoteData* pNoteData, set<int> validNoterows)
// which has no stat-affecting changes made to it.
// So this applies to rows with all Mines
// or rows with all Fakes (in the latest version)
ReplaySnapshot* prevrs = GetReplaySnapshotForNoterow(row - 1);
auto prevrs = GetReplaySnapshotForNoterow(row - 1);
ReplaySnapshot* rs = &m_ReplaySnapshotMap[row];
rs->curwifescore = prevrs->curwifescore;
rs->maxwifescore = prevrs->maxwifescore;
Expand Down Expand Up @@ -625,7 +625,7 @@ PlayerAI::GetAdjustedRowFromUnadjustedCoordinates(int row, int col)
return output;
}

ReplaySnapshot*
std::shared_ptr<ReplaySnapshot>
PlayerAI::GetReplaySnapshotForNoterow(int row)
{
// The row doesn't necessarily have to exist in the Snapshot map.
Expand All @@ -636,15 +636,16 @@ PlayerAI::GetReplaySnapshotForNoterow(int row)
// If the lowest value in the map is above the given row, return an empty
// snapshot
if (m_ReplaySnapshotMap.begin()->first > row) {
ReplaySnapshot rs;
return &rs;
return std::shared_ptr<ReplaySnapshot>{ new ReplaySnapshot };
}

// For some reason I don't feel like figuring out, if the largest value in
// the map is below the given row, it returns 0 So we need to return the
// last value
if (m_ReplaySnapshotMap.rbegin()->first < row) {
return &m_ReplaySnapshotMap.rbegin()->second;
return std::shared_ptr<ReplaySnapshot>{
&m_ReplaySnapshotMap.rbegin()->second, [](ReplaySnapshot*) {}
};
}

// Otherwise just go ahead and return what we want
Expand All @@ -659,11 +660,11 @@ PlayerAI::GetReplaySnapshotForNoterow(int row)
lb--;
foundRow = lb->first;
} else {
ReplaySnapshot rs;
return &rs;
return std::shared_ptr<ReplaySnapshot>{ new ReplaySnapshot };
}
}
return &m_ReplaySnapshotMap[foundRow];
return std::shared_ptr<ReplaySnapshot>{ &m_ReplaySnapshotMap[foundRow],
[](ReplaySnapshot*) {} };
}

bool
Expand Down Expand Up @@ -828,8 +829,8 @@ PlayerAI::GetNextRowNoOffsets(int currentRow)
float
PlayerAI::GetTapNoteOffsetForReplay(TapNote* pTN, int noteRow, int col)
{
/* Given the pTN coming from gameplay, we search for the matching note in
the replay data. If it is not found, it is a miss. (1.f)
/* Given the pTN coming from gameplay, we search for the matching note
in the replay data. If it is not found, it is a miss. (1.f)
*/
if (pScoreData == nullptr) // possible cheat prevention
return -1.f;
Expand Down Expand Up @@ -873,8 +874,8 @@ PlayerAI::GetTapNoteOffsetForReplay(TapNote* pTN, int noteRow, int col)
i++) // go over all elements in the row
{
auto trr = m_ReplayExactTapMap[noteRow][i];
if (trr.track ==
col) // if the column expected is the actual note, use it
if (trr.track == col) // if the column expected is the
// actual note, use it
{
if (trr.type == TapNoteType_Mine) // hack for mines
return -2.f;
Expand All @@ -899,8 +900,8 @@ void
PlayerAI::CalculateRadarValuesForReplay(RadarValues& rv,
RadarValues& possibleRV)
{
// We will do this thoroughly just in case someone decides to use the other
// categories we don't currently use
// We will do this thoroughly just in case someone decides to use the
// other categories we don't currently use
int tapsHit = 0;
int jumpsHit = 0;
int handsHit = 0;
Expand Down Expand Up @@ -977,8 +978,8 @@ void
PlayerAI::SetPlayerStageStatsForReplay(PlayerStageStats* pss)
{
// Radar values.
// The possible radar values have already been handled, so we just do the
// real values.
// The possible radar values have already been handled, so we just do
// the real values.
RadarValues rrv;
CalculateRadarValuesForReplay(rrv, pss->m_radarPossible);
pss->m_radarActual.Zero();
Expand Down Expand Up @@ -1032,7 +1033,7 @@ PlayerAI::GetWifeScoreForRow(int row, float ts)
}

// Take into account dropped holds and full misses
ReplaySnapshot* rs = GetReplaySnapshotForNoterow(row);
auto rs = GetReplaySnapshotForNoterow(row);
out.first += rs->judgments[TNS_Miss] * -8.f;
out.first += rs->hns[HNS_LetGo] * -8.f;
out.second += rs->judgments[TNS_Miss] * 2.f;
Expand Down Expand Up @@ -1119,7 +1120,8 @@ PlayerAI::GenerateComboListForReplay(float timingScale)
PlayerStageStats::Combo_t* curCombo = &(combos[0]);
auto rowOfComboStart = m_ReplayTapMapByElapsedTime.begin();

// Go over all chronological tap rows (only taps should accumulate combo)
// Go over all chronological tap rows (only taps should accumulate
// combo)
for (auto tapIter = m_ReplayTapMapByElapsedTime.begin();
tapIter != m_ReplayTapMapByElapsedTime.end();
tapIter++) {
Expand Down
2 changes: 1 addition & 1 deletion src/Etterna/Models/Misc/PlayerAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class PlayerAI
// Given a column and row, retrieve the adjusted row.
static int GetAdjustedRowFromUnadjustedCoordinates(int row, int col);
// Given a row, retrieve the Snapshot for that row.
static ReplaySnapshot* GetReplaySnapshotForNoterow(int row);
static std::shared_ptr<ReplaySnapshot> GetReplaySnapshotForNoterow(int row);
// Remove a given Tap from the fallback and Full replay data vectors
static void RemoveTapFromVectors(int row, int col);
// Go through the replay data to fill out the radar values for the eval
Expand Down
4 changes: 2 additions & 2 deletions src/Etterna/Screen/Gameplay/ScreenGameplayReplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ ScreenGameplayReplay::SetSongPosition(float newPositionSeconds)
const int rowNow = BeatToNoteRow(fSongBeat);
// This breaks some oop standard in some book
PlayerStageStats* pss = m_vPlayerInfo.GetPlayerStageStats();
ReplaySnapshot* rs = PlayerAI::GetReplaySnapshotForNoterow(rowNow);
auto rs = PlayerAI::GetReplaySnapshotForNoterow(rowNow);
FOREACH_ENUM(TapNoteScore, tns)
{
pss->m_iTapNoteScores[tns] = rs->judgments[tns];
Expand Down Expand Up @@ -410,7 +410,7 @@ ScreenGameplayReplay::TogglePause()
PlayerAI::SetUpExactTapMap(PlayerAI::pReplayTiming);

PlayerStageStats* pss = m_vPlayerInfo.GetPlayerStageStats();
ReplaySnapshot* rs = PlayerAI::GetReplaySnapshotForNoterow(rowNow);
auto rs = PlayerAI::GetReplaySnapshotForNoterow(rowNow);
FOREACH_ENUM(TapNoteScore, tns)
{
pss->m_iTapNoteScores[tns] = rs->judgments[tns];
Expand Down

0 comments on commit 075b994

Please sign in to comment.