From 93dcc68acb57bd3b584dc3ad7152ab536c7969b8 Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Thu, 6 Jun 2024 13:37:22 +0200 Subject: [PATCH] naming modes --- hud.cpp | 1 + menus.cpp | 10 ++++++++++ system.cpp | 11 +++++++++++ yendor.cpp | 36 +++++++++++++++++++++++++++++++----- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/hud.cpp b/hud.cpp index e1298ee0..3ee2d330 100644 --- a/hud.cpp +++ b/hud.cpp @@ -719,6 +719,7 @@ EX void drawStats() { } string vers = VER; if(true) { + if(modename.count(current_modecode)) vers == " '" + modename[current_modecode] + "' "; vers += mode_description(); if(peace::on) vers += " peace"; if(racing::on) vers += " racing"; diff --git a/menus.cpp b/menus.cpp index e2c4838f..46e25166 100644 --- a/menus.cpp +++ b/menus.cpp @@ -487,6 +487,8 @@ EX string custom_welcome; string customfile = "custom.hrm"; +string name_to_edit; + EX void show_custom() { cmode = sm::SIDE | sm::MAYDARK; gamescreen(); @@ -506,6 +508,14 @@ EX void show_custom() { dialog::edit_string(custom_welcome, "custom welcome message", ""); }); dialog::addBreak(100); + auto m = at_or_null(modename, current_modecode); + dialog::addSelItem("name custom mode", m ? *m : "", 'N'); + dialog::add_action([] { + name_to_edit = modename[current_modecode]; + dialog::edit_string(name_to_edit, "name custom mode", ""); + dialog::get_di().reaction_final = [] { update_modename(name_to_edit); }; + }); + dialog::addItem("save custom mode", 'S'); dialog::add_action([] { dialog::openFileDialog(customfile, XLAT("file to save:"), ".hrm", [] () { diff --git a/system.cpp b/system.cpp index 6cdd7ea4..703b8f47 100644 --- a/system.cpp +++ b/system.cpp @@ -171,6 +171,8 @@ EX void initgame() { DEBBI(DF_INIT, ("initGame")); callhooks(hooks_initgame); + modecode(); + if(!safety) fix_land_structure_choice(); if(multi::players < 1 || multi::players > MAXPLAYER) @@ -1227,6 +1229,11 @@ EX void loadsave() { while(s != "" && s.back() < 32) s.pop_back(); load_modecode_line(s); } + if(buf[0] == 'N' && buf[1] == 'A') { + string s = buf; + while(s != "" && s.back() < 32) s.pop_back(); + load_modename_line(s); + } if(buf[0] == 'H' && buf[1] == 'y') { if(fscanf(f, "%9999s", buf) <= 0) break; sc.ver = buf; @@ -1834,6 +1841,7 @@ EX void save_mode_to_file(const string& fname) { println(f, modheader); println(f, s); if(custom_welcome != "") println(f, "CMSG ", custom_welcome); + if(modename.count(current_modecode)) println(f, "NAME ", modename[current_modecode]); for(auto& ap: allowed_params) { auto& s = params[ap]; @@ -1851,10 +1859,12 @@ EX void load_mode_from_file(const string& fname) { shstream ss; ss.s = from_hexstring(hex + "00"); custom_welcome = ""; + string custom_name = ""; while(true) { string s = scanline_noblank(f); if(s == "") break; else if(s.substr(0, 5) == "CMSG ") custom_welcome = s.substr(5); + else if(s.substr(0, 5) == "NAME ") custom_name = s.substr(5); else { auto pos = s.find("="); if(pos != string::npos) { @@ -1870,6 +1880,7 @@ EX void load_mode_from_file(const string& fname) { } stop_game(); load_mode_data_with_zero(ss); + if(custom_name != "") { modecode(); update_modename(custom_name); } start_game(); } diff --git a/yendor.cpp b/yendor.cpp index d882297f..afd0ac13 100644 --- a/yendor.cpp +++ b/yendor.cpp @@ -1141,9 +1141,11 @@ EX modecode_t get_identify(modecode_t xc) { /** handle cases where the encoding changed in the new version */ EX string expected_modecode; +EX modecode_t current_modecode; + EX modecode_t modecode(int mode) { modecode_t x = legacy_modecode(); - if(x != UNKNOWN) return x; + if(x != UNKNOWN) return current_modecode = x; xcheat = (cheater ? 1 : 0); shstream ss; @@ -1181,7 +1183,7 @@ EX modecode_t modecode(int mode) { fprintf(f, "MODE %d %s\n", next, s.c_str()); fclose(f); - return next; + return current_modecode = next; } EX void load_modecode_line(string s) { @@ -1199,6 +1201,26 @@ EX void load_modecode_line(string s) { code_for[nover] = code; } +EX void load_modename_line(string s) { + int code = atoi(&s[5]); + int pos = 5; + while(s[pos] != ' ' && s[pos]) pos++; + if(!s[pos]) { modename.erase(get_identify(code)); return; } + pos++; + modename[get_identify(code)] = s.substr(pos); + } + +EX void update_modename(string newname) { + string old = modename.count(current_modecode) ? modename[current_modecode] : ""; + if(old == newname) return; + if(newname == "") modename.erase(current_modecode); + else modename[current_modecode] = newname; + FILE *f = fopen(scorefile.c_str(), "at"); + if(!f) return; + fprintf(f, "NAME %d %s\n", current_modecode, newname.c_str()); + fclose(f); + } + EX namespace peace { EX bool on = false; @@ -1420,7 +1442,8 @@ void mode_screen_for_current() { cmode = sm::SIDE | sm::MAYDARK; gamescreen(); - auto mc = modecode(); + modecode(); + auto& mc = current_modecode; dialog::init(XLAT("recorded mode %1", its(mc)), iinf[itOrbYendor].color, 150, 100); dialog::addInfo(mode_description1()); @@ -1490,11 +1513,14 @@ EX string mode_to_search; int gscore(modecode_t xc) { if(!qty_scores_for.count(xc)) return 0; return qty_scores_for[xc]; } int gscoreall(modecode_t xc) { return gscore(xc) * 100 + tactic::compute_tscore(xc) * 10 + yendor::compute_tscore(xc); } string gdisplay(modecode_t xc) { - if(mode_description_of.count(xc)) return mode_description_of[xc]; - else return "(mode " + its(xc) + ")"; + string out = ""; + if(modename.count(xc)) out = modename[xc] + ": "; + if(mode_description_of.count(xc)) return out + mode_description_of[xc]; + else return out + "(mode " + its(xc) + ")"; } EX vector mode_list; +EX map modename; EX void prepare_custom() { scores::load_only();