diff options
author | Bardur Arantsson <bardur@scientician.net> | 2012-06-29 20:49:49 +0200 |
---|---|---|
committer | Bardur Arantsson <bardur@scientician.net> | 2012-06-29 20:49:49 +0200 |
commit | f7e87bc98111f97ca489e1f13999058a7df8171e (patch) | |
tree | 7775a192c756e9ae921087e26eb83d9b2db2c343 | |
parent | c6cd8211eb4655520a607d7d1a854927de7a1803 (diff) |
Split high score reading/writing into separate compilation unit
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/files.c | 157 | ||||
-rw-r--r-- | src/hiscore.c | 85 | ||||
-rw-r--r-- | src/hiscore.h | 89 |
4 files changed, 177 insertions, 156 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0e71e07b..50bb33de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,7 +11,7 @@ SET(SRCS corrupt.c joke.c mimic.c status.c files.c notes.c loadsave.c string_list.c cmd1.c cmd2.c cmd3.c cmd4.c cmd5.c cmd6.c cmd7.c - help.c range.c + help.c hiscore.c range.c generate.c gen_maze.c gen_evol.c wild.c levels.c store.c bldg.c cmovie.c wizard2.c init2.c birth.c wizard1.c init1.c main.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 @@ -4694,161 +4696,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. * Only five entries per line, too much info. diff --git a/src/hiscore.c b/src/hiscore.c new file mode 100644 index 00000000..357026da --- /dev/null +++ b/src/hiscore.c @@ -0,0 +1,85 @@ +#include "hiscore.h" + +#include <assert.h> + +#include "angband.h" + +int highscore_seek(int highscore_fd, int i) +{ + /* Seek for the requested record */ + return (fd_seek(highscore_fd, (huge)(i) * sizeof(high_score))); +} + +errr highscore_read(int highscore_fd, high_score *score) +{ + /* Read the record, note failure */ + return (fd_read(highscore_fd, (char*)(score), sizeof(high_score))); +} + +int highscore_write(int highscore_fd, high_score *score) +{ + /* Write the record, note failure */ + return (fd_write(highscore_fd, (char*)(score), sizeof(high_score))); +} + +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); +} + +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); +} diff --git a/src/hiscore.h b/src/hiscore.h new file mode 100644 index 00000000..0eed91b4 --- /dev/null +++ b/src/hiscore.h @@ -0,0 +1,89 @@ +#ifndef H_267bf1c1_eada_4b18_9558_d96330fa7258 +#define H_267bf1c1_eada_4b18_9558_d96330fa7258 + +#include "h-type.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * 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 + */ +int highscore_seek(int highscore_fd, int i); + +/* + * Read one score from the highscore file + */ +errr highscore_read(int highscore_fd, high_score *score); + +/* + * Write one score to the highscore file + */ +int highscore_write(int highscore_fd, high_score *score); + +/* + * Determine where a new score *would* be placed + * Return the location (0 is best) or -1 on failure + */ +int highscore_where(int highscore_fd, high_score *score); + +/* + * Place an entry into the high score file. Return the location (0 is + * best) or -1 on "failure" + */ +int highscore_add(int highscore_fd, high_score *score); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif |