From ea35e8ede4c3a522375d4539c872e8a6d6c9830a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 16 Mar 2021 18:29:37 +0000 Subject: Implement goto so node Encompasses determining a route and a distance to travel --- game/objectives/goto.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ game/objectives/goto.h | 22 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 game/objectives/goto.cpp create mode 100644 game/objectives/goto.h (limited to 'game/objectives') diff --git a/game/objectives/goto.cpp b/game/objectives/goto.cpp new file mode 100644 index 0000000..c089bc3 --- /dev/null +++ b/game/objectives/goto.cpp @@ -0,0 +1,43 @@ +#include "goto.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +GoTo::GoTo(Orders * o, const Link::End & cp, float d, const NodePtr & dest) : + Objective(o), links(RouteWalker().findRouteTo(cp, dest)), startDist {d} +{ +} + +ActivityPtr +GoTo::createActivity() const +{ + return std::make_unique(std::accumulate(links.begin(), links.end(), 0, + [](auto p, const auto & l) { + return p += l.first.lock()->length; + }) + + startDist + + ); +} + +inline bool +operator==(const Link::Next & l, const Link::Next & n) +{ + return l.second == n.second && l.first.lock() == n.first.lock(); +} + +Link::Next +GoTo::navigate(Link::Nexts::const_iterator begin, Link::Nexts::const_iterator end) const +{ + const auto nextStep = std::find_first_of(links.begin(), links.end(), begin, end); + if (nextStep == links.end()) { + return *begin; + } + return *nextStep; +} diff --git a/game/objectives/goto.h b/game/objectives/goto.h new file mode 100644 index 0000000..01e47ae --- /dev/null +++ b/game/objectives/goto.h @@ -0,0 +1,22 @@ +#ifndef GOTO_H +#define GOTO_H + +#include +#include +#include + +class Orders; + +class GoTo : public Objective { +public: + GoTo(Orders * os, const Link::End &, float, const NodePtr & dest); + + [[nodiscard]] ActivityPtr createActivity() const override; + [[nodiscard]] Link::Next navigate(Link::Nexts::const_iterator, Link::Nexts::const_iterator) const override; + +private: + Link::Nexts links; + float startDist; +}; + +#endif -- cgit v1.2.3