Debian Codegraph @

similar/main/scores.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Inferno High Scores and Statistics System
23 *
24 */
25
26 #include "dxxsconf.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sstream>
31 #include <ranges>
32 #include <ctype.h>
33
34 #include "scores.h"
35 #include "dxxerror.h"
36 #include "pstypes.h"
37 #include "window.h"
38 #include "gr.h"
39 #include "key.h"
40 #include "mouse.h"
41 #include "palette.h"
42 #include "game.h"
43 #include "gamefont.h"
44 #include "u_mem.h"
45 #include "newmenu.h"
46 #include "menu.h"
47 #include "player.h"
48 #include "object.h"
49 #include "screens.h"
50 #include "gamefont.h"
51 #include "mouse.h"
52 #include "joy.h"
53 #include "timer.h"
54 #include "text.h"
55 #include "strutil.h"
56 #include "physfsx.h"
57 #include "d_enumerate.h"
58 #include "d_levelstate.h"
59 #include "d_range.h"
60 #include "d_zip.h"
61 #include "serial.h"
62
63 #define SCORES_FILENAME "descent.hi"
64 #define COOL_MESSAGE_LEN 50
65 namespace dcx {
66
67 namespace {
68
69 constexpr std::integral_constant<unsigned, 10> MAX_HIGH_SCORES{};
70 constexpr std::uint8_t high_score_version{1};
71
72 struct score_items_context
73 {
74 const font_x_scaled_float name, score, difficulty, levels, time_played;
75 score_items_context(const font_x_scale_float fspacx, const unsigned border_x) :
76 name{fspacx(43) + border_x}, score{fspacx(134) + border_x}, difficulty{fspacx(151) + border_x}, levels{fspacx(217) + border_x}, time_played{fspacx(276) + border_x}
77 {
78 }
79 };
80
81 }
82
83 }
84
85 namespace dsx {
86
87 namespace {
88
89 struct stats_info
90 {
91 #if DXX_BUILD_DESCENT == 1
92 static constexpr std::size_t disk_size{24};
93 #elif DXX_BUILD_DESCENT == 2
94 static constexpr std::size_t disk_size{28};
95 #endif
96 callsign_t name;
97 int score;
98 sbyte starting_level;
99 sbyte ending_level;
100 sbyte diff_level;
101 short kill_ratio; // 0-100
102 short hostage_ratio; //
103 int seconds; // How long it took in seconds...
104 };
105
106 struct all_scores
107 {
108 #if DXX_BUILD_DESCENT == 1
109 /* Descent 1 has `stats` immediately after `cool_saying`. */
110 static constexpr std::size_t offsetof_stats{54};
111 static constexpr std::size_t disk_size{294};
112 #elif DXX_BUILD_DESCENT == 2
113 /* Descent 2 has a 2 byte hole before `stats`. */
114 static constexpr std::size_t offsetof_stats{56};
115 static constexpr std::size_t disk_size{336};
116 #endif
117 char cool_saying[COOL_MESSAGE_LEN];
118 std::array<stats_info, MAX_HIGH_SCORES> stats;
119 };
120 #if DXX_BUILD_DESCENT == 1
121 #define DXX_SCORE_STATS_INFO_PAD(AMOUNT) /* In Descent 1, stats are stored with no padding, so discard the amount field. */
122 #define TXT_REGISTER_DESCENT "CD-Enhanced Descent Coming Summer '95!"
123 #elif DXX_BUILD_DESCENT == 2
124 #define DXX_SCORE_STATS_INFO_PAD(AMOUNT) /* In Descent 2, stats are stored with padding determined by the ABI of the original DOS compiler, so propagate the amount field to `serial::pad`. */ \
125 serial::pad<AMOUNT, 0>(),
126 #define TXT_REGISTER_DESCENT "D2 Strategy Guide available at 1-800-531-2343!"
127 #endif
128
129 DEFINE_SERIAL_UDT_TO_MESSAGE(stats_info, s, (
130 s.name.a,
131 DXX_SCORE_STATS_INFO_PAD(3)
132 s.score,
133 s.starting_level,
134 s.ending_level,
135 s.diff_level,
136 DXX_SCORE_STATS_INFO_PAD(1)
137 s.kill_ratio,
138 s.hostage_ratio,
139 s.seconds)
140 );
141 ASSERT_SERIAL_UDT_MESSAGE_SIZE(stats_info, stats_info::disk_size);
142
143 void scores_view(grs_canvas &canvas, const stats_info *last_game, int citem);
144
145 [[nodiscard]]
146 static auto build_builtin_placeholder_scores() ->
147 #ifdef USE_BUILTIN_ENGLISH_TEXT_STRINGS
148 const all_scores &
149 #else
150 all_scores
151 #endif
152 {
153 static constexpr all_scores builtin_scores{
154 .cool_saying =
155 #ifdef USE_BUILTIN_ENGLISH_TEXT_STRINGS
156 {TXT_REGISTER_DESCENT},
157 #else
158 {}, /* For !USE_BUILTIN_ENGLISH_TEXT_STRINGS, this requires data from an external file, so it cannot be set here. */
159 #endif
160 .stats = {{
161 {
162 .name = {{"Parallax"}},
163 .score = 10000,
164 .starting_level = 0,
165 .ending_level = 0,
166 .diff_level = 0,
167 .kill_ratio = 0,
168 .hostage_ratio = 0,
169 .seconds = 0,
170 },
171 {
172 .name = {{"Matt"}},
173 .score = 9000,
174 .starting_level = 0,
175 .ending_level = 0,
176 .diff_level = 0,
177 .kill_ratio = 0,
178 .hostage_ratio = 0,
179 .seconds = 0,
180 },
181 {
182 .name = {{"Mike"}},
183 .score = 8000,
184 .starting_level = 0,
185 .ending_level = 0,
186 .diff_level = 0,
187 .kill_ratio = 0,
188 .hostage_ratio = 0,
189 .seconds = 0,
190 },
191 {
192 .name = {{"Adam"}},
193 .score = 7000,
194 .starting_level = 0,
195 .ending_level = 0,
196 .diff_level = 0,
197 .kill_ratio = 0,
198 .hostage_ratio = 0,
199 .seconds = 0,
200 },
201 {
202 .name = {{"Mark"}},
203 .score = 6000,
204 .starting_level = 0,
205 .ending_level = 0,
206 .diff_level = 0,
207 .kill_ratio = 0,
208 .hostage_ratio = 0,
209 .seconds = 0,
210 },
211 {
212 .name = {{"Jasen"}},
213 .score = 5000,
214 .starting_level = 0,
215 .ending_level = 0,
216 .diff_level = 0,
217 .kill_ratio = 0,
218 .hostage_ratio = 0,
219 .seconds = 0,
220 },
221 {
222 .name = {{"Samir"}},
223 .score = 4000,
224 .starting_level = 0,
225 .ending_level = 0,
226 .diff_level = 0,
227 .kill_ratio = 0,
228 .hostage_ratio = 0,
229 .seconds = 0,
230 },
231 {
232 .name = {{"Doug"}},
233 .score = 3000,
234 .starting_level = 0,
235 .ending_level = 0,
236 .diff_level = 0,
237 .kill_ratio = 0,
238 .hostage_ratio = 0,
239 .seconds = 0,
240 },
241 {
242 .name = {{"Dan"}},
243 .score = 2000,
244 .starting_level = 0,
245 .ending_level = 0,
246 .diff_level = 0,
247 .kill_ratio = 0,
248 .hostage_ratio = 0,
249 .seconds = 0,
250 },
251 {
252 .name = {{"Jason"}},
253 .score = 1000,
254 .starting_level = 0,
255 .ending_level = 0,
256 .diff_level = 0,
257 .kill_ratio = 0,
258 .hostage_ratio = 0,
259 .seconds = 0,
260 },
261 }}
262 };
263 #ifdef USE_BUILTIN_ENGLISH_TEXT_STRINGS
264 return builtin_scores;
265 #else
266 auto scores{builtin_scores};
267 strcpy(scores.cool_saying, dxx_gettext(56, TXT_REGISTER_DESCENT));
268 return scores;
269 #endif
270 }
271
272 [[nodiscard]]
273 static all_scores scores_read()
274 {
275 all_scores scores;
276 if (RAIIPHYSFS_File fp{PHYSFS_openRead(SCORES_FILENAME)}; !fp)
277 {
278 // No error message needed, code will work without a scores file
279 [[unlikely]];
280 }
281 else if (const auto fsize{PHYSFS_fileLength(fp)}; fsize != all_scores::disk_size)
282 {
283 [[unlikely]];
284 con_printf(CON_NORMAL, "Ignoring high scores file \"" SCORES_FILENAME "\" due to unexpected size %" DXX_PRI_size_type, static_cast<std::size_t>(fsize));
285 }
286 // Read 'em in...
287 else if (std::array<uint8_t, all_scores::disk_size> buf; PHYSFSX_readBytes(fp, buf.data(), buf.size()) != all_scores::disk_size)
288 {
289 [[unlikely]];
290 con_puts(CON_NORMAL, "Failed to read high scores file \"" SCORES_FILENAME "\"");
291 }
292 else if (buf[0] != 'D' || // Descent
293 buf[1] != 'H' || // High
294 buf[2] != 'S' || // Scores
295 buf[3] != high_score_version)
296 {
297 [[unlikely]];
298 con_printf(CON_NORMAL, "Ignoring high scores file \"" SCORES_FILENAME "\" due to invalid header %.2x%.2x%.2x%.2x", buf[0], buf[1], buf[2], buf[3]);
299 }
300 else
301 {
302 scores = {};
303 /* Skip the last byte, so that the value-initialized null at the end of
304 * `all_scores::cool_saying` is retained. On a well-formed file, this
305 * produces the same result as including the last byte. On an ill-formed
306 * file, this guarantees null termination of `all_scores::cool_saying`.
307 */
308 std::ranges::copy(std::span(buf).subspan<4, COOL_MESSAGE_LEN - 1>(), scores.cool_saying);
309 const auto stats_static_span{std::span(buf).subspan<all_scores::offsetof_stats>()};
310 static_assert(stats_static_span.extent == 10 * stats_info::disk_size);
311 static_assert(all_scores::offsetof_stats + stats_static_span.extent == all_scores::disk_size);
312 for (auto &&[idx, stats] : enumerate(scores.stats))
313 {
314 serial::reader::ne_bytebuffer b{stats_static_span.subspan(idx * stats_info::disk_size, stats_info::disk_size).data()};
315 serial::process_buffer(b, stats);
316 }
317 return scores;
318 }
319 scores = build_builtin_placeholder_scores();
320 return scores;
321 }
322
323 static void scores_write(const all_scores *scores)
324 {
325 RAIIPHYSFS_File fp{PHYSFS_openWrite(SCORES_FILENAME)};
326 if (!fp)
327 {
328 nm_messagebox(menu_title{TXT_WARNING}, {TXT_OK}, "%s\n'%s'", TXT_UNABLE_TO_OPEN, SCORES_FILENAME);
329 return;
330 }
331
332 std::array<uint8_t, all_scores::disk_size> buf;
333 buf[0] = 'D';
334 buf[1] = 'H';
335 buf[2] = 'S';
336 buf[3] = high_score_version;
337 std::ranges::copy(scores->cool_saying, std::span(buf).subspan<4, COOL_MESSAGE_LEN>().begin());
338 const auto stats_static_span{std::span(buf).subspan<all_scores::offsetof_stats>()};
339 static_assert(stats_static_span.extent == 10 * stats_info::disk_size);
340 static_assert(all_scores::offsetof_stats + stats_static_span.extent == all_scores::disk_size);
341 for (auto &&[idx, stats] : enumerate(scores->stats))
342 {
343 serial::writer::ne_bytebuffer b{stats_static_span.subspan(idx * stats_info::disk_size, stats_info::disk_size).data()};
344 serial::process_buffer(b, stats);
345 }
346 PHYSFSX_writeBytes(fp, std::data(buf), std::size(buf));
347 }
348
349 static void scores_fill_struct(stats_info * stats)
350 {
351 auto &Objects = LevelUniqueObjectState.Objects;
352 auto &vmobjptr = Objects.vmptr;
353 auto &plr = get_local_player();
354 stats->name = plr.callsign;
355 auto &player_info = get_local_plrobj().ctype.player_info;
356 stats->score = player_info.mission.score;
357 stats->ending_level = plr.level;
358 if (const auto robots_total = GameUniqueState.accumulated_robots)
359 stats->kill_ratio = (plr.num_kills_total * 100) / robots_total;
360 else
361 stats->kill_ratio = 0;
362
363 if (const auto hostages_total = GameUniqueState.total_hostages)
364 stats->hostage_ratio = (player_info.mission.hostages_rescued_total * 100) / hostages_total;
365 else
366 stats->hostage_ratio = 0;
367
368 stats->seconds = f2i(plr.time_total) + (plr.hours_total * 3600);
369
370 stats->diff_level = underlying_value(GameUniqueState.Difficulty_level);
371 stats->starting_level = plr.starting_level;
372 }
373
374 }
375
376 }
377
378 namespace dcx {
379
380 namespace {
381
382 static inline const char *get_placement_slot_string(const unsigned position)
383 {
384 switch(position)
385 {
386 default:
387 Int3();
388 [[fallthrough]];
389 case 0: return TXT_1ST;
390 case 1: return TXT_2ND;
391 case 2: return TXT_3RD;
392 case 3: return TXT_4TH;
393 case 4: return TXT_5TH;
394 case 5: return TXT_6TH;
395 case 6: return TXT_7TH;
396 case 7: return TXT_8TH;
397 case 8: return TXT_9TH;
398 case 9: return TXT_10TH;
399 }
400 }
401
402 struct request_user_high_score_comment :
403 std::array<char, sizeof(all_scores::cool_saying)>,
404 std::array<newmenu_item, 2>,
405 newmenu
406 {
407 all_scores &scores;
408 request_user_high_score_comment(all_scores &scores, grs_canvas &canvas) :
409 std::array<newmenu_item, 2>{{
410 newmenu_item::nm_item_text{TXT_COOL_SAYING},
411 newmenu_item::nm_item_input(prepare_input_saying(*this)),
412 }},
413 newmenu(menu_title{TXT_HIGH_SCORE}, menu_subtitle{TXT_YOU_PLACED_1ST}, menu_filename{nullptr}, tiny_mode_flag::normal, tab_processing_flag::ignore, adjusted_citem::create(*static_cast<std::array<newmenu_item, 2> *>(this), 0), canvas),
414 scores(scores)
415 {
416 }
417 virtual window_event_result event_handler(const d_event &) override;
418 static std::array<char, sizeof(all_scores::cool_saying)> &prepare_input_saying(std::array<char, sizeof(all_scores::cool_saying)> &buf)
419 {
420 buf.front() = 0;
421 return buf;
422 }
423 };
424
425 window_event_result request_user_high_score_comment::event_handler(const d_event &event)
426 {
427 switch (event.type)
428 {
429 case event_type::window_close:
430 {
431 std::array<char, sizeof(all_scores::cool_saying)> &text1 = *this;
432 strcpy(scores.cool_saying, text1[0] ? text1.data() : "No comment");
433 }
434 break;
435 default:
436 break;
437 }
438 return newmenu::event_handler(event);
439 }
440
441 }
442
443 }
444
445 namespace dsx {
446
447 void scores_maybe_add_player()
448 {
449 auto &Objects = LevelUniqueObjectState.Objects;
450 auto &vmobjptr = Objects.vmptr;
451 stats_info last_game;
452
453 if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP))
454 return;
455 auto scores{scores_read()};
456 auto &player_info = get_local_plrobj().ctype.player_info;
457 const auto predicate = [player_mission_score = player_info.mission.score](const stats_info &stats) {
458 return player_mission_score > stats.score;
459 };
460 const auto begin_score_stats = std::begin(scores.stats);
461 const auto end_score_stats = std::end(scores.stats);
462 /* Find the position at which the player's score should be placed.
463 */
464 const auto &&iter_position{std::ranges::find_if(begin_score_stats, end_score_stats, predicate)};
465 const auto position = std::distance(begin_score_stats, iter_position);
466 /* If iter_position == end_score_stats, then the player's score does
467 * not beat any of the existing high scores. Include a special case
468 * so that the player's statistics can be shown for the duration of
469 * this menu, despite not being a new record.
470 */
471 stats_info *const ptr_last_game = (iter_position == end_score_stats)
472 ? &last_game
473 : nullptr;
474 if (ptr_last_game)
475 {
476 /* Not a new record */
477 scores_fill_struct(ptr_last_game);
478 } else {
479 /* New record - check whether it is the best score. If so,
480 * allow the player to leave a comment.
481 */
482 if (iter_position == begin_score_stats)
483 {
484 run_blocking_newmenu<request_user_high_score_comment>(scores, grd_curscreen->sc_canvas);
485 } else {
486 /* New record, but not a new best score. Tell the player
487 * what slot the new record earned.
488 */
489 nm_messagebox(menu_title{TXT_HIGH_SCORE}, {TXT_OK}, "%s %s!", TXT_YOU_PLACED, get_placement_slot_string(position));
490 }
491
492 // move everyone down...
493 std::move_backward(iter_position, std::prev(end_score_stats), end_score_stats);
494 scores_fill_struct(iter_position);
495 scores_write(&scores);
496 }
497 scores_view(grd_curscreen->sc_canvas, ptr_last_game, position);
498 }
499
500 }
501
502 namespace dcx {
503
504 namespace {
505
506 dxx_compiler_attribute_nonnull()
507 static void scores_rputs(grs_canvas &canvas, const grs_font &cv_font, const font_x_scaled_float x, const font_y_scaled_float y, const char *const buffer)
508 {
509 const auto &&[w, h] = gr_get_string_size(cv_font, buffer);
510 gr_string(canvas, cv_font, x - w, y, buffer, w, h);
511 }
512
513 static unsigned compute_score_y_coordinate(const unsigned i)
514 {
515 const unsigned y = 59 + i * 9;
516 return i ? y : y - 8;
517 }
518
519 }
520
521 }
522
523 namespace dsx {
524
525 namespace {
526
527 struct scores_menu_items
528 {
529 struct row
530 {
531 callsign_t name;
532 uint8_t diff_level;
533 std::array<char, 16> score;
534 std::array<char, 10> levels;
535 std::array<char, 14> time_played;
536 };
537 struct numbered_row : row
538 {
539 std::array<char, sizeof("10.")> line_number;
540 };
541 const int citem;
542 fix64 time_last_color_change = timer_query();
543 uint8_t looper{0};
544 std::array<numbered_row, MAX_HIGH_SCORES> scores;
545 row last_game;
546 std::array<char, COOL_MESSAGE_LEN> cool_saying;
547 static void prepare_row(row &r, const stats_info &stats, std::ostringstream &oss);
548 static void prepare_row(numbered_row &r, const stats_info &stats, std::ostringstream &oss, unsigned idx);
549 static std::ostringstream build_locale_stringstream();
550 void prepare_scores(const all_scores &all_scores, std::ostringstream &oss);
551 scores_menu_items(const int citem, const all_scores &all_scores, const stats_info *last_game_stats);
552 };
553
554 void scores_menu_items::prepare_row(row &r, const stats_info &stats, std::ostringstream &oss)
555 {
556 r.name = stats.name;
557 r.diff_level = stats.diff_level;
558 {
559 /* This std::ostringstream is shared among multiple rows, to
560 * avoid reinitializing the std::locale each time. Clear the
561 * text before inserting the score for this row.
562 */
563 oss.str("");
564 oss << stats.score;
565 auto &&buffer = oss.str();
566 //replace the digit '1' with special wider 1
567 const auto bb = buffer.begin();
568 const auto be = buffer.end();
569 auto ri = r.score.begin();
570 if (std::distance(bb, be) < r.score.size() - 1)
571 ri = std::replace_copy(bb, be, ri, '1', '\x84');
572 *ri = 0;
573 }
574 {
575 r.levels.front() = 0;
576 auto starting_level = stats.starting_level;
577 auto ending_level = stats.ending_level;
578 if (starting_level || ending_level)
579 {
580 const auto secret_start = (starting_level < 0) ? (starting_level = -starting_level, "S") : "";
581 const auto secret_end = (ending_level < 0) ? (ending_level = -ending_level, "S") : "";
582 auto levels_length = std::snprintf(r.levels.data(), r.levels.size(), "%s%d-%s%d", secret_start, starting_level, secret_end, ending_level);
583 auto lb = r.levels.begin();
584 std::replace(lb, std::next(lb, levels_length), '1', '\x84');
585 }
586 }
587 {
588 const auto &&d1 = std::div(stats.seconds, 60);
589 const auto &&d2 = std::div(d1.rem, 60);
590 auto time_length = std::snprintf(r.time_played.data(), r.time_played.size(), "%d:%02d:%02d", d1.quot, d2.quot, d2.rem);
591 auto tb = r.time_played.begin();
592 std::replace(tb, std::next(tb, time_length), '1', '\x84');
593 }
594 }
595
596 void scores_menu_items::prepare_row(numbered_row &r, const stats_info &stats, std::ostringstream &oss, const unsigned idx)
597 {
598 std::snprintf(r.line_number.data(), r.line_number.size(), "%u.", idx);
599 auto b = r.line_number.begin();
600 std::replace(b, std::next(b, 2), '1', '\x84');
601 prepare_row(r, stats, oss);
602 }
603
604 std::ostringstream scores_menu_items::build_locale_stringstream()
605 {
606 const auto user_preferred_locale = []() {
607 try {
608 /* Use the user's locale if possible. */
609 return std::locale("");
610 } catch (std::runtime_error &) {
611 /* Fall back to the default locale if the user's locale
612 * fails to parse.
613 */
614 return std::locale();
615 }
616 }();
617 std::ostringstream oss;
618 oss.imbue(user_preferred_locale);
619 return oss;
620 }
621
622 void scores_menu_items::prepare_scores(const all_scores &all_scores, std::ostringstream &oss)
623 {
624 for (auto &&[idx, sr, si] : enumerate(zip(scores, all_scores.stats), 1u))
625 prepare_row(sr, si, oss, idx);
626 std::copy(std::begin(all_scores.cool_saying), std::prev(std::end(all_scores.cool_saying)), cool_saying.begin());
627 cool_saying.back() = 0;
628 }
629
630 scores_menu_items::scores_menu_items(const int citem, const all_scores &all_scores, const stats_info *const last_game_stats) :
631 citem(citem)
632 {
633 auto oss = build_locale_stringstream();
634 prepare_scores(all_scores, oss);
635 if (last_game_stats)
636 prepare_row(last_game, *last_game_stats, oss);
637 else
638 last_game = {};
639 }
640
641 struct scores_menu : scores_menu_items, window
642 {
643 scores_menu(grs_canvas &src, int x, int y, int w, int h, int citem, const all_scores &scores, const stats_info *last_game) :
644 scores_menu_items(citem, scores, last_game),
645 window(src, x, y, w, h)
646 {
647 }
648 virtual window_event_result event_handler(const d_event &) override;
649 int get_update_looper();
650 };
651
652 static void scores_draw_item(grs_canvas &canvas, const grs_font &cv_font, const score_items_context &shared_item_context, const unsigned shade, const font_y_scaled_float fspacy_y, const scores_menu_items::row &stats)
653 {
654 gr_set_fontcolor(canvas, BM_XRGB(shade, shade, shade), -1);
655 if (!stats.name[0u])
656 {
657 gr_string(canvas, cv_font, shared_item_context.name, fspacy_y, TXT_EMPTY);
658 return;
659 }
660 gr_string(canvas, cv_font, shared_item_context.name, fspacy_y, stats.name);
661 scores_rputs(canvas, cv_font, shared_item_context.score, fspacy_y, stats.score.data());
662
663 if (const auto d = build_difficulty_level_from_untrusted(stats.diff_level))
664 gr_string(canvas, cv_font, shared_item_context.difficulty, fspacy_y, MENU_DIFFICULTY_TEXT(*d));
665
666 scores_rputs(canvas, cv_font, shared_item_context.levels, fspacy_y, stats.levels.data());
667 scores_rputs(canvas, cv_font, shared_item_context.time_played, fspacy_y, stats.time_played.data());
668 }
669
670 window_event_result scores_menu::event_handler(const d_event &event)
671 {
672 int k;
673 const auto &&fspacx = FSPACX();
674 const auto &&fspacy = FSPACY();
675
676 switch (event.type)
677 {
678 case event_type::window_activated:
679 game_flush_inputs(Controls);
680 break;
681
682 case event_type::key_command:
683 k = event_key_get(event);
684 switch( k ) {
685 case KEY_CTRLED+KEY_R:
686 if (citem < 0)
687 {
688 // Reset scores...
689 if (nm_messagebox_str(menu_title{nullptr}, nm_messagebox_tie(TXT_NO, TXT_YES), menu_subtitle{TXT_RESET_HIGH_SCORES}) == 1)
690 {
691 PHYSFS_delete(SCORES_FILENAME);
692 auto oss{build_locale_stringstream()};
693 prepare_scores(build_builtin_placeholder_scores(), oss);
694 return window_event_result::handled;
695 }
696 }
697 return window_event_result::handled;
698 case KEY_ENTER:
699 case KEY_SPACEBAR:
700 case KEY_ESC:
701 return window_event_result::close;
702 }
703 break;
704
705 case event_type::mouse_button_down:
706 case event_type::mouse_button_up:
707 if (event_mouse_get_button(event) == mbtn::left || event_mouse_get_button(event) == mbtn::right)
708 {
709 return window_event_result::close;
710 }
711 break;
712
713 #if DXX_MAX_BUTTONS_PER_JOYSTICK
714 case event_type::joystick_button_down:
715 return window_event_result::close;
716 #endif
717
718 case event_type::idle:
719 timer_delay2(50);
720 break;
721
722 case event_type::window_draw:
723
724 {
725 auto &canvas = w_canv;
726 nm_draw_background(w_canv, 0, 0, w_canv.cv_bitmap.bm_w, w_canv.cv_bitmap.bm_h);
727 auto &medium3_font = *MEDIUM3_FONT;
728 const auto border_x{BORDERX};
729 const auto border_y{BORDERY};
730 gr_string(canvas, medium3_font, 0x8000, border_y, TXT_HIGH_SCORES);
731 auto &game_font = *GAME_FONT;
732 gr_set_fontcolor(canvas, BM_XRGB(28, 28, 28), -1);
733 gr_printf(canvas, game_font, 0x8000, fspacy(16) + border_y, "\"%s\" - %s", cool_saying.data(), static_cast<const char *>(scores[0].name));
734 const font_x_scaled_float fspacx_line_number{fspacx(34) + border_x};
735 const score_items_context shared_item_context(fspacx, border_x);
736 gr_set_fontcolor(canvas, BM_XRGB(31, 26, 5), -1);
737 const auto x_header = fspacx(48) + border_x;
738 const auto fspacy_column_labels = fspacy(35) + border_y;
739 gr_string(canvas, game_font, x_header, fspacy_column_labels, TXT_NAME);
740 gr_string(canvas, game_font, x_header + fspacx(51), fspacy_column_labels, TXT_SCORE);
741 gr_string(canvas, game_font, x_header + fspacx(96), fspacy_column_labels, TXT_SKILL);
742 gr_string(canvas, game_font, x_header + fspacx(139), fspacy_column_labels, TXT_LEVELS);
743 gr_string(canvas, game_font, x_header + fspacx(197), fspacy_column_labels, TXT_TIME);
744
745 if (citem < 0)
746 gr_string(canvas, game_font, 0x8000, fspacy(125) + fspacy_column_labels, TXT_PRESS_CTRL_R);
747
748 for (const auto &&[idx, stat] : enumerate(scores))
749 {
750 const auto shade = (idx == citem)
751 ? get_update_looper()
752 : 28 - idx * 2;
753 const unsigned y = compute_score_y_coordinate(idx);
754 const font_y_scaled_float fspacy_y(fspacy(y) + border_y);
755 scores_draw_item(canvas, game_font, shared_item_context, shade, fspacy_y, stat);
756 scores_rputs(canvas, game_font, fspacx_line_number, fspacy_y, stat.line_number.data());
757 }
758
759 if (citem == MAX_HIGH_SCORES)
760 {
761 const auto shade = get_update_looper();
762 scores_draw_item(canvas, game_font, shared_item_context, shade, fspacy(compute_score_y_coordinate(citem) + 8), last_game);
763 }
764 }
765 break;
766 case event_type::window_close:
767 break;
768 default:
769 break;
770 }
771 return window_event_result::ignored;
772 }
773
774 int scores_menu::get_update_looper()
775 {
776 if (const auto t2 = timer_query(); t2 >= time_last_color_change + F1_0 / 128)
777 {
778 time_last_color_change = t2;
779 if (++ looper >= fades.size())
780 looper = 0;
781 }
782 const auto shade = 7 + fades[looper];
783 return shade;
784 }
785
786 void scores_view(grs_canvas &canvas, const stats_info *const last_game, int citem)
787 {
788 const auto &&fspacx290 = FSPACX(290);
789 const auto &&fspacy170 = FSPACY(170);
790 auto scores{scores_read()};
791 const auto border_x = get_border_x(canvas);
792 const auto border_y = get_border_y(canvas);
793 auto menu = window_create<scores_menu>(canvas, ((canvas.cv_bitmap.bm_w - fspacx290) / 2) - border_x, ((canvas.cv_bitmap.bm_h - fspacy170) / 2) - border_y, fspacx290 + (border_x * 2), fspacy170 + (border_y * 2), citem, scores, last_game);
794 (void)menu;
795
796 newmenu_free_background();
797
798 set_screen_mode(SCREEN_MENU);
799 show_menus();
800 }
801
802 }
803
804 void scores_view_menu(grs_canvas &canvas)
805 {
806 scores_view(canvas, nullptr, -1);
807 }
808
809 }