From a6151f185fb2d061d858c436fb2665b4edfe0e0e Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Sun, 8 Aug 2021 19:13:09 +0200 Subject: [PATCH] refactored whirlline loop detection --- complex.cpp | 4 ++-- complex2.cpp | 8 +------- environment.cpp | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/complex.cpp b/complex.cpp index 28645e03..e03b94bc 100644 --- a/complex.cpp +++ b/complex.cpp @@ -102,6 +102,7 @@ EX namespace whirlwind { again: cell *at = whirlline[isize(whirlline)-1]; cell *prev = whirlline[isize(whirlline)-2]; + if(looped(whirlline)) return; for(int i=0; itype; i++) if(at->move(i) && (euclid || at->move(i)->master->alt) && celldistAlt(at->move(i)) == d && at->move(i) != prev) { whirlline.push_back(at->move(i)); @@ -1062,10 +1063,9 @@ EX namespace whirlpool { again: cell *at = whirlline[isize(whirlline)-1]; cell *prev = whirlline[isize(whirlline)-2]; + if(looped(whirlline)) return; for(int i=0; itype; i++) if(at->move(i) && (eubinary || at->move(i)->master->alt) && celldistAlt(at->move(i)) == d && at->move(i) != prev) { - if(at->move(i) == whirlline[0]) return; // loops in weird geometries? - if(at->move(i) == whirlline[isize(whirlline)/2]) return; // even weirder geometry? whirlline.push_back(at->move(i)); goto again; } diff --git a/complex2.cpp b/complex2.cpp index f11c3dec..676b2f74 100644 --- a/complex2.cpp +++ b/complex2.cpp @@ -225,13 +225,7 @@ EX namespace westwall { again: cell *at = whirlline[isize(whirlline)-1]; cell *prev = whirlline[isize(whirlline)-2]; - if(isize(whirlline) > 2 && at == whirlline[isize(whirlline)/2]) { - /* something weird must have happened... */ - static bool once = true; - if(once) addMessage("warning: a bug in building a whirl line"); - once = false; - return; - } + if(looped(whirlline)) return; for(int i=0; itype; i++) if(at->move(i) && coastvalEdge1(at->move(i)) == d && at->move(i) != prev) { whirlline.push_back(at->move(i)); diff --git a/environment.cpp b/environment.cpp index 3cfa6999..a2508c5a 100644 --- a/environment.cpp +++ b/environment.cpp @@ -865,4 +865,25 @@ EX void monstersTurn() { #endif } +/** check if whirlline is looped, if yes, remove the repeat; may not detect loops immediately */ +EX bool looped(vector& whirlline) { + if(isize(whirlline) == 1) + return false; + if(whirlline.back() == whirlline.front()) { + whirlline.pop_back(); + return true; + } + int pos = isize(whirlline)/2; + if(isize(whirlline) > 2 && whirlline.back() == whirlline[pos]) { + while(pos && whirlline.back() == whirlline[pos]) + whirlline.pop_back(); + /* something weird must have happened... */ + static bool once = true; + if(once) addMessage("warning: a looped line"); + once = false; + return true; + } + return false; + } + }