/* * Predict the players location, and display it. */ void predict_score(void) { int j; high_score the_score; high_score scores[MAX_HISCORES]; /* Read scores, place current score */ highscore_read(scores, N_ELEMENTS(scores)); build_score(&the_score, "nobody (yet!)", NULL); if (p_ptr->is_dead) j = highscore_where(&the_score, scores, N_ELEMENTS(scores)); else j = highscore_add(&the_score, scores, N_ELEMENTS(scores)); /* Hack -- Display the top fifteen scores */ if (j < 10) { display_scores_aux(scores, 0, 15, j); } /* Display some "useful" scores */ else { display_scores_aux(scores, 0, 5, -1); display_scores_aux(scores, j - 2, j + 7, j); } }
static size_t highscore_add(const high_score *entry, high_score scores[], size_t sz) { size_t slot = highscore_where(entry, scores, sz); memmove(&scores[slot+1], &scores[slot], sizeof(high_score) * (sz - 1 - slot)); memcpy(&scores[slot], entry, sizeof(high_score)); return slot; }
/* * Actually place an entry into the high score file * Return the location (0 is best) or -1 on "failure" */ static int highscore_add(high_score *score) { int i, slot; bool done = FALSE; high_score the_score, tmpscore; /* Paranoia -- it may not have opened */ if (highscore_fd < 0) return (-1); /* Determine where the score should go */ slot = highscore_where(score); /* Hack -- Not on the list */ if (slot < 0) return (-1); /* Hack -- prepare to dump the new score */ the_score = (*score); /* Slide all the scores down one */ for (i = slot; !done && (i < MAX_HISCORES); i++) { /* Read the old guy, note errors */ if (highscore_seek(i)) return (-1); if (highscore_read(&tmpscore)) done = TRUE; /* Back up and dump the score we were holding */ if (highscore_seek(i)) return (-1); if (highscore_write(&the_score)) return (-1); /* Hack -- Save the old score, for the next pass */ the_score = tmpscore; } /* Return location used */ return (slot); }
/* * Predict the players location, and display it. */ errr predict_score(void) { int j; high_score the_score; /* No score file */ if (highscore_fd < 0) { msg_print("Score file unavailable."); msg_print(NULL); return (0); } /* Save the version */ sprintf(the_score.what, "%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH); /* Calculate and save the points */ sprintf(the_score.pts, "%9ld", (long)total_points()); /* Save the current gold */ sprintf(the_score.gold, "%9d", p_ptr->au); /* Save the current turn */ sprintf(the_score.turns, "%9d", turn_real(turn)); /* Hack -- no time needed */ strcpy(the_score.day, "TODAY"); /* Save the player name (15 chars) */ sprintf(the_score.who, "%-.15s", player_name); /* Save the player info XXX XXX XXX */ sprintf(the_score.uid, "%7u", player_uid); sprintf(the_score.sex, "%c", (p_ptr->psex ? 'm' : 'f')); sprintf(the_score.p_r, "%2d", p_ptr->prace); sprintf(the_score.p_c, "%2d", p_ptr->pclass); sprintf(the_score.p_a, "%2d", p_ptr->personality); /* Save the level and such */ sprintf(the_score.cur_lev, "%3d", p_ptr->lev); sprintf(the_score.cur_dun, "%3d", dun_level); sprintf(the_score.max_lev, "%3d", p_ptr->max_plv); sprintf(the_score.max_dun, "%3d", max_dlv[dungeon_type]); /* Hack -- no cause of death */ strcpy(the_score.how, "nobody (yet!)"); /* See where the entry would be placed */ j = highscore_where(&the_score); /* Hack -- Display the top fifteen scores */ if (j < 10) { display_scores_aux(0, 15, j, &the_score); } /* Display some "useful" scores */ else { display_scores_aux(0, 5, -1, NULL); display_scores_aux(j - 2, j + 7, j, &the_score); } /* Success */ return (0); }