载入中...
搜索中...
未找到
GpuAgentSimulation.cpp
浏览该文件的文档.
2
7
8#include "common/Assert.h"
9#include "common/Diagnostic.h"
10
11#include <utility>
12
13namespace eve::gpuagents {
14
15Result<void> GpuAgentSimulation::makeDefaultSolver() {
16 switch (profile_.kind) {
18 solver_ = std::make_unique<FishSolver>();
19 break;
21 solver_ = std::make_unique<LifeNetworkSolver>();
22 break;
24 solver_ = std::make_unique<BirdSolver>();
25 break;
27 solver_ = std::make_unique<PetalSolver>();
28 break;
29 default:
31 "kind", {}, "gpuagents"));
32 }
33 return Result<void>::success();
34}
35
37 if (profile.base().maxAgents <= 0) {
39 "maxAgents", {}, "gpuagents"));
40 }
41 if (profile.base().fixedDt <= 0.f) {
43 "fixedDt", {}, "gpuagents"));
44 }
45 profile_ = profile;
46 capacity_ = profile.base().maxAgents;
47 buffers_[0].assign(static_cast<size_t>(capacity_), AgentState{});
48 buffers_[1].assign(static_cast<size_t>(capacity_), AgentState{});
49 front_ = 0;
50 accumulator_ = 0.f;
51 simTime_ = 0.f;
52 stepCount_ = 0;
53 configured_ = true;
54 auto solverRes = makeDefaultSolver();
55 if (!solverRes.ok()) {
56 configured_ = false;
57 return solverRes;
58 }
59 return Result<void>::success();
60}
61
62Result<void> GpuAgentSimulation::setSolver(std::unique_ptr<IAgentSolver> solver) {
63 if (!solver) {
65 Diagnostic::error(DiagnosticCode::InvalidArgument, "solver is null", "solver", {}, "gpuagents"));
66 }
67 if (configured_ && solver->kind() != profile_.kind) {
69 Diagnostic::error(DiagnosticCode::InvalidArgument, "solver kind mismatch", "solver", {}, "gpuagents"));
70 }
71 solver_ = std::move(solver);
72 return Result<void>::success();
73}
74
75Result<void> GpuAgentSimulation::initialize(int count, const std::function<AgentState(int)>& spawnFn) {
76 if (!configured_) {
78 "simulation not configured", "configure", {}, "gpuagents"));
79 }
80 if (!spawnFn) {
82 Diagnostic::error(DiagnosticCode::InvalidArgument, "spawnFn is empty", "spawnFn", {}, "gpuagents"));
83 }
84 if (count < 0 || count > capacity_) {
86 "initialize count out of range", "count", {}, "gpuagents"));
87 }
88 auto& buf = buffers_[front_];
89 for (int i = 0; i < capacity_; ++i) {
90 if (i < count) {
91 AgentState s = spawnFn(i);
92 s.alive = 1;
93 buf[static_cast<size_t>(i)] = s;
94 } else {
95 buf[static_cast<size_t>(i)] = AgentState{};
96 }
97 }
98 buffers_[1 - front_] = buf;
99 accumulator_ = 0.f;
100 return Result<void>::success();
101}
102
104 if (!configured_) return;
105 for (auto& b : buffers_) {
106 for (auto& s : b) s = AgentState{};
107 }
108 accumulator_ = 0.f;
109 simTime_ = 0.f;
110 stepCount_ = 0;
111}
112
114 int n = 0;
115 for (const auto& s : buffers_[front_]) {
116 if (s.alive) ++n;
117 }
118 return n;
119}
120
122 if (!configured_ || !solver_) {
124 "simulation not configured", "configure", {}, "gpuagents"));
125 }
126 EV_PARAM_CHECK(dt >= 0.f);
127 accumulator_ += dt;
128 const float fixed = profile_.base().fixedDt;
129 int guard = 0;
130 while (accumulator_ + 1e-8f >= fixed && guard < 8) {
131 const int read = front_;
132 const int write = 1 - front_;
133 solver_->step(buffers_[read], buffers_[write], profile_, env, fixed);
134 front_ = write;
135 accumulator_ -= fixed;
136 simTime_ += fixed;
137 ++stepCount_;
138 ++guard;
139 }
140 return Result<void>::success();
141}
142
143} // namespace eve::gpuagents
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
const std::string & s
Stable, structured diagnostics shared by engine modules.
glm::vec3 n
Definition Grass.cpp:63
MeleePoint3 b
Definition MeleeHit.cpp:41
std::uint32_t count
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
const EffectProfile & profile() const
Bound profile.
Result< void > setSolver(std::unique_ptr< IAgentSolver > solver)
Replace the solver instance (tests / custom kernels). @ownership Simulation takes ownership.
Result< void > initialize(int count, const std::function< AgentState(int)> &spawnFn)
Spawn count alive agents using spawnFn(index) -> AgentState.
void reset()
Clear all agents and the time accumulator.
int aliveCount() const
Number of alive agents.
Result< void > configure(const EffectProfile &profile)
Configure capacity and bind a solver for the profile kind.
Result< void > step(float dt, const EnvironmentSnapshot &env)
Advance wall-clock dt via fixed substeps.
Standardized per-agent simulation output shared by all effect kinds.
Definition AgentState.h:12
Type-erased profile bundle owned by an EffectBackend.
const EffectProfileBase & base() const
Returns the shared base slice for the active kind.
Read-only environment semantics prepared by GpuAgentWorld each tick.