载入中...
搜索中...
未找到
CrowdRuntime.cpp
浏览该文件的文档.
2
3#include "crowd/Crowd.h"
4
5namespace eve::crowd_editing {
6
7Result<void> CrowdRuntimeApplier::apply(const CrowdDocumentTarget& document,
8 crowd::Crowd* runtime) const {
9 if (!runtime)
10 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.crowd.runtime"),
11 "Live Crowd runtime is required");
12 const auto diagnostics = document.validate();
13 for (const auto& diagnostic : diagnostics)
14 if (diagnostic.severity() == DiagnosticSeverity::Error)
15 return Result<void>::failure(eve::Status(EditorStatus::Rejected, diagnostics));
16 const auto agents = document.agents();
17 if (agents.size() > static_cast<std::size_t>(runtime->getMaxAgents()))
18 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.crowd.runtime-capacity"),
19 "Crowd document exceeds the runtime agent capacity");
20 std::map<StableId, CrowdPathRecord> paths;
21 for (const auto& path : document.paths()) paths.emplace(path.id, path);
22 runtime->clearAgents();
23 for (const auto& agent : agents) {
24 const int index = runtime->addNamedAgent(agent.id.value(), static_cast<float>(agent.x),
25 static_cast<float>(agent.y), static_cast<float>(agent.heading), static_cast<float>(agent.radius));
26 if (index < 0) {
27 runtime->clearAgents();
28 return eve::editing::failed<void>(EditorStatus::Failed, RuleId("editor.crowd.runtime-capacity"),
29 "Crowd runtime rejected an agent or reached capacity");
30 }
31 runtime->setAgentSpeed(index, static_cast<float>(agent.maximumSpeed));
32 std::string action = agent.behavior;
33 if (action == "path") {
34 const auto found = paths.find(agent.path);
35 if (found == paths.end() || found->second.points.empty()) {
36 runtime->clearAgents();
37 return eve::editing::failed<void>(EditorStatus::Conflict, RuleId("editor.crowd.runtime-path"),
38 "Path agent has no runtime waypoint");
39 }
40 action = "seek";
41 runtime->setAgentTarget(index, static_cast<float>(found->second.points.front().x),
42 static_cast<float>(found->second.points.front().y));
43 }
44 if (!runtime->setAgentAction(index, action)) {
45 runtime->clearAgents();
46 return eve::editing::failed<void>(EditorStatus::Failed, RuleId("editor.crowd.runtime-action"),
47 "Crowd runtime rejected an agent behavior");
48 }
49 }
50 return eve::editing::applied<void>();
51}
52
53} // namespace eve::crowd_editing
std::string action
Definition PlayHost.cpp:117
std::string path
Definition PlayHost.cpp:110
std::string id
Definition PlayHost.cpp:108
bool found
std::map< std::string, std::string > paths
uint32_t index
editing::RuleId RuleId
editing::Status EditorStatus
editing::DiagnosticSeverity DiagnosticSeverity
eve::StatusCode Status
eve::Result< T > Result
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16