#include "CPlayer.hpp" #include "CActorParameters.hpp" #include "CMorphBall.hpp" #include "Weapon/CPlayerGun.hpp" #include "CStateManager.hpp" #include "CSimplePool.hpp" #include "GameGlobalObjects.hpp" #include "Particle/CGenDescription.hpp" #include "Camera/CFirstPersonCamera.hpp" #include "Camera/CBallCamera.hpp" #include "Camera/CCinematicCamera.hpp" #include "TCastTo.hpp" #include "CScriptGrapplePoint.hpp" namespace urde { static CModelData MakePlayerAnimRes(ResId resId, const zeus::CVector3f& scale) { return {CAnimRes(resId, 0, scale, 0, true), 1}; } CPlayer::CPlayer(TUniqueId uid, const zeus::CTransform& xf, const zeus::CAABox& aabb, unsigned int resId, const zeus::CVector3f& playerScale, float mass, float stepUp, float stepDown, float f4, const CMaterialList& ml) : CPhysicsActor(uid, true, "CPlayer", CEntityInfo(kInvalidAreaId, CEntity::NullConnectionList), xf, MakePlayerAnimRes(resId, playerScale), ml, aabb, SMoverData(mass), CActorParameters::None(), stepUp, stepDown), x7d0_animRes(resId, 0, playerScale, 0, true) { x490_gun.reset(new CPlayerGun(uid)); x768_morphball.reset(new CMorphBall(*this, f4)); x76c_cameraBob.reset(new CPlayerCameraBob(CPlayerCameraBob::ECameraBobType::One, zeus::CVector2f{CPlayerCameraBob::kCameraBobExtentX, CPlayerCameraBob::kCameraBobExtentY}, CPlayerCameraBob::kCameraBobPeriod)); x9c4_26_ = true; x9c4_27_ = true; x9c4_28_ = true; x9c5_31_ = true; ResId beamId = g_tweakPlayerRes->GetBeamBallTransitionModel(x7ec_); x7f0_ballTransitionBeamModel = std::make_unique(CStaticRes(beamId, playerScale)); } bool CPlayer::IsTransparent() const { return x588_alpha < 1.f; } void CPlayer::Update(float, CStateManager& mgr) {} bool CPlayer::IsPlayerDeadEnough() const { if (x2f8_morphTransState == CPlayer::EPlayerMorphBallState::Unmorphed) return x9f4_deathTime > 2.5f; else if (x2f8_morphTransState == CPlayer::EPlayerMorphBallState::Morphed) return x9f4_deathTime > 6.f; return false; } void CPlayer::AsyncLoadSuit(CStateManager& mgr) { x490_gun->AsyncLoadSuit(mgr); } void CPlayer::LoadAnimationTokens() {} bool CPlayer::CanRenderUnsorted(CStateManager& mgr) const { return false; } const CDamageVulnerability* CPlayer::GetDamageVulnerability(const zeus::CVector3f& v1, const zeus::CVector3f& v2, const CDamageInfo& info) const { return nullptr; } const CDamageVulnerability* CPlayer::GetDamageVulnerability() const { return nullptr; } zeus::CVector3f CPlayer::GetHomingPosition(CStateManager& mgr, float) const { return {}; } zeus::CVector3f CPlayer::GetAimPosition(CStateManager& mgr, float) const { return {}; } void CPlayer::FluidFXThink(CActor::EFluidState, CScriptWater& water, CStateManager& mgr) {} zeus::CVector3f CPlayer::GetDamageLocationWR() const { return {}; } float CPlayer::GetPrevDamageAmount() const { return 0.f; } float CPlayer::GetDamageAmount() const { return 0.f; } bool CPlayer::WasDamaged() const { return false; } void CPlayer::TakeDamage(bool, const zeus::CVector3f&, float, EWeaponType, CStateManager& mgr) {} void CPlayer::Accept(IVisitor& visitor) { visitor.Visit(this); } CHealthInfo* CPlayer::HealthInfo(const CStateManager& mgr) { return &mgr.GetPlayerState()->HealthInfo(); } bool CPlayer::IsUnderBetaMetroidAttack(CStateManager& mgr) const { return false; } rstl::optional_object CPlayer::GetTouchBounds() const { return {}; } void CPlayer::Touch(CActor&, CStateManager& mgr) {} void CPlayer::DoPreThink(float dt, CStateManager& mgr) { PreThink(dt, mgr); if (CEntity* ent = mgr.ObjectById(xa00_)) ent->PreThink(dt, mgr); } void CPlayer::DoThink(float dt, CStateManager& mgr) { Think(dt, mgr); if (CEntity* ent = mgr.ObjectById(xa00_)) ent->Think(dt, mgr); } void CPlayer::UpdateScanningState(const CFinalInput& input, CStateManager& mgr, float) {} void CPlayer::ValidateScanning(const CFinalInput& input, CStateManager& mgr) {} void CPlayer::SetScanningState(EPlayerScanState, CStateManager& mgr) {} bool CPlayer::GetExplorationMode() const { return false; } bool CPlayer::GetCombatMode() const { return false; } void CPlayer::RenderGun(const CStateManager& mgr, const zeus::CVector3f&) const {} void CPlayer::Render(const CStateManager& mgr) const {} void CPlayer::RenderReflectedPlayer(CStateManager& mgr) const {} void CPlayer::PreRender(CStateManager& mgr, const zeus::CFrustum&) {} void CPlayer::CalculateRenderBounds() {} void CPlayer::AddToRenderer(const zeus::CFrustum&, const CStateManager&) {} void CPlayer::ComputeFreeLook(const CFinalInput& input) {} void CPlayer::UpdateFreeLook(float dt) {} float CPlayer::GetMaximumPlayerPositiveVerticalVelocity(CStateManager&) const { return 0.f; } void CPlayer::ProcessInput(const CFinalInput&, CStateManager&) {} void CPlayer::Stop(CStateManager& stateMgr) { } bool CPlayer::GetFrozenState() const { return false; } void CPlayer::Think(float, CStateManager&) {} void CPlayer::PreThink(float dt, CStateManager& mgr) { x558_ = false; x55c_ = 0.f; x560_ = 0.f; x564_ = zeus::CVector3f::skZero; xa04_ = dt; } void CPlayer::AcceptScriptMsg(EScriptObjectMessage, TUniqueId, CStateManager&) {} void CPlayer::SetVisorSteam(float, float, float, u32, bool) {} void CPlayer::UpdateFootstepBounds(const CFinalInput& input, CStateManager&, float) {} u16 CPlayer::GetMaterialSoundUnderPlayer(CStateManager& mgr, const u16*, int, u16) { return 0; } u16 CPlayer::SfxIdFromMaterial(const CMaterialList& mat, const u16* idList, u32 tableLen, u16 defId) { u16 id = defId; for (u32 i = 0; i < tableLen; ++i) { if (mat.HasMaterial(EMaterialTypes(i)) && idList[i] != 0xFFFF) id = idList[i]; } return id; } void CPlayer::UpdateCrosshairsState(const CFinalInput&) {} void CPlayer::UpdateVisorTransition(float, CStateManager& mgr) {} void CPlayer::UpdateVisorState(const CFinalInput&, float, CStateManager& mgr) {} void CPlayer::ForceGunOrientation(const zeus::CTransform&, CStateManager& mgr) {} void CPlayer::UpdateDebugCamera(CStateManager& mgr) {} CFirstPersonCamera& CPlayer::GetFirstPersonCamera(CStateManager& mgr) { return *mgr.GetCameraManager()->GetFirstPersonCamera(); } void CPlayer::UpdateGunTransform(const zeus::CVector3f&, float, CStateManager& mgr, bool) {} void CPlayer::UpdateAssistedAiming(const zeus::CTransform& xf, const CStateManager& mgr) { } void CPlayer::UpdateAimTargetPrediction(const zeus::CTransform& xf, const CStateManager& mgr) { } void CPlayer::ResetAimTargetPrediction(TUniqueId target) { if (target == kInvalidUniqueId || x3f4_aimTarget != target) x404_aimTargetAverage.Clear(); x3f4_aimTarget = target; } void CPlayer::DrawGun(CStateManager& mgr) {} void CPlayer::HolsterGun(CStateManager& mgr) {} void CPlayer::UpdateGrappleArmTransform(const zeus::CVector3f&, CStateManager& mgr, float) {} void CPlayer::ApplyGrappleForces(const CFinalInput& input, CStateManager& mgr, float) {} bool CPlayer::ValidateFPPosition(const zeus::CVector3f& pos, CStateManager& mgr) { return false; } void CPlayer::UpdateGrappleState(const CFinalInput& input, CStateManager& mgr) {} void CPlayer::ApplyGrappleJump(CStateManager& mgr) {} void CPlayer::BeginGrapple(zeus::CVector3f&, CStateManager& mgr) {} void CPlayer::BreakGrapple(CStateManager& mgr) {} void CPlayer::PreventFallingCameraPitch() {} void CPlayer::OrbitCarcass(CStateManager&) {} void CPlayer::OrbitPoint(EPlayerOrbitType, CStateManager& mgr) {} zeus::CVector3f CPlayer::GetHUDOrbitTargetPosition() const { return {}; } void CPlayer::SetOrbitState(EPlayerOrbitState, CStateManager& mgr) {} void CPlayer::SetOrbitTargetId(TUniqueId, CStateManager& mgr) {} void CPlayer::UpdateOrbitPosition(float, CStateManager& mgr) {} void CPlayer::UpdateOrbitZPosition() {} void CPlayer::UpdateOrbitFixedPosition() {} void CPlayer::SetOrbitPosition(float, CStateManager& mgr) {} void CPlayer::UpdateAimTarget(CStateManager& mgr) {} void CPlayer::UpdateAimTargetTimer(float) {} bool CPlayer::ValidateAimTargetId(TUniqueId, CStateManager& mgr) { return false; } bool CPlayer::ValidateObjectForMode(TUniqueId, CStateManager& mgr) const { return false; } TUniqueId CPlayer::FindAimTargetId(CStateManager& mgr) { return {}; } const zeus::CTransform& CPlayer::GetFirstPersonCameraTransform(const CStateManager& mgr) const { return mgr.GetCameraManager()->GetFirstPersonCamera()->GetGunFollowTransform(); } TUniqueId CPlayer::CheckEnemiesAgainstOrbitZone(const std::vector&, EPlayerZoneInfo, EPlayerZoneType, CStateManager& mgr) const { return {}; } TUniqueId CPlayer::FindOrbitTargetId(CStateManager& mgr) { return {}; } static zeus::CAABox BuildNearListBox(bool cropBottom, const zeus::CTransform& xf, float x, float z, float y) { zeus::CAABox aabb(-x, cropBottom ? 0.f : -y, -z, x, y, z); return aabb.getTransformedAABox(xf); } void CPlayer::UpdateOrbitableObjects(CStateManager& mgr) { x354_onScreenOrbitObjects.clear(); x344_nearbyOrbitObjects.clear(); x354_onScreenOrbitObjects.clear(); if (CheckOrbitDisableSourceList(mgr)) return; float dist = GetOrbitMaxTargetDistance(mgr); if (x9c6_24_) dist *= 5.f; zeus::CAABox nearAABB = BuildNearListBox(true, GetFirstPersonCameraTransform(mgr), g_tweakPlayer->GetOrbitNearX(), g_tweakPlayer->GetOrbitNearZ(), dist); CMaterialFilter filter = mgr.GetPlayerState()->GetCurrentVisor() == CPlayerState::EPlayerVisor::Scan ? CMaterialFilter::MakeInclude({EMaterialTypes::Scannable}) : CMaterialFilter::MakeInclude({EMaterialTypes::Orbit}); rstl::reserved_vector nearList; mgr.BuildNearList(nearList, nearAABB, filter, nullptr); FindOrbitableObjects(nearList, x344_nearbyOrbitObjects, x330_orbitZone, EPlayerZoneType::Always, mgr, true); FindOrbitableObjects(nearList, x354_onScreenOrbitObjects, x330_orbitZone, x334_orbitType, mgr, true); FindOrbitableObjects(nearList, x364_offScreenOrbitObjects, x330_orbitZone, x334_orbitType, mgr, false); } TUniqueId CPlayer::FindBestOrbitableObject(const std::vector&, EPlayerZoneInfo, CStateManager& mgr) const { zeus::CVector3f eyePos = GetEyePosition(); zeus::CVector3f lookDir = x34_transform.basis[1].normalized(); /* TODO: Finish */ return {}; } void CPlayer::FindOrbitableObjects(const rstl::reserved_vector& nearObjects, std::vector& listOut, EPlayerZoneInfo zone, EPlayerZoneType type, CStateManager& mgr, bool onScreenTest) const { CFirstPersonCamera* fpCam = mgr.GetCameraManager()->GetFirstPersonCamera(); zeus::CVector3f eyePos = GetEyePosition(); for (TUniqueId id : nearObjects) { if (TCastToConstPtr act = mgr.GetObjectById(id)) { if (GetUniqueId() == act->GetUniqueId()) continue; if (ValidateOrbitTargetId(act->GetUniqueId(), mgr)) continue; zeus::CVector3f orbitPos = act->GetOrbitPosition(mgr); zeus::CVector3f screenPos = fpCam->ConvertToScreenSpace(orbitPos); screenPos.x = g_Viewport.x8_width * screenPos.x / 2.f + g_Viewport.x8_width / 2.f; screenPos.y = g_Viewport.xc_height * screenPos.y / 2.f + g_Viewport.xc_height / 2.f; bool pass = false; if (onScreenTest) { if (WithinOrbitScreenBox(screenPos, zone, type)) pass = true; } else { if (!WithinOrbitScreenBox(screenPos, zone, type)) pass = true; } if (pass && (!act->GetDoTargetDistanceTest() || (orbitPos - eyePos).magnitude() <= GetOrbitMaxTargetDistance(mgr))) listOut.push_back(id); } } } bool CPlayer::WithinOrbitScreenBox(const zeus::CVector3f& screenCoords, EPlayerZoneInfo zone, EPlayerZoneType type) const { if (screenCoords.z >= 1.f) return false; switch (type) { case EPlayerZoneType::Box: return std::fabs(screenCoords.x - g_tweakPlayer->GetOrbitScreenBoxCenterX(int(zone))) < g_tweakPlayer->GetOrbitScreenBoxHalfExtentX(int(zone)) && std::fabs(screenCoords.y - g_tweakPlayer->GetOrbitScreenBoxCenterY(int(zone))) < g_tweakPlayer->GetOrbitScreenBoxHalfExtentY(int(zone)) && screenCoords.z < 1.f; break; case EPlayerZoneType::Ellipse: return WithinOrbitScreenEllipse(screenCoords, zone); default: return true; } return false; } bool CPlayer::WithinOrbitScreenEllipse(const zeus::CVector3f& screenCoords, EPlayerZoneInfo zone) const { if (screenCoords.z >= 1.f) return false; float heYSq = g_tweakPlayer->GetOrbitScreenBoxHalfExtentY(int(zone)); heYSq *= heYSq; float heXSq = g_tweakPlayer->GetOrbitScreenBoxHalfExtentX(int(zone)); heXSq *= heXSq; float tmpY = std::fabs(screenCoords.y - g_tweakPlayer->GetOrbitScreenBoxCenterY(int(zone))); float tmpX = std::fabs(screenCoords.x - g_tweakPlayer->GetOrbitScreenBoxCenterX(int(zone))); return tmpX * tmpX <= (1.f - tmpY * tmpY / heYSq) * heXSq; } bool CPlayer::CheckOrbitDisableSourceList(CStateManager& mgr) { for (auto it = x9e4_orbitDisableList.begin() ; it != x9e4_orbitDisableList.end() ;) { if (!mgr.GetObjectById(*it)) { it = x9e4_orbitDisableList.erase(it); continue; } ++it; } return x9e4_orbitDisableList.size() != 0; } void CPlayer::RemoveOrbitDisableSource(TUniqueId uid) { for (auto it = x9e4_orbitDisableList.begin() ; it != x9e4_orbitDisableList.end() ;) { if (*it == uid) { it = x9e4_orbitDisableList.erase(it); return; } ++it; } } void CPlayer::AddOrbitDisableSource(CStateManager& mgr, TUniqueId addId) { if (x9e4_orbitDisableList.size() >= 5) return; for (TUniqueId uid : x9e4_orbitDisableList) if (uid == addId) return; x9e4_orbitDisableList.push_back(addId); ResetAimTargetPrediction(kInvalidUniqueId); if (!TCastToConstPtr(mgr.GetObjectById(x310_lockonObjectId))) SetOrbitTargetId(kInvalidUniqueId, mgr); } void CPlayer::UpdateOrbitPreventionTimer(float) {} void CPlayer::UpdateOrbitModeTimer(float) {} void CPlayer::UpdateOrbitZone(CStateManager& mgr) {} void CPlayer::UpdateOrbitInput(const CFinalInput& input, CStateManager& mgr) {} void CPlayer::UpdateOrbitSelection(const CFinalInput& input, CStateManager& mgr) {} void CPlayer::UpdateOrbitOrientation(CStateManager& mgr) {} void CPlayer::UpdateOrbitTarget(CStateManager& mgr) {} float CPlayer::GetOrbitMaxLockDistance(CStateManager& mgr) const { return 0.f; } float CPlayer::GetOrbitMaxTargetDistance(CStateManager& mgr) const { return 0.f; } bool CPlayer::ValidateOrbitTargetId(TUniqueId, CStateManager& mgr) const { return false; } bool CPlayer::ValidateCurrentOrbitTargetId(CStateManager& mgr) { return false; } bool CPlayer::ValidateOrbitTargetIdAndPointer(TUniqueId, CStateManager& mgr) const { return false; } zeus::CVector3f CPlayer::GetBallPosition() const { return {}; } zeus::CVector3f CPlayer::GetEyePosition() const { return {}; } float CPlayer::GetEyeHeight() const { return 0.f; } float CPlayer::GetStepUpHeight() const { return 0.f; } float CPlayer::GetStepDownHeight() const { return 0.f; } void CPlayer::Teleport(const zeus::CTransform& xf, CStateManager& mgr, bool) {} void CPlayer::BombJump(const zeus::CVector3f& pos, CStateManager& mgr) {} zeus::CTransform CPlayer::CreateTransformFromMovementDirection() const { return {}; } const CCollisionPrimitive* CPlayer::GetCollisionPrimitive() const { return CPhysicsActor::GetCollisionPrimitive(); } zeus::CTransform CPlayer::GetPrimitiveTransform() const { return {}; } bool CPlayer::CollidedWith(TUniqueId, const CCollisionInfoList&, CStateManager& mgr) { return false; } float CPlayer::GetActualFirstPersonMaxVelocity() const { return 0.f; } void CPlayer::SetMoveState(EPlayerMovementState, CStateManager& mgr) {} float CPlayer::JumpInput(const CFinalInput& input, CStateManager& mgr) { return 0.f; } float CPlayer::TurnInput(const CFinalInput& input) const { return 0.f; } float CPlayer::StrafeInput(const CFinalInput& input) const { return 0.f; } float CPlayer::ForwardInput(const CFinalInput& input, float) const { return 0.f; } void CPlayer::ComputeMovement(const CFinalInput& input, CStateManager& mgr, float) {} float CPlayer::GetWeight() const { return 0.f; } float CPlayer::GetDampedClampedVelocityWR() const { return 0.f; } void CPlayer::UpdateCinematicState(CStateManager& mgr) { if (mgr.GetCameraManager()->IsInCinematicCamera()) { switch (x2f4_cameraState) { } } } void CPlayer::SetCameraState(EPlayerCameraState camState, CStateManager& stateMgr) { if (x2f4_cameraState == camState) return; x2f4_cameraState = camState; CCameraManager* camMgr = stateMgr.GetCameraManager(); switch (camState) { case EPlayerCameraState::Zero: camMgr->SetCurrentCameraId(camMgr->GetFirstPersonCamera()->GetUniqueId(), stateMgr); x768_morphball->SetBallLightActive(stateMgr, false); break; case EPlayerCameraState::One: case EPlayerCameraState::Three: camMgr->SetCurrentCameraId(camMgr->GetBallCamera()->GetUniqueId(), stateMgr); x768_morphball->SetBallLightActive(stateMgr, true); break; case EPlayerCameraState::Two: break; case EPlayerCameraState::Four: { bool ballLight = false; if (TCastToPtr cineCam = camMgr->GetCurrentCamera(stateMgr)) ballLight = x2f8_morphTransState == EPlayerMorphBallState::Morphed && cineCam->GetW1() & 0x40; x768_morphball->SetBallLightActive(stateMgr, ballLight); break; } } } bool CPlayer::IsEnergyLow(const CStateManager& mgr) const { float lowThreshold = mgr.GetPlayerState()->GetItemCapacity(CPlayerState::EItemType::EnergyTanks) < 4 ? 30.f : 100.f; return HealthInfo(mgr)->GetHP() < lowThreshold; } bool CPlayer::ObjectInScanningRange(TUniqueId id, const CStateManager& mgr) const { const CEntity* ent = mgr.GetObjectById(id); if (TCastToConstPtr act = ent) { zeus::CVector3f delta = act->GetTranslation() - GetTranslation(); if (delta.canBeNormalized()) return delta.magnitude() < g_tweakPlayer->GetScanningRange(); } return false; } void CPlayer::Touch() {} void CPlayer::CVisorSteam::SetSteam(float a, float b, float c, ResId d, bool e) { if (x1c_ == -1 || a > x10_) { x10_ = a; x14_ = b; x18_ = c; x1c_ = d; } x28_ = e; } ResId CPlayer::CVisorSteam::GetTextureId() const { return xc_tex; } void CPlayer::CVisorSteam::Update(float dt) { if (x1c_ == -1) x0_ = 0.f; else { x0_ = x10_; x4_ = x14_; x8_ = x18_; xc_tex = x1c_; } x1c_ = -1; if ((x20_alpha - x0_) < 0.000009999f || std::fabs(x20_alpha) > 0.000009999f) return; if (x20_alpha > x0_) { if (x24_ <= 0.f) { x20_alpha -= (dt / x8_); x20_alpha = std::min(x20_alpha, x0_); } else { x24_ = x0_ - dt; x24_ = zeus::max(0.f, x24_); } return; } CToken tmpTex = g_SimplePool->GetObj({SBIG('TXTR'), xc_tex}); if (!tmpTex) return; x20_alpha += (x20_alpha + (dt / x4_)); if (x20_alpha > x0_) x20_alpha = x0_; x24_ = 0.1f; } void CPlayer::SetSpawnedMorphBallState(CPlayer::EPlayerMorphBallState, CStateManager&) {} void CPlayer::DecrementPhazon() { if (xa10_ == 0) return; xa10_--; } void CPlayer::IncrementPhazon() { if (xa10_ != 0) xa10_++; else xa14_ = 0.f; } }