metaforce/Runtime/World/CStateMachine.hpp

132 lines
3.4 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 "CAiFuncMap.hpp"
#include "IObj.hpp"
#include "CToken.hpp"
#include "IOStreams.hpp"
#include "IObjFactory.hpp"
2016-04-25 05:46:28 +00:00
namespace urde
{
class CAiState;
class CStateManager;
class CAiTrigger
{
2018-11-11 06:24:24 +00:00
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:
CAiTrigger() = default;
2018-11-11 06:24:24 +00:00
CAiTrigger* GetAnd() const { return x10_andTrig; }
CAiState* GetState() const { return x14_state; }
bool CallFunc(CStateManager& mgr, CAi& ai)
2016-04-25 05:46:28 +00:00
{
2018-11-11 06:24:24 +00:00
if (x0_func)
{
bool ret = (ai.*x0_func)(mgr, xc_arg);
return x18_lNot ? !ret : ret;
}
return true;
2016-04-25 05:46:28 +00:00
}
2018-11-11 06:24:24 +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
};
class CAiState
{
2018-09-14 00:49:34 +00:00
friend class CStateMachineState;
2016-04-25 05:46:28 +00:00
CAiStateFunc x0_func;
2018-11-12 04:21:36 +00:00
char xc_name[32] = {};
2016-08-15 20:43:04 +00:00
u32 x2c_numTriggers;
2018-11-11 06:24:24 +00:00
CAiTrigger* x30_firstTrigger;
2016-04-25 05:46:28 +00:00
public:
CAiState(CAiStateFunc func, const char* name)
2018-11-11 06:24:24 +00:00
{
x0_func = func;
2018-11-12 04:21:36 +00:00
strncpy(xc_name, name, 31);
2018-11-11 06:24:24 +00:00
}
2016-04-25 05:46:28 +00:00
2018-11-11 06:24:24 +00:00
s32 GetNumTriggers() const { return x2c_numTriggers; }
CAiTrigger* GetTrig(s32 i) const { return &x30_firstTrigger[i]; }
2018-11-11 03:27:54 +00:00
const char* GetName() const { return xc_name; }
2018-11-11 06:24:24 +00:00
void SetTriggers(CAiTrigger* triggers) { x30_firstTrigger = triggers; }
2016-08-15 20:43:04 +00:00
void SetNumTriggers(s32 numTriggers) { x2c_numTriggers = numTriggers; }
2016-04-25 05:46:28 +00:00
void CallFunc(CStateManager& mgr, CAi& ai, EStateMsg msg, float delta) const
{
if (x0_func)
(ai.*x0_func)(mgr, msg, delta);
}
};
class CStateMachine
{
std::vector<CAiState> x0_states;
std::vector<CAiTrigger> x10_triggers;
public:
CStateMachine(CInputStream& in);
2017-11-13 06:19:18 +00:00
s32 GetStateIndex(std::string_view state) const;
2018-11-11 03:27:54 +00:00
const std::vector<CAiState>& GetStateVector() const { return x0_states; }
2016-04-25 05:46:28 +00:00
};
class CStateMachineState
{
2018-09-14 00:49:34 +00:00
friend class CPatterned;
2016-08-15 20:43:04 +00:00
const CStateMachine* x0_machine = nullptr;
2016-04-25 05:46:28 +00:00
CAiState* x4_state = nullptr;
float x8_time = 0.f;
2018-09-14 00:49:34 +00:00
float xc_random = 0.f;
2018-11-11 03:27:54 +00:00
float x10_delay = 0.f;
2018-09-14 00:49:34 +00:00
float x14_;
2016-08-15 20:43:04 +00:00
union
{
struct
{
2018-11-12 04:21:36 +00:00
bool x18_24_codeTrigger : 1;
2016-08-15 20:43:04 +00:00
};
u32 dummy = 0;
};
2016-04-25 05:46:28 +00:00
public:
CStateMachineState()=default;
2018-09-14 00:49:34 +00:00
CAiState* GetActorState() const { return x4_state; }
2016-04-25 05:46:28 +00:00
2018-11-11 06:24:24 +00:00
void Update(CStateManager& mgr, CAi& ai, float delta);
2016-08-15 20:43:04 +00:00
void SetState(CStateManager&, CAi&, s32);
2017-11-13 06:19:18 +00:00
void SetState(CStateManager&, CAi&, const CStateMachine*, std::string_view);
2016-08-15 20:43:04 +00:00
const std::vector<CAiState>* GetStateVector() const;
void Setup(const CStateMachine* machine);
2018-11-11 03:27:54 +00:00
void SetDelay(float delay) { x10_delay = delay; }
float GetTime() const { return x8_time; }
2018-09-14 00:49:34 +00:00
float GetRandom() const { return xc_random; }
2018-11-11 03:27:54 +00:00
float GetDelay() const { return x10_delay; }
2018-09-14 00:49:34 +00:00
2018-11-11 03:27:54 +00:00
const char* GetName() const
2018-09-14 00:49:34 +00:00
{
if (x4_state)
2018-11-11 03:27:54 +00:00
return x4_state->GetName();
return nullptr;
2018-09-14 00:49:34 +00:00
}
2016-04-25 05:46:28 +00:00
};
CFactoryFnReturn FAiFiniteStateMachineFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,
CObjectReference*);
2016-04-25 05:46:28 +00:00
}