Skip to content

Commit b1fb95c

Browse files
committed
Add ATBA example
1 parent 7843be4 commit b1fb95c

File tree

13 files changed

+175
-12
lines changed

13 files changed

+175
-12
lines changed

.gitignore

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
.vscode/
22
build/
33

4-
ExampleBot/ExampleBot
5-
ExampleBot/ExampleBot.exe
6-
ExampleBot/ExampleBot-Launcher
7-
ExampleBot/ExampleBot-Launcher.exe
4+
examples/ATBA/ATBA
5+
examples/ATBA/ATBA.exe
6+
7+
examples/ExampleBot/ExampleBot
8+
examples/ExampleBot/ExampleBot.exe
9+
examples/ExampleBot/ExampleBot-Launcher
10+
examples/ExampleBot/ExampleBot-Launcher.exe

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ add_custom_target(rlbot-generated DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/rlbot_gene
6868

6969
add_subdirectory(library)
7070
add_subdirectory(benchmark)
71-
add_subdirectory(ExampleBot)
71+
add_subdirectory(examples/ATBA)
72+
add_subdirectory(examples/ExampleBot)

examples/ATBA/ATBA.bot.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[settings]
2+
name = "ATBA"
3+
loadout_file = ""
4+
root_dir = ""
5+
run_command = "ATBA.exe"
6+
run_command_linux = "./ATBA"
7+
agent_id = "RLBotCPP/ATBA"
8+
hivemind = true
9+
10+
[details]
11+
description = "Made possible by RLBot"
12+
fun_fact = "This is a test bot"
13+
source_link = "https://github.com/mtheall/RLBotCPP"
14+
developer = "BotMaker"
15+
language = "C++"
16+
tags = []

examples/ATBA/ATBA.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "ATBA.h"
2+
3+
#include <tracy/Tracy.hpp>
4+
5+
#include <algorithm>
6+
#include <cmath>
7+
#include <concepts>
8+
#include <cstdio>
9+
#include <cstdlib>
10+
#include <numbers>
11+
#include <unordered_set>
12+
13+
///////////////////////////////////////////////////////////////////////////
14+
ATBA::~ATBA () noexcept = default;
15+
16+
ATBA::ATBA (std::unordered_set<unsigned> indices_, unsigned const team_, std::string name_) noexcept
17+
: rlbot::Bot (std::move (indices_), team_, std::move (name_))
18+
{
19+
std::set<unsigned> sorted (std::begin (indices), std::end (indices));
20+
for (auto const &index : sorted)
21+
std::printf ("Team %u Index %u: Example Bot created\n", team_, index);
22+
}
23+
24+
void ATBA::update (rlbot::flat::GamePacket const *const packet_,
25+
rlbot::flat::BallPrediction const *const ballPrediction_,
26+
rlbot::flat::FieldInfo const *const fieldInfo_,
27+
rlbot::flat::MatchSettings const *const matchSettings_) noexcept
28+
{
29+
for (auto const &index : this->indices)
30+
{
31+
auto &controller = outputs[index];
32+
controller = {};
33+
34+
// If there's no ball, there's nothing to chase; don't do anything
35+
if (packet_->balls ()->size () == 0)
36+
continue;
37+
38+
// We're not in the game packet; skip this tick
39+
if (packet_->players ()->size () <= index)
40+
continue;
41+
42+
auto const target = packet_->balls ()->Get (0)->physics ();
43+
auto const car = packet_->players ()->Get (index)->physics ();
44+
45+
auto const botToTargetAngle = std::atan2 (target->location ().y () - car->location ().y (),
46+
target->location ().z () - car->location ().z ());
47+
48+
auto botFrontToTargetAngle = botToTargetAngle - car->rotation ().yaw ();
49+
if (botFrontToTargetAngle > std::numbers::pi_v<float>)
50+
botFrontToTargetAngle -= 2.0f * std::numbers::pi_v<float>;
51+
if (botFrontToTargetAngle < -std::numbers::pi_v<float>)
52+
botFrontToTargetAngle += 2.0f * std::numbers::pi_v<float>;
53+
54+
auto const throttle = 1.0f;
55+
auto const steer = std::copysign (1.0f, botFrontToTargetAngle);
56+
57+
controller = {throttle, steer, 0.0f, 0.0f, 0.0f, false, false, false, false};
58+
};
59+
}

examples/ATBA/ATBA.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <rlbot/Bot.h>
4+
5+
#include <string>
6+
#include <unordered_set>
7+
8+
/// @brief Always Towards Ball Agent
9+
/// This bot blindly drives towards the ball
10+
class ATBA final : public rlbot::Bot
11+
{
12+
public:
13+
~ATBA () noexcept override;
14+
15+
ATBA () noexcept = delete;
16+
17+
ATBA (std::unordered_set<unsigned> indices_, unsigned team_, std::string name_) noexcept;
18+
19+
ATBA (ATBA const &) noexcept = delete;
20+
21+
ATBA (ATBA &&) noexcept = delete;
22+
23+
ATBA &operator= (ATBA const &) noexcept = delete;
24+
25+
ATBA &operator= (ATBA &&) noexcept = delete;
26+
27+
void update (rlbot::flat::GamePacket const *packet_,
28+
rlbot::flat::BallPrediction const *ballPrediction_,
29+
rlbot::flat::FieldInfo const *fieldInfo_,
30+
rlbot::flat::MatchSettings const *matchSettings_) noexcept override;
31+
};

examples/ATBA/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
3+
project(ATBA VERSION 1.0.0)
4+
5+
###########################################################################
6+
add_executable(${PROJECT_NAME})
7+
8+
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
9+
10+
target_sources(${PROJECT_NAME} PRIVATE
11+
ATBA.cpp
12+
ATBA.h
13+
main.cpp
14+
)
15+
16+
if(LTO_SUPPORTED)
17+
set_target_properties(${PROJECT_NAME} PROPERTIES
18+
INTERPROCEDURAL_OPTIMIZATION $<BOOL:${RLBOT_CPP_ENABLE_LTO}>
19+
INTERPROCEDURAL_OPTIMIZATION_DEBUG FALSE
20+
)
21+
endif()
22+
23+
target_link_libraries(${PROJECT_NAME} PRIVATE RLBotCPP-static)
24+
25+
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
26+
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${PROJECT_NAME}> ${CMAKE_CURRENT_SOURCE_DIR}
27+
)

examples/ATBA/main.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "ATBA.h"
2+
3+
#include <rlbot/BotManager.h>
4+
5+
#include <cstdio>
6+
#include <cstdlib>
7+
8+
int main (int argc_, char *argv_[])
9+
{
10+
auto const agentId = std::getenv ("RLBOT_AGENT_ID");
11+
if (!agentId || std::strlen (agentId) == 0)
12+
{
13+
std::fprintf (stderr, "Missing environment variable RLBOT_AGENT_ID\n");
14+
return EXIT_FAILURE;
15+
}
16+
17+
auto const serverPort = [] () -> char const * {
18+
auto const env = std::getenv ("RLBOT_SERVER_PORT");
19+
if (env)
20+
return env;
21+
return "23234";
22+
}();
23+
24+
auto const host = argc_ > 1 ? argv_[1] : "127.0.0.1";
25+
auto const port = argc_ > 2 ? argv_[2] : serverPort;
26+
27+
rlbot::BotManager<ATBA> manager{true};
28+
if (!manager.run (host, port, agentId, true))
29+
{
30+
std::fprintf (stderr, "Usage: %s [addr] [port]\n", argv_[0]);
31+
return EXIT_FAILURE;
32+
}
33+
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ void ExampleBot::update (rlbot::flat::GamePacket const *const packet_,
146146
}
147147
}
148148

149-
rlbot::flat::ControllerState ExampleBot::getOutput (unsigned const index_) noexcept
150-
{
151-
return outputs[index_];
152-
}
153-
154149
void ExampleBot::matchComm (rlbot::flat::MatchComm const *const matchComm_) noexcept
155150
{
156151
auto const display = matchComm_->display ();

0 commit comments

Comments
 (0)