2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* limbo_hsm.h
|
|
|
|
* =============================================================================
|
2024-03-04 14:53:39 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-07-21 09:50:06 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-09-29 10:54:07 +00:00
|
|
|
|
|
|
|
#ifndef LIMBO_HSM_H
|
|
|
|
#define LIMBO_HSM_H
|
|
|
|
|
2023-07-20 16:35:36 +00:00
|
|
|
#include "limbo_state.h"
|
|
|
|
|
2024-07-21 11:28:13 +00:00
|
|
|
#define TransitionKey Pair<uint64_t, StringName>
|
|
|
|
|
2022-09-29 10:54:07 +00:00
|
|
|
class LimboHSM : public LimboState {
|
|
|
|
GDCLASS(LimboHSM, LimboState);
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum UpdateMode : unsigned int {
|
|
|
|
IDLE, // automatically call update() during NOTIFICATION_PROCESS
|
|
|
|
PHYSICS, // automatically call update() during NOTIFICATION_PHYSICS
|
|
|
|
MANUAL, // manually update state machine: user must call update(delta)
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2024-07-21 11:45:36 +00:00
|
|
|
struct TransitionKeyHasher {
|
|
|
|
static uint32_t hash(const TransitionKey &P) {
|
|
|
|
uint64_t h1 = HashMapHasherDefault::hash(P.first);
|
|
|
|
uint64_t h2 = HashMapHasherDefault::hash(P.second);
|
|
|
|
return hash_one_uint64((h1 << 32) | h2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-21 11:28:13 +00:00
|
|
|
struct Transition {
|
|
|
|
ObjectID from_state;
|
|
|
|
ObjectID to_state;
|
|
|
|
StringName event;
|
|
|
|
|
|
|
|
inline bool is_valid() const { return to_state != ObjectID(); }
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ TransitionKey make_key(LimboState *p_from_state, const StringName &p_event) {
|
|
|
|
return TransitionKey(
|
|
|
|
p_from_state != nullptr ? uint64_t(p_from_state->get_instance_id()) : 0,
|
|
|
|
p_event);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-29 10:54:07 +00:00
|
|
|
UpdateMode update_mode;
|
|
|
|
LimboState *initial_state;
|
|
|
|
LimboState *active_state;
|
2024-03-05 11:58:55 +00:00
|
|
|
LimboState *previous_active;
|
2024-04-20 19:30:26 +00:00
|
|
|
LimboState *next_active;
|
|
|
|
bool updating = false;
|
2022-09-29 10:54:07 +00:00
|
|
|
|
2024-07-21 11:28:13 +00:00
|
|
|
HashMap<TransitionKey, Transition, TransitionKeyHasher> transitions;
|
2022-09-29 10:54:07 +00:00
|
|
|
|
2024-07-21 11:45:36 +00:00
|
|
|
void _get_transition(LimboState *p_from_state, const StringName &p_event, Transition &r_transition) const;
|
|
|
|
|
2022-09-29 10:54:07 +00:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
void _notification(int p_what);
|
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
virtual void _initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) override;
|
2024-03-04 14:53:39 +00:00
|
|
|
virtual bool _dispatch(const StringName &p_event, const Variant &p_cargo = Variant()) override;
|
2024-01-12 22:20:39 +00:00
|
|
|
|
2024-01-18 10:32:32 +00:00
|
|
|
virtual void _enter() override;
|
|
|
|
virtual void _exit() override;
|
|
|
|
virtual void _update(double p_delta) override;
|
2022-09-29 10:54:07 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void set_update_mode(UpdateMode p_mode) { update_mode = p_mode; }
|
|
|
|
UpdateMode get_update_mode() const { return update_mode; }
|
|
|
|
|
2024-07-20 16:04:41 +00:00
|
|
|
void set_active(bool p_active);
|
|
|
|
|
|
|
|
void change_active_state(LimboState *p_state);
|
|
|
|
|
2024-01-18 10:32:32 +00:00
|
|
|
LimboState *get_active_state() const { return active_state; }
|
2024-03-05 11:58:55 +00:00
|
|
|
LimboState *get_previous_active_state() const { return previous_active; }
|
2022-09-29 10:54:07 +00:00
|
|
|
LimboState *get_leaf_state() const;
|
|
|
|
|
2023-07-25 16:39:41 +00:00
|
|
|
void set_initial_state(LimboState *p_state);
|
2022-10-26 21:12:29 +00:00
|
|
|
LimboState *get_initial_state() const { return initial_state; }
|
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
virtual void initialize(Node *p_agent, const Ref<Blackboard> &p_parent_scope = nullptr);
|
2022-09-29 10:54:07 +00:00
|
|
|
|
2024-01-12 22:20:39 +00:00
|
|
|
void update(double p_delta);
|
2024-07-20 15:23:13 +00:00
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
void add_transition(LimboState *p_from_state, LimboState *p_to_state, const StringName &p_event);
|
2024-07-20 15:23:13 +00:00
|
|
|
void remove_transition(LimboState *p_from_state, const StringName &p_event);
|
2024-07-21 11:35:16 +00:00
|
|
|
bool has_transition(LimboState *p_from_state, const StringName &p_event) const { return transitions.has(Transition::make_key(p_from_state, p_event)); }
|
2024-07-20 15:23:13 +00:00
|
|
|
|
2024-01-18 10:32:32 +00:00
|
|
|
LimboState *anystate() const { return nullptr; }
|
2022-09-29 10:54:07 +00:00
|
|
|
|
|
|
|
LimboHSM();
|
|
|
|
};
|
|
|
|
|
2024-01-13 16:10:42 +00:00
|
|
|
#endif // LIMBO_HSM_H
|