### CMake Project Setup for SimpleAFKExample Source: https://github.com/adepierre/botcraft/blob/master/Examples/3_SimpleAFKExample/CMakeLists.txt Configures a CMake project named '3_SimpleAFKExample'. It sets the source files to be compiled and defines include folders, although none are specified in this example. The `add_example` command is used to add the project as an example. ```cmake project(3_SimpleAFKExample) set(${PROJECT_NAME}_SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/main.cpp ) set(${PROJECT_NAME}_INCLUDE_FOLDERS ) add_example("${${PROJECT_NAME}_INCLUDE_FOLDERS}" "${${PROJECT_NAME}_SOURCE_FILES}") ``` -------------------------------- ### Include Botcraft Example Subdirectories Source: https://github.com/adepierre/botcraft/blob/master/Examples/CMakeLists.txt Includes various example subdirectories within the Botcraft project. These subdirectories contain the source code and CMakeLists.txt files for individual examples, allowing them to be built as separate targets. One example is conditionally included based on the protocol version. ```cmake add_subdirectory(0_HelloWorld) add_subdirectory(1_UserControlledExample) add_subdirectory(2_ChatCommandExample) add_subdirectory(3_SimpleAFKExample) add_subdirectory(4_MapCreatorExample) add_subdirectory(5_MobHitterExample) if (PROTOCOL_VERSION STRGREATER "470") # 1.14+ add_subdirectory(6_DispenserFarmExample) endif() add_subdirectory(7_WorldEaterExample) ``` -------------------------------- ### CMake Macro for Botcraft Example Projects Source: https://github.com/adepierre/botcraft/blob/master/Examples/CMakeLists.txt Defines a reusable CMake macro `add_example` to streamline the creation of executable targets for example projects. It handles setting include directories, linking against the 'botcraft' library, specifying C++ standard, and configuring output properties for different build configurations and platforms (MSVC vs. others). ```cmake macro(add_example include_folders source_files) add_executable(${PROJECT_NAME} ${source_files}) target_include_directories(${PROJECT_NAME} PUBLIC ${include_folders}) target_link_libraries(${PROJECT_NAME} botcraft) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER Examples) set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "_d") set_target_properties(${PROJECT_NAME} PROPERTIES RELWITHDEBINFO_POSTFIX "_rd") if(MSVC) # To avoid having folder for each configuration when building with Visual set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/bin") set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded") else() set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin") endif(MSVC) # Add special $ORIGIN to rpath so Linux can find shared libraries in same folder set_target_properties(${PROJECT_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN") install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) endmacro() ``` -------------------------------- ### Configure Project and Add Example (CMake) Source: https://github.com/adepierre/botcraft/blob/master/Examples/2_ChatCommandExample/CMakeLists.txt This CMake script configures the project named '2_ChatCommandExample'. It sets the source files and include folders, then adds the example using these configurations. Dependencies include CMake. ```cmake project(2_ChatCommandExample) set(${PROJECT_NAME}_SOURCE_FILES ${PROJECT_SOURCE_DIR}/include/ChatCommandClient.hpp ${PROJECT_SOURCE_DIR}/src/ChatCommandClient.cpp ${PROJECT_SOURCE_DIR}/src/main.cpp ) set(${PROJECT_NAME}_INCLUDE_FOLDERS ${PROJECT_SOURCE_DIR}/include ) add_example("${${PROJECT_NAME}_INCLUDE_FOLDERS}" "${${PROJECT_NAME}_SOURCE_FILES}") ``` -------------------------------- ### Specific Entity Implementations (Examples) in C++ Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Illustrates the implementation of concrete entity classes by inheriting from their respective base classes. These examples show how specific game entities like SlimeEntity, SmallFireballEntity, and PlayerEntity are defined. ```cpp // Example: SlimeEntity.cpp #include "SlimeEntity.h" SlimeEntity::SlimeEntity(Level* level) : MonsterEntity(EntityType::SLIME, level, 20.0f) {} // Slime-specific behavior like splitting or jumping would be implemented here. // Example: SmallFireballEntity.cpp #include "SmallFireballEntity.h" SmallFireballEntity::SmallFireballEntity(Level* level) : AbstractHurtingProjectileEntity(EntityType::SMALL_FIREBALL, level) {} // Fireball physics and damage logic would be implemented. // Example: PlayerEntity.cpp #include "PlayerEntity.h" PlayerEntity::PlayerEntity(Level* level, const std::string& name) : LivingEntity(EntityType::PLAYER, level, 20.0f), m_name(name) {} // Player input handling, inventory, and interaction logic would be here. ``` -------------------------------- ### Install Project Targets (CMake) Source: https://github.com/adepierre/botcraft/blob/master/protocolCraft/CMakeLists.txt Installs the 'protocolCraft' target binaries, libraries, and archives to their designated installation directories. It also generates export files for CMake's find_package mechanism. ```cmake include(GNUInstallDirs) install(TARGETS protocolCraft EXPORT protocolCraft-targets ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_BINDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ) ``` -------------------------------- ### Install Botcraft Include Directories (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Installs the Botcraft header files to the system's include directory. This makes the Botcraft library's API accessible to other projects that depend on it. ```cmake install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/botcraft" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/botcraft/Version.hpp" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/botcraft" ) ``` -------------------------------- ### Install Include Directory (CMake) Source: https://github.com/adepierre/botcraft/blob/master/protocolCraft/CMakeLists.txt Installs the 'protocolCraft' header files from the 'include/protocolCraft' directory to the system's include directory. This makes the project's headers available for other projects to include. ```cmake install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/protocolCraft" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) ``` -------------------------------- ### Install Botcraft Executable and Libraries (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Installs the main Botcraft executable, its associated libraries, and archives to the appropriate system directories. This ensures the built project is correctly placed for execution and linking. ```cmake include(GNUInstallDirs) install(TARGETS botcraft EXPORT botcraft-targets ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_BINDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ) ``` -------------------------------- ### Build a Sequence Behaviour Tree with Blackboard Operations (C++) Source: https://github.com/adepierre/botcraft/wiki/Behaviour-system Demonstrates building a simple behaviour tree using Botcraft's builder. It includes setting data on the blackboard and executing a custom action (SayBlackboard). This example requires a SimpleBehaviourClient and assumes SayBlackboard is defined elsewhere. ```cpp auto behaviour = Builder() .sequence() .leaf(SetBlackboardData, "Say.msg", "Hello") .leaf(SayBlackboard) .leaf(SetBlackboardData, "Say.msg", "World!") .leaf(SayBlackboard) .end(); ``` -------------------------------- ### Build Botcraft with Encryption and Compression Source: https://github.com/adepierre/botcraft/blob/master/README.md Instructions to clone the Botcraft repository, configure the build using CMake with specific options for game version, build type, examples, tests, compression, encryption, and disabling OpenGL GUI, and finally build the project. This command sequence is designed for a Linux/macOS environment. ```bash git clone https://github.com/adepierre/Botcraft.git cd Botcraft mkdir build cd build cmake -DBOTCRAFT_GAME_VERSION=latest -DCMAKE_BUILD_TYPE=Release -DBOTCRAFT_BUILD_EXAMPLES=ON -DBOTCRAFT_BUILD_TESTS=ON -DBOTCRAFT_BUILD_TESTS_ONLINE=ON -DBOTCRAFT_COMPRESSION=ON -DBOTCRAFT_ENCRYPTION=ON -DBOTCRAFT_USE_OPENGL_GUI=OFF .. cmake --build . --config Release # Run all tests (optional) ctest -C Release ``` -------------------------------- ### Implementing Composites in Botcraft Builder Source: https://github.com/adepierre/botcraft/wiki/Behaviour-system Shows how to use 'Composite' nodes, such as 'sequence' and 'selector', which manage multiple child nodes. The examples cover both predefined and custom composite types, including how to name them and properly close their declaration with `.end()`. ```cpp auto tree = Builder .sequence() // <-- shortcut to a prebuilt composite .leaf(Foo) .leaf(Foo) .leaf(Foo) .composite>() // <-- example of a full composite creation .leaf(Foo) .leaf(Foo) .leaf(Foo) .end() .sequence("Named sequence") // <-- named sequence .leaf(Foo) .end() .composite>("Named Selector") // <-- named selector .leaf(Foo) .end() .end(); ``` -------------------------------- ### Configure MapCreatorExample Project with CMake Source: https://github.com/adepierre/botcraft/blob/master/Examples/4_MapCreatorExample/CMakeLists.txt This CMake script configures the MapCreatorExample project. It defines source files and include directories, then adds an example target using these configurations. Dependencies include CMake itself. ```cmake project(4_MapCreatorExample) set(${PROJECT_NAME}_SOURCE_FILES ${PROJECT_SOURCE_DIR}/include/MapCreationTasks.hpp ${PROJECT_SOURCE_DIR}/include/CustomBehaviourTree.hpp ${PROJECT_SOURCE_DIR}/src/MapCreationTasks.cpp ${PROJECT_SOURCE_DIR}/src/main.cpp ) set(${PROJECT_NAME}_INCLUDE_FOLDERS ${PROJECT_SOURCE_DIR}/include ) add_example("" "${${PROJECT_NAME}_SOURCE_FILES}") ``` -------------------------------- ### Conditional Installation of Minecraft Assets (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Installs Minecraft asset files into the installation directory. The installation behavior differs based on the BOTCRAFT_INSTALL_MC_ASSETS flag, with an option to exclude specific patterns. ```cmake if (BOTCRAFT_INSTALL_MC_ASSETS) install(DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin/Assets/${BOTCRAFT_GAME_VERSION}" DESTINATION "${CMAKE_INSTALL_BINDIR}/Assets" ) else() install(DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin/Assets/${BOTCRAFT_GAME_VERSION}" DESTINATION "${CMAKE_INSTALL_BINDIR}/Assets" PATTERN "minecraft" EXCLUDE ) endif(BOTCRAFT_INSTALL_MC_ASSETS) ``` -------------------------------- ### Configure and Build Tests Source: https://github.com/adepierre/botcraft/blob/master/CMakeLists.txt This section handles the setup and building of tests. It includes enabling CTest for test management, integrating the Catch2 testing framework, and conditionally enabling online tests which have further dependencies like Java and compression. ```cmake if (BOTCRAFT_BUILD_TESTS) include(CTest) include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/catch2.cmake") if(BOTCRAFT_BUILD_TESTS_ONLINE AND BOTCRAFT_COMPRESSION) # Add subprocess include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/subprocess.cmake") # Check if JRE is present to run the test server if(BOTCRAFT_GAME_VERSION VERSION_LESS "1.17") find_package(Java 8 QUIET COMPONENTS Runtime) elseif(BOTCRAFT_GAME_VERSION VERSION_LESS "1.20.5") find_package(Java 17 QUIET COMPONENTS Runtime) else() find_package(Java 21 QUIET COMPONENTS Runtime) endif() if(NOT Java_FOUND) message(WARNING "Java not found. Online tests will be built, but won't be able to run on this machine. You can disable online tests by setting BOTCRAFT_BUILD_TESTS_ONLINE to OFF") endif() # Make sure server.jar is present download_mc_server(${VERSION_SERVER_URL} "${BOTCRAFT_OUTPUT_DIR}/bin/test_server_files/server_${BOTCRAFT_GAME_VERSION}.jar") elseif(BOTCRAFT_BUILD_TESTS_ONLINE) message(WARNING "Online tests require BOTCRAFT_COMPRESSION") endif() add_subdirectory(tests) endif() ``` -------------------------------- ### Install Botcraft CMake Export Targets (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Installs CMake target export files, which allow other CMake projects to find and use the installed Botcraft library. This is crucial for integrating Botcraft into larger build systems. ```cmake install(EXPORT botcraft-targets FILE botcraft-targets.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/botcraft" ) ``` -------------------------------- ### Create Autonomous Bot with C++ SimpleBehaviourClient and Behaviour Trees Source: https://context7.com/adepierre/botcraft/llms.txt Illustrates the use of SimpleBehaviourClient for creating bots with autonomous capabilities using a behaviour tree system. This example shows how to initialize the client, connect to a server, set up a behaviour tree with tasks like 'GoTo' and 'Say', and run the behaviour loop. It also covers optional features like auto-respawn. ```cpp #include "botcraft/AI/SimpleBehaviourClient.hpp" #include "botcraft/AI/BehaviourTree.hpp" #include "botcraft/AI/Tasks/AllTasks.hpp" #include "botcraft/Utilities/Logger.hpp" using namespace Botcraft; int main() { Logger::GetInstance().SetLogLevel(LogLevel::Info); Logger::GetInstance().SetFilename(""); Logger::GetInstance().RegisterThread("main"); // Create client (false = no GUI rendering) auto client = std::make_shared(false); // Optional: Enable auto-respawn on death client->SetAutoRespawn(true); // Connect to server client->Connect("127.0.0.1:25565", "BehaviourBot"); // Start the behaviour loop client->StartBehaviour(); // Create and set a behaviour tree auto tree = Builder("patrol tree") .sequence() .leaf("go to point A", GoTo, Position(100, 64, 100), 0, 0, 0, true, true, 1.0f) .leaf(Say, "Reached point A!") .leaf("go to point B", GoTo, Position(150, 64, 150), 0, 0, 0, true, true, 1.0f) .leaf(Say, "Reached point B!") .end(); client->SetBehaviourTree(tree); // Main loop - call BehaviourStep() to tick the tree while (!client->GetShouldBeClosed()) { client->BehaviourStep(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); } client->Disconnect(); return 0; } ``` -------------------------------- ### Define glad Library with CMake Source: https://github.com/adepierre/botcraft/blob/master/3rdparty/glad/CMakeLists.txt This snippet defines the 'glad' library as a static library using CMake. It specifies the source and header files, sets public include directories for build and installation, and configures the installation of the library and its targets. ```cmake cmake_minimum_required(VERSION 3.19) project(glad C) set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(GLAD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/glad.c) set(GLAD_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/glad/glad.h ${CMAKE_CURRENT_SOURCE_DIR}/include/KHR/khrplatform.h) add_library(glad STATIC ${GLAD_HEADERS} ${GLAD_SOURCES}) target_include_directories(glad PUBLIC ``` ``` -------------------------------- ### Configure CMake Project with Source and Include Files Source: https://github.com/adepierre/botcraft/blob/master/Examples/1_UserControlledExample/CMakeLists.txt This CMake script configures a project named 'UserControlledExample'. It defines the source files and include directories required for the project and then adds it as a buildable example using the 'add_example' command. ```cmake project(1_UserControlledExample) set(${PROJECT_NAME}_SOURCE_FILES ${PROJECT_SOURCE_DIR}/include/UserControlledClient.hpp ${PROJECT_SOURCE_DIR}/src/UserControlledClient.cpp ${PROJECT_SOURCE_DIR}/src/main.cpp ) set(${PROJECT_NAME}_INCLUDE_FOLDERS ${PROJECT_SOURCE_DIR}/include ) add_example("${${PROJECT_NAME}_INCLUDE_FOLDERS}" "${${PROJECT_NAME}_SOURCE_FILES}") ``` -------------------------------- ### Configure and Install Package Config (CMake) Source: https://github.com/adepierre/botcraft/blob/master/protocolCraft/CMakeLists.txt Configures the package configuration file for 'protocolCraft' using a template and installs it to the appropriate CMake package directory. This enables other projects to find and use 'protocolCraft' via find_package. ```cmake include(CMakePackageConfigHelpers) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/protocolCraft-config.cmake.in" # lowercase as required by find_package "${CMAKE_CURRENT_BINARY_DIR}/cmake/protocolcraft-config.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/protocolCraft" ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/protocolcraft-config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/protocolCraft" ) ``` -------------------------------- ### Install Mingw libstdc++ DLL on Windows (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Conditionally installs the libstdc++-6.dll file when building on Windows with MinGW. This ensures that the necessary runtime library is available for the Botcraft executable. ```cmake if(WIN32 AND MINGW) install(FILES "${BOTCRAFT_OUTPUT_DIR}/bin/libstdc++-6.dll" DESTINATION "${CMAKE_INSTALL_BINDIR}" ) endif() ``` -------------------------------- ### Add Include Directories Source: https://github.com/adepierre/botcraft/blob/master/protocolCraft/CMakeLists.txt This section configures the include directories for the 'protocolCraft' target. It adds both build-time include paths (local to the build) and install-time include paths (for external use). This ensures that the compiler can find necessary header files during compilation and when the library is installed. ```cmake # Add include folders target_include_directories(protocolCraft PUBLIC "$" "$" ) ``` -------------------------------- ### Register Custom Type for Blackboard Debugger Source: https://github.com/adepierre/botcraft/wiki/Behaviour-system Provides an example of how to register a custom C++ class (MyCustomClass) with the Botcraft blackboard debugger. This is necessary for the debugger to display values of custom types by providing a lambda function to convert the object to a string. ```cpp Botcraft::Utilities::AnyParser::RegisterType([](const std::any& f) { std::string output = ""; const MyCustomClass& v = std::any_cast(f); // Convert v to string and store it in output return output; }); ``` -------------------------------- ### Using Decorators in Botcraft Builder Source: https://github.com/adepierre/botcraft/wiki/Behaviour-system Illustrates the use of 'Decorator' nodes in Botcraft, which modify the result of their single child. Examples include predefined decorators like 'inverter' and custom decorators like 'RepeatUntilSuccess', with options for anonymous and named decorators. ```cpp auto tree = Builder .sequence() .inverter() // <-- shortcut to an anonymous predefined decorator .leaf(Foo) .decorator>(5) // <-- example of an anonymous custom decorator with parameter .leaf(Foo) // Same thing but with named decorators .inverter("Named inverter").leaf(Foo) .decorator>("Named RepeatUntilSuccess with parameter", 5).leaf(Foo) .end(); ``` -------------------------------- ### Configure and Install Botcraft Package Config File (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Generates and installs the Botcraft package configuration file (`botcraft-config.cmake`). This file is used by `find_package` in other CMake projects to locate Botcraft and its dependencies. ```cmake include(CMakePackageConfigHelpers) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/botcraft-config.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cmake/botcraft-config.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/botcraft" ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/botcraft-config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/botcraft" ) ``` -------------------------------- ### Custom Client and Action Definition (C++) Source: https://github.com/adepierre/botcraft/wiki/Behaviour-system Defines a custom client class 'MyClient' inheriting from 'TemplatedBehaviourClient' and a sample action 'MyAction' that accesses a member of the client. This demonstrates how to extend the client's capabilities for behaviour trees. ```cpp class MyClient : public TemplatedBehaviourClient { public: int foo; } Status MyAction(MyClient& client) { std::cout << client.foo << std::endl; return Status::Success; } auto tree = Builder() .leaf(MyAction); ``` -------------------------------- ### Conditional Installation of Asset Download Scripts (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Conditionally installs scripts for downloading Minecraft assets based on the BOTCRAFT_INSTALL_MC_ASSETS flag. It provides platform-specific scripts (.bat for Windows, .sh for others). ```cmake if (NOT BOTCRAFT_INSTALL_MC_ASSETS) if (WIN32) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/download_mc_assets.bat" DESTINATION "${CMAKE_INSTALL_BINDIR}" ) else() install(FILES "${CMAKE_CURRENT_BINARY_DIR}/download_mc_assets.sh" DESTINATION "${CMAKE_INSTALL_BINDIR}" ) endif (WIN32) endif(NOT BOTCRAFT_INSTALL_MC_ASSETS) ``` -------------------------------- ### Configure 0_HelloWorld Project with CMake Source: https://github.com/adepierre/botcraft/blob/master/Examples/0_HelloWorld/CMakeLists.txt This snippet defines a CMake project named '0_HelloWorld'. It sets the source files and include folders, then adds an executable target based on these configurations. Dependencies include CMake itself. ```cmake project(0_HelloWorld) set(${PROJECT_NAME}_SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/main.cpp ) set(${PROJECT_NAME}_INCLUDE_FOLDERS ) add_example("${${PROJECT_NAME}_INCLUDE_FOLDERS}" "${${PROJECT_NAME}_SOURCE_FILES}") ``` -------------------------------- ### Export Targets for Find Package (CMake) Source: https://github.com/adepierre/botcraft/blob/master/protocolCraft/CMakeLists.txt Exports the installed targets for 'protocolCraft' into a CMake file. This file is used by CMake's find_package command to locate and link against the installed library. ```cmake export(EXPORT protocolCraft-targets # lowercase as required by find_package FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/protocolcraft-targets.cmake" ) ``` -------------------------------- ### Initialize DoxygenAwesome Features Source: https://github.com/adepierre/botcraft/blob/master/doxygen/header.html Initializes various DoxygenAwesome features for an enhanced documentation experience. This includes paragraph linking, fragment copying, and a dark mode toggle. These are typically called from a JavaScript initialization script. ```javascript DoxygenAwesomeParagraphLink.init(); DoxygenAwesomeFragmentCopyButton.init(); DoxygenAwesomeDarkModeToggle.init(); ``` -------------------------------- ### Build Botcraft with CMake - Bash Source: https://context7.com/adepierre/botcraft/llms.txt Provides instructions for building the Botcraft library from source using CMake. This involves cloning the repository, creating a build directory, and configuring the build with customizable options. This is a standard build process for C++ projects using CMake. ```bash # Clone the repository git clone https://github.com/adepierre/Botcraft.git cd Botcraft # Create build directory mkdir build && cd build ``` -------------------------------- ### Define Valid Botcraft Tree Leaf Functions (C++) Source: https://github.com/adepierre/botcraft/wiki/Behaviour-system Provides examples of valid C++ functions that can be used as leaves in a Botcraft behaviour tree. These functions must return a Botcraft::Status and accept a reference to a BehaviourClient as their first argument. Examples include a simple success function, a logging function, and functions created using std::bind and a lambda. ```cpp Status Success(BehaviourClient& client) { return Status::Success; } Status Log(BehaviourClient& client, const std::string& msg) { std::cout << msg << std::endl; return Status::Success; } auto foo = std::bind(Log, std::placeholders::_1, "foo"); auto bar = [=](SimpleBehaviourClient& client) { return Log(client, "bar"); } ``` -------------------------------- ### Behavior Tree Declaration with Builder (C++) Source: https://github.com/adepierre/botcraft/wiki/Changelog Demonstrates the evolution of the Behavior Tree Builder syntax in C++. The 'Old Builder' shows the previous, more verbose method, while the 'New Builder' illustrates the simplified and more readable approach introduced in a later version. This change aims to improve code clarity and reduce boilerplate. ```cpp auto tree = Builder() .sequence() .inverter() .leaf(Foo) .end() .succeeder() .leaf(Bar) .end() .end() .build(); ``` ```cpp auto tree = Builder("My tree") .sequence("Root sequence") .inverter().leaf("Foo leaf", Foo) // anonymous inverter applied to named leaf .succeeder("My succeeder").leaf(Bar) // named succeeder applied to anonymous leaf .end(); ``` -------------------------------- ### Add Include Directories (CMake) Source: https://github.com/adepierre/botcraft/blob/master/botcraft/CMakeLists.txt Specifies include directories for the botcraft target. It defines public include paths for build and install interfaces, and private include paths for internal use. ```cmake # Add include folders target_include_directories(botcraft PUBLIC "$" "$" "$" PRIVATE "$" ) ``` -------------------------------- ### Add Subdirectories for Modules Source: https://github.com/adepierre/botcraft/blob/master/CMakeLists.txt These commands add subdirectories to the build. Each subdirectory likely contains its own CMakeLists.txt file to define targets and dependencies for that specific module (e.g., protocolCraft, botcraft, Examples, tests). ```cmake add_subdirectory(protocolCraft) add_subdirectory(botcraft) if(BOTCRAFT_BUILD_EXAMPLES) add_subdirectory(Examples) endif() ``` -------------------------------- ### Navigate with GoTo Task Source: https://context7.com/adepierre/botcraft/llms.txt The GoTo task handles pathfinding to a destination, navigating the bot around obstacles and through various terrain types. It offers parameters for distance tolerance, end distance, jumping, sprinting, and speed. ```cpp #include "botcraft/AI/Tasks/PathfindingTask.hpp" // GoTo signature: // Status GoTo(BehaviourClient& client, const Position& goal, // int dist_tolerance = 0, int min_end_dist = 0, int min_end_dist_xz = 0, // bool allow_jump = true, bool sprint = true, float speed_factor = 1.0f) auto navigation_tree = Builder("navigation") .selector() // Basic navigation to exact position .leaf("go to exact", GoTo, Position(100, 64, 100), 0, 0, 0, true, true, 1.0f) // Navigate within 2 blocks of target (useful for interacting) .leaf("go near chest", GoTo, Position(50, 64, 50), 2, // dist_tolerance: succeed if within 2 blocks 1, // min_end_dist: stop at least 1 block away 0, // min_end_dist_xz: no XZ constraint true, // allow_jump: can jump over gaps true, // sprint: run when possible 1.0f) // speed_factor: normal speed // Slow careful movement (no sprinting, half speed) .leaf("careful approach", GoTo, Position(30, 64, 30), 1, 0, 0, false, false, 0.5f) .leaf(Say, "Navigation failed!") .end(); // Using precise double coordinates // Status GoToDouble(BehaviourClient& client, const Vector3& goal, // bool allow_jump = true, bool sprint = true, float speed_factor = 1.0f) auto precise_tree = Builder("precise movement") .leaf("go to precise position", GoToDouble, Vector3(100.5, 64.0, 100.5), true, true, 1.0f); ``` -------------------------------- ### Define Project and Source Files (CMake) Source: https://github.com/adepierre/botcraft/blob/master/tests/botcraft_online/CMakeLists.txt This CMake code defines the project name, lists header and source files, and creates an executable target. It specifies the C++ standard to be C++17 and links essential libraries like Catch2, botcraft, and subprocess. It also sets include directories and compile definitions. ```cmake project(botcraft_online_tests) set(HDR_FILES include/MinecraftServer.hpp include/TestManager.hpp include/Utils.hpp ) set(SRC_FILES src/main.cpp src/MinecraftServer.cpp src/TestManager.cpp src/Utils.cpp src/tests/base_tasks.cpp src/tests/dig.cpp src/tests/entities.cpp src/tests/inventory.cpp src/tests/pathfinding.cpp src/tests/physics.cpp src/tests/protocol.cpp src/tests/self-tests.cpp src/tests/world.cpp ) add_executable(${PROJECT_NAME} ${HDR_FILES} ${SRC_FILES}) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17) target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2) target_link_libraries(${PROJECT_NAME} PRIVATE botcraft) target_link_libraries(${PROJECT_NAME} PRIVATE subprocess) target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") get_filename_component(BOTCRAFT_ROOT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE) target_compile_definitions(${PROJECT_NAME} PRIVATE BASE_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/tests") if(BOTCRAFT_COMPRESSION) target_compile_definitions(${PROJECT_NAME} PRIVATE USE_COMPRESSION=1) endif(BOTCRAFT_COMPRESSION) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER Tests) ``` -------------------------------- ### Compare Traditional Code vs. Botcraft Behaviour Tree Source: https://github.com/adepierre/botcraft/wiki/Behaviour-system Demonstrates the difference in syntax between a traditional C++ function for behaviour logic and the Botcraft behaviour tree builder. The behaviour tree allows for a more declarative and potentially simpler way to define complex actions. ```cpp bool Behaviour() { if (!FindFood()) return false; if (!Eat()) return false; if (!Say("Hello")) return false; if (!GoTo(-3, 65, 26)) return false; return true; } ``` ```cpp auto behaviour = Builder() .sequence() .leaf(FindFood) .leaf(Eat) .leaf(Say, "Hello") .leaf(GoTo, -3, 65, 26) .end(); ``` -------------------------------- ### Botcraft CMake Build Options Source: https://github.com/adepierre/botcraft/blob/master/CMakeLists.txt This snippet defines various boolean options for the Botcraft build, controlling features like GUI rendering, compression, encryption, examples, tests, and precompiled headers. These options allow for customization of the build based on user requirements. ```cmake option(BOTCRAFT_USE_OPENGL_GUI "Activate if you want to use OpenGL renderer" OFF) option(BOTCRAFT_USE_IMGUI "Activate if you want to use display information on screen with ImGui" OFF) option(BOTCRAFT_COMPRESSION "Activate if compression is enabled on the server" ON) option(BOTCRAFT_ENCRYPTION "Activate if you want to connect to a server in online mode" ON) option(BOTCRAFT_BUILD_EXAMPLES "Set to compile examples with the library" ON) option(BOTCRAFT_BUILD_TESTS "Activate if you want to build tests" OFF) option(BOTCRAFT_BUILD_TESTS_ONLINE "Activate if you want to enable additional on server tests (requires Java)" OFF) option(BOTCRAFT_WINDOWS_BETTER_SLEEP "Set to true to use better thread sleep on Windows" ON) option(BOTCRAFT_USE_PRECOMPILED_HEADERS "Set to true to precompile botcraft headers, reducing compilation time with MSVC and Clang, ignored on GCC" ON) option(BOTCRAFT_BUILD_DOC "Build documentation (requires Doxygen)" ON) option(BOTCRAFT_FORCE_LOCAL_ZLIB "Set to true to force using a local install of zlib even if already present on the system" OFF) option(BOTCRAFT_FORCE_LOCAL_OPENSSL "Set to true to force using a local install of openssl even if already present on the system" OFF) option(BOTCRAFT_FORCE_LOCAL_GLFW "Set to true to force using a local install of glfw even if already present on the system" OFF) option(BOTCRAFT_FORCE_LOCAL_GLAD "Set to true to force using a local install of glad even if already present on the system" OFF) option(BOTCRAFT_FORCE_LOCAL_CATCH "Set to true to force using a local install of catch2 even if already present on the system" OFF) option(BOTCRAFT_INSTALL_MC_ASSETS "Install Minecraft assets next to custom ones" ON) option(PROTOCOLCRAFT_STATIC "If ON, will build protocolcraft as a static library instead of a dynamic one" OFF) mark_as_advanced(PROTOCOLCRAFT_STATIC) option(BOTCRAFT_STATIC "If ON, will build botcraft as a static library instead of a dynamic one" OFF) mark_as_advanced(BOTCRAFT_STATIC) ``` -------------------------------- ### Connect to Minecraft Server with C++ ConnectionClient Source: https://context7.com/adepierre/botcraft/llms.txt Demonstrates how to use the ConnectionClient class to establish a connection to a Minecraft server. It covers connecting in offline mode with a username, online mode with Microsoft account authentication (including OAuth flow and cached credentials), sending chat messages and commands, respawning, and disconnecting. ```cpp #include "botcraft/Game/ConnectionClient.hpp" #include "botcraft/Utilities/Logger.hpp" int main() { // Initialize logging Botcraft::Logger::GetInstance().SetLogLevel(Botcraft::LogLevel::Info); Botcraft::Logger::GetInstance().SetFilename(""); // Console only Botcraft::Logger::GetInstance().RegisterThread("main"); Botcraft::ConnectionClient client; // Connect in offline mode with username client.Connect("127.0.0.1:25565", "MyBot"); // Or connect with Microsoft account (empty login triggers OAuth flow) // client.Connect("play.myserver.com", ""); // Or use cached Microsoft credentials // client.ConnectMicrosoft("play.myserver.com", "my_account_cache"); // Send chat message client.SendChatMessage(u8"Hello, World!"); // Send command (no leading slash) client.SendChatCommand("gamemode creative"); // Respawn when dead client.Respawn(); // Disconnect when done client.Disconnect(); return 0; } ``` -------------------------------- ### Manage Data with Blackboard Source: https://context7.com/adepierre/botcraft/llms.txt The Blackboard provides a mechanism for storing and retrieving arbitrary data, facilitating communication between behaviour tree nodes. It supports setting and getting values of various types, including custom objects, and allows for default values. ```cpp #include "botcraft/AI/Blackboard.hpp" #include "botcraft/AI/Tasks/BaseTasks.hpp" // Store data in the blackboard Status StoreTargetPosition(BehaviourClient& client) { Blackboard& blackboard = client.GetBlackboard(); // Set values of any type blackboard.Set("target_pos", Position(100, 64, 200)); blackboard.Set("target_name", "home_base"); blackboard.Set("retry_count", 0); blackboard.Set("task_complete", false); return Status::Success; } // Retrieve data from the blackboard Status UseTargetPosition(BehaviourClient& client) { Blackboard& blackboard = client.GetBlackboard(); // Get values (throws if key doesn't exist) Position target = blackboard.Get("target_pos"); std::string name = blackboard.Get("target_name"); // Get with default value int retries = blackboard.Get("retry_count", 0); // Check if key exists using Contains (if available) or try-catch return Status::Success; } // Using built-in blackboard tasks in a tree auto tree = Builder("blackboard example") .sequence() // Set a value .leaf(SetBlackboardData, "InteractWithBlock.pos", Position(10, 64, 10)) // Use the value with a Blackboard variant of a task .leaf(InteractWithBlockBlackboard) // Check a boolean value .leaf(CheckBlackboardBoolData, "task_complete") // Remove a value .leaf(RemoveBlackboardData, "InteractWithBlock.pos") .end(); ``` -------------------------------- ### Run Tick-Accurate Physics Tests (Bash) Source: https://github.com/adepierre/botcraft/wiki/Test-framework Enable and run Botcraft's tick-accurate physics tests. These tests are disabled by default and require specific trajectory files. They are primarily for validating physics implementation against Minecraft vanilla physics. ```bash botcraft_online_tests [.physics] ``` -------------------------------- ### C++ Pathfinding GoTo Function in Botcraft Source: https://github.com/adepierre/botcraft/wiki/Pathfinding The `GoTo` function in Botcraft's PathfindingTask.hpp is responsible for navigating the bot to a specified goal position. It takes various parameters to control movement behavior, including goal tolerance, jump allowance, and sprinting. The function returns a Status indicating success or failure. ```cpp Status GoTo(BehaviourClient& client, const Position& goal, const int dist_tolerance = 0, const int min_end_dist = 0, const int min_end_dist_xz = 0, const bool allow_jump = true, const bool sprint = true, const float speed_factor = 1.0f,); ``` -------------------------------- ### Enable Documentation Generation Source: https://github.com/adepierre/botcraft/blob/master/CMakeLists.txt This conditional block enables the generation of project documentation. It includes the necessary CMake script for Doxygen, a widely used documentation system for C++ and other languages. This feature is controlled by the BOTCRAFT_BUILD_DOC variable. ```cmake if (BOTCRAFT_BUILD_DOC) include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/doxygen.cmake") endif(BOTCRAFT_BUILD_DOC) ``` -------------------------------- ### Configure ProtocolCraft Tests Executable (CMake) Source: https://github.com/adepierre/botcraft/blob/master/tests/protocolCraft/CMakeLists.txt This CMake script configures the protocolCraft_tests executable. It specifies source files, sets the C++ standard to 17, links necessary libraries (Catch2 and protocolCraft), and defines output directories based on the build configuration and compiler. ```cmake project(protocolCraft_tests) set(SRC_FILES src/constexpr_string_processing.cpp src/hashed_slot.cpp src/json.cpp src/nbt.cpp src/serialization.cpp src/templates.cpp src/types.cpp ) add_executable(${PROJECT_NAME} ${SRC_FILES}) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17) target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain) target_link_libraries(${PROJECT_NAME} PRIVATE protocolCraft) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER Tests) if(BOTCRAFT_COMPRESSION) target_compile_definitions(${PROJECT_NAME} PRIVATE USE_COMPRESSION=1) endif(BOTCRAFT_COMPRESSION) # Output the test executable next to the examples and library files if(MSVC) # To avoid having folder for each configuration when building with Visual set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/lib") set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/lib") set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/lib") set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/lib") set_target_properties(${PROJECT_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded") else() set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/lib") endif(MSVC) catch_discover_tests(${PROJECT_NAME} WORKING_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin") ``` -------------------------------- ### InteractWithBlock - Right-Click Interaction in C++ Source: https://context7.com/adepierre/botcraft/llms.txt The InteractWithBlock task simulates right-clicking on a block, useful for opening chests, pressing buttons, or activating levers. The code examples show navigating to a block before interacting and using the blackboard for dynamic interaction positions. It allows specifying the face of interaction and whether to play the animation. ```cpp #include "botcraft/AI/Tasks/BaseTasks.hpp" // InteractWithBlock signature: // Status InteractWithBlock(BehaviourClient& client, const Position& pos, // PlayerDiggingFace face = PlayerDiggingFace::Up, // bool animation = true) auto interaction_tree = Builder("interaction") .sequence() // Navigate close to the block first .leaf("approach", GoTo, Position(100, 64, 100), 4, 0, 1, true, false, 1.0f) // Interact with the block (e.g., open chest, press button) .leaf("interact", InteractWithBlock, Position(100, 64, 100), PlayerDiggingFace::Up, true) // Using blackboard .leaf(SetBlackboardData, "InteractWithBlock.pos", Position(100, 64, 100)) .leaf(InteractWithBlockBlackboard) .end(); ``` -------------------------------- ### Configure Output Directories for MSVC Source: https://github.com/adepierre/botcraft/blob/master/protocolCraft/CMakeLists.txt This section sets the output directories for runtime, library, and archive files for the 'protocolCraft' target when using MSVC. It ensures that binaries and libraries are placed in specific subdirectories ('bin' and 'lib') within the BOTCRAFT_OUTPUT_DIR, organized by build configuration. ```cmake if(MSVC) # To avoid having folder for each configuration when building with Visual set_target_properties(protocolCraft PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/lib") set_target_properties(protocolCraft PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/lib") set_target_properties(protocolCraft PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/lib") set_target_properties(protocolCraft PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/lib") else() set_target_properties(protocolCraft PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin") set_target_properties(protocolCraft PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/lib") endif(MSVC) ```