OSDN Git Service

Merge pull request #2723 from Hourier/Fix-History-Generator-Humand
[hengbandforosx/hengbandosx.git] / src / core / scores.cpp
1 /*!
2  * @file scores.c
3  * @brief ハイスコア処理 / Highscores handling
4  * @date 2014/07/14
5  * @author
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.
11  */
12
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 "game-option/birth-options.h"
19 #include "game-option/game-play-options.h"
20 #include "io/files-util.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 "locale/japanese.h"
26 #include "player-base/player-race.h"
27 #include "player-info/class-info.h"
28 #include "player/player-personality.h"
29 #include "player/player-status.h"
30 #include "player/race-info-table.h"
31 #include "system/angband-version.h"
32 #include "system/dungeon-info.h"
33 #include "system/floor-type-definition.h"
34 #include "system/player-type-definition.h"
35 #include "term/screen-processor.h"
36 #include "term/term-color-types.h"
37 #include "util/angband-files.h"
38 #include "util/enum-converter.h"
39 #include "util/int-char-converter.h"
40 #include "util/string-processor.h"
41 #include "view/display-messages.h"
42 #include "view/display-scores.h"
43 #include "world/world.h"
44
45 /*!
46  * @brief 所定ポインタへスコア情報を書き込む / Write one score to the highscore file
47  * @param score スコア情報参照ポインタ
48  * @return エラーコード(問題がなければ0を返す)
49  */
50 static int highscore_write(high_score *score)
51 {
52     /* Write the record, note failure */
53     return fd_write(highscore_fd, (char *)(score), sizeof(high_score));
54 }
55
56 /*!
57  * @brief スコア情報を全て得るまで繰り返し取得する / Just determine where a new score *would* be placed
58  * @param score スコア情報参照ポインタ
59  * @return 正常ならば(MAX_HISCORES - 1)、問題があれば-1を返す
60  */
61 static int highscore_where(high_score *score)
62 {
63     /* Paranoia -- it may not have opened */
64     if (highscore_fd < 0) {
65         return -1;
66     }
67
68     /* Go to the start of the highscore file */
69     if (highscore_seek(0)) {
70         return -1;
71     }
72
73     /* Read until we get to a higher score */
74     high_score the_score;
75     int my_score = atoi(score->pts);
76     for (int i = 0; i < MAX_HISCORES; i++) {
77         int old_score;
78         if (highscore_read(&the_score)) {
79             return i;
80         }
81         old_score = atoi(the_score.pts);
82         if (my_score > old_score) {
83             return i;
84         }
85     }
86
87     /* The "last" entry is always usable */
88     return MAX_HISCORES - 1;
89 }
90
91 /*!
92  * @brief スコア情報をバッファの末尾に追加する / Actually place an entry into the high score file
93  * @param score スコア情報参照ポインタ
94  * @return 正常ならば書き込んだスロット位置、問題があれば-1を返す / Return the location (0 is best) or -1 on "failure"
95  */
96 static int highscore_add(high_score *score)
97 {
98     /* Paranoia -- it may not have opened */
99     if (highscore_fd < 0) {
100         return -1;
101     }
102
103     /* Determine where the score should go */
104     int slot = highscore_where(score);
105
106     /* Hack -- Not on the list */
107     if (slot < 0) {
108         return -1;
109     }
110
111     /* Hack -- prepare to dump the new score */
112     high_score the_score = (*score);
113
114     /* Slide all the scores down one */
115     bool done = false;
116     high_score tmpscore;
117     for (int i = slot; !done && (i < MAX_HISCORES); i++) {
118         /* Read the old guy, note errors */
119         if (highscore_seek(i)) {
120             return -1;
121         }
122         if (highscore_read(&tmpscore)) {
123             done = true;
124         }
125
126         /* Back up and dump the score we were holding */
127         if (highscore_seek(i)) {
128             return -1;
129         }
130         if (highscore_write(&the_score)) {
131             return -1;
132         }
133
134         /* Hack -- Save the old score, for the next pass */
135         the_score = tmpscore;
136     }
137
138     /* Return location used */
139     return slot;
140 }
141
142 /*!
143  * @brief スコアサーバへの転送処理
144  * @param player_ptr プレイヤーへの参照ポインタ
145  * @param do_send 実際に転送ア処置を行うか否か
146  * @return 転送が成功したらTRUEを返す
147  */
148 bool send_world_score(PlayerType *player_ptr, bool do_send)
149 {
150 #ifdef WORLD_SCORE
151     if (!send_score || !do_send) {
152         return true;
153     }
154
155     auto is_registration = get_check_strict(
156         player_ptr, _("スコアをスコア・サーバに登録しますか? ", "Do you send score to the world score server? "), (CHECK_NO_ESCAPE | CHECK_NO_HISTORY));
157     if (!is_registration) {
158         return true;
159     }
160
161     prt("", 0, 0);
162     prt(_("送信中..", "Sending..."), 0, 0);
163     term_fresh();
164     screen_save();
165     auto successful_send = report_score(player_ptr);
166     screen_load();
167     if (!successful_send) {
168         return false;
169     }
170
171     prt(_("完了。何かキーを押してください。", "Completed.  Hit any key."), 0, 0);
172     (void)inkey();
173 #else
174     (void)player_ptr;
175     (void)do_send;
176 #endif
177     return true;
178 }
179
180 /*!
181  * @brief スコアの過去二十位内ランキングを表示する
182  * Enters a players name on a hi-score table, if "legal", and in any
183  * case, displays some relevant portion of the high score list.
184  * @param player_ptr プレイヤーへの参照ポインタ
185  * @return エラーコード
186  * @details
187  * Assumes "signals_ignore_tstp()" has been called.
188  */
189 errr top_twenty(PlayerType *player_ptr)
190 {
191     high_score the_score = {};
192     char buf[32];
193
194     /* Save the version */
195     sprintf(the_score.what, "%u.%u.%u", H_VER_MAJOR, H_VER_MINOR, H_VER_PATCH);
196
197     /* Calculate and save the points */
198     sprintf(the_score.pts, "%9ld", (long)calc_score(player_ptr));
199     the_score.pts[9] = '\0';
200
201     /* Save the current gold */
202     sprintf(the_score.gold, "%9lu", (long)player_ptr->au);
203     the_score.gold[9] = '\0';
204
205     /* Save the current turn */
206     sprintf(the_score.turns, "%9lu", (long)turn_real(player_ptr, w_ptr->game_turn));
207     the_score.turns[9] = '\0';
208
209     time_t ct = time((time_t *)0);
210
211     /* Save the date in standard encoded form (9 chars) */
212     strftime(the_score.day, 10, "@%Y%m%d", localtime(&ct));
213
214     /* Save the player name (15 chars) */
215     sprintf(the_score.who, "%-.15s", player_ptr->name);
216
217     /* Save the player info */
218     sprintf(the_score.uid, "%7u", player_ptr->player_uid);
219     sprintf(the_score.sex, "%c", (player_ptr->psex ? 'm' : 'f'));
220     snprintf(buf, sizeof(buf), "%2d", std::min(enum2i(player_ptr->prace), MAX_RACES));
221     memcpy(the_score.p_r, buf, 3);
222     snprintf(buf, sizeof(buf), "%2d", enum2i(std::min(player_ptr->pclass, PlayerClassType::MAX)));
223     memcpy(the_score.p_c, buf, 3);
224     snprintf(buf, sizeof(buf), "%2d", std::min(player_ptr->ppersonality, MAX_PERSONALITIES));
225     memcpy(the_score.p_a, buf, 3);
226
227     /* Save the level and such */
228     sprintf(the_score.cur_lev, "%3d", std::min<ushort>(player_ptr->lev, 999));
229     sprintf(the_score.cur_dun, "%3d", (int)player_ptr->current_floor_ptr->dun_level);
230     sprintf(the_score.max_lev, "%3d", std::min<ushort>(player_ptr->max_plv, 999));
231     sprintf(the_score.max_dun, "%3d", (int)max_dlv[player_ptr->dungeon_idx]);
232
233     /* Save the cause of death (31 chars) */
234     if (player_ptr->died_from.size() >= sizeof(the_score.how)) {
235 #ifdef JP
236         angband_strcpy(the_score.how, player_ptr->died_from.c_str(), sizeof(the_score.how) - 2);
237         strcat(the_score.how, "…");
238 #else
239         angband_strcpy(the_score.how, player_ptr->died_from.c_str(), sizeof(the_score.how) - 3);
240         strcat(the_score.how, "...");
241 #endif
242     } else {
243         angband_strcpy(the_score.how, player_ptr->died_from.c_str(), sizeof(the_score.how));
244     }
245
246     /* Grab permissions */
247     safe_setuid_grab(player_ptr);
248
249     /* Lock (for writing) the highscore file, or fail */
250     errr err = fd_lock(highscore_fd, F_WRLCK);
251
252     /* Drop permissions */
253     safe_setuid_drop();
254
255     if (err) {
256         return 1;
257     }
258
259     /* Add a new entry to the score list, see where it went */
260     int j = highscore_add(&the_score);
261
262     /* Grab permissions */
263     safe_setuid_grab(player_ptr);
264
265     /* Unlock the highscore file, or fail */
266     err = fd_lock(highscore_fd, F_UNLCK);
267
268     /* Drop permissions */
269     safe_setuid_drop();
270
271     if (err) {
272         return 1;
273     }
274
275     /* Hack -- Display the top fifteen scores */
276     if (j < 10) {
277         display_scores(0, 15, j, nullptr);
278         return 0;
279     }
280
281     /* Display the scores surrounding the player */
282     display_scores(0, 5, j, nullptr);
283     display_scores(j - 2, j + 7, j, nullptr);
284     return 0;
285 }
286
287 /*!
288  * @brief プレイヤーの現在のスコアをランキングに挟む /
289  * Predict the players location, and display it.
290  * @return エラーコード
291  */
292 errr predict_score(PlayerType *player_ptr)
293 {
294     high_score the_score;
295     char buf[32];
296
297     /* No score file */
298     if (highscore_fd < 0) {
299         msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
300         msg_print(nullptr);
301         return 0;
302     }
303
304     /* Save the version */
305     sprintf(the_score.what, "%u.%u.%u", H_VER_MAJOR, H_VER_MINOR, H_VER_PATCH);
306
307     /* Calculate and save the points */
308     sprintf(the_score.pts, "%9ld", (long)calc_score(player_ptr));
309
310     /* Save the current gold */
311     sprintf(the_score.gold, "%9lu", (long)player_ptr->au);
312
313     /* Save the current turn */
314     sprintf(the_score.turns, "%9lu", (long)turn_real(player_ptr, w_ptr->game_turn));
315
316     /* Hack -- no time needed */
317     strcpy(the_score.day, _("今日", "TODAY"));
318
319     /* Save the player name (15 chars) */
320     sprintf(the_score.who, "%-.15s", player_ptr->name);
321
322     /* Save the player info */
323     sprintf(the_score.uid, "%7u", player_ptr->player_uid);
324     sprintf(the_score.sex, "%c", (player_ptr->psex ? 'm' : 'f'));
325     snprintf(buf, sizeof(buf), "%2d", std::min(enum2i(player_ptr->prace), MAX_RACES));
326     memcpy(the_score.p_r, buf, 3);
327     snprintf(buf, sizeof(buf), "%2d", enum2i(std::min(player_ptr->pclass, PlayerClassType::MAX)));
328     memcpy(the_score.p_c, buf, 3);
329     snprintf(buf, sizeof(buf), "%2d", std::min(player_ptr->ppersonality, MAX_PERSONALITIES));
330     memcpy(the_score.p_a, buf, 3);
331
332     /* Save the level and such */
333     sprintf(the_score.cur_lev, "%3d", std::min<ushort>(player_ptr->lev, 999));
334     sprintf(the_score.cur_dun, "%3d", (int)player_ptr->current_floor_ptr->dun_level);
335     sprintf(the_score.max_lev, "%3d", std::min<ushort>(player_ptr->max_plv, 999));
336     sprintf(the_score.max_dun, "%3d", (int)max_dlv[player_ptr->dungeon_idx]);
337
338     /* まだ死んでいないときの識別文字 */
339     strcpy(the_score.how, _("yet", "nobody (yet!)"));
340
341     /* See where the entry would be placed */
342     int j = highscore_where(&the_score);
343
344     /* Hack -- Display the top fifteen scores */
345     if (j < 10) {
346         display_scores(0, 15, j, &the_score);
347         return 0;
348     }
349
350     display_scores(0, 5, -1, nullptr);
351     display_scores(j - 2, j + 7, j, &the_score);
352     return 0;
353 }
354
355 /*!
356  * @brief スコアランキングの簡易表示 /
357  * show_highclass - selectively list highscores based on class -KMW-
358  */
359 void show_highclass(PlayerType *player_ptr)
360 {
361     screen_save();
362     char buf[1024], out_val[256];
363     path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
364
365     highscore_fd = fd_open(buf, O_RDONLY);
366
367     if (highscore_fd < 0) {
368         msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
369         msg_print(nullptr);
370         return;
371     }
372
373     if (highscore_seek(0)) {
374         return;
375     }
376
377     high_score the_score;
378     for (int i = 0; i < MAX_HISCORES; i++) {
379         if (highscore_read(&the_score)) {
380             break;
381         }
382     }
383
384     int m = 0;
385     int j = 0;
386     PLAYER_LEVEL clev = 0;
387     int pr;
388     while ((m < 9) && (j < MAX_HISCORES)) {
389         if (highscore_seek(j)) {
390             break;
391         }
392         if (highscore_read(&the_score)) {
393             break;
394         }
395         pr = atoi(the_score.p_r);
396         clev = (PLAYER_LEVEL)atoi(the_score.cur_lev);
397
398 #ifdef JP
399         sprintf(out_val, "   %3d) %sの%s (レベル %2d)", (m + 1), race_info[pr].title, the_score.who, clev);
400 #else
401         sprintf(out_val, "%3d) %s the %s (Level %2d)", (m + 1), the_score.who, race_info[pr].title, clev);
402 #endif
403
404         prt(out_val, (m + 7), 0);
405         m++;
406         j++;
407     }
408
409 #ifdef JP
410     sprintf(out_val, "あなた) %sの%s (レベル %2d)", race_info[enum2i(player_ptr->prace)].title, player_ptr->name, player_ptr->lev);
411 #else
412     sprintf(out_val, "You) %s the %s (Level %2d)", player_ptr->name, race_info[enum2i(player_ptr->prace)].title, player_ptr->lev);
413 #endif
414
415     prt(out_val, (m + 8), 0);
416
417     (void)fd_close(highscore_fd);
418     highscore_fd = -1;
419     prt(_("何かキーを押すとゲームに戻ります", "Hit any key to continue"), 0, 0);
420
421     (void)inkey();
422
423     for (j = 5; j < 18; j++) {
424         prt("", j, 0);
425     }
426     screen_load();
427 }
428
429 /*!
430  * @brief スコアランキングの簡易表示(種族毎)サブルーチン /
431  * Race Legends -KMW-
432  * @param race_num 種族ID
433  */
434 void race_score(PlayerType *player_ptr, int race_num)
435 {
436     int i = 0, j, m = 0;
437     int pr, clev, lastlev;
438     high_score the_score;
439     char buf[1024], out_val[256], tmp_str[80];
440
441     lastlev = 0;
442
443     /* rr9: TODO - pluralize the race */
444     sprintf(tmp_str, _("最高の%s", "The Greatest of all the %s"), race_info[race_num].title);
445
446     prt(tmp_str, 5, 15);
447     path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
448
449     highscore_fd = fd_open(buf, O_RDONLY);
450
451     if (highscore_fd < 0) {
452         msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
453         msg_print(nullptr);
454         return;
455     }
456
457     if (highscore_seek(0)) {
458         return;
459     }
460
461     for (i = 0; i < MAX_HISCORES; i++) {
462         if (highscore_read(&the_score)) {
463             break;
464         }
465     }
466
467     m = 0;
468     j = 0;
469
470     while ((m < 10) || (j < MAX_HISCORES)) {
471         if (highscore_seek(j)) {
472             break;
473         }
474         if (highscore_read(&the_score)) {
475             break;
476         }
477         pr = atoi(the_score.p_r);
478         clev = atoi(the_score.cur_lev);
479
480         if (pr == race_num) {
481 #ifdef JP
482             sprintf(out_val, "   %3d) %sの%s (レベル %2d)", (m + 1), race_info[pr].title, the_score.who, clev);
483 #else
484             sprintf(out_val, "%3d) %s the %s (Level %3d)", (m + 1), the_score.who, race_info[pr].title, clev);
485 #endif
486
487             prt(out_val, (m + 7), 0);
488             m++;
489             lastlev = clev;
490         }
491         j++;
492     }
493
494     /* add player if qualified */
495     if ((enum2i(player_ptr->prace) == race_num) && (player_ptr->lev >= lastlev)) {
496 #ifdef JP
497         sprintf(out_val, "あなた) %sの%s (レベル %2d)", race_info[enum2i(player_ptr->prace)].title, player_ptr->name, player_ptr->lev);
498 #else
499         sprintf(out_val, "You) %s the %s (Level %3d)", player_ptr->name, race_info[enum2i(player_ptr->prace)].title, player_ptr->lev);
500 #endif
501
502         prt(out_val, (m + 8), 0);
503     }
504
505     (void)fd_close(highscore_fd);
506     highscore_fd = -1;
507 }
508
509 /*!
510  * @brief スコアランキングの簡易表示(種族毎)メインルーチン /
511  * Race Legends -KMW-
512  */
513 void race_legends(PlayerType *player_ptr)
514 {
515     for (int i = 0; i < MAX_RACES; i++) {
516         race_score(player_ptr, i);
517         msg_print(_("何かキーを押すとゲームに戻ります", "Hit any key to continue"));
518         msg_print(nullptr);
519         for (int j = 5; j < 19; j++) {
520             prt("", j, 0);
521         }
522     }
523 }
524
525 /*!
526  * @brief スコアファイル出力
527  * Display some character info
528  */
529 bool check_score(PlayerType *player_ptr)
530 {
531     term_clear();
532
533     /* No score file */
534     if (highscore_fd < 0) {
535         msg_print(_("スコア・ファイルが使用できません。", "Score file unavailable."));
536         msg_print(nullptr);
537         return false;
538     }
539
540     /* Wizard-mode pre-empts scoring */
541     if (w_ptr->noscore & 0x000F) {
542         msg_print(_("ウィザード・モードではスコアが記録されません。", "Score not registered for wizards."));
543         msg_print(nullptr);
544         return false;
545     }
546
547     /* Cheaters are not scored */
548     if (w_ptr->noscore & 0xFF00) {
549         msg_print(_("詐欺をやった人はスコアが記録されません。", "Score not registered for cheaters."));
550         msg_print(nullptr);
551         return false;
552     }
553
554     /* Interupted */
555     if (!w_ptr->total_winner && streq(player_ptr->died_from, _("強制終了", "Interrupting"))) {
556         msg_print(_("強制終了のためスコアが記録されません。", "Score not registered due to interruption."));
557         msg_print(nullptr);
558         return false;
559     }
560
561     /* Quitter */
562     if (!w_ptr->total_winner && streq(player_ptr->died_from, _("途中終了", "Quitting"))) {
563         msg_print(_("途中終了のためスコアが記録されません。", "Score not registered due to quitting."));
564         msg_print(nullptr);
565         return false;
566     }
567     return true;
568 }