// Hyperbolic Rogue // Copyright (C) 2011-2016 Zeno Rogue, see 'hyper.cpp' for details // Hyperbolic geometry is a good tool to visualize data, // especially trees and scale-free networks. This file // uses HyperRogue to browse such vizualizations. // Since it is not of use for general HyperRogue players, // it is disabled by default -- compile with the ROGUEVIZ flag to enable this. // How to use: // hyper -embed -- visualize a social network // embedded into hyperbolic plane, see: // https://bitbucket.org/HaiZhung/hyperbolic-embedder/overview // (it uses the same format) // hyper -tol -- visualize the tree of life, // based on a XML dump from https://tree.opentreeoflife.org/ // hyper -tess -- visualize a horocyclic tesselation, #include "../hyper.h" #include "rogueviz.h" namespace hr { namespace linepatterns { extern linepattern patPalace, patZebraTriangles, patZebraLines; } } namespace rogueviz { ld fat_edges = 0; ld ggamma = 1; using namespace hr; edgetype default_edgetype = { .1, .1, .1, DEFAULT_COLOR, 0xFF0000FF, "default" }; void init(); bool showlabels = false; bool specialmark = false; bool edge_legend = false; bool rog3 = false; int vertex_shape = 1; string edgename; string fname; // const char *fname; // const char *cfname; eVizkind kind; bool on; vector> edgetypes; edgetype *add_edgetype(const string& name) { auto e = make_shared (default_edgetype); e->name = name; edgetypes.push_back(e); return &*e; } map > next_hue; color_t parse1(const string& s) { // color can be given as RRGGBB // or as 'Rmax,min,alpha,step,start', for rainbow Collatz if(s[0] == 'R') { int mh = 192, minh = 0, alpha = 255, step = 50, start = 0; sscanf(s.c_str(), "R%x,%x,%x,%d,%d", &mh, &minh, &alpha, &step, &start); vector hues; color_t difh = mh - minh; color_t base = alpha + minh * 0x1010100; for(unsigned y=0; y(); auto& i = *cp.img; i.tdata.twidth = 1024; if(!(i.tdata.readtexture(fname) && i.tdata.loadTextureGL())) { println(hlog, "failed to load: ", fname); cp.img = NULL; return cp; } println(hlog, "loaded texture: ", fname); for(int x=0; x<16; x++) for(int y=0; y<16; y++) { auto addv = [&] (ld x, ld y) { x -= 8; y -= 8; x /= 16.; y /= 16.; ld r = max(abs(x), abs(y)) / hypot(x, y); if(x || y) { x *= r; y *= r; } i.tinf.tvertices.push_back(glhr::makevertex(x + .5, y + .5, 0)); i.vertices.push_back(hpxy(x * .4, y * .4)); }; addv(x, y); addv(x, y+1); addv(x+1, y); addv(x, y+1); addv(x+1, y); addv(x+1, y+1); } i.tinf.texture_id = i.tdata.textureid; #endif } auto pos = s.find(":"); if(pos != string::npos) { cp.color1 = parse1(s.substr(0, pos)); cp.shade = s[pos+1]; cp.color2 = parse1(s.substr(pos+2)); } else { cp.shade = 0; cp.color2 = 0; cp.color1 = parse1(s); } return cp; } vector vdata; transmatrix cspin(int i, int ch) { return spin(M_PI + (2 * M_PI * (i+1)) / (ch+1)); } map labeler; int getid(const string& s) { if(labeler.count(s)) return labeler[s]; else { int id = isize(vdata); vdata.resize(isize(vdata) + 1); vdata[id].name = s; return labeler[s] = id; } } int getnewid(string s) { while(labeler.count(s)) s += "'"; return getid(s); } void addedge0(int i, int j, edgeinfo *ei) { vdata[i].edges.push_back(make_pair(j, ei)); vdata[j].edges.push_back(make_pair(i, ei)); } void createViz(int id, cell *c, transmatrix at) { vertexdata& vd(vdata[id]); vd.m = new shmup::monster; vd.m->pid = id; vd.m->type = moRogueviz; vd.m->base = c; vd.m->at = at; vd.m->isVirtual = false; } void notimpl() { printf("Not implemented\n"); exit(1); } hyperpoint where(int i, cell *base) { auto m = vdata[i].m; if(m->base == base) return tC0(m->at); else if(confusingGeometry()) { return calc_relative_matrix(m->base, base, C0) * tC0(m->at); } else { // notimpl(); // actually probably that's a buug return inverse(ggmatrix(currentmap->gamestart())) * (ggmatrix(m->base) * tC0(m->at)); } } void addedge(int i, int j, edgeinfo *ei) { cell *base = confusingGeometry() ? vdata[i].m->base : currentmap->gamestart(); hyperpoint hi = where(i, base); hyperpoint hj = where(j, base); double d = hdist(hi, hj); if(d >= 4) { hyperpoint h = mid(hi, hj); int id = isize(vdata); vdata.resize(id+1); vertexdata& vd(vdata[id]); vd.cp = colorpair(0x400000FF); vd.virt = ei; createViz(id, base, rgpushxto0(h)); vd.m->no_targetting = true; addedge(i, id, ei); addedge(id, j, ei); virtualRebase(vdata[i].m); } else addedge0(i, j, ei); } vector edgeinfos; void addedge(int i, int j, double wei, bool subdiv, edgetype *t) { edgeinfo *ei = new edgeinfo(t); edgeinfos.push_back(ei); ei->i = i; ei->j = j; ei->weight = wei; if(subdiv) addedge(i, j, ei); else addedge0(i, j, ei); } void storeall(int from) { for(int i=from; istore(); } colorpair dftcolor = 0x282828FF; namespace spiral { ld mul; transmatrix at(double d) { return spin(log(d) * 2 * M_PI / log(mul)) * xpush(log(d)); } void place(int N, ld _mul) { mul = _mul; init(); kind = kSpiral; vdata.resize(N); for(int i=0; ivisible_from = 1. / (N+.5); for(int i=0; i= 0 && j0 < N) addedge(i, j0, 1/(i+1), false, t); } } void color(ld start, ld period, colorpair c) { int N = isize(vdata); int maxw = N; while(start >= 0 && start < N) { int i = int(start); vdata[i].cp = c; start += period; maxw--; if(maxw <= 0) break; } } } namespace collatz { double s2, s3, p2, p3; double cshift = -1; transmatrix T2, T3; edgetype *collatz1, *collatz2; void start() { init(); kind = kCollatz; collatz1 = add_edgetype("1"); collatz2 = add_edgetype("2"); vdata.resize(1); vertexdata& vd = vdata[0]; createViz(0, cwt.at, xpush(cshift)); virtualRebase(vd.m); vd.cp = dftcolor; vd.data = 0; addedge(0, 0, 1, false, collatz::collatz1); vd.name = "1"; storeall(); T2 = spin(collatz::s2) * xpush(collatz::p2); T3 = spin(collatz::s3) * xpush(collatz::p3); } void lookup(long long reached, int bits) { while(reached < (1ll< seq; while(reached>1) { seq.push_back(llts(reached)); if(reached&1) reached += (reached>>1)+1; else reached >>= 1; } // seq.push_back("1"); reverse(seq.begin(), seq.end()); int id = 0; int next = 0; int steps = 0; while(true) { steps++; if(std::isnan(View[0][0])) exit(1); shmup::turn(100); drawthemap(); centerpc(100); optimizeview(); fixmatrix(View); bfs(); setdist(cwt.at, 7 - getDistLimit() - genrange_bonus, NULL); vertexdata& vd = vdata[id]; for(int e=0; ebase; if(shmup::on) shmup::pc[0]->base = cwt.at; if(next == isize(seq)) goto found; } } } found: printf("steps = %d\n", steps); } } int readLabel(fhstream& f) { string s = scan(f); if(s == "") return -1; return getid(s); } namespace anygraph { double R, alpha, T; vector > coords; edgetype *any; int N; void fixedges() { for(int i=N; idead = true; for(int i=0; iorig = NULL; addedge(e->i, e->j, e); } } void tst() {} void read(string fn, bool subdiv, bool doRebase, bool doStore) { init(); kind = kAnyGraph; any = add_edgetype("embedded edges"); fname = fn; fhstream f(fn + "-coordinates.txt", "rt"); if(!f.f) { printf("Missing file: %s-coordinates.txt\n", fname.c_str()); exit(1); } printf("Reading coordinates...\n"); string ignore; if(!scan(f, ignore, ignore, ignore, ignore, N, anygraph::R, anygraph::alpha, anygraph::T)) { printf("Error: incorrect format of the first line\n"); exit(1); } vdata.reserve(N); while(true) { string s = scan(f); println(hlog, "s: ", s.c_str()); if(s == "D11.11") tst(); if(s == "" || s == "#ROGUEVIZ_ENDOFDATA") break; int id = getid(s); vertexdata& vd(vdata[id]); vd.name = s; vd.cp = colorpair(dftcolor); double r, alpha; if(!scan(f, r, alpha)) { printf("Error: incorrect format of r/alpha\n"); exit(1); } coords.push_back(make_pair(r, alpha)); transmatrix h = spin(alpha * degree) * xpush(r); createViz(id, currentmap->gamestart(), h); } fhstream g(fn + "-links.txt", "rt"); if(!g.f) { println(hlog, "Missing file: ", fname, "-links.txt"); exit(1); } println(hlog, "Reading links..."); int qlink = 0; while(true) { int i = readLabel(g), j = readLabel(g); if(i == -1 || j == -1) break; addedge(i, j, 1, subdiv, any); qlink++; } if(doRebase) { printf("Rebasing...\n"); for(int i=0; i children; }; vector tol; void child(int pid, int id) { if(isize(tol) <= id) tol.resize(id+1); treevertex& v = tol[id]; v.parent = pid; tol.push_back(v); if(pid >= 0) tol[pid].children.push_back(id); } void readnode(FILE *f, int pid) { string lab = ""; while(true) { int c = fgetc(f); if(c == EOF) { fprintf(stderr, "Ended prematurely\n"); exit(1); } if(c == ',') break; if(c == ')') { int id = getnewid(lab); child(pid, id); return; } lab += c; } int id = getnewid(lab); child(pid, id); while(true) { int c = fgetc(f); // printf("c=%c at %d/%d\n", c, pid, id); if(c == EOF) { fprintf(stderr, "Ended prematurely\n"); exit(1); } if(c == ' ' || c == 10 || c == 13 || c == 9 || c == ',') continue; else if(c == '(') readnode(f, id); else if(c == ')') break; } } int xpos; void spos(int at, int d) { tol[at].spos = xpos++; tol[at].depth = d; for(int i=0; ipid = i; vd.data = lv.parent; createViz(i, cwt.at, h); vd.cp = dftcolor; if(tol[i].parent >= 0) addedge(i, tol[i].parent, 1, true, tree_edge); } for(int i=0; i snakecells; vector snakefirst, snakelast; vector snakenode; vector snakeid; vector lpbak; vector wpbak; bool snake_enabled; void setsnake(cellwalker& cw, int i) { lpbak[i] = cw.at->landparam; wpbak[i] = cw.at->wparam; cw.at->landparam = i; cw.at->wparam = INSNAKE; // cw.at->monst = moWormtail; cw.at->mondir = cw.spin; snakecells[i] = cw.at; } void snakeswitch() { for(int i=0; ilandparam; c->landparam = x; x = wpbak[i]; wpbak[i] = c->wparam; c->wparam = x; } snake_enabled = !snake_enabled; } void enable_snake() { if(!snake_enabled) snakeswitch(); } void disable_snake() { if(snake_enabled) snakeswitch(); } int snakedist(int i, int j) { if(i < insnaketab && j < insnaketab) return sdist[i][j]; if(bounded) return celldistance(snakecells[i], snakecells[j]); int i0 = i, i1 = i, j0 = j, j1 = j; int cost = 0; // intersect while(true) { if(j0 > i1+1) { j0 = snakefirst[j0], j1 = snakelast[j1]; cost++; } else if(i0 > j1+1) { i0 = snakefirst[i0], i1 = snakelast[i1]; cost++; } else if(j1+1 == i0) return cost+1; else if(i1+1 == j0) return cost+1; else return cost; } } void initSnake(int n) { if(bounded) n = isize(currentmap->allcells()); numsnake = n; snakecells.resize(numsnake); snakefirst.resize(numsnake); snakelast.resize(numsnake); snakenode.resize(numsnake); lpbak.resize(numsnake); wpbak.resize(numsnake); if(bounded) { for(int i=0; iallcells()[i], 0); setsnake(cw, i); } } else { cellwalker cw = cwt; setsnake(cw, 0); cw += wstep; setsnake(cw, 1); for(int i=2; i<=numsnake; i++) { if(i == numsnake && sphere) break; cw += wstep; snakefirst[i-1] = cw.at->landparam; while(cw.at->wparam == INSNAKE) { snakelast[i-1] = cw.at->landparam; cw = cw + wstep + 1 + wstep; } if(i == numsnake) break; setsnake(cw, i); cw += 1; } } int stab = min(numsnake, MAXSNAKETAB); for(int i=0; iweight2; } /* cell *c = snakecells[id]; for(int i=0; itype; i++) { cell *c2 = c->move(i); if(c2 && c2->wparam == INSNAKE && snakenode[c2->landparam] >= 0) cost += 100; } */ return cost; } // std::mt19937 los; bool infullsa; double cost; int N; vector chgs; edgetype *sag_edge; void forgetedges(int id) { for(int i=0; iorig = NULL; } bool chance(double p) { p *= double(hrngen.max()) + 1; auto l = hrngen(); auto pv = (decltype(l)) p; if(l < pv) return true; if(l == pv) return chance(p-pv); return false; } void saiter() { aiter: int t1 = hrand(N); int sid1 = snakeid[t1]; int sid2; int s = hrand(6); if(s == 3) s = 2; if(s == 4) s = 5; if((sagpar&1) && (s == 2 || s == 3 || s == 4)) return; if(s == 5) sid2 = hrand(numsnake); else { cell *c; if(s>=2 && isize(vdata[t1].edges)) c = snakecells[snakeid[hrand(isize(vdata[t1].edges))]]; else c = snakecells[sid1]; int it = s<2 ? (s+1) : s-2; for(int ii=0; iitype); c = c->move(d); if(!c) goto aiter; if(c->wparam != INSNAKE) goto aiter; } sid2 = c->landparam; } int t2 = snakenode[sid2]; snakenode[sid1] = -1; snakeid[t1] = -1; snakenode[sid2] = -1; if(t2 >= 0) snakeid[t2] = -1; double change = costat(t1,sid2) + costat(t2,sid1) - costat(t1,sid1) - costat(t2,sid2); snakenode[sid1] = t1; snakeid[t1] = sid1; snakenode[sid2] = t2; if(t2 >= 0) snakeid[t2] = sid2; if(change < 0) chgs.push_back(-change); if(change > 0 && (sagmode == sagHC || !chance(exp(-change * exp(-temperature))))) return; snakenode[sid1] = t2; snakenode[sid2] = t1; snakeid[t1] = sid2; if(t2 >= 0) snakeid[t2] = sid1; if(vdata[t1].m) vdata[t1].m->base = snakecells[sid2]; if(t2 >= 0 && vdata[t2].m) vdata[t2].m->base = snakecells[sid1]; cost += 2*change; if(t1 >= 0) forgetedges(t1); if(t2 >= 0) forgetedges(t2); } void organize() { for(int i=0; i freenodes; for(int i=0; i= numsnake || err < 1) sid = -1; if(!labeler.count(lab)) { printf("unknown vertex: %s\n", lab.c_str()); } else { int id = getid(lab); snakeid[id] = sid; } } afterload: if(sf) fclose(sf); organize(); for(int i=0; ibase = snakecells[sag::snakeid[i]]; forgetedges(i); } shmup::fixStorage(); } vector sagedges; /* bool totcmp(int i, int j) { return totwei[i] > totwei[j]; } */ int ipturn = 100; int numiter = 0; int hightemp = 10; int lowtemp = -15; void dofullsa(int satime) { sagmode = sagSA; enable_snake(); int t1 = SDL_GetTicks(); while(true) { int t2 = SDL_GetTicks(); double d = (t2-t1) / (1000. * satime); if(d > 1) break; temperature = hightemp - (d*(hightemp-lowtemp)); chgs.clear(); for(int i=0; i<50000; i++) { numiter++; sag::saiter(); } DEBB(DF_LOG, (format("it %8d temp %6.4f [1/e at %13.6f] cost = %f ", numiter, double(sag::temperature), (double) exp(sag::temperature), double(sag::cost)))); sort(chgs.begin(), chgs.end()); int cc = chgs.size() - 1; DEBB(DF_LOG, (format("%9.4f .. %9.4f .. %9.4f .. %9.4f .. %9.4f\n", double(chgs[0]), double(chgs[cc/4]), double(chgs[cc/2]), double(chgs[cc*3/4]), double(chgs[cc])))); fflush(stdout); } temperature = -5; disable_snake(); sagmode = sagOff; } void iterate() { if(!sagmode) return; int t1 = SDL_GetTicks(); enable_snake(); for(int i=0; i 200) ipturn /= 2; else ipturn = ipturn * 100 / t; DEBB(DF_LOG, ("it %8d temp %6.4f [2:%8.6f,10:%8.6f,50:%8.6f] cost = %f\n", numiter, double(sag::temperature), (double) exp(-2 * exp(-sag::temperature)), (double) exp(-10 * exp(-sag::temperature)), (double) exp(-50 * exp(-sag::temperature)), (double) sag::cost)); } void savesnake(const string& fname) { FILE *f = fopen(fname.c_str(), "wt"); for(int i=0; i= sag_edge->visible_from) pedge[snakedist(snakeid[ei.i], snakeid[ei.j])]++; } for(int d=0; d<30; d++) if(indist[d]) printf("%2d: %7d/%7d %7.3lf\n", d, pedge[d], indist[d], double(pedge[d] * 100. / indist[d])); ld loglik = 0; for(int d=0; d<30; d++) { int p = pedge[d], pq = indist[d]; int q = pq - p; if(p && q) loglik += p * log(p) + q * log(q) - pq * log(pq); } println(hlog, "loglikelihood = ", fts(loglik)); } void readsag(const char *fname) { maxweight = 0; sag_edge = add_edgetype("SAG edge"); fhstream f(fname, "rt"); if(!f.f) { printf("Failed to open SAG file: %s\n", fname); exit(1); } // while(fgetc(f) != 10 && fgetc(f) != 13 && !feof(f)) ; while(!feof(f.f)) { string l1, l2; while(true) { int c = fgetc(f.f); if(c == EOF) return; else if(c == ';') break; else if(c == 10 || c == 13 || c == 32 || c == 9) ; else l1 += c; } while(true) { int c = fgetc(f.f); if(c == EOF) return; else if(c == ';') break; else if(c == 10 || c == 13 || c == 32 || c == 9) ; else l2 += c; } ld wei; if(!scan(f, wei)) continue; edgeinfo ei(sag_edge); ei.i = getid(l1); ei.j = getid(l2); ei.weight = wei; sagedges.push_back(ei); } } ld edgepower=1, edgemul=1; void read(string fn) { fname = fn; init(); kind = kSAG; temperature = 0; sagmode = sagOff; readsag(fname.c_str()); N = isize(vdata); // totwei.resize(N); // for(int i=0; i= maxwei[ei.i] / 5 || ei.weight >= maxwei[ei.j] / 5); ei.weight2 = pow((double) ei.weight, (double) edgepower) * edgemul; // LANG:: pow(ei.weight, .4) / 50; // ei.weight2 = 0; int w = ei.weight; while(w) { w >>= 1; ei.weight2++; } /* if(totwei[ei.i] <= 0 || totwei[ei.j] <= 0) { printf("BAD TOTWEI\n"); exit(1); } ei.weight2 = 3 * ( sqrt(ei.weight * 1. / totwei[ei.i]) * log(totwei[ei.i]) * log(totwei[ei.i]) + sqrt(ei.weight * 1. / totwei[ei.j]) * log(totwei[ei.j]) * log(totwei[ei.j])); */ // printf("%f\n", ei.weight2); addedge0(ei.i, ei.j, &ei); } initSnake(N*2); printf("numsnake = %d\n", numsnake); if(numsnake < N) { printf("Error: snake does not fit\n"); exit(1); } snakeid.resize(N); for(int i=0; iweight > e2->weight; } bool which_weight = false; void rogueviz_help(int id, int pagenumber) { vertexdata& vd = vdata[id]; int noedges = isize(vd.edges); help = helptitle(vd.name, vd.cp.color1 >> 8); if(vd.info) { #if CAP_URL help_extension hex; hex.key = 'L'; hex.text = "open link"; hex.subtext = *vd.info; hex.action = [&vd] () { open_url(*vd.info); }; help_extensions.push_back(hex); #else help += "\n\nlink: " + *vd.info; #endif } vector alledges; for(int j=0; jweight < ei->type->visible_from_help) continue; int k = ei->i ^ ei->j ^ id; hex.text = vdata[k].name; hex.color = vdata[k].cp.color1 >> 8; if(kind == kSAG) { if(which_weight) hex.subtext = fts(ei->weight2); else hex.subtext = fts(ei->weight); } hex.action = [k] () { help_extensions.clear(); rogueviz_help(k, 0); }; help_extensions.push_back(hex); } if(noedges > pagenumber + 10) { help_extension hex; hex.key = 'z'; hex.text = "next page"; hex.subtext = its(pagenumber+10) + "/" + its(noedges) + " edges"; hex.action = [id, pagenumber] () { help_extensions.clear(); rogueviz_help(id, pagenumber + 10); }; help_extensions.push_back(hex); } if(kind == kSAG && noedges) { help_extension hex; hex.key = 'w'; hex.text = "displayed weight"; hex.subtext = which_weight ? "attraction force" : "weight from the data"; hex.action = [id, pagenumber] () { which_weight = !which_weight; help_extensions.clear(); rogueviz_help(id, pagenumber); }; help_extensions.push_back(hex); } } bool describe_monster(shmup::monster *m, string& out) { if(m->type != moRogueviz) return false; int i = m->pid; vertexdata& vd = vdata[i]; string o = vd.name + ", "+its(isize(vd.edges))+" edges"; /* if(isize(vd.edges) < 10) { for(int i=0; isnakeid)); } */ help = bygen([i] () { rogueviz_help(i, 0); }); if(out == XLATN("Canvas")) out = o; else out = out + ", " + o; return true; } bool activate(shmup::monster *m) { if(m->type != moRogueviz) return false; int i = m->pid; vertexdata& vd = vdata[i]; vd.cp = colorpair(rand() & 0xFFFFFFFF); for(int i=0; iorig = NULL; return true; /* if(ealpha == 1) ealpha = 8; else if(ealpha == 8) ealpha = 32; else if(ealpha == 32) ealpha = 255; else ealpha = 1; */ } void storevertex(vector& tab, const hyperpoint& h) { tab.push_back(glhr::pointtogl(h)); } double linequality = .1; void storelineto(vector& tab, const hyperpoint& h1, const hyperpoint& h2) { if(intval(h1, h2) < linequality) storevertex(tab, h2); else { hyperpoint h3 = mid(h1, h2); storelineto(tab, h1, h3); storelineto(tab, h3, h2); } } void storeline(vector& tab, const hyperpoint& h1, const hyperpoint& h2) { storevertex(tab, h1); storelineto(tab, h1, h2); } color_t darken_a(color_t c) { for(int p=0; p<3; p++) for(int i=0; i> 1; return c; } #if CAP_SVG #define SVG_LINK(x) svg::link = (x) #else #define SVG_LINK(x) #endif void queuedisk(const transmatrix& V, const colorpair& cp, bool legend, const string* info, int i) { if(legend && (int) cp.color1 == (int) 0x000000FF && backcolor == 0) poly_outline = 0x606060FF; else poly_outline = (bordcolor << 8) | 0xFF; if(cp.img) { for(hyperpoint h: cp.img->vertices) curvepoint(V * h); auto& qc = queuecurve(0, 0xFFFFFFFF, PPR::MONSTER_HEAD); qc.tinf = &cp.img->tinf; qc.flags |= POLY_TRIANGLES; return; } transmatrix V1; auto& sh = vertex_shape == 2 ? cgi.shHeptaMarker : vertex_shape == 3 ? cgi.shSnowball : cgi.shDisk; if(vertex_shape == 0) ; else if(GDIM == 3 && among(cp.shade, 'b', 'f', 'g', 'B', 'F', 'G')) { V1 = V; } else if(GDIM == 3) { V1 = face_the_player(V); if(info) queueaction(PPR::MONSTER_HEAD, [info] () { SVG_LINK(*info); }); queuepolyat(V1, sh, darken_a(cp.color1), PPR::MONSTER_HEAD); if(info) queueaction(PPR::MONSTER_HEAD, [] () { SVG_LINK(""); }); V1 = V; } else if(rog3) { int p = poly_outline; poly_outline = OUTLINE_TRANS; queuepolyat(V, sh, 0x80, PPR::MONSTER_SHADOW); poly_outline = p; if(info) queueaction(PPR::MONSTER_HEAD, [info] () { SVG_LINK(*info); }); queuepolyat(V1 = mscale(V, cgi.BODY), sh, darken_a(cp.color1), PPR::MONSTER_HEAD); if(info) queueaction(PPR::MONSTER_HEAD, [] () { SVG_LINK(""); }); } else { if(info) queueaction(PPR::MONSTER_HEAD, [info] () { SVG_LINK(*info); }); queuepoly(V1 = V, sh, darken_a(cp.color1)); if(info) queueaction(PPR::MONSTER_HEAD, [] () { SVG_LINK(""); }); } switch(cp.shade) { case 't': queuepoly(V1, cgi.shDiskT, darken_a(cp.color2)); return; case 's': queuepoly(V1, cgi.shDiskS, darken_a(cp.color2)); return; case 'q': queuepoly(V1, cgi.shDiskSq, darken_a(cp.color2)); return; case 'm': queuepoly(V1, cgi.shDiskM, darken_a(cp.color2)); return; case 'b': queuepoly(V1, GDIM == 3 ? cgi.shAnimatedTinyEagle[wingphase(200)] : cgi.shTinyBird, darken_a(cp.color2)); return; case 'f': queuepoly(V1, cgi.shTinyShark, darken_a(cp.color2)); return; case 'g': queuepoly(V1, cgi.shMiniGhost, darken_a(cp.color2)); return; case 'B': queuepoly(V1, GDIM == 3 ? cgi.shAnimatedEagle[wingphase(100)] : cgi.shEagle, darken_a(cp.color2)); return; case 'F': queuepoly(V1, cgi.shShark, darken_a(cp.color2)); return; case 'G': queuepoly(V1, cgi.shGhost, darken_a(cp.color2)); return; } } unordered_map, int> drawn_edges; map, transmatrix> relmatrices; transmatrix& memo_relative_matrix(cell *c1, cell *c2) { auto& p = relmatrices[make_pair(c1, c2)]; if(p[2][2] == 0) p = calc_relative_matrix(c1, c2, C0); return p; } void queue_prec(const transmatrix& V, edgeinfo*& ei, color_t col) { if(!fat_edges) queuetable(V, ei->prec, isize(ei->prec), col, 0, PPR::STRUCT0); #if MAXMDIM >= 4 else { auto& t = queuetable(V, ei->prec, isize(ei->prec), 0, col | 0x000000FF, PPR::STRUCT0); t.flags |= (1<<22), // poly triangles t.offset_texture = 0, t.tinf = &ei->tinf; t.tinf->texture_id = floor_textures->renderedTexture; } #endif } bool drawVertex(const transmatrix &V, cell *c, shmup::monster *m) { if(m->dead) return true; if(m->type != moRogueviz) return false; int i = m->pid; vertexdata& vd = vdata[i]; // bool ghilite = false; // if(vd.special && specialmark) ghilite = true; if(!gmatrix.count(m->base)) printf("base not in gmatrix\n"); int lid = shmup::lmousetarget ? shmup::lmousetarget->pid : -2; if(!leftclick) for(int j=0; ji]; vertexdata& vd2 = vdata[ei->j]; int oi = ei->i, oj = ei->j; bool hilite = false; if(vdata[oi].special && vdata[oj].special && specialmark) hilite = true; else if(svg::in || inHighQual) hilite = false; else if(vd1.m == shmup::lmousetarget) hilite = true; else if(vd2.m == shmup::lmousetarget) hilite = true; else if(oi == lid || oj == lid) hilite = true; if(ei->weight < (hilite ? ei->type->visible_from_hi : ei->type->visible_from)) continue; // if(hilite) ghilite = true; bool multidraw = quotient; if(ei->lastdraw < frameid || multidraw) { ei->lastdraw = frameid; color_t col = (hilite ? ei->type->color_hi : ei->type->color); auto& alpha = part(col, 0); if(kind == kSAG) { if(ei->weight2 > maxweight) maxweight = ei->weight2; alpha *= pow(ei->weight2 / maxweight, ggamma); } // if(hilite || hiliteclick) alpha = (alpha + 256) / 2; if(svg::in && alpha < 16) continue; if(ISWEB) { if(alpha >= 128) alpha |= 15; else if(alpha >= 64) alpha |= 7; else if(alpha >= 32) alpha |= 3; else if(alpha >= 16) alpha |= 1; } alpha >>= darken; transmatrix gm1 = (multidraw || elliptic) ? V * memo_relative_matrix(vd1.m->base, c) : ggmatrix(vd1.m->base); transmatrix gm2 = (multidraw || elliptic) ? V * memo_relative_matrix(vd2.m->base, c) : ggmatrix(vd2.m->base); hyperpoint h1 = gm1 * vd1.m->at * C0; hyperpoint h2 = gm2 * vd2.m->at * C0; if(elliptic && intval(h1, h2) > intval(h1, centralsym * h2)) h2 = centralsym * h2; if(multidraw) { int code = int(h1[0]) + int(h1[1]) * 12789117 + int(h2[0]) * 126081253 + int(h2[1]) * 126891531; int& lastdraw = drawn_edges[make_pair(ei, code)]; if(lastdraw == frameid) continue; lastdraw = frameid; } /* if(hdist0(h1) < .001 || hdist0(h2) < .001) { printf("h1 = %s\n", display(h1)); printf("h2 = %s\n", display(h2)); display(m->at); display(vd2.m->at); display(V); display(gmatrix[vd2.m->base]); display(shmup::calc_gmatrix(vd2.m->base)); } */ if((col >> 8) == (DEFAULT_COLOR >> 8)) { col &= 0xFF; col |= (forecolor << 8); } bool onspiral = kind == kSpiral && abs(ei->i - ei->j) == 1; if((pmodel || onspiral) && !fat_edges) { if(onspiral) { const int prec = 20; transmatrix T = ggmatrix(currentmap->gamestart()); hyperpoint l1 = T*tC0(spiral::at(1+ei->i)); for(int z=1; z<=prec; z++) { hyperpoint l2 = T*tC0(spiral::at(1+ei->i+(ei->j-ei->i) * z / (prec+.0))); queueline(l1, l2, col, vid.linequality).prio = PPR::STRUCT0; l1 = l2; } } else { queueline(h1, h2, col, 2 + vid.linequality).prio = PPR::STRUCT0; } } else { cell *center = multidraw ? c : centerover; if(!multidraw && ei->orig && ei->orig != center && celldistance(ei->orig, center) > 3) ei->orig = NULL; if(!ei->orig) { ei->orig = center; // cwt.at; ei->prec.clear(); transmatrix T = inverse(ggmatrix(ei->orig)); if(fat_edges) { ei->tinf.tvertices.clear(); transmatrix T1 = inverse(gm1 * vd1.m->at); hyperpoint goal = T1 * h2; transmatrix S = T * gm1 * vd1.m->at * rspintox(goal); ld d = hdist0(goal); for(int a=0; a<360; a+=30) { auto store = [&] (ld a, ld b) { storevertex(ei->prec, S * cpush(0, b) * hr::cspin(1, 2, a * degree) * cpush(1, fat_edges) * C0); ei->tinf.tvertices.push_back(glhr::makevertex(0,(3+cos(a * degree))/4,0)); }; store(a, 0); store(a+30, 0); store(a, d); store(a+30, 0); store(a, d); store(a+30, d); } } else if(kind == kSpiral && abs(ei->i - ei->j) == 1) { ei->orig = currentmap->gamestart(); hyperpoint l1 = tC0(spiral::at(1+ei->i)); storevertex(ei->prec, l1); const int prec = 20; for(int z=1; z<=prec; z++) { hyperpoint l2 = tC0(spiral::at(1+ei->i+(ei->j-ei->i) * z / (prec+.0))); storeline(ei->prec, l1, l2); l1 = l2; } } else storeline(ei->prec, T*h1, T*h2); } queue_prec(multidraw ? V : ggmatrix(ei->orig), ei, col); if(elliptic) queue_prec(centralsym * ggmatrix(ei->orig), ei, col); } } /* */ } if(!vd.virt) { queuedisk(V * m->at, vd.cp, false, vd.info, i); } if(showlabels) { bool doshow = true; if(kind == kTree && i > 0 && !vd.virt) { vertexdata& vdp = vdata[vd.data]; hyperpoint h2 = ggmatrix(vdp.m->base) * vdp.m->at * C0; if(hdist(h2, V * m->at * C0) < 0.1) doshow = false; } hyperpoint h = tC0(V * m->at); transmatrix V2 = GDIM == 3 ? V * m->at : rgpushxto0(h) * ypush(PURE ? .3 : .2); // todo-variation if(doshow && !behindsphere(V2)) { auto info = vd.info; if(info) queueaction(PPR::MONSTER_HEAD, [info] () { SVG_LINK(*info); }); queuestr(V2, (svg::in ? .28 : .2) * cgi.crossf / cgi.hcrossf, vd.name, forecolor, (svg::in || ISWEB) ? 0 : 1); if(info) queueaction(PPR::MONSTER_HEAD, [] () { SVG_LINK(""); }); } } if(kind == kCollatz) { if(c->cpdist > 7 && euclid) ; else if(vd.data == 2) { // doubler vertex string s = vd.name; colorpair cp = vd.cp; vd.data = 20; int i0 = isize(vdata); vdata.resize(i0+1); vertexdata& vdn = vdata[i0]; createViz(i0, m->base, m->at * collatz::T2); virtualRebase(vdn.m); vdn.cp = perturb(cp); vdn.data = 0; addedge(i, i0, 1, false, collatz::collatz1); vdn.m->store(); int carry = 0; string s2 = s; for(int i=isize(s2)-1; i>=0; i--) { int x = 2*(s2[i] - '0') + carry; carry = x>=10; if(carry) x-=10; s2[i] = '0'+x; } if(carry) s2 = "1" + s2; vdn.name = s2; int m3 = 0; for(int i=0; ibase, m->at * collatz::T3); virtualRebase(vdn.m); vdn.cp = perturb(cp); vdn.data = 0; addedge(i, i0+1, 1, false, collatz::collatz2); vdn.m->store(); int carry = -1; string s2 = s; for(int i=isize(s2)-1; i>=0; i--) { carry += 2 * (s2[i] - '0'); int ncarry = 0; while(carry % 3) carry += 10, ncarry--; if(carry >= 30) carry -= 30, ncarry += 3; s2[i] = '0'+carry/3; carry = ncarry; } if(s2[0] == '0') s2 = s2.substr(1); vdn.name = s2; vdn.cp = perturb(vdn.cp); } } else if(vd.data < 2) { vd.data++; fixmatrix(vd.m->at); } } return true; } vector legend; vector named; color_t chosen_legend_color = DEFAULT_COLOR; bool rogueviz_hud() { color_t legend_color = chosen_legend_color == DEFAULT_COLOR ? forecolor : chosen_legend_color; if(!rogueviz::on) return false; if(cmode & sm::DRAW) return false; int qet = isize(edgetypes); if(qet == 1 || !edge_legend) qet = 0; int legit = qet + isize(legend); if(legit == 0) return true; initquickqueue(); int rad = current_display->radius/10; ld x = vid.xres - rad; for(int i=0; iradius * (i+.5)) / legit * 2 - current_display->radius + vid.yres/2; transmatrix V = atscreenpos(x, y, current_display->radius/4); poly_outline = OUTLINE_NONE; queuedisk(V, vd.cp, true, NULL, i); poly_outline = OUTLINE_DEFAULT; queuestr(int(x-rad), int(y), 0, rad*(svg::in?5:3)/4, vd.name, legend_color, 0, 16); } for(int i=0; iradius * (i+isize(legend)+.5)) / legit * 2 - current_display->radius + vid.yres/2; transmatrix V = atscreenpos(x, y, current_display->radius/8); poly_outline = t->color | 0xFF; queuepolyat(V, cgi.shTriangle, 0, PPR::MONSTER_HEAD); poly_outline = OUTLINE_DEFAULT; queuestr(int(x-rad), int(y), 0, rad*(svg::in?5:3)/4, t->name, legend_color, 0, 16); } quickqueue(); return true; } void drawExtra() { if(kind == kFullNet) { for(map::iterator it = gmatrix.begin(); it != gmatrix.end(); it++) { cell *c = it->first; c->wall = waChasm; } int index = 0; for(map::iterator it = gmatrix.begin(); it != gmatrix.end(); it++) { cell *c = it->first; bool draw = true; for(int i=0; isecond, dftcolor, false, NULL, index++); // queuepolyat(it->second, shDisk, dftcolor., PPR::LINE); } for(int i=0; i 0) vdata[getid(lab)].info = new string(buf); // replace with std::shared_ptr in C++111 continue; } else { ungetc(c2, f); char buf[600]; int err = fscanf(f, "%500s", buf); if(err > 0) x = parse(buf); } if(isize(lab) && lab[0] == '*') { lab = lab.substr(1); for(int i=0; i= 0) goto again; } } } } void init() { if(on) return; autocheat = true; showstartmenu = false; #if !ISWEB mapeditor::drawplayer = false; stop_game(); firstland = specialland = laCanvas; restart_game(shmup::on ? rg::shmup : rg::nothing); #else stop_game(); firstland = specialland = laCanvas; restart_game(rg::nothing); #endif on = true; autocheat = true; items[itOrbLife] = 0; timerghost = false; gmatrix.clear(); calcparam(); drawthemap(); gmatrix0 = gmatrix; addMessage("RogueViz enabled"); } int search_for = -1; void close() { search_for = -1; for(int i=0; idead = true; vdata.clear(); labeler.clear(); legend.clear(); for(int i=0; irebase(); } #ifndef CAP_RVSLIDES #define CAP_RVSLIDES (CAP_TOUR && !ISWEB) #endif int dimid(char x) { if(x >= 'a' && x < 'a' + GDIM) return x - 'a'; else if(x >= '0' && x < '0' + GDIM) return x - '0'; else if(x >= 'x' && x < 'x' + GDIM) return x - 'x'; else { println(hlog, "incorrect dimension ID"); throw hr_exception(); } } #if CAP_COMMANDLINE int readArgs() { using namespace arg; // options before reading if(0) ; else if(argis("-dftcolor")) { shift(); dftcolor = parse(args()); } // tree visualizer (e.g. Tree of Life) //------------------------------------- else if(argis("-tree")) { PHASE(3); shift(); tree::read(args()); } // SAG visualizer (e.g. Reddit roguelikes, GitHub languages) //----------------------------------------------------------- // (1) configure edge weights else if(argis("-edgepower")) { shift_arg_formula(sag::edgepower); shift_arg_formula(sag::edgemul); } // (1) configure temperature (high, low) else if(argis("-sagtemp")) { shift(); sag::hightemp = argi(); shift(); sag::lowtemp = argi(); } else if(argis("-sagmin")) { shift_arg_formula(default_edgetype.visible_from); default_edgetype.visible_from_hi = default_edgetype.visible_from; default_edgetype.visible_from_help = default_edgetype.visible_from; } else if(argis("-sagminhi")) { shift_arg_formula(default_edgetype.visible_from_hi); } else if(argis("-sagminhelp")) { shift_arg_formula(default_edgetype.visible_from_help); } // (2) read the edge data else if(argis("-sagpar")) { PHASE(3); shift(); sag::sagpar = argi(); } else if(argis("-sag")) { PHASE(3); shift(); sag::read(args()); } // (3) load the initial positioning else if(argis("-gload")) { PHASE(3); shift(); sag::loadsnake(args()); } // (4) perform simulated annealing: -fullsa