nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
war.cc
Go to the documentation of this file.
1#include "bot/war.h"
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6#include <cstdint>
7#include <limits>
8#include <optional>
9#include <string>
10
11#include "protocol/nanommo.pb.h"
12
13namespace nanommo {
14
15std::optional<protocol::GuildAction> Enlist(const Side& side,
16 const protocol::Guild& guild,
17 std::size_t& next) {
18 if (!side.leads) {
19 return std::nullopt;
20 }
21 protocol::GuildAction action;
22 if (guild.name().empty()) {
23 action.set_create(side.guild);
24 return action;
25 }
26 if (!guild.has_war()) {
27 action.set_war(side.enemy);
28 return action;
29 }
30 for (std::size_t tried = 0; tried < side.recruits.size(); ++tried) {
31 const std::string& name = side.recruits[next++ % side.recruits.size()];
32 if (std::ranges::none_of(guild.members(),
33 [&name](const protocol::GuildMember& member) {
34 return member.name() == name;
35 })) {
36 action.set_invite(name);
37 return action;
38 }
39 }
40 return std::nullopt;
41}
42
43uint32_t NearestEnemy(const Side& side, const protocol::World& seen, float x,
44 float z) {
45 uint32_t nearest = 0;
46 float best = std::numeric_limits<float>::max();
47 for (const protocol::Player& player : seen.players()) {
48 const float away = std::hypot(player.x() - x, player.z() - z);
49 if (player.guild() == side.enemy &&
50 player.motion() != protocol::MOTION_DEAD && away < best) {
51 best = away;
52 nearest = player.id();
53 }
54 }
55 return nearest;
56}
57
58} // namespace nanommo
Definition chat.cc:9
uint32_t NearestEnemy(const Side &side, const protocol::World &seen, float x, float z)
Definition war.cc:43
std::optional< protocol::GuildAction > Enlist(const Side &side, const protocol::Guild &guild, std::size_t &next)
Definition war.cc:15
A bot at war is in one of two guilds, and fights the other's players.
Definition war.h:15
std::string enemy
Definition war.h:17
std::vector< std::string > recruits
Definition war.h:21
bool leads
Definition war.h:24
std::string guild
Definition war.h:16