1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-15 09:44:48 +00:00
hyperrogue/devmods/rulegen-tests.cpp

1400 lines
38 KiB
C++
Raw Normal View History

2021-07-29 14:18:41 +00:00
/* some extra routines for debugging and testing the rulegen algorithm */
#include "../hyper.h"
#include <fstream>
#include <chrono>
2021-07-29 14:18:41 +00:00
2021-11-01 16:27:06 +00:00
// extra ruleflags:
// 30: do not clear memory
2021-07-29 14:18:41 +00:00
namespace hr {
namespace rulegen {
EX flagtype sub_rulegen_flags;
2021-07-29 14:18:41 +00:00
string testroot = "devmods/rulegen-tests/";
string testdir = testroot;
struct hrmap_testproto : hrmap {
map<heptagon*, tcell*> counterpart;
map<tcell*, heptagon*> counterpart2;
heptagon* clone(tcell *o) {
auto& h = counterpart2[o];
if(!h) {
h = tailored_alloc<heptagon> (o->type);
counterpart[h] = o;
h->zebraval = 0;
h->emeraldval = 0;
h->distance = 0;
h->cdata = nullptr;
h->c7 = newCell(o->type, h);
h->c7->land = laCanvas;
2021-07-29 14:18:41 +00:00
}
return h;
}
~hrmap_testproto() {
println(hlog, "clearing ", isize(counterpart), " heptagons from testproto");
for(auto p: counterpart) {
auto& at = p.first;
if(at->cdata) delete at->cdata;
destroy_cell(at->c7);
tailored_delete(at);
}
}
heptagon *getOrigin() override {
return clone(t_origin[0].at);
2021-07-29 14:18:41 +00:00
}
heptagon *create_step(heptagon *h, int d) override {
auto ch = counterpart[h];
if(!ch->move(d)) {
h->c.connect(d, &oob, 0, false);
h->c7->c.connect(d, &out_of_bounds, 0, false);
out_of_bounds.master = &oob;
oob.c7 = &out_of_bounds;
return &oob;
}
auto ch1 = ch->cmove(d);
auto d1 = ch->c.spin(d);
auto h1 = clone(ch1);
if(ch == ch1)
throw rulegen_failure("connected to self??");
if(h == h1)
throw rulegen_failure("connected to self on hept level??");
h->c.connect(d, h1, d1, false);
return h1;
}
void find_cell_connection(cell *c, int d) {
heptagon *h2 = createStep(c->master, d);
if(h2 == &oob) return;
c->c.connect(d, h2->c7,c->master->c.spin(d), c->master->c.mirror(d));
}
transmatrix adj(heptagon *h, int dir) override {
2021-08-18 23:39:49 +00:00
if(h->move(dir) == &oob || h == &oob) return Id;
return arb::get_adj(arb::current_or_slided(), shvid(h->c7), dir, -1, h->c.move(dir) ? h->c.spin(dir) : -1);
2021-07-29 14:18:41 +00:00
}
2021-08-19 11:19:32 +00:00
hyperpoint get_corner(cell *c, int cid, ld cf) override {
auto h = c->master;
if(!counterpart.count(h)) {
auto& sh = arb::current_or_slided().shapes[c->master->zebraval];
cid = gmod(cid, sh.size());
return normalize(C0 + (sh.vertices[cid] - C0) * 3 / cf);
}
int s = counterpart[h]->id;
auto& sh = arb::current_or_slided().shapes[s];
return normalize(C0 + (sh.vertices[cid] - C0) * 3 / cf);
}
2021-07-29 14:18:41 +00:00
int shvid(cell *c) override {
if(!counterpart.count(c->master)) {
/* may happen while handling floorshapes */
return arb::id_of(c->master);
}
auto cc = counterpart[c->master];
return cc->id;
}
2021-08-18 23:39:49 +00:00
bool strict_tree_rules() { return false; }
2021-07-29 14:18:41 +00:00
};
reaction_t clear_debug = [] {};
map<tcell*,int> sprawl_shown;
2021-08-23 22:34:25 +00:00
vector<twalker> old_sprawl, cur_sprawl;
int old_sprawl_id;
2021-08-18 23:39:49 +00:00
int total_analyzers();
2021-08-19 11:19:32 +00:00
void cleanup_protomap() {
2021-08-18 23:39:49 +00:00
auto m = dynamic_cast<hrmap_testproto*> (currentmap);
2021-08-19 11:19:32 +00:00
auto *c = first_tcell;
while(c) {
auto wh = m->clone(c);
for(int i=0; i<wh->type; i++) if(wh->move(i) == &oob) wh->move(i) = nullptr;
for(int i=0; i<wh->c7->type; i++) if(wh->c7->move(i) == &out_of_bounds) wh->c7->move(i) = nullptr;
c = c->next;
}
}
void iterate(int qty) {
dynamicval<int> set_timeout(rulegen_timeout, 999999);
2021-08-18 23:39:49 +00:00
for(int i=0; i<qty; i++) {
try {
rules_iteration();
try_count--;
break;
}
catch(rulegen_retry& f) {
println(hlog, "retry on: ", f.what());
}
catch(rulegen_surrender& f) {
println(hlog, "surrender: ", f.what());
}
2021-08-18 23:40:15 +00:00
catch(rulegen_failure& f) {
println(hlog, "failure: ", f.what());
}
2021-08-18 23:39:49 +00:00
}
println(hlog, "try_count = ", try_count, " states = ", isize(treestates), " imp = ", isize(important), " analyzers = ", total_analyzers(), " cell = ", tcellcount);
2021-08-19 11:19:32 +00:00
cleanup_protomap();
2021-08-18 23:39:49 +00:00
}
void print_rules();
2021-08-19 11:19:32 +00:00
void irradiate() {
dynamicval<int> set_timeout(rulegen_timeout, 999999);
2021-08-19 11:19:32 +00:00
try{
vector<tcell*> last;
auto *c = first_tcell;
while(c) {
last.push_back(c);
c = c->next;
}
for(tcell* l: last) {
ufindc(l);
for(int i=0; i<l->type; i++) l->cmove(i);
}
handle_distance_errors();
}
catch(rulegen_failure& r) {
println(hlog, "error in irradiate: ", r.what());
}
cleanup_protomap();
}
2021-11-23 09:25:16 +00:00
void move_to(cellwalker cw) {
2021-08-23 13:56:45 +00:00
cwt = cw;
centerover = cwt.at;
View = Id;
}
void move_to(twalker dw) {
auto m = dynamic_cast<hrmap_testproto*> (currentmap);
2021-08-23 22:34:25 +00:00
ufind(dw);
2021-08-23 13:56:45 +00:00
move_to(cellwalker(m->clone(dw.at)->c7, dw.spin, dw.mirrored));
}
void sprawl(twalker c) {
2021-08-23 22:34:25 +00:00
auto [d, id] = get_code(c);
twalker cw(c.at, d);
2021-08-23 22:34:25 +00:00
cur_sprawl = spread(get_analyzer(cw), cw);
println(hlog, "sprawl result = ", cur_sprawl);
println(hlog, "code = ", treestates[id].code);
sprawl_shown.clear();
for(int i=0; i<isize(cur_sprawl); i++) sprawl_shown[cur_sprawl[i].at] = i;
if(isize(cur_sprawl) == isize(old_sprawl) && old_sprawl_id < isize(treestates)) {
auto& oldcode = treestates[old_sprawl_id].code.second;
auto& newcode = treestates[id].code.second;
int q = isize(cur_sprawl);
debuglist = {};
for(int i=0; i<q; i++)
if(oldcode[i] != newcode[i]) {
println(hlog, "index: ", i, " old: ", oldcode[i], " new: ", newcode[i], " at ", old_sprawl[i], " vs ", cur_sprawl[i]);
debuglist.push_back(old_sprawl[i]);
debuglist.push_back(cur_sprawl[i]);
}
}
old_sprawl = cur_sprawl;
old_sprawl_id = id;
}
2021-08-18 23:39:49 +00:00
void debug_menu() {
cmode = sm::SIDE | sm::MAYDARK;
gamescreen(0);
auto m = dynamic_cast<hrmap_testproto*> (currentmap);
dialog::init("debug menu");
dialog::addItem("sprawl", 's');
dialog::add_action([m] {
sprawl(twalker(m->counterpart[centerover->master], 0));
2021-08-18 23:39:49 +00:00
});
dialog::addItem("parent_dir", 'p');
dialog::add_action([m] {
tcell *c = m->counterpart[centerover->master];
auto c1 = c;
ufindc(c1);
if(c != c1) {
println(hlog, "ufindc changes ", c, " to ", c1);
for(int i=0; i<c->type; i++)
println(hlog, twalker(c,i), twalker(c,i)+wstep, twalker(c,i)+wstep+wstep);
}
println(hlog, "parent_dir = ", c->parent_dir);
c->parent_dir = MYSTERY;
parent_debug = true;
2021-08-19 11:19:32 +00:00
try {
twalker cw(c, 0);
get_parent_dir(cw);
2021-08-19 11:19:32 +00:00
}
catch(rulegen_failure& f) {
println(hlog, "catched: ", f.what());
}
2021-08-18 23:39:49 +00:00
parent_debug = false;
println(hlog, "parent_dir = ", c->parent_dir);
2021-08-19 11:19:32 +00:00
cleanup_protomap();
2021-08-18 23:39:49 +00:00
});
dialog::addItem("iterate", 'm');
dialog::add_action([] { iterate(1); });
dialog::addItem("iterate x100", 'M');
dialog::add_action([] { iterate(100); });
dialog::addItem("iterate x1000", 'T');
dialog::add_action([] { iterate(1000); });
dialog::addItem("debug tiles", 'd');
dialog::add_action_push([m] {
cmode = sm::SIDE | sm::MAYDARK;
gamescreen(0);
dialog::init();
for(auto dw: debuglist) {
dialog::addItem("go to " + index_pointer(dw.at), 'a');
2021-08-23 13:56:45 +00:00
dialog::add_action([dw] {
move_to(dw);
2021-08-18 23:39:49 +00:00
});
}
dialog::display();
});
2021-08-23 22:34:25 +00:00
dialog::addItem("furthest", 'f');
dialog::add_action([m] {
auto *c = first_tcell;
tcell *furthest = c;
while(c) {
if(c->unified_to.at == c)
if(c->dist < MYSTERY && c->dist > furthest->dist) furthest = c;
c = c->next;
}
println(hlog, "furthest in distance = ", furthest->dist);
move_to(furthest);
});
2021-08-18 23:39:49 +00:00
dialog::addItem("print rules", 'P');
dialog::add_action(print_rules);
dialog::addItem("clean data", 'c');
dialog::add_action(clean_data);
dialog::addItem("clean data and parents", 'C');
dialog::add_action(clean_parents);
2021-08-19 11:19:32 +00:00
dialog::addItem("irradiate", 'i');
dialog::add_action(irradiate);
2021-08-23 22:34:25 +00:00
dialog::addItem("irradiate x10", 'I');
dialog::add_action([] { for(int i=0; i<10; i++) irradiate(); });
2021-08-19 11:19:32 +00:00
dialog::addItem("name", 'n');
dialog::add_action([m] { println(hlog, "name = ", index_pointer(m->counterpart[cwt.at->master])); });
2021-08-18 23:39:49 +00:00
dialog::display();
}
color_t label_color = 1, wall_color = 0, tree_color = 0xFFFFFFFF, out_color = 0xFF0000FF;
bool show_codes = true;
bool show_dist = true;
2021-07-29 14:18:41 +00:00
void view_debug() {
auto m = dynamic_cast<hrmap_testproto*> (currentmap);
if(m) {
int ah = addHook(hooks_drawcell, 50, [m] (cell *c, const shiftmatrix& V) {
tcell *tc = m->counterpart[c->master];
string s;
auto label = (tc->dist == MYSTERY ? "?" : its(tc->dist));
if(show_codes) {
string code = (tc->code == MYSTERY ? "?" : its(tc->code));
if(show_dist) label = label + "/" + code;
else label = code;
}
2021-07-29 14:18:41 +00:00
color_t col = label_color == 1 ? 0xFFFFFF + 0x512960 * tc->code : label_color;
2021-07-29 14:18:41 +00:00
if(pointer_indices.count(tc)) label += " " + index_pointer(tc);
queuestr(V, 0.4, label, col, 1);
if(tree_color) {
if(tc->parent_dir >= 0 && tc->parent_dir < tc->type) {
vid.linewidth *= 8;
queueline(V * C0, V * currentmap->adj(c, tc->parent_dir) * C0, tree_color);
vid.linewidth /= 8;
}
2021-07-29 14:18:41 +00:00
}
if(sprawl_shown.count(tc))
queuepoly(V, cgi.shDisk, 0xFF0000FF);
2021-08-19 11:19:32 +00:00
if(out_color) {
for(int i=0; i<tc->type; i++) if(!tc->move(i)) {
vid.linewidth *= 8;
hyperpoint h1 = currentmap->get_corner(c, (i+1)%tc->type);
hyperpoint h2 = currentmap->get_corner(c, i);
queueline(V * h1, V * h2, 0xFF0000FF);
vid.linewidth /= 8;
}
2021-08-19 11:19:32 +00:00
}
2021-07-29 14:18:41 +00:00
return false;
});
vector<int> dh;
dh.push_back(addHook(hooks_o_key, 15, [m] (o_funcs& v) {
2021-08-18 23:39:49 +00:00
/* v.push_back(named_functionality("mark", [m] {
for(auto c: marklist)
m->clone(c.at)->c7->item = itGold;
})); */
v.push_back(named_dialog("debug menu", debug_menu));
2021-07-29 14:18:41 +00:00
}));
clear_debug = [ah, dh] {
delHook(hooks_drawcell, ah);
for(auto dhk: dh) delHook(hooks_o_key, dhk);
clear_debug = [] {};
};
}
else if(currentmap->strict_tree_rules()) {
int ah = addHook(hooks_drawcell, 50, [] (cell *c, const shiftmatrix& V) {
string s;
int id = get_state(c);
auto label = its(id);
color_t col = 0xFFFFFF + 0x512960 * id;
if(pointer_indices.count(c->master)) label += " " + index_pointer(c->master);
queuestr(V, 0.3, label, col, 1);
vid.linewidth *= 8;
queueline(V * C0, V * currentmap->adj(c, 0) * C0, 0xFFFFFFFF, 4);
vid.linewidth /= 8;
return false;
});
clear_debug = [ah] { delHook(hooks_drawcell, ah); };
}
}
int test_rotate_val = 0;
int test_count = 10000;
void test_rules() {
cell *c = currentmap->gamestart();
int N = isize(treestates);
if(!N) {
println(hlog, "no states generated");
return;
}
vector<bignum> howmany(N);
howmany[0] = 1;
celllister cl(c, 100, test_count, nullptr);
vector<int> bydist(64, 0);
for(cell *cc: cl.lst) {
int d = cl.getdist(cc);
if(d < 64) bydist[d]++;
}
vector<int> vals;
vector<string> seq;
for(int iter=0; iter<30; iter++) {
bignum total;
for(auto& h: howmany) total += h;
seq.push_back(total.get_str(100));
println(hlog, iter, " : ", total.get_str(100), " vs ", bydist[iter]);
if(bydist[iter] && bydist[iter] != total.approx_ll())
println(hlog, "ERROR count mismatch");
vector<bignum> next(N);
for(int id=0; id<N; id++)
for(int s: treestates[id].rules) if(s >= 0)
next[s] += howmany[id];
howmany = std::move(next);
}
println(hlog, "sequence: ", seq);
}
fhstream *seq_stream;
void list_all_sequences(string tesname) {
2021-07-29 14:18:41 +00:00
int N = isize(treestates);
if(!N) {
println(hlog, "no states generated");
return;
}
for(int i=0; i<N; i++) if(treestates[i].is_root) {
vector<bignum> howmany(N);
2021-07-29 14:18:41 +00:00
howmany[i] = 1;
vector<string> seq;
for(int iter=0; iter<60; iter++) {
bignum total;
for(auto& h: howmany) total += h;
seq.push_back(total.get_str(100));
vector<bignum> next(N);
for(int id=0; id<N; id++)
for(int s: treestates[id].rules) if(s >= 0)
next[s] += howmany[id];
howmany = std::move(next);
}
println(*seq_stream, (hyperbolic ? "H " : "E "), "FILE ", tesname, ", id ");
println(*seq_stream, seq);
2021-07-29 14:18:41 +00:00
}
fflush(seq_stream->f);
2021-07-29 14:18:41 +00:00
}
void print_rules();
string rule_name(int r) {
if(r == DIR_UNKNOWN) return "??";
else if(r == DIR_LEFT) return "L";
else if(r == DIR_RIGHT) return "R";
else if(r == DIR_PARENT) return "P";
else return its(r);
}
void print_rules() {
for(int i=0; i<isize(treestates); i++) {
print(hlog, i, ":");
auto& ts = treestates[i];
for(auto r: ts.rules)
print(hlog, " ", rule_name(r));
if(ts.giver.at)
2021-11-23 09:25:16 +00:00
print(hlog, " ", ts.giver);
2021-07-29 14:18:41 +00:00
else
print(hlog, " (no giver)");
2021-08-17 12:34:34 +00:00
println(hlog, " rg:", ts.giver, ts.is_root ? " [root]" : ts.is_live ? " [live]" : " [dead]");
2021-07-29 14:18:41 +00:00
}
}
int draw_which = 0;
void restart_game_on(hrmap *m) {
stop_game();
int a = addHook(hooks_newmap, 0, [m] { return m;});
set_geometry(gArbitrary);
start_game();
delHook(hooks_newmap, a);
}
bool add_header = false;
bool add_labels = true;
2021-11-23 09:25:16 +00:00
string test_stats = "gsmTPcuQthlpf"; // "gsmctTlAhf";
2021-08-18 23:39:49 +00:00
pair<int,int> longest_shortcut() {
int res = 0;
int qty = 0;
for(auto& p: shortcuts) for(auto& v: p.second) {
res = max<int>(res, isize(v->pre));
qty++;
}
return {qty, res};
}
int longest_analyzer() {
int res = 0;
for(auto& a: analyzers) res = max(res, isize(a.second.spread));
return res;
}
int total_analyzers() {
int res = 0;
for(auto& a: analyzers) res += isize(a.second.spread);
return res;
}
2021-08-21 22:28:52 +00:00
int shape_edges() {
int res = 0;
for(auto& sh: arb::current.shapes) res += sh.size();
return res;
}
int max_edge() {
int res = 0;
for(auto& sh: arb::current.shapes) res = max(res, sh.size());
return res;
}
int max_valence() {
int res = 0;
for(auto& sh: arb::current.shapes) for(auto& va: sh.vertex_valence) res = max(res, va);
return res;
}
2021-12-24 22:10:06 +00:00
bool same_shape_at(const arb::shape& s1, const arb::shape& s2, int i) {
auto N = s1.size();
for(int j=0; j<N; j++) {
if(abs(s1.edges[j] - s2.edges[(i+j)%N]) > 1e-6)
return false;
if(abs(s1.angles[j] - s2.angles[(i+j)%N]) > 1e-6)
return false;
}
return true;
}
bool same_shape_inv_at(const arb::shape& s1, const arb::shape& s2, int i) {
auto N = s1.size();
for(int j=0; j<N; j++) {
if(abs(s1.edges[N-1-j] - s2.edges[(i+j)%N]) > 1e-6)
return false;
if(abs(s1.angles[(N*2-2-j) % N] - s2.angles[(i+j)%N]) > 1e-6)
return false;
}
return true;
}
bool same_shape(arb::shape& s1, arb::shape& s2, bool sym) {
if(s1.size() != s2.size()) return false;
for(int i=0; i<s1.size(); i++)
if(same_shape_at(s1, s2, i))
return true;
if(sym) for(int i=0; i<s1.size(); i++)
if(same_shape_inv_at(s1, s2, i))
return true;
return false;
}
int count_different_shapes(bool sym) {
vector<arb::shape*> dsi;
for(auto& sh: arb::current.shapes) {
for(auto& ksh: dsi) if(same_shape(sh, *ksh, sym)) goto next;
dsi.push_back(&sh);
next: ;
}
return isize(dsi);
}
int count_vertex_orbits() {
double t = 0;
for(auto& sh: arb::current.shapes) {
for(int i=0; i<sh.cycle_length; i++)
t += 1. / sh.vertex_period[i];
}
int res = int(t + .5);
if(abs(t - res) > .01) throw hr_exception("count_vertex_orbits error");
return res;
}
int count_edge_orbits() {
int eo = 0;
for(auto& sh: arb::current.shapes)
eo += sh.cycle_length;
return eo;
}
vector<ld> normalize_anglelist(vector<ld> v, bool sym) {
for(auto& va: v) va = int(va * 1000000 + .5);
vector<ld> res = v;
for(int r=0; r<2; r++) {
for(int u=0; u<isize(v); u++) {
if(v < res) res = v;
std::rotate(v.begin(), v.begin()+1, v.end());
}
if(!sym) break;
reverse(v.begin(), v.end());
}
return res;
}
int count_different_vertices(bool sym) {
set<vector<ld>> seen;
for(auto& sh: arb::current.shapes)
for(auto& al: sh.vertex_angles) {
al = normalize_anglelist(al, sym);
seen.insert(al);
}
return isize(seen);
}
int count_different_edges() {
vector<ld> seen;
for(auto& sh: arb::current.shapes)
for(auto& e: sh.edges)
seen.push_back(e);
sort(seen.begin(), seen.end());
int res = 1;
for(int i=1; i<isize(seen); i++)
if(seen[i] > seen[i-1] + 1e-5) res++;
return res;
}
string count_uniform() {
auto& sh = arb::current.shapes;
int N = sh.size();
vector<int> starts;
int qty = 0;
for(auto& s: sh) { starts.push_back(qty); qty += s.cycle_length * 2; }
// for(auto s: sh) println(hlog, "CSV;clen: ", s.cycle_length);
// println(hlog, "CSV;size: ", starts, " N=", N);
vector<int> rtile(qty), rvert(qty, -4), redge(qty), rangle(qty);
for(int i=0; i<N; i++)
for(int j=0; j<sh[i].cycle_length; j++) {
auto c = sh[i].cycle_length;
int a = starts[i]+2*j;
int b = a+1;
int jp = gmod(j+1, c);
int jn = gmod(j-1, c);
rtile[a] = starts[i] + 2*jp;
rtile[b] = starts[i] + 2*jn + 1;
rangle[a] = rangle[b] = int(sh[i].angles[j] * 100000 + .5);
redge[a] = int(sh[i].edges[jp] * 100000 + .5);
redge[b] = int(sh[i].edges[j] * 100000 + .5);
// rvert[a]: go through the edge and through the tile
auto co = sh[i].connections[jp];
// println(hlog, "CSV; for ", tie(i,j), " got ", tie(co.sid, co.eid), "with jp=", jp);
auto res = starts[co.sid] + 2 * gmod(co.eid, sh[co.sid].cycle_length);
rvert[a] = res;
co = sh[i].connections[j];
res = starts[co.sid] + 2 * gmod(co.eid-1, sh[co.sid].cycle_length) + 1;
// println(hlog, "CSV; for ", tie(i,j), " got ", tie(co.sid, co.eid), " .. ", sh[i].connections);
rvert[b] = res;
}
/*
println(hlog, "CSV;tile=", rtile);
println(hlog, "CSV;vert=", rvert);
println(hlog, "CSV;", rangle);
println(hlog, "CSV;", redge);
*/
std::vector<int> eq_class(qty, 0);
int num_eq_class = 1;
int last_num_eq_class = 0;
while (num_eq_class > last_num_eq_class) {
// println(hlog, "CSV;", eq_class);
using vertex_data = std::array<int, 6>;
std::vector<std::pair<vertex_data, int > > data(qty);
last_num_eq_class = num_eq_class;
for (int i = 0; i < qty; i++) {
data[i].first[0] = eq_class[i];
data[i].first[1] = rangle[i];
data[i].first[2] = redge[i];
data[i].first[3] = eq_class[rtile[i]];
data[i].first[4] = eq_class[rvert[i]];
data[i].first[5] = eq_class[i^1];
data[i].second = i;
}
sort(data.begin(), data.end());
eq_class[data[0].second] = 0;
num_eq_class = 0;
for (int i = 1; i < qty; i++) {
if (data[i].first != data[i - 1].first) num_eq_class++;
eq_class[data[i].second] = num_eq_class;
}
num_eq_class++;
}
std::vector<int> reps(num_eq_class, -1);
for(int i=0; i<qty; i++) reps[eq_class[i]] = i;
int num_edges = num_eq_class;
int num_vert = 0, num_tile = 0, num_vert_sym = 0, num_tile_sym = 0, num_edges_sym = 0, num_edges_ev = 0, num_edges_et = 0, num_edges_ez = 0;
for(int i: reps) {
int i1 = reps[eq_class[rvert[i] ^ 1]];
int i2 = reps[eq_class[rtile[i] ^ 1]];
int i3 = reps[eq_class[rtile[i1] ^ 1]];
// println(hlog, "CSV; ", i, " with ", tie(i1, i2, i3));
if(i >= i1 && i >= i2 && i >= i3) num_edges_sym++;
if(i >= i1) num_edges_ev++;
if(i >= i2) num_edges_et++;
if(i >= i3) num_edges_ez++;
}
for (int i: reps) {
if(rtile[i] >= 0) {
num_tile++;
int maxj = i;
int j = i; while(rtile[j] >= 0) maxj = max(maxj, j), tie(j, rtile[j]) = make_pair(reps[eq_class[rtile[j]]], -1);
if(maxj&1) num_tile_sym++;
// println(hlog, "CSV;found ", i);
}
if(rvert[i] >= 0) {
num_vert++;
int maxj = i;
int j = i; while(rvert[j] >= 0) maxj = max(maxj, j), tie(j, rvert[j]) = make_pair(reps[eq_class[rvert[j]]], -1);
if(maxj&1) num_vert_sym++;
}
}
// println(hlog, "CSV;eq_class = ", eq_class);
// println(hlog, "CSV;", lalign(0, num_tile, ";", num_vert, ";", num_tile_sym, ";", num_vert_sym, ";", num_edges), " = tv/stv/e");
return lalign(0, num_tile, ";", num_vert, ";", num_edges, ";", num_tile_sym, ";", num_vert_sym, ";", num_edges_sym,";", num_edges_ev, ";", num_edges_et, ";", num_edges_ez);
}
void test_current(string tesname) {
disable_bigstuff = true;
worst_precision_error = 0;
2021-07-29 14:18:41 +00:00
stop_game();
pointer_indices.clear();
// if(s.find("884-211-045") == string::npos) return;
start_game();
string t = testdir + "/";
for(char c: tesname)
2021-07-29 14:18:41 +00:00
if(isalnum(c)) t += c;
else if(c == ',') t += "_";
else if(c == ' ') t += "__";
else if(c == '(') t += "op";
else if(c == '(') t += "cp";
else if(c == '[') t += "ob";
else if(c == '[') t += "cb";
else if(c == '/') t += "__";
if(euclid) pconf.scale = .1;
if(hyperbolic) pconf.scale = .95;
if(sphere) pconf.scale = .5;
shot::transparent = hyperbolic;
println(hlog, "TESTING: ", tesname, " AS: ", t);
2021-07-29 14:18:41 +00:00
indenter ind(2);
if(draw_which & 1) {
View = Id; shot::take(t+"-old.png");
}
string status, message;
2021-07-29 14:18:41 +00:00
bool ok = false;
// make print_rules() not crash in case of a conversion error
treestates.clear();
/* we do not want to include the conversion time */
if(!arb::in()) try {
arb::convert::convert();
arb::convert::activate();
}
catch(hr_exception& e) {
println(hlog, "CSV; failed to convert ", tesname);
return;
}
if(flags & w_known_structure) {
dynamicval<flagtype> f(rulegen::flags, sub_rulegen_flags);
prepare_rules();
if(!known()) return;
alt_treestates = treestates;
pointer_indices.clear();
}
2021-07-29 14:18:41 +00:00
int tstart = SDL_GetTicks();
int attempts = 0;
double max_time = 0, avg_time = 0, variance_time = 0;
2021-08-23 22:34:35 +00:00
auto begin = clock(); // std::chrono::high_resolution_clock::now();
auto last = begin;
2021-07-29 14:18:41 +00:00
try {
while(!attempts) { // || (clock() < begin + 0.1 * CLOCKS_PER_SEC && attempts < 1000)) {
if(true) {
rulegen::delete_tmap();
rulegen::clear_all();
last = clock();
rulegen::movecount = 0;
}
generate_rules();
auto cur = clock();
double t = (cur - last) * 1. / CLOCKS_PER_SEC;
last = cur;
if(t > max_time) max_time = t;
avg_time += t;
variance_time += t * t;
attempts++;
}
status = "ACC";
message = "OK";
2021-07-29 14:18:41 +00:00
ok = true;
}
catch(rulegen_surrender& e) {
println(hlog, "surrender: ** ", e.what());
status = "SUR";
message = e.what();
2021-07-29 14:18:41 +00:00
}
catch(rulegen_retry& e) {
println(hlog, "try exceeded: ** ", e.what());
status = "TRY";
message = e.what();
2021-07-29 14:18:41 +00:00
}
catch(rulegen_failure& e) {
println(hlog, "error: ** ", e.what());
status = "ERR";
message = e.what();
2021-07-29 14:18:41 +00:00
}
catch(hr_precision_error& e) {
println(hlog, "precision error: ** ", e.what());
status = "PRE";
message = e.what();
}
if(!attempts) {
auto cur = clock();
double t = (cur - last) * 1. / CLOCKS_PER_SEC;
avg_time += t; variance_time += t*t; max_time = t;
attempts = 1;
}
avg_time /= attempts;
variance_time /= attempts;
variance_time -= avg_time * avg_time;
if(attempts > 1) {
variance_time *= attempts;
variance_time /= (attempts-1);
}
2021-07-29 14:18:41 +00:00
2021-08-23 22:34:35 +00:00
auto end = clock(); // std::chrono::high_resolution_clock::now();
if(t_origin.size() && (draw_which & 2)) {
2021-07-29 14:18:41 +00:00
restart_game_on(new hrmap_testproto);
view_debug();
View = Id; shot::take(t+"-orig.png");
clear_debug();
}
if(ok && (draw_which & 4)) {
restart_game_on(new_hrmap_rulegen());
view_debug();
View = Id; shot::take(t+"-rule.png");
clear_debug();
}
tstart = SDL_GetTicks() - tstart;
int qsolid = 0, qcode = 0, qdist = 0;
auto *c = first_tcell;
while(c) {
if(c->is_solid) qsolid++;
if(c->code != MYSTERY) qcode++;
if(c->dist != MYSTERY) qdist++;
c = c->next;
}
vector<ld> areas;
for(auto& sh: arb::current.shapes) {
if(hyperbolic) {
ld s = 0; int i = 0;
for(auto a: sh.angles) { while(a > 2 * M_PI) a -= 2 * M_PI; while(a<0) a += 2 * M_PI; s += a; i++; }
areas.push_back((i-2) * M_PI - s);
}
else {
ld s = 0;
for(int i=0; i<sh.size(); i++) {
auto v = sh.vertices[i];
auto v2 = sh.vertices[(i+1)%sh.size()];
s += (v2[0] - v[0]) * (v[1] + v2[1]);
}
s /= 2;
if(s < 0) s = -s;
areas.push_back(s);
}
}
sort(areas.begin(), areas.end());
vector<ld> edgelens;
for(auto& sh: arb::current.shapes) {
for(int i=0; i<sh.size(); i++) {
auto v = sh.vertices[i];
auto v2 = sh.vertices[(i+1)%sh.size()];
ld len = hdist(v, v2);
edgelens.push_back(len);
}
}
sort(edgelens.begin(), edgelens.end());
again:
print(hlog, "CSV");
// easier parsing
for(auto& ch: message) if(ch == ' ') ch = '_';
#define Out(title,value) if(add_header) print(hlog, ";", title); else if(add_labels) print(hlog, " ", title, "=", value); else print(hlog, ";", value); break;
for(char c: test_stats) switch(c) {
case 'g': Out("geom", euclid ? "E" : hyperbolic ? "H" : "?");
case 's': Out("status", status);
case 'm': Out("message", message);
case 'c': Out("cells", tcellcount);
case 'u':
if(flags & w_numerical) {
Out("precision", worst_precision_error);
}
else {
Out("unis", tunified);
}
case 'q': Out("solid", qsolid);
case 'Q': Out("solid_err", all_solid_errors);
case 'd': Out("dist", qdist);
case 'C': Out("code", qcode);
case 't': Out("try", try_count);
case 'T': Out("T", tstart / 1000.);
2021-08-23 22:34:35 +00:00
// case 'P': Out("Tp", std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count() / 1000000000.);
case 'P': Out("Tp", (end-begin) * 1. / CLOCKS_PER_SEC);
case 'N': Out("attempts", attempts);
case 'M': Out("maxtime", max_time);
case 'E': Out("avgtime", avg_time);
case 'V': Out("vartime", variance_time);
case 'y': Out("tree", isize(treestates));
case 'a': Out("amin;amax", lalign(0, areas[0], ";", areas.back()));
case 'j': Out("emin;emax", lalign(0, edgelens[0], ";", edgelens.back()));
case 'h': Out("shapes", isize(arb::current.shapes));
2021-08-21 22:28:52 +00:00
case 'e': Out("edges", shape_edges());
case 'W': Out("max_valence;max_edge", lalign(0, max_valence(), ";", max_edge()));
2021-12-24 22:10:06 +00:00
case 'D': Out("dshapes;dverts;dedges;bshapes;bverts", lalign(0, count_different_shapes(true), ";", count_different_vertices(true), ";", count_different_edges(), ";", count_different_shapes(false), ";", count_different_vertices(false)));
case 'O': Out("overts;oedges", lalign(0, count_vertex_orbits(), ";", count_edge_orbits()));
case 'U': Out("vshapes;vverts;ushapes;uverts;uedges;xea;xeb;xec", count_uniform());
case 'L': Out("mirror_rules", arb::current.mirror_rules);
case 'f': Out("file", tesname);
2021-08-18 23:39:49 +00:00
case 'l': Out("shortcut", longest_shortcut());
case '3': Out("shqty", longest_shortcut().first);
case '4': Out("shlong", longest_shortcut().second);
2021-08-18 23:39:49 +00:00
case 'A': Out("analyzer", longest_analyzer());
case 'H': Out("hard", hard_parents);
case '1': Out("single", single_live_branches);
case '2': Out("double", double_live_branches);
case 'p': Out("premini", states_premini);
case 'K': Out("movecount", format("%ld", rulegen::movecount));
}
println(hlog);
2021-07-29 14:18:41 +00:00
fflush(stdout);
if(add_header) { add_header = false; goto again; }
// for(auto& sh: shortcuts) println(hlog, sh.first, " : ", isize(sh.second), " shortcuts (CSV)");
2021-11-23 09:25:16 +00:00
/*if(status == "ACC")*/ print_rules();
if(status != "ACC") treestates = alt_treestates;
2021-07-29 14:18:41 +00:00
/* for(auto& a: analyzers)
println(hlog, "analyzer ", a.first, " size is ", isize(a.second.spread)); */
fflush(stdout);
if(seq_stream)
list_all_sequences(tesname);
2021-07-29 14:18:41 +00:00
fflush(stdout);
}
void out_reg() {
println(hlog, "X ", int(geometry), " ", int(variation), " ", int(gp::param.first), " ", int(gp::param.second));
}
void test_all_regular(vector<eGeometry> glist) {
for(eGeometry g: glist) {
set_geometry(g);
set_variation(eVariation::pure);
out_reg();
set_geometry(g);
set_variation(eVariation::bitruncated);
out_reg();
for(int a=1; a<5; a++)
for(int b=0; b<=a; b++) {
if(a==1 && b == 0) continue;
if(a==1 && b == 1 && S3 == 3) continue;
stop_game();
set_geometry(g);
gp::param = {a, b};
set_variation(eVariation::goldberg);
out_reg();
if(S3 == 4 && (geometry == g46 || ((a+b)%2 == 0))) {
stop_game();
set_variation(eVariation::unrectified);
out_reg();
}
if(S3 == 3 && (geometry == gOctagon || (a-b)%3 == 0)) {
stop_game();
set_variation(eVariation::untruncated);
out_reg();
stop_game();
set_variation(eVariation::warped);
out_reg();
}
}
}
}
void set_dir(string s) {
testdir = testroot + s;
system(("mkdir -p " + testdir).c_str());
}
void set_arcm(eVariation v, string symbol) {
auto& c = arcm::current;
c.parse(symbol);
c.compute_geometry();
set_geometry(gArchimedean);
set_variation(v);
if(c.errors) {
println(hlog, "ERROR: incorrect Archimedean symbol ", symbol);
exit(1);
}
}
bool set_general(const string& s) {
stop_game();
2021-11-23 09:25:16 +00:00
arb::current.name = s;
if(s[0] == 'X') {
int a, b, c, d;
sscanf(s.c_str()+2, "%d%d%d%d", &a, &b, &c, &d);
geometry = eGeometry(a);
variation = eVariation(b);
gp::param = {c, d};
println(hlog, "parsed ", s, " as ", full_geometry_name());
}
else if(s[0] == 'P') set_arcm(eVariation::pure, s.c_str()+2);
else if(s[0] == 'D') set_arcm(eVariation::dual, s.c_str()+2);
else if(s[0] == 'B') set_arcm(eVariation::bitruncated, s.c_str()+2);
else try {
set_geometry(gArbitrary);
arb::load(s);
}
catch(hr_parse_exception& ex) {
println(hlog, "failed: ", ex.s);
return false;
}
catch(arb::hr_polygon_error& ex) {
println(hlog, "poly error");
return false;
}
catch(hr_exception& ex) {
println(hlog, "other exception");
return false;
}
if(cgflags & qAFFINE) {
println(hlog, "illegal tessellation found: affine");
return false;
}
if(arb::current.is_star) {
println(hlog, "illegal tessellation found: star");
return false;
}
if(arb::current.have_tree) {
println(hlog, "illegal tessellation found: tree");
return false;
}
if(sphere) {
println(hlog, "illegal tessellation found: spherical");
return false;
}
return true;
}
2021-07-29 14:18:41 +00:00
void test_from_file(string list) {
2021-08-23 13:57:22 +00:00
set_dir(list);
2021-07-29 14:18:41 +00:00
vector<string> filenames;
std::ifstream is("devmods/rulegen-tests/" + list + ".lst");
string s;
while(getline(is, s)) {
while(s != "" && s[0] == ' ') s = s.substr(1);
if(s != "" && s[0] != '#') filenames.push_back(s);
2021-07-29 14:18:41 +00:00
}
int trv = test_rotate_val;
int id = 0;
for(const string& s: filenames) {
println(hlog, "loading ", s, "... ", id++, "/", isize(filenames));
2021-07-29 14:18:41 +00:00
if(trv) { trv--; id++; continue; }
if(set_general(s))
test_current(s);
2021-07-29 14:18:41 +00:00
}
}
2021-11-23 09:25:16 +00:00
void rulecat(string list) {
set_dir(list);
vector<string> filenames;
std::ifstream is("devmods/rulegen-tests/" + list + ".lst");
string s;
while(getline(is, s)) {
while(s != "" && s[0] == ' ') s = s.substr(1);
if(s != "" && s[0] != '#') filenames.push_back(s);
}
for(const string& s: filenames) {
string cat1;
/*
if(s[0] == 'X' && s[4] == '1')
cat1 = "regular";
else if(s[0] == 'X' && s[4] == '0')
cat1 = "bitruncated";
else if(s[0] == 'X' && s[4] == '5' && s[6] == 1 && s[7] == 0)
cat1 = "regular"; */
if(0) ;
else if(s[0] == 'X')
cat1 = "variations";
/*
else if(s[0] == 'P' && is_reg(s))
cat1 = "regular";
else if(s[0] == 'D' && is_reg(s))
cat1 = "regular";
else if(s[0] == 'B' && is_reg(s))
cat1 = "bitruncated";
*/
else if(s[0] == 'P')
cat1 = "archimedean";
else if(s[0] == 'D')
cat1 = "lavasz";
else if(s[0] == 'B')
cat1 = "archibi";
else {
int i = 0;
while(s[i] != '/') i++;
i++;
int i1 = i;
while(s[i] != '/') i++;
cat1 = s.substr(i1, i-i1);
if(cat1 == "multitile" && s.substr(i+1, 9) == "polyforms")
cat1 = "polyforms";
}
printf("CSV;%s;%s\n", cat1.c_str(), s.c_str());
}
}
void label_all(int i, int mode) {
queue<tcell*> to_label;
auto m = dynamic_cast<hrmap_testproto*> (currentmap);
set<tcell*> visited;
tcell *next_dist = nullptr;
int cdist = 0;
auto visit = [&] (tcell *c) {
if(visited.count(c)) return;
visited.insert(c);
to_label.push(c);
if(!next_dist) next_dist = c;
};
visit(m->counterpart[cwt.at->master]);
while(true) {
tcell *c = to_label.front();
if(c == next_dist) {
println(hlog, "next distance for ", c);
next_dist = nullptr;
cdist++;
if(cdist == i) break;
}
try {
twalker cw(c, 0);
if(mode & 4) index_pointer(c);
if(mode & 1) get_parent_dir(cw);
if(mode & 2) get_code(cw);
}
catch(hr_exception& ex) {
}
to_label.pop();
for(int i=0; i<c->type; i++)
visit(c->cmove(i));
}
cleanup_protomap();
}
void seek_label(string s) {
auto *c = first_tcell;
while(c) {
if(pointer_indices.count(c) && index_pointer(c) == s) {
move_to(c);
return;
}
c = c->next;
}
println(hlog, "not found");
}
2021-07-29 14:18:41 +00:00
int testargs() {
using namespace arg;
if(0) ;
else if(argis("-testproto")) {
restart_game_on(new hrmap_testproto);
println(hlog, "creatad a testproto map with ", tcellcount, " cells");
}
else if(argis("-test-stats")) {
shift(); test_stats = args();
add_header = true;
add_labels = false;
}
else if(argis("-test-stats-label")) {
shift(); test_stats = args();
add_header = false;
add_labels = true;
}
2021-07-29 14:18:41 +00:00
else if(argis("-test-this")) {
PHASEFROM(3);
try {
test_current(arb::current.filename);
2021-07-29 14:18:41 +00:00
}
catch(rulegen_failure& e) {
}
}
else if(argis("-test-list")) {
PHASEFROM(3);
shift();
test_from_file(args());
}
2021-11-23 09:25:16 +00:00
else if(argis("-rulecat")) {
PHASEFROM(3);
shift();
rulecat(args());
}
2021-07-29 14:18:41 +00:00
else if(argis("-trv")) {
shift(); test_rotate_val = argi();
}
else if(argis("-ruleflag")) {
shift();
rulegen::flags ^= Flag(argi());
}
else if(argis("-ruleflag-sub")) {
swap(rulegen::flags, sub_rulegen_flags);
}
2021-07-29 14:18:41 +00:00
else if(argis("-view-debug"))
view_debug();
2021-08-17 12:35:53 +00:00
else if(argis("-print-rules"))
print_rules();
2021-07-29 14:18:41 +00:00
else if(argis("-clear-debug"))
clear_debug();
/* tools for taking screenshots -- after testproto */
else if(argis("-label-all")) {
shift(); int q = argi();
shift(); int mode = argi();
label_all(q, mode);
}
else if(argis("-label-clean")) {
pointer_indices.clear();
}
else if(argis("-label-seek")) {
shift();
seek_label(args());
}
else if(argis("-color-this")) {
auto m = dynamic_cast<hrmap_testproto*> (currentmap);
shift();
color_t hex = arghex();
println(hlog, "assigning ", hex, " to ", m->counterpart[cwt.at->master]);
setdist(cwt.at, 7, nullptr);
cwt.at->landparam = hex;
}
else if(argis("-color-label")) {
shift(); label_color = arghex();
}
else if(argis("-color-wall")) {
shift(); wall_color = arghex();
}
else if(argis("-color-out")) {
shift(); out_color = arghex();
}
else if(argis("-color-tree")) {
shift(); tree_color = arghex();
}
else if(argis("-draw-which")) {
shift(); draw_which = argi();
}
else if(argis("-no-codes")) {
show_codes = false;
}
else if(argis("-no-dist")) {
show_dist = false;
}
else if(argis("-dseek-start")) {
shift(); move_to(t_origin[argi()]);
}
2021-07-29 14:18:41 +00:00
else if(argis("-dseek-giver")) {
shift(); move_to(treestates[argi()].giver);
}
2021-08-23 22:34:25 +00:00
else if(argis("-origin-id")) {
shift(); origin_id = argi();
}
else if(argis("-seqf")) {
shift();
seq_stream = new fhstream(args(), "w");
}
else if(argis("-tesgen")) {
shift(); string s = args();
set_general(s);
if(!arb::in()) try {
arb::convert::convert();
arb::convert::activate();
}
catch(hr_exception& e) {
println(hlog, "failed to convert ", s);
}
}
2021-08-23 22:34:25 +00:00
else if(argis("-dseek")) {
shift();
int i = argi();
if(i >= 0 && i < isize(debuglist)) {
auto m = dynamic_cast<hrmap_testproto*> (currentmap);
cwt = {m->clone(debuglist[i].at)->c7, debuglist[i].spin};
centerover = cwt.at;
View = Id;
}
else
println(hlog, "wrong dseek index");
}
2021-07-29 14:18:41 +00:00
else return 1;
return 0;
}
auto testhooks =
addHook(hooks_args, 100, testargs);
} }