Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core2/ba/ba_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ int func_80290D48(void){


camera_node_index = func_802903CC();
// [port] False when the port's camera keeps (or takes) the camera in a fixed-camera area.
if(!EventSystem_Should(VB_CAMERA_FIXED_NODE, true)){
func_80290BC0(0);
return false;
}
if(camera_node_index == -1 || !ncCameraNodeList_nodeIsValid(camera_node_index)){
func_80290BC0(0);
return false;
Expand Down Expand Up @@ -108,6 +113,10 @@ int func_80290E8C(void){
if(player_getWaterState() != BSWATERGROUP_2_UNDERWATER)
return false;

// [port] False when the port's camera keeps (or takes) the camera underwater.
if(!EventSystem_Should(VB_CAMERA_UNDERWATER, true))
return false;

ncDynamicCamera_setState(3);
func_80291488(0xB);
if( gsworld_getMap() == MAP_B_CC_CLANKERS_CAVERN
Expand Down
6 changes: 5 additions & 1 deletion src/core2/gc/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "core2/gc/zoombox.h"
#include "port/ResourceHelpers.h"

int port_dialogueSkipPressed(void);

extern void func_803114D0(void);
extern int gcdialog_hasCurrentTextId(void);
extern char *dialogBin_get(enum asset_e text_id);
Expand Down Expand Up @@ -388,6 +390,7 @@ void dialog_update(void) {
char *sp4C[8];

ret = -1;
const s32 portSkipPressed = port_dialogueSkipPressed(); // [port] Skip Dialogue (CutsceneSkips.cpp)

if (g_Dialog.u8_s.unk128_31 & 0x80) {
controller_copyFaceButtonsPrimary(0, controller_face_buttons);
Expand Down Expand Up @@ -636,7 +639,8 @@ void dialog_update(void) {
break;

case DIALOG_STATE_3:
if ((g_Dialog.u8_s.unk128_31 & 1) && controller_face_buttons[FACE_BUTTON(BUTTON_B)] == 1u) {
if (((g_Dialog.u8_s.unk128_31 & 1) && controller_face_buttons[FACE_BUTTON(BUTTON_B)] == 1u) ||
portSkipPressed) {
dialog_setState(DIALOG_STATE_6);
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/core2/map/gsworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void gsworld_draw(Gfx** gfx, Mtx **mtx, Vtx **vtx) {
core2_34790_getClipDistances(&near, &far);
viewport_setNearAndFar(near, far);
viewport_setRenderViewportAndPerspectiveMatrix(gfx, mtx);
FrameInterpolation_WorldRelativePush(); // [port] see FrameInterpolation.cpp

if (mapModel_has_xlu_bin()) {
FrameInterpolation_RecordOpenChild("map_opa", 0);
Expand Down Expand Up @@ -133,6 +134,7 @@ void gsworld_draw(Gfx** gfx, Mtx **mtx, Vtx **vtx) {
if (!game_is_frozen()) {
func_802BBD0C(gfx, mtx, vtx);
}
FrameInterpolation_WorldRelativePop();

spawnQueue_lock();
}
Expand Down
6 changes: 6 additions & 0 deletions src/port/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ void GameEngine::ArchivesChanged() {
port_spriteDisplayCache_clear();
}

extern "C" void port_dialogueSkipKey(void);

void GameEngine::StartFrame() const {
using Ship::KbScancode;
const int32_t dwScancode = this->context->GetWindow()->GetLastScancode();
Expand All @@ -490,6 +492,10 @@ void GameEngine::StartFrame() const {
// gNextGameState = GSTATE_BOOT;
break;
}
// Skip Dialogue (Enhancements/Cutscenes/CutsceneSkips.cpp)
case KbScancode::LUS_KB_ENTER:
port_dialogueSkipKey();
break;
default:
break;
}
Expand Down
167 changes: 162 additions & 5 deletions src/port/Enhancements/Camera/FreeLookCamera.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Free look camera

#include <chrono>
#include <cmath>

#include <libultraship/bridge/consolevariablebridge.h>
#include <ship/Context.h>
#include <ship/window/Window.h>

#include "port/UI/cvar_prefixes.h"
#include "port/Controller/ControlSchemes.h"
Expand All @@ -11,6 +14,12 @@
#include "port/Enhancements/Events/Hooks/Events.h"
#include "port/ShipInit.hpp"

extern "C" {
#include "enums.h"
int globalTimer_getTime(void);
int getGameMode(void);
}

namespace {
using namespace PortCamera;

Expand All @@ -20,6 +29,11 @@ using namespace PortCamera;
#define CVAR_FREELOOK_INVERT_X CVAR_ENHANCEMENT("Camera.FreeLook.InvertX")
#define CVAR_FREELOOK_INVERT_Y CVAR_ENHANCEMENT("Camera.FreeLook.InvertY")
#define CVAR_FREELOOK_SMOOTH_RATE CVAR_ENHANCEMENT("Camera.FreeLook.SmoothRate")
#define CVAR_FREELOOK_MOUSE CVAR_ENHANCEMENT("Camera.FreeLook.Mouse")
#define CVAR_FREELOOK_MOUSE_SENS CVAR_ENHANCEMENT("Camera.FreeLook.MouseSensitivity")
#define CVAR_FREELOOK_MOUSE_INVERT_X CVAR_ENHANCEMENT("Camera.FreeLook.MouseInvertX")
#define CVAR_FREELOOK_MOUSE_INVERT_Y CVAR_ENHANCEMENT("Camera.FreeLook.MouseInvertY")
#define CVAR_FREELOOK_EVERYWHERE CVAR_ENHANCEMENT("Camera.FreeLook.Everywhere")

constexpr float kDeadzone = 0.15f;
constexpr float kEnterThreshold = 0.30f;
Expand All @@ -36,11 +50,26 @@ constexpr float kDampRatio = 4.0f;

constexpr float kRadToDeg = 57.29577951308232f;

// Mouse free look turns the camera by a fixed angle per pixel the mouse moves (not a speed, like
// the stick), so a given hand movement always turns it the same amount: degrees per pixel at 100%
// Mouse Sensitivity.
constexpr float kMouseDegreesPerPixel = 0.2f;
constexpr int kDefaultMouseSensitivity = 50;
// Mouse movement older than this when the camera next asks (a cutscene, a menu, a load) is dropped
// rather than turning the camera all at once.
constexpr double kMouseStaleSeconds = 0.1;

bool sActive = false;
bool sAimValid = false;
float sPitch = 0.0f;
float sAimY = 0.0f;

float sMouseYaw = 0.0f; // degrees the mouse has turned since the last camera update
float sMousePitch = 0.0f;
bool sMouseCaptureApplied = false;
int sPolledFrame = -1; // the game frame the mouse was last read in
bool sMouseMovedThisFrame = false;

float SpringGain() {
return CVarGetFloat(CVAR_FREELOOK_SMOOTH_RATE, kDefaultSmooth) * kSmoothToGain;
}
Expand Down Expand Up @@ -76,6 +105,84 @@ float CapturePitch() {
return clampf(std::asin(clampf(-diff[1] / dist, -1.0f, 1.0f)) * kRadToDeg, kMinPitch, kMaxPitch);
}

// --- mouse -------------------------------------------------------------------------------------

bool MouseEnabled() {
return port_freeLook_isEnabled() && CVarGetInteger(CVAR_FREELOOK_MOUSE, 0) != 0;
}

// Keeps the window's mouse capture in step with the setting: the mouse is captured (pointer hidden,
// raw movement read) while playing. libultraship frees it while a menu is open and F2 toggles it.
void SyncMouseCapture() {
const bool want = MouseEnabled();
if (want == sMouseCaptureApplied) {
return;
}
sMouseCaptureApplied = want;
auto window = Ship::Context::GetRawInstance()->GetWindow();
window->SetAutoCaptureMouse(want);
window->GetMouseStateManager()->UpdateMouseCapture();
}

void ClearMouse() {
sMouseYaw = 0.0f;
sMousePitch = 0.0f;
}

// Adds the mouse's movement since the last call to sMouseYaw/sMousePitch. Returns true if it moved.
bool PollMouse() {
using Clock = std::chrono::steady_clock;
static Clock::time_point sLastPoll;
auto window = Ship::Context::GetRawInstance()->GetWindow();
const Ship::Coords delta = window->GetMouseDelta(); // read every time, so movement doesn't pile up
const Clock::time_point now = Clock::now();
const bool stale = std::chrono::duration<double>(now - sLastPoll).count() > kMouseStaleSeconds;
sLastPoll = now;
if (!MouseEnabled() || !window->IsMouseCaptured() || stale || (delta.x == 0 && delta.y == 0)) {
return false;
}
const float perPixel =
kMouseDegreesPerPixel * CVarGetInteger(CVAR_FREELOOK_MOUSE_SENS, kDefaultMouseSensitivity) / 100.0f;
const bool invertX = CVarGetInteger(CVAR_FREELOOK_MOUSE_INVERT_X, 0) != 0;
const bool invertY = CVarGetInteger(CVAR_FREELOOK_MOUSE_INVERT_Y, 0) != 0;
// Mouse right turns the view right (the orbit yaw goes down); mouse up looks up (the pitch goes up,
// taking the camera lower).
sMouseYaw += -delta.x * perPixel * (invertX ? -1.0f : 1.0f);
sMousePitch += -delta.y * perPixel * (invertY ? -1.0f : 1.0f);
return true;
}

// Reads the mouse once per game frame (the swivel check and the fixed-camera checks all ask).
bool MouseMovedThisFrame() {
const int frame = globalTimer_getTime();
if (frame != sPolledFrame) {
sPolledFrame = frame;
sMouseMovedThisFrame = PollMouse();
}
return sMouseMovedThisFrame;
}

// --- entering and leaving ---------------------------------------------------------------------

// Free look only starts while the player is playing: not in the attract demo behind "Press Start"
// or the file select playback, where the game is moving Banjo, nor in the pause menu.
bool Playing() {
return getGameMode() == GAME_MODE_3_NORMAL;
}

bool Everywhere() {
return CVarGetInteger(CVAR_FREELOOK_EVERYWHERE, 1) != 0;
}

// Camera states free look can take over from: the follow cameras, and with Free Look Everywhere,
// the underwater camera (3) and the fixed cameras of camera-node areas (8: pivot, 0x11: zoom).
bool CanEnterFrom(int state) {
if (state == 0xB || state == 0x1 || state == 0xA) {
return true;
}
return Everywhere() && (state == 0x3 || state == 0x8 || state == 0x11);
}

void EnterOrbit() {
func_802C0150(2);
func_802BE230(SpringGain(), SpringGain() * kDampRatio);
Expand All @@ -97,44 +204,76 @@ void ExitOrbit() {
}

bool HandleInput() {
SyncMouseCapture();
const bool mouseMoved = MouseMovedThisFrame();

if (!port_freeLook_isEnabled()) {
if (sActive) {
ExitOrbit();
}
ClearMouse();
return false;
}

if (!Playing()) {
ClearMouse();
return sActive; // paused mid-orbit: keep it for when play resumes, but never start one
}

int state = ncDynamicCamera_getState();

if (sActive) {
if (state != FREELOOK_CAM_STATE) {
sActive = false;
ClearMouse();
return false;
}
if (ManualCameraControl()) {
ExitOrbit();
ClearMouse();
return false;
}
return true;
}

if (ManualCameraControl()) {
ClearMouse();
return false;
}

if (state != 0xB && state != 0x1 && state != 0xA) {
if (!CanEnterFrom(state)) {
ClearMouse();
return false;
}

// The stick takes over past a threshold; any mouse movement takes over at once.
float stick[2];
if (ReadStick(stick) >= kEnterThreshold) {
if (ReadStick(stick) >= kEnterThreshold || mouseMoved) {
EnterOrbit();
return true;
}

ClearMouse();
return false;
}

// Free Look Everywhere: asked by the underwater camera and the camera-node (fixed camera) areas
// before they take the camera. True while free look holds the camera, or when the stick or mouse
// asks for it this frame, so they leave it to free look. A C-button camera control hands it back.
bool TakesOverFixedCamera() {
if (!port_freeLook_isEnabled() || !Everywhere() || !Playing()) {
return false;
}
if (sActive && ncDynamicCamera_getState() == FREELOOK_CAM_STATE) {
return !ManualCameraControl();
}
if (ManualCameraControl()) {
return false;
}
float stick[2];
return ReadStick(stick) >= kEnterThreshold || MouseMovedThisFrame();
}

void UpdateOrbit() {
float dt = time_getDelta();

Expand All @@ -149,6 +288,11 @@ void UpdateOrbit() {
D_8037DB70 = mlNormalizeAngle(D_8037DB70 + (invertX ? -stick[0] : stick[0]) * kYawSpeed * yawSens * dt);
sPitch = clampf(sPitch + (invertY ? stick[1] : -stick[1]) * kPitchSpeed * pitchSens * dt, kMinPitch, kMaxPitch);

// The mouse: the angle it moved since the last update, whatever the frame time.
D_8037DB70 = mlNormalizeAngle(D_8037DB70 + sMouseYaw);
sPitch = clampf(sPitch + sMousePitch, kMinPitch, kMaxPitch);
ClearMouse();

float focus[3];
float offset[3];
float target[3];
Expand All @@ -161,10 +305,12 @@ void UpdateOrbit() {
func_802C0394(target);
func_802BE190(target);

// Keep the camera out of walls, but don't use the follow camera's line-of-sight rescue
// (func_802BC84C): when the player has been hidden from the camera for a few frames it jumps the
// camera to a spot beside them, and free look pulls it straight back to where the player put it,
// over and over, so the view (and the player in it) kept jumping and snapping back.
if (func_802BE60C()) {
if (!func_802BC84C(1)) {
func_802C03BC();
}
func_802C03BC();
func_802C04B0();
}

Expand Down Expand Up @@ -195,6 +341,17 @@ void RegisterFreeLookCamera_Init() {
UpdateOrbit();
}
});
// Free Look Everywhere: the fixed-camera areas and the underwater camera leave the camera to free look.
COND_VB_SHOULD(VB_CAMERA_FIXED_NODE, EVENT_PRIORITY_NORMAL, true, {
if (TakesOverFixedCamera()) {
*should = false;
}
});
COND_VB_SHOULD(VB_CAMERA_UNDERWATER, EVENT_PRIORITY_NORMAL, true, {
if (TakesOverFixedCamera()) {
*should = false;
}
});
}

static RegisterShipInitFunc initFreeLookCamera(RegisterFreeLookCamera_Init, {});
4 changes: 4 additions & 0 deletions src/port/Enhancements/Camera/FreeLookCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
// tracks the vanilla zoom level (shared with the modern-scheme camera), so the
// zoom controls re-zoom in place. Returns to the normal camera when the player
// rotates or enters first-person via a C-button camera control.
//
// The mouse can turn it too (Free Look Mouse), and with Free Look Everywhere it can also take over
// from the underwater camera and the fixed cameras of camera-node areas (VB_CAMERA_FIXED_NODE,
// VB_CAMERA_UNDERWATER).

#define FREELOOK_CAM_STATE 0x14

Expand Down
Loading