metaforce/Runtime/World/CStateMachine.hpp

126 lines
3.3 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
2016-04-25 05:46:28 +00:00
#include <vector>
#include "Runtime/CToken.hpp"
#include "Runtime/GCNTypes.hpp"
#include "Runtime/IObj.hpp"
#include "Runtime/IObjFactory.hpp"
#include "Runtime/IOStreams.hpp"
#include "Runtime/World/CAiFuncMap.hpp"
2016-04-25 05:46:28 +00:00
2018-12-08 05:30:43 +00:00
namespace urde {
2016-04-25 05:46:28 +00:00
class CAiState;
class CStateManager;
2018-12-08 05:30:43 +00:00
class CAiTrigger {
CAiTriggerFunc x0_func;
float xc_arg = 0.f;
CAiTrigger* x10_andTrig = nullptr;
CAiState* x14_state = nullptr;
bool x18_lNot = false;
2016-04-25 05:46:28 +00:00
public:
2018-12-08 05:30:43 +00:00
CAiTrigger() = default;
CAiTrigger* GetAnd() const { return x10_andTrig; }
CAiState* GetState() const { return x14_state; }
bool CallFunc(CStateManager& mgr, CAi& ai) const {
2018-12-08 05:30:43 +00:00
if (x0_func) {
bool ret = (ai.*x0_func)(mgr, xc_arg);
2020-03-06 15:16:55 +00:00
return x18_lNot == !ret;
2016-04-25 05:46:28 +00:00
}
2018-12-08 05:30:43 +00:00
return true;
}
2016-04-25 05:46:28 +00:00
2018-12-08 05:30:43 +00:00
void Setup(CAiTriggerFunc func, bool lnot, float arg, CAiTrigger* andTrig) {
x0_func = func;
x18_lNot = lnot;
xc_arg = arg;
x10_andTrig = andTrig;
}
void Setup(CAiTriggerFunc func, bool lnot, float arg, CAiState* state) {
x0_func = func;
x18_lNot = lnot;
xc_arg = arg;
x14_state = state;
}
2016-04-25 05:46:28 +00:00
};
2018-12-08 05:30:43 +00:00
class CAiState {
friend class CStateMachineState;
CAiStateFunc x0_func;
char xc_name[32] = {};
u32 x2c_numTriggers;
CAiTrigger* x30_firstTrigger;
2016-04-25 05:46:28 +00:00
public:
2018-12-08 05:30:43 +00:00
CAiState(CAiStateFunc func, const char* name) {
x0_func = func;
strncpy(xc_name, name, 31);
}
2016-04-25 05:46:28 +00:00
2018-12-08 05:30:43 +00:00
s32 GetNumTriggers() const { return x2c_numTriggers; }
CAiTrigger* GetTrig(s32 i) const { return &x30_firstTrigger[i]; }
const char* GetName() const { return xc_name; }
void SetTriggers(CAiTrigger* triggers) { x30_firstTrigger = triggers; }
void SetNumTriggers(s32 numTriggers) { x2c_numTriggers = numTriggers; }
void CallFunc(CStateManager& mgr, CAi& ai, EStateMsg msg, float delta) const {
if (x0_func)
(ai.*x0_func)(mgr, msg, delta);
}
2016-04-25 05:46:28 +00:00
};
2018-12-08 05:30:43 +00:00
class CStateMachine {
std::vector<CAiState> x0_states;
std::vector<CAiTrigger> x10_triggers;
2016-04-25 05:46:28 +00:00
public:
explicit CStateMachine(CInputStream& in);
2016-04-25 05:46:28 +00:00
2018-12-08 05:30:43 +00:00
s32 GetStateIndex(std::string_view state) const;
const std::vector<CAiState>& GetStateVector() const { return x0_states; }
2016-04-25 05:46:28 +00:00
};
2018-12-08 05:30:43 +00:00
class CStateMachineState {
friend class CPatterned;
const CStateMachine* x0_machine = nullptr;
CAiState* x4_state = nullptr;
float x8_time = 0.f;
float xc_random = 0.f;
float x10_delay = 0.f;
float x14_;
union {
struct {
bool x18_24_codeTrigger : 1;
2016-08-15 20:43:04 +00:00
};
2018-12-08 05:30:43 +00:00
u32 dummy = 0;
};
2016-04-25 05:46:28 +00:00
public:
2018-12-08 05:30:43 +00:00
CStateMachineState() = default;
CAiState* GetActorState() const { return x4_state; }
void Update(CStateManager& mgr, CAi& ai, float delta);
void SetState(CStateManager&, CAi&, s32);
void SetState(CStateManager&, CAi&, const CStateMachine*, std::string_view);
const std::vector<CAiState>* GetStateVector() const;
void Setup(const CStateMachine* machine);
void SetDelay(float delay) { x10_delay = delay; }
float GetTime() const { return x8_time; }
float GetRandom() const { return xc_random; }
float GetDelay() const { return x10_delay; }
2018-12-13 07:39:16 +00:00
void SetCodeTrigger() { x18_24_codeTrigger = true; }
2018-12-08 05:30:43 +00:00
const char* GetName() const {
if (x4_state)
return x4_state->GetName();
return nullptr;
}
2016-04-25 05:46:28 +00:00
};
CFactoryFnReturn FAiFiniteStateMachineFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,
2018-12-08 05:30:43 +00:00
CObjectReference*);
2016-04-25 05:46:28 +00:00
2018-12-08 05:30:43 +00:00
} // namespace urde