From 34143048eb72e60a87e302463e98c911d5e5e4fb Mon Sep 17 00:00:00 2001 From: MonsterDruide1 <5958456@gmail.com> Date: Thu, 5 Aug 2021 01:33:20 +0200 Subject: [PATCH] ksys/act: Add LinkTag --- data/uking_functions.csv | 44 +- src/KingSystem/ActorSystem/CMakeLists.txt | 2 + src/KingSystem/ActorSystem/actActor.h | 5 +- src/KingSystem/ActorSystem/actBaseProcMgr.h | 3 + src/KingSystem/ActorSystem/actLinkTag.cpp | 619 ++++++++++++++++++++ src/KingSystem/ActorSystem/actLinkTag.h | 97 +++ src/KingSystem/GameData/gdtManager.h | 5 + src/KingSystem/Map/mapObject.h | 3 + src/KingSystem/Map/mapObjectLink.h | 17 + src/KingSystem/System/CMakeLists.txt | 1 + src/KingSystem/System/MCMgr.h | 55 ++ 11 files changed, 828 insertions(+), 23 deletions(-) create mode 100644 src/KingSystem/ActorSystem/actLinkTag.cpp create mode 100644 src/KingSystem/ActorSystem/actLinkTag.h create mode 100644 src/KingSystem/System/MCMgr.h diff --git a/data/uking_functions.csv b/data/uking_functions.csv index c6f3fc339..7e63ca120 100644 --- a/data/uking_functions.csv +++ b/data/uking_functions.csv @@ -73700,31 +73700,31 @@ Address,Quality,Size,Name 0x0000007100d375e4,O,000076,_ZN4ksys3map16LazyTraverseListD1Ev 0x0000007100d37630,O,000132,_ZN4ksys3map16LazyTraverseList11updateFlagsEv 0x0000007100d376b4,O,000216,_ZN4ksys3map16LazyTraverseList16wereFlagsUpdatedEv -0x0000007100d3778c,U,000204,LinkTag::construct -0x0000007100d37858,U,000280,LinkTag::calc -0x0000007100d37970,U,000064,LinkTag::dtor -0x0000007100d379b0,U,000072,LinkTag::dtorDelete -0x0000007100d379f8,U,001656,LinkTag::init2 +0x0000007100d3778c,O,000204,_ZN4ksys3act7LinkTag9constructERKNS0_8BaseProc9CreateArgEPN4sead4HeapE +0x0000007100d37858,O,000280,_ZN4ksys3act7LinkTag4calcEv +0x0000007100d37970,O,000064,_ZN4ksys3act7LinkTagD1Ev +0x0000007100d379b0,O,000072,_ZN4ksys3act7LinkTagD0Ev +0x0000007100d379f8,W,001656,_ZN4ksys3act7LinkTag4initEv 0x0000007100d38070,U,000256,isLinkTagNAndOrNOr 0x0000007100d38170,U,000308,map::isFlagSet -0x0000007100d382a4,U,000056,LinkTag::finalizeInit -0x0000007100d382dc,U,000188,LinkTag::prepareForPreDelete -0x0000007100d38398,U,000092,LinkTag::isDonePreparingForPreDelete -0x0000007100d383f4,U,000320,LinkTag::calcCount -0x0000007100d38534,U,000748,LinkTag::calcPulse -0x0000007100d38820,U,001372,LinkTag::calcOther -0x0000007100d38d7c,U,000372,LinkTag::onEnterCalc -0x0000007100d38ef0,U,000076,LinkTag::hasJobType -0x0000007100d38f3c,U,000132,LinkTag::queueExtraJobPush -0x0000007100d38fc0,U,000008,LinkTag::isSpecialJobType -0x0000007100d38fc8,U,000148,LinkTag::canWakeUp -0x0000007100d3905c,U,000064,LinkTag::shouldSkipJobPush_ -0x0000007100d3909c,U,000132,LinkTag::prePushJob2 +0x0000007100d382a4,O,000056,_ZN4ksys3act7LinkTag12finalizeInitEPNS0_8BaseProc11InitContextE +0x0000007100d382dc,O,000188,_ZN4ksys3act7LinkTag20prepareForPreDelete_Ev +0x0000007100d38398,O,000092,_ZN4ksys3act7LinkTag27isDonePreparingForPreDeleteEv +0x0000007100d383f4,W,000320,_ZN4ksys3act7LinkTag9calcCountEb +0x0000007100d38534,W,000748,_ZN4ksys3act7LinkTag9calcPulseEb +0x0000007100d38820,W,001372,_ZN4ksys3act7LinkTag9calcOtherEb +0x0000007100d38d7c,O,000372,_ZN4ksys3act7LinkTag11onEnterCalcEv +0x0000007100d38ef0,O,000076,_ZN4ksys3act7LinkTag10hasJobTypeENS0_7JobTypeE +0x0000007100d38f3c,O,000132,_ZN4ksys3act7LinkTag17queueExtraJobPushENS0_7JobTypeEj +0x0000007100d38fc0,O,000008,_ZN4ksys3act7LinkTag16isSpecialJobTypeEv +0x0000007100d38fc8,O,000148,_ZN4ksys3act7LinkTag9canWakeUpEv +0x0000007100d3905c,O,000064,_ZN4ksys3act7LinkTag17shouldSkipJobPushEv +0x0000007100d3909c,O,000132,_ZN4ksys3act7LinkTag11prePushJob2Ev 0x0000007100d39120,U,000172, -0x0000007100d391cc,U,000596,LinkTag::updateIsFlagSetFlag +0x0000007100d391cc,O,000596,_ZN4ksys3act7LinkTag19updateIsFlagSetFlagEbbb 0x0000007100d39420,U,000052, 0x0000007100d39454,U,000588,isAreaOrLinkTagOrOtherTag -0x0000007100d396a0,U,000584,LinkTag::isTriggered +0x0000007100d396a0,W,000584,_ZN4ksys3act7LinkTag11isTriggeredEPNS_3map10ObjectLinkEh 0x0000007100d398e8,U,000124,PlacementObj::checkRevivalMaybe 0x0000007100d39964,U,000492,map::getLinkTagRevivalGameDataFlagHash 0x0000007100d39b50,U,000332,map::getLinkTagSaveFlagName @@ -73732,8 +73732,8 @@ Address,Quality,Size,Name 0x0000007100d39d60,U,000132, 0x0000007100d39de4,U,000072, 0x0000007100d39e2c,U,000028, -0x0000007100d39e48,U,000204,LinkTag::rtti1 -0x0000007100d39f14,U,000092,LinkTag::rtti2 +0x0000007100d39e48,O,000204,_ZNK4ksys3act7LinkTag27checkDerivedRuntimeTypeInfoEPKN4sead15RuntimeTypeInfo9InterfaceE +0x0000007100d39f14,O,000092,_ZNK4ksys3act7LinkTag18getRuntimeTypeInfoEv 0x0000007100d39f70,O,000004,_ZN4ksys3act8BaseProc18onDeleteRequested_ENS1_12DeleteReasonE 0x0000007100d39f74,O,000004,_ZN4ksys3act8BaseProc17onSleepRequested_ENS1_15SleepWakeReasonE 0x0000007100d39f78,O,000004,_ZN4ksys3act8BaseProc18onWakeUpRequested_ENS1_15SleepWakeReasonE diff --git a/src/KingSystem/ActorSystem/CMakeLists.txt b/src/KingSystem/ActorSystem/CMakeLists.txt index 79574c50b..5c70edf90 100644 --- a/src/KingSystem/ActorSystem/CMakeLists.txt +++ b/src/KingSystem/ActorSystem/CMakeLists.txt @@ -108,6 +108,8 @@ target_sources(uking PRIVATE actInstParamPack.cpp actInstParamPack.h actLifeRecoveryInfo.h + actLinkTag.cpp + actLinkTag.h actPhysicsConstraints.cpp actPhysicsConstraints.h actPhysicsUserTag.cpp diff --git a/src/KingSystem/ActorSystem/actActor.h b/src/KingSystem/ActorSystem/actActor.h index ce14c73a2..42f095fdc 100644 --- a/src/KingSystem/ActorSystem/actActor.h +++ b/src/KingSystem/ActorSystem/actActor.h @@ -16,6 +16,7 @@ #include "KingSystem/ActorSystem/actPhysicsConstraints.h" #include "KingSystem/ActorSystem/actPhysicsUserTag.h" #include "KingSystem/Map/mapMubinIter.h" +#include "KingSystem/Map/mapObjectLink.h" #include "KingSystem/Utils/AtomicLongBitFlag.h" #include "KingSystem/Utils/Thread/ActorMessageTransceiver.h" @@ -137,6 +138,7 @@ class Actor : public BaseProc, public ActorMessageTransceiver::IHandler { const ActorParam* getParam() const { return mActorParam; } map::Object* getMapObject() const { return mMapObject; } const map::MubinIter& getMapObjIter() const { return mMapObjIter; } + map::MapLinkDefType getEmittedSignalType() const { return mEmittedSignalType; } const sead::Matrix34f& getMtx() const { return mMtx; } const sead::Vector3f& getVelocity() const { return mVelocity; } @@ -457,7 +459,8 @@ class Actor : public BaseProc, public ActorMessageTransceiver::IHandler { /* 0x700 */ int _700 = 0; /* 0x708 */ ImpulseBaseProcLink* mImpulseBaseProcLink = nullptr; /* 0x710 */ sead::TypedBitFlag mStasisFlags; // TODO: probably need to rename this - /* 0x714 */ float mLodLoadDistanceMultiplier = 1.0; + /* 0x714 */ map::MapLinkDefType mEmittedSignalType; + ///* 0x714 */ float mLodLoadDistanceMultiplier = 1.0; /* 0x718 */ float _718 = 0.0; /* 0x71c */ sead::BitFlag32 mSignals; /* 0x720 */ sead::BitFlag32 _720; diff --git a/src/KingSystem/ActorSystem/actBaseProcMgr.h b/src/KingSystem/ActorSystem/actBaseProcMgr.h index 6ac3a7622..870201ce3 100644 --- a/src/KingSystem/ActorSystem/actBaseProcMgr.h +++ b/src/KingSystem/ActorSystem/actBaseProcMgr.h @@ -217,12 +217,15 @@ class BaseProcMgr { void incrementPendingDeletions() { mNumPendingDeletions.increment(); } void decrementPendingDeletions() { mNumPendingDeletions.decrement(); } + s8 getCurrentExtraJobArrayIdx() const { return mCurrentExtraJobArrayIdx; } Status getStatus() const { return mStatus; } JobType getJobType() const { return mJobType; } u32 getNumJobTypes() const { return mJobLists.size(); } BaseProcJobLists& getJobLists(JobType type) { return mJobLists[u32(type)]; } bool isPushingJobs() const { return mIsPushingJobs; } + void setPushActorJobType3InsteadOf6(bool value) { mPushActorJobType3InsteadOf6 = value; } + static u32 sConstant0; static u32 sConstant1; static u32 sConstant2; diff --git a/src/KingSystem/ActorSystem/actLinkTag.cpp b/src/KingSystem/ActorSystem/actLinkTag.cpp new file mode 100644 index 000000000..fb2e21a06 --- /dev/null +++ b/src/KingSystem/ActorSystem/actLinkTag.cpp @@ -0,0 +1,619 @@ +#include "KingSystem/ActorSystem/actLinkTag.h" + +#include +#include "KingSystem/ActorSystem/actActor.h" +#include "KingSystem/ActorSystem/actBaseProcMgr.h" +#include "KingSystem/GameData/gdtManager.h" +#include "KingSystem/Map/mapObject.h" +#include "KingSystem/System/MCMgr.h" +#include "KingSystem/System/SystemTimers.h" + +namespace ksys::act { + +LinkTag* LinkTag::construct(const BaseProc::CreateArg& arg, sead::Heap* heap) { + return new (heap, std::nothrow) LinkTag(arg); +} + +LinkTag::LinkTag(const BaseProc::CreateArg& arg) : BaseProc(arg), mJob(this, &LinkTag::calc) { + mJobHandlers[3] = &mJob; +} + +LinkTag::~LinkTag() { + if (getSceneStatus() != 4) { + mJobFlags.load(); + } +} + +bool LinkTag::init() { + if (mName == "LinkTagOr") + mLinkTagType = Type::Or; + else if (mName == "LinkTagNOr") + mLinkTagType = Type::NOr; + else if (mName == "LinkTagNAnd") + mLinkTagType = Type::NAnd; + else if (mName == "LinkTagXOr") + mLinkTagType = Type::XOr; + else if (mName == "LinkTagAnd") + mLinkTagType = Type::And; + else if (mName == "LinkTagCount") + mLinkTagType = Type::Count; + else if (mName == "LinkTagPulse") + mLinkTagType = Type::Pulse; + else if (mName == "LinkTagNone") + mLinkTagType = Type::None; + + if (mName == "LinkTagNAnd" || mName == "LinkTagNOr") + mFlags.set(LinkTagFlag::_4); + + bool noChangeSignalValue = false; + if (mMubinIter.tryGetParamBoolByKey(&noChangeSignalValue, "NoChangeSignal")) { + if (noChangeSignalValue) + mFlags.set(LinkTagFlag::_40); + } + + int saveFlagOnOffTypeValue = 0; + if (mMubinIter.tryGetParamIntByKey(&saveFlagOnOffTypeValue, "SaveFlagOnOffType")) { + if (saveFlagOnOffTypeValue == 2) + mFlags.set(LinkTagFlag::_200); + else if (saveFlagOnOffTypeValue == 1) + mFlags.set(LinkTagFlag::_100); + } + + bool flagSet = false; + isFlagSet(&flagSet, 0, mMapObj); + updateIsFlagSetFlag(flagSet, true, true); + if (flagSet) { + mFlags.set(LinkTagFlag::_10); + mTriggeredLinkFlags.makeAllOne(); + mFlags.set(LinkTagFlag::_20); + } else { + mFlags.reset(LinkTagFlag::_10); + mTriggeredLinkFlags.makeAllZero(); + mFlags.reset(LinkTagFlag::_20); + } + return true; +} + +void LinkTag::finalizeInit(BaseProc::InitContext* context) { + if (mMapObj) + mMapObj->registerBaseProc(this); + BaseProc::finalizeInit_(context); +} + +void LinkTag::calc() { + if (this->mState != BaseProc::State::Delete && !_1de) { + int currentExtraJobArrayIdx = BaseProcMgr::instance()->getCurrentExtraJobArrayIdx(); + s32 currentCounter = SystemTimers::instance() ? SystemTimers::instance()->mFrameCounter : 0; + s32 oldCounter = mCounter; + mCounter = currentCounter; + + switch (mLinkTagType) { + case Type::Count: + calcCount(mCounter != oldCounter); + break; + case Type::Pulse: + calcPulse(mCounter != oldCounter); + break; + case Type::None: + break; + default: + calcOther(mCounter != oldCounter); + break; + } + if (mFlags.isOff(LinkTagFlag::_1)) { + mFlags.set(LinkTagFlag::_1); + if (mMapObj && mMapObj->getLinkData()) + mMapObj->getLinkData()->setNumExecLinkTagTo1(); + } + mFlags.reset(LinkTagFlag::_1000); + mJobFlags.setBitOff(currentExtraJobArrayIdx ^ 1); + } +} + +inline s8 LinkTag::getSelfLinkCount() { + if (!mMapObj || !mMapObj->getLinkData()) + return 0; + auto& links = mMapObj->getLinkData()->mLinksToSelf.links; + s8 linkCount = 0; + + s32 end = std::min(links.size(), 0x60); + for (s32 i = 0; i < end; i++) { + auto& link = links[i]; + if (link.type <= map::MapLinkDefType::ChangeAtnSig && isTriggered(&link, i)) + linkCount += 1; + } + return linkCount; +} + +void LinkTag::calcCount(bool update) { + if (mFlags.isOff(LinkTagFlag::_1) || !mMapObj) + return; + + s8 linkCount = getSelfLinkCount(); + mFlags.reset(LinkTagFlag::_10); + if (linkCount != mTagCount) { + mTagCount = linkCount; + gdt::Manager::instance()->setS32ByIdxForLinkTag(linkCount, + mMapObj->getRevivalGameDataFlagHash()); + updateIsFlagSetFlag(mTagCount > 0, false, update); + } +} + +void LinkTag::calcPulse(bool update) { + if (mFlags.isOff(LinkTagFlag::_1)) { + if (mMapObj && mMapObj->getLinkData()) { // maybe inlined getSelfLinkCount? + auto& links = mMapObj->getLinkData()->mLinksToSelf.links; + s32 end = std::min(links.size(), 0x60); + for (s32 i = 0; i < end; i++) { + auto& link = links[i]; + if (link.type <= map::MapLinkDefType::ChangeAtnSig && isTriggered(&link, i)) + mTagCount += 1; + } + } + mFlags.reset(LinkTagFlag::_10); + return; + } + + bool something = mFlags.isOn(LinkTagFlag::_80); + s8 selfLinkCount = getSelfLinkCount(); + s8 oldTagCount = mTagCount; + mTagCount = selfLinkCount; + + if (update) { + if ((mTagCount != oldTagCount) == mFlags.isOn(LinkTagFlag::_8)) { + something = mFlags.isOn(LinkTagFlag::_80); + mFlags.reset(LinkTagFlag::_10); + } else { + something = true; + updateIsFlagSetFlag(mTagCount != oldTagCount, false, true); + } + + if (selfLinkCount != oldTagCount) { + mFlags.set(LinkTagFlag::_2000); + if (mMapObj && mMapObj->getLinkData()) { + for (auto& link : mMapObj->getLinkData()->mLinksOther.links) { + if (link.type == map::MapLinkDefType::OffWaitRevival && link.other_obj) { + gdt::Manager::instance()->setBoolByHash( + false, link.other_obj->getRevivalGameDataFlagHash()); + } + } + } + } else { + mFlags.reset(LinkTagFlag::_2000); + } + } + if (!something) + return; + + if (mMapObj) { + if (mMapObj->getLinkData()) { + for (auto& link : mMapObj->getLinkData()->mLinksOther.links) { + ActorLinkConstDataAccess acc{}; + link.getObjectProcWithAccessor(acc); + triggerLink(acc); + } + } + if (mMapObj->getRevivalGameDataFlagHash() != gdt::InvalidHandle) { + BaseProcMgr::instance()->setPushActorJobType3InsteadOf6(true); + } + } + mFlags.reset(LinkTagFlag::_80); +} + +void LinkTag::onEnterCalc() { + if (mLinkTagType == Type::Pulse) { + mFlags.set(LinkTagFlag::_4000); + if (mMapObj) { + auto mapLinkData = mMapObj->getLinkData(); + if (mapLinkData) { + // inlined getSelfLinkCount? + auto& links = mMapObj->getLinkData()->mLinksToSelf.links; + + s32 end = std::min(links.size(), 0x60); + for (s32 i = 0; i < end; i++) { + auto& link = links[i]; + if (link.type <= map::MapLinkDefType::ChangeAtnSig && isTriggered(&link, i)) + mTagCount += 1; + } + } + } + mFlags.reset(LinkTagFlag::_10); + } + _1dd = 0; + mFlags.reset(LinkTagFlag::_1081); + mFlags.set(LinkTagFlag::_80); + + if (mMapObj) { + if (mMapObj->getRevivalGameDataFlagHash() != gdt::InvalidHandle) + mFlags.set(LinkTagFlag::_4000); + s8 extraJobArrayId = BaseProcMgr::instance()->getCurrentExtraJobArrayIdx(); + if (!isDeletedOrDeleting() && mJobFlags.setBitOn(extraJobArrayId)) { + BaseProcMgr::instance()->queueExtraJobPush(&mJob.getLink()); + } + } +} + +void LinkTag::updateIsFlagSetFlag(bool isFlagSet, bool a3, bool a4) { + if (!mMapObj) + return; + + mFlags.change(LinkTagFlag::_8, isFlagSet); + + if (mFlags.isOff(LinkTagFlag::_40) || a3) { + mMapObj->getFlags0().change(map::Object::Flag0::_100, isFlagSet); + + if (mFlags.isOff(LinkTagFlag::_40)) { + auto weirdFlag = + isFlagSet == mMapObj->getFlags().isOn(map::Object::Flag::IsLinkTagNAndOrNOr); + int frameCounter = + SystemTimers::instance() ? SystemTimers::instance()->mFrameCounter : 0; + + if (mMapObj->getLinkData()) { + for (auto& mapLink : mMapObj->getLinkData()->mLinksOther.links) { + if (!mapLink.other_obj) + continue; + + switch (mapLink.type) { + case map::MapLinkDefType::Create: + case map::MapLinkDefType::MtxCopyCreate: + if (weirdFlag) { + if (mapLink.other_obj->getFlags0().isOff( + map::Object::Flag0::_10000000)) { + mapLink.other_obj->getFlags0().set(map::Object::Flag0::_10000000); + _1E8 = frameCounter; + } + } else if (_1E8 == frameCounter) { + mapLink.other_obj->getFlags0().reset(map::Object::Flag0::_10000000); + } + break; + case map::MapLinkDefType::Delete: + if (!weirdFlag) { + if (mapLink.other_obj->getFlags0().isOff( + map::Object::Flag0::_20000000)) { + mapLink.other_obj->getFlags0().set(map::Object::Flag0::_20000000); + _1E8 = frameCounter; + } + } else if (_1E8 == frameCounter) { + mapLink.other_obj->getFlags0().reset(map::Object::Flag0::_20000000); + } + break; + } + } + } + } + } + + bool v0 = isFlagSet != mFlags.isOn(LinkTagFlag::_4); + + if (a4) { + mFlags.reset(LinkTagFlag::_800); + } + if (v0) { + if (mFlags.isOff(LinkTagFlag::_400)) { + mFlags.set(LinkTagFlag::_800); + } + mFlags.set(LinkTagFlag::_400); + } else if (mFlags.isOn(LinkTagFlag::_800)) { + mFlags.reset(LinkTagFlag::_800); + mFlags.reset(LinkTagFlag::_400); + } + + if (!a3) { + auto* mapLinkData = mMapObj->getLinkData(); + if (mapLinkData) { + if (mapLinkData->isGenGroupInitState3() && + (mapLinkData->findLinkWithType(map::MapLinkDefType::Create) || + mapLinkData->findLinkWithType(map::MapLinkDefType::Delete) || + mapLinkData->findLinkWithType(map::MapLinkDefType::MtxCopyCreate))) { + mapLinkData->sub_7100D4FA90(); + } + } + } +} + +BaseProc::PreDeletePrepareResult LinkTag::prepareForPreDelete_() { + if (!BaseProc::startPreparingForPreDelete_()) + return PreDeletePrepareResult::NotDone; + if (!mJobFlags) + return PreDeletePrepareResult::Done; + + auto jobLink = &mJob.getLink(); + if (mJobFlags.isBitOn(0)) { + if (BaseProcMgr::instance()->hasExtraJobLink(jobLink, 0)) + return PreDeletePrepareResult::NotDone; + mJobFlags.setBitOff(0); + } + + if (mJobFlags.isBitOn(1)) { + if (BaseProcMgr::instance()->hasExtraJobLink(jobLink, 1)) + return PreDeletePrepareResult::NotDone; + mJobFlags.setBitOff(1); + } + return PreDeletePrepareResult::Done; +} + +bool LinkTag::isDonePreparingForPreDelete() { + if (!mMapObj) + return true; + auto linkData = mMapObj->getLinkData(); + if (linkData && mFlags.isOn(LinkTagFlag::_8000)) { + linkData->x_3(true); + linkData->printDebugInfo(); + } + mMapObj->unlinkProc(true); + mMapObj = nullptr; + return true; +} + +bool LinkTag::hasJobType(JobType jobType) { + if (jobType != JobType::Calc3) + return false; + if (mFlags.isOn(LinkTagFlag::_4000)) + return true; + if (!mMapObj || mMapObj->getRevivalGameDataFlagHash() == gdt::InvalidHandle) + return mLinkTagType == Type::Pulse; + + return true; +} + +bool LinkTag::shouldSkipJobPush() { + if (mJobFlags.isBitOn(BaseProcMgr::instance()->getCurrentExtraJobArrayIdx())) + return true; + + if (mFlags.isOn(LinkTagFlag::_1000)) + return false; + + return mFlags.isOff(LinkTagFlag::_2000); +} + +bool LinkTag::isSpecialJobType() { + return false; +} + +void LinkTag::queueExtraJobPush(JobType jobType, u32 jobFlagBit) { + if (jobType == JobType::Calc3) { + mJobFlags.setBitOff(1 ^ jobFlagBit); + s8 extraJobArrayId = BaseProcMgr::instance()->getCurrentExtraJobArrayIdx(); + if (!isDeletedOrDeleting() && mJobFlags.setBitOn(extraJobArrayId)) { + BaseProcMgr::instance()->queueExtraJobPush(&mJob.getLink()); + } + } +} + +bool LinkTag::canWakeUp() { + if (mMapObj && mMapObj->getLinkData()) { + auto linkData = mMapObj->getLinkData(); + if (mFlags.isOff(LinkTagFlag::_8000)) { + mFlags.set(LinkTagFlag::_8000); + linkData->x_8(true); + linkData->x_9(); + } + if (!linkData->x_7(true)) + return false; + } + if (mMapObj && mMapObj->getLinkData()) { + if (mMapObj->getLinkData()->findLinkWithType(map::MapLinkDefType::MtxCopyCreate)) + mFlags.set(LinkTagFlag::_2); + } + + return true; +} + +void LinkTag::prePushJob2() { + if (MCMgr::instance()) { + if (_1dd == MCMgr::instance()->mCalcJobCounterMaybe) { + mFlags.set(LinkTagFlag::_1000); // Here + } else if (mMapObj) { + bool a1 = false; + if (isFlagSet(&a1, 1, mMapObj)) { + if (mFlags.isOn(LinkTagFlag::_8) != a1) { + mFlags.set(LinkTagFlag::_1000); // duplicate usage with above that gets + // removed in ASM + } + } + } + } + _1dd.storeNonAtomic(-1); +} + +void LinkTag::calcOther(bool update) { + if (update) + _1E3 = 0; + + bool flagIsSet = false; + if (isFlagSet(&flagIsSet, 1, mMapObj) && flagIsSet != mFlags.isOn(LinkTagFlag::_8)) { + updateIsFlagSetFlag(flagIsSet, 0, update); + if (mMapObj) { + auto* linkData = mMapObj->getLinkData(); + if (linkData) { + for (auto& link : linkData->mLinksOther.links) { + ksys::act::ActorLinkConstDataAccess accessor{}; + link.getObjectProcWithAccessor(accessor); + triggerLink(accessor); + } + } + } + return; + } + + if (!mMapObj || !mMapObj->getLinkData()) { + return; + } + auto& links = mMapObj->getLinkData()->mLinksToSelf.links; + if (!links.size()) { + return; + } + bool v35, v36; + gdt::FlagHandle flagHash; + bool status = false; + s32 end; + + switch (mLinkTagType) { + case Type::And: + case Type::NAnd: + status = true; + end = std::min(links.size(), 0x60); + for (s32 i = 0; i < end; i++) { + auto& link = links[i]; + if (link.type <= map::MapLinkDefType::ChangeAtnSig) + status &= isTriggered(&link, i); + } + break; + case Type::Or: + case Type::NOr: + status = false; + end = std::min(links.size(), 0x60); + for (s32 i = 0; i < end; i++) { + auto& link = links[i]; + if (link.type <= map::MapLinkDefType::ChangeAtnSig) + status |= isTriggered(&link, i); + } + break; + case Type::XOr: + status = false; + end = std::min(links.size(), 0x60); + for (s32 i = 0; i < end; i++) { + auto& link = links[i]; + if (link.type <= map::MapLinkDefType::ChangeAtnSig) { + if (mLinkTagType == Type::Or || mLinkTagType == Type::NOr) + status |= isTriggered(&link, i); + else + status ^= isTriggered(&link, i); + } + } + break; + } + mFlags.change(LinkTagFlag::_10, status); + if (mMapObj) { + flagHash = mMapObj->getRevivalGameDataFlagHash(); + v35 = false; + v36 = mMapObj->getFlags0().isOn(map::Object::Flag0::_2); + } else { + flagHash = gdt::InvalidHandle; + v35 = false; + v36 = true; + } + + if (status != mFlags.isOn(LinkTagFlag::_8)) { + if (_1E3) + _1E3 = 0; + else if (v36 || (mFlags.isOn(LinkTagFlag::_100))) { + status = true; + if (!v36 && mFlags.isOff(LinkTagFlag::_8)) { + status = true; + _1E3 = 1; + } + } else { + status &= mFlags.isOff(LinkTagFlag::_200); + if (mFlags.isOn(LinkTagFlag::_200) && mFlags.isOn(LinkTagFlag::_8)) { + status = false; + _1E3 = 1; + } + } + } + + if (!v35 && mFlags.isOn(LinkTagFlag::_8)) + goto LABEL_105; + else if (status != mFlags.isOn(LinkTagFlag::_8)) { + updateIsFlagSetFlag(status, false, update); + if (flagHash != gdt::InvalidHandle) { + if (!v36) { + gdt::Manager::instance()->setBoolByHash(status, flagHash); + if (BaseProcMgr::instance()->getJobType() == JobType::Calc3) { + if (MCMgr::instance() && MCMgr::instance()->mCalcJobCounterMaybe >= 1) + BaseProcMgr::instance()->setPushActorJobType3InsteadOf6(true); + } + if (mState == State::Calc) { + if (mStateFlags.isOff(StateFlags::RequestDelete)) { + if (mJobFlags.setBitOn( + BaseProcMgr::instance()->getCurrentExtraJobArrayIdx())) { + BaseProcMgr::instance()->queueExtraJobPush(&mJob.getLink()); + } + } + } + _1dd.storeNonAtomic(MCMgr::instance() ? MCMgr::instance()->mCalcJobCounterMaybe : + 0); + } else if (status) { + gdt::Manager::instance()->setS32ByIdxForLinkTag(true, flagHash); + } + } + } else if (!v36 || mFlags.isOn(LinkTagFlag::_10) || mFlags.isOff(LinkTagFlag::_10)) { + LABEL_105: + if (mFlags.isOff(LinkTagFlag::_80)) + return; + } else { + int value = 0; + gdt::Manager::instance()->getS32(flagHash, &value); + gdt::Manager::instance()->setS32ByIdxForLinkTag(value, flagHash); + return; + } + if (mMapObj) { + if (mMapObj->getLinkData()) { + for (auto& link : mMapObj->getLinkData()->mLinksOther.links) { + ActorLinkConstDataAccess acc{}; + link.getObjectProcWithAccessor(acc); + triggerLink(acc); + } + } + if (mMapObj->getRevivalGameDataFlagHash() != gdt::InvalidHandle) { + BaseProcMgr::instance()->setPushActorJobType3InsteadOf6(true); + } + } + mFlags.reset(LinkTagFlag::_80); +} + +inline void setBitAfterCheck(sead::LongBitFlag<96>* mTriggeredLinkFlags, u8 linkIdx) { + if (linkIdx < 0x60) + mTriggeredLinkFlags->setBit(linkIdx); +} +inline void resetBitAfterCheck(sead::LongBitFlag<96>* mTriggeredLinkFlags, u8 linkIdx) { + if (linkIdx < 0x60) + mTriggeredLinkFlags->resetBit(linkIdx); +} + +bool LinkTag::isTriggered(map::ObjectLink* link, u8 linkIdx) { + auto* otherObj = link->other_obj; + if (!otherObj) + return mTriggeredLinkFlags.isOnBit(linkIdx); + + BaseProc* baseProc = otherObj->getProc(); + if (baseProc && + (baseProc->getState() == State::Calc || baseProc->getState() == State::Delete)) { + LinkTag* linkTag = sead::DynamicCast(baseProc); + if (!linkTag) { + Actor* actor = sead::DynamicCast(baseProc); + if (!actor) { + resetBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + } else if (actor->getEmittedSignalType() == link->type) + resetBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + else + setBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + } else if (linkTag->mFlags.isOn(LinkTagFlag::_40)) { + if (linkTag->mFlags.isOn(LinkTagFlag::_20) == linkTag->mFlags.isOn(LinkTagFlag::_4)) + resetBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + setBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + } else if (link->type != map::MapLinkDefType::BasicSigOnOnly) { + if (linkTag->mFlags.isOn(LinkTagFlag::_8) == linkTag->mFlags.isOn(LinkTagFlag::_4)) + resetBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + setBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + } else if (linkTag->mFlags.isOff(LinkTagFlag::_400)) { + resetBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + } else { + setBitAfterCheck(&mTriggeredLinkFlags, linkIdx); + } + } else { + if (link->type <= map::MapLinkDefType::DeadUp) { + if (otherObj->getFlags0().isOn(map::Object::Flag0::_100000) || + otherObj->checkRevivalFlag(map::ActorData::Flag::RevivalEnable)) { + if (linkIdx >= 0x60) + mTriggeredLinkFlags.resetBit(linkIdx); + } + } + } + if (mFlags.isOn(LinkTagFlag::_2) && otherObj->getFlags().isOff(map::Object::Flag::IsLinkTag)) + _1df = linkIdx; + + return mTriggeredLinkFlags.isOnBit(linkIdx); +} + +} // namespace ksys::act diff --git a/src/KingSystem/ActorSystem/actLinkTag.h b/src/KingSystem/ActorSystem/actLinkTag.h new file mode 100644 index 000000000..540e88dea --- /dev/null +++ b/src/KingSystem/ActorSystem/actLinkTag.h @@ -0,0 +1,97 @@ +#pragma once + +#include +#include +#include +#include "KingSystem/ActorSystem/actBaseProc.h" +#include "KingSystem/ActorSystem/actBaseProcJobHandler.h" +#include "KingSystem/Map/mapMubinIter.h" +#include "KingSystem/Map/mapObjectLink.h" +#include "KingSystem/Utils/Types.h" + +namespace ksys::map { +class Object; +} // namespace ksys::map + +namespace ksys::act { + +class LinkTag : public BaseProc { + SEAD_RTTI_OVERRIDE(LinkTag, BaseProc) + +public: + enum class Type { And, Or, NAnd, NOr, XOr, Count, Pulse, None }; + enum class JobFlag {}; + enum class LinkTagFlag : u16 { + _1 = 0x1, + _2 = 0x2, + _4 = 0x4, + _8 = 0x8, + _10 = 0x10, + _20 = 0x20, + _40 = 0x40, + _80 = 0x80, + _100 = 0x100, + _200 = 0x200, + _400 = 0x400, + _800 = 0x800, + _1000 = 0x1000, + _2000 = 0x2000, + _4000 = 0x4000, + _8000 = 0x8000, + + _30 = _10 | _20, + _1081 = _1000 | _80 | _1 + }; + + static LinkTag* construct(const BaseProc::CreateArg& arg, sead::Heap* heap); + + explicit LinkTag(const BaseProc::CreateArg& arg); + ~LinkTag(); + + void calc(); + void calcPulse(bool update); + void calcCount(bool update); + void calcOther(bool update); + void onEnterCalc(); + bool init(); + void finalizeInit(BaseProc::InitContext* context); + void updateIsFlagSetFlag(bool isFlagSet, bool a3, bool a4); + bool isTriggered(map::ObjectLink* link, u8 linkIdx); + PreDeletePrepareResult prepareForPreDelete_() override; + bool isDonePreparingForPreDelete(); + bool hasJobType(JobType jobType); + bool shouldSkipJobPush(); + bool isSpecialJobType(); + void queueExtraJobPush(JobType jobType, u32 jobFlagBit); + bool canWakeUp(); + void prePushJob2(); + + s8 getSelfLinkCount(); + + // this doesn't even belong in this namespace, but is used as a placeholder for init() and + // prePushJob2() + static bool isFlagSet(bool* isSet, int unk, map::Object* object); + // this also doesn't belong here, used in calcPulse(), calcOther() (7100d15b04); + static void triggerLink(ActorLinkConstDataAccess& acc); + // this also doesn't belong here, used in dtor + static u32 getSceneStatus(); + + BaseProcJobHandlerT mJob; + sead::LongBitFlag<96> mTriggeredLinkFlags; + sead::SizedEnum mLinkTagType = Type::And; + sead::Atomic _1dd = 0xFF; + sead::Atomic _1de = 0; + u8 _1df = 0xFF; + sead::TypedBitFlag mFlags; + s8 mTagCount = 0; + u8 _1E3 = 0; + s32 mCounter = 0; + int _1E8 = 0; + sead::Atomic mJobFlags; + u32 mHashId = 0; + map::MubinIter mMubinIter; + map::Object* mMapObj = nullptr; +}; +KSYS_CHECK_SIZE_NX150(LinkTag, 0x210); + +} // namespace ksys::act diff --git a/src/KingSystem/GameData/gdtManager.h b/src/KingSystem/GameData/gdtManager.h index 923cea2d4..69c64aed2 100644 --- a/src/KingSystem/GameData/gdtManager.h +++ b/src/KingSystem/GameData/gdtManager.h @@ -519,6 +519,11 @@ class Manager : public IManager, public KingEditorComponent { mResetFlags.set(ResetFlag::AnimalMaster); } + // TODO needs implementation: 0x7100D39C9C + void setS32ByIdxForLinkTag(s32, FlagHandle); + // TODO needs implementation: 0x7100247B3C + bool setBoolByHash(bool, FlagHandle); + private: enum class BitFlag { _1 = 0x1, diff --git a/src/KingSystem/Map/mapObject.h b/src/KingSystem/Map/mapObject.h index e3634c808..cf8604b18 100644 --- a/src/KingSystem/Map/mapObject.h +++ b/src/KingSystem/Map/mapObject.h @@ -52,6 +52,8 @@ class Object { _400000 = 0x400000, _2000000 = 0x2000000, _4000000 = 0x4000000, + _10000000 = 0x10000000, + _20000000 = 0x20000000, _40000000 = 0x40000000, _80000000 = 0x80000000, ResetOnUnlinkFailed = _100 | _2000, @@ -181,6 +183,7 @@ class Object { const auto& getFlags0() const { return mFlags0; } void setFlags0(Flag0 bit) { mFlags0.set(bit); } void resetFlags0(Flag0 bit) { mFlags0.reset(bit); } + auto& getFlags0() { return mFlags0; } const auto& getFlags() const { return mFlags; } const auto& getActorFlags8() const { return mActorFlags8; } const auto& getHardModeFlags() const { return mHardModeFlags; } diff --git a/src/KingSystem/Map/mapObjectLink.h b/src/KingSystem/Map/mapObjectLink.h index 382aa804d..4328f9e6f 100644 --- a/src/KingSystem/Map/mapObjectLink.h +++ b/src/KingSystem/Map/mapObjectLink.h @@ -112,6 +112,23 @@ class ObjectLinkData { return checkDeleteLinkObjRevival() || checkCreateLinkObjRevival(); } + // TODO requires implementation: 0x7100D4FA70 + bool isGenGroupInitState3() const; + // TODO requires implementation: 0x7100D4FA90 + void sub_7100D4FA90(); + // TODO requires implementation: 0x7100D4F9C8 + void setNumExecLinkTagTo1(); + // TODO requires implementation: 0x7100D4F97C + void x_3(bool a1); + // TODO requires implementation: 0x7100D4F884 + void printDebugInfo(); + // TODO requires implementation: 0x7100D4F968 + void x_8(bool a1); + // TODO requires implementation: 0x7100D4F824 + void x_9(); + // TODO requires implementation: 0x7100D4F9B0 + bool x_7(bool a1); + Object* mCreateLinksSrcObj = nullptr; Object* mDeleteLinksSrcObj = nullptr; diff --git a/src/KingSystem/System/CMakeLists.txt b/src/KingSystem/System/CMakeLists.txt index 4f907b819..379e0cb90 100644 --- a/src/KingSystem/System/CMakeLists.txt +++ b/src/KingSystem/System/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(uking PRIVATE Hio.h KingEditor.cpp KingEditor.h + MCMgr.h MemoryProfiler.cpp MemoryProfiler.h MessageCapture.cpp diff --git a/src/KingSystem/System/MCMgr.h b/src/KingSystem/System/MCMgr.h new file mode 100644 index 000000000..e929e6a59 --- /dev/null +++ b/src/KingSystem/System/MCMgr.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include "KingSystem/Utils/Types.h" + +namespace ksys { + +struct ActorPhysics; + +class MCMgrJob : sead::Job { +public: + void invoke() override; + +private: + void* mActorChemicalStuff; + ActorPhysics* mActorPhysics; + short mFlags; + const char* mName; + void* _38; +}; + +class MCMgr { + SEAD_SINGLETON_DISPOSER(MCMgr) + +public: + MCMgr(); + + void* _0; // TODO: Add gsys::ISystemTaskCallback, as it is a base class of this one + sead::FixedSizeJQ mQueue0; + sead::FixedSizeJQ mQueue1; + sead::FixedSizeJQ mQueue2; + sead::FixedSizeJQ mQueue3; + sead::FixedSizeJQ mPostBgPlayerAfter; + sead::FixedSizeJQ mQueue4; + sead::FixedSizeJQ mPlacementMgrJobQueue; + sead::FixedSizeJQ mQueue5; + sead::FixedSizeJQ mQueue6; + sead::FixedSizeJQ mQueue7; + sead::FixedSizeJQ mQueues8[8]; + sead::Job0 mCalcControllerAndUiJob; + sead::Job0 mMessageDispatcherJob; + u32 mFlags; + int _bdc; + MCMgrJob mInitProcessJobs[64]; + int mNumJobs; + int mCalcJobCounterMaybe; + void* _1be8; + sead::Delegate1R mInvoker2; + sead::Delegate1R mInvoker3; + sead::Delegate1R mInvoker4; +}; +KSYS_CHECK_SIZE_NX150(MCMgr, 0x1C50); + +} // namespace ksys