rulegen3:: faster get_roadsign

This commit is contained in:
Zeno Rogue 2022-08-17 17:31:32 +02:00
parent eb4af6c584
commit 7f0bbcfef3
1 changed files with 16 additions and 13 deletions

View File

@ -110,30 +110,33 @@ EX int get_roadsign(twalker what) {
tail.push_back(t->c.spin(t->parent_dir)); tail.push_back(t->c.spin(t->parent_dir));
t = t->move(t->parent_dir); t = t->move(t->parent_dir);
} }
map<tcell*, int> visited; /* we reuse known_sides */
queue<tcell*> vqueue; vector<tcell*> vqueue;
auto visit = [&] (tcell *c, int dir) { auto visit = [&] (tcell *c, int dir) {
if(visited.count(c)) return; if(c->known_sides) return;
visited[c] = dir; c->known_sides = dir + 1;
vqueue.push(c); vqueue.push_back(c);
}; };
visit(s, MYSTERY); visit(s, MYSTERY);
while(true) { for(int i=0;; i++) {
if(vqueue.empty()) throw hr_exception("vqueue empty"); if(i == isize(vqueue)) throw hr_exception("vqueue empty");
tcell *c = vqueue.front(); tcell *c = vqueue[i];
if(c == t) break; if(c == t) break;
vqueue.pop(); for(int i=0; i<c->type; i++) {
for(int i=0; i<c->type; i++) tcell *c1 = c->move(i);
if(c->move(i) && c->move(i)->dist <= dlimit) if(c1 && c1->dist <= dlimit)
visit(c->move(i), c->c.spin(i)); visit(c1, c->c.spin(i));
if(c1 == t) break;
}
} }
while(t != s) { while(t != s) {
add_road_shortcut(s, t); add_road_shortcut(s, t);
int d = visited.at(t); int d = t->known_sides-1;
tail.push_back(t->dist - dlimit); tail.push_back(t->dist - dlimit);
tail.push_back(t->c.spin(d)); tail.push_back(t->c.spin(d));
t = t->move(d); t = t->move(d);
} }
for(auto c: vqueue) c->known_sides = 0;
reverse(tail.begin(), tail.end()); reverse(tail.begin(), tail.end());
for(auto t: tail) result.push_back(t); for(auto t: tail) result.push_back(t);
if(roadsign_id.count(result)) return roadsign_id[result]; if(roadsign_id.count(result)) return roadsign_id[result];