2019-08-10 11:43:24 +00:00
|
|
|
// Hyperbolic Rogue -- basic utility functions
|
2018-02-08 23:40:26 +00:00
|
|
|
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
|
2017-03-23 10:53:57 +00:00
|
|
|
|
2019-08-10 11:43:24 +00:00
|
|
|
/** \file util.cpp
|
|
|
|
* \brief basic utility functions: maths, parsing expressions
|
|
|
|
*/
|
2017-03-23 10:53:57 +00:00
|
|
|
|
2019-09-05 07:15:40 +00:00
|
|
|
#include "hyper.h"
|
2018-06-10 23:58:31 +00:00
|
|
|
namespace hr {
|
|
|
|
|
2018-06-12 21:10:20 +00:00
|
|
|
#if CAP_TIMEOFDAY
|
|
|
|
#if !CAP_SDL
|
2018-07-23 03:16:16 +00:00
|
|
|
int lastusec;
|
|
|
|
int uticks;
|
|
|
|
|
2020-07-03 12:42:33 +00:00
|
|
|
EX int SDL_GetTicks() {
|
2018-07-23 03:16:16 +00:00
|
|
|
struct timeval tim;
|
|
|
|
gettimeofday(&tim, NULL);
|
|
|
|
int newusec = tim.tv_usec;
|
|
|
|
uticks += newusec - lastusec;
|
|
|
|
if(newusec <= lastusec)
|
|
|
|
uticks += 1000000;
|
|
|
|
lastusec = newusec;
|
|
|
|
return uticks / 1000;
|
2018-06-12 21:10:20 +00:00
|
|
|
}
|
2018-07-23 03:16:16 +00:00
|
|
|
|
2018-06-12 21:10:20 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX long double sqr(long double x) { return x*x; }
|
2017-03-23 10:53:57 +00:00
|
|
|
|
2019-08-14 16:58:42 +00:00
|
|
|
EX ld round_nearest(ld x) { if(x > 0) return int(x+.5); else return -int(.5-x); }
|
|
|
|
EX ld round_nearest(ld x, ld multiple_of) { return multiple_of * round_nearest(x / multiple_of); }
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX int gcd(int i, int j) {
|
2017-12-14 01:52:00 +00:00
|
|
|
return i ? gcd(j%i, i) : j;
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX int gmod(int i, int j) {
|
2017-12-28 15:46:10 +00:00
|
|
|
i %= j; if(i<0) i += j;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-11-08 13:56:54 +00:00
|
|
|
EX int zgmod(int a, int b) { return b ? gmod(a, b) : a; }
|
|
|
|
|
2019-11-13 19:48:38 +00:00
|
|
|
EX int szgmod(int a, int b) {
|
|
|
|
if(!b) return a;
|
|
|
|
a = gmod(a, b);
|
|
|
|
if(2*a >= b) return a - b;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX int gdiv(int i, int j) {
|
2018-11-27 01:32:11 +00:00
|
|
|
return (i - gmod(i, j)) / j;
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX ld frac(ld x) {
|
2017-12-28 17:39:49 +00:00
|
|
|
x -= int(x);
|
|
|
|
if(x < 0) x++;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX ld lerp(ld a0, ld a1, ld x) {
|
2019-07-28 09:13:00 +00:00
|
|
|
return a0 + (a1-a0) * x;
|
|
|
|
}
|
|
|
|
|
2020-04-09 06:09:11 +00:00
|
|
|
EX cld lerp(cld a0, cld a1, ld x) {
|
|
|
|
return a0 + (a1-a0) * x;
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX ld ilerp(ld a0, ld a1, ld x) {
|
2019-07-28 09:13:00 +00:00
|
|
|
return (x-a0) / (a1-a0);
|
|
|
|
}
|
|
|
|
|
2019-09-05 10:00:55 +00:00
|
|
|
EX purehookset hooks_tests;
|
2018-07-19 22:04:23 +00:00
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX string simplify(const string& s) {
|
2018-09-27 23:49:56 +00:00
|
|
|
string res;
|
|
|
|
for(char c: s) if(isalnum(c)) res += c;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX bool appears(const string& haystack, const string& needle) {
|
2018-09-27 23:49:56 +00:00
|
|
|
return simplify(haystack).find(simplify(needle)) != string::npos;
|
2018-07-19 22:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 19:18:13 +00:00
|
|
|
#if HDR
|
2019-12-23 20:44:51 +00:00
|
|
|
struct hr_parse_exception : hr_exception {
|
|
|
|
string s;
|
|
|
|
hr_parse_exception(const string& z) : s(z) {}
|
2019-12-27 12:11:31 +00:00
|
|
|
~hr_parse_exception() noexcept(true) {}
|
2019-12-23 20:44:51 +00:00
|
|
|
};
|
|
|
|
|
2019-08-09 19:18:13 +00:00
|
|
|
struct exp_parser {
|
|
|
|
string s;
|
|
|
|
int at;
|
2020-04-16 18:59:34 +00:00
|
|
|
int line_number, last_line;
|
|
|
|
exp_parser() { at = 0; line_number = 1; last_line = 0; }
|
|
|
|
|
|
|
|
string where() {
|
|
|
|
if(s.find('\n')) return "(line " + its(line_number) + ", pos " + its(at-last_line) + ")";
|
|
|
|
else return "(pos " + its(at) + ")";
|
|
|
|
}
|
2019-08-09 19:18:13 +00:00
|
|
|
|
|
|
|
map<string, cld> extra_params;
|
|
|
|
|
|
|
|
bool ok() { return at == isize(s); }
|
2019-12-23 20:44:51 +00:00
|
|
|
char next(int step=0) { if(at >= isize(s)-step) return 0; else return s[at+step]; }
|
2019-08-09 19:18:13 +00:00
|
|
|
|
2021-10-15 20:13:52 +00:00
|
|
|
char eatchar() {
|
|
|
|
return s[at++];
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:18:13 +00:00
|
|
|
bool eat(const char *c) {
|
|
|
|
int orig_at = at;
|
|
|
|
while(*c && *c == next()) at++, c++;
|
|
|
|
if(*c == 0) return true;
|
|
|
|
else at = orig_at;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-23 20:18:26 +00:00
|
|
|
void skip_white();
|
2019-12-23 20:49:17 +00:00
|
|
|
|
|
|
|
string next_token();
|
|
|
|
|
2019-12-23 20:18:26 +00:00
|
|
|
char snext(int step=0) { skip_white(); return next(step); }
|
|
|
|
|
2022-04-24 18:53:08 +00:00
|
|
|
vector<pair<ld, ld>> parse_with_reps();
|
|
|
|
|
2019-08-09 19:18:13 +00:00
|
|
|
cld parse(int prio = 0);
|
|
|
|
|
2020-05-01 15:23:29 +00:00
|
|
|
ld rparse(int prio = 0) { return validate_real(parse(prio)); }
|
2019-12-23 20:18:35 +00:00
|
|
|
int iparse(int prio = 0) { return int(floor(rparse(prio) + .5)); }
|
|
|
|
|
2019-08-09 19:18:13 +00:00
|
|
|
cld parsepar() {
|
|
|
|
cld res = parse();
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat(")");
|
2019-08-09 19:18:13 +00:00
|
|
|
return res;
|
|
|
|
}
|
2020-05-01 15:23:29 +00:00
|
|
|
|
|
|
|
ld validate_real(cld x) {
|
|
|
|
if(kz(imag(x))) throw hr_parse_exception("expected real number but " + lalign(-1, x) + " found at " + where());
|
|
|
|
return real(x);
|
|
|
|
}
|
2019-12-23 20:44:51 +00:00
|
|
|
|
|
|
|
void force_eat(const char *c) {
|
|
|
|
skip_white();
|
2020-04-16 18:59:34 +00:00
|
|
|
if(!eat(c)) throw hr_parse_exception("expected: " + string(c) + " at " + where());
|
2019-12-23 20:44:51 +00:00
|
|
|
}
|
2019-08-09 19:18:13 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-12-23 20:18:26 +00:00
|
|
|
void exp_parser::skip_white() {
|
2020-04-16 18:59:34 +00:00
|
|
|
while(next() == ' ' || next() == '\n' || next() == '\r' || next() == '\t') {
|
|
|
|
if(next() == '\r') last_line++;
|
|
|
|
if(next() == '\n') {
|
|
|
|
line_number++, last_line = at;
|
|
|
|
}
|
|
|
|
at++;
|
|
|
|
}
|
2019-12-23 20:18:26 +00:00
|
|
|
}
|
2019-12-23 20:49:17 +00:00
|
|
|
|
|
|
|
string exp_parser::next_token() {
|
|
|
|
skip_white();
|
|
|
|
string token;
|
|
|
|
while(true) {
|
|
|
|
char c = next();
|
|
|
|
if((c >= '0' && c <= '9') || (c == '.' && next(1) != '.') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')
|
|
|
|
token += c, at++;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2022-04-24 18:53:08 +00:00
|
|
|
vector<pair<ld, ld>> exp_parser::parse_with_reps() {
|
|
|
|
vector<pair<ld, ld>> vals;
|
|
|
|
vals.emplace_back(rparse(0), 1);
|
|
|
|
while(true) {
|
|
|
|
skip_white();
|
|
|
|
if(eat(":^")) {
|
|
|
|
ld rep = rparse(0);
|
|
|
|
vals.back().second *= rep;
|
|
|
|
}
|
|
|
|
if(eat(",")) vals.emplace_back(rparse(0), 1);
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
return vals;
|
|
|
|
}
|
|
|
|
|
2018-11-06 23:51:41 +00:00
|
|
|
cld exp_parser::parse(int prio) {
|
|
|
|
cld res;
|
2019-12-23 20:18:26 +00:00
|
|
|
skip_white();
|
2018-10-23 15:05:46 +00:00
|
|
|
if(eat("sin(")) res = sin(parsepar());
|
|
|
|
else if(eat("cos(")) res = cos(parsepar());
|
|
|
|
else if(eat("sinh(")) res = sinh(parsepar());
|
|
|
|
else if(eat("cosh(")) res = cosh(parsepar());
|
|
|
|
else if(eat("asin(")) res = asin(parsepar());
|
|
|
|
else if(eat("acos(")) res = acos(parsepar());
|
|
|
|
else if(eat("asinh(")) res = asinh(parsepar());
|
|
|
|
else if(eat("acosh(")) res = acosh(parsepar());
|
|
|
|
else if(eat("exp(")) res = exp(parsepar());
|
2019-12-23 20:49:37 +00:00
|
|
|
else if(eat("sqrt(")) res = sqrt(parsepar());
|
2018-10-23 15:05:46 +00:00
|
|
|
else if(eat("log(")) res = log(parsepar());
|
|
|
|
else if(eat("tan(")) res = tan(parsepar());
|
|
|
|
else if(eat("tanh(")) res = tanh(parsepar());
|
|
|
|
else if(eat("atan(")) res = atan(parsepar());
|
|
|
|
else if(eat("atanh(")) res = atanh(parsepar());
|
2018-11-06 23:51:41 +00:00
|
|
|
else if(eat("abs(")) res = abs(parsepar());
|
|
|
|
else if(eat("re(")) res = real(parsepar());
|
|
|
|
else if(eat("im(")) res = imag(parsepar());
|
|
|
|
else if(eat("conj(")) res = std::conj(parsepar());
|
2020-05-01 15:23:29 +00:00
|
|
|
else if(eat("floor(")) res = floor(validate_real(parsepar()));
|
|
|
|
else if(eat("frac(")) { res = parsepar(); res = res - floor(validate_real(res)); }
|
2019-05-04 16:28:03 +00:00
|
|
|
else if(eat("to01(")) { res = parsepar(); return atan(res) / ld(M_PI) + ld(0.5); }
|
2022-04-21 10:49:23 +00:00
|
|
|
else if(eat("min(")) {
|
|
|
|
ld a = rparse(0);
|
|
|
|
while(skip_white(), eat(",")) a = min(a, rparse(0));
|
|
|
|
force_eat(")");
|
|
|
|
res = a;
|
|
|
|
}
|
|
|
|
else if(eat("max(")) {
|
|
|
|
ld a = rparse(0);
|
|
|
|
while(skip_white(), eat(",")) a = max(a, rparse(0));
|
|
|
|
force_eat(")");
|
|
|
|
res = a;
|
|
|
|
}
|
2019-12-26 21:29:29 +00:00
|
|
|
else if(eat("edge(")) {
|
2020-03-22 10:27:59 +00:00
|
|
|
ld a = rparse(0);
|
2019-12-26 21:29:29 +00:00
|
|
|
force_eat(",");
|
2020-03-22 10:27:59 +00:00
|
|
|
ld b = rparse(0);
|
2019-12-26 21:29:29 +00:00
|
|
|
force_eat(")");
|
2022-11-12 21:38:45 +00:00
|
|
|
res = edge_of_triangle_with_angles(TAU/a, M_PI/b, M_PI/b);
|
2019-12-26 21:29:29 +00:00
|
|
|
}
|
2020-05-31 01:26:27 +00:00
|
|
|
else if(eat("edge_angles(")) {
|
|
|
|
cld a = rparse(0);
|
|
|
|
force_eat(",");
|
|
|
|
cld b = rparse(0);
|
|
|
|
force_eat(",");
|
|
|
|
cld c = rparse(0);
|
|
|
|
force_eat(")");
|
|
|
|
|
2021-07-18 21:33:25 +00:00
|
|
|
if (auto *angleunit = hr::at_or_null(extra_params, "angleunit")) {
|
|
|
|
a *= *angleunit;
|
|
|
|
b *= *angleunit;
|
|
|
|
c *= *angleunit;
|
2020-05-31 01:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return edge_of_triangle_with_angles(real(a), real(b), real(c));
|
|
|
|
}
|
2019-12-26 21:30:48 +00:00
|
|
|
else if(eat("regradius(")) {
|
2020-03-22 10:27:59 +00:00
|
|
|
ld a = rparse(0);
|
2019-12-26 21:30:48 +00:00
|
|
|
force_eat(",");
|
2020-03-22 10:27:59 +00:00
|
|
|
ld b = rparse(0);
|
2019-12-26 21:30:48 +00:00
|
|
|
force_eat(")");
|
2022-11-12 21:38:45 +00:00
|
|
|
res = edge_of_triangle_with_angles(90._deg, M_PI/a, M_PI/b);
|
2020-03-22 10:27:06 +00:00
|
|
|
}
|
2020-10-15 14:33:52 +00:00
|
|
|
#if CAP_ARCM
|
2020-03-22 10:27:06 +00:00
|
|
|
else if(eat("arcmedge(")) {
|
2022-04-24 18:53:08 +00:00
|
|
|
vector<pair<ld, ld>> vals = parse_with_reps();
|
2020-03-22 10:27:06 +00:00
|
|
|
force_eat(")");
|
2022-04-24 18:53:08 +00:00
|
|
|
res = euclid ? 1 : arcm::compute_edgelength(vals);
|
2021-07-18 21:33:25 +00:00
|
|
|
if (auto *distunit = hr::at_or_null(extra_params, "distunit"))
|
|
|
|
res /= *distunit;
|
2020-03-22 10:27:06 +00:00
|
|
|
}
|
2022-04-24 18:53:08 +00:00
|
|
|
else if(eat("arcmcurv(")) {
|
|
|
|
vector<pair<ld, ld>> vals = parse_with_reps();
|
|
|
|
force_eat(")");
|
|
|
|
ld total = 0;
|
|
|
|
for(auto p: vals) total += p.second * (180 - 360 / p.first);
|
|
|
|
total = (360 - total) * degree;
|
|
|
|
if(abs(total) < 1e-10) total = 0;
|
|
|
|
res = total;
|
|
|
|
}
|
2020-10-15 14:33:52 +00:00
|
|
|
#endif
|
2022-04-10 11:15:05 +00:00
|
|
|
else if(eat("ideal_angle(")) {
|
|
|
|
ld edges = rparse(0);
|
2022-04-25 17:53:36 +00:00
|
|
|
ld u = 1;
|
|
|
|
skip_white(); if(eat(",")) u = rparse(0);
|
2022-04-10 11:15:05 +00:00
|
|
|
force_eat(")");
|
2022-04-25 17:53:36 +00:00
|
|
|
return arb::rep_ideal(edges, u).second;
|
2022-04-10 11:15:05 +00:00
|
|
|
}
|
|
|
|
else if(eat("ideal_edge(")) {
|
|
|
|
ld edges = rparse(0);
|
2022-04-25 17:53:36 +00:00
|
|
|
ld u = 1;
|
|
|
|
skip_white(); if(eat(",")) u = rparse(0);
|
2022-04-10 11:15:05 +00:00
|
|
|
force_eat(")");
|
2022-04-25 17:53:36 +00:00
|
|
|
return arb::rep_ideal(edges, u).first;
|
2022-04-10 11:15:05 +00:00
|
|
|
}
|
2020-03-22 10:27:06 +00:00
|
|
|
else if(eat("regangle(")) {
|
2020-05-01 15:23:29 +00:00
|
|
|
cld edgelen = parse(0);
|
2021-07-18 21:33:25 +00:00
|
|
|
if (auto *distunit = hr::at_or_null(extra_params, "distunit")) {
|
|
|
|
edgelen *= *distunit;
|
2020-03-22 10:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
force_eat(",");
|
2020-05-01 15:23:29 +00:00
|
|
|
ld edges = rparse(0);
|
2020-03-22 10:27:06 +00:00
|
|
|
force_eat(")");
|
|
|
|
ld alpha = M_PI / edges;
|
2022-04-22 22:39:44 +00:00
|
|
|
if(isinf(edges)) {
|
|
|
|
ld u = sqrt(cosh(validate_real(edgelen)) * 2 - 2);
|
|
|
|
ld a = atan2(1, u/2);
|
|
|
|
res = 2 * a;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ld c = asin_auto(sin_auto(validate_real(edgelen)/2) / sin(alpha));
|
|
|
|
hyperpoint h = xpush(c) * spin(M_PI - 2*alpha) * xpush0(c);
|
|
|
|
ld result = 2 * atan2(h);
|
|
|
|
if(result < 0) result = -result;
|
2022-11-12 21:38:45 +00:00
|
|
|
cyclefix(result, 0);
|
2022-04-22 22:39:44 +00:00
|
|
|
res = result;
|
|
|
|
}
|
2020-03-22 10:27:06 +00:00
|
|
|
|
2021-07-18 21:33:25 +00:00
|
|
|
if (auto *angleunit = hr::at_or_null(extra_params, "angleunit"))
|
|
|
|
res /= *angleunit;
|
2020-03-22 10:27:06 +00:00
|
|
|
}
|
|
|
|
else if(eat("test(")) {
|
|
|
|
res = parsepar();
|
2020-05-31 01:26:39 +00:00
|
|
|
println(hlog, "res = ", res, ": ", fts(real(res), 10), ",", fts(imag(res), 10));
|
2019-12-26 21:30:48 +00:00
|
|
|
}
|
2019-01-28 20:42:42 +00:00
|
|
|
else if(eat("ifp(")) {
|
|
|
|
cld cond = parse(0);
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat(",");
|
2019-01-28 20:42:42 +00:00
|
|
|
cld yes = parse(0);
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat(",");
|
2019-01-28 20:42:42 +00:00
|
|
|
cld no = parsepar();
|
2020-03-22 10:27:59 +00:00
|
|
|
res = real(cond) > 0 ? yes : no;
|
2019-01-28 20:42:42 +00:00
|
|
|
}
|
2022-04-24 20:10:43 +00:00
|
|
|
else if(eat("ifz(")) {
|
|
|
|
cld cond = parse(0);
|
|
|
|
force_eat(",");
|
|
|
|
cld yes = parse(0);
|
|
|
|
force_eat(",");
|
|
|
|
cld no = parsepar();
|
|
|
|
res = abs(cond) < 1e-8 ? yes : no;
|
|
|
|
}
|
2019-11-15 12:37:41 +00:00
|
|
|
else if(eat("wallif(")) {
|
|
|
|
cld val0 = parse(0);
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat(",");
|
2019-11-15 12:37:41 +00:00
|
|
|
cld val1 = parsepar();
|
2020-03-22 10:27:59 +00:00
|
|
|
if(real(extra_params["p"]) >= 3.5) res = val0;
|
|
|
|
else res = val1;
|
2019-11-15 12:37:41 +00:00
|
|
|
}
|
2019-01-28 20:43:13 +00:00
|
|
|
else if(eat("rgb(")) {
|
|
|
|
cld val0 = parse(0);
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat(",");
|
2019-01-28 20:43:13 +00:00
|
|
|
cld val1 = parse(0);
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat(",");
|
2019-01-28 20:43:13 +00:00
|
|
|
cld val2 = parsepar();
|
|
|
|
switch(int(real(extra_params["p"]) + .5)) {
|
2020-03-22 10:27:59 +00:00
|
|
|
case 1: res = val0; break;
|
|
|
|
case 2: res = val1; break;
|
|
|
|
case 3: res = val2; break;
|
|
|
|
default: res = 0;
|
2019-01-28 20:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 23:51:41 +00:00
|
|
|
else if(eat("let(")) {
|
2019-12-23 20:49:17 +00:00
|
|
|
string name = next_token();
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat("=");
|
2018-11-06 23:51:41 +00:00
|
|
|
cld val = parse(0);
|
2019-12-23 20:44:51 +00:00
|
|
|
force_eat(",");
|
2018-11-06 23:51:41 +00:00
|
|
|
dynamicval<cld> d(extra_params[name], val);
|
2020-03-22 10:27:59 +00:00
|
|
|
res = parsepar();
|
2018-11-06 23:51:41 +00:00
|
|
|
}
|
2019-11-15 12:37:57 +00:00
|
|
|
#if CAP_TEXTURE
|
|
|
|
else if(eat("txp(")) {
|
|
|
|
cld val = parsepar();
|
2020-03-22 10:27:59 +00:00
|
|
|
res = texture::get_txp(real(val), imag(val), int(real(extra_params["p"]) + .5)-1);
|
2019-11-15 12:37:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-11-06 23:51:41 +00:00
|
|
|
else if(next() == '(') at++, res = parsepar();
|
2018-10-23 15:05:46 +00:00
|
|
|
else {
|
2019-12-23 20:49:17 +00:00
|
|
|
string number = next_token();
|
2021-07-18 21:33:25 +00:00
|
|
|
if (auto *p = hr::at_or_null(extra_params, number)) res = *p;
|
|
|
|
else if (auto *p = hr::at_or_null(params, number)) res = (*p)->get_cld();
|
2020-04-29 13:13:09 +00:00
|
|
|
else if(number == "e") res = exp(1);
|
2018-11-06 23:51:41 +00:00
|
|
|
else if(number == "i") res = cld(0, 1);
|
2022-04-21 20:09:04 +00:00
|
|
|
else if(number == "inf") res = HUGE_VAL;
|
2018-10-23 15:05:46 +00:00
|
|
|
else if(number == "p" || number == "pi") res = M_PI;
|
2021-12-09 19:53:52 +00:00
|
|
|
else if(number == "" && next() == '-') { at++; res = -parse(20); }
|
2020-06-06 16:42:21 +00:00
|
|
|
else if(number == "") throw hr_parse_exception("number missing, " + where());
|
2018-11-07 06:21:57 +00:00
|
|
|
else if(number == "s") res = ticks / 1000.;
|
|
|
|
else if(number == "ms") res = ticks;
|
2019-01-18 20:04:29 +00:00
|
|
|
else if(number[0] == '0' && number[1] == 'x') res = strtoll(number.c_str()+2, NULL, 16);
|
2018-11-10 19:18:57 +00:00
|
|
|
else if(number == "mousex") res = mousex;
|
2019-12-23 20:18:45 +00:00
|
|
|
else if(number == "deg") res = degree;
|
2020-05-27 23:51:46 +00:00
|
|
|
else if(number == "ultra_mirror_dist") res = cgi.ultra_mirror_dist;
|
2020-07-24 00:30:50 +00:00
|
|
|
else if(number == "psl_steps") res = cgi.psl_steps;
|
|
|
|
else if(number == "single_step") res = cgi.single_step;
|
2020-05-27 23:51:46 +00:00
|
|
|
else if(number == "step") res = hdist0(tC0(currentmap->adj(cwt.at, 0)));
|
2023-03-16 13:42:10 +00:00
|
|
|
else if(number == "edgelen") { start_game(); res = hdist(get_corner_position(cwt.at, 0), get_corner_position(cwt.at, 1)); }
|
2018-11-10 19:18:57 +00:00
|
|
|
else if(number == "mousey") res = mousey;
|
2019-11-16 00:39:31 +00:00
|
|
|
else if(number == "random") res = randd();
|
2018-11-17 18:24:02 +00:00
|
|
|
else if(number == "mousez") res = cld(mousex - current_display->xcenter, mousey - current_display->ycenter) / cld(current_display->radius, 0);
|
2019-04-07 01:09:52 +00:00
|
|
|
else if(number == "shot") res = inHighQual ? 1 : 0;
|
2022-05-06 10:40:48 +00:00
|
|
|
#if CAP_ARCM
|
2021-08-08 16:29:49 +00:00
|
|
|
else if(number == "fake_edgelength") res = arcm::fake_current.edgelength;
|
2022-05-06 10:40:48 +00:00
|
|
|
#endif
|
2022-04-24 20:28:32 +00:00
|
|
|
else if(number == "MAX_EDGE") res = FULL_EDGE;
|
|
|
|
else if(number == "MAX_VALENCE") res = 120;
|
2019-12-23 20:44:51 +00:00
|
|
|
else if(number[0] >= 'a' && number[0] <= 'z') throw hr_parse_exception("unknown value: " + number);
|
2021-07-31 09:56:31 +00:00
|
|
|
else if(number[0] >= 'A' && number[0] <= 'Z') throw hr_parse_exception("unknown value: " + number);
|
|
|
|
else if(number[0] == '_') throw hr_parse_exception("unknown value: " + number);
|
2018-10-23 15:05:46 +00:00
|
|
|
else { std::stringstream ss; res = 0; ss << number; ss >> res; }
|
2018-09-10 17:28:12 +00:00
|
|
|
}
|
2018-10-23 15:05:46 +00:00
|
|
|
while(true) {
|
2020-04-29 13:13:44 +00:00
|
|
|
skip_white();
|
2019-02-17 17:43:39 +00:00
|
|
|
#if CAP_ANIMATIONS
|
2018-11-09 19:41:55 +00:00
|
|
|
if(next() == '.' && next(1) == '.' && prio == 0) {
|
2020-04-09 06:09:11 +00:00
|
|
|
static const cld NO_DERIVATIVE(3.1, 2.5);
|
|
|
|
vector<array<cld, 4>> rest = { make_array(res, NO_DERIVATIVE, res, NO_DERIVATIVE) };
|
|
|
|
bool second = true;
|
2018-11-09 19:41:55 +00:00
|
|
|
while(next() == '.' && next(1) == '.') {
|
2020-04-09 06:09:11 +00:00
|
|
|
/* spline interpolation */
|
|
|
|
if(next(2) == '/') {
|
2020-04-09 05:46:39 +00:00
|
|
|
at += 3;
|
2020-04-09 06:09:11 +00:00
|
|
|
rest.back()[second ? 3 : 1] = parse(10);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* sharp end */
|
|
|
|
else if(next(2) == '|') {
|
|
|
|
at += 3;
|
|
|
|
rest.back()[2] = parse(10);
|
|
|
|
rest.back()[3] = NO_DERIVATIVE;
|
|
|
|
second = true;
|
2020-05-15 09:53:30 +00:00
|
|
|
continue;
|
2020-04-09 05:46:39 +00:00
|
|
|
}
|
|
|
|
at += 2;
|
|
|
|
auto val = parse(10);
|
2020-04-09 06:09:11 +00:00
|
|
|
rest.emplace_back(make_array(val, NO_DERIVATIVE, val, NO_DERIVATIVE));
|
|
|
|
second = false;
|
2018-11-09 19:41:55 +00:00
|
|
|
}
|
|
|
|
ld v = ticks * (isize(rest)-1.) / anims::period;
|
|
|
|
int vf = v;
|
|
|
|
v -= vf;
|
2021-02-01 12:42:24 +00:00
|
|
|
if(isize(rest) == 1) rest.push_back(rest[0]);
|
2018-11-09 19:41:55 +00:00
|
|
|
vf %= (isize(rest)-1);
|
2020-04-09 06:09:11 +00:00
|
|
|
auto& lft = rest[vf];
|
|
|
|
auto& rgt = rest[vf+1];
|
|
|
|
if(lft[3] == NO_DERIVATIVE && rgt[1] == NO_DERIVATIVE)
|
|
|
|
res = lerp(lft[2], rgt[0], v);
|
|
|
|
else if(rgt[1] == NO_DERIVATIVE)
|
|
|
|
res = lerp(lft[2] + lft[3] * v, rgt[0], v*v);
|
|
|
|
else if(lft[3] == NO_DERIVATIVE)
|
|
|
|
res = lerp(lft[2], rgt[0] + rgt[1] * (v-1), (2-v)*v);
|
|
|
|
else {
|
|
|
|
res = lerp(lft[2] + lft[3] * v, rgt[0] + rgt[1] * (v-1), v*v*(3-2*v));
|
|
|
|
}
|
2018-11-09 19:41:55 +00:00
|
|
|
return res;
|
|
|
|
}
|
2019-02-17 17:43:39 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if(next() == '+' && prio <= 10) at++, res = res + parse(20);
|
2018-11-09 19:41:55 +00:00
|
|
|
else if(next() == '-' && prio <= 10) at++, res = res - parse(20);
|
|
|
|
else if(next() == '*' && prio <= 20) at++, res = res * parse(30);
|
|
|
|
else if(next() == '/' && prio <= 20) at++, res = res / parse(30);
|
|
|
|
else if(next() == '^') at++, res = pow(res, parse(40));
|
2018-10-23 15:05:46 +00:00
|
|
|
else break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-09-10 17:28:12 +00:00
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX ld parseld(const string& s) {
|
2018-09-10 17:28:12 +00:00
|
|
|
exp_parser ep;
|
|
|
|
ep.s = s;
|
2021-02-04 13:58:47 +00:00
|
|
|
return ep.rparse();
|
|
|
|
}
|
|
|
|
|
|
|
|
EX int parseint(const string& s) {
|
|
|
|
exp_parser ep;
|
|
|
|
ep.s = s;
|
|
|
|
return ep.iparse();
|
2018-09-10 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:42:12 +00:00
|
|
|
EX string available_functions() {
|
|
|
|
return
|
|
|
|
"(a)sin(h), (a)cos(h), (a)tan(h), exp, log, abs, re, im, conj, let(t=...,...t...), floor, frac, sqrt, to01, random, edge(7,3), regradius(7,3), ifp(a,v,w) [if positive]";
|
|
|
|
}
|
|
|
|
|
|
|
|
EX string available_constants() {
|
|
|
|
return
|
|
|
|
"e, i, pi, s, ms, mousex, mousey, mousez, shot [1 if taking screenshot/animation]";
|
2018-11-07 00:03:27 +00:00
|
|
|
}
|
2018-11-24 16:01:49 +00:00
|
|
|
|
2019-12-25 10:47:11 +00:00
|
|
|
#if HDR
|
|
|
|
struct bignum {
|
|
|
|
static const int BASE = 1000000000;
|
|
|
|
static const long long BASE2 = BASE * (long long)BASE;
|
|
|
|
vector<int> digits;
|
|
|
|
bignum() {}
|
|
|
|
bignum(int i) : digits() { digits.push_back(i); }
|
|
|
|
void be(int i) { digits.resize(1); digits[0] = i; }
|
|
|
|
bignum& operator +=(const bignum& b);
|
|
|
|
void addmul(const bignum& b, int factor);
|
|
|
|
string get_str(int max_length) const;
|
2019-12-25 10:51:56 +00:00
|
|
|
bignum(ld d);
|
2019-12-25 10:47:11 +00:00
|
|
|
|
|
|
|
bool operator < (const bignum&) const;
|
2019-12-25 11:17:24 +00:00
|
|
|
bool operator > (const bignum& b) const { return b < self; }
|
2019-12-25 10:47:11 +00:00
|
|
|
|
|
|
|
ld leading() const {
|
|
|
|
switch(isize(digits)) {
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
return digits.back();
|
|
|
|
default:
|
|
|
|
return digits.back() + ld(digits[isize(digits)-2]) / BASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ld approx() const {
|
|
|
|
return leading() * pow(BASE, isize(digits) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ld log_approx() const {
|
|
|
|
return log(leading()) * log(BASE) * (isize(digits) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ld approx_div(const bignum& b) const {
|
|
|
|
return leading() / b.leading() * pow(BASE, isize(digits) - isize(b.digits));
|
|
|
|
}
|
|
|
|
|
|
|
|
int approx_int() const {
|
|
|
|
if(isize(digits) > 1) return BASE;
|
|
|
|
if(digits.empty()) return 0;
|
|
|
|
return digits[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nonzero() { return approx_ld() != 0; }
|
|
|
|
|
|
|
|
bignum randomized_div(int x) const;
|
|
|
|
|
|
|
|
ld approx_ld() const {
|
|
|
|
ld res = 0;
|
|
|
|
for(int i=0; i<isize(digits); i++) res += digits[i] * pow(BASE, i);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long approx_ll() const {
|
|
|
|
if(isize(digits) > 2) return BASE2;
|
|
|
|
if(digits.empty()) return 0;
|
|
|
|
if(isize(digits) == 1) return digits[0];
|
|
|
|
return digits[0] + digits[1] * (long long) BASE;
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:53:02 +00:00
|
|
|
#if CAP_GMP
|
|
|
|
mpq_class as_mpq() const {
|
|
|
|
string s = get_str(999999);
|
|
|
|
string t;
|
|
|
|
for(char c: s) if(c != ' ') t += c;
|
|
|
|
return mpq_class(t);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-25 10:47:11 +00:00
|
|
|
friend inline bignum operator +(bignum a, const bignum& b) { a.addmul(b, 1); return a; }
|
|
|
|
friend inline bignum operator -(bignum a, const bignum& b) { a.addmul(b, -1); return a; }
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bignum& bignum::operator +=(const bignum& b) {
|
|
|
|
int K = isize(b.digits);
|
|
|
|
if(K > isize(digits)) digits.resize(K);
|
|
|
|
int carry = 0;
|
|
|
|
for(int i=0; i<K || carry; i++) {
|
|
|
|
if(i >= isize(digits)) digits.push_back(0);
|
|
|
|
digits[i] += carry;
|
|
|
|
if(i < K) digits[i] += b.digits[i];
|
|
|
|
if(digits[i] >= BASE) {
|
|
|
|
digits[i] -= BASE;
|
|
|
|
carry = 1;
|
|
|
|
}
|
|
|
|
else carry = 0;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bignum::operator < (const bignum& b) const {
|
|
|
|
if(isize(digits) != isize(b.digits))
|
|
|
|
return isize(digits) < isize(b.digits);
|
|
|
|
for(int i = isize(digits)-1; i>=0; i--)
|
|
|
|
if(digits[i] != b.digits[i])
|
|
|
|
return digits[i] < b.digits[i];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bignum bignum::randomized_div(int x) const {
|
|
|
|
bignum res = self;
|
|
|
|
long long carry = 0;
|
|
|
|
int K = isize(res.digits);
|
|
|
|
for(int i=K-1; i>=0; i--) {
|
|
|
|
carry *= BASE;
|
|
|
|
carry += digits[i];
|
2019-12-27 13:10:02 +00:00
|
|
|
// strange compiler buug:
|
|
|
|
// if I do / and %, function 'divmod' is called, and it complains on launch that divmod is unimplemented
|
2020-04-06 06:39:31 +00:00
|
|
|
res.digits[i] = int(carry / x);
|
2020-01-02 15:57:45 +00:00
|
|
|
carry -= res.digits[i] * (long long)(x);
|
2019-12-25 10:47:11 +00:00
|
|
|
}
|
|
|
|
while(isize(res.digits) && res.digits.back() == 0) res.digits.pop_back();
|
|
|
|
if(rand() % x < carry) res += 1;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bignum::addmul(const bignum& b, int factor) {
|
|
|
|
int K = isize(b.digits);
|
|
|
|
if(K > isize(digits)) digits.resize(K);
|
|
|
|
int carry = 0;
|
|
|
|
for(int i=0; i<K || (carry > 0 || carry < -1) || (carry == -1 && i < isize(digits)); i++) {
|
|
|
|
if(i >= isize(digits)) digits.push_back(0);
|
|
|
|
long long l = digits[i];
|
|
|
|
l += carry;
|
|
|
|
if(i < K) l += b.digits[i] * factor;
|
|
|
|
carry = 0;
|
2020-04-06 06:39:31 +00:00
|
|
|
if(l >= BASE) carry = int(l / BASE);
|
|
|
|
if(l < 0) carry = -int((BASE-1-l) / BASE);
|
2019-12-25 10:47:11 +00:00
|
|
|
l -= carry * BASE;
|
2020-04-06 06:39:31 +00:00
|
|
|
digits[i] = int(l);
|
2019-12-25 10:47:11 +00:00
|
|
|
}
|
|
|
|
if(carry < 0) digits.back() -= BASE;
|
|
|
|
while(isize(digits) && digits.back() == 0) digits.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
EX bignum hrand(bignum b) {
|
|
|
|
bignum res;
|
|
|
|
int d = isize(b.digits);
|
|
|
|
while(true) {
|
|
|
|
res.digits.resize(d);
|
|
|
|
for(int i=0; i<d-1; i++) res.digits[i] = hrand(bignum::BASE);
|
|
|
|
res.digits.back() = hrand(b.digits.back() + 1);
|
|
|
|
if(res < b) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EX void operator ++(bignum &b, int) {
|
|
|
|
int i = 0;
|
|
|
|
while(true) {
|
|
|
|
if(isize(b.digits) == i) { b.digits.push_back(1); break; }
|
|
|
|
else if(b.digits[i] == bignum::BASE-1) {
|
|
|
|
b.digits[i] = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
b.digits[i]++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EX void operator --(bignum &b, int) {
|
|
|
|
int i = 0;
|
|
|
|
while(true) {
|
|
|
|
if(isize(b.digits) == i) { b.digits.push_back(bignum::BASE-1); break; }
|
|
|
|
else if(b.digits[i] == 0) {
|
|
|
|
b.digits[i] = bignum::BASE-1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
b.digits[i]--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string bignum::get_str(int max_length) const {
|
|
|
|
if(digits.empty()) return "0";
|
|
|
|
string ret = its(digits.back());
|
|
|
|
for(int i=isize(digits)-2; i>=0; i--) {
|
|
|
|
if(isize(ret) > max_length && i) {
|
|
|
|
ret += XLAT(" (%1 more digits)", its(9 * (i+1)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += " ";
|
|
|
|
string val = its(digits[i]);
|
|
|
|
while(isize(val) < 9) val = "0" + val;
|
|
|
|
ret += val;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-01 01:35:19 +00:00
|
|
|
EX string short_form(bignum b) {
|
|
|
|
if(b < 0) return "-" + short_form(0-b);
|
|
|
|
else if(b < 100000) return its(b.approx_int());
|
|
|
|
else {
|
|
|
|
long long val;
|
|
|
|
int q;
|
|
|
|
if(isize(b.digits) >= 2) {
|
|
|
|
q = max(isize(b.digits) - 2, 0);
|
|
|
|
val = b.digits[q] + (long long)(bignum::BASE) * b.digits[q+1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
q = 0;
|
|
|
|
val = b.digits[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
int digits = q * 9;
|
|
|
|
while(val >= 1000) { val /= 10; digits++; }
|
2020-04-06 06:39:31 +00:00
|
|
|
string str = its(int(val)) + "E" + its(digits + 2);
|
2020-03-01 01:35:19 +00:00
|
|
|
str.insert(1, ".");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 10:51:56 +00:00
|
|
|
bignum::bignum(ld d) {
|
|
|
|
if(d == 0) return;
|
|
|
|
int n = 1;
|
|
|
|
while(d > BASE) d /= BASE, n++;
|
|
|
|
digits.resize(n);
|
|
|
|
n--;
|
|
|
|
while(n >= 0) { digits[n] = int(d); d -= digits[n]; d *= BASE; n--; }
|
|
|
|
}
|
|
|
|
|
2021-02-04 16:21:57 +00:00
|
|
|
/** in s, replace occurences of a with b */
|
|
|
|
EX void replace_str(string& s, string a, string b) {
|
|
|
|
while(s.find(a) != string::npos)
|
|
|
|
s.replace(s.find(a), isize(a), b);
|
|
|
|
}
|
|
|
|
|
2020-02-15 13:10:49 +00:00
|
|
|
#if CAP_ZLIB
|
2020-01-25 23:30:13 +00:00
|
|
|
/* compression/decompression */
|
|
|
|
|
|
|
|
EX string compress_string(string s) {
|
|
|
|
z_stream strm;
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
println(hlog, "pre init");
|
|
|
|
auto ret = deflateInit(&strm, 9);
|
2021-03-24 19:25:44 +00:00
|
|
|
if(ret != Z_OK) throw hr_exception("z-error");
|
2020-01-25 23:30:13 +00:00
|
|
|
println(hlog, "init ok");
|
|
|
|
strm.avail_in = isize(s);
|
|
|
|
strm.next_in = (Bytef*) &s[0];
|
2021-05-23 13:59:06 +00:00
|
|
|
vector<char> buf(10000000, 0);
|
|
|
|
strm.avail_out = 10000000;
|
2020-01-25 23:30:13 +00:00
|
|
|
strm.next_out = (Bytef*) &buf[0];
|
2021-03-24 19:25:44 +00:00
|
|
|
if(deflate(&strm, Z_FINISH) != Z_STREAM_END) throw hr_exception("z-error-2");
|
2020-01-25 23:30:13 +00:00
|
|
|
println(hlog, "deflate ok");
|
|
|
|
string out(&buf[0], (char*)(strm.next_out) - &buf[0]);
|
2022-08-06 23:57:06 +00:00
|
|
|
deflateEnd(&strm);
|
2020-01-25 23:30:13 +00:00
|
|
|
println(hlog, isize(s), " -> ", isize(out));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
EX string decompress_string(string s) {
|
|
|
|
z_stream strm;
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
auto ret = inflateInit(&strm);
|
2021-03-24 19:25:44 +00:00
|
|
|
if(ret != Z_OK) throw hr_exception("z-error");
|
2020-01-25 23:30:13 +00:00
|
|
|
strm.avail_in = isize(s);
|
|
|
|
strm.next_in = (Bytef*) &s[0];
|
2021-05-23 13:59:06 +00:00
|
|
|
vector<char> buf(10000000, 0);
|
|
|
|
strm.avail_out = 10000000;
|
2020-01-25 23:30:13 +00:00
|
|
|
strm.next_out = (Bytef*) &buf[0];
|
2022-08-06 23:57:06 +00:00
|
|
|
if(inflate(&strm, Z_FINISH) != Z_STREAM_END) throw hr_exception("z-error-2");
|
2020-01-25 23:30:13 +00:00
|
|
|
string out(&buf[0], (char*)(strm.next_out) - &buf[0]);
|
2022-08-06 23:57:06 +00:00
|
|
|
inflateEnd(&strm);
|
2020-01-25 23:30:13 +00:00
|
|
|
println(hlog, isize(s), " -> ", isize(out));
|
|
|
|
return out;
|
|
|
|
}
|
2020-02-15 13:10:49 +00:00
|
|
|
#endif
|
2020-01-25 23:30:13 +00:00
|
|
|
|
2021-03-31 10:14:23 +00:00
|
|
|
EX bool file_exists(string fname) {
|
|
|
|
return access(fname.c_str(), F_OK) != -1;
|
|
|
|
}
|
|
|
|
|
2022-08-26 10:23:58 +00:00
|
|
|
/** find a file named s, possibly in HYPERPATH */
|
|
|
|
EX string find_file(string s) {
|
|
|
|
string s1;
|
|
|
|
if(file_exists(s)) return s;
|
|
|
|
char *p = getenv("HYPERPATH");
|
|
|
|
if(p && file_exists(s1 = s0 + p + s)) return s1;
|
|
|
|
if(file_exists(s1 = HYPERPATH + s)) return s1;
|
|
|
|
#ifdef FHS
|
|
|
|
if(file_exists(s1 = "/usr/share/hyperrogue/" + s)) return s1;
|
|
|
|
#endif
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-03-31 18:11:27 +00:00
|
|
|
EX void open_url(string s) {
|
|
|
|
#if ISWEB
|
|
|
|
EM_ASM_({
|
|
|
|
window.open(UTF8ToString($0, 1000));
|
|
|
|
}, s.c_str());
|
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef WINDOWS
|
|
|
|
ShellExecute(0, 0, s.c_str(), 0, 0, SW_SHOW);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LINUX
|
2021-04-07 18:00:49 +00:00
|
|
|
ignore(system(("xdg-open "+s).c_str()));
|
2021-03-31 18:11:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MAC
|
2021-04-07 18:00:49 +00:00
|
|
|
ignore(system(("open "+s).c_str()));
|
2021-03-31 18:11:27 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-04 02:38:20 +00:00
|
|
|
const char *urlhex = "0123456789ABCDEF";
|
|
|
|
EX void open_wiki(const char *title) {
|
2022-02-12 22:57:34 +00:00
|
|
|
// Since "Crossroads" is ambiguous, we use the direct link to Crossroads I.
|
|
|
|
if (!strcmp(title, "Crossroads")) {
|
|
|
|
title = "Crossroads (Land)";
|
|
|
|
}
|
|
|
|
|
2021-07-04 02:38:20 +00:00
|
|
|
string url = "https://hyperrogue.miraheze.org/wiki/";
|
|
|
|
unsigned char c;
|
|
|
|
for (size_t i = 0; (c = title[i]); ++i) {
|
|
|
|
if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_' || c == '-')
|
|
|
|
url += c;
|
|
|
|
else if (c == ' ')
|
|
|
|
url += "_";
|
|
|
|
else
|
|
|
|
url += string("%") + urlhex[c/16] + urlhex[c%16];
|
|
|
|
}
|
|
|
|
open_url(url);
|
|
|
|
}
|
|
|
|
|
2022-07-13 19:00:16 +00:00
|
|
|
EX string read_file_as_string(string fname) {
|
|
|
|
string buf;
|
|
|
|
#if ISANDROID || ISIOS
|
|
|
|
buf = get_asset(fname);
|
|
|
|
#else
|
|
|
|
FILE *f = fopen(fname.c_str(), "rb");
|
|
|
|
if(!f) f = fopen((rsrcdir + fname).c_str(), "rb");
|
|
|
|
buf.resize(1000000);
|
|
|
|
int qty = fread(&buf[0], 1, 1000000, f);
|
|
|
|
buf.resize(qty);
|
|
|
|
fclose(f);
|
|
|
|
#endif
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2021-07-11 13:07:40 +00:00
|
|
|
EX void floyd_warshall(vector<vector<char>>& v) {
|
|
|
|
int N = isize(v);
|
|
|
|
for(int k=0; k<N; k++)
|
|
|
|
for(int i=0; i<N; i++)
|
|
|
|
for(int j=0; j<N; j++)
|
|
|
|
v[i][j] = min<int>(v[i][j], v[i][k] + v[k][j]);
|
|
|
|
}
|
2018-06-10 23:58:31 +00:00
|
|
|
}
|