aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Linskey2026-06-08 11:09:37 -0400
committerBenjamin Linskey2026-06-08 17:01:22 -0400
commit794d1c58ce6e7f1093a6fecb847aa913fad080bc (patch)
treeba561c9cd1542093c2ea31d4f663d2f81cef8dc7
parent3044b34494f0fd6133d10cd5a93e513d8ce1671f (diff)
downloadrogue-794d1c58ce6e7f1093a6fecb847aa913fad080bc.tar.gz

Allow multiple scores to be saved for one user

The original multi-user design only permitted each user to occupy a single spot in the high-score table.

-rw-r--r--score.c70
1 files changed, 22 insertions, 48 deletions
diff --git a/score.c b/score.c
index e8815ec..c438803 100644
--- a/score.c
+++ b/score.c
@@ -336,7 +336,7 @@ write_score_entry(const struct score_entry *se, int rank, FILE *fp)
void
put_scores(const object *monster, short other)
{
- short i, rank=-1, found_player = -1, numscores = 0;
+ short i, rank=-1, numscores = 0;
struct score_entry scores[NUM_SCORE_ENTRIES];
const char *name;
FILE *fp;
@@ -351,69 +351,43 @@ put_scores(const object *monster, short other)
rewind(fp);
(void)xxx(1);
+ // Get the number of scores saved in the file.
for (numscores = 0; numscores < NUM_SCORE_ENTRIES; numscores++) {
if (read_score_entry(&scores[numscores], fp) == 0) {
break;
}
}
- /* Search the score list. */
- for (i=0; i<numscores; i++) {
- if (!strcmp(scores[i].username, login_name)) {
- /* found our score */
- if (rogue.gold < scores[i].gold) {
- /* we didn't do as well as last time */
- score_only = 1;
- } else {
- /* we did better; mark entry for removal */
- found_player = i;
- }
+ // Find where we rank relative to the saved scores.
+ rank = numscores;
+ for (i = 0; i < numscores; i++) {
+ if (rogue.gold > scores[i].gold) {
+ rank = i;
break;
}
}
- /* Remove a superseded entry, if any. */
- if (found_player != -1) {
- numscores--;
- for (i = found_player; i < numscores; i++) {
- scores[i] = scores[i+1];
- }
+ // If we're in last place, expand the list if there's room.
+ if (rank == numscores && numscores < NUM_SCORE_ENTRIES) {
+ numscores++;
}
- /* If we're going to insert ourselves, do it now */
- if (!score_only) {
-
- /* if we aren't better than anyone, add at end. */
- rank = numscores;
-
- /* Otherwise, find our slot. */
- for (i = 0; i < numscores; i++) {
- if (rogue.gold >= scores[i].gold) {
- rank = i;
- break;
- }
- }
-
- if (rank < NUM_SCORE_ENTRIES) {
- /* Open up a slot */
- for (i = numscores; i > rank; i--) {
- scores[i] = scores[i-1];
- }
- numscores++;
-
- /* Put our info in the slot */
- make_score(&scores[rank], monster, other);
+ if (rank < NUM_SCORE_ENTRIES) {
+ for (i = numscores - 1; i >= rank; i--) {
+ scores[i] = scores[i - 1];
}
+ }
- /* Now rewrite the score file */
+ /* Put our info in the slot */
+ make_score(&scores[rank], monster, other);
- md_ignore_signals();
- rewind(fp);
- (void)xxx(1);
+ md_ignore_signals();
+ rewind(fp);
+ (void)xxx(1);
- for (i = 0; i < numscores; i++) {
- write_score_entry(&scores[i], i, fp);
- }
+ /* Now rewrite the score file */
+ for (i = 0; i < numscores; i++) {
+ write_score_entry(&scores[i], i, fp);
}
fclose(fp);