diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-06 03:22:03 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-05-06 03:22:03 +0200 |
commit | 518e8cd4cc14bf04dd08f2f8db793430fea175fd (patch) | |
tree | 3e3f57b5c18bbb335b4840f0512eb57d8bc21222 /src | |
parent | de33d8773c3357653414143b8a76b08b8aa8d149 (diff) |
a?
Diffstat (limited to 'src')
-rw-r--r-- | src/critter-script-walk.cpp | 79 | ||||
-rw-r--r-- | src/critter-script.hpp | 6 | ||||
-rw-r--r-- | src/script.hpp | 3 | ||||
-rw-r--r-- | src/script.inl | 19 | ||||
-rw-r--r-- | src/search-node.hpp | 6 | ||||
-rw-r--r-- | src/search-result.cpp | 1 | ||||
-rw-r--r-- | src/search-result.hpp | 7 |
7 files changed, 102 insertions, 19 deletions
diff --git a/src/critter-script-walk.cpp b/src/critter-script-walk.cpp index 9c92798c..564c3023 100644 --- a/src/critter-script-walk.cpp +++ b/src/critter-script-walk.cpp @@ -5,42 +5,88 @@ #include "search-result.hpp" #include "search-astar.hpp" #include "entity/name-of.hpp" +#include <cr/Optional.h> namespace floormat { +using walk_mode = critter_script::walk_mode; + namespace { -enum class walk_mode : uint8_t +bool walk_line(point dest, const std::shared_ptr<critter>& c, size_t& i, const Ns& dt) { - none, line, path, -}; + Debug{} << "move from" << c->position() << "to" << dest; + auto res = c->move_toward(i, dt, dest); + DBG_nospace << "blocked:" << res.blocked << " moved:" << res.moved; + return res.blocked || c->position() == dest; +} struct walk_script final : critter_script { + struct line_tag_t {}; + struct path_tag_t {}; + const void* type_id() const override; void on_init(const std::shared_ptr<critter>& c) override; void on_update(const std::shared_ptr<critter>& c, size_t& i, const Ns& dt) override; void on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason reason) override; void delete_self() noexcept override; - point to; - path_search_result path; + walk_script(point dest, line_tag_t); + //walk_script(point dest, path_search_result path, path_tag_t); + +private: + point dest; + Optional<path_search_result> path; walk_mode mode = walk_mode::none; }; -constexpr StringView script_name = name_of<walk_script>; - -const void* walk_script::type_id() const +walk_script::walk_script(point dest, line_tag_t) : + dest{dest}, mode{walk_mode::line} { - return &script_name; } void walk_script::on_init(const std::shared_ptr<critter>& c) { + c->movement.AUTO = true; } void walk_script::on_update(const std::shared_ptr<critter>& c, size_t& i, const Ns& dt) { + auto& m = c->movement; + + if (m.L | m.R | m.U | m.D) + m.AUTO = false; + + if (!m.AUTO) + goto done; + + switch (mode) + { + case walk_mode::none: + fm_abort("bad walk_mode 'none'"); + case walk_mode::line: + fm_assert(!path); + if (walk_line(dest, c, i, dt)) + goto done; + return; + case walk_mode::path: + fm_abort("todo!"); + //break; + } + std::unreachable(); + fm_assert(false); + +done: + Debug{} << " finished walking"; + c->script.do_clear(c); +} + +constexpr StringView script_name = name_of<walk_script>; + +const void* walk_script::type_id() const +{ + return &script_name; } void walk_script::on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason reason) @@ -54,9 +100,20 @@ void walk_script::delete_self() noexcept } // namespace -critter_script* critter_script::make_walk_script(point to, path_search_result path) +Pointer<critter_script> critter_script::make_walk_script(point dest, const path_search_result& path, walk_mode mode) { - return new walk_script{}; + switch (mode) + { + case walk_mode::line: + fm_assert(path.empty()); + return Pointer<critter_script>(new walk_script{dest, walk_script::line_tag_t{}}); + case walk_mode::path: + fm_abort("todo!"); + fm_assert(!path.empty()); + case walk_mode::none: + break; + } + fm_abort("bad walk_mode %d", (int)mode); } } // namespace floormat diff --git a/src/critter-script.hpp b/src/critter-script.hpp index dbfe9dd8..630baa4b 100644 --- a/src/critter-script.hpp +++ b/src/critter-script.hpp @@ -1,6 +1,7 @@ #pragma once #include "script.hpp" #include <memory> +#include <cr/Pointer.h> namespace floormat { @@ -21,7 +22,10 @@ struct critter_script : base_script virtual void on_destroy(const std::shared_ptr<critter>& c, script_destroy_reason reason) = 0; virtual void delete_self() = 0; - [[nodiscard]] static critter_script* make_walk_script(point to, path_search_result path); + // todo! move to src/scripts dir + + enum class walk_mode : uint8_t { none, line, path, }; + static Pointer<critter_script> make_walk_script(point to, const path_search_result& path, walk_mode mode); }; } // namespace floormat diff --git a/src/script.hpp b/src/script.hpp index 7493dd28..59b914cc 100644 --- a/src/script.hpp +++ b/src/script.hpp @@ -46,10 +46,13 @@ public: script_lifecycle state() const; S* operator->(); + explicit operator bool() const; void do_create(S* ptr); + void do_create(Pointer<S> ptr); void do_initialize(const std::shared_ptr<Obj>& obj); void do_reassign(S* ptr, const std::shared_ptr<Obj>& obj); + void do_reassign(Pointer<S> ptr, const std::shared_ptr<Obj>& obj); void do_clear(const std::shared_ptr<Obj>& obj); void do_destroy_pre(const std::shared_ptr<Obj>& obj, script_destroy_reason r); void do_finish_destroy(); diff --git a/src/script.inl b/src/script.inl index afa84e65..27a79e31 100644 --- a/src/script.inl +++ b/src/script.inl @@ -2,7 +2,8 @@ #include "script.hpp" #include "compat/assert.hpp" #include <utility> -#include <Corrade/Containers/StringView.h> +#include <cr/StringView.h> +#include <cr/Pointer.h> // ReSharper disable CppDFAUnreachableCode @@ -61,6 +62,7 @@ Script<S, Obj>::Script(): ptr{nullptr}, _state{script_lifecycle::no_init} } template <typename S, typename Obj> script_lifecycle Script<S, Obj>::state() const { return _state; } +template<typename S, typename Obj> Script<S, Obj>::operator bool() const { return ptr; } template <typename S, typename Obj> S* Script<S, Obj>::operator->() @@ -73,12 +75,19 @@ S* Script<S, Obj>::operator->() template<typename S, typename Obj> void Script<S, Obj>::do_create(S* p) { - fm_assert(p); + if (!p) + p = make_empty(); FM_ASSERT_SCRIPT_STATE(script_lifecycle::no_init); _state = script_lifecycle::initializing; ptr = p; } +template<typename S, typename Obj> +void Script<S, Obj>::do_create(Pointer<S> p) +{ + do_create(p.release()); +} + template <typename S, typename Obj> void Script<S, Obj>::do_initialize(const std::shared_ptr<Obj>& obj) { @@ -112,6 +121,12 @@ void Script<S, Obj>::do_reassign(S* p, const std::shared_ptr<Obj>& obj) } template <typename S, typename Obj> +void Script<S, Obj>::do_reassign(Pointer<S> p, const std::shared_ptr<Obj>& obj) +{ + return do_reassign(p.release(), obj); +} + +template <typename S, typename Obj> void Script<S, Obj>::do_clear(const std::shared_ptr<Obj>& obj) { FM_ASSERT_SCRIPT_STATE(script_lifecycle::created); diff --git a/src/search-node.hpp b/src/search-node.hpp index ebe33b9a..68d68605 100644 --- a/src/search-node.hpp +++ b/src/search-node.hpp @@ -7,18 +7,18 @@ namespace floormat { +template<typename T> struct path_search_result_pool_access; + struct path_search_result::node { friend struct path_search_result; - friend struct test_app; + template<typename T> friend struct path_search_result_pool_access; node() noexcept; fm_DECLARE_DELETED_COPY_ASSIGNMENT(node); fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(node); std::vector<point> vec; - -private: Pointer<node> _next; }; diff --git a/src/search-result.cpp b/src/search-result.cpp index c2065735..ed9c5194 100644 --- a/src/search-result.cpp +++ b/src/search-result.cpp @@ -80,6 +80,7 @@ path_search_result& path_search_result::operator=(path_search_result&& other) no } size_t path_search_result::size() const { return _node->vec.size(); } +bool path_search_result::empty() const { return _node->vec.empty(); } path_search_result::node::node() noexcept = default; float path_search_result::time() const { return _time; } diff --git a/src/search-result.hpp b/src/search-result.hpp index 97d40242..aa27c374 100644 --- a/src/search-result.hpp +++ b/src/search-result.hpp @@ -1,18 +1,21 @@ #pragma once #include "compat/vector-wrapper-fwd.hpp" #include "src/global-coords.hpp" -#include <Corrade/Containers/Pointer.h> +#include <cr/Pointer.h> namespace floormat { struct point; +template<typename T> struct path_search_result_pool_access; struct path_search_result final { - friend struct test_app; + template<typename T> friend struct path_search_result_pool_access; + struct node; size_t size() const; + bool empty() const; uint32_t cost() const; void set_cost(uint32_t value); float time() const; |