From 2970b0c7d633f03a17c3a8c08b609a108c96ed51 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 04:36:29 -0500 Subject: [PATCH 1/2] Add a floor scan that finds the floor carrying Link in guest memory on 1.1.1. The Deku Tree's floating platform is not one of the engine's ground groups, and where its position lives is not known. With floor_scan set (off by default), once Link has been carried at 60 fps for 30 frames with the left stick at rest, one snapshot of the game's heap and alias regions keeps every float triplet near him; 30 frames later the triplets that moved as far as he did are kept, and from then on each frame logs the twelve of them farthest from him with their addresses, positions and moves. The snapshot logs how long it took. Co-Authored-By: Claude Opus 5.5 --- nx2navi.ini.example | 4 + runtime/cfg.c | 6 +- runtime/frame_rate.c | 195 +++++++++++++++++++++++++++++++++++++++ tests/frame_step_check.c | 7 ++ 4 files changed, 210 insertions(+), 2 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 6f188c5a..bf84e0cc 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -1014,6 +1014,10 @@ # Diagnosis: one log line per frame while Link stands near a moving floor (the floating platforms): the frame's step, # how far he moved and how far the floor moved. 1 writes 4000 lines, a larger number that many; 0 is off. # floor_probe = 0 +# Diagnosis: while Link is carried by a moving floor at 60 fps and the left stick rests, one snapshot of the game's +# memory finds the values that move with him (the floor's position among them) and logs them each frame from then on. +# The snapshot holds the game for a moment once. 1 writes 6000 lines, a larger number that many; 0 is off. +# floor_scan = 0 # 1: with trace = perf, walks of the process's address space count its private and write-combined memory every 600 # frames (the "mem: process" line) and where its committed memory lies every 3600 frames (the "commit census" lines). # Each walk holds the address-space lock for 100-240 ms and delays the next frame by as much, so they are off unless diff --git a/runtime/cfg.c b/runtime/cfg.c index ddb97133..194b342b 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -34,7 +34,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. static const char *const known[] = { - "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "WATER_STEPS", "FLOOR_PROBE", "MOVIE_PTS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) + "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "WATER_STEPS", "FLOOR_PROBE", "FLOOR_SCAN", "MOVIE_PTS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AA", "AA_LOG", "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CATCHUP", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_RAMP", "AUDIO_STATS", "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "MESH_FALLBACK_WORKERS", "PROBE_MESHFB_DIR", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", @@ -100,7 +100,7 @@ static const struct { const char *key, *section; } sections[] = { {"VRAM_HEAP_LIMIT", "performance"}, {"VRAM_RESERVE_MB", "performance"}, {"UPLOAD_BATCH_MB", "performance"}, {"MESH_EMULATION", "performance"}, {"MESH_EMU_MB", "performance"}, {"MESH_EMU_TASKS", "performance"}, {"MESH_EMU_BATCH", "performance"}, {"VEC_INDEX_FIX", "performance"}, {"GUEST_OPT", "performance"}, {"PASS_TIMES", "diagnostics"}, {"DUMP_KEY", "diagnostics"}, - {"FLOOR_PROBE", "diagnostics"}, + {"FLOOR_PROBE", "diagnostics"}, {"FLOOR_SCAN", "diagnostics"}, {"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, {"RTSS", "frame rate"}, {"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"}, {"PHYSICS_STEPS", "frame rate"}, {"PICKUP_COUNT", "frame rate"}, {"WATER_STEPS", "frame rate"}, @@ -248,6 +248,8 @@ static const CfgMeta metas[] = { {"WATER_STEPS", "1", "0|1", RESTART}, // frame_rate.c floor_probe: lines about Link on a moving floor (0 off, 1 = 4000 lines, or a count) {"FLOOR_PROBE", "0", NULL, RESTART}, + // frame_rate.c floor_scan: finds the moving floor under Link in guest memory (0 off, 1 = 6000 lines, or a count) + {"FLOOR_SCAN", "0", NULL, RESTART}, {"LANG", "", "ja|en-US|fr|de|it|es|zh-CN|ko|nl|pt|ru|zh-TW|en-GB|fr-CA|es-419|zh-Hans|zh-Hant|pt-BR|pl", RESTART}, {"OPERATION_MODE", "docked", "docked|handheld", LIVE}, {"TEXT_SPEED", "normal", "normal|fast|instant", LIVE}, // text_speed.c: the message window's typing diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 5d4ed6e2..7f7bfbe8 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -51,6 +51,7 @@ #include "cfg.h" #include "gfx.h" #include "host.h" +#include "input.h" #include "layout.h" #include "platform/platform.h" #include "vk_dlssg.h" @@ -899,6 +900,195 @@ static void floor_probe(void) { floor_link_ok = link_ok; } +// ---- NX2NAVI_FLOOR_SCAN (floor_scan in nx2navi.ini, 0 by default): finds the moving floor under Link in guest memory +// (diagnosis, 1.1.1). The Deku Tree's floating platform is not one of the engine's ground groups (floor_probe), and +// where its position lives is not known. While Link is carried at the 60 fps rate (a step of one frame, a horizontal +// move of 2 to 30 mm a present for FLOOR_SCAN_ARM presents in a row, the gamepad's left stick under 20%), the scan +// takes one snapshot of every 4-byte-aligned float triplet in the game's heap and alias regions whose x and z lie +// within 6 m of Link and whose y lies within 4 m (walked as the debug menu's memory search walks them, debug_tools.c +// dbg_search_new), and logs how long that took. FLOOR_SCAN_ARM presents later it keeps the triplets that moved +// horizontally as far as Link (within a fifth, 2 cm at least): Link's own copies (his actor, body, bones) and the +// floor's (its matrix, its body, its drive). From then on each present logs the step, Link, and the survivors farthest +// from him horizontally (the floor's points sit away from him; his own copies stay within half a metre), each with its +// address, position and move, so a run at a higher frame rate shows which of those move and how fast. A snapshot that +// leaves no survivor farther than half a metre arms the scan again, three times at most. The value is the number of +// lines to write (1 means 6000); off it costs one test per present. +#define FLOOR_SCAN_ARM 30 +#define FLOOR_SCAN_MAX 2000000 // candidate triplets a snapshot keeps +#define FLOOR_SCAN_SHOW 12 // survivors a line names +static int floor_scan_left = -1; // lines still to write; -1 before the setting was read +static struct { + int state, run, tries; // state 0: waiting for a carry, 1: filtering, 2: logging + float link0[3], last[3]; + int have_last; + uint64_t *addr; + float *val; // the triplet at the snapshot, later the last logged one + int n; + int show[FLOOR_SCAN_SHOW], shown; +} fs; + +static void floor_scan_snapshot(const float link[3]) { + enum { PIECE = 16 << 20 }; + if (!fs.addr) fs.addr = malloc(sizeof(uint64_t) * FLOOR_SCAN_MAX); + if (!fs.val) fs.val = malloc(sizeof(float) * 3 * FLOOR_SCAN_MAX); + if (!fs.addr || !fs.val) return; + uint64_t t0 = plat_now_ns(), bytes = 0; + int over = 0; + fs.n = 0; + const RtGuestLayout *l = mem_layout(); + const RtRange ranges[2] = {l->heap, l->alias}; + for (int r = 0; r < 2; r++) { + uint64_t a = ranges[r].base, end = ranges[r].base + ranges[r].size; + while (a < end) { + RtMemoryInfo mi; + if (mem_query(a, &mi) || !mi.size) break; + uint64_t stop = mi.base + mi.size < end ? mi.base + mi.size : end; + if (!mi.state || (mi.perm & 3) != 3) { + a = stop; + continue; + } + while (a < stop) { + uint64_t n = stop - a < PIECE ? stop - a : PIECE; + if (n >= 12 && mem_read_hold(a, n)) { + const float *f = (const float *)(uintptr_t)a; + uint64_t words = n / 4; + bytes += n; + for (uint64_t i = 0; i + 2 < words; i++) { + float x = f[i]; + if (!(fabsf(x - link[0]) < 6.0f)) continue; // the cheap test first: most words fail it + float y = f[i + 1], z = f[i + 2]; + if (!(fabsf(z - link[2]) < 6.0f) || !(fabsf(y - link[1]) < 4.0f)) continue; + if (fs.n < FLOOR_SCAN_MAX) { + fs.addr[fs.n] = a + 4 * i; + memcpy(fs.val + 3 * fs.n, f + i, 12); + fs.n++; + } else + over = 1; + } + mem_read_release(); + } + a += n; + } + } + } + memcpy(fs.link0, link, 12); + rt_log("floor scan: snapshot of %.0f MB in %.0f ms: %d float triplets near Link at %.3f %.3f %.3f%s", bytes / 1e6, + (plat_now_ns() - t0) / 1e6, fs.n, link[0], link[1], link[2], over ? " (more were left out)" : ""); +} + +// Reads the n triplets at fs.addr into out (NAN where the memory could not be read), in one batched copy. +static void floor_scan_read(int n, float *out) { + static uint64_t (*src)[2]; + static uint8_t **dst, *ok; + if (!src) src = malloc(sizeof *src * FLOOR_SCAN_MAX); + if (!dst) dst = malloc(sizeof *dst * FLOOR_SCAN_MAX); + if (!ok) ok = malloc(FLOOR_SCAN_MAX); + if (!src || !dst || !ok) return; + for (int i = 0; i < n; i++) src[i][0] = fs.addr[i], src[i][1] = 12, dst[i] = (uint8_t *)(out + 3 * i); + mem_copy_readable(dst, (const uint64_t (*)[2])src, n, ok); + for (int i = 0; i < n; i++) + if (!ok[i]) out[3 * i] = out[3 * i + 1] = out[3 * i + 2] = NAN; +} + +static float hdist(const float a[3], const float b[3]) { return hypotf(a[0] - b[0], a[2] - b[2]); } + +static void floor_scan(const float link[3], int link_ok) { + if (floor_scan_left < 0) { + const char *e = cfg_get("NX2NAVI_FLOOR_SCAN"); + int n = e && *e ? atoi(e) : 0; + floor_scan_left = !RT_V111 || n <= 0 ? 0 : n == 1 ? 6000 : n; + if (floor_scan_left) rt_log("floor scan: on for %d lines", floor_scan_left); + } + if (!floor_scan_left) return; + if (!link_ok) { + fs.have_last = 0, fs.run = 0; + return; + } + float move = fs.have_last ? hdist(link, fs.last) : 0; + memcpy(fs.last, link, 12); + int had = fs.have_last; + fs.have_last = 1; + if (fs.state == 0) { + double pct[2] = {0, 0}; + float step = frame_step_frames(); + int still = !input_pad_stick_percent(pct) || pct[0] < 20.0; + int carried = had && still && fabsf(step - 1.0f) < 0.05f && move > 0.002f && move < 0.03f; + fs.run = carried ? fs.run + 1 : 0; + if (fs.run >= FLOOR_SCAN_ARM && fs.tries < 3) { + fs.tries++, fs.run = 0; + floor_scan_snapshot(link); + if (fs.n) fs.state = 1; + } + return; + } + if (fs.state == 1) { + if (++fs.run < FLOOR_SCAN_ARM) return; + fs.run = 0; + float d_link = hdist(link, fs.link0); + static float *now; + if (!now) now = malloc(sizeof(float) * 3 * FLOOR_SCAN_MAX); + if (!now) return; + floor_scan_read(fs.n, now); + int kept = 0, far = 0; + float tol = d_link * 0.2f > 0.02f ? d_link * 0.2f : 0.02f; + for (int i = 0; i < fs.n; i++) { + const float *v = now + 3 * i, *w = fs.val + 3 * i; + if (!isfinite(v[0]) || !isfinite(v[2])) continue; + float d = hypotf(v[0] - w[0], v[2] - w[2]); + if (!(fabsf(d - d_link) <= tol) || d < 0.01f) continue; + fs.addr[kept] = fs.addr[i]; + memcpy(fs.val + 3 * kept, v, 12); + far += hdist(v, link) > 0.5f; + kept++; + } + fs.n = kept; + rt_log("floor scan: %d of the triplets moved with Link (%.3f m in %d presents), %d of them more than 0.5 m " + "from him", + kept, d_link, FLOOR_SCAN_ARM, far); + if (!far) { // Link's own copies alone: the next carry arms the scan again + fs.state = 0, fs.n = 0; + return; + } + // the survivors to show: the farthest from Link horizontally + fs.shown = 0; + for (int j = 0; j < FLOOR_SCAN_SHOW; j++) { // a selection: the farthest not yet shown, FLOOR_SCAN_SHOW times + int best = -1; + float best_d = -1; + for (int i = 0; i < fs.n; i++) { + int taken = 0; + for (int t = 0; t < fs.shown && !taken; t++) taken = fs.show[t] == i; + float d = hdist(fs.val + 3 * i, link); + if (!taken && d > best_d) best = i, best_d = d; + } + if (best < 0) break; + fs.show[fs.shown++] = best; + } + fs.state = 2; + return; + } + // state 2: one line a present for the shown survivors + float v[FLOOR_SCAN_SHOW][3]; + int k = fs.shown; + for (int j = 0; j < k; j++) { + uint8_t ok = 0, *dst = (uint8_t *)v[j]; + uint64_t src[1][2] = {{fs.addr[fs.show[j]], 12}}; + mem_copy_readable(&dst, (const uint64_t (*)[2])src, 1, &ok); + if (!ok) v[j][0] = v[j][1] = v[j][2] = NAN; + } + char line[256 + FLOOR_SCAN_SHOW * 110]; + int o = snprintf(line, sizeof line, "floor scan: step %.4f frames; link %.4f %.4f %.4f moved %.5f", + (double)frame_step_frames(), link[0], link[1], link[2], move); + for (int j = 0; j < k && o > 0 && o < (int)sizeof line; j++) { + float *w = fs.val + 3 * fs.show[j]; + o += snprintf(line + o, sizeof line - o, "; %#llx at %.4f %.4f %.4f moved %.5f %.5f %.5f", + (unsigned long long)fs.addr[fs.show[j]], v[j][0], v[j][1], v[j][2], v[j][0] - w[0], + v[j][1] - w[1], v[j][2] - w[2]); + memcpy(w, v[j], 12); + } + rt_log("%s", line); + floor_scan_left--; +} + void frame_step_present(uint64_t display_ns, int interval) { atomic_fetch_add_explicit(&guest_presents, 1, memory_order_relaxed); game_interval = interval > 0 ? interval : 1; @@ -931,6 +1121,11 @@ void frame_step_present(uint64_t display_ns, int interval) { memory_order_relaxed); world_clock(dt, step); floor_probe(); + if (floor_scan_left) { // after its first read the scan costs this test alone while off + float link[3]; + int ok = floor_scan_left > 0 ? player_pos(link) : 0; + floor_scan(link, ok); + } if (rt_trace & RT_TRACE_PERF) { time_dump(); if (frame_free_running()) { // the game's own step, as its time manager holds it diff --git a/tests/frame_step_check.c b/tests/frame_step_check.c index 8607fd0a..285fd9e2 100644 --- a/tests/frame_step_check.c +++ b/tests/frame_step_check.c @@ -45,6 +45,13 @@ int plat_mem_readable(uint64_t a, size_t n) { (void)a, (void)n; return 0; } void mem_kernel_access(uint64_t a, uint64_t n) { (void)a, (void)n; } int mem_host_readable(uint64_t a, uint64_t n) { (void)a, (void)n; return 0; } int mem_info(uint32_t id, uint64_t *out) { (void)id, (void)out; return 0; } // the floor probe (off here) +// the floor scan (off here) +static RtGuestLayout layout_stub; +const RtGuestLayout *mem_layout(void) { return &layout_stub; } +uint32_t mem_query(uint64_t a, RtMemoryInfo *out) { (void)a, memset(out, 0, sizeof *out); return 1; } +int mem_read_hold(uint64_t a, uint64_t n) { (void)a, (void)n; return 0; } +void mem_read_release(void) {} +int input_pad_stick_percent(double pct[2]) { pct[0] = pct[1] = 0; return 0; } int mem_copy_readable(uint8_t *const *dst, const uint64_t (*src)[2], int n, uint8_t *ok) { (void)dst, (void)src, (void)n, (void)ok; return 0; From 27dae195ab9c9e48f2c8ed5e99a74641f213eb51 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 04:45:52 -0500 Subject: [PATCH 2/2] Arm the floor scan only on a sideways drift and keep what lies below Link, logged once a second. Scan run 1 armed on a floor that only sinks and found points 3.4 m above Link that follow him (the camera's). The scan now arms on a straight horizontal drift at a steady height, snapshots from 4 m below Link to 0.5 m above, keeps the triplets that moved with him and lie more than 0.3 m below him or more than 0.5 m from his feet, and logs each of them (64 at most) once a second with its move over that second. Co-Authored-By: Claude Opus 5.5 --- nx2navi.ini.example | 7 +- runtime/cfg.c | 2 +- runtime/frame_rate.c | 148 +++++++++++++++++++++---------------------- 3 files changed, 78 insertions(+), 79 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index bf84e0cc..7fee3a37 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -1014,9 +1014,10 @@ # Diagnosis: one log line per frame while Link stands near a moving floor (the floating platforms): the frame's step, # how far he moved and how far the floor moved. 1 writes 4000 lines, a larger number that many; 0 is off. # floor_probe = 0 -# Diagnosis: while Link is carried by a moving floor at 60 fps and the left stick rests, one snapshot of the game's -# memory finds the values that move with him (the floor's position among them) and logs them each frame from then on. -# The snapshot holds the game for a moment once. 1 writes 6000 lines, a larger number that many; 0 is off. +# Diagnosis: while a floor carries Link sideways at 60 fps and the left stick rests, one snapshot of the game's memory +# finds the values below him that move with him (the floor's position among them) and logs them once a second from +# then on. The snapshot holds the game for about a second once. 1 writes 20000 lines, a larger number that many; 0 is +# off. # floor_scan = 0 # 1: with trace = perf, walks of the process's address space count its private and write-combined memory every 600 # frames (the "mem: process" line) and where its committed memory lies every 3600 frames (the "commit census" lines). diff --git a/runtime/cfg.c b/runtime/cfg.c index 194b342b..186ace31 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -248,7 +248,7 @@ static const CfgMeta metas[] = { {"WATER_STEPS", "1", "0|1", RESTART}, // frame_rate.c floor_probe: lines about Link on a moving floor (0 off, 1 = 4000 lines, or a count) {"FLOOR_PROBE", "0", NULL, RESTART}, - // frame_rate.c floor_scan: finds the moving floor under Link in guest memory (0 off, 1 = 6000 lines, or a count) + // frame_rate.c floor_scan: finds the moving floor under Link in guest memory (0 off, 1 = 20000 lines, or a count) {"FLOOR_SCAN", "0", NULL, RESTART}, {"LANG", "", "ja|en-US|fr|de|it|es|zh-CN|ko|nl|pt|ru|zh-TW|en-GB|fr-CA|es-419|zh-Hans|zh-Hant|pt-BR|pl", RESTART}, {"OPERATION_MODE", "docked", "docked|handheld", LIVE}, diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 7f7bfbe8..66d81f40 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -902,29 +902,30 @@ static void floor_probe(void) { // ---- NX2NAVI_FLOOR_SCAN (floor_scan in nx2navi.ini, 0 by default): finds the moving floor under Link in guest memory // (diagnosis, 1.1.1). The Deku Tree's floating platform is not one of the engine's ground groups (floor_probe), and -// where its position lives is not known. While Link is carried at the 60 fps rate (a step of one frame, a horizontal -// move of 2 to 30 mm a present for FLOOR_SCAN_ARM presents in a row, the gamepad's left stick under 20%), the scan -// takes one snapshot of every 4-byte-aligned float triplet in the game's heap and alias regions whose x and z lie -// within 6 m of Link and whose y lies within 4 m (walked as the debug menu's memory search walks them, debug_tools.c -// dbg_search_new), and logs how long that took. FLOOR_SCAN_ARM presents later it keeps the triplets that moved -// horizontally as far as Link (within a fifth, 2 cm at least): Link's own copies (his actor, body, bones) and the -// floor's (its matrix, its body, its drive). From then on each present logs the step, Link, and the survivors farthest -// from him horizontally (the floor's points sit away from him; his own copies stay within half a metre), each with its -// address, position and move, so a run at a higher frame rate shows which of those move and how fast. A snapshot that -// leaves no survivor farther than half a metre arms the scan again, three times at most. The value is the number of -// lines to write (1 means 6000); off it costs one test per present. +// where its position lives is not known. The scan arms while Link drifts at the 60 fps rate: a step of one frame, the +// gamepad's left stick under 20%, and for FLOOR_SCAN_ARM presents in a row a horizontal move of 2 to 30 mm a present, +// his height within 5 mm of the present before, and the whole stretch straight (its end within 80% of the sum of its +// moves), so a floor that only sinks does not arm it. It then takes one snapshot of every 4-byte-aligned float triplet +// in the game's heap and alias regions whose x and z lie within 6 m of Link and whose y lies from 4 m below him to +// 0.5 m above (walked as the debug menu's memory search walks them, debug_tools.c dbg_search_new), and logs how long +// that took. FLOOR_SCAN_ARM presents later it keeps the triplets that moved horizontally as far as Link (within a +// fifth, 2 cm at least) and lie more than 0.3 m below him or more than 0.5 m from him horizontally: the floor's +// matrix, body and drive, but not Link's own copies (his actor, body, feet) nor what rides above him. A snapshot that +// keeps none arms the scan again, three times at most. From then on, once a second, it logs Link's position and move +// over that second and one line per survivor (FLOOR_SCAN_SHOW at most) with its address, position and move over the +// same second, so a stretch at a higher frame rate shows each one's own speed. The value is the number of lines to +// write (1 means 20000); off it costs one test per present. #define FLOOR_SCAN_ARM 30 #define FLOOR_SCAN_MAX 2000000 // candidate triplets a snapshot keeps -#define FLOOR_SCAN_SHOW 12 // survivors a line names +#define FLOOR_SCAN_SHOW 64 // survivors logged each second static int floor_scan_left = -1; // lines still to write; -1 before the setting was read static struct { - int state, run, tries; // state 0: waiting for a carry, 1: filtering, 2: logging - float link0[3], last[3]; + int state, run, tries; // state 0: waiting for a drift, 1: filtering, 2: logging + float link0[3], last[3], start[3], path, sec_link[3]; int have_last; - uint64_t *addr; - float *val; // the triplet at the snapshot, later the last logged one + uint64_t *addr, sec_ns; + float *val; // the triplets at the snapshot, then at the last line int n; - int show[FLOOR_SCAN_SHOW], shown; } fs; static void floor_scan_snapshot(const float link[3]) { @@ -956,8 +957,8 @@ static void floor_scan_snapshot(const float link[3]) { for (uint64_t i = 0; i + 2 < words; i++) { float x = f[i]; if (!(fabsf(x - link[0]) < 6.0f)) continue; // the cheap test first: most words fail it - float y = f[i + 1], z = f[i + 2]; - if (!(fabsf(z - link[2]) < 6.0f) || !(fabsf(y - link[1]) < 4.0f)) continue; + float y = f[i + 1] - link[1], z = f[i + 2]; + if (!(fabsf(z - link[2]) < 6.0f) || !(y > -4.0f && y < 0.5f)) continue; if (fs.n < FLOOR_SCAN_MAX) { fs.addr[fs.n] = a + 4 * i; memcpy(fs.val + 3 * fs.n, f + i, 12); @@ -972,8 +973,9 @@ static void floor_scan_snapshot(const float link[3]) { } } memcpy(fs.link0, link, 12); - rt_log("floor scan: snapshot of %.0f MB in %.0f ms: %d float triplets near Link at %.3f %.3f %.3f%s", bytes / 1e6, - (plat_now_ns() - t0) / 1e6, fs.n, link[0], link[1], link[2], over ? " (more were left out)" : ""); + rt_log("floor scan: snapshot of %.0f MB in %.0f ms: %d float triplets around and below Link at %.3f %.3f %.3f%s", + bytes / 1e6, (plat_now_ns() - t0) / 1e6, fs.n, link[0], link[1], link[2], + over ? " (more were left out)" : ""); } // Reads the n triplets at fs.addr into out (NAN where the memory could not be read), in one batched copy. @@ -996,7 +998,7 @@ static void floor_scan(const float link[3], int link_ok) { if (floor_scan_left < 0) { const char *e = cfg_get("NX2NAVI_FLOOR_SCAN"); int n = e && *e ? atoi(e) : 0; - floor_scan_left = !RT_V111 || n <= 0 ? 0 : n == 1 ? 6000 : n; + floor_scan_left = !RT_V111 || n <= 0 ? 0 : n == 1 ? 20000 : n; if (floor_scan_left) rt_log("floor scan: on for %d lines", floor_scan_left); } if (!floor_scan_left) return; @@ -1004,20 +1006,29 @@ static void floor_scan(const float link[3], int link_ok) { fs.have_last = 0, fs.run = 0; return; } - float move = fs.have_last ? hdist(link, fs.last) : 0; - memcpy(fs.last, link, 12); + float move = fs.have_last ? hdist(link, fs.last) : 0, rise = fs.have_last ? fabsf(link[1] - fs.last[1]) : 0; int had = fs.have_last; + memcpy(fs.last, link, 12); fs.have_last = 1; if (fs.state == 0) { double pct[2] = {0, 0}; - float step = frame_step_frames(); int still = !input_pad_stick_percent(pct) || pct[0] < 20.0; - int carried = had && still && fabsf(step - 1.0f) < 0.05f && move > 0.002f && move < 0.03f; - fs.run = carried ? fs.run + 1 : 0; - if (fs.run >= FLOOR_SCAN_ARM && fs.tries < 3) { - fs.tries++, fs.run = 0; - floor_scan_snapshot(link); - if (fs.n) fs.state = 1; + int drift = had && still && fabsf(frame_step_frames() - 1.0f) < 0.05f && move > 0.002f && move < 0.03f && + rise < 0.005f; + if (!drift) { + fs.run = 0; + return; + } + if (!fs.run) memcpy(fs.start, link, 12), fs.path = 0; + fs.run++, fs.path += move; + if (fs.run >= FLOOR_SCAN_ARM) { + int straight = hdist(link, fs.start) >= 0.8f * fs.path; + fs.run = 0; + if (straight && fs.tries < 3) { + fs.tries++; + floor_scan_snapshot(link); + if (fs.n) fs.state = 1; + } } return; } @@ -1029,64 +1040,51 @@ static void floor_scan(const float link[3], int link_ok) { if (!now) now = malloc(sizeof(float) * 3 * FLOOR_SCAN_MAX); if (!now) return; floor_scan_read(fs.n, now); - int kept = 0, far = 0; + int kept = 0, moved = 0; float tol = d_link * 0.2f > 0.02f ? d_link * 0.2f : 0.02f; for (int i = 0; i < fs.n; i++) { const float *v = now + 3 * i, *w = fs.val + 3 * i; - if (!isfinite(v[0]) || !isfinite(v[2])) continue; + if (!isfinite(v[0]) || !isfinite(v[1]) || !isfinite(v[2])) continue; float d = hypotf(v[0] - w[0], v[2] - w[2]); if (!(fabsf(d - d_link) <= tol) || d < 0.01f) continue; - fs.addr[kept] = fs.addr[i]; - memcpy(fs.val + 3 * kept, v, 12); - far += hdist(v, link) > 0.5f; + moved++; + if (!(v[1] < link[1] - 0.3f || hdist(v, link) > 0.5f)) continue; // Link's own copies stay at his feet + if (kept < FLOOR_SCAN_SHOW) fs.addr[kept] = fs.addr[i], memcpy(fs.val + 3 * kept, v, 12); kept++; } - fs.n = kept; - rt_log("floor scan: %d of the triplets moved with Link (%.3f m in %d presents), %d of them more than 0.5 m " - "from him", - kept, d_link, FLOOR_SCAN_ARM, far); - if (!far) { // Link's own copies alone: the next carry arms the scan again + rt_log("floor scan: %d of %d triplets moved with Link (%.3f m in %d presents); %d of those lie below him or " + "away from his feet%s", moved, fs.n, d_link, FLOOR_SCAN_ARM, kept, + kept > FLOOR_SCAN_SHOW ? " (the first of them are logged)" : ""); + if (!kept) { // nothing below him: the next drift arms the scan again fs.state = 0, fs.n = 0; return; } - // the survivors to show: the farthest from Link horizontally - fs.shown = 0; - for (int j = 0; j < FLOOR_SCAN_SHOW; j++) { // a selection: the farthest not yet shown, FLOOR_SCAN_SHOW times - int best = -1; - float best_d = -1; - for (int i = 0; i < fs.n; i++) { - int taken = 0; - for (int t = 0; t < fs.shown && !taken; t++) taken = fs.show[t] == i; - float d = hdist(fs.val + 3 * i, link); - if (!taken && d > best_d) best = i, best_d = d; - } - if (best < 0) break; - fs.show[fs.shown++] = best; - } + fs.n = kept < FLOOR_SCAN_SHOW ? kept : FLOOR_SCAN_SHOW; + memcpy(fs.sec_link, link, 12); + fs.sec_ns = plat_now_ns(); fs.state = 2; return; } - // state 2: one line a present for the shown survivors - float v[FLOOR_SCAN_SHOW][3]; - int k = fs.shown; - for (int j = 0; j < k; j++) { - uint8_t ok = 0, *dst = (uint8_t *)v[j]; - uint64_t src[1][2] = {{fs.addr[fs.show[j]], 12}}; - mem_copy_readable(&dst, (const uint64_t (*)[2])src, 1, &ok); - if (!ok) v[j][0] = v[j][1] = v[j][2] = NAN; - } - char line[256 + FLOOR_SCAN_SHOW * 110]; - int o = snprintf(line, sizeof line, "floor scan: step %.4f frames; link %.4f %.4f %.4f moved %.5f", - (double)frame_step_frames(), link[0], link[1], link[2], move); - for (int j = 0; j < k && o > 0 && o < (int)sizeof line; j++) { - float *w = fs.val + 3 * fs.show[j]; - o += snprintf(line + o, sizeof line - o, "; %#llx at %.4f %.4f %.4f moved %.5f %.5f %.5f", - (unsigned long long)fs.addr[fs.show[j]], v[j][0], v[j][1], v[j][2], v[j][0] - w[0], - v[j][1] - w[1], v[j][2] - w[2]); - memcpy(w, v[j], 12); - } - rt_log("%s", line); + // state 2: once a second, Link and each survivor with its move over that second + uint64_t t = plat_now_ns(); + if (t - fs.sec_ns < 1000000000ull) return; + double secs = (t - fs.sec_ns) / 1e9; + fs.sec_ns = t; + float v[FLOOR_SCAN_SHOW * 3]; + floor_scan_read(fs.n, v); + rt_log("floor scan: step %.4f frames; link %.4f %.4f %.4f moved %.4f %.4f %.4f in %.3f s (%.3f m/s)", + (double)frame_step_frames(), link[0], link[1], link[2], link[0] - fs.sec_link[0], link[1] - fs.sec_link[1], + link[2] - fs.sec_link[2], secs, hdist(link, fs.sec_link) / secs); + memcpy(fs.sec_link, link, 12); floor_scan_left--; + for (int k = 0; k < fs.n && floor_scan_left > 0; k++) { + float *w = fs.val + 3 * k, *u = v + 3 * k; + rt_log("floor scan: %#llx at %.4f %.4f %.4f moved %.4f %.4f %.4f (%.3f m/s)", (unsigned long long)fs.addr[k], + u[0], u[1], u[2], u[0] - w[0], u[1] - w[1], u[2] - w[2], hdist(u, w) / secs); + memcpy(w, u, 12); + floor_scan_left--; + } + if (floor_scan_left < 0) floor_scan_left = 0; } void frame_step_present(uint64_t display_ns, int interval) {