metaforce/Runtime/World/CScriptDock.cpp

240 lines
8.4 KiB
C++
Raw Permalink Normal View History

#include "Runtime/World/CScriptDock.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/Character/CModelData.hpp"
#include "Runtime/Collision/CMaterialList.hpp"
#include "Runtime/Particle/CGenDescription.hpp"
#include "Runtime/World/CActorParameters.hpp"
#include "Runtime/World/CPlayer.hpp"
#include "Runtime/World/CScriptDoor.hpp"
#include "Runtime/World/CWorld.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2016-04-20 21:07:18 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
constexpr auto skDockMaterialList =
CMaterialList{EMaterialTypes::Trigger, EMaterialTypes::Immovable, EMaterialTypes::AIBlock};
2016-04-20 21:07:18 +00:00
CScriptDock::CScriptDock(TUniqueId uid, std::string_view name, const CEntityInfo& info, const zeus::CVector3f& position,
const zeus::CVector3f& extents, s32 dock, TAreaId area, bool active, s32 dockReferenceCount,
bool loadConnected)
: CPhysicsActor(uid, active, name, info, zeus::CTransform(zeus::CMatrix3f(), position), CModelData::CModelDataNull(),
skDockMaterialList, zeus::CAABox(-extents * 0.5f, extents * 0.5f), SMoverData(1.f),
CActorParameters::None(), 0.3f, 0.1f)
, x258_dockReferenceCount(dockReferenceCount)
, x25c_dock(dock)
, x260_area(area)
, x268_25_loadConnected(loadConnected) {}
2016-12-22 02:48:22 +00:00
void CScriptDock::Accept(IVisitor& visitor) { visitor.Visit(this); }
2017-01-15 03:07:01 +00:00
2018-12-08 05:30:43 +00:00
void CScriptDock::Think(float dt, CStateManager& mgr) {
if (!GetActive()) {
UpdateAreaActivateFlags(mgr);
x268_24_dockReferenced = false;
}
const IGameArea::Dock* gameDock = mgr.GetWorld()->GetArea(x260_area)->GetDock(x25c_dock);
const TAreaId connArea = gameDock->GetConnectedAreaId(gameDock->GetReferenceCount());
2018-12-08 05:30:43 +00:00
if (connArea != kInvalidAreaId) {
const bool connPostConstructed = mgr.GetWorld()->GetArea(connArea)->IsPostConstructed();
2018-12-08 05:30:43 +00:00
if (x268_26_areaPostConstructed != connPostConstructed) {
x268_26_areaPostConstructed = connPostConstructed;
if (connPostConstructed) {
2018-12-08 05:30:43 +00:00
CEntity::SendScriptMsgs(EScriptObjectState::MaxReached, mgr, EScriptObjectMessage::None);
} else {
2018-12-08 05:30:43 +00:00
CEntity::SendScriptMsgs(EScriptObjectState::Zero, mgr, EScriptObjectMessage::None);
}
}
2018-12-08 05:30:43 +00:00
}
if (mgr.GetNextAreaId() != x260_area) {
x264_dockState = EDockState::InNextRoom;
} else if (x264_dockState == EDockState::InNextRoom) {
x264_dockState = EDockState::InSourceRoom;
} else if (x264_dockState == EDockState::PlayerTouched) {
2018-12-08 05:30:43 +00:00
x264_dockState = EDockState::EnterNextArea;
} else if (x264_dockState == EDockState::EnterNextArea) {
2018-12-08 05:30:43 +00:00
CPlayer& player = mgr.GetPlayer();
if (HasPointCrossedDock(mgr, player.GetTranslation())) {
const IGameArea::Dock* dock = mgr.GetWorld()->GetArea(mgr.GetNextAreaId())->GetDock(x25c_dock);
const TAreaId aid = dock->GetConnectedAreaId(dock->GetReferenceCount());
if (aid != kInvalidAreaId && mgr.GetWorld()->GetArea(aid)->GetActive()) {
2018-12-08 05:30:43 +00:00
mgr.SetCurrentAreaId(aid);
const s32 otherDock = dock->GetOtherDockNumber(dock->GetReferenceCount());
2018-12-08 05:30:43 +00:00
if (CObjectList* objs = mgr.GetWorld()->GetArea(aid)->GetAreaObjects()) {
2018-12-08 05:30:43 +00:00
for (CEntity* ent : *objs) {
const TCastToPtr<CScriptDock> dock2(ent);
if (dock2 && dock2->GetDockId() == otherDock) {
dock2->SetLoadConnected(mgr, true);
}
2018-12-08 05:30:43 +00:00
}
2018-10-27 06:16:12 +00:00
}
2018-12-08 05:30:43 +00:00
}
}
x264_dockState = EDockState::InSourceRoom;
2018-12-08 05:30:43 +00:00
}
}
2018-12-08 05:30:43 +00:00
void CScriptDock::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
switch (msg) {
case EScriptObjectMessage::Registered: {
CGameArea* area = mgr.GetWorld()->GetArea(x260_area);
if (area->GetDockCount() < x25c_dock) {
2018-12-08 05:30:43 +00:00
return;
}
IGameArea::Dock* dock = area->GetDock(x25c_dock);
if (!dock->IsReferenced()) {
2018-12-08 05:30:43 +00:00
dock->SetReferenceCount(x258_dockReferenceCount);
}
2018-12-08 05:30:43 +00:00
} break;
case EScriptObjectMessage::Deleted:
CleanUp();
break;
2018-12-08 05:30:43 +00:00
case EScriptObjectMessage::InitializedInArea:
AreaLoaded(mgr);
break;
2018-12-08 05:30:43 +00:00
case EScriptObjectMessage::WorldInitialized: {
UpdateAreaActivateFlags(mgr);
const CMaterialList exclude = GetMaterialFilter().GetExcludeList();
2018-12-08 05:30:43 +00:00
CMaterialList include = GetMaterialFilter().GetIncludeList();
include.Add(EMaterialTypes::AIBlock);
SetMaterialFilter(CMaterialFilter::MakeIncludeExclude(include, exclude));
} break;
case EScriptObjectMessage::SetToZero: {
if (mgr.GetNextAreaId() != x260_area) {
2018-12-08 05:30:43 +00:00
return;
}
2018-12-08 05:30:43 +00:00
SetLoadConnected(mgr, false);
const CGameArea* area = mgr.GetWorld()->GetArea(x260_area);
const IGameArea::Dock* dock = area->GetDock(x25c_dock);
const TAreaId aid = dock->GetConnectedAreaId(dock->GetReferenceCount());
2018-12-08 05:30:43 +00:00
CPlatformAndDoorList& lst = mgr.GetPlatformAndDoorObjectList();
for (CEntity* ent : lst) {
const TCastToPtr<CScriptDoor> door(ent);
if (door && door->IsConnectedToArea(mgr, aid)) {
2018-12-08 05:30:43 +00:00
door->ForceClosed(mgr);
}
}
2018-12-08 05:30:43 +00:00
} break;
case EScriptObjectMessage::SetToMax:
if (mgr.GetNextAreaId() != x260_area) {
2018-12-08 05:30:43 +00:00
return;
}
2018-12-08 05:30:43 +00:00
SetLoadConnected(mgr, true);
break;
2018-12-08 05:30:43 +00:00
case EScriptObjectMessage::Increment:
SetLoadConnected(mgr, true);
[[fallthrough]];
case EScriptObjectMessage::Decrement: {
TAreaId aid = x260_area;
if (mgr.GetNextAreaId() == x260_area) {
const IGameArea::Dock* dock = mgr.GetWorld()->GetArea(x260_area)->GetDock(x25c_dock);
2018-12-08 05:30:43 +00:00
aid = dock->GetConnectedAreaId(dock->GetReferenceCount());
}
2018-12-08 05:30:43 +00:00
if (aid >= 0 && aid < mgr.GetWorld()->GetNumAreas() && mgr.GetWorld()->GetArea(aid)->GetActive()) {
2018-12-08 05:30:43 +00:00
CWorld::PropogateAreaChain(CGameArea::EOcclusionState(msg == EScriptObjectMessage::Increment),
mgr.GetWorld()->GetArea(aid), mgr.GetWorld());
}
2018-12-08 05:30:43 +00:00
} break;
default:
CPhysicsActor::AcceptScriptMsg(msg, uid, mgr);
break;
}
}
std::optional<zeus::CAABox> CScriptDock::GetTouchBounds() const {
if (x264_dockState == EDockState::InNextRoom) {
return std::nullopt;
}
2018-12-08 05:30:43 +00:00
return GetBoundingBox();
}
2018-12-08 05:30:43 +00:00
void CScriptDock::Touch(CActor& act, CStateManager&) {
if (x264_dockState == EDockState::InNextRoom) {
2018-12-08 05:30:43 +00:00
return;
}
if (TCastToConstPtr<CPlayer>(act)) {
2018-12-08 05:30:43 +00:00
x264_dockState = EDockState::PlayerTouched;
}
}
2018-12-08 05:30:43 +00:00
zeus::CPlane CScriptDock::GetPlane(const CStateManager& mgr) const {
const IGameArea::Dock* dock = mgr.GetWorld()->GetAreaAlways(x260_area)->GetDock(x25c_dock);
return zeus::CPlane(dock->GetPoint(0), dock->GetPoint(1), dock->GetPoint(2));
}
2018-12-08 05:30:43 +00:00
void CScriptDock::SetDockReference(CStateManager& mgr, s32 ref) {
mgr.GetWorld()->GetArea(x260_area)->GetDock(x25c_dock)->SetReferenceCount(ref);
2018-12-08 05:30:43 +00:00
x268_24_dockReferenced = true;
}
2018-12-08 05:30:43 +00:00
s32 CScriptDock::GetDockReference(const CStateManager& mgr) const {
return mgr.GetWorld()->GetAreaAlways(x260_area)->GetDock(x25c_dock)->GetReferenceCount();
}
2018-12-08 05:30:43 +00:00
TAreaId CScriptDock::GetCurrentConnectedAreaId(const CStateManager& mgr) const {
if (x260_area >= mgr.GetWorld()->GetNumAreas()) {
2018-12-08 05:30:43 +00:00
return kInvalidAreaId;
}
2018-12-08 05:30:43 +00:00
const CGameArea* area = mgr.GetWorld()->GetAreaAlways(x260_area);
if (x25c_dock >= area->GetDockCount()) {
2018-12-08 05:30:43 +00:00
return kInvalidAreaId;
}
2018-12-08 05:30:43 +00:00
const IGameArea::Dock* dock = area->GetDock(x25c_dock);
return dock->GetConnectedAreaId(dock->GetReferenceCount());
}
2018-12-08 05:30:43 +00:00
void CScriptDock::UpdateAreaActivateFlags(CStateManager& mgr) {
CWorld* world = mgr.GetWorld();
if (x260_area >= world->GetNumAreas()) {
2018-12-08 05:30:43 +00:00
return;
}
2018-12-08 05:30:43 +00:00
CGameArea* area = world->GetArea(x260_area);
if (x25c_dock >= area->GetDockCount())
return;
const IGameArea::Dock* dock = area->GetDock(x25c_dock);
2018-12-08 05:30:43 +00:00
for (s32 i = 0; i < dock->GetDockRefs().size(); ++i) {
const s32 dockRefCount = dock->GetReferenceCount();
const TAreaId aid = dock->GetConnectedAreaId(i);
if (aid != kInvalidAreaId) {
2018-12-08 05:30:43 +00:00
world->GetArea(aid)->SetActive(i == dockRefCount);
}
2018-12-08 05:30:43 +00:00
}
mgr.SetCurrentAreaId(mgr.GetNextAreaId());
}
2018-12-08 05:30:43 +00:00
bool CScriptDock::HasPointCrossedDock(const CStateManager& mgr, const zeus::CVector3f& point) const {
const zeus::CPlane plane = GetPlane(mgr);
return plane.pointToPlaneDist(point) >= 0.f;
2016-12-22 02:48:22 +00:00
}
void CScriptDock::AreaLoaded(CStateManager& mgr) { SetLoadConnected(mgr, x268_25_loadConnected); }
2018-12-08 05:30:43 +00:00
void CScriptDock::SetLoadConnected(CStateManager& mgr, bool loadOther) {
IGameArea::Dock* dock = mgr.GetWorld()->GetArea(x260_area)->GetDock(x25c_dock);
const bool cur = dock->GetShouldLoadOther(dock->GetReferenceCount());
if (cur == loadOther) {
2018-12-08 05:30:43 +00:00
return;
}
2018-12-08 05:30:43 +00:00
dock->SetShouldLoadOther(dock->GetReferenceCount(), loadOther);
2016-12-22 02:48:22 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce