mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-21 04:55:56 +00:00
ZL+ZR+L3 detaches the view from Link on 1.4.3 and flies it with the gamepad (freecam_pad)
The camera component update (main+0x628460) copies each component's eye, target and up into its sead camera. The site freecam (main+0x6286d4, the load every path to that copy runs first) calls runtime/freecam.c freecam_hook, which writes the component's one-shot eye and target overrides (+0x180, +0x198) while the free camera is on; the update takes them in place of the pose and clears them, so the game camera's own state is never changed and resumes the frame the free camera ends. The up vector is levelled through the component's up offset (+0x200), which is kept and put back. Of the three components a frame updates, the hook drives the two that share the player camera's eye, leaves the one at the default pose and picks again after a load; with the free camera off it does only the displaced load. input.c freecam_gate latches TOTK_FREECAM_PAD (zl+zr+lstick when unset, none for no combo) like Minus+Plus and, while the free camera is on, gives it the sticks, the trigger axes and L and R presses and the game nothing. The left stick flies along the view and sideways, the triggers straight up and down, the right stick turns, each L or R press halves or doubles the speed (0.25 to 512 m/s, 8 at the start); the flight follows the wall clock. TOTK_FREECAM_PATCH=0 removes the hook. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94c33b1d85
commit
29eec07245
9 files changed
+353
-4
No files matched your search
@@ -129,6 +129,31 @@ bind_X = pad:west, key:space
|
||||
bind_Y = pad:north, key:mouse_l
|
||||
```
|
||||
|
||||
## Free camera
|
||||
|
||||
On game 1.4.3, ZL+ZR+L3 together (LT+RT+L3 on an Xbox pad; `freecam_pad` in `totk.ini` picks another combo, `none` none)
|
||||
detach the view from Link; the same combo brings it back. The world goes on, but the game gets no gamepad input while the
|
||||
free camera is on, so Link stands where he was.
|
||||
|
||||
| Input | Free camera |
|
||||
|---|---|
|
||||
| Left stick | forward and back along the view, and sideways |
|
||||
| ZR / ZL (RT / LT) | straight up / down, by how far the trigger is pressed |
|
||||
| Right stick | turn (yaw and pitch; pitch stops 5 degrees short of straight up or down) |
|
||||
| R / L (RB / LB) | each press doubles / halves the speed, 0.25 to 512 m/s |
|
||||
|
||||
The keys that drive the sticks (W A S D and the arrow keys by default) move it too. Each switch starts at the game
|
||||
camera's position and view at 8 m/s; the speed follows the clock, so it is the same at any frame rate and game speed.
|
||||
The horizon stays level.
|
||||
|
||||
How it works (`runtime/freecam.c`): the camera component update (main+0x628460) copies each component's eye, target
|
||||
and up into its sead camera. The site `freecam` (`recomp/patches-v143.txt`, main+0x6286d4) runs before that copy and
|
||||
writes the component's one-shot eye and target overrides (+0x180, +0x198), which the update takes in place of the pose
|
||||
and then clears, so the game camera's own state is never changed and resumes the frame the free camera ends. The up
|
||||
vector is levelled through the component's up offset (+0x200), which is kept and put back. Of the three components a
|
||||
frame updates, the free camera drives the two that share the player camera's eye and leaves the one at the default pose
|
||||
(eye 0, 0, 10). After a load replaced the components it picks the new ones and keeps its position.
|
||||
|
||||
## Mouse camera
|
||||
|
||||
`mouse_mode = direct` (the default) turns the camera by the raw mouse motion on games 1.4.3 and 1.2.1, through hooks in the game's camera code (`runtime/mouse_direct.c`, the `mouse_direct` sites of `recomp/patches-v143.txt` and `recomp/patches-v121.txt`). None of them changes a step of the gamepad alone. The free-orbit camera receives raw-count
|
||||
|
||||
@@ -231,6 +231,7 @@ These names are code patch sets built into the generated game code (`recomp/patc
|
||||
| `aspect_patch` (default `0`) | Patch set that feeds the camera the output's aspect ratio when it is not 16:9. | The resolution code switches this set on or off itself at start, so a value here is replaced; the code does not settle what a manual value does. | Unknown |
|
||||
| `audio_drain_fix` (default `1`) | Patch set that sends the game's audio thread back to its command ring after each list (1.2.1 main+0x883780, 1.4.3 main+0x149244), so it takes every queued list per 5 ms audio frame instead of one (#337). Game 1.1.0 has no site and keeps the game's code. | `0` runs the game's code as shipped: above about 200 game updates a second the ring stays full and the game's guard (1.2.1 main+0x9bcdc4) switches the sound commands off until the game is restarted. The set acts only when more than one list is queued, so below that rate it seldom changes anything. Measured at `fps_limit = 240`, seconds with sound: on 1.2.1 5 of 118 with `0` and 108 of 118 with `1`; on 1.4.3 2 of 118 with `0` and 110 of 119 with `1`. | Safe |
|
||||
| `fixed_res_patch` (default `0`) | Patch set that holds the game's dynamic resolution at 100 percent (the scene tier). | The resolution code switches this set from `dynamic_res` (on at `0`); a manual value is replaced. | Unknown |
|
||||
| `freecam_patch` (default `1`) | Patch set of the hook in the camera component update behind the free camera (`freecam_pad`) on game 1.4.3. | `0` removes the hook: the free camera combo does nothing and the game gets the combo's buttons. | Safe |
|
||||
| `fov_patch` (default `0`) | Patch set that scales the camera's vertical view angle by the field-of-view setting. | The resolution code switches this set itself whenever the field of view is not 100 percent; a manual value is replaced. | Unknown |
|
||||
| `mouse_direct_patch` (default `1`) | Patch set of the hooks in the game's camera code behind `mouse_mode = direct` on game 1.4.3. | `0` stops `mouse_mode = direct` from taking effect; other mouse modes are unaffected. | Safe |
|
||||
| `optref_guard` (default `1`) | Patch set that treats a negative value in the optional reference at object +0x6b0 (1.4.3 main+0x1bd01c0, 1.2.1 main+0x1622bec) as absent instead of loading through it (#1005). | `0` runs the game's code as shipped: the random crash reading `0xffffffffffffffff` after minutes to hours of play can return. | Risky |
|
||||
|
||||
@@ -128,6 +128,14 @@ set fov TOTK_FOV_PATCH 0
|
||||
site fov main 628c3c bd413e60 f81f0fea @page:x10 bd413e60 bd403d48 1e280800 f84107ea # ldr s0, [x19, #0x13c] (camera update)
|
||||
site fov main b4342c bd4066c0 f81f0fea @page:x10 bd4066c0 bd403d48 1e280800 f84107ea # ldr s0, [x22, #0x64] (camera setup)
|
||||
|
||||
# Free camera (runtime/freecam.c freecam_hook, page +0xf50). The camera component update (main+0x628460, the routine of
|
||||
# the first fov and aspect sites) copies the component's eye, target and up into its sead camera; every path to that
|
||||
# copy runs this load of the component's link first. The hook performs the load and, while the free camera is on,
|
||||
# writes the component's one-shot eye and target overrides (+0x180, +0x198). The function saves x30 in its prologue, so
|
||||
# the bl's link write is harmless.
|
||||
set freecam TOTK_FREECAM_PATCH 1
|
||||
site freecam main 6286d4 f9400e68 94d2de1f # ldr x8, [x19, #0x18]
|
||||
|
||||
# The camera input update, after its stick dead zone, unit-radius clamp and StickExponent. The host callback
|
||||
# performs the displaced store and adds raw mouse counts to s9/s10 (runtime/mouse_direct.c camera_input).
|
||||
# Its address is page +0xfd0; the page is main+0x3adf000 on this verified build.
|
||||
|
||||
+2
-2
@@ -43,7 +43,7 @@ static const char *const known[] = {
|
||||
"COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT",
|
||||
"DESC_TEMPLATE", "DESWIZZLE", "DEVICE_PROFILE", "DIALOGUE_CLICK", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_JITTER", "DLAA_EFFECTS_AFTER", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_FIXED_EXPOSURE", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR",
|
||||
"DUMP_LOG", "DYNAMIC_RES", "DYN_DELTA", "EXEFS", "FEEDBACK", "FIXED_RES_PATCH", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES",
|
||||
"FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAME_QUEUE",
|
||||
"FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAME_QUEUE", "FREECAM_PAD", "FREECAM_PATCH",
|
||||
"FREEZE_IDLE", "FREE_CORES",
|
||||
"FULLSCREEN", "FXAA",
|
||||
"GAMEPLAY_DISTANCE_SENSITIVITY", "GAMEPLAY_ITEM_AD", "GAMEPLAY_ITEM_SENSITIVITY", "GAMEPLAY_ITEM_WHEEL",
|
||||
@@ -99,7 +99,7 @@ static const struct { const char *key, *section; } sections[] = {
|
||||
{"GYRO_BIAS_X", "input"}, {"GYRO_BIAS_Y", "input"}, {"GYRO_BIAS_Z", "input"}, {"GYRO_BIAS_PAD", "input"},
|
||||
{"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"},
|
||||
{"STICK_SENS", "input"},
|
||||
{"MENU_CURSOR", "input"}, {"MAP_CLICK", "input"}, {"DIALOGUE_CLICK", "input"}, {"POINTER_IMAGE", "input"}, {"QUICKSAVE", "input"}, {"QUICKSAVE_KEY", "input"}, {"QUICKLOAD_KEY", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"MOUSE_AIM_SCALE", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"},
|
||||
{"MENU_CURSOR", "input"}, {"MAP_CLICK", "input"}, {"DIALOGUE_CLICK", "input"}, {"POINTER_IMAGE", "input"}, {"QUICKSAVE", "input"}, {"QUICKSAVE_KEY", "input"}, {"QUICKLOAD_KEY", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"MOUSE_AIM_SCALE", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"FREECAM_PAD", "input"}, {"INPUT_RECORD", "input"},
|
||||
{"BIND_A", "input"}, {"BIND_B", "input"}, {"BIND_X", "input"}, {"BIND_Y", "input"}, {"BIND_LCLICK", "input"},
|
||||
{"BIND_RCLICK", "input"}, {"BIND_L", "input"}, {"BIND_R", "input"}, {"BIND_ZL", "input"}, {"BIND_ZR", "input"},
|
||||
{"BIND_PLUS", "input"}, {"BIND_MINUS", "input"}, {"BIND_LEFT", "input"}, {"BIND_UP", "input"},
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
// Free camera for TotK 1.4.3: the view leaves Link and flies with the gamepad while the game goes on (docs/controls.md,
|
||||
// "Free camera").
|
||||
//
|
||||
// The camera component update (main+0x628460, called with the component as x0 = x19 from main+0x627dc8, the camera
|
||||
// actor's component array +0x228, slot +0x270) copies the component's pose into its sead camera and builds the view
|
||||
// matrix from it. Component fields (doubles unless noted): eye +0xe8, target +0x110, up +0x128 (floats), then near,
|
||||
// far, vertical view angle and aspect +0x134..+0x140 (the fov and aspect sets of recomp/patches-v143.txt load the last
|
||||
// two). Two one-shot overrides come after: a non-zero eye at +0x180 or target at +0x198 replaces the pose for that
|
||||
// update and is cleared (main+0x628fdc, main+0x629004). Additive offsets follow at +0x1b0 (flag) / +0x1c0 (eye),
|
||||
// +0x1d8 / +0x1e8 (target) and +0x200 (up, three floats, applied when any is non-zero and never cleared by the game).
|
||||
//
|
||||
// The freecam set's one site (main+0x6286d4, `ldr x8, [x19, #0x18]`, the first instruction every path to the pose
|
||||
// copy runs) calls freecam_hook, which performs that load and, while the free camera is on, writes the overrides.
|
||||
// The game's own eye and target stay as they were, so the game camera takes over the update after the hook stops.
|
||||
// The up offset is the game's state: the hook keeps the component's value and puts it back when the free camera ends,
|
||||
// and makes the up vector the world's +Y meanwhile, so the horizon stays level.
|
||||
//
|
||||
// Which components: a 1.4.3 frame updates three. Two share the player camera's pose (both must move, or the scene
|
||||
// and what follows the second disagree); the third rests at the default pose, eye (0, 0, 10) towards the origin. The
|
||||
// first component seen away from that default anchors the pick, and every component whose eye is within MATCH_M of
|
||||
// the anchor is driven. When no driven component has been updated for LOST_NS (a load replaced them) the next one
|
||||
// away from the default anchors again; the free camera keeps its own position.
|
||||
//
|
||||
// Input (runtime/input.c freecam_gate): TOTK_FREECAM_PAD switches it, ZL+ZR+L3 by default. While it is on the game
|
||||
// gets no gamepad input; the left stick flies along the view and to the side, the triggers straight up and down, the
|
||||
// right stick turns, and each press of L or R halves or doubles the speed. Every driven component steps the flight by
|
||||
// the wall-clock time since the last step, so the speed is the same at any frame rate and game speed.
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cfg.h"
|
||||
#include "host.h"
|
||||
#include "input.h"
|
||||
#include "platform/platform.h"
|
||||
|
||||
#if __has_include("patches.h")
|
||||
#include "patches.h"
|
||||
#endif
|
||||
|
||||
#define SPEED_START 8.0 // metres a second at full deflection
|
||||
#define SPEED_MIN 0.25 // 8 m/s halved five times
|
||||
#define SPEED_MAX 512.0 // 8 m/s doubled six times
|
||||
#define TURN_DEG_S 110.0 // the right stick's turn rate at full deflection
|
||||
#define PITCH_MAX_DEG 85.0 // short of straight up or down, where the view's up vector would fail
|
||||
#define TARGET_M 10.0 // the target's distance in front of the eye
|
||||
#define MATCH_M 2.0
|
||||
#define LOST_NS 500000000ull
|
||||
#define STEP_MAX_S 0.1 // a longer gap (a load, a hitch) moves the camera no further than this
|
||||
#define MAX_DRIVEN 8
|
||||
|
||||
static PlatMutex pad_lock = PLAT_MUTEX_INIT;
|
||||
static FreecamPad pad_now; // the last poll's input (input thread), under pad_lock
|
||||
static int speed_clicks; // R presses minus L presses not yet applied (input thread adds, the hook takes)
|
||||
// + 1 at every switch (input thread); odd while the free camera is on. One value, so the hook never sees a switch
|
||||
// without the state it switched to.
|
||||
static unsigned generation;
|
||||
|
||||
int freecam_on(void) { return __atomic_load_n(&generation, __ATOMIC_ACQUIRE) & 1; }
|
||||
|
||||
void freecam_pad(const FreecamPad *p) {
|
||||
plat_mutex_lock(&pad_lock);
|
||||
pad_now = *p;
|
||||
plat_mutex_unlock(&pad_lock);
|
||||
}
|
||||
|
||||
void freecam_speed_click(int faster) { __atomic_add_fetch(&speed_clicks, faster ? 1 : -1, __ATOMIC_RELAXED); }
|
||||
|
||||
#ifdef RT_PATCH_PAGE
|
||||
#define HOST_FREECAM (RT_PATCH_PAGE + 0xf50)
|
||||
|
||||
enum {
|
||||
OFF_LINK = 0x18, OFF_EYE = 0xe8, OFF_TARGET = 0x110, OFF_UP = 0x128,
|
||||
OFF_EYE_OVERRIDE = 0x180, OFF_TARGET_OVERRIDE = 0x198, OFF_UP_OFFSET = 0x200,
|
||||
SPAN = OFF_UP_OFFSET + 12 - OFF_EYE, // the fields the hook reads and writes, from OFF_EYE
|
||||
};
|
||||
|
||||
int freecam_available(void) { return rt_game_version == 143 && rt_patch_enabled(rt_patch_set("freecam")); }
|
||||
|
||||
void freecam_toggle(void) {
|
||||
unsigned gen = __atomic_add_fetch(&generation, 1, __ATOMIC_ACQ_REL);
|
||||
rt_log("freecam: %s", gen & 1 ? "on" : "off");
|
||||
}
|
||||
|
||||
// Game thread only (the camera update runs on one thread). A component stays listed from its first drive until its
|
||||
// up offset is put back (the first update after the switch goes off) or, while off, until it has gone LOST_NS
|
||||
// without an update (freed by a load).
|
||||
static struct {
|
||||
unsigned generation;
|
||||
int started; // pos, yaw and pitch hold a pose
|
||||
double pos[3], yaw, pitch, speed; // radians; yaw 0 looks along +Z, pitch > 0 up
|
||||
uint64_t last_step, anchor_seen;
|
||||
int anchored; // anchor holds the eye of the components being driven
|
||||
double anchor[3];
|
||||
int n;
|
||||
struct { uint64_t cam, seen; float up_offset[3]; } driven[MAX_DRIVEN];
|
||||
} fc;
|
||||
|
||||
static void read3(uint64_t at, double v[3]) { memcpy(v, (const void *)(uintptr_t)at, 3 * sizeof(double)); }
|
||||
static void write3(uint64_t at, const double v[3]) { memcpy((void *)(uintptr_t)at, v, 3 * sizeof(double)); }
|
||||
|
||||
static int at_default(const double eye[3], const double target[3]) {
|
||||
return eye[0] == 0 && eye[1] == 0 && eye[2] == 10 && target[0] == 0 && target[1] == 0 && target[2] == 0;
|
||||
}
|
||||
|
||||
static void direction(double d[3]) {
|
||||
d[0] = sin(fc.yaw) * cos(fc.pitch);
|
||||
d[1] = sin(fc.pitch);
|
||||
d[2] = cos(fc.yaw) * cos(fc.pitch);
|
||||
}
|
||||
|
||||
static int driven_index(uint64_t cam) {
|
||||
for (int i = 0; i < fc.n; i++)
|
||||
if (fc.driven[i].cam == cam) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void drop(int i) { fc.driven[i] = fc.driven[--fc.n]; }
|
||||
|
||||
// The free camera starts where the game camera is: its eye, looking at its target.
|
||||
static void start(const double eye[3], const double target[3], uint64_t now) {
|
||||
double d[3] = {target[0] - eye[0], target[1] - eye[1], target[2] - eye[2]};
|
||||
double len = sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
|
||||
memcpy(fc.pos, eye, sizeof fc.pos);
|
||||
fc.yaw = len > 1e-6 ? atan2(d[0], d[2]) : 0;
|
||||
fc.pitch = len > 1e-6 ? asin(fmax(-1.0, fmin(1.0, d[1] / len))) : 0;
|
||||
fc.started = 1;
|
||||
fc.last_step = now;
|
||||
rt_log("freecam: starting at (%.1f %.1f %.1f)", eye[0], eye[1], eye[2]);
|
||||
}
|
||||
|
||||
// A new component joins while it shares the anchor's eye; the first one away from the default pose sets the anchor.
|
||||
static int pick(uint64_t cam, const double eye[3], const double target[3]) {
|
||||
if (at_default(eye, target)) return -1;
|
||||
if (fc.anchored) {
|
||||
double dx = eye[0] - fc.anchor[0], dy = eye[1] - fc.anchor[1], dz = eye[2] - fc.anchor[2];
|
||||
if (dx * dx + dy * dy + dz * dz > MATCH_M * MATCH_M) return -1;
|
||||
} else {
|
||||
memcpy(fc.anchor, eye, sizeof fc.anchor);
|
||||
fc.anchored = 1;
|
||||
}
|
||||
if (fc.n == MAX_DRIVEN) { // full after several loads: the one unseen longest was freed by a load
|
||||
int oldest = 0;
|
||||
for (int k = 1; k < fc.n; k++)
|
||||
if (fc.driven[k].seen < fc.driven[oldest].seen) oldest = k;
|
||||
drop(oldest);
|
||||
}
|
||||
int i = fc.n++;
|
||||
fc.driven[i].cam = cam;
|
||||
memcpy(fc.driven[i].up_offset, (const void *)(uintptr_t)(cam + OFF_UP_OFFSET), sizeof fc.driven[i].up_offset);
|
||||
rt_log("freecam: driving camera component %#llx", (unsigned long long)cam);
|
||||
return i;
|
||||
}
|
||||
|
||||
static double axis(float v) { return v > 1 ? 1 : v < -1 ? -1 : v; }
|
||||
|
||||
// Flight since the last step: L and R presses scale the speed, the right stick turns, the left stick moves along the
|
||||
// view and to the side, the triggers along the world's up.
|
||||
// Every driven component steps; the second one of a frame comes microseconds after the first and moves that little.
|
||||
static void step(uint64_t now) {
|
||||
double dt = (now - fc.last_step) / 1e9;
|
||||
fc.last_step = now;
|
||||
if (dt <= 0) return;
|
||||
if (dt > STEP_MAX_S) dt = STEP_MAX_S;
|
||||
FreecamPad p;
|
||||
plat_mutex_lock(&pad_lock);
|
||||
p = pad_now;
|
||||
plat_mutex_unlock(&pad_lock);
|
||||
int clicks = __atomic_exchange_n(&speed_clicks, 0, __ATOMIC_RELAXED);
|
||||
if (clicks) {
|
||||
fc.speed *= exp2(clicks);
|
||||
fc.speed = fc.speed < SPEED_MIN ? SPEED_MIN : fc.speed > SPEED_MAX ? SPEED_MAX : fc.speed;
|
||||
}
|
||||
double turn = TURN_DEG_S * M_PI / 180 * dt, limit = PITCH_MAX_DEG * M_PI / 180;
|
||||
fc.yaw = remainder(fc.yaw - axis(p.rx) * turn, 2 * M_PI); // right turns towards -yaw (see right[] below)
|
||||
fc.pitch += axis(p.ry) * turn;
|
||||
fc.pitch = fc.pitch > limit ? limit : fc.pitch < -limit ? -limit : fc.pitch;
|
||||
double d[3], right[3];
|
||||
direction(d);
|
||||
// world +Y up, right-handed: right = d x Y, level
|
||||
double rl = sqrt(d[0] * d[0] + d[2] * d[2]);
|
||||
right[0] = -d[2] / rl, right[1] = 0, right[2] = d[0] / rl;
|
||||
double fwd = axis(p.ly), side = axis(p.lx), lift = axis(p.rt) - axis(p.lt), dist = fc.speed * dt;
|
||||
for (int k = 0; k < 3; k++) fc.pos[k] += (d[k] * fwd + right[k] * side) * dist;
|
||||
fc.pos[1] += lift * dist;
|
||||
}
|
||||
|
||||
static void drive(uint64_t cam) {
|
||||
double d[3], target[3];
|
||||
direction(d);
|
||||
for (int k = 0; k < 3; k++) target[k] = fc.pos[k] + d[k] * TARGET_M;
|
||||
write3(cam + OFF_EYE_OVERRIDE, fc.pos);
|
||||
write3(cam + OFF_TARGET_OVERRIDE, target);
|
||||
// up + offset = +Y: the game adds the offset to its up vector before it builds the view
|
||||
float up[3], offset[3];
|
||||
memcpy(up, (const void *)(uintptr_t)(cam + OFF_UP), sizeof up);
|
||||
offset[0] = -up[0], offset[1] = 1.0f - up[1], offset[2] = -up[2];
|
||||
if (offset[0] == 0 && offset[1] == 0 && offset[2] == 0) offset[1] = 1e-6f; // all zero would skip the offset
|
||||
memcpy((void *)(uintptr_t)(cam + OFF_UP_OFFSET), offset, sizeof offset);
|
||||
}
|
||||
|
||||
// The site's own instruction first: x8 = [x19 + 0x18]. x19 is the camera component (read only: the lifter keeps
|
||||
// X19-X29 in locals across a host call).
|
||||
static void freecam_hook(Cpu *c) {
|
||||
uint64_t cam = c->x[19];
|
||||
memcpy(&c->x[8], (const void *)(uintptr_t)(cam + OFF_LINK), sizeof c->x[8]);
|
||||
unsigned gen = __atomic_load_n(&generation, __ATOMIC_ACQUIRE);
|
||||
int active = gen & 1;
|
||||
if (!active && !fc.n) return; // the common case: one load and two compares
|
||||
if (gen != fc.generation) { // a switch: a new flight starts at the game camera, at the start speed
|
||||
fc.generation = gen;
|
||||
fc.started = 0, fc.anchored = 0, fc.speed = SPEED_START;
|
||||
}
|
||||
uint64_t now = plat_now_ns();
|
||||
int i = driven_index(cam);
|
||||
if (!active) {
|
||||
if (i >= 0) {
|
||||
memcpy((void *)(uintptr_t)(cam + OFF_UP_OFFSET), fc.driven[i].up_offset, sizeof fc.driven[i].up_offset);
|
||||
drop(i);
|
||||
}
|
||||
for (int k = fc.n - 1; k >= 0; k--)
|
||||
if (now - fc.driven[k].seen > LOST_NS) drop(k); // freed by a load while it waited
|
||||
return;
|
||||
}
|
||||
if (!mem_guest_readable(cam + OFF_EYE, SPAN)) return;
|
||||
if (fc.anchored && now - fc.anchor_seen > LOST_NS) fc.anchored = 0; // a load replaced the components
|
||||
double eye[3], target[3];
|
||||
read3(cam + OFF_EYE, eye);
|
||||
read3(cam + OFF_TARGET, target);
|
||||
if (i < 0 && (i = pick(cam, eye, target)) < 0) return;
|
||||
if (!fc.started) start(eye, target, now);
|
||||
fc.driven[i].seen = fc.anchor_seen = now;
|
||||
step(now);
|
||||
drive(cam);
|
||||
}
|
||||
|
||||
__attribute__((constructor)) static void freecam_register(void) {
|
||||
if (rt_game_version == 143) rt_register_host(HOST_FREECAM, freecam_hook);
|
||||
}
|
||||
#else
|
||||
int freecam_available(void) { return 0; }
|
||||
void freecam_toggle(void) {}
|
||||
#endif
|
||||
@@ -815,6 +815,62 @@ uint32_t input_pad_combo(const char *env, const char *s) {
|
||||
return mask;
|
||||
}
|
||||
|
||||
// TOTK_FREECAM_PAD (live): the combo that switches the free camera (freecam.c); zl+zr+lstick (LT+RT+L3) when unset,
|
||||
// none for no combo. Under lock.
|
||||
static uint32_t freecam_pad_mask(void) {
|
||||
static unsigned seen;
|
||||
static int read;
|
||||
static uint32_t mask;
|
||||
unsigned v = cfg_version();
|
||||
if (read && v == seen) return mask;
|
||||
read = 1, seen = v;
|
||||
const char *s = cfg_get("TOTK_FREECAM_PAD");
|
||||
if (!s || !*s) s = "zl+zr+lstick";
|
||||
mask = strcasecmp(s, "none") ? input_pad_combo("TOTK_FREECAM_PAD", s) : 0;
|
||||
return mask;
|
||||
}
|
||||
|
||||
static float trigger_unit(int16_t v) { // 0..1 past a 5 % dead zone
|
||||
float t = v / 32767.0f;
|
||||
return t < 0.05f ? 0 : (t - 0.05f) / 0.95f;
|
||||
}
|
||||
|
||||
// The free camera combo, latched like Minus+Plus (input_map.c, chord): once every button of it is down the free
|
||||
// camera switches once, and the buttons stay hidden from the game until all are released. While the free camera is on
|
||||
// it gets the gamepad (and the keys' sticks, merged into sl and sr) and the game gets nothing, so Link stands still;
|
||||
// each press of L or R is a speed click. Off for other updates and with the freecam patch set off. Under lock.
|
||||
static void freecam_gate(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
|
||||
static int held, available = -1, shoulder[2];
|
||||
if (available < 0) available = freecam_available();
|
||||
uint32_t mask = available ? freecam_pad_mask() : 0, raw = 0;
|
||||
for (int p = PAD_SOUTH; p <= PAD_GUIDE; p++)
|
||||
if (mask >> p & 1 && pad_seen[p]) raw |= 1u << p;
|
||||
if (!mask) held = 0;
|
||||
else if (held) held = raw != 0;
|
||||
else if (raw == mask) {
|
||||
held = 1;
|
||||
freecam_toggle();
|
||||
}
|
||||
if (available && freecam_on()) {
|
||||
FreecamPad fp = {sl[0] / (float)STICK_MAX, sl[1] / (float)STICK_MAX, sr[0] / (float)STICK_MAX,
|
||||
sr[1] / (float)STICK_MAX, trigger_unit(trigger_seen[0]), trigger_unit(trigger_seen[1])};
|
||||
freecam_pad(&fp);
|
||||
for (int k = 0; k < 2; k++) {
|
||||
int down = pad_seen[k ? PAD_R : PAD_L];
|
||||
if (down && !shoulder[k]) freecam_speed_click(k);
|
||||
shoulder[k] = down;
|
||||
}
|
||||
*b = 0;
|
||||
sl[0] = sl[1] = sr[0] = sr[1] = 0;
|
||||
return;
|
||||
}
|
||||
shoulder[0] = shoulder[1] = 1; // a shoulder held when the free camera starts is no click
|
||||
if (!held) return;
|
||||
uint8_t sub[PAD_COUNT] = {0};
|
||||
for (int p = PAD_SOUTH; p < PAD_COUNT; p++) sub[p] = mask >> p & 1;
|
||||
*b &= ~input_map_pad_buttons(sub, pad_layout_get());
|
||||
}
|
||||
|
||||
int input_pad_stick_percent(double pct[2]) {
|
||||
int ok = 0;
|
||||
pct[0] = pct[1] = 0;
|
||||
@@ -935,6 +991,7 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
|
||||
static int overlay_held; // L3+R3 step the performance overlay (F10)
|
||||
int overlay = input_overlay_gate(&overlay_held, &b);
|
||||
sl[0] = st[0], sl[1] = st[1], sr[0] = st[2], sr[1] = st[3];
|
||||
freecam_gate(&b, sl, sr); // TOTK_FREECAM_PAD switches the free camera, which then takes the pad (freecam.c)
|
||||
plat_mutex_unlock(&lock);
|
||||
if (toggle) menu_toggle();
|
||||
if (overlay) menu_perf_cycle();
|
||||
|
||||
@@ -98,6 +98,15 @@ uint32_t input_pad_combo(const char *env, const char *s);
|
||||
int input_pad_read(uint64_t *buttons, int32_t *lx, int32_t *ly);
|
||||
// The gamepad layout in effect (PAD_LAYOUT_*, input_map.h; TOTK_PAD_LAYOUT, live); -1 without a gamepad. Any thread.
|
||||
int input_pad_layout(void);
|
||||
|
||||
// Free camera (freecam.c). input.c switches it with TOTK_FREECAM_PAD and, while it is on, hands it the gamepad and
|
||||
// gives the game none: sticks -1..1 with up +y after the dead zone, triggers 0..1; each L or R press is one speed click.
|
||||
typedef struct { float lx, ly, rx, ry, lt, rt; } FreecamPad;
|
||||
int freecam_available(void); // game 1.4.3 with the freecam patch set on
|
||||
int freecam_on(void);
|
||||
void freecam_toggle(void); // logs; input thread
|
||||
void freecam_pad(const FreecamPad *p);
|
||||
void freecam_speed_click(int faster); // R (1) doubles the speed, L (0) halves it
|
||||
// How far the open gamepad's left (pct[0]) and right (pct[1]) sticks are from the centre now, in percent of full
|
||||
// deflection, before the dead zone: the menu shows it on the dead zone rows, so a drifting stick's resting value can be
|
||||
// read off. 0 (and both 0) without a connected gamepad. Any thread.
|
||||
|
||||
+2
-2
@@ -201,11 +201,11 @@ LLVM_LINK_LIBS = ['-lpsapi', '-lbcrypt', '-luser32', '-lgdi32', '-ldwmapi', '-li
|
||||
DEFAULT_DEPS = ROOT / 'toolchain' / 'deps-mingw'
|
||||
# Runtime sources compiled per install, not in the library: romfs.c includes romfs_config.h, patches.c and ov_zstd.c
|
||||
# the patches.h of the player's game version (its patch page is the page after that version's main; ov_zstd.c's host
|
||||
# functions sit on it). mouse_direct.c and ov_ability.c also register host callbacks relative to that version's patch
|
||||
# functions sit on it). mouse_direct.c, ov_ability.c and freecam.c also register host callbacks relative to that version's patch
|
||||
# page. plugin_init.c reads the install's own plugin records from patches.h (RT_PLUGINS, RT_PLUGIN_WRITES): a mod plugin
|
||||
# the rebuild built in. A library source that includes one of PER_VERSION_HEADERS stops the library build
|
||||
# (library_sources).
|
||||
INSTALL_SIDE = {'romfs.c', 'patches.c', 'ov_zstd.c', 'mouse_direct.c', 'ov_ability.c', 'plugin_init.c'}
|
||||
INSTALL_SIDE = {'romfs.c', 'patches.c', 'ov_zstd.c', 'mouse_direct.c', 'ov_ability.c', 'plugin_init.c', 'freecam.c'}
|
||||
PER_VERSION_HEADERS = ('"patches.h"', '"romfs_config.h"')
|
||||
LIB_NAME = 'libtotkrt.a'
|
||||
AR = UCRT / 'llvm-ar.exe' if WIN else on_path('llvm-ar')
|
||||
|
||||
@@ -480,6 +480,11 @@
|
||||
# reads as a partial tilt only.
|
||||
# walk_key = lshift
|
||||
# walk_scale = 0.51
|
||||
# Free camera (game 1.4.3): the gamepad combo that switches it, buttons by position joined with +, the pad: names of the
|
||||
# bind_ lines. While it is on the game gets no input and the view flies: the left stick along the view and sideways,
|
||||
# the triggers straight up and down, the right stick turns; each press of L halves the speed, of R doubles it
|
||||
# (docs/controls.md, "Free camera"). none turns the combo off. Default: zl+zr+lstick (LT+RT+L3 on an Xbox pad).
|
||||
# freecam_pad = zl+zr+lstick
|
||||
# Write the input of this run to a file that replays as a script (docs/controls.md, "Scripted input"). Default: off.
|
||||
# input_record = input.txt
|
||||
# Remap buttons: the Controls tab of the settings menu (F1) writes these lines. One line per Switch button or stick
|
||||
|
||||
Reference in new issue
Block a user