Skip to content

Commit

Permalink
beatmap: implement advanced scoring system.
Browse files Browse the repository at this point in the history
Early and late hits are now only awared a third of a point, just like in osu!
  • Loading branch information
Leo Prikler authored and fmang committed Mar 2, 2021
1 parent fbd6982 commit 5e75c63
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
6 changes: 6 additions & 0 deletions include/beatmap/beatmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,12 @@ int load_beatmap_headers(const char *path, oshu::beatmap *beatmap);
*/
void destroy_beatmap(oshu::beatmap *beatmap);

/**
* Return a numeric value between 0 (no hits) and 1 (perfect) based on the
* player's hit/miss ratio.
*/
double score(oshu::beatmap *beatmap);

/** \} */

}
3 changes: 1 addition & 2 deletions include/ui/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ struct display;
struct score_frame {
oshu::display *display;
oshu::beatmap *beatmap;
int good;
int bad;
double score;
};

/**
Expand Down
33 changes: 33 additions & 0 deletions lib/beatmap/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

#include "beatmap/beatmap.h"
#include <assert.h>
#include <limits>

double oshu::hit_end_time(oshu::hit *hit)
{
Expand All @@ -23,3 +25,34 @@ oshu::point oshu::end_point(oshu::hit *hit)
else
return hit->p;
}

double oshu::score(oshu::beatmap *beatmap)
{
double score = 0, total = 0;
auto leniency = beatmap->difficulty.leniency;

for (oshu::hit *hit = beatmap->hits; hit; hit = hit->next) {
switch (hit->state) {
case oshu::GOOD_HIT:
if (hit->offset < - leniency / 2)
score += 1.0/3;
else if (hit->offset > leniency / 2)
score += 1.0/3;
else
score += 1.0;
[[fallthrough]];
case oshu::MISSED_HIT:
++total;
break;
}
}

score /= total;
if (total == 0) {
if (!std::isnan(score))
score = std::numeric_limits<double>::quiet_NaN();
}
else
assert ((0 <= score) && (score <= 1));
return score;
}
25 changes: 8 additions & 17 deletions lib/game/tty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,13 @@ void oshu::congratulate(oshu::game_base *game)
{
/* Clear the status line. */
printf("\r \r");
/* Compute the score. */
int good = 0;
int missed = 0;
for (oshu::hit *hit = game->beatmap.hits; hit; hit = hit->next) {
if (hit->state == oshu::MISSED_HIT)
missed++;
else if (hit->state == oshu::GOOD_HIT)
good++;
}
double rate = (double) good / (good + missed);
double score = oshu::score(&game->beatmap);
if (std::isnan(score)) return;

int score_color = 0;
if (score >= 0.9) score_color = 32;
else if (score < 0.5) score_color = 31;
printf(
" \033[1mScore:\033[0m\n"
" \033[%dm%3d\033[0m good\n"
" \033[%dm%3d\033[0m miss\n"
"\n",
rate >= 0.9 ? 32 : 0, good,
rate < 0.5 ? 31 : 0, missed
);
" \033[1mScore: \033[%dm%3.2f\033[0m%%\n\n",
score_color, score * 100);
}
16 changes: 3 additions & 13 deletions lib/ui/score.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,13 @@ int oshu::create_score_frame(oshu::display *display, oshu::beatmap *beatmap, osh
memset(frame, 0, sizeof(*frame));
frame->display = display;
frame->beatmap = beatmap;

for (oshu::hit *hit = beatmap->hits; hit; hit = hit->next) {
if (hit->state == oshu::MISSED_HIT)
++frame->bad;
else if (hit->state == oshu::GOOD_HIT)
++frame->good;
}

frame->score = oshu::score(beatmap);
return 0;
}

void oshu::show_score_frame(oshu::score_frame *frame, double opacity)
{
int notes = frame->good + frame->bad;
if (notes == 0)
return;

if (std::isnan(frame->score)) return;
SDL_SetRenderDrawBlendMode(frame->display->renderer, SDL_BLENDMODE_BLEND);

SDL_Rect bar = {
Expand All @@ -46,7 +36,7 @@ void oshu::show_score_frame(oshu::score_frame *frame, double opacity)
SDL_Rect good = {
.x = bar.x,
.y = bar.y,
.w = (int) ((double) frame->good / notes * bar.w),
.w = (int) ((double) frame->score * bar.w),
.h = bar.h,
};
SDL_SetRenderDrawColor(frame->display->renderer, 0, 255, 0, 196 * opacity);
Expand Down

0 comments on commit 5e75c63

Please sign in to comment.