# XC2 3.0.1 game fixes (recomp/patches.py), included by patches-upd.txt, which tools/fps_sites_xc2.py regenerates:
# these sets are written by hand and live here so a regeneration keeps them.

# Cloud Sea top (issue 5; NOTES.md "Cloud Sea"), on by default. main+0x70fd18 fills the scene constants the Cloud Sea
# shaders read: c3[8].w is the cloud top (the view object's +0x194, from the area's environment data), c3[10].y the
# camera height (+0x154). The fragment shader draws the inside-the-clouds fog when the top is above the camera. Where
# the floor is just under the top (Goldmouth's dock: top -8, floor -8.5), the low camera sits about 0.14 m under it and
# flips between the two looks. With the set on, a camera less than 1 m under the top gets a top 0.125 m below itself,
# so the clouds stay drawn from above; deeper than that the game's value is kept. The code after the site derives the
# plane's view-space origin (c3[8].xyz) and depth (c3[11].y) from s10, so the moved top stays consistent. s0-s3 are
# loaded again right after the site, and no flags are used.
#   ldr s10, [x22, #0x194]; ldr s0, [x22, #0x154]; fsub s1, s10, s0; fmov s2, #1.0; fmov s3, #0.125; fsub s3, s0, s3
#   fcmgt s2, s2, s1; fcmgt s1, s1, #0.0; and v1.8b, v1.8b, v2.8b; bit v10.8b, v3.8b, v1.8b
set cloudtop XC2_CLOUD_TOP_FIX 1
site cloudtop main 710250 bd4196ca bd4196ca bd4156c0 1e203941 1e2e1002 1e281003 1e233803 7ea1e442 5ea0c821 0e221c21 2ea11c6a

# The wind and HIPC sites below were found in Aegis (aegis-xc2, games/xc2/patches.py A041 and A069, MIT) and checked
# against the 3.0.1 modules.

# A wind object removed twice: main+0x1ab7c0 (manager, object) drops the object's reference when it is the manager's
# current one, which can destroy the object and take it out of its list and the broad phase, then takes it out of both
# again; the game then stops on wind.m_pProxy != nullptr. Its only call, here, goes to runtime/xc2_fixes.c
# wind_remove, the same function with the game's own checks before the second removal.
set wind XC2_WIND_GUARD 1
site wind main 1abcd8 97fffeba @host:fd0

# nn::sf's HIPC session pool (Deallocate sdk+0x525b0, and SetSessionCountImpl) signals one waiter, and only
# when a session goes back to an empty free list: two sessions returned before the first woken waiter runs wake one
# waiter, and the second sleeps with a session free (a boot hang, about 1 in 9 boots in Aegis). Broadcast instead:
# every waiter rechecks the list in the loop of Allocate (sdk+0x5253c). sdk+0x863fc is nn::os::BroadcastConditionVariable.
set hipcwake XC2_HIPC_BROADCAST 1
site hipcwake sdk 525f4 940ddbb9 9400cf82
site hipcwake sdk 52684 940ddb95 9400cf5e
