nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
rig.cc
Go to the documentation of this file.
1#include "content/rig.h"
2
3#include <set>
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include "absl/strings/str_cat.h"
9#include "content/text.h"
10
11namespace nanommo {
12
13namespace {
14
15// A joint on the left has Left in its name where its twin has Right.
16std::string ToTheRight(std::string name) {
17 if (name.starts_with("Left")) {
18 name.replace(0, 4, "Right");
19 }
20 return name;
21}
22
23absl::Status CheckGait(const rig::Gait& gait, std::string_view name) {
24 if (gait.speed() <= 0.0f || gait.cycle_ms() == 0) {
25 return absl::InvalidArgumentError(
26 absl::StrCat("the ", name, " needs a speed and a cycle"));
27 }
28 if (gait.stance() <= 0.0f || gait.stance() >= 1.0f) {
29 return absl::InvalidArgumentError(absl::StrCat(
30 "the ", name, "'s feet are down for none of its cycle, or all of it"));
31 }
32 return absl::OkStatus();
33}
34
35absl::Status CheckMove(const skills::Move& move, std::string_view name) {
36 uint32_t last = 0;
37 for (const skills::Key& key : move.keys()) {
38 if (key.at_ms() < last) {
39 return absl::InvalidArgumentError(
40 absl::StrCat("the ", name, " goes back in time"));
41 }
42 last = key.at_ms();
43 }
44 return absl::OkStatus();
45}
46
47} // namespace
48
49absl::StatusOr<rig::Rig> ParseRig(std::string_view text) {
50 rig::Rig file;
51 const absl::Status parsed = ParseText(text, file);
52 if (!parsed.ok()) {
53 return parsed;
54 }
55 // The joints so far, by name, with the twins on the right.
56 std::set<std::string> joints;
57 for (const rig::Joint& joint : file.joints()) {
58 if (joint.name().empty()) {
59 return absl::InvalidArgumentError("a joint has no name");
60 }
61 if (joint.parent().empty() ? !joints.empty()
62 : !joints.contains(joint.parent())) {
63 return absl::InvalidArgumentError(absl::StrCat(
64 "the ", joint.name(), " hangs from a joint that isn't before it"));
65 }
66 std::vector<std::string> names = {joint.name()};
67 if (joint.mirrored()) {
68 names.push_back(ToTheRight(joint.name()));
69 }
70 for (const std::string& name : names) {
71 if (!joints.insert(name).second) {
72 return absl::InvalidArgumentError(
73 absl::StrCat("there are two joints called ", name));
74 }
75 }
76 }
77 for (const rig::Build& build : file.builds()) {
78 for (const rig::Shift& shift : build.shifts()) {
79 if (!joints.contains(shift.joint())) {
80 return absl::InvalidArgumentError(absl::StrCat(
81 "a build moves a joint ", shift.joint(), " the rig doesn't have"));
82 }
83 }
84 }
85 absl::Status checked = CheckGait(file.walk(), "walk");
86 if (checked.ok()) {
87 checked = CheckGait(file.run(), "run");
88 }
89 if (checked.ok()) {
90 checked = CheckMove(file.idle(), "idle");
91 }
92 if (checked.ok()) {
93 checked = CheckMove(file.death(), "death");
94 }
95 if (!checked.ok()) {
96 return checked;
97 }
98 return file;
99}
100
101std::vector<rig::Joint> Skeleton(const rig::Rig& rig, int build) {
102 std::vector<rig::Joint> joints;
103 for (const rig::Joint& joint : rig.joints()) {
104 joints.push_back(joint);
105 if (!joint.mirrored()) {
106 continue;
107 }
108 rig::Joint right = joint;
109 right.set_name(ToTheRight(joint.name()));
110 right.set_parent(ToTheRight(joint.parent()));
111 right.mutable_at()->set_x(-joint.at().x());
112 joints.push_back(right);
113 }
114 if (build < 0 || build >= rig.builds_size()) {
115 return joints;
116 }
117 for (const rig::Shift& shift : rig.builds(build).shifts()) {
118 // The joint, and the joints that hang from it, which come after it, as a
119 // joint comes after its parent. Its twin moves the other way across.
120 const std::string twin = ToTheRight(shift.joint());
121 for (const bool right : {false, true}) {
122 if (right && twin == shift.joint()) {
123 continue;
124 }
125 std::set<std::string> moved = {right ? twin : shift.joint()};
126 for (rig::Joint& joint : joints) {
127 if (!moved.contains(joint.name()) && !moved.contains(joint.parent())) {
128 continue;
129 }
130 moved.insert(joint.name());
131 figures::Offset& at = *joint.mutable_at();
132 at.set_x(at.x() + (right ? -shift.by().x() : shift.by().x()));
133 at.set_y(at.y() + shift.by().y());
134 at.set_z(at.z() + shift.by().z());
135 }
136 }
137 }
138 return joints;
139}
140
141} // namespace nanommo
Definition chat.cc:9
absl::StatusOr< rig::Rig > ParseRig(std::string_view text)
Definition rig.cc:49
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
std::vector< rig::Joint > Skeleton(const rig::Rig &rig, int build)
Definition rig.cc:101