nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
factions.cc
Go to the documentation of this file.
1#include "bot/factions.h"
2
3#include <algorithm>
4#include <cstdint>
5#include <iterator>
6#include <optional>
7
8#include "terrain/lands.h"
9
10namespace nanommo {
11
12uint32_t FactionFor(const Content& content, uint32_t seed) {
13 if (content.factions.empty()) {
14 return 0;
15 }
16 const auto picked =
17 std::next(content.factions.begin(), seed % content.factions.size());
18 return picked->first;
19}
20
21std::optional<Errand> Oath(const Content& content, uint32_t faction,
22 const protocol::World& seen, int land) {
23 const auto found = content.factions.find(faction);
24 if (found == content.factions.end()) {
25 return std::nullopt;
26 }
27 const uint32_t kind = found->second.herald();
28 const auto herald =
29 std::ranges::find(seen.npcs(), kind, &protocol::Npc::kind);
30 if (herald != seen.npcs().end()) {
31 return Errand{.kind = Errand::Kind::kSwear,
32 .x = herald->x(),
33 .z = herald->z(),
34 .herald = herald->id()};
35 }
36 // Not in sight yet, as NPCs come once a second, and only those near.
37 for (const npcs::Spawn& spawn : content.npcs.spawns) {
38 if (spawn.kind() == kind && LandNamed(spawn.land()) == land) {
39 return Errand{
40 .kind = Errand::Kind::kSwear, .x = spawn.x(), .z = spawn.z()};
41 }
42 }
43 return std::nullopt;
44}
45
46bool Sworn(const protocol::World& seen) {
47 return std::ranges::any_of(
48 seen.players(), [&seen](const protocol::Player& player) {
49 return player.id() == seen.you() && player.faction() != 0;
50 });
51}
52
53} // namespace nanommo
Definition chat.cc:9
bool Sworn(const protocol::World &seen)
Definition factions.cc:46
std::optional< Errand > Oath(const Content &content, uint32_t faction, const protocol::World &seen, int land)
Definition factions.cc:21
int LandNamed(std::string_view name)
Its place in the list, or the first land's for a name that isn't there.
Definition lands.cc:10
uint32_t FactionFor(const Content &content, uint32_t seed)
Definition factions.cc:12
std::map< uint32_t, factions::Faction > factions
By id.
Definition content.h:25
NpcTable npcs
Definition content.h:29
std::vector< npcs::Spawn > spawns
Definition npcs.h:22