/
hiscores.cc
3012 lines (2551 loc) · 84.8 KB
/
hiscores.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* @brief deal with reading and writing of highscore file
**/
/*
* ----------- MODIFYING THE PRINTED SCORE FORMAT ---------------------
* Do this at your leisure. Change hiscores_format_single() as much
* as you like.
*
*/
#include "AppHdr.h"
#include "hiscores.h"
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <memory>
#ifndef TARGET_COMPILER_VC
#include <unistd.h>
#endif
#include "branch.h"
#include "chardump.h"
#include "cio.h"
#include "dungeon.h"
#include "end.h"
#include "english.h"
#include "files.h"
#include "initfile.h"
#include "item-prop.h"
#include "item-status-flag-type.h"
#include "items.h"
#include "jobs.h"
#include "kills.h"
#include "libutil.h"
#include "menu.h"
#include "misc.h"
#include "mon-util.h"
#include "options.h"
#include "ouch.h"
#include "place.h"
#include "religion.h"
#include "scroller.h"
#include "skills.h"
#include "state.h"
#include "status.h"
#include "stringutil.h"
#ifdef USE_TILE
#include "tilepick.h"
#endif
#ifdef USE_TILE_LOCAL
#include "tilereg-crt.h"
#endif
#include "unwind.h"
#include "version.h"
#include "outer-menu.h"
using namespace ui;
#define SCORE_VERSION "0.1"
// enough memory allocated to snarf in the scorefile entries
static unique_ptr<scorefile_entry> hs_list[SCORE_FILE_ENTRIES];
static int hs_list_size = 0;
static bool hs_list_initalized = false;
static FILE *_hs_open(const char *mode, const string &filename);
static void _hs_close(FILE *handle, const string &filename);
static bool _hs_read(FILE *scores, scorefile_entry &dest);
static void _hs_write(FILE *scores, scorefile_entry &entry);
static time_t _parse_time(const string &st);
static string _xlog_escape(const string &s);
static string _xlog_unescape(const string &s);
static vector<string> _xlog_split_fields(const string &s);
static string _score_file_name()
{
string ret;
if (!SysEnv.scorefile.empty())
ret = SysEnv.scorefile;
else
ret = Options.shared_dir + "scores";
ret += crawl_state.game_type_qualifier();
if (crawl_state.game_is_sprint() && !crawl_state.map.empty())
ret += "-" + crawl_state.map;
return ret;
}
static string _log_file_name()
{
return Options.shared_dir + "logfile" + crawl_state.game_type_qualifier();
}
int hiscores_new_entry(const scorefile_entry &ne)
{
unwind_bool score_update(crawl_state.updating_scores, true);
FILE *scores;
int i;
bool inserted = false;
int newest_entry = -1;
// open highscore file (reading) -- nullptr is fatal!
//
// Opening as a+ instead of r+ to force an exclusive lock (see
// hs_open) and to create the file if it's not there already.
scores = _hs_open("a+", _score_file_name());
if (scores == nullptr)
end(1, true, "failed to open score file for writing");
// we're at the end of the file, seek back to beginning.
fseek(scores, 0, SEEK_SET);
// read highscore file, inserting new entry at appropriate point,
for (i = 0; i < SCORE_FILE_ENTRIES; i++)
{
hs_list[i].reset(new scorefile_entry);
if (_hs_read(scores, *hs_list[i]) == false)
break;
// compare points..
if (!inserted && ne.get_score() >= hs_list[i]->get_score())
{
newest_entry = i; // for later printing
inserted = true;
// copy read entry to i+1th position
// Fixed a nasty overflow bug here -- Sharp
if (i+1 < SCORE_FILE_ENTRIES)
{
hs_list[i + 1] = move(hs_list[i]);
hs_list[i].reset(new scorefile_entry(ne));
i++;
}
else
*hs_list[i] = ne; // copy new entry to current position
}
}
// special case: lowest score, with room
if (!inserted && i < SCORE_FILE_ENTRIES)
{
newest_entry = i;
inserted = true;
// copy new entry
hs_list[i].reset(new scorefile_entry(ne));
i++;
}
hs_list_size = i;
hs_list_initalized = true;
// If we've still not inserted it, it's not a highscore.
if (!inserted)
{
_hs_close(scores, _score_file_name());
return -1;
}
// The old code closed and reopened the score file, leading to a
// race condition where one Crawl process could overwrite the
// other's highscore. Now we truncate and rewrite the file without
// closing it.
if (ftruncate(fileno(scores), 0))
end(1, true, "unable to truncate scorefile");
rewind(scores);
// write scorefile entries.
for (i = 0; i < hs_list_size; i++)
{
_hs_write(scores, *hs_list[i]);
// Leave in memory. Does this anyway if !inserted.
// Can write cleanup function if nessicary??
// hs_list[i].reset(nullptr);
}
// close scorefile.
_hs_close(scores, _score_file_name());
return newest_entry;
}
void logfile_new_entry(const scorefile_entry &ne)
{
unwind_bool logfile_update(crawl_state.updating_scores, true);
FILE *logfile;
scorefile_entry le = ne;
// open logfile (appending) -- nullptr *is* fatal here.
logfile = _hs_open("a", _log_file_name());
if (logfile == nullptr)
{
mprf(MSGCH_ERROR, "ERROR: failure writing to the logfile.");
return;
}
_hs_write(logfile, le);
// close logfile.
_hs_close(logfile, _log_file_name());
}
template <class t_printf>
static void _hiscores_print_entry(const scorefile_entry &se,
int index, int format, t_printf pf)
{
char buf[200];
// print position (tracked implicitly by order score file)
snprintf(buf, sizeof buf, "%3d.", index + 1);
pf("%s", buf);
string entry;
// format the entry
if (format == SCORE_TERSE)
entry = hiscores_format_single(se);
else
entry = hiscores_format_single_long(se, (format == SCORE_VERBOSE));
entry += "\n";
pf("%s", entry.c_str());
}
// Reads hiscores file to memory
void hiscores_read_to_memory()
{
FILE *scores;
int i;
// open highscore file (reading)
scores = _hs_open("r", _score_file_name());
if (scores == nullptr)
return;
// read highscore file
for (i = 0; i < SCORE_FILE_ENTRIES; i++)
{
hs_list[i].reset(new scorefile_entry);
if (_hs_read(scores, *hs_list[i]) == false)
break;
}
hs_list_size = i;
hs_list_initalized = true;
//close off
_hs_close(scores, _score_file_name());
}
// Writes all entries in the scorefile to stdout in human-readable form.
void hiscores_print_all(int display_count, int format)
{
unwind_bool scorefile_display(crawl_state.updating_scores, true);
FILE *scores = _hs_open("r", _score_file_name());
if (scores == nullptr)
{
// will only happen from command line
puts("No scores.");
return;
}
for (int entry = 0; display_count <= 0 || entry < display_count; ++entry)
{
scorefile_entry se;
if (!_hs_read(scores, se))
break;
if (format == -1)
printf("%s", se.raw_string().c_str());
else
_hiscores_print_entry(se, entry, format, printf);
}
_hs_close(scores, _score_file_name());
}
// Displays high scores using curses. For output to the console, use
// hiscores_print_all.
string hiscores_print_list(int display_count, int format, int newest_entry, int& start_out)
{
unwind_bool scorefile_display(crawl_state.updating_scores, true);
string ret;
// Additional check to preserve previous functionality
if (!hs_list_initalized) {
hiscores_read_to_memory();
}
int i, total_entries;
if (display_count <= 0)
return "";
total_entries = hs_list_size;
int start = newest_entry - display_count / 2;
if (start + display_count > total_entries)
start = total_entries - display_count;
if (start < 0)
start = 0;
const int finish = start + display_count;
for (i = start; i < finish && i < total_entries; i++)
{
// check for recently added entry
if (i == newest_entry)
ret += "<yellow>";
_hiscores_print_entry(*hs_list[i], i, format, [&ret](const char *fmt, const char *s){
ret += string(s);
});
// return to normal color for next entry
if (i == newest_entry)
ret += "<lightgrey>";
}
start_out = start;
return ret;
}
static void _show_morgue(scorefile_entry& se)
{
int flags = FS_PREWRAPPED_TEXT;
formatted_scroller morgue_file(flags);
morgue_file.set_tag("morgue");
morgue_file.set_more();
string morgue_base = morgue_name(se.get_name(), se.get_death_time());
string morgue_path = morgue_directory()
+ strip_filename_unsafe_chars(morgue_base) + ".txt";
FILE* morgue = lk_open("r", morgue_path);
if (!morgue) // TODO: add an error message
return;
char buf[200];
string morgue_text = "";
while (fgets(buf, sizeof buf, morgue) != nullptr)
{
string line = string(buf);
size_t newline_pos = line.find_last_of('\n');
if (newline_pos != string::npos)
line.erase(newline_pos);
morgue_text += "<w>" + replace_all(line, "<", "<<") + "</w>" + '\n';
}
lk_close(morgue, morgue_path);
column_composer cols(2, 40);
cols.add_formatted(
0,
morgue_text,
true);
vector<formatted_string> blines = cols.formatted_lines();
unsigned i;
for (i = 0; i < blines.size(); ++i)
morgue_file.add_formatted_string(blines[i], true);
morgue_file.show();
}
class UIHiscoresMenu : public Widget
{
public:
UIHiscoresMenu();
virtual shared_ptr<Widget> get_child_at_offset(int x, int y) override {
return static_pointer_cast<Widget>(m_root);
}
virtual void _render() override;
virtual SizeReq _get_preferred_size(Direction dim, int prosp_width) override;
virtual void _allocate_region() override;
virtual bool on_event(const wm_event& event) override;
void on_show();
bool done = false;
private:
void _construct_hiscore_table();
void _add_hiscore_row(scorefile_entry& se, int id);
Widget* initial_focus = nullptr;
bool have_allocated {false};
shared_ptr<Box> m_root;
shared_ptr<Text> m_description;
shared_ptr<OuterMenu> m_score_entries;
};
static int nhsr;
UIHiscoresMenu::UIHiscoresMenu()
{
m_root = make_shared<Box>(Widget::VERT);
m_root->_set_parent(this);
m_root->align_cross = Widget::STRETCH;
auto title_hbox = make_shared<Box>(Widget::HORZ);
title_hbox->set_margin_for_sdl(0, 0, 20, 0);
title_hbox->set_margin_for_crt(0, 0, 1, 0);
#ifdef USE_TILE
auto tile = make_shared<Image>();
tile->set_tile(tile_def(TILEG_STARTUP_HIGH_SCORES, TEX_GUI));
title_hbox->add_child(move(tile));
#endif
auto title = make_shared<Text>(formatted_string(
"Dungeon Crawl Stone Soup: High Scores", YELLOW));
title->set_margin_for_sdl(0, 0, 0, 16);
title_hbox->add_child(move(title));
title_hbox->align_main = Widget::CENTER;
title_hbox->align_cross = Widget::CENTER;
m_description = make_shared<Text>(string(9, '\n'));
m_score_entries= make_shared<OuterMenu>(true, 1, 100);
nhsr = 0;
_construct_hiscore_table();
m_root->add_child(move(title_hbox));
if (initial_focus)
{
m_root->add_child(m_description);
m_root->add_child(m_score_entries);
}
else
{
auto placeholder = formatted_string("No high scores yet...", DARKGRAY);
m_root->add_child(make_shared<Text>(placeholder));
initial_focus = this;
}
}
void UIHiscoresMenu::_construct_hiscore_table()
{
FILE *scores = _hs_open("r", _score_file_name());
if (scores == nullptr)
return;
int i;
// read highscore file
for (i = 0; i < SCORE_FILE_ENTRIES; i++)
{
hs_list[i].reset(new scorefile_entry);
if (_hs_read(scores, *hs_list[i]) == false)
break;
}
_hs_close(scores, _score_file_name());
for (int j=0; j<i; j++)
_add_hiscore_row(*hs_list[j], j);
}
void UIHiscoresMenu::_add_hiscore_row(scorefile_entry& se, int id)
{
auto tmp = make_shared<Text>();
tmp->set_text(formatted_string(hiscores_format_single(se)));
auto btn = make_shared<MenuButton>();
tmp->set_margin_for_sdl(2);
btn->set_child(move(tmp));
btn->on(Widget::slots.event, [this, id, se](wm_event ev) {
if (ev.type == WME_MOUSEBUTTONUP && ev.mouse_event.button == MouseEvent::LEFT
|| ev.type == WME_KEYDOWN && ev.key.keysym.sym == CK_ENTER)
{
_show_morgue(*hs_list[id]);
return true;
}
if (ev.type == WME_FOCUSIN)
{
formatted_string desc(hiscores_format_single_long(se, true));
desc.cprintf(string(max(0, 9-count_linebreaks(desc)), '\n'));
m_description->set_text(move(desc));
}
return false;
});
if (!initial_focus)
initial_focus = btn.get();
m_score_entries->add_button(move(btn), 0, nhsr++);
}
void UIHiscoresMenu::_render()
{
m_root->render();
}
void UIHiscoresMenu::on_show()
{
ui::set_focused_widget(initial_focus);
}
SizeReq UIHiscoresMenu::_get_preferred_size(Direction dim, int prosp_width)
{
return m_root->get_preferred_size(dim, prosp_width);
}
void UIHiscoresMenu::_allocate_region()
{
if (!have_allocated)
{
have_allocated = true;
on_show();
}
m_root->allocate_region(m_region);
}
bool UIHiscoresMenu::on_event(const wm_event& ev)
{
if (ev.type != WME_KEYDOWN)
return false;
int key = ev.key.keysym.sym;
if (key_is_escape(key) || key == CK_MOUSE_CMD)
return done = true;
return true;
}
void show_hiscore_table()
{
unwind_var<string> sprintmap(crawl_state.map, crawl_state.sprint_map);
auto hiscore_ui = make_shared<UIHiscoresMenu>();
auto popup = make_shared<ui::Popup>(hiscore_ui);
ui::run_layout(move(popup), hiscore_ui->done);
}
// Trying to supply an appropriate verb for the attack type. -- bwr
static const char *_range_type_verb(const char *const aux)
{
if (strncmp(aux, "Shot ", 5) == 0) // launched
return "shot";
else if (aux[0] == 0 // unknown
|| strncmp(aux, "Hit ", 4) == 0 // thrown
|| strncmp(aux, "volley ", 7) == 0) // manticore spikes
{
return "hit from afar";
}
return "blasted"; // spells, wands
}
string hiscores_format_single(const scorefile_entry &se)
{
return se.hiscore_line(scorefile_entry::DDV_ONELINE);
}
static bool _hiscore_same_day(time_t t1, time_t t2)
{
struct tm *d1 = TIME_FN(&t1);
const int year = d1->tm_year;
const int mon = d1->tm_mon;
const int day = d1->tm_mday;
struct tm *d2 = TIME_FN(&t2);
return d2->tm_mday == day && d2->tm_mon == mon && d2->tm_year == year;
}
static string _hiscore_date_string(time_t time)
{
struct tm *date = TIME_FN(&time);
const char *mons[12] = { "Jan", "Feb", "Mar", "Apr", "May", "June",
"July", "Aug", "Sept", "Oct", "Nov", "Dec" };
return make_stringf("%s %d, %d", mons[date->tm_mon], date->tm_mday,
date->tm_year + 1900);
}
static string _hiscore_newline_string()
{
return "\n ";
}
string hiscores_format_single_long(const scorefile_entry &se, bool verbose)
{
return se.hiscore_line(verbose ? scorefile_entry::DDV_VERBOSE
: scorefile_entry::DDV_NORMAL);
}
// --------------------------------------------------------------------------
// BEGIN private functions
// --------------------------------------------------------------------------
static FILE *_hs_open(const char *mode, const string &scores)
{
// allow reading from standard input
if (scores == "-")
return stdin;
return lk_open(mode, scores);
}
static void _hs_close(FILE *handle, const string &scores)
{
lk_close(handle, scores);
}
static bool _hs_read(FILE *scores, scorefile_entry &dest)
{
char inbuf[1300];
if (!scores || feof(scores))
return false;
memset(inbuf, 0, sizeof inbuf);
dest.reset();
if (!fgets(inbuf, sizeof inbuf, scores))
return false;
return dest.parse(inbuf);
}
static int _val_char(char digit)
{
return digit - '0';
}
static time_t _parse_time(const string &st)
{
struct tm date;
if (st.length() < 15)
return static_cast<time_t>(0);
date.tm_year = _val_char(st[0]) * 1000 + _val_char(st[1]) * 100
+ _val_char(st[2]) * 10 + _val_char(st[3]) - 1900;
date.tm_mon = _val_char(st[4]) * 10 + _val_char(st[5]);
date.tm_mday = _val_char(st[6]) * 10 + _val_char(st[7]);
date.tm_hour = _val_char(st[8]) * 10 + _val_char(st[9]);
date.tm_min = _val_char(st[10]) * 10 + _val_char(st[11]);
date.tm_sec = _val_char(st[12]) * 10 + _val_char(st[13]);
date.tm_isdst = (st[14] == 'D');
return mktime(&date);
}
static void _hs_write(FILE *scores, scorefile_entry &se)
{
fprintf(scores, "%s", se.raw_string().c_str());
}
static const char *kill_method_names[] =
{
"mon", "pois", "cloud", "beam", "lava", "water",
"stupidity", "weakness", "clumsiness", "trap", "leaving", "winning",
"quitting", "wizmode", "draining", "starvation", "freezing", "burning",
"wild_magic", "xom", "rotting", "targeting", "spore",
"tso_smiting", "petrification", "something",
"falling_down_stairs", "acid", "curare",
"beogh_smiting", "divine_wrath", "bounce", "reflect", "self_aimed",
"falling_through_gate", "disintegration", "headbutt", "rolling",
"mirror_damage", "spines", "frailty", "barbs", "being_thrown",
"collision",
};
static const char *_kill_method_name(kill_method_type kmt)
{
COMPILE_CHECK(NUM_KILLBY == ARRAYSZ(kill_method_names));
if (kmt == NUM_KILLBY)
return "";
return kill_method_names[kmt];
}
static kill_method_type _str_to_kill_method(const string &s)
{
COMPILE_CHECK(NUM_KILLBY == ARRAYSZ(kill_method_names));
for (int i = 0; i < NUM_KILLBY; ++i)
{
if (s == kill_method_names[i])
return static_cast<kill_method_type>(i);
}
return NUM_KILLBY;
}
//////////////////////////////////////////////////////////////////////////
// scorefile_entry
scorefile_entry::scorefile_entry(int dam, mid_t dsource, int dtype,
const char *aux, bool death_cause_only,
const char *dsource_name, time_t dt)
{
reset();
init_death_cause(dam, dsource, dtype, aux, dsource_name);
if (!death_cause_only)
init(dt);
}
scorefile_entry::scorefile_entry()
{
// Completely uninitialised, caveat user.
reset();
}
scorefile_entry::scorefile_entry(const scorefile_entry &se)
{
init_from(se);
}
scorefile_entry &scorefile_entry::operator = (const scorefile_entry &se)
{
init_from(se);
return *this;
}
void scorefile_entry::init_from(const scorefile_entry &se)
{
version = se.version;
save_rcs_version = se.save_rcs_version;
save_tag_version = se.save_tag_version;
tiles = se.tiles;
points = se.points;
name = se.name;
race = se.race;
job = se.job;
race_class_name = se.race_class_name;
lvl = se.lvl;
best_skill = se.best_skill;
best_skill_lvl = se.best_skill_lvl;
title = se.title;
death_type = se.death_type;
death_source = se.death_source;
death_source_name = se.death_source_name;
death_source_flags = se.death_source_flags;
auxkilldata = se.auxkilldata;
indirectkiller = se.indirectkiller;
killerpath = se.killerpath;
last_banisher = se.last_banisher;
dlvl = se.dlvl;
absdepth = se.absdepth;
branch = se.branch;
map = se.map;
mapdesc = se.mapdesc;
killer_map = se.killer_map;
final_hp = se.final_hp;
final_max_hp = se.final_max_hp;
final_max_max_hp = se.final_max_max_hp;
final_mp = se.final_mp;
final_max_mp = se.final_max_mp;
final_base_max_mp = se.final_base_max_mp;
damage = se.damage;
source_damage = se.source_damage;
turn_damage = se.turn_damage;
str = se.str;
intel = se.intel;
dex = se.dex;
ac = se.ac;
ev = se.ev;
sh = se.sh;
god = se.god;
piety = se.piety;
penance = se.penance;
wiz_mode = se.wiz_mode;
explore_mode = se.explore_mode;
birth_time = se.birth_time;
death_time = se.death_time;
real_time = se.real_time;
num_turns = se.num_turns;
num_aut = se.num_aut;
num_diff_runes = se.num_diff_runes;
num_runes = se.num_runes;
kills = se.kills;
maxed_skills = se.maxed_skills;
fifteen_skills = se.fifteen_skills;
status_effects = se.status_effects;
gold = se.gold;
gold_spent = se.gold_spent;
gold_found = se.gold_found;
zigs = se.zigs;
zigmax = se.zigmax;
scrolls_used = se.scrolls_used;
potions_used = se.potions_used;
seed = se.seed;
fixup_char_name();
// We could just reset raw_line to "" instead.
raw_line = se.raw_line;
}
actor* scorefile_entry::killer() const
{
return actor_by_mid(death_source);
}
xlog_fields scorefile_entry::get_fields() const
{
if (!fields)
return xlog_fields();
else
return *fields;
}
bool scorefile_entry::parse(const string &line)
{
// Scorefile formats down the ages:
//
// 1) old-style lines which were 80 character blocks
// 2) 4.0 pr1 through pr7 versions which were newline terminated
// 3) 4.0 pr8 and onwards which are colon-separated fields (and
// start with a colon), and may exceed 80 characters!
// 4) 0.2 and onwards, which are xlogfile format - no leading
// colon, fields separated by colons, each field specified as
// key=value. Colons are not allowed in key names, must be escaped to
// :: in values.
//
// 0.3 only reads and writes entries of type (4).
// Leading colon implies 4.0 style line:
if (line[0] == ':')
{
dprf("Corrupted xlog-line: %s", line.c_str());
return false;
}
raw_line = line;
return parse_scoreline(line);
}
string scorefile_entry::raw_string() const
{
if (!raw_line.empty())
return raw_line;
set_score_fields();
if (!fields)
return "";
return fields->xlog_line() + "\n";
}
bool scorefile_entry::parse_scoreline(const string &line)
{
fields.reset(new xlog_fields(line));
init_with_fields();
return true;
}
static const char* _short_branch_name(int branch)
{
if (branch >= 0 && branch < NUM_BRANCHES)
return branches[branch].abbrevname;
return "";
}
enum old_job_type
{
OLD_JOB_THIEF = -1,
OLD_JOB_DEATH_KNIGHT = -2,
OLD_JOB_PALADIN = -3,
OLD_JOB_REAVER = -4,
OLD_JOB_STALKER = -5,
OLD_JOB_JESTER = -6,
OLD_JOB_PRIEST = -7,
OLD_JOB_HEALER = -8,
NUM_OLD_JOBS = -OLD_JOB_HEALER
};
static const char* _job_name(int job)
{
switch (job)
{
case OLD_JOB_THIEF:
return "Thief";
case OLD_JOB_DEATH_KNIGHT:
return "Death Knight";
case OLD_JOB_PALADIN:
return "Paladin";
case OLD_JOB_REAVER:
return "Reaver";
case OLD_JOB_STALKER:
return "Stalker";
case OLD_JOB_JESTER:
return "Jester";
case OLD_JOB_PRIEST:
return "Priest";
case OLD_JOB_HEALER:
return "Healer";
}
return get_job_name(static_cast<job_type>(job));
}
static const char* _job_abbrev(int job)
{
switch (job)
{
case OLD_JOB_THIEF:
return "Th";
case OLD_JOB_DEATH_KNIGHT:
return "DK";
case OLD_JOB_PALADIN:
return "Pa";
case OLD_JOB_REAVER:
return "Re";
case OLD_JOB_STALKER:
return "St";
case OLD_JOB_JESTER:
return "Jr";
case OLD_JOB_PRIEST:
return "Pr";
case OLD_JOB_HEALER:
return "He";
}
return get_job_abbrev(static_cast<job_type>(job));
}
static int _job_by_name(const string& name)
{
int job = get_job_by_name(name.c_str());
if (job != JOB_UNKNOWN)
return job;
for (job = -1; job >= -NUM_OLD_JOBS; job--)
if (name == _job_name(job))
return job;
return JOB_UNKNOWN;
}
enum old_species_type
{
OLD_SP_ELF = -1,
OLD_SP_HILL_DWARF = -2,
OLD_SP_OGRE_MAGE = -3,
OLD_SP_GREY_ELF = -4,
OLD_SP_GNOME = -5,
OLD_SP_MOUNTAIN_DWARF = -6,
OLD_SP_SLUDGE_ELF = -7,
OLD_SP_DJINNI = -8,
OLD_SP_LAVA_ORC = -9,
NUM_OLD_SPECIES = -OLD_SP_LAVA_ORC
};
static string _species_name(int race)
{
switch (race)
{
case OLD_SP_ELF: return "Elf";
case OLD_SP_HILL_DWARF: return "Hill Dwarf";
case OLD_SP_OGRE_MAGE: return "Ogre-Mage";
case OLD_SP_GREY_ELF: return "Grey Elf";
case OLD_SP_GNOME: return "Gnome";
case OLD_SP_MOUNTAIN_DWARF: return "Mountain Dwarf";
case OLD_SP_SLUDGE_ELF: return "Sludge Elf";
case OLD_SP_DJINNI: return "Djinni";
case OLD_SP_LAVA_ORC: return "Lava Orc";
}
return species_name(static_cast<species_type>(race));
}
static const char* _species_abbrev(int race)
{
switch (race)
{
case OLD_SP_ELF: return "El";
case OLD_SP_HILL_DWARF: return "HD";
case OLD_SP_OGRE_MAGE: return "OM";
case OLD_SP_GREY_ELF: return "GE";
case OLD_SP_GNOME: return "Gn";
case OLD_SP_MOUNTAIN_DWARF: return "MD";
case OLD_SP_SLUDGE_ELF: return "SE";
case OLD_SP_DJINNI: return "Dj";
case OLD_SP_LAVA_ORC: return "LO";
}
return get_species_abbrev(static_cast<species_type>(race));
}