载入中...
搜索中...
未找到
CrowdField.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5// 连续流场:Dijkstra 积分场 + 梯度方向场 + 双线性采样。
6// 与 map/FlowField 的区别:map 的流场是格级 next 指针,本类是连续方向向量,
7// 供单位在格子内平滑移动、平滑转向。纯数学,零模块依赖。
8
9#include <cstdint>
10#include <utility>
11#include <vector>
12
13namespace eve::crowd {
16
26public:
28 static constexpr float kUnreachable = 3.4e38f;
29
31 CrowdField() = default;
32
47 void resize(int width, int height, float cellSize, float originX, float originY);
48
50 void clear();
51
53 bool valid() const { return width_ > 0 && height_ > 0 && cellSize_ > 0.f; }
54
56 int getWidth() const { return width_; }
58 int getHeight() const { return height_; }
60 float getCellSize() const { return cellSize_; }
62 float getOriginX() const { return originX_; }
64 float getOriginY() const { return originY_; }
65
72 void setBlocked(int cx, int cy, bool blocked);
73
75 bool isBlocked(int cx, int cy) const;
76
81 void setCellCost(int cx, int cy, float cost);
82
84 float getCellCost(int cx, int cy) const;
85
87 void setAllCellCost(float cost);
88
94 void setGoal(int gx, int gy);
95
97 void addGoal(int gx, int gy);
99 void clearGoals();
101 int goalCount() const { return int(goals_.size() / 2); }
102
104 bool isBuilt() const { return built_ && builtRevision_ == revision_; }
106 [[nodiscard]] std::uint64_t revision() const noexcept { return revision_; }
108 [[nodiscard]] std::uint64_t inputRevision() const noexcept { return inputRevision_; }
110 [[nodiscard]] std::uint64_t builtRevision() const noexcept { return builtRevision_; }
112 [[nodiscard]] FieldState state() const noexcept {
113 if (!valid()) return FieldState::Unconfigured;
114 if (!built_) return FieldState::Unbuilt;
115 return isBuilt() ? FieldState::Ready : FieldState::Stale;
116 }
117
123 void build();
124
126 float costAtCell(int cx, int cy) const;
127
129 bool isReachable(int cx, int cy) const;
130
138 void flowAtCell(int cx, int cy, float &dx, float &dy) const;
139
146 float costAtWorld(float wx, float wy) const;
147
155 void flowAtWorld(float wx, float wy, float &dx, float &dy) const;
156
168 bool resolvePenetration(float &wx, float &wy, float radius) const;
169
171 const std::vector<float> &integration() const { return integ_; }
173 const std::vector<float> &flowX() const { return flowX_; }
175 const std::vector<float> &flowY() const { return flowY_; }
176
177private:
178 friend class Crowd;
179 void beginBuildWorking();
180 std::pair<int, int> advanceBuildWorking(int maxWork);
181 void invalidate();
182 void invalidateInputs();
183 void buildWorking();
184 bool inBounds(int cx, int cy) const { return cx >= 0 && cy >= 0 && cx < width_ && cy < height_; }
185 int index(int cx, int cy) const { return cy * width_ + cx; }
186 void worldToCell(float wx, float wy, float &fx, float &fy) const;
187
188 void rebuildFlowCell(int index);
189
190 int width_ = 0;
191 int height_ = 0;
192 float cellSize_ = 0.f;
193 float originX_ = 0.f;
194 float originY_ = 0.f;
195 std::vector<float> cost_; // 地形代价(0=阻挡,>=1 可走)
196 std::vector<float> navigationCost_;
197 bool navigationDiagonal_ = true, destinationEntryCost_ = false;
198 bool navigationBlocked(int cx, int cy) const;
199 float navigationCost(int index) const;
200 std::vector<float> integ_; // Dijkstra 积分场
201 std::vector<float> flowX_; // 每格方向 X
202 std::vector<float> flowY_; // 每格方向 Y
203 std::vector<int> goals_; // 目标格 (x,y) 对
204 bool built_ = false;
205 std::uint64_t revision_ = 0, builtRevision_ = 0, inputRevision_ = 0;
206 std::vector<std::pair<float, int>> buildHeap_;
207 int flowCursor_ = 0;
208};
209
210} // namespace eve::crowd
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
eve::resource::CostSpec cost
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::uint32_t height
std::uint32_t width
bool valid
double radius
Definition MeleeHit.cpp:42
glm::dvec3 dx
std::string build
float dy
Definition Shadow.cpp:151
uint32_t index
连续流场(flow field)。
Definition CrowdField.h:25
const std::vector< float > & flowX() const
只读访问方向场 X 分量(调试渲染用)。
Definition CrowdField.h:173
float getOriginX() const
Returns the origin x.
Definition CrowdField.h:62
int getWidth() const
网格尺寸访问器。
Definition CrowdField.h:56
FieldState state() const noexcept
Distinguish absent, unbuilt, current and stale navigation.
Definition CrowdField.h:112
std::uint64_t revision() const noexcept
Read owner-local terrain/goal revision; snapshots preserve it.
Definition CrowdField.h:106
float getOriginY() const
Returns the origin y.
Definition CrowdField.h:64
const std::vector< float > & integration() const
只读访问积分场(调试渲染用)。
Definition CrowdField.h:171
int goalCount() const
目标个数。
Definition CrowdField.h:101
bool isBuilt() const
Whether directions were built for the current terrain and goals.
Definition CrowdField.h:104
int getHeight() const
Returns the height.
Definition CrowdField.h:58
std::uint64_t inputRevision() const noexcept
Navigation geometry/cost/rule epoch; goal edits and field publications do not change it.
Definition CrowdField.h:108
std::uint64_t builtRevision() const noexcept
Read revision consumed by the last completed build.
Definition CrowdField.h:110
CrowdField()=default
Crowd field.
float getCellSize() const
Returns the cell size.
Definition CrowdField.h:60
const std::vector< float > & flowY() const
只读访问方向场 Y 分量(调试渲染用)。
Definition CrowdField.h:175
bool valid() const
是否配置了有效网格。
Definition CrowdField.h:53
群体行为模块:连续流场寻路 + 海量单位移动/转向/行动 + Boids 鸟群。
Definition Crowd.h:266
FieldState
Navigation readiness relative to the current terrain and goals.
Definition CrowdField.h:15