Skip to content

Commit

Permalink
use math.log10() to find digits in note counts
Browse files Browse the repository at this point in the history
In 5316b92, I added support for the StepStats side pane in Gameplay to accommodate more than 4 digits of notes.

If the total note count for a song is >= 9999, 4 digits will be used (and leftpadded with 0s as needed), and the display will grow to accommodate extra digits as needed.  For example:

 • "Last Trip" has 22897 notes and needs 5 digits
 • "140 BPM Stamina Killer" has over 100k notes and needs 6 digits
 • "1 week of 100 BPM Stream" has over 4M notes and needs 7 digits

The previous implementation used a while loop, division, and math.pow() to count digits.  This was probably fine since this calculation would only be performed once, prior to Init, but I'd also failed to remember that I needed the same calculation elsewhere in the same file, and was using a very sloppy (number -> string -> number) cast there.

This commit changes both of those to use the more standard math.log10() approach of determining digits in base-10.  It is currently unclear what performance impact this might have, or how I might even assess that.

I still need to handle positioning and zooming of the BitmapText when Center1Player is in effect, but that is a different matter.
  • Loading branch information
quietly-turning committed Sep 15, 2019
1 parent 7a0db22 commit 4a65329
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ local possible, rv, pss
local StepsOrTrail = (GAMESTATE:IsCourseMode() and GAMESTATE:GetCurrentTrail(player)) or GAMESTATE:GetCurrentSteps(player)
local total_tapnotes = StepsOrTrail:GetRadarValues(player):GetValue( "RadarCategory_Notes" )

-- minimum 4 digits for aesthetic reasons
local digits = 4
-- if total note count is greater than 9999, however, increase digits displayed as needed
if total_tapnotes > 9999 then
while ((total_tapnotes / math.pow(10, digits)) >= 1) do
digits = digits + 1
end
end
-- determine how many digits are needed to express the number of notes in base-10
local digits = (math.floor(math.log10(total_tapnotes)) + 1)
-- display a minimum 4 digits for aesthetic reasons
digits = math.max(4, digits)

-- generate a Lua string pattern that will be used to leftpad with 0s
local pattern = ("%%0%dd"):format(digits)

Expand Down Expand Up @@ -64,7 +61,10 @@ for index, window in ipairs(TapNoteScores) do
TapNoteJudgments[window] = TapNoteJudgments[window] + 1
self:settext( (pattern):format(TapNoteJudgments[window]) )

leadingZeroAttr = { Length=(digits-tonumber(tostring(TapNoteJudgments[window]):len())), Diffuse=Brightness(SL.JudgmentColors[SL.Global.GameMode][index], 0.35) }
leadingZeroAttr = {
Length=(digits - (math.floor(math.log10(TapNoteJudgments[window]))+1)),
Diffuse=Brightness(SL.JudgmentColors[SL.Global.GameMode][index], 0.35)
}
self:AddAttribute(0, leadingZeroAttr )
end
end
Expand Down

1 comment on commit 4a65329

@quietly-turning
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message here contains a misleading typo.

If the total note count for a song is >= 9999 should have read If the total note count for a song is <= 9999. Apologies for the confusion.

Please sign in to comment.