nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
skills.cc
Go to the documentation of this file.
1#include "content/skills.h"
2
3#include <cstdint>
4#include <map>
5#include <set>
6#include <string_view>
7#include <utility>
8
9#include "absl/strings/str_cat.h"
10#include "content/checks.h"
11#include "content/text.h"
12
13namespace nanommo {
14
15absl::StatusOr<SkillTable> ParseSkills(std::string_view text) {
16 skills::Skills file;
17 const absl::Status parsed = ParseText(text, file);
18 if (!parsed.ok()) {
19 return parsed;
20 }
21 absl::StatusOr<std::map<uint32_t, skills::Skill>> by_id = ById(file.skills());
22 if (!by_id.ok()) {
23 return by_id.status();
24 }
25 for (const auto& [id, skill] : *by_id) {
26 // Zero is what protobuf gives a level we leave out, so a skill whose
27 // level we forgot would be everyone's, with no error.
28 if (skill.level() == 0) {
29 return absl::InvalidArgumentError(
30 absl::StrCat("the ", skill.name(), " has no level"));
31 }
32 if (skill.effect().hit_ms() > skill.effect().cast_ms()) {
33 return absl::InvalidArgumentError(
34 absl::StrCat("the ", skill.name(), " lands after it's over"));
35 }
36 uint32_t blow = 0;
37 for (const skills::Blow& each : skill.effect().blows()) {
38 if (each.at_ms() < blow) {
39 return absl::InvalidArgumentError(
40 absl::StrCat("the ", skill.name(), "'s blows go back in time"));
41 }
42 if (each.at_ms() > skill.effect().cast_ms()) {
43 return absl::InvalidArgumentError(absl::StrCat(
44 "the ", skill.name(), " lands a blow after it's over"));
45 }
46 blow = each.at_ms();
47 }
48 uint32_t stride = 0;
49 for (const skills::Stride& each : skill.effect().strides()) {
50 if (each.at_ms() < stride) {
51 return absl::InvalidArgumentError(
52 absl::StrCat("the ", skill.name(), "'s strides go back in time"));
53 }
54 // A swing's as long as the weapon's, so only the others are checked.
55 if (!skill.effect().weapon() && each.at_ms() > skill.effect().cast_ms()) {
56 return absl::InvalidArgumentError(absl::StrCat(
57 "the ", skill.name(), " takes a stride after it's over"));
58 }
59 stride = each.at_ms();
60 }
61 // A swing's as long as the weapon's, so only the others are checked.
62 uint32_t last = 0;
63 for (const skills::Key& key : skill.look().move().keys()) {
64 if (key.at_ms() < last) {
65 return absl::InvalidArgumentError(
66 absl::StrCat("the ", skill.name(), "'s move goes back in time"));
67 }
68 last = key.at_ms();
69 }
70 if (!skill.effect().weapon() && last > skill.effect().cast_ms()) {
71 return absl::InvalidArgumentError(absl::StrCat(
72 "the ", skill.name(), "'s move runs on after it's over"));
73 }
74 }
75 std::set<uint32_t> starting(file.starting().begin(), file.starting().end());
76 for (const uint32_t id : starting) {
77 if (!by_id->contains(id)) {
78 return absl::InvalidArgumentError(
79 absl::StrCat("there's no skill ", id, " to start with"));
80 }
81 }
82 for (const auto& [id, skill] : *by_id) {
83 if (skill.next() != 0 && !by_id->contains(skill.next())) {
84 return absl::InvalidArgumentError(absl::StrCat(
85 "the ", skill.name(), " goes on into a skill that isn't there"));
86 }
87 }
88 // Everyone has the swing, and the swings it goes on into, from the start.
89 if (file.attack() != 0) {
90 if (!by_id->contains(file.attack())) {
91 return absl::InvalidArgumentError(
92 absl::StrCat("there's no skill ", file.attack(), " to swing with"));
93 }
94 for (uint32_t id = file.attack(); id != 0 && !starting.contains(id);
95 id = by_id->at(id).next()) {
96 starting.insert(id);
97 }
98 }
99 return SkillTable{.by_id = *std::move(by_id),
100 .starting = std::move(starting),
101 .attack = file.attack()};
102}
103
104float Eased(skills::Ease ease, float t) {
105 switch (ease) {
106 case skills::SNAP:
107 return 1.0f - (1.0f - t) * (1.0f - t) * (1.0f - t);
108 case skills::GATHER:
109 return t * t * t;
110 default:
111 return t * t * (3.0f - 2.0f * t);
112 }
113}
114
115float AheadAt(const skills::Effect& effect, float ms) {
116 float at = 0.0f;
117 float ahead = 0.0f;
118 for (const skills::Stride& stride : effect.strides()) {
119 if (ms < static_cast<float>(stride.at_ms())) {
120 const float span = static_cast<float>(stride.at_ms()) - at;
121 const float t = span > 0.0f ? (ms - at) / span : 1.0f;
122 return ahead + (stride.ahead() - ahead) * Eased(stride.ease(), t);
123 }
124 at = static_cast<float>(stride.at_ms());
125 ahead = stride.ahead();
126 }
127 return ahead;
128}
129
130} // namespace nanommo
Definition chat.cc:9
absl::StatusOr< std::map< uint32_t, Entry > > ById(const google::protobuf::RepeatedPtrField< Entry > &entries)
Definition checks.h:19
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
float AheadAt(const skills::Effect &effect, float ms)
Definition skills.cc:115
float Eased(skills::Ease ease, float t)
Definition skills.cc:104
absl::StatusOr< SkillTable > ParseSkills(std::string_view text)
Definition skills.cc:15