From bb4f870dae29f43cac248c65438b213e49e9700a Mon Sep 17 00:00:00 2001 From: Sara Date: Sat, 30 Mar 2024 22:59:57 +0100 Subject: [PATCH] feat: added the apply state to Action --- src/action.cpp | 22 ++++++++++++++++++++++ src/action.hpp | 8 ++++++++ 2 files changed, 30 insertions(+) diff --git a/src/action.cpp b/src/action.cpp index 7d8e515..909c3cc 100644 --- a/src/action.cpp +++ b/src/action.cpp @@ -1,5 +1,8 @@ #include "action.hpp" +#include "character_actor.hpp" +#include "global_world_state.hpp" #include "utils/godot_macros.h" +#include #include namespace godot::goap { @@ -8,6 +11,7 @@ void Action::_bind_methods() { GDPROPERTY(context_prerequisites, Variant::DICTIONARY); GDPROPERTY(prerequisites, Variant::DICTIONARY); GDPROPERTY(effects, Variant::DICTIONARY); + GDPROPERTY_HINTED(apply_state, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "StateArgs"); } void Action::set_context_prerequisites(Dictionary dict) { @@ -34,6 +38,24 @@ Dictionary Action::get_effects() const { return Action::property_map_to_dict(this->effects); } +void Action::set_apply_state(Ref state) { + this->apply_state = state; +} + +Ref Action::get_apply_state() const { + return this->apply_state; +} + +bool Action::get_is_completed(CharacterActor *context) { + GlobalWorldState *state = GlobalWorldState::get_singleton(); + for(WorldProperty const &prop : this->effects) { + return (prop.key.begins_with("g_") + ? state->get_world_property(prop.key) + : context->call("get_" + prop.key)) == prop.value; + } + return true; +} + Dictionary Action::property_map_to_dict(WorldState const &props) { Dictionary out{}; for(KeyValue const &prop : props) { diff --git a/src/action.hpp b/src/action.hpp index eac8d58..582c342 100644 --- a/src/action.hpp +++ b/src/action.hpp @@ -1,6 +1,7 @@ #ifndef GOAP_ACTION_HPP #define GOAP_ACTION_HPP +#include "state.hpp" #include #include #include @@ -8,6 +9,8 @@ #include #include +namespace godot { class CharacterActor; } + namespace godot::goap { typedef HashMap WorldState; typedef KeyValue WorldProperty; @@ -22,6 +25,10 @@ public: Dictionary get_prerequisites() const; void set_effects(Dictionary dict); Dictionary get_effects() const; + void set_apply_state(Ref args); + Ref get_apply_state() const; + + bool get_is_completed(CharacterActor *context); static Dictionary property_map_to_dict(WorldState const &props); static void dict_to_property_map(Dictionary const &dict, WorldState &out); @@ -29,6 +36,7 @@ public: WorldState context_prerequisites{}; WorldState prerequisites{}; WorldState effects{}; + Ref apply_state{}; }; }