metaforce/Runtime/MP1/World/CMagdolite.cpp

680 lines
25 KiB
C++
Raw Normal View History

#include "Runtime/MP1/World/CMagdolite.hpp"
2018-10-11 22:57:05 +00:00
2020-03-01 13:14:39 +00:00
#include "Runtime/Collision/CCollisionActor.hpp"
#include "Runtime/Collision/CCollisionActorManager.hpp"
#include "Runtime/Collision/CJointCollisionDescription.hpp"
#include "Runtime/Graphics/CSkinnedModel.hpp"
#include "Runtime/Weapon/CFlameThrower.hpp"
#include "Runtime/World/CPlayer.hpp"
#include "Runtime/World/CScriptWater.hpp"
#include "Runtime/World/CScriptWaypoint.hpp"
#include "Runtime/GameGlobalObjects.hpp"
#include "Runtime/CSimplePool.hpp"
#include "Runtime/CStateManager.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
#include <array>
2021-04-10 08:42:06 +00:00
namespace metaforce::MP1 {
2018-10-11 22:57:05 +00:00
2020-03-01 13:14:39 +00:00
namespace {
constexpr std::array<SSphereJointInfo, 5> skSpineJoints{{
{"spine1", 0.75f},
{"spine3", 0.75f},
{"spine5", 0.75f},
{"spine7", 0.75f},
{"spine9", 0.75f},
}};
constexpr std::array<SOBBJointInfo, 2> skHeadJoints{{
{"head", "Top_LCTR", {1.f, 0.15f, 0.5f}},
{"head", "Bottom_LCTR", {0.75f, 0.15f, 0.25f}},
}};
} // namespace
2018-10-11 22:57:05 +00:00
CMagdolite::CMagdolite(TUniqueId uid, std::string_view name, const CEntityInfo& info, const zeus::CTransform& xf,
2018-12-08 05:30:43 +00:00
CModelData&& mData, const CPatternedInfo& pInfo, const CActorParameters& actParms, float f1,
float f2, const CDamageInfo& dInfo1, const CDamageInfo& dInfo2,
const CDamageVulnerability& dVuln1, const CDamageVulnerability& dVuln2, CAssetId modelId,
2020-03-01 13:14:39 +00:00
CAssetId skinId, float f3, float f4, float f5, float f6, const CFlameInfo& magData, float f7,
float f8, float f9)
2018-10-11 22:57:05 +00:00
: CPatterned(ECharacter::Magdolite, uid, name, EFlavorType::Zero, info, xf, std::move(mData), pInfo,
2019-05-14 22:30:15 +00:00
EMovementType::Flyer, EColliderType::One, EBodyType::BiPedal, actParms, EKnockBackVariant::Large)
, x568_initialDelay(f4)
, x56c_minDelay(f5)
, x570_maxDelay(f6)
, x574_minHp(f3)
, x578_losMaxDistance(std::cos(zeus::degToRad(f2)))
2019-05-14 22:30:15 +00:00
, x57c_(f1)
, x584_boneTracker(*GetModelData()->GetAnimationData(), "head"sv, zeus::degToRad(f1), zeus::degToRad(90.f),
2020-03-01 13:14:39 +00:00
EBoneTrackingFlags::ParentIk)
2020-04-21 18:15:17 +00:00
, x5bc_instaKillVulnerability(dVuln1)
, x624_normalVulnerability(dVuln2)
2020-03-01 13:14:39 +00:00
, x690_headlessModel(
CToken(TObjOwnerDerivedFromIObj<CSkinnedModel>::GetNewDerivedObject(std::make_unique<CSkinnedModel>(
g_SimplePool->GetObj({SBIG('CMDL'), modelId}), g_SimplePool->GetObj({SBIG('CSKR'), skinId}),
x64_modelData->GetAnimationData()->GetModelData()->GetLayoutInfo()))))
2020-04-21 18:15:17 +00:00
, x6a8_flameInfo(magData)
, x6cc_flameThrowerDesc(g_SimplePool->GetObj("FlameThrower"sv))
, x6d4_flameThrowerDamage(dInfo1)
, x6f0_headContactDamage(dInfo2)
, x71c_attackTarget(xf.origin)
2020-03-01 13:14:39 +00:00
, x744_(f7)
, x748_(f8)
, x74c_(f9) {
x460_knockBackController.SetAutoResetImpulse(false);
x460_knockBackController.SetEnableBurn(false);
2020-04-21 05:32:16 +00:00
// redundant
// x690_headlessModel->SetLayoutInfo(GetModelData()->GetAnimationData()->GetModelData()->GetLayoutInfo());
2019-05-14 22:30:15 +00:00
}
void CMagdolite::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
2019-05-14 22:30:15 +00:00
switch (msg) {
2020-03-01 13:14:39 +00:00
case EScriptObjectMessage::Touched:
ApplyContactDamage(uid, mgr);
break;
case EScriptObjectMessage::Damage:
case EScriptObjectMessage::InvulnDamage:
2020-03-01 13:14:39 +00:00
if (TCastToConstPtr<CGameProjectile> proj = mgr.GetObjectById(uid)) {
if (proj->GetOwnerId() == mgr.GetPlayer().GetUniqueId()) {
if (GetBodyController()->GetPercentageFrozen() <= 0.f ||
2020-04-21 18:15:17 +00:00
x5bc_instaKillVulnerability.GetVulnerability(proj->GetDamageInfo().GetWeaponMode(), false) ==
EVulnerability::Deflect) {
float hp = HealthInfo(mgr)->GetHP();
if (x70c_curHealth - hp <= x574_minHp) {
2020-04-21 18:15:17 +00:00
if (x624_normalVulnerability.GetVulnerability(proj->GetDamageInfo().GetWeaponMode(), false) !=
EVulnerability::Deflect) {
x400_24_hitByPlayerProjectile = true;
}
} else {
x70c_curHealth = hp;
x754_24_retreat = true;
}
} else if (x400_24_hitByPlayerProjectile) {
x754_26_lostMyHead = true;
x401_30_pendingDeath = true;
2020-03-01 13:14:39 +00:00
}
}
}
return;
case EScriptObjectMessage::SuspendedMove:
2020-04-21 05:32:16 +00:00
if (x580_collisionManager) {
x580_collisionManager->SetMovable(mgr, false);
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
break;
case EScriptObjectMessage::Registered: {
x450_bodyController->Activate(mgr);
RemoveMaterial(EMaterialTypes::Solid, mgr);
AddMaterial(EMaterialTypes::NonSolidDamageable, mgr);
2019-05-14 22:30:15 +00:00
x584_boneTracker.SetActive(false);
CreateShadow(false);
2020-03-01 13:14:39 +00:00
const zeus::CVector3f boxExtents = GetBoundingBox().extents();
SetBoundingBox({-boxExtents, boxExtents});
SetupCollisionActors(mgr);
x330_stateMachineState.SetDelay(0.f);
CreateFlameThrower(mgr);
x70c_curHealth = GetHealthInfo(mgr)->GetHP();
2020-03-01 13:14:39 +00:00
break;
}
case EScriptObjectMessage::Deleted:
x580_collisionManager->Destroy(mgr);
2020-03-01 13:14:39 +00:00
if (x6c8_flameThrowerId != kInvalidUniqueId) {
mgr.FreeScriptObject(x6c8_flameThrowerId);
x6c8_flameThrowerId = kInvalidUniqueId;
}
2019-02-18 05:47:46 +00:00
break;
default:
break;
}
CPatterned::AcceptScriptMsg(msg, uid, mgr);
}
2020-03-01 13:14:39 +00:00
void CMagdolite::ApplyContactDamage(TUniqueId uid, CStateManager& mgr) {
if (TCastToConstPtr<CCollisionActor> colAct = mgr.GetObjectById(uid)) {
2020-04-21 18:15:17 +00:00
if (!IsAlive()) {
2020-03-01 13:14:39 +00:00
return;
2020-04-21 18:15:17 +00:00
}
2020-03-01 13:14:39 +00:00
CDamageInfo dInfo = GetContactDamage();
for (TUniqueId testId : x69c_) {
if (testId == colAct->GetUniqueId()) {
2020-04-21 18:15:17 +00:00
dInfo = x6f0_headContactDamage;
2020-03-01 13:14:39 +00:00
break;
}
}
if (colAct->GetLastTouchedObject() == mgr.GetPlayer().GetUniqueId() && x420_curDamageRemTime <= 0.f) {
mgr.ApplyDamage(GetUniqueId(), mgr.GetPlayer().GetUniqueId(), GetUniqueId(), dInfo,
CMaterialFilter::MakeIncludeExclude({EMaterialTypes::Solid}, {}), {});
x420_curDamageRemTime = x424_damageWaitTime;
}
}
}
void CMagdolite::SetupCollisionActors(CStateManager& mgr) {
std::vector<CJointCollisionDescription> joints;
joints.reserve(8);
const CAnimData* animData = GetModelData()->GetAnimationData();
for (const auto& info : skSpineJoints) {
joints.push_back(CJointCollisionDescription::SphereCollision(animData->GetLocatorSegId(info.name), info.radius,
info.name, 200.f));
}
for (const auto& info : skHeadJoints) {
joints.push_back(CJointCollisionDescription::OBBAutoSizeCollision(
animData->GetLocatorSegId(info.from), animData->GetLocatorSegId(info.to), info.bounds,
CJointCollisionDescription::EOrientationType::One, info.to, 200.f));
}
2020-04-21 05:32:16 +00:00
x580_collisionManager =
std::make_unique<CCollisionActorManager>(mgr, GetUniqueId(), GetAreaIdAlways(), joints, GetActive());
2020-03-01 13:14:39 +00:00
for (int i = 0; i < x580_collisionManager->GetNumCollisionActors(); ++i) {
const auto& desc = x580_collisionManager->GetCollisionDescFromIndex(i);
2020-03-01 13:14:39 +00:00
if (TCastToPtr<CCollisionActor> colAct = mgr.ObjectById(desc.GetCollisionActorId())) {
colAct->AddMaterial(EMaterialTypes::AIJoint, mgr);
}
}
for (int i = 0; i < x580_collisionManager->GetNumCollisionActors(); ++i) {
const auto& desc = x580_collisionManager->GetCollisionDescFromIndex(i);
2020-04-21 06:15:02 +00:00
if (desc.GetName() == "Top_LCTR"sv || desc.GetName() == "Bottom_LCTR"sv) {
2020-03-01 13:14:39 +00:00
if (TCastToPtr<CCollisionActor> colAct = mgr.ObjectById(desc.GetCollisionActorId())) {
colAct->AddMaterial(EMaterialTypes::ProjectilePassthrough, mgr);
x69c_.push_back(colAct->GetUniqueId());
}
}
}
}
void CMagdolite::CreateFlameThrower(CStateManager& mgr) {
2020-04-21 05:32:16 +00:00
if (x6c8_flameThrowerId != kInvalidUniqueId) {
2020-03-01 13:14:39 +00:00
return;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
x6c8_flameThrowerId = mgr.AllocateUniqueId();
2020-04-21 18:15:17 +00:00
mgr.AddObject(new CFlameThrower(x6cc_flameThrowerDesc, "Magdolite_Flame"sv, EWeaponType::Plasma, x6a8_flameInfo, {},
EMaterialTypes::CollisionActor, x6d4_flameThrowerDamage, x6c8_flameThrowerId,
GetAreaIdAlways(), GetUniqueId(), EProjectileAttrib::None, CAssetId(), -1,
CAssetId()));
2020-03-01 13:14:39 +00:00
}
void CMagdolite::LaunchFlameThrower(CStateManager& mgr, bool fire) {
if ((!fire || x754_30_inProjectileAttack) && IsAlive()) {
2020-04-21 05:32:16 +00:00
if (auto* fl = static_cast<CFlameThrower*>(mgr.ObjectById(x6c8_flameThrowerId))) {
if (!fire) {
2020-03-01 13:14:39 +00:00
fl->Reset(mgr, false);
2020-04-21 05:32:16 +00:00
} else {
2020-03-01 13:14:39 +00:00
fl->Fire(GetTransform(), mgr, false);
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
}
x754_27_flameThrowerActive = fire;
2020-03-01 13:14:39 +00:00
}
}
void CMagdolite::Think(float dt, CStateManager& mgr) {
CPatterned::Think(dt, mgr);
2020-04-21 05:32:16 +00:00
if (!GetActive()) {
2020-03-01 13:14:39 +00:00
return;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
x758_ += dt;
if (GetBodyController()->GetPercentageFrozen() > 0.f && x754_27_flameThrowerActive) {
2020-03-01 13:14:39 +00:00
LaunchFlameThrower(mgr, false);
}
if (!IsAlive()) {
2020-04-21 05:32:16 +00:00
if (auto* fl = static_cast<CFlameThrower*>(mgr.ObjectById(x6c8_flameThrowerId))) {
2020-03-01 13:14:39 +00:00
fl->SetTransform(GetLctrTransform("LCTR_MAGMOUTH"sv), dt);
}
}
zeus::CVector3f plPos = mgr.GetPlayer().GetTranslation();
zeus::CVector3f aimPos = mgr.GetPlayer().GetAimPosition(mgr, 0.f);
zeus::CTransform xf = GetLctrTransform("LCTR_MAGMOUTH"sv);
zeus::CVector3f aimDir = (aimPos - xf.origin).normalized();
2020-04-21 18:15:17 +00:00
float angleDiff = zeus::CVector3f::getAngleDiff(xf.frontVector(), aimDir);
2020-03-01 13:14:39 +00:00
float dVar7 = std::min(angleDiff, zeus::degToRad(x57c_));
2020-04-21 18:15:17 +00:00
if (xf.upVector().dot(aimDir) < 0.f) {
2020-03-01 13:14:39 +00:00
dVar7 = -dVar7;
}
float dVar8 = dVar7 + zeus::degToRad(x57c_) / 2.f * zeus::degToRad(x57c_);
float dVar2 = 1.f - dVar8;
x710_attackOffset = plPos * dVar2 + (aimPos * dVar8);
2020-03-01 13:14:39 +00:00
if (GetActive()) { // lol tautologies
if (IsAlive()) {
GetModelData()->GetAnimationData()->PreRender();
x584_boneTracker.Update(dt);
x584_boneTracker.PreRender(mgr, *GetModelData()->GetAnimationData(), GetTransform(), GetModelData()->GetScale(),
*GetBodyController());
2020-04-21 05:32:16 +00:00
if (auto* fl = static_cast<CFlameThrower*>(mgr.ObjectById(x6c8_flameThrowerId))) {
2020-03-01 13:14:39 +00:00
fl->SetTransform(GetLctrTransform("LCTR_MAGMOUTH"sv), dt);
}
}
x580_collisionManager->Update(dt, mgr, CCollisionActorManager::EUpdateOptions::ObjectSpace);
2020-03-01 13:14:39 +00:00
zeus::CTransform headXf = GetLocatorTransform("head"sv);
MoveCollisionPrimitive(GetTransform().rotate(GetModelData()->GetScale() * headXf.origin));
2020-03-01 13:14:39 +00:00
xe4_27_notInSortedLists = true;
}
if (x750_aiStage == 2) {
2020-04-21 05:32:16 +00:00
if (x738_ * 0.5f <= x734_) {
2020-03-01 13:14:39 +00:00
x740_ -= x73c_ * dt;
2020-04-21 05:32:16 +00:00
} else {
2020-03-01 13:14:39 +00:00
x740_ += x73c_ * dt;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
x734_ += x740_ * dt;
SetTranslation(GetTranslation() - zeus::CVector3f{0.f, 0.f, x740_ * dt});
if (GetTranslation().z() < x728_cachedTarget.z()) {
SetTranslation(x728_cachedTarget);
x750_aiStage = 0;
2020-03-01 13:14:39 +00:00
}
} else if (x750_aiStage == 1) {
2020-04-21 18:15:17 +00:00
if (x738_ * 0.5f <= x734_) {
2020-03-01 13:14:39 +00:00
x740_ -= x73c_ * dt;
2020-04-21 18:15:17 +00:00
} else {
2020-03-01 13:14:39 +00:00
x740_ += x73c_ * dt;
2020-04-21 18:15:17 +00:00
}
2020-03-01 13:14:39 +00:00
x734_ += x740_ * dt;
SetTranslation(GetTranslation() + zeus::CVector3f{0.f, 0.f, x740_ * dt});
if (GetTranslation().z() > x728_cachedTarget.z()) {
SetTranslation(x728_cachedTarget);
x750_aiStage = 0;
2020-03-01 13:14:39 +00:00
}
}
}
2020-04-21 05:32:16 +00:00
void CMagdolite::DoUserAnimEvent(CStateManager& mgr, const CInt32POINode& node, EUserEventType type, float dt) {
2020-03-01 13:14:39 +00:00
if (type == EUserEventType::DamageOff) {
LaunchFlameThrower(mgr, false);
} else if (type == EUserEventType::DamageOn) {
LaunchFlameThrower(mgr, true);
} else if (type == EUserEventType::BreakLockOn) {
RemoveMaterial(EMaterialTypes::Target, EMaterialTypes::Orbit, mgr);
mgr.GetPlayer().SetOrbitRequestForTarget(GetUniqueId(), CPlayer::EPlayerOrbitRequest::ActivateOrbitSource, mgr);
return;
}
CPatterned::DoUserAnimEvent(mgr, node, type, dt);
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
void CMagdolite::Death(CStateManager& mgr, const zeus::CVector3f& direction, EScriptObjectState state) {
2020-04-21 05:32:16 +00:00
if (!IsAlive()) {
2020-03-01 13:14:39 +00:00
return;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
zeus::CTransform tmpXf = GetTransform();
SetTransform(GetLctrTransform("head"sv));
SendScriptMsgs(EScriptObjectState::MassiveDeath, mgr, EScriptObjectMessage::None);
GenerateDeathExplosion(mgr);
SetTransform(tmpXf);
GetModelData()->GetAnimationData()->SubstituteModelData(x690_headlessModel);
x460_knockBackController.SetEnableFreeze(false);
GetBodyController()->UnFreeze();
2020-04-21 05:32:16 +00:00
if (!x754_26_lostMyHead) {
2020-03-01 13:14:39 +00:00
x460_knockBackController.SetSeverity(pas::ESeverity::Two);
2020-04-21 05:32:16 +00:00
} else {
2020-03-01 13:14:39 +00:00
GetModelData()->GetAnimationData()->SubstituteModelData(x690_headlessModel);
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
CPatterned::Death(mgr, direction, state);
}
void CMagdolite::FluidFXThink(EFluidState state, CScriptWater& water, CStateManager& mgr) {
if (x754_28_alert && state == EFluidState::InFluid) {
2020-03-01 13:14:39 +00:00
CFluidPlaneManager* flMgr = mgr.GetFluidPlaneManager();
if (flMgr->GetLastRippleDeltaTime(GetUniqueId()) >= 2.3f) {
zeus::CVector3f center = GetTranslation();
center.z() = float(water.GetTriggerBoundsWR().max.z());
water.GetFluidPlane().AddRipple(3.f, GetUniqueId(), center, water, mgr);
}
}
}
void CMagdolite::SelectTarget(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Update) {
TUniqueId targetId = FindSuitableTarget(mgr, EScriptObjectState::Attack, EScriptObjectMessage::Follow);
if (targetId != kInvalidUniqueId) {
if (TCastToConstPtr<CScriptWaypoint> wp = mgr.GetObjectById(targetId)) {
SetTransform(wp->GetTransform());
x71c_attackTarget = GetTranslation();
2020-03-01 13:14:39 +00:00
}
}
} else if (msg == EStateMsg::Deactivate) {
UpdateOrientation(mgr);
}
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
void CMagdolite::Generate(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
x754_24_retreat = false;
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Update) {
2020-05-13 13:20:40 +00:00
TryCommand(mgr, pas::EAnimationState::Generate, &CPatterned::TryGenerate, 0);
2020-04-21 05:32:16 +00:00
if (x32c_animState == EAnimState::Repeat) {
2020-03-01 13:14:39 +00:00
GetBodyController()->SetLocomotionType(pas::ELocomotionType::Relaxed);
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Deactivate) {
x32c_animState = EAnimState::NotReady;
}
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
void CMagdolite::Deactivate(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
x754_24_retreat = false;
2020-03-01 13:14:39 +00:00
x584_boneTracker.SetActive(false);
} else if (msg == EStateMsg::Update) {
TryCommand(mgr, pas::EAnimationState::Step, &CPatterned::TryStep, int(pas::EStepDirection::Down));
2020-04-21 05:32:16 +00:00
if (x32c_animState == EAnimState::Repeat) {
2020-03-01 13:14:39 +00:00
GetBodyController()->SetLocomotionType(pas::ELocomotionType::Internal7);
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Deactivate) {
x32c_animState = EAnimState::NotReady;
}
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
void CMagdolite::Attack(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
x584_boneTracker.SetActive(false);
zeus::CVector3f plPos = mgr.GetPlayer().GetTranslation();
zeus::CTransform headXf = GetLctrTransform("head"sv);
float zDiff = headXf.origin.z() - plPos.z();
float fVar1 = (zDiff - x74c_);
float fVar2 = 0.f;
if (fVar1 >= 0.f) {
fVar2 = fVar1;
2020-04-21 05:32:16 +00:00
if (x748_ < fVar1) {
2020-03-01 13:14:39 +00:00
fVar2 = x748_;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
}
x728_cachedTarget = x71c_attackTarget - zeus::CVector3f(0.f, 0.f, fVar1 - fVar2);
2020-03-01 13:14:39 +00:00
x740_ = 0.f;
x734_ = 0.f;
x738_ = fVar2;
x73c_ = (2.f * x738_) / (x744_ * x744_);
2020-04-21 05:32:16 +00:00
if (GetTranslation().z() <= x728_cachedTarget.z()) {
x750_aiStage = 1;
2020-04-21 05:32:16 +00:00
} else {
x750_aiStage = 2;
2020-04-21 05:32:16 +00:00
}
x754_30_inProjectileAttack = true;
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Update) {
TryCommand(mgr, pas::EAnimationState::MeleeAttack, &CPatterned::TryMeleeAttack, 1);
zeus::CVector3f direction = (mgr.GetPlayer().GetTranslation().toVec2f() - GetTranslation().toVec2f()).normalized();
2020-04-21 18:15:17 +00:00
float angle = zeus::CVector3f::getAngleDiff(GetTransform().frontVector(), direction);
if (GetTransform().rightVector().dot(direction) > 0.f) {
2020-03-01 13:14:39 +00:00
angle *= -1.f;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
if ((57.29f * angle) <= x3b8_turnSpeed && (57.29f * angle) < -x3b8_turnSpeed) {
angle = zeus::degToRad(-x3b8_turnSpeed);
} else {
angle = zeus::degToRad(x3b8_turnSpeed);
}
RotateInOneFrameOR(zeus::CQuaternion::fromAxisAngle({0.f, 0.f, 1.f}, angle), arg);
} else if (msg == EStateMsg::Deactivate) {
x32c_animState = EAnimState::NotReady;
LaunchFlameThrower(mgr, false);
x750_aiStage = 1;
x728_cachedTarget = x71c_attackTarget;
2020-03-01 13:14:39 +00:00
x740_ = 0.f;
x744_ = 0.f;
x738_ = x728_cachedTarget.z() - GetTranslation().z();
2020-03-01 13:14:39 +00:00
x73c_ = (2.f * x738_) / (x744_ * x744_);
x754_30_inProjectileAttack = false;
2020-03-01 13:14:39 +00:00
}
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
void CMagdolite::Active(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
GetBodyController()->SetLocomotionType(pas::ELocomotionType::Lurk);
x584_boneTracker.SetActive(true);
x584_boneTracker.SetTarget(mgr.GetPlayer().GetUniqueId());
x400_24_hitByPlayerProjectile = false;
x754_29_useDetectionRange = false;
x330_stateMachineState.SetDelay(x568_initialDelay);
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Update) {
zeus::CVector3f posDiff = (mgr.GetPlayer().GetTranslation().toVec2f() - GetTranslation().toVec2f());
if (posDiff.canBeNormalized()) {
posDiff.normalize();
2020-04-21 18:15:17 +00:00
if (GetTransform().frontVector().dot(posDiff) < x578_losMaxDistance) {
2020-03-01 13:14:39 +00:00
GetBodyController()->GetCommandMgr().DeliverCmd(CBCLocomotionCmd({}, posDiff, 1.f));
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
}
} else if (msg == EStateMsg::Deactivate) {
x330_stateMachineState.SetDelay(((x570_maxDelay - x56c_minDelay) * mgr.GetActiveRandom()->Float()) + x56c_minDelay);
2020-03-01 13:14:39 +00:00
}
}
void CMagdolite::InActive(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
xe7_27_enableRender = false;
GetBodyController()->SetLocomotionType(pas::ELocomotionType::Internal7);
RemoveMaterial(EMaterialTypes::Orbit, EMaterialTypes::Target, mgr);
} else if (msg == EStateMsg::Deactivate) {
AddMaterial(EMaterialTypes::Orbit, EMaterialTypes::Target, mgr);
x754_25_up = false;
2020-03-01 13:14:39 +00:00
UpdateOrientation(mgr);
xe7_27_enableRender = true;
}
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
void CMagdolite::GetUp(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
x754_24_retreat = false;
x754_28_alert = true;
2020-03-01 13:14:39 +00:00
AddMaterial(EMaterialTypes::Orbit, EMaterialTypes::Target, mgr);
} else if (msg == EStateMsg::Update) {
2020-04-21 05:32:16 +00:00
if (!x754_25_up) {
2020-03-01 13:14:39 +00:00
TryCommand(mgr, pas::EAnimationState::Step, &CPatterned::TryStep, int(pas::EStepDirection::Forward));
2020-04-21 05:32:16 +00:00
} else {
2020-03-01 13:14:39 +00:00
TryCommand(mgr, pas::EAnimationState::Step, &CPatterned::TryStep, int(pas::EStepDirection::Up));
2020-04-21 05:32:16 +00:00
}
if (x32c_animState == EAnimState::Repeat) {
2020-03-01 13:14:39 +00:00
GetBodyController()->SetLocomotionType(pas::ELocomotionType::Lurk);
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Deactivate) {
x32c_animState = EAnimState::NotReady;
x754_28_alert = false;
2020-03-01 13:14:39 +00:00
}
}
void CMagdolite::Taunt(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
x584_boneTracker.SetActive(true);
} else if (msg == EStateMsg::Update) {
TryCommand(mgr, pas::EAnimationState::Taunt, &CPatterned::TryTaunt, 1);
} else if (msg == EStateMsg::Deactivate) {
x32c_animState = EAnimState::NotReady;
x584_boneTracker.SetActive(false);
}
}
void CMagdolite::Lurk(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
GetBodyController()->SetLocomotionType(pas::ELocomotionType::Relaxed);
x584_boneTracker.SetActive(false);
x330_stateMachineState.SetDelay(0.f);
AddMaterial(EMaterialTypes::Target, EMaterialTypes::Orbit, mgr);
} else if (msg == EStateMsg::Deactivate) {
x754_25_up = true;
2020-03-01 13:14:39 +00:00
}
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
void CMagdolite::ProjectileAttack(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
LaunchFlameThrower(mgr, true);
x584_boneTracker.SetActive(true);
x584_boneTracker.SetNoHorizontalAim(true);
x584_boneTracker.SetTarget(kInvalidUniqueId);
x754_30_inProjectileAttack = true;
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Update) {
if (TooClose(mgr, 0.f)) {
TryCommand(mgr, pas::EAnimationState::ProjectileAttack, &CPatterned::TryProjectileAttack, 0);
} else {
TryCommand(mgr, pas::EAnimationState::ProjectileAttack, &CPatterned::TryProjectileAttack, 1);
}
x584_boneTracker.SetTargetPosition(x710_attackOffset);
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Deactivate) {
x32c_animState = EAnimState::NotReady;
LaunchFlameThrower(mgr, false);
x584_boneTracker.SetActive(false);
x584_boneTracker.SetNoHorizontalAim(false);
x754_30_inProjectileAttack = false;
2020-03-01 13:14:39 +00:00
x584_boneTracker.SetTarget(mgr.GetPlayer().GetUniqueId());
}
}
void CMagdolite::Flinch(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
x400_24_hitByPlayerProjectile = false;
x584_boneTracker.SetActive(false);
} else if (msg == EStateMsg::Update) {
TryCommand(mgr, pas::EAnimationState::KnockBack, &CPatterned::TryKnockBack, 0);
} else if (msg == EStateMsg::Deactivate) {
x32c_animState = EAnimState::NotReady;
}
}
void CMagdolite::Retreat(CStateManager& mgr, EStateMsg msg, float arg) {
if (msg == EStateMsg::Activate) {
x32c_animState = EAnimState::Ready;
x754_24_retreat = false;
2020-03-01 13:14:39 +00:00
x584_boneTracker.SetActive(false);
GetBodyController()->GetCommandMgr().DeliverCmd(CBodyStateCmd(EBodyStateCmd::NextState));
x754_28_alert = true;
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Update) {
2020-05-13 13:20:40 +00:00
TryCommand(mgr, pas::EAnimationState::Generate, &CPatterned::TryGenerateNoXf, 1);
2020-04-21 05:32:16 +00:00
if (x32c_animState == EAnimState::Repeat) {
2020-03-01 13:14:39 +00:00
GetBodyController()->SetLocomotionType(pas::ELocomotionType::Internal7);
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
} else if (msg == EStateMsg::Deactivate) {
x754_28_alert = false;
2020-03-01 13:14:39 +00:00
x32c_animState = EAnimState::NotReady;
}
}
bool CMagdolite::InAttackPosition(CStateManager& mgr, float arg) {
zeus::CTransform xf = GetLctrTransform("head"sv);
zeus::CVector3f plAimPos = mgr.GetPlayer().GetAimPosition(mgr, 0.f);
zeus::CVector3f diff = (plAimPos - GetTranslation());
2020-04-21 05:32:16 +00:00
if (GetTranslation().z() < plAimPos.z() && plAimPos.z() < xf.origin.z()) {
2020-03-01 13:14:39 +00:00
diff.z() = 0.f;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
if (std::fabs(diff.magnitude()) >= FLT_EPSILON) {
2020-04-21 18:15:17 +00:00
return ((1.f / diff.magnitude()) * diff).dot(GetTransform().frontVector()) < x578_losMaxDistance;
2020-03-01 13:14:39 +00:00
}
return false;
}
bool CMagdolite::Leash(CStateManager& mgr, float arg) {
float dist = (GetTranslation() - mgr.GetPlayer().GetTranslation()).magSquared();
bool ret = dist < x3c8_leashRadius * x3c8_leashRadius;
return ret;
}
bool CMagdolite::HasAttackPattern(CStateManager& mgr, float arg) {
2020-04-21 06:15:02 +00:00
return FindSuitableTarget(mgr, EScriptObjectState::Attack, EScriptObjectMessage::Follow) != kInvalidUniqueId;
2020-03-01 13:14:39 +00:00
}
2020-04-21 05:32:16 +00:00
2020-03-01 13:14:39 +00:00
bool CMagdolite::LineOfSight(CStateManager& mgr, float arg) {
zeus::CTransform mouthXf = GetLctrTransform("LCTR_MAGMOUTH"sv);
zeus::CVector3f diff = x710_attackOffset - mouthXf.origin;
2020-03-01 13:14:39 +00:00
if (diff.canBeNormalized()) {
diff.normalize();
2020-04-21 18:15:17 +00:00
if (diff.dot(GetTransform().frontVector()) < x578_losMaxDistance) {
2020-03-01 13:14:39 +00:00
return false;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
}
bool ret = mgr.RayCollideWorld(
mouthXf.origin, x710_attackOffset,
2020-03-01 13:14:39 +00:00
CMaterialFilter::MakeIncludeExclude({EMaterialTypes::Solid, EMaterialTypes::Character},
{EMaterialTypes::Player, EMaterialTypes::ProjectilePassthrough}),
this);
return ret;
}
2020-04-21 05:32:16 +00:00
bool CMagdolite::ShouldRetreat(CStateManager& mgr, float arg) { return x754_24_retreat; }
2020-03-01 13:14:39 +00:00
void CMagdolite::UpdateOrientation(CStateManager& mgr) {
zeus::CVector3f plDiff = (mgr.GetPlayer().GetTranslation().toVec2f() - GetTranslation().toVec2f());
plDiff = plDiff.normalized();
2020-04-21 18:15:17 +00:00
float angle = zeus::CVector3f::getAngleDiff(GetTransform().frontVector(), plDiff);
if (GetTransform().rightVector().dot(plDiff) > 0.f) {
2020-03-01 13:14:39 +00:00
angle *= -1.f;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
zeus::CQuaternion q = GetTransform().basis;
q.rotateZ(angle);
SetTransform(q.toTransform(GetTranslation()));
}
TUniqueId CMagdolite::FindSuitableTarget(CStateManager& mgr, EScriptObjectState state, EScriptObjectMessage msg) {
float lastDistance = FLT_MAX;
int wpCount = 0;
float maxDistanceSq =
x754_29_useDetectionRange ? x3bc_detectionRange * x3bc_detectionRange : x300_maxAttackRange * x300_maxAttackRange;
2020-03-01 13:14:39 +00:00
TUniqueId tmpId = kInvalidUniqueId;
for (const auto& conn : x20_conns) {
2020-04-21 05:32:16 +00:00
if (conn.x0_state != state || conn.x4_msg != msg) {
2020-03-01 13:14:39 +00:00
continue;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
TUniqueId uid = mgr.GetIdForScript(conn.x8_objId);
if (const CEntity* ent = mgr.GetObjectById(uid)) {
2020-04-21 05:32:16 +00:00
if (!ent->GetActive()) {
2020-03-01 13:14:39 +00:00
continue;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
if (TCastToConstPtr<CScriptWaypoint> wp = ent) {
++wpCount;
const float dist = (wp->GetTranslation() - mgr.GetPlayer().GetTranslation()).magSquared();
if (dist < maxDistanceSq) {
if (dist < lastDistance) {
lastDistance = dist;
tmpId = uid;
}
}
}
}
}
if (!x754_29_useDetectionRange) {
2020-03-01 13:14:39 +00:00
int skipCount = mgr.GetActiveRandom()->Next();
skipCount = skipCount - (skipCount / wpCount) * wpCount;
for (const auto& conn : x20_conns) {
2020-04-21 05:32:16 +00:00
if (conn.x0_state != state || conn.x4_msg != msg) {
2020-03-01 13:14:39 +00:00
continue;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
TUniqueId uid = mgr.GetIdForScript(conn.x8_objId);
if (const CEntity* ent = mgr.GetObjectById(uid)) {
2020-04-21 05:32:16 +00:00
if (!ent->GetActive()) {
2020-03-01 13:14:39 +00:00
continue;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
if (TCastToConstPtr<CScriptWaypoint>(ent)) {
2020-03-01 13:14:39 +00:00
tmpId = uid;
2020-04-21 05:32:16 +00:00
if (skipCount == 0) {
2020-03-01 13:14:39 +00:00
break;
2020-04-21 05:32:16 +00:00
}
2020-03-01 13:14:39 +00:00
--skipCount;
}
}
}
}
return tmpId;
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce::MP1