1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-27 14:37:16 +00:00

nilrider:: loading saved plans and replays

This commit is contained in:
Zeno Rogue 2022-05-02 03:38:20 +02:00
parent ae503cf322
commit 2bd079003a
2 changed files with 100 additions and 2 deletions

View File

@ -237,6 +237,57 @@ void settings() {
dialog::addBack();
}
bool deleting = false;
template<class T, class U> void replays_of_type(vector<T>& 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();

View File

@ -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<int> headings;
int size = scan<int> (f);
if(size < 0 || size > 1000000) throw hstream_exception();
for(int i=0; i<size; i++) headings.push_back(scan<int>(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<int> (f);
if(size < 0 || size > 1000000) throw hstream_exception();
plan.resize(size, {C0, C0});
for(int i=0; i<size; i++) scan(f, plan[i].at[0], plan[i].at[1], plan[i].vel[0], plan[i].vel[1]);
auto l = level_by_name(lev);
if(l) l->plan_replays.emplace_back(plan_replay{name, std::move(plan)});
continue;
}
println(hlog, "error: unknown content ", s);
}
}
}