diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/gamestate.h | 3 | ||||
-rw-r--r-- | game/geoData.cpp | 30 | ||||
-rw-r--r-- | game/network/link.cpp | 65 | ||||
-rw-r--r-- | game/network/link.h | 7 | ||||
-rw-r--r-- | game/network/network.cpp | 83 | ||||
-rw-r--r-- | game/network/network.h | 29 | ||||
-rw-r--r-- | game/network/network.impl.h | 41 | ||||
-rw-r--r-- | game/network/rail.cpp | 90 | ||||
-rw-r--r-- | game/orders.h | 2 | ||||
-rw-r--r-- | game/scenary/foliage.cpp | 10 | ||||
-rw-r--r-- | game/scenary/foliage.h | 2 | ||||
-rw-r--r-- | game/scenary/illuminator.cpp | 10 | ||||
-rw-r--r-- | game/scenary/illuminator.h | 2 | ||||
-rw-r--r-- | game/vehicles/railVehicle.cpp | 9 | ||||
-rw-r--r-- | game/vehicles/railVehicleClass.cpp | 13 | ||||
-rw-r--r-- | game/vehicles/railVehicleClass.h | 2 | ||||
-rw-r--r-- | game/vehicles/train.h | 2 | ||||
-rw-r--r-- | game/water.h | 2 |
18 files changed, 237 insertions, 165 deletions
diff --git a/game/gamestate.h b/game/gamestate.h index 189417d..85cb5db 100644 --- a/game/gamestate.h +++ b/game/gamestate.h @@ -8,6 +8,7 @@ class WorldObject; class Terrain; class Environment; +class Renderable; class GameState { public: @@ -16,7 +17,7 @@ public: NO_MOVE(GameState); NO_COPY(GameState); - Collection<WorldObject> world; + SharedCollection<WorldObject, Renderable> world; std::shared_ptr<Terrain> terrain; std::shared_ptr<Environment> environment; AssetFactory::Assets assets; diff --git a/game/geoData.cpp b/game/geoData.cpp index 5cea4dd..b886efd 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -121,11 +121,9 @@ GeoData::intersectRay(const Ray<GlobalPosition3D> & ray, FaceHandle face) const walkUntil(PointFace {ray.start, face}, ray.start.xy() + (ray.direction.xy() * ::difference(extents.max.xy(), extents.min.xy())), [&out, &ray, this](const auto & step) { - BaryPosition bari {}; - RelativeDistance dist {}; const auto t = triangle<3>(step.current); - if (ray.intersectTriangle(t.x, t.y, t.z, bari, dist)) { - out.emplace(t * bari, step.current); + if (const auto inter = ray.intersectTriangle(t.x, t.y, t.z)) { + out.emplace(t * inter->bary, step.current); return true; } return false; @@ -286,9 +284,27 @@ GeoData::updateAllVertexNormals(const R & range) void GeoData::updateVertexNormal(VertexHandle vertex) { - Normal3D n; - calc_vertex_normal_correct(vertex, n); - set_normal(vertex, glm::normalize(n)); + Normal3D normal {}; + { // Lifted from calc_vertex_normal_correct in PolyMeshT_impl.hh but doesn't use Scalar to normalise + ConstVertexIHalfedgeIter cvih_it = this->cvih_iter(vertex); + if (!cvih_it.is_valid()) { // don't crash on isolated vertices + return; + } + Normal in_he_vec; + calc_edge_vector(*cvih_it, in_he_vec); + for (; cvih_it.is_valid(); ++cvih_it) { // calculates the sector normal defined by cvih_it and adds it to normal + if (this->is_boundary(*cvih_it)) { + continue; + } + HalfedgeHandle out_heh(this->next_halfedge_handle(*cvih_it)); + Normal out_he_vec; + calc_edge_vector(out_heh, out_he_vec); + normal += cross(in_he_vec, out_he_vec); // sector area is taken into account + in_he_vec = out_he_vec; + in_he_vec *= -1; // change the orientation + } + } // End lift + set_normal(vertex, glm::normalize(normal)); } OpenMesh::VertexHandle diff --git a/game/network/link.cpp b/game/network/link.cpp index c84524c..b8ffee2 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -5,37 +5,43 @@ #include <ray.h> #include <tuple> -Link::Link(End a, End b, float l) : ends {{std::move(a), std::move(b)}}, length {l} { } +Link::Link(End endA, End endB, float len) : ends {{std::move(endA), std::move(endB)}}, length {len} { } -LinkCurve::LinkCurve(GlobalPosition3D c, RelativeDistance r, Arc a) : centreBase {c}, radius {r}, arc {std::move(a)} { } +LinkCurve::LinkCurve(GlobalPosition3D centre, RelativeDistance radius, Arc arc) : + centreBase {centre}, radius {radius}, arc {std::move(arc)} +{ +} bool -operator<(const GlobalPosition3D & a, const GlobalPosition3D & b) +operator<(const GlobalPosition3D & left, const GlobalPosition3D & right) { // NOLINTNEXTLINE(hicpp-use-nullptr,modernize-use-nullptr) - return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z); + return std::tie(left.x, left.y, left.z) < std::tie(right.x, right.y, right.z); } bool -operator<(const Node & a, const Node & b) +operator<(const Node & left, const Node & right) { - return a.pos < b.pos; + return left.pos < right.pos; } Location LinkStraight::positionAt(RelativeDistance dist, unsigned char start) const { - const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; - const RelativePosition3D diff {es.second->pos - es.first->pos}; - const auto dir {glm::normalize(diff)}; - return Location {es.first->pos + (vehiclePositionOffset() + dir * dist), {vector_pitch(dir), vector_yaw(dir), 0}}; + const auto endNodes = std::make_pair(ends[start].node.get(), ends[1 - start].node.get()); + const auto diff = ::difference(endNodes.second->pos, endNodes.first->pos); + const auto directionVector = glm::normalize(diff); + return Location { + .pos = endNodes.first->pos + (vehiclePositionOffset() + directionVector * dist), + .rot = {vector_pitch(directionVector), vector_yaw(directionVector), 0}, + }; } bool LinkStraight::intersectRay(const Ray<GlobalPosition3D> & ray) const { - return ray.passesCloseToEdges( - std::array {GlobalPosition3D {ends.front().node->pos}, GlobalPosition3D {ends.back().node->pos}}, 1000); + static constexpr auto PROXIMITY = 1'000; + return ray.passesCloseToEdges(std::array {ends.front().node->pos, ends.back().node->pos}, PROXIMITY); } std::vector<GlobalPosition3D> @@ -55,33 +61,36 @@ LinkStraight::getBase(RelativeDistance width) const Location LinkCurve::positionAt(float dist, unsigned char start) const { - static constexpr std::array<float, 2> dirOffset {half_pi, -half_pi}; - const auto frac {dist / length}; - const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; - const auto as {std::make_pair(arc[start], arc[1 - start])}; - const auto ang {as.first + ((as.second - as.first) * frac)}; - const auto relPos {(sincos(ang) || 0.F) * radius}; - const auto relClimb {vehiclePositionOffset() + static constexpr std::array DIR_OFFSET {half_pi, -half_pi}; + const auto frac = dist / length; + const auto endNodes = std::make_pair(ends[start].node.get(), ends[1 - start].node.get()); + const auto arcEndAngles = std::make_pair(arc[start], arc[1 - start]); + const auto ang = glm::mix(arcEndAngles.first, arcEndAngles.second, frac); + const auto relPos = (sincos(ang) || 0.F) * radius; + const auto relClimb = vehiclePositionOffset() + RelativePosition3D {0, 0, - static_cast<RelativeDistance>(es.first->pos.z - centreBase.z) - + (static_cast<RelativeDistance>(es.second->pos.z - es.first->pos.z) * frac)}}; - const auto pitch {vector_pitch(difference(es.second->pos, es.first->pos) / length)}; - return Location {GlobalPosition3D(relPos + relClimb) + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; + static_cast<RelativeDistance>(endNodes.first->pos.z - centreBase.z) + + (static_cast<RelativeDistance>(endNodes.second->pos.z - endNodes.first->pos.z) * frac)}; + const auto pitch {vector_pitch(difference(endNodes.second->pos, endNodes.first->pos) / length)}; + return Location { + .pos = GlobalPosition3D(relPos + relClimb) + centreBase, + .rot = {pitch, normalize(ang + DIR_OFFSET[start]), 0}, + }; } bool LinkCurve::intersectRay(const Ray<GlobalPosition3D> & ray) const { - const auto & e0p {ends[0].node->pos}; - const auto & e1p {ends[1].node->pos}; + const auto e0p = ends[0].node->pos.z; + const auto e1p = ends[1].node->pos.z; const auto slength = round_frac(length / 2.F, 5.F); const auto segs = std::round(15.F * slength / std::pow(radius, 0.7F)); - const auto step {glm::vec<2, RelativeDistance> {arc.length(), e1p.z - e0p.z} / segs}; + const auto step {glm::vec<2, RelativeDistance> {arc.length(), e1p - e0p} / segs}; auto segCount = static_cast<std::size_t>(std::lround(segs)) + 1; std::vector<GlobalPosition3D> points; points.reserve(segCount); - for (std::remove_const_t<decltype(step)> swing = {arc.first, centreBase.z - e0p.z}; segCount; + for (std::remove_const_t<decltype(step)> swing = {arc.first, centreBase.z - e0p}; segCount; swing += step, --segCount) { points.emplace_back(centreBase + ((sincos(swing.x) * radius) || swing.y)); } @@ -94,7 +103,7 @@ LinkCurve::getBase(RelativeDistance width) const const auto start = ends.front().node->pos; const auto end = ends.back().node->pos; const auto segs = std::ceil(std::sqrt(radius) * 0.02F * arc.length()); - const auto step {glm::vec<2, RelativeDistance> {arc.length(), end.z - start.z} / segs}; + const auto step = glm::vec<2, RelativeDistance> {arc.length(), end.z - start.z} / segs; auto segCount = static_cast<size_t>(segs) + 1; std::vector<GlobalPosition3D> out; diff --git a/game/network/link.h b/game/network/link.h index 59bbb65..0b58558 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -16,7 +16,7 @@ template<typename> class Ray; // it has location class Node : public StdTypeDefs<Node> { public: - explicit Node(GlobalPosition3D p) noexcept : pos(p) { }; + explicit Node(GlobalPosition3D position) noexcept : pos(position) { }; virtual ~Node() noexcept = default; NO_COPY(Node); NO_MOVE(Node); @@ -35,6 +35,7 @@ public: struct End { Node::Ptr node; float dir; + // NOLINTNEXTLINE(readability-redundant-member-init) don't require client to empty initialise this Nexts nexts {}; }; @@ -58,8 +59,8 @@ protected: } }; -bool operator<(const GlobalPosition3D & a, const GlobalPosition3D & b); -bool operator<(const Node & a, const Node & b); +bool operator<(const GlobalPosition3D &, const GlobalPosition3D &); +bool operator<(const Node &, const Node &); class LinkStraight : public virtual Link { public: diff --git a/game/network/network.cpp b/game/network/network.cpp index e67942f..c8482de 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -8,8 +8,8 @@ #include <stdexcept> #include <utility> -Network::Network(const std::string & tn) : - texture {std::make_shared<Texture>(tn, +Network::Network(const std::string & textureName) : + texture {std::make_shared<Texture>(textureName, TextureOptions { .minFilter = GL_NEAREST_MIPMAP_LINEAR, })} @@ -25,19 +25,18 @@ Network::nodeAt(GlobalPosition3D pos) Network::NodeInsertion Network::newNodeAt(GlobalPosition3D pos) { - if (auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) { - return {*nodes.insert(std::move(n)).first, i}; - } - else { - return {std::move(n), NodeIs::InNetwork}; + auto [node, inNetwork] = candidateNodeAt(pos); + if (inNetwork == NodeIs::NotInNetwork) { + return {*nodes.insert(std::move(node)).first, inNetwork}; } + return {std::move(node), NodeIs::InNetwork}; } Node::Ptr Network::findNodeAt(GlobalPosition3D pos) const { - if (const auto n = nodes.find(pos); n != nodes.end()) { - return *n; + if (const auto node = nodes.find(pos); node != nodes.end()) { + return *node; } return {}; } @@ -45,8 +44,8 @@ Network::findNodeAt(GlobalPosition3D pos) const Network::NodeInsertion Network::candidateNodeAt(GlobalPosition3D pos) const { - if (const auto n = nodes.find(pos); n != nodes.end()) { - return {*n, NodeIs::InNetwork}; + if (const auto node = nodes.find(pos); node != nodes.end()) { + return {*node, NodeIs::InNetwork}; } return {std::make_shared<Node>(pos), NodeIs::NotInNetwork}; } @@ -54,12 +53,11 @@ Network::candidateNodeAt(GlobalPosition3D pos) const Node::Ptr Network::intersectRayNodes(const Ray<GlobalPosition3D> & ray) const { + static constexpr auto MIN_DISTANCE = 2000; // Click within 2m of a node if (const auto node = std::find_if(nodes.begin(), nodes.end(), [&ray](const Node::Ptr & node) { - GlobalPosition3D ipos; - Normal3D inorm; - return ray.intersectSphere(node->pos, 2000, ipos, inorm); + return ray.intersectSphere(node->pos, MIN_DISTANCE); }); node != nodes.end()) { return *node; @@ -68,14 +66,14 @@ Network::intersectRayNodes(const Ray<GlobalPosition3D> & ray) const } void -Network::joinLinks(const Link::Ptr & l, const Link::Ptr & ol) +Network::joinLinks(const Link::Ptr & link, const Link::Ptr & oldLink) { - if (l != ol) { - for (const auto oe : {0U, 1U}) { - for (const auto te : {0U, 1U}) { - if (l->ends[te].node == ol->ends[oe].node) { - l->ends[te].nexts.emplace_back(ol, oe); - ol->ends[oe].nexts.emplace_back(l, te); + if (link != oldLink) { + for (const auto oldLinkEnd : {0U, 1U}) { + for (const auto linkEnd : {0U, 1U}) { + if (link->ends[linkEnd].node == oldLink->ends[oldLinkEnd].node) { + link->ends[linkEnd].nexts.emplace_back(oldLink, oldLinkEnd); + oldLink->ends[oldLinkEnd].nexts.emplace_back(link, linkEnd); } } } @@ -93,7 +91,7 @@ Network::routeFromTo(const Link::End & start, GlobalPosition3D dest) const } Link::Nexts -Network::routeFromTo(const Link::End & end, const Node::Ptr & dest) const +Network::routeFromTo(const Link::End & end, const Node::Ptr & dest) { return RouteWalker().findRouteTo(end, dest); } @@ -102,11 +100,11 @@ GenCurveDef Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir) { const auto diff = difference(end, start); - const auto vy {vector_yaw(diff)}; + const auto yaw = vector_yaw(diff); const auto dir = pi + startDir; - const auto flatStart {start.xy()}, flatEnd {end.xy()}; - const auto n2ed {(vy * 2) - dir - pi}; - const auto centre {find_arc_centre(flatStart, dir, flatEnd, n2ed)}; + const auto flatStart = start.xy(), flatEnd = end.xy(); + const auto n2ed = (yaw * 2) - dir - pi; + const auto centre = find_arc_centre(flatStart, dir, flatEnd, n2ed); if (centre.second) { // right hand arc return {end, start, centre.first}; @@ -121,24 +119,23 @@ Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & en endDir += pi; const auto flatStart {start.xy()}, flatEnd {end.xy()}; auto midheight = [&](auto mid) { - const auto sm = ::distance<2>(flatStart, mid); - const auto em = ::distance<2>(flatEnd, mid); - return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (sm / (sm + em))); + const auto startToMid = ::distance<2>(flatStart, mid); + const auto endToMid = ::distance<2>(flatEnd, mid); + return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (startToMid / (startToMid + endToMid))); }; - if (const auto radii = find_arcs_radius(flatStart, startDir, flatEnd, endDir); radii.first < radii.second) { - const auto radius {radii.first}; - const auto c1 = flatStart + (sincos(startDir + half_pi) * radius); - const auto c2 = flatEnd + (sincos(endDir + half_pi) * radius); - const auto mid = (c1 + c2) / 2; - const auto midh = mid || midheight(mid); - return {{start, midh, c1}, {end, midh, c2}}; - } - else { - const auto radius {radii.second}; - const auto c1 = flatStart + (sincos(startDir - half_pi) * radius); - const auto c2 = flatEnd + (sincos(endDir - half_pi) * radius); - const auto mid = (c1 + c2) / 2; + const auto radii = find_arcs_radius(flatStart, startDir, flatEnd, endDir); + if (radii.first < radii.second) { + const auto radius = radii.first; + const auto centre1 = flatStart + (sincos(startDir + half_pi) * radius); + const auto centre2 = flatEnd + (sincos(endDir + half_pi) * radius); + const auto mid = (centre1 + centre2) / 2; const auto midh = mid || midheight(mid); - return {{midh, start, c1}, {midh, end, c2}}; + return {{start, midh, centre1}, {end, midh, centre2}}; } + const auto radius = radii.second; + const auto centre1 = flatStart + (sincos(startDir - half_pi) * radius); + const auto centre2 = flatEnd + (sincos(endDir - half_pi) * radius); + const auto mid = (centre1 + centre2) / 2; + const auto midh = mid || midheight(mid); + return {{midh, start, centre1}, {midh, end, centre2}}; } diff --git a/game/network/network.h b/game/network/network.h index 291c4ec..4f5d2b0 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -18,7 +18,7 @@ struct Surface; class GeoData; template<typename> class Ray; -template<size_t... n> using GenDef = std::tuple<glm::vec<n, GlobalDistance>...>; +template<size_t... N> using GenDef = std::tuple<glm::vec<N, GlobalDistance>...>; using GenCurveDef = GenDef<3, 3, 2>; class Network { @@ -30,7 +30,7 @@ public: [[nodiscard]] Node::Ptr findNodeAt(GlobalPosition3D) const; [[nodiscard]] Node::Ptr nodeAt(GlobalPosition3D); - enum class NodeIs { InNetwork, NotInNetwork }; + enum class NodeIs : uint8_t { InNetwork, NotInNetwork }; using NodeInsertion = std::pair<Node::Ptr, NodeIs>; [[nodiscard]] NodeInsertion newNodeAt(GlobalPosition3D); [[nodiscard]] NodeInsertion candidateNodeAt(GlobalPosition3D) const; @@ -38,7 +38,7 @@ public: [[nodiscard]] virtual Node::Ptr intersectRayNodes(const Ray<GlobalPosition3D> &) const; [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, GlobalPosition3D) const; - [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, const Node::Ptr &) const; + [[nodiscard]] static Link::Nexts routeFromTo(const Link::End &, const Node::Ptr &); virtual Link::CCollection candidateStraight(GlobalPosition3D, GlobalPosition3D) = 0; virtual Link::CCollection candidateJoins(GlobalPosition3D, GlobalPosition3D) = 0; @@ -53,7 +53,7 @@ public: [[nodiscard]] virtual RelativeDistance getBaseWidth() const = 0; protected: - static void joinLinks(const Link::Ptr & l, const Link::Ptr & ol); + static void joinLinks(const Link::Ptr & link, const Link::Ptr & oldLink); static GenCurveDef genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir); static std::pair<GenCurveDef, GenCurveDef> genCurveDef( const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir, float endDir); @@ -77,37 +77,36 @@ class NetworkOf : public Network, public Renderable, public NetworkLinkHolder<Li protected: using Network::Network; - Collection<T> links; + SharedCollection<T> links; void joinLinks(const Link::Ptr &) const; -protected: [[nodiscard]] Link::Ptr intersectRayLinks(const Ray<GlobalPosition3D> &) const override; public: template<typename L, typename... Params> std::shared_ptr<L> - candidateLink(GlobalPosition3D a, GlobalPosition3D b, Params &&... params) + candidateLink(GlobalPosition3D positionA, GlobalPosition3D positionB, Params &&... params) requires std::is_base_of_v<T, L> { - const auto node1 = candidateNodeAt(a).first, node2 = candidateNodeAt(b).first; + const auto node1 = candidateNodeAt(positionA).first, node2 = candidateNodeAt(positionB).first; return std::make_shared<L>(*this, node1, node2, std::forward<Params>(params)...); } template<typename L, typename... Params> std::shared_ptr<L> - addLink(GlobalPosition3D a, GlobalPosition3D b, Params &&... params) + addLink(GlobalPosition3D positionA, GlobalPosition3D positionB, Params &&... params) requires std::is_base_of_v<T, L> { - const auto node1 = nodeAt(a), node2 = nodeAt(b); - auto l {links.template create<L>(*this, node1, node2, std::forward<Params>(params)...)}; - joinLinks(l); - return l; + const auto node1 = nodeAt(positionA), node2 = nodeAt(positionB); + auto newLink = links.template create<L>(*this, node1, node2, std::forward<Params>(params)...); + joinLinks(newLink); + return std::move(newLink); } - Link::CCollection candidateStraight(GlobalPosition3D n1, GlobalPosition3D n2) override; + Link::CCollection candidateStraight(GlobalPosition3D, GlobalPosition3D) override; Link::CCollection candidateJoins(GlobalPosition3D, GlobalPosition3D) override; Link::CCollection candidateExtend(GlobalPosition3D, GlobalPosition3D) override; - Link::CCollection addStraight(const GeoData *, GlobalPosition3D n1, GlobalPosition3D n2) override; + Link::CCollection addStraight(const GeoData *, GlobalPosition3D, GlobalPosition3D) override; Link::CCollection addJoins(const GeoData *, GlobalPosition3D, GlobalPosition3D) override; Link::CCollection addExtend(const GeoData *, GlobalPosition3D, GlobalPosition3D) override; diff --git a/game/network/network.impl.h b/game/network/network.impl.h index 0a2f9ca..e339922 100644 --- a/game/network/network.impl.h +++ b/game/network/network.impl.h @@ -6,10 +6,10 @@ template<typename T, typename... Links> void -NetworkOf<T, Links...>::joinLinks(const Link::Ptr & l) const +NetworkOf<T, Links...>::joinLinks(const Link::Ptr & link) const { - for (const auto & ol : links.objects) { - Network::joinLinks(l, ol); + for (const auto & oldLink : links) { + Network::joinLinks(link, oldLink); } } @@ -18,11 +18,11 @@ Link::Ptr NetworkOf<T, Links...>::intersectRayLinks(const Ray<GlobalPosition3D> & ray) const { // Click link - if (const auto link = std::find_if(links.objects.begin(), links.objects.end(), + if (const auto link = std::find_if(links.begin(), links.end(), [&ray](const std::shared_ptr<T> & link) { return link->intersectRay(ray); }); - link != links.objects.end()) { + link != links.end()) { return *link; } return {}; @@ -32,11 +32,11 @@ template<typename T, typename... Links> float NetworkOf<T, Links...>::findNodeDirection(Node::AnyCPtr n) const { - for (const auto & l : links.objects) { - for (const auto & e : l->ends) { + for (const auto & link : links) { + for (const auto & end : link->ends) { // cppcheck-suppress useStlAlgorithm - if (e.node.get() == n.get()) { - return e.dir; + if (end.node.get() == n.get()) { + return end.dir; } } } @@ -45,16 +45,17 @@ NetworkOf<T, Links...>::findNodeDirection(Node::AnyCPtr n) const template<typename T, typename... Links> Link::CCollection -NetworkOf<T, Links...>::candidateStraight(GlobalPosition3D n1, GlobalPosition3D n2) +NetworkOf<T, Links...>::candidateStraight(GlobalPosition3D positionA, GlobalPosition3D positionB) { - return {candidateLink<typename T::StraightLink>(n1, n2)}; + return {candidateLink<typename T::StraightLink>(positionA, positionB)}; } template<typename T, typename... Links> Link::CCollection NetworkOf<T, Links...>::candidateJoins(GlobalPosition3D start, GlobalPosition3D end) { - if (::distance(start, end) < 2000.F) { + static constexpr auto MIN_DISTANCE = 2000.F; + if (::distance(start, end) < MIN_DISTANCE) { return {}; } const auto defs = genCurveDef( @@ -74,17 +75,17 @@ NetworkOf<T, Links...>::candidateExtend(GlobalPosition3D start, GlobalPosition3D template<typename T, typename... Links> Link::CCollection -NetworkOf<T, Links...>::addStraight(const GeoData * geoData, GlobalPosition3D n1, GlobalPosition3D n2) +NetworkOf<T, Links...>::addStraight(const GeoData * geoData, GlobalPosition3D positionA, GlobalPosition3D positionB) { Link::CCollection out; - geoData->walk(n1.xy(), n2, [geoData, &out, this, &n1](const GeoData::WalkStep & step) { + geoData->walk(positionA.xy(), positionB, [geoData, &out, this, &positionA](const GeoData::WalkStep & step) { if (step.previous.is_valid() && geoData->getSurface(step.current) != geoData->getSurface(step.previous)) { const auto surfaceEdgePosition = geoData->positionAt(GeoData::PointFace(step.exitPosition, step.current)); - out.emplace_back(addLink<typename T::StraightLink>(n1, surfaceEdgePosition)); - n1 = surfaceEdgePosition; + out.emplace_back(addLink<typename T::StraightLink>(positionA, surfaceEdgePosition)); + positionA = surfaceEdgePosition; } }); - out.emplace_back(addLink<typename T::StraightLink>(n1, n2)); + out.emplace_back(addLink<typename T::StraightLink>(positionA, positionB)); return out; } @@ -92,6 +93,7 @@ template<typename T, typename... Links> Link::CCollection NetworkOf<T, Links...>::addCurve(const GeoData * geoData, const GenCurveDef & curve) { + static constexpr auto MIN_DISTANCE = 2000.F; auto [cstart, cend, centre] = curve; Link::CCollection out; std::set<GeoData::WalkStepCurve, SortedBy<&GeoData::WalkStepCurve::angle>> breaks; @@ -115,7 +117,7 @@ NetworkOf<T, Links...>::addCurve(const GeoData * geoData, const GenCurveDef & cu || geoData->positionAt(GeoData::PointFace(step.exitPosition, step.current)).z; }); points.push_back(cend); - mergeClose(points, ::distance<3, GlobalDistance>, ::midpoint<3, GlobalDistance>, 2'000.F); + mergeClose(points, ::distance<3, GlobalDistance>, ::midpoint<3, GlobalDistance>, MIN_DISTANCE); std::ranges::transform(points | std::views::pairwise, std::back_inserter(out), [this, centre](const auto pair) { const auto [a, b] = pair; return this->addLink<typename T::CurveLink>(a, b, centre); @@ -127,7 +129,8 @@ template<typename T, typename... Links> Link::CCollection NetworkOf<T, Links...>::addJoins(const GeoData * geoData, GlobalPosition3D start, GlobalPosition3D end) { - if (::distance(start, end) < 2000.F) { + static constexpr auto MIN_DISTANCE = 2000.F; + if (::distance(start, end) < MIN_DISTANCE) { return {}; } const auto defs = genCurveDef(start, end, findNodeDirection(nodeAt(start)), findNodeDirection(nodeAt(end))); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 2a18b9a..c0e597d 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -1,6 +1,5 @@ #include "rail.h" #include "game/gamestate.h" -#include "game/geoData.h" #include "network.h" #include <game/network/network.impl.h> // IWYU pragma: keep #include <gfx/gl/sceneShader.h> @@ -40,34 +39,33 @@ RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) const auto flatStart {start.xy()}, flatEnd {end.xy()}; if (node2ins.second == NodeIs::InNetwork) { auto midheight = [&](auto mid) { - const auto sm = ::distance<2>(flatStart, mid); - const auto em = ::distance<2>(flatEnd, mid); - return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (sm / (sm + em))); + const auto startToMid = ::distance<2>(flatStart, mid); + const auto endToMid = ::distance<2>(flatEnd, mid); + return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (startToMid / (startToMid + endToMid))); }; const float dir2 = pi + findNodeDirection(node2ins.first); - if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) { - const auto radius {radii.first}; - const auto c1 = flatStart + (sincos(dir + half_pi) * radius); - const auto c2 = flatEnd + (sincos(dir2 + half_pi) * radius); - const auto mid = (c1 + c2) / 2; + const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); + if (radii.first < radii.second) { + const auto radius = radii.first; + const auto centre1 = flatStart + (sincos(dir + half_pi) * radius); + const auto centre2 = flatEnd + (sincos(dir2 + half_pi) * radius); + const auto mid = (centre1 + centre2) / 2; const auto midh = mid || midheight(mid); - addLink<RailLinkCurve>(start, midh, c1); - return addLink<RailLinkCurve>(end, midh, c2); - } - else { - const auto radius {radii.second}; - const auto c1 = flatStart + (sincos(dir - half_pi) * radius); - const auto c2 = flatEnd + (sincos(dir2 - half_pi) * radius); - const auto mid = (c1 + c2) / 2; - const auto midh = mid || midheight(mid); - addLink<RailLinkCurve>(midh, start, c1); - return addLink<RailLinkCurve>(midh, end, c2); + addLink<RailLinkCurve>(start, midh, centre1); + return addLink<RailLinkCurve>(end, midh, centre2); } + const auto radius = radii.second; + const auto centre1 = flatStart + (sincos(dir - half_pi) * radius); + const auto centre2 = flatEnd + (sincos(dir2 - half_pi) * radius); + const auto mid = (centre1 + centre2) / 2; + const auto midh = mid || midheight(mid); + addLink<RailLinkCurve>(midh, start, centre1); + return addLink<RailLinkCurve>(midh, end, centre2); } const auto diff = difference(end, start); - const auto vy {vector_yaw(diff)}; - const auto n2ed {(vy * 2) - dir - pi}; - const auto centre {find_arc_centre(flatStart, dir, flatEnd, n2ed)}; + const auto yaw = vector_yaw(diff); + const auto n2ed = (yaw * 2) - dir - pi; + const auto centre = find_arc_centre(flatStart, dir, flatEnd, n2ed); if (centre.second) { // right hand arc std::swap(start, end); @@ -102,31 +100,34 @@ namespace { } } -RailLinkStraight::RailLinkStraight(NetworkLinkHolder<RailLinkStraight> & instances, const Node::Ptr & a, - const Node::Ptr & b) : RailLinkStraight(instances, a, b, b->pos - a->pos) +RailLinkStraight::RailLinkStraight(NetworkLinkHolder<RailLinkStraight> & instances, const Node::Ptr & nodeA, + const Node::Ptr & nodeB) : RailLinkStraight(instances, nodeA, nodeB, nodeB->pos - nodeA->pos) { } -RailLinkStraight::RailLinkStraight( - NetworkLinkHolder<RailLinkStraight> & instances, Node::Ptr a, Node::Ptr b, const RelativePosition3D & diff) : - Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)), +RailLinkStraight::RailLinkStraight(NetworkLinkHolder<RailLinkStraight> & instances, Node::Ptr nodeA, Node::Ptr nodeB, + const RelativePosition3D & diff) : + Link({.node = std::move(nodeA), .dir = vector_yaw(diff)}, {.node = std::move(nodeB), .dir = vector_yaw(-diff)}, + glm::length(diff)), instance {instances.vertices.acquire( ends[0].node->pos, ends[1].node->pos, flat_orientation(diff), roundSleepers(length))} { } -RailLinkCurve::RailLinkCurve( - NetworkLinkHolder<RailLinkCurve> & instances, const Node::Ptr & a, const Node::Ptr & b, GlobalPosition2D c) : - RailLinkCurve(instances, a, b, c || a->pos.z, ::distance<2>(a->pos.xy(), c), {c, a->pos, b->pos}) +RailLinkCurve::RailLinkCurve(NetworkLinkHolder<RailLinkCurve> & instances, const Node::Ptr & nodeA, + const Node::Ptr & nodeB, GlobalPosition2D centre) : + RailLinkCurve(instances, nodeA, nodeB, centre || nodeA->pos.z, ::distance<2>(nodeA->pos.xy(), centre), + {centre, nodeA->pos, nodeB->pos}) { } -RailLinkCurve::RailLinkCurve(NetworkLinkHolder<RailLinkCurve> & instances, const Node::Ptr & a, const Node::Ptr & b, - GlobalPosition3D c, RelativeDistance radius, const Arc arc) : - Link({a, normalize(arc.first + half_pi)}, {b, normalize(arc.second - half_pi)}, - glm::length(RelativePosition2D {radius * arc.length(), difference(a->pos, b->pos).z})), - LinkCurve {c, radius, arc}, instance {instances.vertices.acquire(ends[0].node->pos, ends[1].node->pos, c, - roundSleepers(length), half_pi - arc.first, half_pi - arc.second, radius)} +RailLinkCurve::RailLinkCurve(NetworkLinkHolder<RailLinkCurve> & instances, const Node::Ptr & nodeA, + const Node::Ptr & nodeB, GlobalPosition3D centre, RelativeDistance radius, const Arc arc) : + Link({.node = nodeA, .dir = normalize(arc.first + half_pi)}, + {.node = nodeB, .dir = normalize(arc.second - half_pi)}, + glm::length(RelativePosition2D {radius * arc.length(), difference(nodeA->pos, nodeB->pos).z})), + LinkCurve {centre, radius, arc}, instance {instances.vertices.acquire(ends[0].node->pos, ends[1].node->pos, centre, + roundSleepers(length), half_pi - arc.first, half_pi - arc.second, radius)} { } @@ -155,11 +156,11 @@ template<> NetworkLinkHolder<RailLinkCurve>::NetworkLinkHolder() namespace { template<typename LinkType> void - renderType(const NetworkLinkHolder<LinkType> & n, auto & s) + renderType(const NetworkLinkHolder<LinkType> & networkLinks, auto & shader) { - if (auto count = n.vertices.size()) { - s.use(RAIL_CROSS_SECTION, RAIL_TEXTURE_POS); - glBindVertexArray(n.vao); + if (auto count = networkLinks.vertices.size()) { + shader.use(RAIL_CROSS_SECTION, RAIL_TEXTURE_POS); + glBindVertexArray(networkLinks.vao); glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(count)); } }; @@ -168,7 +169,7 @@ namespace { void RailLinks::render(const SceneShader & shader, const Frustum &) const { - if (!links.objects.empty()) { + if (!links.empty()) { texture->bind(); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(-1, 0); @@ -182,11 +183,12 @@ RailLinks::render(const SceneShader & shader, const Frustum &) const const Surface * RailLinks::getBaseSurface() const { - return std::dynamic_pointer_cast<Surface>(gameState->assets.at("terrain.surface.gravel")).get(); + return gameState->assets.at("terrain.surface.gravel").dynamicCast<const Surface>().get(); } RelativeDistance RailLinks::getBaseWidth() const { - return 5'700; + static constexpr auto BASE_WIDTH = 5'700; + return BASE_WIDTH; } diff --git a/game/orders.h b/game/orders.h index ca5cfdb..840aa3c 100644 --- a/game/orders.h +++ b/game/orders.h @@ -5,7 +5,7 @@ class Objective; -class Orders : public Collection<Objective> { +class Orders : public SharedCollection<Objective> { public: [[nodiscard]] Objective * current() const; Objective * next(); diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 159a078..140c4e5 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -2,6 +2,16 @@ #include "gfx/gl/sceneShader.h" #include "gfx/gl/shadowMapper.h" #include "gfx/gl/vertexArrayObject.h" +#include <location.h> + +static_assert(std::is_constructible_v<Foliage>); + +std::any +Foliage::createAt(const Location & position) const +{ + return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>( + instances.acquire(position.getRotationTransform(), position.rot.y, position.pos)); +} bool Foliage::persist(Persistence::PersistenceStore & store) diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index 71bc734..d15a8b0 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -17,6 +17,8 @@ class Foliage : public Asset, public Renderable, public StdTypeDefs<Foliage> { glVertexArray instancePointVAO; public: + [[nodiscard]] std::any createAt(const Location &) const override; + struct LocationVertex { glm::mat3 rotation; float yaw; diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index f1a02b2..d8e4c4e 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -2,6 +2,16 @@ #include "gfx/gl/sceneShader.h" #include "gfx/gl/vertexArrayObject.h" #include "gfx/models/texture.h" // IWYU pragma: keep +#include <location.h> + +static_assert(std::is_constructible_v<Illuminator>); + +std::any +Illuminator::createAt(const Location & position) const +{ + return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>( + instances.acquire(position.getRotationTransform(), position.pos)); +} bool Illuminator::SpotLight::persist(Persistence::PersistenceStore & store) diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index 47ce337..200ba40 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -15,6 +15,8 @@ class Illuminator : public Asset, public Renderable, public StdTypeDefs<Illumina std::optional<glVertexArray> instancesSpotLightVAO, instancesPointLightVAO; public: + [[nodiscard]] std::any createAt(const Location &) const override; + struct LightCommonVertex { RelativePosition3D position; RGB colour; diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 59d1e83..4a0a22d 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -72,7 +72,12 @@ RailVehicle::intersectRay(const Ray<GlobalPosition3D> & ray, BaryPosition & bary }}; return std::any_of( triangles.begin(), triangles.end(), [&cornerVertices, &ray, &baryPos, &distance](const auto & idx) { - return ray.intersectTriangle( - cornerVertices[idx[0]], cornerVertices[idx[1]], cornerVertices[idx[2]], baryPos, distance); + if (const auto inter = ray.intersectTriangle( + cornerVertices[idx[0]], cornerVertices[idx[1]], cornerVertices[idx[2]])) { + baryPos = inter->bary; + distance = inter->distance; + return true; + }; + return false; }); } diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 162a29a..21f01c8 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -17,6 +17,19 @@ RailVehicleClass::persist(Persistence::PersistenceStore & store) && STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store); } +std::any +RailVehicleClass::createAt(const Location & position) const +{ + return std::make_shared<InstanceVertices<LocationVertex>::InstanceProxy>(instances.acquire(LocationVertex { + .body = position.getRotationTransform(), + .front = position.getRotationTransform(), + .back = position.getRotationTransform(), + .bodyPos = position.pos, + .frontPos = {sincos(position.rot.x) * wheelBase * 0.5F, position.pos.z}, + .backPos = {sincos(position.rot.x) * wheelBase * -0.5F, position.pos.z}, + })); +} + void RailVehicleClass::postLoad() { diff --git a/game/vehicles/railVehicleClass.h b/game/vehicles/railVehicleClass.h index 6eb4ca5..ccff3e2 100644 --- a/game/vehicles/railVehicleClass.h +++ b/game/vehicles/railVehicleClass.h @@ -17,6 +17,8 @@ public: void render(const SceneShader & shader, const Frustum &) const override; void shadows(const ShadowMapper & shadowMapper, const Frustum &) const override; + [[nodiscard]] std::any createAt(const Location &) const override; + struct LocationVertex { glm::mat3 body, front, back; GlobalPosition3D bodyPos, frontPos, backPos; diff --git a/game/vehicles/train.h b/game/vehicles/train.h index 4933347..88e30f9 100644 --- a/game/vehicles/train.h +++ b/game/vehicles/train.h @@ -15,7 +15,7 @@ class SceneShader; class ShadowMapper; template<typename> class Ray; -class Train : public Vehicle, public Collection<RailVehicle, false>, public Can<Go>, public Can<Idle> { +class Train : public Vehicle, public UniqueCollection<RailVehicle>, public Can<Go>, public Can<Idle> { public: explicit Train(const Link::CPtr & link, float linkDist = 0) : Vehicle {link, linkDist} { } diff --git a/game/water.h b/game/water.h index f9fe080..07d9ae1 100644 --- a/game/water.h +++ b/game/water.h @@ -29,6 +29,6 @@ private: void generateMeshes(); std::shared_ptr<GeoData> geoData; - Collection<MeshT<Vertex>, false> meshes; + UniqueCollection<MeshT<Vertex>> meshes; Texture::Ptr water; }; |