Rift challenge timer follows the guest clock (#16, #17)

The timer no longer advances while the app is in the background or the screen is locked, or while the system keyboard is up. Loading screens, time in town and open panels still count. Start, HUD and menu readouts, the timeout check and settlement all read the same clock. Adds the host unit test t_rift_clock.

Idea and first implementation by @hubeiqiao in #17; re-implemented at the source after checking each site.

Co-authored-by: hubeiqiao <12777592+hubeiqiao@users.noreply.github.com>
This commit is contained in:
jhaizhou
2026-10-13 09:57:37 +08:00
co-authored by hubeiqiao
parent c3f981251f
commit a1eb75288c
3 changed files with 58 additions and 7 deletions
+12 -6
View File
@@ -91,7 +91,7 @@ static void say(const char *key, ...) { char line[160], full[180]; va_list ap; v
enum { ST_IDLE, ST_RUN, ST_DONE };
static struct {
_Atomic int st; int tier, maps[3], nmaps, cur; // 本次层数、串起来的图、正在第几张 | This rift's level, the chained maps, which map we are on
_Atomic int score, need; uint64_t t0_ns; _Atomic int timeout, guard_dead; // 分数(客户端加)、开局墙钟、超时、守护者已死 | Score (added by the client), wall clock at rift start, timeout, guardian dead
_Atomic int score, need; uint64_t t0_ns; _Atomic int timeout, guard_dead; // 分数(客户端加)、开局时刻(客体时钟)、超时、守护者已死 | Score (added by the client), start time (guest clock), timed out, guardian dead
uint32_t guard; int guard_cls, inc; // 守护者单位 id;成功涨了几层 | Guardian unit id; how many levels were gained on success
int players_old; // 开局前的 /players 档 | The /players setting from before the start of the run
char who[64]; // 角色名(层数侧文件) | Character name (level sidecar file)
@@ -268,11 +268,15 @@ static int in_rift_map(int lv) { for (int i = 0; i < K.nmaps; i++) if (K.maps[i]
int dm_rift_active(void) { return atomic_load(&K.st) == ST_RUN; }
// ---- 界面读数(前端每帧)
// 挑战计时一律读客体时钟 guest_ns()(hle_os.c):App 在后台 / 锁屏、系统键盘弹着的时候它不走,倒计时也就不走;
// 回城、开面板、读取画面这些游戏照常运行的时间照常计入。开局时刻不落盘(侧文件里记的是用时秒数)。公开仓库
// ---- UI readouts (front end, every frame)
// All challenge timing reads the guest clock guest_ns() (hle_os.c): it does not advance while the App is in the background / the screen is locked or while the system keyboard is up, so the countdown does not advance either;
// time during which the game keeps running -- back in town, panels open, loading screens -- counts as usual. The start time is not written to disk (the sidecar file records elapsed seconds).
static int g_left_done; // 结算时剩余秒数:打倒守护者后顶部条与面板都显示它,不再往下走 | Seconds left at completion: after the guardian is defeated both the top bar and the panel show it, and it no longer counts down
int dm_rift_hud(dm_rift_hud_t *o) {
int st = atomic_load(&K.st); if (st != ST_RUN && st != ST_DONE) return 0;
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); uint64_t now = (uint64_t)ts.tv_sec * 1000000000ull + ts.tv_nsec;
uint64_t now = guest_ns();
int el = K.t0_ns ? (int)((now - K.t0_ns) / 1000000000ull) : 0; int need = atomic_load(&K.need);
o->tier = K.tier; o->progress = need > 0 ? (float)atomic_load(&K.score) / need : 0; if (o->progress > 1) o->progress = 1;
o->secs_left = st == ST_DONE ? g_left_done : RIFT_SECS - el; o->done = st == ST_DONE; o->guardian = K.guard != 0 && !atomic_load(&K.guard_dead); o->inc = K.inc;
@@ -864,7 +868,7 @@ static void milestone(uint64_t G, uint64_t player, int m) {
R.ms |= 1u << (N - 1); say("dmRiftSayFirst", m);
}
static void finish(uint64_t G, uint64_t player, uint64_t guard) {
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); uint64_t now = (uint64_t)ts.tv_sec * 1000000000ull + ts.tv_nsec;
uint64_t now = guest_ns();
int left = RIFT_SECS - (int)((now - K.t0_ns) / 1000000000ull); g_left_done = left; // 结算这一拍停表 | Stop the clock on the completion tick
K.inc = left >= 600 ? 3 : left >= 300 ? 2 : left > 0 ? 1 : 0; int next = K.tier + K.inc; if (next > 100) next = 100; if (!K.inc) next = K.tier;
if (K.inc) { R.clears++; if (K.tier > R.best) R.best = K.tier; } if (K.inc && (!R.fast[K.tier] || RIFT_SECS - left < R.fast[K.tier])) R.fast[K.tier] = RIFT_SECS - left;
@@ -939,7 +943,7 @@ static void finish(uint64_t G, uint64_t player, uint64_t guard) {
// The portal at the Guardian's spot uses this number (same source as the completion log and the "Next: level N" on the quest bar)
static int next_tier(void) { int nt = K.inc ? K.tier + K.inc : K.tier; return nt > 100 ? 100 : nt; }
static void menu_fill(dm_rift_menu_t *o, int m) {
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); uint64_t now = (uint64_t)ts.tv_sec * 1000000000ull + ts.tv_nsec;
uint64_t now = guest_ns();
int need = atomic_load(&K.need); o->menu = m; o->best = R.best; o->unlock = R.unlock < 1 ? 1 : R.unlock; o->def = R.tier; o->ms = R.ms; o->clvl = atomic_load(&M.clvl);
o->tier = K.tier; o->done = atomic_load(&K.st) == ST_DONE; o->inc = K.inc; o->secs_left = o->done ? g_left_done : K.t0_ns ? RIFT_SECS - (int)((now - K.t0_ns) / 1000000000ull) : RIFT_SECS; if (o->secs_left < 0) o->secs_left = 0;
o->progress = need > 0 ? (float)atomic_load(&K.score) / need : 0; if (o->progress > 1) o->progress = 1;
@@ -982,7 +986,7 @@ static int start_rift(uint64_t G, uint64_t player, uint32_t t, int tier) {
atomic_fetch_add(&g_run_seq, 1);
rift_setup(G); K.cur = 0; atomic_store(&K.score, getenv("DM_RIFT_TEST_SCORE") ? atoi(getenv("DM_RIFT_TEST_SCORE")) : 0); // 测试:初始分数(看进度条) | Test: initial score (to see the progress bar)
atomic_store(&K.guard_dead, 0); atomic_store(&K.timeout, 0); K.guard = 0; K.inc = 0;
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); K.t0_ns = (uint64_t)ts.tv_sec * 1000000000ull + ts.tv_nsec;
K.t0_ns = guest_ns();
if (getenv("DM_RIFT_TEST_LATE")) { K.t0_ns -= (uint64_t)RIFT_SECS * 1000000000ull; LOG("秘境:测试 DM_RIFT_TEST_LATE 开局即超时"); } // 造「超时打倒守护者」现场 | set up the "Guardian defeated after time ran out" test case
K.players_old = dm_hud_requested_players(); if (!K.players_old) K.players_old = 1;
dm_quiet_difficulty_msg(1); // 本局原版「游戏难度设为 N」提示不弹(观测门 15,diag.c;换局关) | The original 「游戏难度设为 N」(Game difficulty set to N) notice is not shown this run (observation gate 15, diag.c; turned off on run change)
@@ -1035,6 +1039,8 @@ void dm_rift_tick(uint64_t G, uint64_t player, uint8_t *main_base) {
if (lv == LV_TOWN && !L.door && !L.door_tried) { L.door_tried = 1; L.door = make_rift_door(G, player); if (!L.door) L.door_tried = 0; }
if (L.eligible > 0) atomic_store(&M.clvl, gs(player, 12));
if (L.resume && L.eligible > 0 && lv == LV_TOWN && t > 50) { int tq = L.resume; L.resume = 0; LOG("秘境:一键重开后的新局 → 直接开第 %d 层", tq); start_rift(G, player, t, tq); return; }
{ static int at = -1, tr; if (at < 0) { const char *e = getenv("DM_RIFT_TEST_START"); at = e ? atoi(e) : 0; const char *c = e ? strchr(e, ':') : NULL; tr = c ? atoi(c + 1) : 1; } // 测试开关 DM_RIFT_TEST_START=<拍>[:<层>]:人在哈洛加斯时到这一拍直接开这一层(裸宿主上选层页点不了;走的是一键重开后续局的同一句) | Test switch DM_RIFT_TEST_START=<tick>[:<floor>]: while the player is in Harrogath, start that floor directly at that tick (the floor selection page cannot be tapped on the bare host; this goes through the same statement as the follow-up run after a quick restart)
if (at > 0 && t >= (uint32_t)at && lv == LV_TOWN && L.eligible > 0 && atomic_load(&K.st) == ST_IDLE) { at = 0; LOG("秘境:测试 DM_RIFT_TEST_START 拍 %u 直接开第 %d 层", t, tr); start_rift(G, player, t, tr); return; } }
{ static int at = -1; if (at < 0) { const char *e = getenv("DM_RIFT_TEST_RESTART"); at = e ? atoi(e) : 0; } if (at > 0 && t >= (uint32_t)at) { at = 0; void dm_restart_request(void); LOG("秘境:测试 DM_RIFT_TEST_RESTART 拍 %u 请求一键重开", t); dm_restart_request(); } } // 测试:拍 N 替玩家一键重开 | Test: at tick N, do a one-tap restart on the player's behalf
if (lv != LV_TOWN && atomic_load(&M.menu)) atomic_store(&M.menu, MENU_NONE); // 离城自动收起 | Closes automatically on leaving town
{ int tq = atomic_exchange(&M.req_start, 0); if (tq > 0 && lv == LV_TOWN && st != ST_RUN) { if (tq > R.unlock) tq = R.unlock; if (tq < 1) tq = 1; LOG("秘境:选层页开始第 %d 层", tq); L.next_door = L.town_door = 0; start_rift(G, player, t, tq); return; } }
@@ -1139,7 +1145,7 @@ void dm_rift_tick(uint64_t G, uint64_t player, uint8_t *main_base) {
int ok = dm_teleport(B, G, player, rr, pos[0], pos[1]); static int ln; if (ln++ < 5) LOG("秘境:测试拉人 → %d,%d(最近的怪 %d 格)%s", pos[0], pos[1], bd, ok ? "成" : "不成"); } } }
// 计时
// Timer
{ struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); uint64_t now = (uint64_t)ts.tv_sec * 1000000000ull + ts.tv_nsec;
{ uint64_t now = guest_ns();
if (!atomic_load(&K.timeout) && now - K.t0_ns > (uint64_t)RIFT_SECS * 1000000000ull) { atomic_store(&K.timeout, 1); say("dmRiftSayTimeout"); } }
// 守护者
// Guardian
+4 -1
View File
@@ -4,11 +4,12 @@ DM_ROOT ?= $(HOME)/dm-work
PY ?= $(DM_ROOT)/build/cache/dm/venv/bin/python
EXEFS ?= $(DM_ROOT)/data/switch/3.3.94012/Update/ExeFS
B = build
run: $(B)/t_textbox $(B)/t_padmap $(B)/t_touch $(B)/t_hid_receipt $(B)/t_hid_script $(B)/t_touch_mac $(B)/t_oe $(B)/t_fs $(B)/t_fs_case $(B)/t_romfs_validate $(B)/t_audio $(B)/t_audio_spsc $(B)/t_swkbd $(B)/t_altstack $(B)/t_movie $(B)/t_condvar $(B)/t_condvar_old $(B)/t_target_rank $(B)/t_hud_dead $(B)/t_namegen $(B)/t_modetx $(B)/t_unit_level $(B)/t_widget $(B)/t_doorspot $(B)/t_cloudsync $(B)/t_i18n $(B)/main.orig.bin $(B)/subsdk0.orig.bin
run: $(B)/t_rift_clock $(B)/t_textbox $(B)/t_padmap $(B)/t_touch $(B)/t_hid_receipt $(B)/t_hid_script $(B)/t_touch_mac $(B)/t_oe $(B)/t_fs $(B)/t_fs_case $(B)/t_romfs_validate $(B)/t_audio $(B)/t_audio_spsc $(B)/t_swkbd $(B)/t_altstack $(B)/t_movie $(B)/t_condvar $(B)/t_condvar_old $(B)/t_target_rank $(B)/t_hud_dead $(B)/t_namegen $(B)/t_modetx $(B)/t_unit_level $(B)/t_widget $(B)/t_doorspot $(B)/t_cloudsync $(B)/t_i18n $(B)/main.orig.bin $(B)/subsdk0.orig.bin
python3 native_write_scan.py --selftest ..
python3 mode_scan.py ../..
python3 ../../ios/tools/dm_i18n.py --check
python3 ../../i18n/scan_literals.py
$(B)/t_rift_clock
$(B)/t_textbox
$(B)/t_touch
$(B)/t_hid_script 2>/dev/null
@@ -102,3 +103,5 @@ $(B)/t_touch_mac: t_touch_mac.c ../touch_mac.c ../dm_cursor.c ../dm_hud.c ../dm_
clang -std=c11 -Wall -Wextra -Werror -I.. -I../../ios/Sources -o $@ t_touch_mac.c ../dm_cursor.c ../dm_hud.c -lm
$(B)/t_target_rank: t_target_rank.c ../hle_target.c ../dm.h | $(B)
clang -O1 -Wno-unused-function -I.. -o $@ t_target_rank.c ../hle_target.c
$(B)/t_rift_clock: t_rift_clock.c ../dm_rift.c ../dm_rift.h ../hle_os.c ../hle.h ../dm.h | $(B)
clang -O1 -Wall -Wno-unused-function -ffunction-sections -fdata-sections -Wl,-dead_strip -I.. -o $@ t_rift_clock.c ../hle_os.c
+42
View File
@@ -0,0 +1,42 @@
// 秘境计时的单测(公开仓库):直接包含真的 dm_rift.c、链接真的客体时钟 hle_os.c,不调任何原版地址。
// 要证明的事:倒计时读的是客体时钟——冻结(后台 = 原因 1、系统键盘 = 原因 2)期间 HUD 与菜单的剩余秒数一秒不少,放开后照墙钟走。
// 期望值都不靠被测实现算:开局时刻摆在 120 秒前 ⇒ 剩余 = 900 − 120 = 780;冻结 N 秒前后差 0;放开后睡 2 秒 ⇒ 少 2 秒。
// 读数取整到秒、睡眠有抖动,所以「照墙钟走」那几项允许差 1 秒;「冻结期间不走」那几项必须一秒不差。
// 不覆盖:真机锁屏、超时判负与结算(那两处在 dm_rift_tick / finish 里,要真局)。
// Unit test of the rift timer: includes the real dm_rift.c directly and links the real guest clock hle_os.c, calling no original-game addresses.
// What it proves: the countdown reads the guest clock -- during a freeze (background = reason 1, system keyboard = reason 2) the remaining seconds on the HUD and in the menu do not drop by a single second, and after release they follow the wall clock.
// No expected value is computed by the implementation under test: the start time is placed 120 seconds in the past => remaining = 900 - 120 = 780; before and after a freeze of N seconds the difference is 0; after release, sleeping 2 seconds => 2 seconds less.
// Readouts are rounded to seconds and sleep has jitter, so the "follows the wall clock" items allow a difference of 1 second; the "does not advance while frozen" items must match to the second.
// Not covered: locking the screen on a real device, the timeout verdict and settlement (those two are in dm_rift_tick / finish and need a real run).
#include "../dm_rift.c"
void (*g_dm_pre_commit_hook[4])(const char *);
uint64_t dm_fs_commit_count(void) { return 0; }
static int fails, checks;
#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf(" ✗ "); printf(__VA_ARGS__); printf("\n"); } } while (0)
static void nap(long ms) { struct timespec d = { ms / 1000, (ms % 1000) * 1000000L }, r; while (nanosleep(&d, &r)) d = r; }
static void left(int *hud, int *menu) { dm_rift_hud_t h = {0}; dm_rift_menu_t m = {0}; if (!dm_rift_hud(&h)) h.secs_left = -9999; menu_fill(&m, 2); *hud = h.secs_left; *menu = m.secs_left; }
int main(void) {
unsetenv("DM_DETCLOCK");
int h0, m0, h, m;
K.t0_ns = guest_ns() - 120ull * 1000000000ull; atomic_store(&K.st, ST_RUN);
left(&h0, &m0); CHECK((h0 == 780 || h0 == 779) && m0 == h0, "开局 120 秒后剩余应为 780(差 1 秒内),HUD %d 菜单 %d", h0, m0);
// 进后台:冻结 1.3 秒,前后一秒不差
// Enter the background: frozen for 1.3 seconds, not a second of difference before and after
guest_clock_freeze_for(1, 1); left(&h0, &m0); nap(1300); left(&h, &m);
CHECK(h == h0 && m == m0, "后台 1.3 秒,剩余不该变:HUD %d → %d,菜单 %d → %d", h0, h, m0, m);
// 后台期间键盘也弹着;先回前台,键盘还在 ⇒ 仍然不走
// The keyboard is also up while in the background; return to the foreground first with the keyboard still up => still not advancing
guest_clock_freeze_for(2, 1); guest_clock_freeze_for(1, 0); nap(1300); left(&h, &m);
CHECK(h == h0 && m == m0, "回前台但键盘还弹着 1.3 秒,剩余不该变:HUD %d → %d,菜单 %d → %d", h0, h, m0, m);
// 全部放开的那一刻不跳;之后照墙钟走:睡 2.2 秒 ⇒ 少 2 秒(取整抖动差 1 秒内)
// No jump at the moment everything is released; afterwards it follows the wall clock: sleeping 2.2 seconds => 2 seconds less (rounding jitter within 1 second)
guest_clock_freeze_for(2, 0); left(&h, &m); CHECK(h == h0 && m == m0, "放开那一刻不该跳:HUD %d → %d", h0, h);
nap(2200); left(&h, &m); CHECK(h0 - h >= 2 && h0 - h <= 3 && m == h, "放开后 2.2 秒应少 2 秒(差 1 秒内):HUD %d → %d,菜单 %d", h0, h, m);
// 已结算:两处都停在结算那一刻的读数
// Settled: both readouts stay at the value of the moment of settlement
g_left_done = 321; atomic_store(&K.st, ST_DONE); left(&h, &m); CHECK(h == 321 && m == 321, "结算后应停在 321:HUD %d 菜单 %d", h, m);
printf("t_rift_clock:%d 项,%d 项不通\n", checks, fails); return fails != 0;
}