metaforce/Runtime/World/CStateMachine.cpp

159 lines
4.7 KiB
C++
Raw Normal View History

#include "Runtime/World/CStateMachine.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/World/CAi.hpp"
2016-04-24 22:46:28 -07:00
#include "ConsoleVariables/CVarManager.hpp"
2020-10-02 23:28:05 -07:00
2021-04-10 01:42:06 -07:00
namespace metaforce {
2020-10-02 23:28:05 -07:00
namespace {
2021-04-10 01:42:06 -07:00
logvisor::Module Log("metaforce::CStateMachine");
2020-10-02 23:28:05 -07:00
}
2018-12-07 21:30:43 -08:00
CStateMachine::CStateMachine(CInputStream& in) {
CAiTrigger* lastTrig = nullptr;
u32 stateCount = in.ReadLong();
2018-12-07 21:30:43 -08:00
x0_states.reserve(stateCount);
for (u32 i = 0; i < stateCount; ++i) {
std::string name;
while (name.size() < 31) {
const auto chr = in.ReadChar();
name += chr;
if (chr == '\0') {
break;
}
}
CAiStateFunc func = CAi::GetStateFunc(name);
2018-12-07 21:30:43 -08:00
x0_states.emplace_back(func, name.c_str());
}
x10_triggers.reserve(in.ReadLong());
2018-12-07 21:30:43 -08:00
for (u32 i = 0; i < stateCount; ++i) {
x0_states[i].SetNumTriggers(in.ReadLong());
2018-12-07 21:30:43 -08:00
if (x0_states[i].GetNumTriggers() == 0)
continue;
CAiTrigger* firstTrig = x10_triggers.data() + x10_triggers.size();
x0_states[i].SetTriggers(firstTrig);
x10_triggers.resize(x10_triggers.size() + x0_states[i].GetNumTriggers());
for (s32 j = 0; j < x0_states[i].GetNumTriggers(); ++j) {
const u32 triggerCount = in.ReadLong();
const u32 lastTriggerIdx = triggerCount - 1;
2018-12-07 21:30:43 -08:00
for (u32 k = 0; k < triggerCount; ++k) {
std::string name;
while (name.size() < 31) {
const auto chr = in.ReadChar();
name += chr;
if (chr == '\0') {
break;
}
}
const bool isNot = name.front() == '!';
const CAiTriggerFunc func = CAi::GetTriggerFunc(isNot ? name.c_str() + 1 : name.c_str());
const float arg = in.ReadFloat();
2018-12-07 21:30:43 -08:00
CAiTrigger* newTrig;
if (k < lastTriggerIdx) {
newTrig = &x10_triggers.emplace_back();
2018-12-07 21:30:43 -08:00
} else {
newTrig = &firstTrig[j];
2016-08-15 13:43:04 -07:00
}
2018-12-07 21:30:43 -08:00
if (k == 0)
newTrig->Setup(func, isNot, arg, &x0_states[in.ReadLong()]);
2018-12-07 21:30:43 -08:00
else
newTrig->Setup(func, isNot, arg, lastTrig);
lastTrig = newTrig;
}
2016-08-15 13:43:04 -07:00
}
2018-12-07 21:30:43 -08:00
}
2016-04-24 22:46:28 -07:00
}
2018-12-07 21:30:43 -08:00
s32 CStateMachine::GetStateIndex(std::string_view state) const {
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())
return 0;
2018-12-07 21:30:43 -08:00
return it - x0_states.begin();
}
2018-12-07 21:30:43 -08:00
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();
2018-11-10 22:24:24 -08:00
}
2018-12-07 21:30:43 -08:00
}
2020-04-20 15:35:56 -07:00
if (andPassed && state != nullptr) {
2018-12-07 21:30:43 -08:00
x4_state->CallFunc(mgr, ai, EStateMsg::Deactivate, 0.f);
2018-11-10 22:24:24 -08:00
x4_state = state;
Log.report(logvisor::Info, FMT_STRING("{} {} {} - {} {}"), ai.GetUniqueId(), ai.GetEditorId(), ai.GetName(),
state->xc_name, int(state - x0_machine->GetStateVector().data()));
2018-11-10 22:24:24 -08:00
x8_time = 0.f;
2018-11-11 20:21:36 -08:00
x18_24_codeTrigger = false;
2018-12-07 21:30:43 -08:00
xc_random = mgr.GetActiveRandom()->Float();
x4_state->CallFunc(mgr, ai, EStateMsg::Activate, delta);
return;
}
2018-11-10 22:24:24 -08:00
}
2018-12-07 21:30:43 -08:00
}
}
2016-08-15 13:43:04 -07:00
2018-12-07 21:30:43 -08:00
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_codeTrigger = false;
x4_state->CallFunc(mgr, ai, EStateMsg::Activate, 0.f);
}
}
2018-12-07 21:30:43 -08:00
void CStateMachineState::SetState(CStateManager& mgr, CAi& ai, const CStateMachine* machine, std::string_view state) {
if (!machine)
return;
2018-12-07 21:30:43 -08:00
if (!x0_machine)
2020-03-06 07:16:55 -08:00
Setup(machine);
2018-12-07 21:30:43 -08:00
s32 idx = machine->GetStateIndex(state);
SetState(mgr, ai, idx);
2016-08-15 13:43:04 -07:00
}
2018-12-07 21:30:43 -08:00
const std::vector<CAiState>* CStateMachineState::GetStateVector() const {
if (!x0_machine)
return nullptr;
2016-08-15 13:43:04 -07:00
2018-12-07 21:30:43 -08:00
return &x0_machine->GetStateVector();
2016-08-15 13:43:04 -07:00
}
2018-12-07 21:30:43 -08:00
void CStateMachineState::Setup(const CStateMachine* machine) {
x0_machine = machine;
x4_state = nullptr;
x8_time = 0.f;
xc_random = 0.f;
x10_delay = 0.f;
2016-08-15 13:43:04 -07:00
}
2018-12-07 21:30:43 -08:00
CFactoryFnReturn FAiFiniteStateMachineFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,
CObjectReference*) {
return TToken<CStateMachine>::GetIObjObjectFor(std::make_unique<CStateMachine>(in));
}
2021-04-10 01:42:06 -07:00
} // namespace metaforce