From 2bd079003a36106e927ba38a35d885c4858dcaae Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Mon, 2 May 2022 03:38:20 +0200 Subject: [PATCH] nilrider:: loading saved plans and replays --- rogueviz/nilrider/nilrider.cpp | 59 +++++++++++++++++++++++++++++++++- rogueviz/nilrider/save.cpp | 43 ++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/rogueviz/nilrider/nilrider.cpp b/rogueviz/nilrider/nilrider.cpp index 71518346..f1da95fc 100644 --- a/rogueviz/nilrider/nilrider.cpp +++ b/rogueviz/nilrider/nilrider.cpp @@ -237,6 +237,57 @@ void settings() { dialog::addBack(); } +bool deleting = false; + +template void replays_of_type(vector& v, const U& loader) { + int i = 0; + for(auto& r: v) { + dialog::addItem(r.name, 'a'); + dialog::add_action([&v, i, loader] { + if(deleting) { + dialog::push_confirm_dialog( + [&, i] { v.erase(v.begin() + i); save(); }, + "Are you sure you want to delete '" + v[i].name + "'?" + ); + } + else loader(v[i]); + }); + i++; + } + } + +void replays() { + dialog::init(XLAT(planning_mode ? "saved plans" : "replays"), 0xC0C0FFFF, 150, 100); + if(!planning_mode) replays_of_type(curlev->manual_replays, [] (manual_replay& r) { + view_replay = false; + curlev->history.clear(); + auto& current = curlev->current; + current = curlev->start; + for(auto h: r.headings) { + current.heading_angle = int_to_heading(h); + curlev->history.push_back(current); + if(!current.tick(curlev)) break; + } + toggle_replay(); + popScreen(); + }); + if(planning_mode) replays_of_type(curlev->plan_replays, [] (plan_replay& r) { + view_replay = false; + curlev->history.clear(); + curlev->plan = r.plan; + popScreen(); + }); + dialog::addBoolItem_action("delete", deleting, 'X'); + dialog::addBack(); + dialog::display(); + } + +void pop_and_push_replays() { + deleting = false; + popScreen(); + pushScreen(replays); + } + reaction_t on_quit = [] { exit(0); }; void main_menu() { @@ -265,6 +316,9 @@ void main_menu() { curlev->manual_replays.emplace_back(manual_replay{new_replay_name(), std::move(ang)}); save(); }); + + dialog::addItem("load a replay", 'l'); + dialog::add_action(pop_and_push_replays); } else { dialog::addItem("save this plan", 's'); @@ -272,6 +326,9 @@ void main_menu() { curlev->plan_replays.emplace_back(plan_replay{new_replay_name(), curlev->plan}); save(); }); + + dialog::addItem("load a plan", 'l'); + dialog::add_action(pop_and_push_replays); } dialog::addItem("change track or game settings", 't'); @@ -312,7 +369,7 @@ void nilrider_keys() { } void initialize() { - + load(); nilrider_keys(); check_cgi(); diff --git a/rogueviz/nilrider/save.cpp b/rogueviz/nilrider/save.cpp index 496079e6..b151fb4b 100644 --- a/rogueviz/nilrider/save.cpp +++ b/rogueviz/nilrider/save.cpp @@ -13,7 +13,7 @@ string new_replay_name() { void save() { println(hlog, "save called"); fhstream f("nilrider.save", "wt"); - println(f, "version ", ver); + println(f, "NilRider version ", ver); for(auto l: all_levels) { for(auto& p: l->manual_replays) { println(f, "*MANUAL"); @@ -34,4 +34,45 @@ void save() { } } +level *level_by_name(string s) { + for(auto l: all_levels) if(l->name == s) return l; + println(hlog, "error: unknown level ", s); + return nullptr; + } + +void load() { + println(hlog, "load called"); + fhstream f("nilrider.save", "rt"); + if(!f.f) return; + string ver = scanline_noblank(f); + while(!feof(f.f)) { + string s = scanline_noblank(f); + if(s == "") continue; + if(s == "*MANUAL") { + string lev = scanline_noblank(f); + string name = scanline_noblank(f); + vector headings; + int size = scan (f); + if(size < 0 || size > 1000000) throw hstream_exception(); + for(int i=0; i(f)); + auto l = level_by_name(lev); + if(l) l->manual_replays.emplace_back(manual_replay{name, std::move(headings)}); + continue; + } + if(s == "*PLANNING") { + string lev = scanline_noblank(f); + string name = scanline_noblank(f); + plan_t plan; + int size = scan (f); + if(size < 0 || size > 1000000) throw hstream_exception(); + plan.resize(size, {C0, C0}); + for(int i=0; iplan_replays.emplace_back(plan_replay{name, std::move(plan)}); + continue; + } + println(hlog, "error: unknown content ", s); + } + } + }