OSDN Git Service

[Refactor] #39962 files.c からuid-checker.c/h を分離 / Separated uid-checker.c/h from...
[hengband/hengband.git] / src / scores.c
index 2933def..c532a2a 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include "angband.h"
+#include "signal-handlers.h"
 #include "term.h"
 #include "util.h"
 #include "core.h"
@@ -21,6 +22,7 @@
 #include "player-class.h"
 #include "player-personality.h"
 #include "player-sex.h"
+#include "uid-checker.h"
 #include "files.h"
 #include "scores.h"
 #include "floor.h"
@@ -69,6 +71,7 @@ static int highscore_write(high_score *score)
        return (fd_write(highscore_fd, (char*)(score), sizeof(high_score)));
 }
 
+
 /*!
  * @brief スコア情報を全て得るまで繰り返し取得する / Just determine where a new score *would* be placed
  * @param score スコア情報参照ポインタ
@@ -76,31 +79,25 @@ static int highscore_write(high_score *score)
  */
 static int highscore_where(high_score *score)
 {
-       int                     i;
-
-       high_score              the_score;
-       int my_score;
-
-       my_score = atoi(score->pts);
-
        /* Paranoia -- it may not have opened */
-       if (highscore_fd < 0) return (-1);
+       if (highscore_fd < 0) return -1;
 
        /* Go to the start of the highscore file */
-       if (highscore_seek(0)) return (-1);
+       if (highscore_seek(0)) return -1;
 
        /* Read until we get to a higher score */
-       for (i = 0; i < MAX_HISCORES; i++)
+       high_score the_score;
+       int my_score = atoi(score->pts);
+       for (int i = 0; i < MAX_HISCORES; i++)
        {
                int old_score;
                if (highscore_read(&the_score)) return (i);
                old_score = atoi(the_score.pts);
-/*             if (strcmp(the_score.pts, score->pts) < 0) return (i); */
                if (my_score > old_score) return (i);
        }
 
        /* The "last" entry is always usable */
-       return (MAX_HISCORES - 1);
+       return MAX_HISCORES - 1;
 }
 
 
@@ -111,45 +108,40 @@ static int highscore_where(high_score *score)
  */
 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);
+       if (highscore_fd < 0) return -1;
 
        /* Determine where the score should go */
-       slot = highscore_where(score);
+       int slot = highscore_where(score);
 
        /* Hack -- Not on the list */
-       if (slot < 0) return (-1);
+       if (slot < 0) return -1;
 
        /* Hack -- prepare to dump the new score */
-       the_score = (*score);
+       high_score the_score = (*score);
 
        /* Slide all the scores down one */
-       for (i = slot; !done && (i < MAX_HISCORES); i++)
+       bool done = FALSE;
+       high_score tmpscore;
+       for (int i = slot; !done && (i < MAX_HISCORES); i++)
        {
                /* Read the old guy, note errors */
-               if (highscore_seek(i)) return (-1);
+               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);
+               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);
+       return slot;
 }
 
 
-
 /*!
  * @brief 指定された順位範囲でスコアを並べて表示する / Display the scores in a given range.
  * @param from 順位先頭
@@ -431,62 +423,56 @@ void display_scores(int from, int to)
 
 
 /*!
+ * todo プリプロが邪魔していて最初のif文を削除すると到達不能コードが発生する
  * @brief スコアサーバへの転送処理
+ * @param current_player_ptr プレーヤーへの参照ポインタ
  * @param do_send 実際に転送ア処置を行うか否か
  * @return 転送が成功したらTRUEを返す
  */
-bool send_world_score(bool do_send)
+bool send_world_score(player_type *current_player_ptr, bool do_send)
 {
 #ifdef WORLD_SCORE
-       if(send_score && do_send)
+       if (send_score && do_send)
        {
-               if(easy_band)
+               if (easy_band)
                {
                        msg_print(_("初心者モードではワールドスコアに登録できません。",
-                       "Since you are in the Easy Mode, you cannot send score to world score server."));
-               }
-               else if(get_check_strict(_("スコアをスコア・サーバに登録しますか? ", "Do you send score to the world score server? "), 
-                               (CHECK_NO_ESCAPE | CHECK_NO_HISTORY)))
-               {
-                       errr err;
-                       prt("",0,0);
-                       prt(_("送信中..", "Sending..."),0,0);
-                       Term_fresh();
-                       screen_save();
-                       err = report_score();
-                       screen_load();
-                       if (err)
-                       {
-                               return FALSE;
-                       }
-                       prt(_("完了。何かキーを押してください。", "Completed.  Hit any key."), 0, 0);
-                       (void)inkey();
+                               "Since you are in the Easy Mode, you cannot send score to world score server."));
+                       return TRUE;
                }
-               else return FALSE;
+               
+               bool is_registration = get_check_strict(_("スコアをスコア・サーバに登録しますか? ", "Do you send score to the world score server? "), (CHECK_NO_ESCAPE | CHECK_NO_HISTORY));
+               if (!is_registration) return FALSE;
+
+               errr err;
+               prt("", 0, 0);
+               prt(_("送信中..", "Sending..."), 0, 0);
+               Term_fresh();
+               screen_save();
+               err = report_score(current_player_ptr);
+               screen_load();
+               if (err) return FALSE;
+
+               prt(_("完了。何かキーを押してください。", "Completed.  Hit any key."), 0, 0);
+               (void)inkey();
        }
 #endif
        return TRUE;
 }
 
+
 /*!
  * @brief スコアの過去二十位内ランキングを表示する
  * Enters a players name on a hi-score table, if "legal", and in any
  * case, displays some relevant portion of the high score list.
+ * @param current_player_ptr スコアに適用するための現在プレイヤークリーチャー参照ポインタ
  * @return エラーコード
  * @details
  * Assumes "signals_ignore_tstp()" has been called.
  */
-errr top_twenty(void)
+errr top_twenty(player_type *current_player_ptr)
 {
-       int          j;
-
-       high_score   the_score;
-
-       time_t ct = time((time_t*)0);
-
-       errr err;
-
-       /* Clear the record */
+       high_score the_score;
        (void)WIPE(&the_score, high_score);
 
        /* Save the version */
@@ -494,72 +480,67 @@ errr top_twenty(void)
                FAKE_VER_MAJOR, FAKE_VER_MINOR, FAKE_VER_PATCH);
 
        /* Calculate and save the points */
-       sprintf(the_score.pts, "%9ld", (long)calc_score(p_ptr));
+       sprintf(the_score.pts, "%9ld", (long)calc_score(current_player_ptr));
        the_score.pts[9] = '\0';
 
        /* Save the current gold */
-       sprintf(the_score.gold, "%9lu", (long)p_ptr->au);
+       sprintf(the_score.gold, "%9lu", (long)current_player_ptr->au);
        the_score.gold[9] = '\0';
 
        /* Save the current turn */
-       sprintf(the_score.turns, "%9lu", (long)turn_real(current_world_ptr->game_turn));
+       sprintf(the_score.turns, "%9lu", (long)turn_real(current_player_ptr, current_world_ptr->game_turn));
        the_score.turns[9] = '\0';
 
-#ifdef HIGHSCORE_DATE_HACK
-       /* Save the date in a hacked up form (9 chars) */
-       (void)sprintf(the_score.day, "%-.6s %-.2s", ctime(&ct) + 4, ctime(&ct) + 22);
-#else
-       /* Save the date in standard form (8 chars) */
-/*     (void)strftime(the_score.day, 9, "%m/%d/%y", localtime(&ct)); */
+       time_t ct = time((time_t*)0);
+
        /* Save the date in standard encoded form (9 chars) */
        strftime(the_score.day, 10, "@%Y%m%d", localtime(&ct));
-#endif
 
        /* Save the player name (15 chars) */
-       sprintf(the_score.who, "%-.15s", p_ptr->name);
+       sprintf(the_score.who, "%-.15s", current_player_ptr->name);
 
        /* Save the player info */
-       sprintf(the_score.uid, "%7u", p_ptr->player_uid);
-       sprintf(the_score.sex, "%c", (p_ptr->psex ? 'm' : 'f'));
-       sprintf(the_score.p_r, "%2d", MIN(p_ptr->prace, MAX_RACES));
-       sprintf(the_score.p_c, "%2d", MIN(p_ptr->pclass, MAX_CLASS));
-       sprintf(the_score.p_a, "%2d", MIN(p_ptr->pseikaku, MAX_SEIKAKU));
+       sprintf(the_score.uid, "%7u", current_player_ptr->player_uid);
+       sprintf(the_score.sex, "%c", (current_player_ptr->psex ? 'm' : 'f'));
+       sprintf(the_score.p_r, "%2d", MIN(current_player_ptr->prace, MAX_RACES));
+       sprintf(the_score.p_c, "%2d", MIN(current_player_ptr->pclass, MAX_CLASS));
+       sprintf(the_score.p_a, "%2d", MIN(current_player_ptr->pseikaku, MAX_SEIKAKU));
 
        /* Save the level and such */
-       sprintf(the_score.cur_lev, "%3d", MIN((u16b)p_ptr->lev, 999));
-       sprintf(the_score.cur_dun, "%3d", (int)p_ptr->current_floor_ptr->dun_level);
-       sprintf(the_score.max_lev, "%3d", MIN((u16b)p_ptr->max_plv, 999));
-       sprintf(the_score.max_dun, "%3d", (int)max_dlv[p_ptr->dungeon_idx]);
+       sprintf(the_score.cur_lev, "%3d", MIN((u16b)current_player_ptr->lev, 999));
+       sprintf(the_score.cur_dun, "%3d", (int)current_player_ptr->current_floor_ptr->dun_level);
+       sprintf(the_score.max_lev, "%3d", MIN((u16b)current_player_ptr->max_plv, 999));
+       sprintf(the_score.max_dun, "%3d", (int)max_dlv[current_player_ptr->dungeon_idx]);
 
        /* Save the cause of death (31 chars) */
-       if (strlen(p_ptr->died_from) >= sizeof(the_score.how))
+       if (strlen(current_player_ptr->died_from) >= sizeof(the_score.how))
        {
 #ifdef JP
-               my_strcpy(the_score.how, p_ptr->died_from, sizeof(the_score.how) - 2);
+               my_strcpy(the_score.how, current_player_ptr->died_from, sizeof(the_score.how) - 2);
                strcat(the_score.how, "…");
 #else
-               my_strcpy(the_score.how, p_ptr->died_from, sizeof(the_score.how) - 3);
+               my_strcpy(the_score.how, current_player_ptr->died_from, sizeof(the_score.how) - 3);
                strcat(the_score.how, "...");
 #endif
        }
        else
        {
-               strcpy(the_score.how, p_ptr->died_from);
+               strcpy(the_score.how, current_player_ptr->died_from);
        }
 
        /* Grab permissions */
        safe_setuid_grab();
 
        /* Lock (for writing) the highscore file, or fail */
-       err = fd_lock(highscore_fd, F_WRLCK);
+       errr err = fd_lock(highscore_fd, F_WRLCK);
 
        /* Drop permissions */
        safe_setuid_drop();
 
-       if (err) return (1);
+       if (err) return 1;
 
        /* Add a new entry to the score list, see where it went */
-       j = highscore_add(&the_score);
+       int j = highscore_add(&the_score);
 
        /* Grab permissions */
        safe_setuid_grab();
@@ -570,35 +551,29 @@ errr top_twenty(void)
        /* Drop permissions */
        safe_setuid_drop();
 
-       if (err) return (1);
-
+       if (err) return 1;
 
        /* Hack -- Display the top fifteen scores */
        if (j < 10)
        {
                display_scores_aux(0, 15, j, NULL);
+               return 0;
        }
 
        /* Display the scores surrounding the player */
-       else
-       {
-               display_scores_aux(0, 5, j, NULL);
-               display_scores_aux(j - 2, j + 7, j, NULL);
-       }
-
-
-       /* Success */
-       return (0);
+       display_scores_aux(0, 5, j, NULL);
+       display_scores_aux(j - 2, j + 7, j, NULL);
+       return 0;
 }
 
+
 /*!
  * @brief プレイヤーの現在のスコアをランキングに挟む /
  * Predict the players location, and display it.
  * @return エラーコード
  */
-errr predict_score(player_type *creature_ptr)
+errr predict_score(player_type *current_player_ptr)
 {
-       int j;
        high_score the_score;
 
        /* No score file */
@@ -606,7 +581,7 @@ errr predict_score(player_type *creature_ptr)
        {
                msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
                msg_print(NULL);
-               return (0);
+               return 0;
        }
 
        /* Save the version */
@@ -614,57 +589,50 @@ errr predict_score(player_type *creature_ptr)
                FAKE_VER_MAJOR, FAKE_VER_MINOR, FAKE_VER_PATCH);
 
        /* Calculate and save the points */
-       sprintf(the_score.pts, "%9ld", (long)calc_score(creature_ptr));
+       sprintf(the_score.pts, "%9ld", (long)calc_score(current_player_ptr));
 
        /* Save the current gold */
-       sprintf(the_score.gold, "%9lu", (long)creature_ptr->au);
+       sprintf(the_score.gold, "%9lu", (long)current_player_ptr->au);
 
        /* Save the current turn */
-       sprintf(the_score.turns, "%9lu", (long)turn_real(current_world_ptr->game_turn));
+       sprintf(the_score.turns, "%9lu", (long)turn_real(current_player_ptr, current_world_ptr->game_turn));
 
        /* Hack -- no time needed */
        strcpy(the_score.day, _("今日", "TODAY"));
 
        /* Save the player name (15 chars) */
-       sprintf(the_score.who, "%-.15s", creature_ptr->name);
+       sprintf(the_score.who, "%-.15s", current_player_ptr->name);
 
        /* Save the player info */
-       sprintf(the_score.uid, "%7u", creature_ptr->player_uid);
-       sprintf(the_score.sex, "%c", (creature_ptr->psex ? 'm' : 'f'));
-       sprintf(the_score.p_r, "%2d", MIN(creature_ptr->prace, MAX_RACES));
-       sprintf(the_score.p_c, "%2d", MIN(creature_ptr->pclass, MAX_CLASS));
-       sprintf(the_score.p_a, "%2d", MIN(creature_ptr->pseikaku, MAX_SEIKAKU));
+       sprintf(the_score.uid, "%7u", current_player_ptr->player_uid);
+       sprintf(the_score.sex, "%c", (current_player_ptr->psex ? 'm' : 'f'));
+       sprintf(the_score.p_r, "%2d", MIN(current_player_ptr->prace, MAX_RACES));
+       sprintf(the_score.p_c, "%2d", MIN(current_player_ptr->pclass, MAX_CLASS));
+       sprintf(the_score.p_a, "%2d", MIN(current_player_ptr->pseikaku, MAX_SEIKAKU));
 
        /* Save the level and such */
-       sprintf(the_score.cur_lev, "%3d", MIN((u16b)creature_ptr->lev, 999));
-       sprintf(the_score.cur_dun, "%3d", (int)creature_ptr->current_floor_ptr->dun_level);
-       sprintf(the_score.max_lev, "%3d", MIN((u16b)creature_ptr->max_plv, 999));
-       sprintf(the_score.max_dun, "%3d", (int)max_dlv[creature_ptr->dungeon_idx]);
+       sprintf(the_score.cur_lev, "%3d", MIN((u16b)current_player_ptr->lev, 999));
+       sprintf(the_score.cur_dun, "%3d", (int)current_player_ptr->current_floor_ptr->dun_level);
+       sprintf(the_score.max_lev, "%3d", MIN((u16b)current_player_ptr->max_plv, 999));
+       sprintf(the_score.max_dun, "%3d", (int)max_dlv[current_player_ptr->dungeon_idx]);
 
        /* Hack -- no cause of death */
        /* まだ死んでいないときの識別文字 */
        strcpy(the_score.how, _("yet", "nobody (yet!)"));
 
        /* See where the entry would be placed */
-       j = highscore_where(&the_score);
-
+       int j = highscore_where(&the_score);
 
        /* Hack -- Display the top fifteen scores */
        if (j < 10)
        {
                display_scores_aux(0, 15, j, &the_score);
+               return 0;
        }
 
-       /* 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);
+       display_scores_aux(0, 5, -1, NULL);
+       display_scores_aux(j - 2, j + 7, j, &the_score);
+       return 0;
 }
 
 
@@ -673,16 +641,10 @@ errr predict_score(player_type *creature_ptr)
  * show_highclass - selectively list highscores based on class -KMW-
  * @return なし
  */
-void show_highclass(void)
+void show_highclass(player_type *current_player_ptr)
 {
-
-       register int i = 0, j, m = 0;
-       int pr;
-       PLAYER_LEVEL clev/*, al*/;
-       high_score the_score;
-       char buf[1024], out_val[256];
-
        screen_save();
+       char buf[1024], out_val[256];
        path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
 
        highscore_fd = fd_open(buf, O_RDONLY);
@@ -696,13 +658,14 @@ void show_highclass(void)
 
        if (highscore_seek(0)) return;
 
-       for (i = 0; i < MAX_HISCORES; i++)
+       high_score the_score;
+       for (int i = 0; i < MAX_HISCORES; i++)
                if (highscore_read(&the_score)) break;
 
-       m = 0;
-       j = 0;
-       clev = 0;
-
+       int m = 0;
+       int j = 0;
+       PLAYER_LEVEL clev = 0;
+       int pr;
        while ((m < 9) && (j < MAX_HISCORES))
        {
                if (highscore_seek(j)) break;
@@ -725,10 +688,10 @@ void show_highclass(void)
 
 #ifdef JP
        sprintf(out_val, "あなた) %sの%s (レベル %2d)",
-           race_info[p_ptr->prace].title,p_ptr->name, p_ptr->lev);
+           race_info[current_player_ptr->prace].title,current_player_ptr->name, current_player_ptr->lev);
 #else
        sprintf(out_val, "You) %s the %s (Level %2d)",
-           p_ptr->name, race_info[p_ptr->prace].title, p_ptr->lev);
+           current_player_ptr->name, race_info[current_player_ptr->prace].title, current_player_ptr->lev);
 #endif
 
        prt(out_val, (m + 8), 0);
@@ -743,13 +706,14 @@ void show_highclass(void)
        screen_load();
 }
 
+
 /*!
  * @brief スコアランキングの簡易表示(種族毎)サブルーチン /
  * Race Legends -KMW-
  * @param race_num 種族ID
  * @return なし
  */
-void race_score(int race_num)
+void race_score(player_type *current_player_ptr, int race_num)
 {
        register int i = 0, j, m = 0;
        int pr, clev, lastlev;
@@ -810,14 +774,14 @@ void race_score(int race_num)
        }
 
        /* add player if qualified */
-       if ((p_ptr->prace == race_num) && (p_ptr->lev >= lastlev))
+       if ((current_player_ptr->prace == race_num) && (current_player_ptr->lev >= lastlev))
        {
 #ifdef JP
        sprintf(out_val, "あなた) %sの%s (レベル %2d)",
-                    race_info[p_ptr->prace].title,p_ptr->name, p_ptr->lev);
+                    race_info[current_player_ptr->prace].title,current_player_ptr->name, current_player_ptr->lev);
 #else
                sprintf(out_val, "You) %s the %s (Level %3d)",
-                   p_ptr->name, race_info[p_ptr->prace].title, p_ptr->lev);
+                   current_player_ptr->name, race_info[current_player_ptr->prace].title, current_player_ptr->lev);
 #endif
 
                prt(out_val, (m + 8), 0);
@@ -833,20 +797,19 @@ void race_score(int race_num)
  * Race Legends -KMW-
  * @return なし
  */
-void race_legends(void)
+void race_legends(player_type *current_player_ptr)
 {
-       int i, j;
-
-       for (i = 0; i < MAX_RACES; i++)
+       for (int i = 0; i < MAX_RACES; i++)
        {
-               race_score(i);
+               race_score(current_player_ptr, i);
                msg_print(_("何かキーを押すとゲームに戻ります", "Hit any key to continue"));
                msg_print(NULL);
-               for (j = 5; j < 19; j++)
+               for (int j = 5; j < 19; j++)
                        prt("", j, 0);
        }
 }
 
+
 /*!
  * @brief 勝利者用の引退演出処理 /
  * Change the player into a King! -RAK-
@@ -908,9 +871,9 @@ void kingly(player_type *winner_ptr)
        /* If player did Seppuku, that is already written in playrecord */
        if (!seppuku)
        {
-               exe_write_diary(winner_ptr, NIKKI_BUNSHOU, 0, _("ダンジョンの探索から引退した。", "retired exploring dungeons."));
-               exe_write_diary(winner_ptr, NIKKI_GAMESTART, 1, _("-------- ゲームオーバー --------", "--------   Game  Over   --------"));
-               exe_write_diary(winner_ptr, NIKKI_BUNSHOU, 1, "\n\n\n\n");
+               exe_write_diary(winner_ptr, DIARY_DESCRIPTION, 0, _("ダンジョンの探索から引退した。", "retired exploring dungeons."));
+               exe_write_diary(winner_ptr, DIARY_GAMESTART, 1, _("-------- ゲームオーバー --------", "--------   Game  Over   --------"));
+               exe_write_diary(winner_ptr, DIARY_DESCRIPTION, 1, "\n\n\n\n");
        }
 
        /* Flush input */
@@ -920,12 +883,13 @@ void kingly(player_type *winner_ptr)
        pause_line(hgt - 1);
 }
 
+
 /*!
  * @brief スコアファイル出力
  * Display some character info
  * @return なし
  */
-bool check_score(void)
+bool check_score(player_type *current_player_ptr)
 {
        Term_clear();
 
@@ -937,38 +901,24 @@ bool check_score(void)
                return FALSE;
        }
 
-#ifndef SCORE_WIZARDS
        /* Wizard-mode pre-empts scoring */
-       if (p_ptr->noscore & 0x000F)
+       if (current_world_ptr->noscore & 0x000F)
        {
                msg_print(_("ウィザード・モードではスコアが記録されません。", "Score not registered for wizards."));
                msg_print(NULL);
                return FALSE;
        }
-#endif
-
-#ifndef SCORE_BORGS
-       /* Borg-mode pre-empts scoring */
-       if (p_ptr->noscore & 0x00F0)
-       {
-               msg_print(_("ボーグ・モードではスコアが記録されません。", "Score not registered for borgs."));
-               msg_print(NULL);
-               return FALSE;
-       }
-#endif
 
-#ifndef SCORE_CHEATERS
        /* Cheaters are not scored */
-       if (p_ptr->noscore & 0xFF00)
+       if (current_world_ptr->noscore & 0xFF00)
        {
                msg_print(_("詐欺をやった人はスコアが記録されません。", "Score not registered for cheaters."));
                msg_print(NULL);
                return FALSE;
        }
-#endif
 
        /* Interupted */
-       if (!p_ptr->total_winner && streq(p_ptr->died_from, _("強制終了", "Interrupting")))
+       if (!current_world_ptr->total_winner && streq(current_player_ptr->died_from, _("強制終了", "Interrupting")))
        {
                msg_print(_("強制終了のためスコアが記録されません。", "Score not registered due to interruption."));
                msg_print(NULL);
@@ -976,7 +926,7 @@ bool check_score(void)
        }
 
        /* Quitter */
-       if (!p_ptr->total_winner && streq(p_ptr->died_from, _("途中終了", "Quitting")))
+       if (!current_world_ptr->total_winner && streq(current_player_ptr->died_from, _("途中終了", "Quitting")))
        {
                msg_print(_("途中終了のためスコアが記録されません。", "Score not registered due to quitting."));
                msg_print(NULL);
@@ -984,4 +934,3 @@ bool check_score(void)
        }
        return TRUE;
 }
-