nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
monsters.cc
Go to the documentation of this file.
1#include "content/monsters.h"
2
3#include <algorithm>
4#include <chrono>
5#include <cmath>
6#include <cstdint>
7#include <map>
8#include <numbers>
9#include <string>
10#include <string_view>
11#include <utility>
12
13#include "absl/strings/str_cat.h"
14#include "content/checks.h"
15#include "content/text.h"
16
17namespace nanommo {
18
19namespace {
20
21// Whether players can see the skill coming and step out of it: it has a
22// shape, the player it's fighting is in it as it starts, it shows for long
23// enough, and it can't start again before it's landed.
24absl::Status CheckSkill(const monsters::Kind& kind,
25 const monsters::Skill& skill) {
26 const std::string name =
27 absl::StrCat("the ", kind.name(), "'s ", skill.name());
28 if (skill.size() <= 0.0f) {
29 return absl::InvalidArgumentError(absl::StrCat(name, " has no size"));
30 }
31 if (skill.shape() == monsters::CONE && skill.arc() <= 0.0f) {
32 return absl::InvalidArgumentError(
33 absl::StrCat(name, " is a cone without an arc"));
34 }
35 if (skill.range() <= 0.0f) {
36 return absl::InvalidArgumentError(absl::StrCat(name, " has no range"));
37 }
38 if (skill.range() > skill.size()) {
39 return absl::InvalidArgumentError(
40 absl::StrCat(name, " starts from ", skill.range(),
41 " metres, outside its ", skill.size()));
42 }
43 if (std::chrono::milliseconds(skill.telegraph_ms()) < kShortestTelegraph) {
44 return absl::InvalidArgumentError(
45 absl::StrCat(name, " shows for ", skill.telegraph_ms(),
46 " ms, too short to step out of: it needs ",
47 kShortestTelegraph.count(), " at least"));
48 }
49 if (skill.cooldown_ms() < skill.telegraph_ms()) {
50 return absl::InvalidArgumentError(
51 absl::StrCat(name, " cools down in ", skill.cooldown_ms(),
52 " ms, before it lands, at ", skill.telegraph_ms()));
53 }
54 return absl::OkStatus();
55}
56
57// Whether a boss's phases go down its health, from the first, at full health,
58// each lower than the last, while it's alive, use skills it has, and call up
59// some of kinds that there are.
60absl::Status CheckPhases(const monsters::Kind& kind,
61 const std::map<uint32_t, monsters::Kind>& kinds) {
62 for (int i = 0; i < kind.phases_size(); ++i) {
63 const monsters::Phase& phase = kind.phases(i);
64 const std::string name =
65 absl::StrCat("the ", kind.name(), "'s phase ", i + 1);
66 if (i == 0 && phase.health() != 100) {
67 return absl::InvalidArgumentError(absl::StrCat(
68 name, " starts at ", phase.health(), "% of its health, not at 100%"));
69 }
70 if (i > 0 && phase.health() == 0) {
71 return absl::InvalidArgumentError(
72 absl::StrCat(name, " starts at 0%, when it's dead"));
73 }
74 if (i > 0 && phase.health() >= kind.phases(i - 1).health()) {
75 return absl::InvalidArgumentError(absl::StrCat(
76 name, " starts at ", phase.health(), "%, which isn't below phase ", i,
77 "'s ", kind.phases(i - 1).health(), "%"));
78 }
79 for (const uint32_t skill : phase.skills()) {
80 if (std::ranges::find(kind.skills(), skill, &monsters::Skill::id) ==
81 kind.skills().end()) {
82 return absl::InvalidArgumentError(
83 absl::StrCat(name, " uses a skill ", skill, " it doesn't have"));
84 }
85 }
86 for (const monsters::Add& add : phase.adds()) {
87 const auto called = kinds.find(add.kind());
88 if (called == kinds.end()) {
89 return absl::InvalidArgumentError(absl::StrCat(
90 name, " calls up a kind ", add.kind(), " that isn't in the file"));
91 }
92 if (add.count() == 0) {
93 return absl::InvalidArgumentError(
94 absl::StrCat(name, " calls up no ", called->second.name(), "s"));
95 }
96 }
97 }
98 return absl::OkStatus();
99}
100
101} // namespace
102
103absl::StatusOr<MonsterTable> ParseMonsters(std::string_view text) {
104 monsters::Monsters file;
105 const absl::Status parsed = ParseText(text, file);
106 if (!parsed.ok()) {
107 return parsed;
108 }
109 absl::StatusOr<std::map<uint32_t, monsters::Kind>> kinds = ById(file.kinds());
110 if (!kinds.ok()) {
111 return kinds.status();
112 }
113 for (const auto& [id, kind] : *kinds) {
114 for (const monsters::Drop& drop : kind.loot()) {
115 if (drop.chance() <= 0.0f || drop.chance() > 1.0f) {
116 return absl::InvalidArgumentError(
117 absl::StrCat("the ", kind.name(), " drops item ", drop.item(),
118 " with a chance of ", drop.chance(),
119 ", which has to be more than 0 and at most 1"));
120 }
121 if (drop.max() != 0 && drop.max() < drop.min()) {
122 return absl::InvalidArgumentError(
123 absl::StrCat("the ", kind.name(), " drops from ", drop.min(),
124 " to ", drop.max(), " of item ", drop.item()));
125 }
126 }
127 if (kind.gold().max() < kind.gold().min()) {
128 return absl::InvalidArgumentError(
129 absl::StrCat("the ", kind.name(), " drops from ", kind.gold().min(),
130 " to ", kind.gold().max(), " gold"));
131 }
132 // The clients hear which skill a monster's using by its id, as they do
133 // its kind.
134 if (const absl::Status ids = ById(kind.skills()).status(); !ids.ok()) {
135 return ids;
136 }
137 for (const monsters::Skill& skill : kind.skills()) {
138 if (const absl::Status checked = CheckSkill(kind, skill); !checked.ok()) {
139 return checked;
140 }
141 }
142 if (const absl::Status checked = CheckPhases(kind, *kinds); !checked.ok()) {
143 return checked;
144 }
145 }
146 MonsterTable table{.kinds = *std::move(kinds),
147 .spawns = {file.spawns().begin(), file.spawns().end()},
148 .camps = {file.camps().begin(), file.camps().end()}};
149 for (const monsters::Spawn& spawn : table.spawns) {
150 const absl::Status checked = CheckSpawn(table.kinds, spawn);
151 if (!checked.ok()) {
152 return checked;
153 }
154 // Players come back to life in the middle of their land, so a monster
155 // can't chase them there.
156 const monsters::Kind& kind = table.kinds.at(spawn.kind());
157 if (std::hypot(spawn.x(), spawn.z()) <= kind.fight().leash()) {
158 return absl::InvalidArgumentError(absl::StrCat(
159 "the ", kind.name(), " at ", spawn.x(), ", ", spawn.z(),
160 " could chase players in the middle of the ", spawn.land()));
161 }
162 }
163 for (const monsters::Camps& camps : table.camps) {
164 const absl::Status checked = CheckSpawn(table.kinds, camps);
165 if (!checked.ok()) {
166 return checked;
167 }
168 }
169 return table;
170}
171
172bool InShape(const monsters::Skill& skill, Place from, float yaw, Place place) {
173 const float dx = place.x - from.x;
174 const float dz = place.z - from.z;
175 if (std::hypot(dx, dz) > skill.size()) {
176 return false;
177 }
178 if (skill.shape() != monsters::CONE) {
179 return true;
180 }
181 // As a player's slice is: half the arc to either side of the way it faced.
182 const float off = std::remainder(std::atan2(dx, dz) - yaw,
183 2.0f * std::numbers::pi_v<float>);
184 return std::abs(off) <=
185 skill.arc() / 2.0f * std::numbers::pi_v<float> / 180.0f;
186}
187
188} // namespace nanommo
Definition chat.cc:9
bool InShape(const monsters::Skill &skill, Place from, float yaw, Place place)
Definition monsters.cc:172
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
absl::StatusOr< MonsterTable > ParseMonsters(std::string_view text)
Definition monsters.cc:103
constexpr std::chrono::milliseconds kShortestTelegraph
Definition monsters.h:25
absl::Status CheckSpawn(const std::map< uint32_t, Kind > &kinds, const Spawn &spawn)
Whether a spawn names a kind we have, in a land we have.
Definition checks.h:37
The kinds of monster, by id, and where they spawn.
Definition monsters.h:17
std::vector< monsters::Spawn > spawns
Definition monsters.h:19
std::map< uint32_t, monsters::Kind > kinds
Definition monsters.h:18
std::vector< monsters::Camps > camps
Definition monsters.h:20
In metres from the middle of a land.
Definition lands.h:13
float z
Definition lands.h:15
float x
Definition lands.h:14