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
|
|
|
|
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;
|
|
|
|
|
2018-06-12 21:10:20 +00:00
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-23 10:53:57 +00:00
|
|
|
// debug utilities
|
|
|
|
|
2017-07-22 23:33:27 +00:00
|
|
|
#if CAP_PROFILING
|
2017-03-23 10:53:57 +00:00
|
|
|
|
|
|
|
#define FRAMES 64
|
|
|
|
#define CATS 16
|
|
|
|
|
|
|
|
long long proftable[16][FRAMES];
|
|
|
|
int pframeid;
|
|
|
|
|
|
|
|
void profile_frame() {
|
|
|
|
pframeid++; pframeid %= FRAMES;
|
|
|
|
for(int t=0; t<16; t++) proftable[t][pframeid] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void profile_start(int t) { proftable[t][pframeid] -= getms(); }
|
|
|
|
void profile_stop(int t) { proftable[t][pframeid] += getms(); }
|
|
|
|
|
|
|
|
void profile_info() {
|
|
|
|
for(int t=0; t<16; t++) {
|
|
|
|
sort(proftable[t], proftable[t]+FRAMES);
|
|
|
|
if(proftable[t][FRAMES-1] == 0) continue;
|
|
|
|
long long sum = 0;
|
|
|
|
for(int f=0; f<FRAMES; f++) sum += proftable[t][f];
|
|
|
|
printf("Category %d: avg = %Ld, %Ld..%Ld..%Ld..%Ld..%Ld\n",
|
|
|
|
t, sum / FRAMES, proftable[t][0], proftable[t][16], proftable[t][32],
|
|
|
|
proftable[t][48], proftable[t][63]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define profile_frame()
|
|
|
|
#define profile_start(t)
|
|
|
|
#define profile_stop(t)
|
|
|
|
#define profile_info()
|
|
|
|
#endif
|
|
|
|
|
2017-12-28 15:46:10 +00:00
|
|
|
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
|
|
|
|
struct exp_parser {
|
|
|
|
string s;
|
|
|
|
int at;
|
|
|
|
exp_parser() { at = 0; }
|
|
|
|
|
|
|
|
map<string, cld> extra_params;
|
|
|
|
|
|
|
|
bool ok() { return at == isize(s); }
|
|
|
|
char next(int step=0) { if(at >= isize(s)-step || at == -1) return 0; else return s[at+step]; }
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
cld parse(int prio = 0);
|
|
|
|
|
|
|
|
cld parsepar() {
|
|
|
|
cld res = parse();
|
|
|
|
if(next() != ')') { at = -1; return res; }
|
|
|
|
at++;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-11-06 23:51:41 +00:00
|
|
|
cld exp_parser::parse(int prio) {
|
|
|
|
cld res;
|
|
|
|
while(next() == ' ') at++;
|
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());
|
|
|
|
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());
|
2018-11-09 20:20:03 +00:00
|
|
|
else if(eat("floor(")) res = floor(real(parsepar()));
|
|
|
|
else if(eat("frac(")) { res = parsepar(); res = res - floor(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); }
|
2019-01-28 20:42:42 +00:00
|
|
|
else if(eat("ifp(")) {
|
|
|
|
cld cond = parse(0);
|
|
|
|
if(next() != ',') {at = -1; return 0; } at++;
|
|
|
|
cld yes = parse(0);
|
|
|
|
if(next() != ',') {at = -1; return 0; } at++;
|
|
|
|
cld no = parsepar();
|
|
|
|
return real(cond) > 0 ? yes : no;
|
|
|
|
}
|
2019-01-28 20:43:13 +00:00
|
|
|
else if(eat("rgb(")) {
|
|
|
|
cld val0 = parse(0);
|
|
|
|
if(next() != ',') {at = -1; return 0; } at++;
|
|
|
|
cld val1 = parse(0);
|
|
|
|
if(next() != ',') {at = -1; return 0; } at++;
|
|
|
|
cld val2 = parsepar();
|
|
|
|
switch(int(real(extra_params["p"]) + .5)) {
|
|
|
|
case 1: return val0;
|
|
|
|
case 2: return val1;
|
|
|
|
case 3: return val2;
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 23:51:41 +00:00
|
|
|
else if(eat("let(")) {
|
|
|
|
string name;
|
|
|
|
while(true) {
|
|
|
|
char c = next();
|
2018-11-09 19:41:55 +00:00
|
|
|
if((c >= '0' && c <= '9') || (c == '.' && next(1) != '.') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')
|
2018-11-06 23:51:41 +00:00
|
|
|
name += c, at++;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
if(next() != '=') { at = -1; return 0; }
|
|
|
|
at++;
|
|
|
|
cld val = parse(0);
|
|
|
|
if(next() != ',') { at = -1; return 0; }
|
|
|
|
at++;
|
|
|
|
dynamicval<cld> d(extra_params[name], val);
|
|
|
|
return parsepar();
|
|
|
|
}
|
|
|
|
else if(next() == '(') at++, res = parsepar();
|
2018-10-23 15:05:46 +00:00
|
|
|
else {
|
|
|
|
string number;
|
2018-09-10 17:28:12 +00:00
|
|
|
while(true) {
|
2018-10-23 15:05:46 +00:00
|
|
|
char c = next();
|
2018-11-09 19:41:55 +00:00
|
|
|
if((c >= '0' && c <= '9') || (c == '.' && next(1) != '.') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')
|
2018-10-23 15:05:46 +00:00
|
|
|
number += c, at++;
|
2018-09-10 17:28:12 +00:00
|
|
|
else break;
|
|
|
|
}
|
2018-10-23 15:05:46 +00:00
|
|
|
if(number == "e") res = exp(1);
|
2018-11-06 23:51:41 +00:00
|
|
|
else if(number == "i") res = cld(0, 1);
|
2018-10-23 15:05:46 +00:00
|
|
|
else if(number == "p" || number == "pi") res = M_PI;
|
2018-11-09 19:41:55 +00:00
|
|
|
else if(number == "" && next() == '-') { at++; res = -parse(prio); }
|
2018-10-23 15:05:46 +00:00
|
|
|
else if(number == "") at = -1;
|
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;
|
|
|
|
else if(number == "mousey") res = mousey;
|
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;
|
2018-11-09 20:18:58 +00:00
|
|
|
else if(extra_params.count(number)) res = extra_params[number];
|
|
|
|
else if(params.count(number)) res = params.at(number);
|
2018-10-23 15:05:46 +00:00
|
|
|
else if(number[0] >= 'a' && number[0] <= 'z') at = -1;
|
|
|
|
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) {
|
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) {
|
|
|
|
vector<cld> rest = { res };
|
|
|
|
while(next() == '.' && next(1) == '.') {
|
|
|
|
at += 2; rest.push_back(parse(10));
|
|
|
|
}
|
|
|
|
ld v = ticks * (isize(rest)-1.) / anims::period;
|
|
|
|
int vf = v;
|
|
|
|
v -= vf;
|
|
|
|
vf %= (isize(rest)-1);
|
|
|
|
res = rest[vf] + (rest[vf+1] - rest[vf]) * v;
|
|
|
|
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;
|
2018-11-06 23:51:41 +00:00
|
|
|
return real(ep.parse());
|
2018-09-10 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 19:00:52 +00:00
|
|
|
EX string parser_help() {
|
2018-11-07 00:03:27 +00:00
|
|
|
return XLAT("Functions available: %1",
|
2019-04-07 01:09:52 +00:00
|
|
|
"(a)sin(h), (a)cos(h), (a)tan(h), exp, log, abs, re, im, conj, let(t=...,...t...), floor, frac, e, i, pi, s, ms, mousex, mousey, mousez, shot [1 if taking screenshot/animation], to01, ifp(a,v,w) [if positive]");
|
2018-11-07 00:03:27 +00:00
|
|
|
}
|
2018-11-24 16:01:49 +00:00
|
|
|
|
2018-06-10 23:58:31 +00:00
|
|
|
}
|