aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / allen-scene / native / mind.c
| 1 | /* mind.c -- Allen's presence; see mind.h. */ |
| 2 | #include "mind.h" |
| 3 | |
| 4 | #define S(v) ((int32_t)(((int64_t)(v) * (int64_t)m->k + 128) / 256)) |
| 5 | #define INNER 24u /* the last INNER points sit inside the sphere */ |
| 6 | #define TILT 3641u /* the upright axis leans toward the viewer by 20 degrees, in 65536ths */ |
| 7 | |
| 8 | static uint32_t rnd(ck_mind *m) |
| 9 | { |
| 10 | m->rng ^= m->rng << 13; |
| 11 | m->rng ^= m->rng >> 17; |
| 12 | m->rng ^= m->rng << 5; |
| 13 | return m->rng; |
| 14 | } |
| 15 | |
| 16 | static int64_t dist2(const int16_t *a, const int16_t *b) |
| 17 | { |
| 18 | int64_t dx = a[0] - b[0], dy = a[1] - b[1], dz = a[2] - b[2]; |
| 19 | return dx * dx + dy * dy + dz * dz; |
| 20 | } |
| 21 | |
| 22 | static int has_edge(const ck_mind *m, unsigned a, unsigned b) |
| 23 | { |
| 24 | for (unsigned i = 0; i < m->ne; i++) |
| 25 | if ((m->ea[i] == a && m->eb[i] == b) || (m->ea[i] == b && m->eb[i] == a)) |
| 26 | return 1; |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | void ck_mind_init(ck_mind *m, int32_t cx, int32_t cy, uint32_t k, uint32_t born) |
| 31 | { |
| 32 | *m = (ck_mind){0}; |
| 33 | m->cx = cx, m->cy = cy, m->k = k, m->born = born; |
| 34 | m->R = S(118); |
| 35 | m->rng = 0x9e3779b9u; |
| 36 | m->spin = 349; /* a turn in 48 s */ |
| 37 | unsigned outer = CK_MIND_NODES - INNER; |
| 38 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 39 | int64_t y, rr, x, z, len; |
| 40 | if (i < outer) { /* evenly spread over the sphere: a Fibonacci lattice */ |
| 41 | y = 16384 - (int64_t)(2 * i + 1) * 16384 / outer; |
| 42 | rr = (int64_t)ck_isqrt((uint64_t)(16384 * 16384 - y * y)); |
| 43 | uint32_t th = (uint32_t)(i * 25033u); /* the golden angle */ |
| 44 | x = rr * ck_cos(th) >> 14, z = rr * ck_sin(th) >> 14; |
| 45 | len = 16384 - 2950 + (int64_t)(ck_hash((int32_t)i, 1, 31) % 5900u); /* +-18%: a cloud, not a ball */ |
| 46 | } else { |
| 47 | uint32_t h1 = ck_hash((int32_t)i, 2, 31), h2 = ck_hash((int32_t)i, 3, 31); |
| 48 | y = 16384 - (int64_t)(h1 % 32768u); |
| 49 | rr = (int64_t)ck_isqrt((uint64_t)(16384 * 16384 - y * y)); |
| 50 | x = rr * ck_cos(h2) >> 14, z = rr * ck_sin(h2) >> 14; |
| 51 | len = 4915 + (int64_t)(ck_hash((int32_t)i, 4, 31) % 5734u); /* 0.30 .. 0.65 */ |
| 52 | } |
| 53 | m->home[i][0] = (int16_t)(x * len >> 14); |
| 54 | m->home[i][1] = (int16_t)(y * len >> 14); |
| 55 | m->home[i][2] = (int16_t)(z * len >> 14); |
| 56 | } |
| 57 | /* each point joined to its nearest two (the inner ones to three) */ |
| 58 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 59 | unsigned want = i < outer ? 2 : 3; |
| 60 | for (unsigned w = 0; w < want; w++) { |
| 61 | int best = -1; |
| 62 | int64_t bd = 0; |
| 63 | for (unsigned j = 0; j < CK_MIND_NODES; j++) { |
| 64 | if (j == i || has_edge(m, i, j)) |
| 65 | continue; |
| 66 | int64_t d = dist2(m->home[i], m->home[j]); |
| 67 | if (best < 0 || d < bd) |
| 68 | best = (int)j, bd = d; |
| 69 | } |
| 70 | if (best >= 0 && m->ne < CK_MIND_EDGES) |
| 71 | m->ea[m->ne] = (uint8_t)i, m->eb[m->ne] = (uint8_t)best, m->ne++; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void ck_mind_mode(ck_mind *m, int mode, uint32_t t) |
| 77 | { |
| 78 | (void)t; |
| 79 | m->mode = mode; |
| 80 | } |
| 81 | |
| 82 | static void push(uint32_t *ring, uint32_t t) |
| 83 | { |
| 84 | for (unsigned i = 3; i > 0; i--) |
| 85 | ring[i] = ring[i - 1]; |
| 86 | ring[0] = t ? t : 1; |
| 87 | } |
| 88 | |
| 89 | void ck_mind_word(ck_mind *m, uint32_t t) { push(m->words, t); } |
| 90 | void ck_mind_key(ck_mind *m, uint32_t t) { push(m->keys, t); } |
| 91 | void ck_mind_voice(ck_mind *m, uint32_t level) { m->voice = level > 256 ? 256 : level; } |
| 92 | |
| 93 | static void spark(ck_mind *m, unsigned from) |
| 94 | { |
| 95 | if (m->nspark >= CK_MIND_SPARKS || !m->ne) |
| 96 | return; |
| 97 | /* a thread leaving `from`, or any thread when from is out of range */ |
| 98 | unsigned e = rnd(m) % m->ne, tries = 0; |
| 99 | if (from < CK_MIND_NODES) |
| 100 | while (m->ea[e] != from && m->eb[e] != from && tries++ < m->ne) |
| 101 | e = (e + 1) % m->ne; |
| 102 | unsigned s = m->nspark++; |
| 103 | m->spark[s].e = (uint16_t)e; |
| 104 | m->spark[s].back = (uint8_t)(from < CK_MIND_NODES ? m->eb[e] == from : rnd(m) & 1); |
| 105 | m->spark[s].t0 = m->t; |
| 106 | m->spark[s].dur = (uint16_t)(m->mode == CK_MIND_THINK ? 240 + rnd(m) % 260 : 420 + rnd(m) % 480); |
| 107 | } |
| 108 | |
| 109 | static uint32_t toward(uint32_t cur, uint32_t want, uint32_t dt, uint32_t ms) |
| 110 | { |
| 111 | int64_t d = (int64_t)want - cur, step = d * (int64_t)(dt < ms ? dt : ms) / (int64_t)ms; |
| 112 | if (step == 0 && d && dt) |
| 113 | step = d > 0 ? 1 : -1; |
| 114 | return (uint32_t)((int64_t)cur + step); |
| 115 | } |
| 116 | |
| 117 | static uint32_t pulse(int32_t off, int32_t width) /* (1 - (off/width)^2)^2, 0..256 */ |
| 118 | { |
| 119 | if (off <= -width || off >= width) |
| 120 | return 0; |
| 121 | int64_t q = 256 - (int64_t)off * off * 256 / ((int64_t)width * width); |
| 122 | return (uint32_t)(q * q >> 8); |
| 123 | } |
| 124 | |
| 125 | void ck_mind_at(ck_mind *m, uint32_t t) |
| 126 | { |
| 127 | uint32_t dt = t > m->t ? t - m->t : 0; |
| 128 | if (dt > 250) |
| 129 | dt = 250; /* a long pause is not a jump */ |
| 130 | m->t = t; |
| 131 | m->was = m->box; |
| 132 | if (t < m->born) { |
| 133 | m->alpha = 0; |
| 134 | m->box = (ck_rect){0, 0, 0, 0}; |
| 135 | return; |
| 136 | } |
| 137 | static const uint32_t spins[4] = {349, 520, 1400, 600}, squeezes[4] = {0, 4, 16, 0}, hearts[4] = {50, 70, 120, 80}, |
| 138 | rates[4] = {2, 6, 25, 12}; |
| 139 | int md = m->mode & 3; |
| 140 | m->spin = toward(m->spin, spins[md], dt, 700); |
| 141 | m->squeeze = toward(m->squeeze, squeezes[md], dt, 500); |
| 142 | uint32_t want_heart = hearts[md] + (md == CK_MIND_SPEAK ? m->voice * 120 / 256 : 0); |
| 143 | if (md == CK_MIND_THINK) |
| 144 | want_heart += (uint32_t)(16384 + ck_sin(t * 65536u / 900u)) * 30 >> 15; |
| 145 | m->heart = toward(m->heart, want_heart, dt, md == CK_MIND_SPEAK ? 80 : 400); |
| 146 | m->angle += m->spin * dt; |
| 147 | |
| 148 | /* sparks: born at the mode's rate; each that arrives lights its point, and while thinking may go on */ |
| 149 | uint32_t form = (uint32_t)ck_prog(t, m->born, m->born + CK_MIND_FORM_MS); |
| 150 | if (form >= 192) { |
| 151 | m->spawn += rates[md] * dt; |
| 152 | while (m->spawn >= 1000) { |
| 153 | m->spawn -= 1000; |
| 154 | spark(m, CK_MIND_NODES); |
| 155 | } |
| 156 | } |
| 157 | for (unsigned i = 0; i < m->nspark;) { |
| 158 | uint32_t age = t - m->spark[i].t0; |
| 159 | if (age < m->spark[i].dur) { |
| 160 | i++; |
| 161 | continue; |
| 162 | } |
| 163 | unsigned e = m->spark[i].e, to = m->spark[i].back ? m->ea[e] : m->eb[e]; |
| 164 | m->act[to] = 256; |
| 165 | m->spark[i] = m->spark[--m->nspark]; |
| 166 | if (md == CK_MIND_THINK ? rnd(m) % 100 < 70 : rnd(m) % 100 < 18) |
| 167 | spark(m, to); |
| 168 | } |
| 169 | for (unsigned i = 0; i < CK_MIND_NODES; i++) |
| 170 | m->act[i] = (uint16_t)(m->act[i] * (uint32_t)ck_exp_neg(dt * 256 / 260) >> 16); |
| 171 | |
| 172 | /* the shape: breathing, drawn in while thinking, and while being born gathered in from far out */ |
| 173 | int64_t scale = 65536 + ((int64_t)ck_sin(t * 65536u / 5500u) * 1966 >> 14) - (int64_t)m->squeeze * 65536 / 256; |
| 174 | uint32_t gather = ck_ease_out((int32_t)form); |
| 175 | scale = scale * (256 + (int64_t)(256 - gather) * 560 / 256) >> 8; /* 3.2 times out at the start */ |
| 176 | uint32_t swirl = (256 - gather) * (256 - gather) >> 8; /* an extra spiral turn that unwinds as it gathers */ |
| 177 | m->alpha = ck_smooth((int32_t)form * 3); |
| 178 | uint32_t a = (m->angle >> 8) + swirl * 256; |
| 179 | int64_t ca = ck_cos(a), sa = ck_sin(a), ct = ck_cos(TILT), st = ck_sin(TILT); |
| 180 | int64_t R16 = (int64_t)m->R * 16; |
| 181 | int32_t x0 = m->cx * 16, x1 = x0, y0 = m->cy * 16, y1 = y0; |
| 182 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 183 | int64_t x = m->home[i][0], y = m->home[i][1], z = m->home[i][2]; |
| 184 | int64_t rx = (x * ca - z * sa) >> 14, rz = (x * sa + z * ca) >> 14; /* turn about the upright axis */ |
| 185 | int64_t ry = (y * ct - rz * st) >> 14, fz = (y * st + rz * ct) >> 14; /* lean toward the viewer */ |
| 186 | int64_t persp = 16384 + fz * 3 / 16; /* nearer is a little larger */ |
| 187 | m->px[i] = (int32_t)(m->cx * 16 + 8 + (rx * persp >> 14) * R16 / 16384 * scale / 65536); |
| 188 | m->py[i] = (int32_t)(m->cy * 16 + 8 + (ry * persp >> 14) * R16 / 16384 * scale / 65536); |
| 189 | uint32_t nr = (uint32_t)((fz + 16384) * 256 / 32768); |
| 190 | m->near[i] = (uint16_t)(nr > 256 ? 256 : nr); |
| 191 | /* light from the pulses: words outward from the heart, keys upward from below */ |
| 192 | int64_t rad = (int64_t)ck_isqrt((uint64_t)(x * x + y * y + z * z)) * 256 / 16384; |
| 193 | uint32_t extra = 0; |
| 194 | for (unsigned w = 0; w < 4; w++) { |
| 195 | if (m->words[w] && t - m->words[w] < 900) { |
| 196 | uint32_t age = t - m->words[w]; |
| 197 | extra += pulse((int32_t)rad - (int32_t)(age * 300 / 600), 70) * (900 - age) / 900 * 150 >> 8; |
| 198 | } |
| 199 | if (m->keys[w] && t - m->keys[w] < 600) { |
| 200 | uint32_t age = t - m->keys[w]; |
| 201 | int32_t ky = 300 - (int32_t)(age * 600 / 500), ny = (int32_t)(y * 256 / 16384); |
| 202 | extra += pulse(ny - ky, 60) * (600 - age) / 600 * 90 >> 8; |
| 203 | } |
| 204 | } |
| 205 | uint32_t l = 34 + m->near[i] * m->near[i] / 256 * 180 / 256 + extra + m->act[i] * 120 / 256; |
| 206 | m->lum[i] = (uint16_t)(l > 256 ? 256 : l); |
| 207 | x0 = ck_min(x0, m->px[i]), x1 = ck_max(x1, m->px[i]); |
| 208 | y0 = ck_min(y0, m->py[i]), y1 = ck_max(y1, m->py[i]); |
| 209 | } |
| 210 | m->hx = m->cx * 16 + 8, m->hy = m->cy * 16 + 8; |
| 211 | m->hr = (int32_t)(R16 * 52 / 100 * scale / 65536); |
| 212 | int32_t halo = (int32_t)(R16 * 17 / 10 * ck_min((int32_t)(scale >> 8), 256) / 256); |
| 213 | int32_t pad = S(8) * 16; |
| 214 | x0 = ck_min(x0 - pad, m->hx - halo), x1 = ck_max(x1 + pad, m->hx + halo); |
| 215 | y0 = ck_min(y0 - pad, m->hy - halo), y1 = ck_max(y1 + pad, m->hy + halo); |
| 216 | m->box = (ck_rect){(x0 >> 4) - 1, (y0 >> 4) - 1, ((x1 - x0) >> 4) + 3, ((y1 - y0) >> 4) + 3}; |
| 217 | } |
| 218 | |
| 219 | ck_rect ck_mind_dirty(const ck_mind *m) { return ck_union(m->was, m->box); } |
| 220 | |
| 221 | void ck_mind_draw(const ck_mind *m, ck_canvas *out, const ck_rect *r) |
| 222 | { |
| 223 | if (!m->alpha || !ck_meets(m->box, r)) |
| 224 | return; |
| 225 | uint32_t A = m->alpha, form = (uint32_t)ck_prog(m->t, m->born, m->born + CK_MIND_FORM_MS); |
| 226 | int64_t R16 = (int64_t)m->R * 16; |
| 227 | /* the halo: a faint blue light round the whole mind, swelling a little with the heart */ |
| 228 | ck_glow(out, r, m->hx, m->hy, (int32_t)(R16 * 17 / 10), CK_RGB(60, 74, 210), (52 + m->heart / 8) * A >> 8); |
| 229 | ck_glow(out, r, m->hx, m->hy, (int32_t)R16, CK_RGB(112, 92, 255), (30 + m->heart / 8) * A >> 8); |
| 230 | /* the threads, fading in once the points have mostly gathered */ |
| 231 | uint32_t ta = ck_smooth(((int32_t)form - 128) * 2) * A >> 8; |
| 232 | if (ta) |
| 233 | for (unsigned i = 0; i < m->ne; i++) { |
| 234 | unsigned a = m->ea[i], b = m->eb[i]; |
| 235 | uint32_t a0 = (m->lum[a] * (16u + m->near[a] * 60u / 256u) >> 8) * ta >> 8, |
| 236 | a1 = (m->lum[b] * (16u + m->near[b] * 60u / 256u) >> 8) * ta >> 8; |
| 237 | ck_thread(out, r, m->px[a], m->py[a], m->px[b], m->py[b], CK_RGB(120, 140, 255), a0, a1); |
| 238 | if (m->k > 320) /* a second strand on big screens, so the thread keeps its weight */ |
| 239 | ck_thread(out, r, m->px[a], m->py[a] + 16, m->px[b], m->py[b] + 16, CK_RGB(120, 140, 255), a0 / 2, |
| 240 | a1 / 2); |
| 241 | } |
| 242 | /* the points: pale blue-white, warming as they light */ |
| 243 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 244 | int32_t rad = (int32_t)((int64_t)S(3) * 16 * (160 + m->near[i] * 120 / 256) >> 8); |
| 245 | uint32_t c = ck_mix(CK_RGB(206, 222, 255), CK_RGB(255, 214, 160), m->act[i]); |
| 246 | ck_glow(out, r, m->px[i], m->py[i], rad * 3, c, (m->lum[i] * 50 >> 8) * A >> 8); |
| 247 | ck_glow(out, r, m->px[i], m->py[i], rad, c, m->lum[i] * A >> 8); |
| 248 | } |
| 249 | /* the sparks, each with a short fading tail */ |
| 250 | for (unsigned i = 0; i < m->nspark; i++) { |
| 251 | unsigned e = m->spark[i].e, a = m->ea[e], b = m->eb[e]; |
| 252 | if (m->spark[i].back) |
| 253 | a = m->eb[e], b = m->ea[e]; |
| 254 | int32_t f = (int32_t)((m->t - m->spark[i].t0) * 256u / m->spark[i].dur); |
| 255 | for (int k = 0; k < 3; k++) { |
| 256 | int32_t g = ck_clamp(f - k * 22, 0, 256); |
| 257 | int32_t x = m->px[a] + (int32_t)((int64_t)(m->px[b] - m->px[a]) * g >> 8); |
| 258 | int32_t y = m->py[a] + (int32_t)((int64_t)(m->py[b] - m->py[a]) * g >> 8); |
| 259 | ck_glow(out, r, x, y, S(5) * 16 - k * S(1) * 16, CK_RGB(255, 222, 176), (200 >> k) * A >> 8); |
| 260 | } |
| 261 | } |
| 262 | /* the heart: a warm light at the centre, and a small bright core */ |
| 263 | ck_glow(out, r, m->hx, m->hy, m->hr, CK_RGB(255, 210, 170), m->heart * 7 / 10 * A >> 8); |
| 264 | ck_glow(out, r, m->hx, m->hy, m->hr * 2 / 5, CK_RGB(255, 236, 214), (90 + m->heart) * A >> 8); |
| 265 | ck_glow(out, r, m->hx, m->hy, m->hr / 8, CK_RGB(255, 250, 240), 230 * A >> 8); |
| 266 | } |