summaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-03-09 01:21:46 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-03-09 01:21:46 +0000
commitd39c6dd18248927044d2f75cefd2ed8ba12eb3b7 (patch)
tree49f165919282a3791d2545d2f18aaf9549586e95 /game
parentSplit ActivityOf<> into its own file (diff)
downloadilt-d39c6dd18248927044d2f75cefd2ed8ba12eb3b7.tar.bz2
ilt-d39c6dd18248927044d2f75cefd2ed8ba12eb3b7.tar.xz
ilt-d39c6dd18248927044d2f75cefd2ed8ba12eb3b7.zip
Make objectives responsible for navigation
Diffstat (limited to 'game')
-rw-r--r--game/network/link.h3
-rw-r--r--game/objective.h2
-rw-r--r--game/objectives/freeroam.cpp22
-rw-r--r--game/objectives/freeroam.h12
-rw-r--r--game/vehicles/vehicle.cpp7
5 files changed, 34 insertions, 12 deletions
diff --git a/game/network/link.h b/game/network/link.h
index e541975..afb3900 100644
--- a/game/network/link.h
+++ b/game/network/link.h
@@ -44,7 +44,8 @@ public:
std::array<End, 2> ends;
float length;
using Next = std::pair<LinkWPtr, unsigned char /*end*/>;
- std::array<std::vector<Next>, 2> nexts;
+ using Nexts = std::vector<Next>;
+ std::array<Nexts, 2> nexts;
};
bool operator<(const glm::vec3 & a, const glm::vec3 & b);
diff --git a/game/objective.h b/game/objective.h
index e4fd6d4..5b3690e 100644
--- a/game/objective.h
+++ b/game/objective.h
@@ -2,6 +2,7 @@
#define OBJECTIVE_H
#include "activity.h"
+#include "network/link.h"
#include <memory>
#include <special_members.hpp>
@@ -15,6 +16,7 @@ public:
[[nodiscard]] virtual Objective * complete() const;
[[nodiscard]] virtual ActivityPtr createActivity() const = 0;
+ [[nodiscard]] virtual Link::Next navigate(Link::Nexts::const_iterator, Link::Nexts::const_iterator) const = 0;
Orders * orders;
};
diff --git a/game/objectives/freeroam.cpp b/game/objectives/freeroam.cpp
new file mode 100644
index 0000000..0721ef2
--- /dev/null
+++ b/game/objectives/freeroam.cpp
@@ -0,0 +1,22 @@
+#include "freeroam.h"
+#include <game/activities/go.h>
+#include <game/activity.h>
+#include <game/network/link.h>
+#include <iterator>
+#include <memory>
+#include <random>
+#include <vector>
+
+ActivityPtr
+FreeRoam::createActivity() const
+{
+ return std::make_unique<Go>();
+}
+
+Link::Next
+FreeRoam::navigate(Link::Nexts::const_iterator begin, Link::Nexts::const_iterator end) const
+{
+ static std::mt19937 gen(std::random_device {}());
+ auto off = std::uniform_int_distribution<>(0, std::distance(begin, end) - 1)(gen);
+ return begin[off];
+}
diff --git a/game/objectives/freeroam.h b/game/objectives/freeroam.h
index c97d9b4..2cfdcfc 100644
--- a/game/objectives/freeroam.h
+++ b/game/objectives/freeroam.h
@@ -1,18 +1,16 @@
#ifndef FREEROAM_H
#define FREEROAM_H
-#include "game/activities/go.h"
-#include "game/objective.h"
+#include <game/activity.h>
+#include <game/network/link.h>
+#include <game/objective.h>
class FreeRoam : public Objective {
public:
using Objective::Objective;
- [[nodiscard]] ActivityPtr
- createActivity() const override
- {
- return std::make_unique<Go>();
- }
+ [[nodiscard]] ActivityPtr createActivity() const override;
+ [[nodiscard]] Link::Next navigate(Link::Nexts::const_iterator, Link::Nexts::const_iterator) const override;
};
#endif
diff --git a/game/vehicles/vehicle.cpp b/game/vehicles/vehicle.cpp
index db61564..e30cbf9 100644
--- a/game/vehicles/vehicle.cpp
+++ b/game/vehicles/vehicle.cpp
@@ -12,7 +12,6 @@
#include <iterator>
#include <location.hpp>
#include <maths.h>
-#include <random>
#include <utility>
#include <vector>
@@ -26,7 +25,6 @@ Vehicle::Vehicle(const LinkPtr & l, float ld) : linkDist {ld}
void
Vehicle::move(TickDuration dur)
{
- static std::mt19937 gen(std::random_device {}());
linkDist += dur.count() * speed;
auto curLink {linkHist.getCurrent()};
while (linkDist > curLink.first->length) {
@@ -36,9 +34,10 @@ Vehicle::move(TickDuration dur)
return std::abs(normalize(n.first.lock()->ends[n.second].second - ang)) > 0.1F;
});
if (last != nexts.begin()) {
- auto off = std::uniform_int_distribution<>(0, std::distance(nexts.begin(), last) - 1)(gen);
+ auto next = (std::distance(nexts.begin(), last) > 1) ? orders.current()->navigate(nexts.cbegin(), last)
+ : nexts.front();
linkDist -= curLink.first->length;
- curLink = linkHist.add(nexts[off].first, nexts[off].second);
+ curLink = linkHist.add(next.first, next.second);
}
else {
linkDist = curLink.first->length;