3 * @brief ハイスコア処理 / Highscores handling
6 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7 * This software may be copied and distributed for educational, research,
8 * and not for profit purposes provided that this copyright and statement
9 * are included in all such copies. Other copyrights may also apply.
10 * 2014 Deskull rearranged comment for Doxygen.
13 #include "core/scores.h"
14 #include "cmd-io/cmd-dump.h"
15 #include "core/asking-player.h"
16 #include "core/score-util.h"
17 #include "core/turn-compensator.h"
18 #include "dungeon/dungeon.h"
19 #include "game-option/birth-options.h"
20 #include "game-option/game-play-options.h"
21 #include "io/input-key-acceptor.h"
22 #include "io/report.h"
23 #include "io/signal-handlers.h"
24 #include "io/uid-checker.h"
25 #include "player/player-class.h"
26 #include "player/player-personality.h"
27 #include "player/player-status.h"
28 #include "player/race-info-table.h"
29 #include "system/angband-version.h"
30 #include "system/floor-type-definition.h"
31 #include "system/player-type-definition.h"
32 #include "term/screen-processor.h"
33 #include "term/term-color-types.h"
34 #include "util/angband-files.h"
35 #include "util/int-char-converter.h"
36 #include "util/string-processor.h"
37 #include "view/display-messages.h"
38 #include "world/world.h"
41 #include "locale/japanese.h"
45 * @brief 所定ポインタへスコア情報を書き込む / Write one score to the highscore file
46 * @param score スコア情報参照ポインタ
47 * @return エラーコード(問題がなければ0を返す)
49 static int highscore_write(high_score *score)
51 /* Write the record, note failure */
52 return (fd_write(highscore_fd, (char *)(score), sizeof(high_score)));
56 * @brief スコア情報を全て得るまで繰り返し取得する / Just determine where a new score *would* be placed
57 * @param score スコア情報参照ポインタ
58 * @return 正常ならば(MAX_HISCORES - 1)、問題があれば-1を返す
60 static int highscore_where(high_score *score)
62 /* Paranoia -- it may not have opened */
66 /* Go to the start of the highscore file */
67 if (highscore_seek(0))
70 /* Read until we get to a higher score */
72 int my_score = atoi(score->pts);
73 for (int i = 0; i < MAX_HISCORES; i++) {
75 if (highscore_read(&the_score))
77 old_score = atoi(the_score.pts);
78 if (my_score > old_score)
82 /* The "last" entry is always usable */
83 return MAX_HISCORES - 1;
87 * @brief スコア情報をバッファの末尾に追加する / Actually place an entry into the high score file
88 * @param score スコア情報参照ポインタ
89 * @return 正常ならば書き込んだスロット位置、問題があれば-1を返す / Return the location (0 is best) or -1 on "failure"
91 static int highscore_add(high_score *score)
93 /* Paranoia -- it may not have opened */
97 /* Determine where the score should go */
98 int slot = highscore_where(score);
100 /* Hack -- Not on the list */
104 /* Hack -- prepare to dump the new score */
105 high_score the_score = (*score);
107 /* Slide all the scores down one */
110 for (int i = slot; !done && (i < MAX_HISCORES); i++) {
111 /* Read the old guy, note errors */
112 if (highscore_seek(i))
114 if (highscore_read(&tmpscore))
117 /* Back up and dump the score we were holding */
118 if (highscore_seek(i))
120 if (highscore_write(&the_score))
123 /* Hack -- Save the old score, for the next pass */
124 the_score = tmpscore;
127 /* Return location used */
132 * @brief 指定された順位範囲でスコアを並べて表示する / Display the scores in a given range.
135 * @param note 黄色表示でハイライトする順位
136 * @param score スコア配列参照ポインタ
139 * Assumes the high score list is already open.
140 * Only five entries per line, too much info.
142 * Mega-Hack -- allow "fake" entry at the given position.
145 void display_scores_aux(int from, int to, int note, high_score *score)
147 int i, j, k, n, place;
150 high_score the_score;
152 GAME_TEXT out_val[256];
153 GAME_TEXT tmp_val[160];
155 TERM_LEN wid, hgt, per_screen;
157 term_get_size(&wid, &hgt);
158 per_screen = (hgt - 4) / 4;
160 /* Paranoia -- it may not have opened */
161 if (highscore_fd < 0)
164 /* Assume we will show the first 10 */
169 if (to > MAX_HISCORES)
172 /* Seek to the beginning */
173 if (highscore_seek(0))
176 /* Hack -- Count the high scores */
177 for (i = 0; i < MAX_HISCORES; i++) {
178 if (highscore_read(&the_score))
182 /* Hack -- allow "fake" entry to be last */
183 if ((note == i) && score)
186 /* Forget about the last entries */
190 /* Show per_screen per page, until "done" */
191 for (k = from, place = k + 1; k < i; k += per_screen) {
195 put_str(_(" 変愚蛮怒: 勇者の殿堂", " Hengband Hall of Fame"), 0, 0);
197 /* Indicate non-top scores */
199 sprintf(tmp_val, _("( %d 位以下 )", "(from position %d)"), k + 1);
200 put_str(tmp_val, 0, 40);
203 /* Dump per_screen entries */
204 for (j = k, n = 0; j < i && n < per_screen; place++, j++, n++) {
205 int pr, pc, pa, clev, mlev, cdun, mdun;
207 concptr user, gold, when, aged;
209 /* Hack -- indicate death in yellow */
210 attr = (j == note) ? TERM_YELLOW : TERM_WHITE;
212 /* Mega-Hack -- insert a "fake" record */
213 if ((note == j) && score) {
214 the_score = (*score);
221 /* Read a normal record */
223 /* Read the proper record */
224 if (highscore_seek(j))
226 if (highscore_read(&the_score))
230 /* Extract the race/class */
231 pr = atoi(the_score.p_r);
232 pc = atoi(the_score.p_c);
233 pa = atoi(the_score.p_a);
235 /* Extract the level info */
236 clev = atoi(the_score.cur_lev);
237 mlev = atoi(the_score.max_lev);
238 cdun = atoi(the_score.cur_dun);
239 mdun = atoi(the_score.max_dun);
241 /* Hack -- extract the gold and such */
242 for (user = the_score.uid; iswspace(*user); user++) /* loop */
244 for (when = the_score.day; iswspace(*when); when++) /* loop */
246 for (gold = the_score.gold; iswspace(*gold); gold++) /* loop */
248 for (aged = the_score.turns; iswspace(*aged); aged++) /* loop */
251 /* Clean up standard encoded form of "when" */
252 if ((*when == '@') && strlen(when) == 9) {
253 sprintf(tmp_val, "%.4s-%.2s-%.2s", when + 1, when + 5, when + 7);
259 /*sprintf(out_val, "%3d.%9s %s%s%sという名の%sの%s (レベル %d)", */
260 sprintf(out_val, "%3d.%9s %s%s%s - %s%s (レベル %d)", place, the_score.pts, personality_info[pa].title, (personality_info[pa].no ? "の" : ""),
261 the_score.who, race_info[pr].title, class_info[pc].title, clev);
264 sprintf(out_val, "%3d.%9s %s %s the %s %s, Level %d", place, the_score.pts, personality_info[pa].title, the_score.who, race_info[pr].title,
265 class_info[pc].title, clev);
268 /* Append a "maximum level" */
270 strcat(out_val, format(_(" (最高%d)", " (Max %d)"), mlev));
272 /* Dump the first line */
273 c_put_str(attr, out_val, n * 4 + 2, 0);
275 /* Another line of info */
278 sprintf(out_val, " 最高%3d階", mdun);
280 sprintf(out_val, " ");
282 /* 死亡原因をオリジナルより細かく表示 */
283 if (streq(the_score.how, "yet")) {
284 sprintf(out_val + 13, " まだ生きている (%d%s)", cdun, "階");
285 } else if (streq(the_score.how, "ripe")) {
286 sprintf(out_val + 13, " 勝利の後に引退 (%d%s)", cdun, "階");
287 } else if (streq(the_score.how, "Seppuku")) {
288 sprintf(out_val + 13, " 勝利の後に切腹 (%d%s)", cdun, "階");
290 codeconv(the_score.how);
292 /* Some people die outside of the dungeon */
294 sprintf(out_val + 13, " 地上で%sに殺された", the_score.how);
296 sprintf(out_val + 13, " %d階で%sに殺された", cdun, the_score.how);
300 /* Some people die outside of the dungeon */
302 sprintf(out_val, " Killed by %s on the surface", the_score.how);
304 sprintf(out_val, " Killed by %s on %s %d", the_score.how, "Dungeon Level", cdun);
306 /* Append a "maximum level" */
308 strcat(out_val, format(" (Max %d)", mdun));
312 c_put_str(attr, out_val, n * 4 + 3, 0);
314 /* And still another line of info */
319 /* 日付を 19yy/mm/dd の形式に変更する */
320 if (strlen(when) == 8 && when[2] == '/' && when[5] == '/') {
321 sprintf(buf, "%d%s/%.5s", 19 + (when[6] < '8'), when + 6, when);
324 sprintf(out_val, " (ユーザー:%s, 日付:%s, 所持金:%s, ターン:%s)", user, when, gold, aged);
328 sprintf(out_val, " (User %s, Date %s, Gold %s, Turn %s).", user, when, gold, aged);
331 c_put_str(attr, out_val, n * 4 + 4, 0);
334 /* Wait for response */
335 prt(_("[ ESCで中断, その他のキーで続けます ]", "[Press ESC to quit, any other key to continue.]"), hgt - 1, _(21, 17));
340 /* Hack -- notice Escape */
347 * @brief スコア表示処理メインルーチン / Hack -- Display the scores in a given range and quit.
352 * This function is only called from "main.c" when the user asks
353 * to see the "high scores".
356 void display_scores(int from, int to)
359 path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
361 /* Open the binary high score file, for reading */
362 highscore_fd = fd_open(buf, O_RDONLY);
364 /* Paranoia -- No score file */
365 if (highscore_fd < 0)
366 quit(_("スコア・ファイルが使用できません。", "Score file unavailable."));
369 /* Display the scores */
370 display_scores_aux(from, to, -1, NULL);
372 /* Shut the high score file */
373 (void)fd_close(highscore_fd);
375 /* Forget the high score fd */
383 * @brief スコアサーバへの転送処理
384 * @param current_player_ptr プレーヤーへの参照ポインタ
385 * @param do_send 実際に転送ア処置を行うか否か
386 * @return 転送が成功したらTRUEを返す
388 bool send_world_score(player_type *current_player_ptr, bool do_send, display_player_pf display_player)
391 if (!send_score || !do_send) {
396 msg_print(_("初心者モードではワールドスコアに登録できません。", "Since you are in the Easy Mode, you cannot send score to world score server."));
400 auto is_registration = get_check_strict(
401 current_player_ptr, _("スコアをスコア・サーバに登録しますか? ", "Do you send score to the world score server? "), (CHECK_NO_ESCAPE | CHECK_NO_HISTORY));
402 if (!is_registration) {
407 prt(_("送信中..", "Sending..."), 0, 0);
410 auto err = report_score(current_player_ptr, display_player);
416 prt(_("完了。何かキーを押してください。", "Completed. Hit any key."), 0, 0);
419 (void)current_player_ptr;
421 (void)display_player;
427 * @brief スコアの過去二十位内ランキングを表示する
428 * Enters a players name on a hi-score table, if "legal", and in any
429 * case, displays some relevant portion of the high score list.
430 * @param current_player_ptr スコアに適用するための現在プレイヤークリーチャー参照ポインタ
433 * Assumes "signals_ignore_tstp()" has been called.
435 errr top_twenty(player_type *current_player_ptr)
437 high_score the_score;
439 (void)WIPE(&the_score, high_score);
441 /* Save the version */
442 sprintf(the_score.what, "%u.%u.%u", FAKE_VER_MAJOR, FAKE_VER_MINOR, FAKE_VER_PATCH);
444 /* Calculate and save the points */
445 sprintf(the_score.pts, "%9ld", (long)calc_score(current_player_ptr));
446 the_score.pts[9] = '\0';
448 /* Save the current gold */
449 sprintf(the_score.gold, "%9lu", (long)current_player_ptr->au);
450 the_score.gold[9] = '\0';
452 /* Save the current turn */
453 sprintf(the_score.turns, "%9lu", (long)turn_real(current_player_ptr, current_world_ptr->game_turn));
454 the_score.turns[9] = '\0';
456 time_t ct = time((time_t *)0);
458 /* Save the date in standard encoded form (9 chars) */
459 strftime(the_score.day, 10, "@%Y%m%d", localtime(&ct));
461 /* Save the player name (15 chars) */
462 sprintf(the_score.who, "%-.15s", current_player_ptr->name);
464 /* Save the player info */
465 sprintf(the_score.uid, "%7u", current_player_ptr->player_uid);
466 sprintf(the_score.sex, "%c", (current_player_ptr->psex ? 'm' : 'f'));
467 snprintf(buf, sizeof(buf), "%2d", MIN(current_player_ptr->prace, MAX_RACES));
468 memcpy(the_score.p_r, buf, 3);
469 snprintf(buf, sizeof(buf), "%2d", MIN(current_player_ptr->pclass, MAX_CLASS));
470 memcpy(the_score.p_c, buf, 3);
471 snprintf(buf, sizeof(buf), "%2d", MIN(current_player_ptr->pseikaku, MAX_PERSONALITIES));
472 memcpy(the_score.p_a, buf, 3);
474 /* Save the level and such */
475 sprintf(the_score.cur_lev, "%3d", MIN((u16b)current_player_ptr->lev, 999));
476 sprintf(the_score.cur_dun, "%3d", (int)current_player_ptr->current_floor_ptr->dun_level);
477 sprintf(the_score.max_lev, "%3d", MIN((u16b)current_player_ptr->max_plv, 999));
478 sprintf(the_score.max_dun, "%3d", (int)max_dlv[current_player_ptr->dungeon_idx]);
480 /* Save the cause of death (31 chars) */
481 if (strlen(current_player_ptr->died_from) >= sizeof(the_score.how)) {
483 angband_strcpy(the_score.how, current_player_ptr->died_from, sizeof(the_score.how) - 2);
484 strcat(the_score.how, "…");
486 angband_strcpy(the_score.how, current_player_ptr->died_from, sizeof(the_score.how) - 3);
487 strcat(the_score.how, "...");
490 strcpy(the_score.how, current_player_ptr->died_from);
493 /* Grab permissions */
494 safe_setuid_grab(current_player_ptr);
496 /* Lock (for writing) the highscore file, or fail */
497 errr err = fd_lock(highscore_fd, F_WRLCK);
499 /* Drop permissions */
505 /* Add a new entry to the score list, see where it went */
506 int j = highscore_add(&the_score);
508 /* Grab permissions */
509 safe_setuid_grab(current_player_ptr);
511 /* Unlock the highscore file, or fail */
512 err = fd_lock(highscore_fd, F_UNLCK);
514 /* Drop permissions */
520 /* Hack -- Display the top fifteen scores */
522 display_scores_aux(0, 15, j, NULL);
526 /* Display the scores surrounding the player */
527 display_scores_aux(0, 5, j, NULL);
528 display_scores_aux(j - 2, j + 7, j, NULL);
533 * @brief プレイヤーの現在のスコアをランキングに挟む /
534 * Predict the players location, and display it.
537 errr predict_score(player_type *current_player_ptr)
539 high_score the_score;
543 if (highscore_fd < 0) {
544 msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
549 /* Save the version */
550 sprintf(the_score.what, "%u.%u.%u", FAKE_VER_MAJOR, FAKE_VER_MINOR, FAKE_VER_PATCH);
552 /* Calculate and save the points */
553 sprintf(the_score.pts, "%9ld", (long)calc_score(current_player_ptr));
555 /* Save the current gold */
556 sprintf(the_score.gold, "%9lu", (long)current_player_ptr->au);
558 /* Save the current turn */
559 sprintf(the_score.turns, "%9lu", (long)turn_real(current_player_ptr, current_world_ptr->game_turn));
561 /* Hack -- no time needed */
562 strcpy(the_score.day, _("今日", "TODAY"));
564 /* Save the player name (15 chars) */
565 sprintf(the_score.who, "%-.15s", current_player_ptr->name);
567 /* Save the player info */
568 sprintf(the_score.uid, "%7u", current_player_ptr->player_uid);
569 sprintf(the_score.sex, "%c", (current_player_ptr->psex ? 'm' : 'f'));
570 snprintf(buf, sizeof(buf), "%2d", MIN(current_player_ptr->prace, MAX_RACES));
571 memcpy(the_score.p_r, buf, 3);
572 snprintf(buf, sizeof(buf), "%2d", MIN(current_player_ptr->pclass, MAX_CLASS));
573 memcpy(the_score.p_c, buf, 3);
574 snprintf(buf, sizeof(buf), "%2d", MIN(current_player_ptr->pseikaku, MAX_PERSONALITIES));
575 memcpy(the_score.p_a, buf, 3);
577 /* Save the level and such */
578 sprintf(the_score.cur_lev, "%3d", MIN((u16b)current_player_ptr->lev, 999));
579 sprintf(the_score.cur_dun, "%3d", (int)current_player_ptr->current_floor_ptr->dun_level);
580 sprintf(the_score.max_lev, "%3d", MIN((u16b)current_player_ptr->max_plv, 999));
581 sprintf(the_score.max_dun, "%3d", (int)max_dlv[current_player_ptr->dungeon_idx]);
583 /* Hack -- no cause of death */
584 /* まだ死んでいないときの識別文字 */
585 strcpy(the_score.how, _("yet", "nobody (yet!)"));
587 /* See where the entry would be placed */
588 int j = highscore_where(&the_score);
590 /* Hack -- Display the top fifteen scores */
592 display_scores_aux(0, 15, j, &the_score);
596 display_scores_aux(0, 5, -1, NULL);
597 display_scores_aux(j - 2, j + 7, j, &the_score);
602 * @brief スコアランキングの簡易表示 /
603 * show_highclass - selectively list highscores based on class -KMW-
605 void show_highclass(player_type *current_player_ptr)
608 char buf[1024], out_val[256];
609 path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
611 highscore_fd = fd_open(buf, O_RDONLY);
613 if (highscore_fd < 0) {
614 msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
619 if (highscore_seek(0))
622 high_score the_score;
623 for (int i = 0; i < MAX_HISCORES; i++)
624 if (highscore_read(&the_score))
629 PLAYER_LEVEL clev = 0;
631 while ((m < 9) && (j < MAX_HISCORES)) {
632 if (highscore_seek(j))
634 if (highscore_read(&the_score))
636 pr = atoi(the_score.p_r);
637 clev = (PLAYER_LEVEL)atoi(the_score.cur_lev);
640 sprintf(out_val, " %3d) %sの%s (レベル %2d)", (m + 1), race_info[pr].title, the_score.who, clev);
642 sprintf(out_val, "%3d) %s the %s (Level %2d)", (m + 1), the_score.who, race_info[pr].title, clev);
645 prt(out_val, (m + 7), 0);
651 sprintf(out_val, "あなた) %sの%s (レベル %2d)", race_info[current_player_ptr->prace].title, current_player_ptr->name, current_player_ptr->lev);
653 sprintf(out_val, "You) %s the %s (Level %2d)", current_player_ptr->name, race_info[current_player_ptr->prace].title, current_player_ptr->lev);
656 prt(out_val, (m + 8), 0);
658 (void)fd_close(highscore_fd);
660 prt(_("何かキーを押すとゲームに戻ります", "Hit any key to continue"), 0, 0);
664 for (j = 5; j < 18; j++)
670 * @brief スコアランキングの簡易表示(種族毎)サブルーチン /
672 * @param race_num 種族ID
674 void race_score(player_type *current_player_ptr, int race_num)
677 int pr, clev, lastlev;
678 high_score the_score;
679 char buf[1024], out_val[256], tmp_str[80];
683 /* rr9: TODO - pluralize the race */
684 sprintf(tmp_str, _("最高の%s", "The Greatest of all the %s"), race_info[race_num].title);
687 path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
689 highscore_fd = fd_open(buf, O_RDONLY);
691 if (highscore_fd < 0) {
692 msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
697 if (highscore_seek(0))
700 for (i = 0; i < MAX_HISCORES; i++) {
701 if (highscore_read(&the_score))
708 while ((m < 10) || (j < MAX_HISCORES)) {
709 if (highscore_seek(j))
711 if (highscore_read(&the_score))
713 pr = atoi(the_score.p_r);
714 clev = atoi(the_score.cur_lev);
716 if (pr == race_num) {
718 sprintf(out_val, " %3d) %sの%s (レベル %2d)", (m + 1), race_info[pr].title, the_score.who, clev);
720 sprintf(out_val, "%3d) %s the %s (Level %3d)", (m + 1), the_score.who, race_info[pr].title, clev);
723 prt(out_val, (m + 7), 0);
730 /* add player if qualified */
731 if ((current_player_ptr->prace == race_num) && (current_player_ptr->lev >= lastlev)) {
733 sprintf(out_val, "あなた) %sの%s (レベル %2d)", race_info[current_player_ptr->prace].title, current_player_ptr->name, current_player_ptr->lev);
735 sprintf(out_val, "You) %s the %s (Level %3d)", current_player_ptr->name, race_info[current_player_ptr->prace].title, current_player_ptr->lev);
738 prt(out_val, (m + 8), 0);
741 (void)fd_close(highscore_fd);
746 * @brief スコアランキングの簡易表示(種族毎)メインルーチン /
749 void race_legends(player_type *current_player_ptr)
751 for (int i = 0; i < MAX_RACES; i++) {
752 race_score(current_player_ptr, i);
753 msg_print(_("何かキーを押すとゲームに戻ります", "Hit any key to continue"));
755 for (int j = 5; j < 19; j++)
762 * Display some character info
764 bool check_score(player_type *current_player_ptr)
769 if (highscore_fd < 0) {
770 msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
775 /* Wizard-mode pre-empts scoring */
776 if (current_world_ptr->noscore & 0x000F) {
777 msg_print(_("ウィザード・モードではスコアが記録されません。", "Score not registered for wizards."));
782 /* Cheaters are not scored */
783 if (current_world_ptr->noscore & 0xFF00) {
784 msg_print(_("詐欺をやった人はスコアが記録されません。", "Score not registered for cheaters."));
790 if (!current_world_ptr->total_winner && streq(current_player_ptr->died_from, _("強制終了", "Interrupting"))) {
791 msg_print(_("強制終了のためスコアが記録されません。", "Score not registered due to interruption."));
797 if (!current_world_ptr->total_winner && streq(current_player_ptr->died_from, _("途中終了", "Quitting"))) {
798 msg_print(_("途中終了のためスコアが記録されません。", "Score not registered due to quitting."));