From f7e87bc98111f97ca489e1f13999058a7df8171e Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Fri, 29 Jun 2012 20:49:49 +0200 Subject: Split high score reading/writing into separate compilation unit --- src/files.c | 157 +----------------------------------------------------------- 1 file changed, 2 insertions(+), 155 deletions(-) (limited to 'src/files.c') diff --git a/src/files.c b/src/files.c index ea1922a5..f8125148 100644 --- a/src/files.c +++ b/src/files.c @@ -12,6 +12,8 @@ #include "angband.h" +#include "hiscore.h" + /* * Extract the first few "tokens" from a buffer @@ -4693,161 +4695,6 @@ static void show_info(void) -/* - * Semi-Portable High Score List Entry (128 bytes) -- BEN - * - * All fields listed below are null terminated ascii strings. - * - * In addition, the "number" fields are right justified, and - * space padded, to the full available length (minus the "null"). - * - * Note that "string comparisons" are thus valid on "pts". - */ - -typedef struct high_score high_score; - -struct high_score -{ - char what[8]; /* Version info (string) */ - - char pts[10]; /* Total Score (number) */ - - char gold[10]; /* Total Gold (number) */ - - char turns[10]; /* Turns Taken (number) */ - - char day[10]; /* Time stamp (string) */ - - char who[16]; /* Player Name (string) */ - - char unused_1[8]; /* Kept for compatibility only */ - - char sex[2]; /* Player Sex (string) */ - char p_r[3]; /* Player Race (number) */ - char p_s[3]; /* Player Subrace (number) */ - char p_c[3]; /* Player Class (number) */ - char p_cs[3]; /* Player Class spec (number) */ - - char cur_lev[4]; /* Current Player Level (number) */ - char cur_dun[4]; /* Current Dungeon Level (number) */ - char max_lev[4]; /* Max Player Level (number) */ - char max_dun[4]; /* Max Dungeon Level (number) */ - - char arena_number[4]; /* Arena level attained -KMW- */ - char inside_arena[4]; /* Did the player die in the arena? */ - char inside_quest[4]; /* Did the player die in a quest? */ - char exit_bldg[4]; /* Can the player exit arena? Goal obtained? -KMW- */ - - char how[32]; /* Method of death (string) */ -}; - - - -/* - * Seek score 'i' in the highscore file - */ -static int highscore_seek(int highscore_fd, int i) -{ - /* Seek for the requested record */ - return (fd_seek(highscore_fd, (huge)(i) * sizeof(high_score))); -} - - -/* - * Read one score from the highscore file - */ -static errr highscore_read(int highscore_fd, high_score *score) -{ - /* Read the record, note failure */ - return (fd_read(highscore_fd, (char*)(score), sizeof(high_score))); -} - - -/* - * Write one score to the highscore file - */ -static int highscore_write(int highscore_fd, high_score *score) -{ - /* Write the record, note failure */ - return (fd_write(highscore_fd, (char*)(score), sizeof(high_score))); -} - - - - -/* - * Just determine where a new score *would* be placed - * Return the location (0 is best) or -1 on failure - */ -static int highscore_where(int highscore_fd, high_score *score) -{ - int i; - - high_score the_score; - - /* Paranoia -- it may not have opened */ - if (highscore_fd < 0) return ( -1); - - /* Go to the start of the highscore file */ - if (highscore_seek(highscore_fd, 0)) return ( -1); - - /* Read until we get to a higher score */ - for (i = 0; i < MAX_HISCORES; i++) - { - if (highscore_read(highscore_fd, &the_score)) return (i); - if (strcmp(the_score.pts, score->pts) < 0) return (i); - } - - /* The "last" entry is always usable */ - return (MAX_HISCORES - 1); -} - - -/* - * Actually place an entry into the high score file - * Return the location (0 is best) or -1 on "failure" - */ -static int highscore_add(int highscore_fd, 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(highscore_fd, 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(highscore_fd, i)) return ( -1); - if (highscore_read(highscore_fd, &tmpscore)) done = TRUE; - - /* Back up and dump the score we were holding */ - if (highscore_seek(highscore_fd, i)) return ( -1); - if (highscore_write(highscore_fd, &the_score)) return ( -1); - - /* Hack -- Save the old score, for the next pass */ - the_score = tmpscore; - } - - /* Return location used */ - return (slot); -} - - - /* * Display the scores in a given range. * Assumes the high score list is already open. -- cgit v1.2.3