nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
figures.cc
Go to the documentation of this file.
1#include "content/figures.h"
2
3#include <string>
4#include <string_view>
5#include <vector>
6
7#include "absl/strings/str_cat.h"
8#include "content/text.h"
9
10namespace nanommo {
11
12namespace {
13
14// A bone on the left has Left in its name where its twin has Right.
15std::string ToTheRight(std::string bone) {
16 if (bone.starts_with("Left")) {
17 bone.replace(0, 4, "Right");
18 }
19 return bone;
20}
21
22} // namespace
23
24absl::Status CheckPart(const figures::Part& part) {
25 if (part.bone().empty()) {
26 return absl::InvalidArgumentError("a part has no bone to move with");
27 }
28 const std::string& bone = part.bone();
29 switch (part.shape()) {
30 case figures::Part::SHAPE_LIMB:
31 if (part.to().empty() || part.start_radius() <= 0.0f ||
32 part.end_radius() <= 0.0f) {
33 return absl::InvalidArgumentError(absl::StrCat(
34 "a limb on the ", bone, " needs an end, and a radius at each"));
35 }
36 return absl::OkStatus();
37 case figures::Part::SHAPE_BALL:
38 if (part.size().x() <= 0.0f || part.size().y() <= 0.0f ||
39 part.size().z() <= 0.0f) {
40 return absl::InvalidArgumentError(
41 absl::StrCat("a ball on the ", bone, " needs a size each way"));
42 }
43 return absl::OkStatus();
44 default:
45 return absl::InvalidArgumentError(
46 absl::StrCat("a part on the ", bone, " has no shape"));
47 }
48}
49
50absl::StatusOr<figures::Figures> ParseFigures(std::string_view text) {
51 figures::Figures file;
52 const absl::Status parsed = ParseText(text, file);
53 if (!parsed.ok()) {
54 return parsed;
55 }
56 for (const figures::Part& part : file.body()) {
57 const absl::Status checked = CheckPart(part);
58 if (!checked.ok()) {
59 return checked;
60 }
61 }
62 for (const figures::Hair& hair : file.hairs()) {
63 for (const figures::Part& part : hair.parts()) {
64 const absl::Status checked = CheckPart(part);
65 if (!checked.ok()) {
66 return absl::InvalidArgumentError(
67 absl::StrCat("a way to wear the hair: ", checked.message()));
68 }
69 }
70 }
71 return file;
72}
73
74Features FeaturesOf(const figures::Figures& figures, uint32_t id) {
75 // The id times a large odd number scatters its bits, so that players who
76 // join one after another don't look alike, and each feature picks with
77 // the bits from a byte further up.
78 const uint32_t mixed = id * 2654435761u;
79 auto pick = [&](int count, int byte) {
80 return static_cast<int>((mixed >> (8 * byte)) % count);
81 };
82 Features features;
83 if (!figures.skins().empty()) {
84 features.skin = figures.skins(pick(figures.skins_size(), 1));
85 }
86 if (!figures.hair_colors().empty()) {
87 features.hair_color =
88 figures.hair_colors(pick(figures.hair_colors_size(), 2));
89 }
90 if (!figures.hairs().empty()) {
91 features.hair = pick(figures.hairs_size(), 3);
92 }
93 return features;
94}
95
96std::vector<figures::Part> Unfold(
97 const google::protobuf::RepeatedPtrField<figures::Part>& parts) {
98 std::vector<figures::Part> unfolded;
99 for (const figures::Part& part : parts) {
100 unfolded.push_back(part);
101 if (!part.mirrored()) {
102 continue;
103 }
104 figures::Part right = part;
105 right.set_bone(ToTheRight(part.bone()));
106 right.set_from(ToTheRight(part.from()));
107 right.set_to(ToTheRight(part.to()));
108 right.mutable_from_offset()->set_x(-part.from_offset().x());
109 right.mutable_to_offset()->set_x(-part.to_offset().x());
110 unfolded.push_back(right);
111 }
112 return unfolded;
113}
114
115} // namespace nanommo
Definition chat.cc:9
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
absl::Status CheckPart(const figures::Part &part)
Definition figures.cc:24
absl::StatusOr< figures::Figures > ParseFigures(std::string_view text)
Definition figures.cc:50
std::vector< figures::Part > Unfold(const google::protobuf::RepeatedPtrField< figures::Part > &parts)
Definition figures.cc:96
Features FeaturesOf(const figures::Figures &figures, uint32_t id)
Definition figures.cc:74
uint32_t hair_color
Definition figures.h:31
uint32_t skin
As 0xRRGGBBAA: white, when there are none to pick from.
Definition figures.h:30