mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-12-09 23:07:43 +00:00
Finish CStateMachine
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
#include "CStateMachine.hpp"
|
||||
#include "CAi.hpp"
|
||||
#include "CStateManager.hpp"
|
||||
|
||||
namespace urde
|
||||
{
|
||||
CStateMachine::CStateMachine(CInputStream& in)
|
||||
{
|
||||
#if 0
|
||||
CAiTrigger* lastTrig = nullptr;
|
||||
u32 stateCount = in.readUint32Big();
|
||||
|
||||
x0_states.reserve(stateCount);
|
||||
|
||||
for (u32 i = 0; i < stateCount; ++i)
|
||||
{
|
||||
std::string name = in.readString(31);
|
||||
std::string name = in.readString(31, false);
|
||||
CAiStateFunc func = CAi::GetStateFunc(name.c_str());
|
||||
x0_states.emplace_back(func, name);
|
||||
x0_states.emplace_back(func, name.c_str());
|
||||
}
|
||||
|
||||
x10_triggers.reserve(in.readUint32Big());
|
||||
@@ -24,27 +25,43 @@ CStateMachine::CStateMachine(CInputStream& in)
|
||||
x0_states[i].SetNumTriggers(in.readUint32Big());
|
||||
if (x0_states[i].GetNumTriggers() == 0)
|
||||
continue;
|
||||
for (u32 j = 0; j < x0_states[i].GetNumTriggers(); ++j)
|
||||
x10_triggers.emplace_back();
|
||||
CAiTrigger* firstTrig = x10_triggers.data() + x10_triggers.size();
|
||||
x0_states[i].SetTriggers(firstTrig);
|
||||
x10_triggers.resize(x10_triggers.size() + x0_states[i].GetNumTriggers());
|
||||
|
||||
for (u32 j = 0; j < x0_states[i].GetNumTriggers(); ++j)
|
||||
{
|
||||
u32 triggerCount = in.readUint32Big();
|
||||
u32 r19 = triggerCount - 1;
|
||||
u32 lastTriggerIdx = triggerCount - 1;
|
||||
for (u32 k = 0; k < triggerCount; ++k)
|
||||
{
|
||||
std::string name = in.readString(31);
|
||||
CAiTriggerFunc func = CAi::GetTrigerFunc(name.c_str());
|
||||
float f31 = in.readFloatBig();
|
||||
std::string name = in.readString(31, false);
|
||||
bool isNot = name.front() == '!';
|
||||
CAiTriggerFunc func = CAi::GetTrigerFunc(isNot ? name.c_str() + 1 : name.c_str());
|
||||
float arg = in.readFloatBig();
|
||||
CAiTrigger* newTrig;
|
||||
if (k < lastTriggerIdx)
|
||||
{
|
||||
x10_triggers.emplace_back();
|
||||
newTrig = &x10_triggers.back();
|
||||
}
|
||||
else
|
||||
{
|
||||
newTrig = &firstTrig[j];
|
||||
}
|
||||
if (k == 0)
|
||||
newTrig->Setup(func, isNot, arg, &x0_states[in.readUint32Big()]);
|
||||
else
|
||||
newTrig->Setup(func, isNot, arg, lastTrig);
|
||||
lastTrig = newTrig;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
s32 CStateMachine::GetStateIndex(std::string_view state) const
|
||||
{
|
||||
auto it = std::find_if(x0_states.begin(), x0_states.end(), [&state](const CAiState& st) -> bool {
|
||||
auto it = std::find_if(x0_states.begin(), x0_states.end(), [&state](const CAiState& st) {
|
||||
return (strncmp(st.GetName(), state.data(), 31) == 0);
|
||||
});
|
||||
if (it == x0_states.end())
|
||||
@@ -53,8 +70,54 @@ s32 CStateMachine::GetStateIndex(std::string_view state) const
|
||||
return it - x0_states.begin();
|
||||
}
|
||||
|
||||
void CStateMachineState::SetState(CStateManager &, CAi &, s32 idx)
|
||||
void CStateMachineState::Update(CStateManager& mgr, CAi& ai, float delta)
|
||||
{
|
||||
if (x4_state)
|
||||
{
|
||||
x8_time += delta;
|
||||
x4_state->CallFunc(mgr, ai, EStateMsg::Update, delta);
|
||||
for (int i = 0; i < x4_state->GetNumTriggers(); ++i)
|
||||
{
|
||||
CAiTrigger* trig = x4_state->GetTrig(i);
|
||||
CAiState* state = nullptr;
|
||||
bool andPassed = true;
|
||||
while (andPassed && trig)
|
||||
{
|
||||
andPassed = false;
|
||||
if (trig->CallFunc(mgr, ai))
|
||||
{
|
||||
andPassed = true;
|
||||
state = trig->GetState();
|
||||
trig = trig->GetAnd();
|
||||
}
|
||||
}
|
||||
if (andPassed && state)
|
||||
{
|
||||
x4_state->CallFunc(mgr, ai, EStateMsg::Deactivate, 0.f);
|
||||
x4_state = state;
|
||||
x8_time = 0.f;
|
||||
x18_24_ = false;
|
||||
xc_random = mgr.GetActiveRandom()->Float();
|
||||
x4_state->CallFunc(mgr, ai, EStateMsg::Activate, delta);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CStateMachineState::SetState(CStateManager& mgr, CAi& ai, s32 idx)
|
||||
{
|
||||
CAiState* state = const_cast<CAiState*>(&x0_machine->GetStateVector()[idx]);
|
||||
if (x4_state != state)
|
||||
{
|
||||
if (x4_state)
|
||||
x4_state->CallFunc(mgr, ai, EStateMsg::Deactivate, 0.f);
|
||||
x4_state = state;
|
||||
x8_time = 0.f;
|
||||
xc_random = mgr.GetActiveRandom()->Float();
|
||||
x18_24_ = false;
|
||||
x4_state->CallFunc(mgr, ai, EStateMsg::Activate, 0.f);
|
||||
}
|
||||
}
|
||||
|
||||
void CStateMachineState::SetState(CStateManager& mgr, CAi& ai, const CStateMachine* machine, std::string_view state)
|
||||
|
||||
Reference in New Issue
Block a user