├── .gitmodules
├── mcbot
├── components
│ ├── TargetingComponent.cpp
│ ├── JumpComponent.cpp
│ ├── TargetingComponent.h
│ ├── JumpComponent.h
│ ├── SpeedComponent.h
│ ├── PhysicsComponent.cpp
│ ├── EffectComponent.cpp
│ ├── SynchronizationComponent.h
│ ├── EffectComponent.h
│ ├── PhysicsComponent.h
│ ├── SynchronizationComponent.cpp
│ └── SpeedComponent.cpp
├── mcbot.vcxproj.user
├── Actor.cpp
├── Utility.cpp
├── Pathfinder.h
├── Utility.h
├── Math.h
├── Collision.h
├── BotUpdate.h
├── Component.h
├── PlayerList.h
├── Actor.h
├── actions
│ └── WanderAction.h
├── WorldGraph.h
├── GameClient.h
├── GameClient.cpp
├── BotUpdate.cpp
├── Decision.h
├── PlayerList.cpp
├── Collision.cpp
├── Steering.h
├── mcbot.vcxproj.filters
├── Pathing.h
├── Pathing.cpp
├── main.cpp
├── Pathfinder.cpp
├── Steering.cpp
├── mcbot.vcxproj
└── WorldGraph.cpp
├── .gitignore
├── README.md
├── Makefile
├── LICENSE
└── mcbot.sln
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "lib/mclib"]
2 | path = lib/mclib
3 | url = https://github.com/plushmonkey/mclib.git
4 |
--------------------------------------------------------------------------------
/mcbot/components/TargetingComponent.cpp:
--------------------------------------------------------------------------------
1 | #include "TargetingComponent.h"
2 |
3 | const char* TargetingComponent::name = "Targeting";
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | Debug
2 | Release
3 | Debug-Fast
4 | *.opensdf
5 | *.sdf
6 | *.suo
7 | *.vsp
8 | *.psess
9 | bin
10 | *.so
11 | *.o
12 | .vs
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # mcbot
2 | A basic network bot for Minecraft 1.11.0.
3 |
4 | # Examples
5 | 
6 |
--------------------------------------------------------------------------------
/mcbot/mcbot.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 |
6 |
--------------------------------------------------------------------------------
/mcbot/Actor.cpp:
--------------------------------------------------------------------------------
1 | #include "Actor.h"
2 |
3 | void Actor::Update(double dt) {
4 | for (auto& entry : m_Components)
5 | entry.second->Update(dt);
6 | }
7 |
8 | void Actor::AddComponent(std::unique_ptr component) {
9 | m_Components.insert(std::make_pair(component->GetId(), std::move(component)));
10 | }
11 |
12 | void Actor::RemoveComponent(ComponentId id) {
13 | auto iter = m_Components.find(id);
14 | m_Components.erase(iter);
15 | }
16 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CXXFLAGS=-std=c++14 -Wall -fPIC -O2 -I/usr/include/jsoncpp -Ilib/mclib/mclib/include
2 | LIBS=-L. -ljsoncpp -lmc
3 | CXX=clang++
4 | BIN=bin
5 |
6 | SRC=$(shell find mcbot -type f -name *.cpp)
7 |
8 | bot: $(SRC:.cpp=.o) libmc.so
9 | $(CXX) -o $(BIN)/$@ $(CXXFLAGS) $^ -Wl,-rpath,. $(LIBS)
10 | -mv -f libmc.so bin/libmc.so
11 |
12 | libmc.so: directory
13 | $(MAKE) -C lib/mclib
14 | cp lib/mclib/libmc.so libmc.so
15 |
16 | directory:
17 | -mkdir $(BIN)
18 |
19 | clean:
20 | -rm -f mcbot/*.o
21 | -rm -f mcbot/*/*.o
22 |
--------------------------------------------------------------------------------
/mcbot/Utility.cpp:
--------------------------------------------------------------------------------
1 | #include "Utility.h"
2 |
3 | #include
4 |
5 | using mc::Vector3d;
6 |
7 | namespace util {
8 |
9 | s64 GetTime() {
10 | return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count();
11 | }
12 |
13 | Vector3d OrientationToVector(double orientation) {
14 | double pitch = 0.0;
15 |
16 | return Vector3d(
17 | -std::cos(pitch) * std::sin(orientation),
18 | -std::sin(pitch),
19 | std::cos(pitch) * std::cos(orientation)
20 | );
21 | }
22 |
23 | } // ns util
24 |
--------------------------------------------------------------------------------
/mcbot/components/JumpComponent.cpp:
--------------------------------------------------------------------------------
1 | #include "JumpComponent.h"
2 |
3 | #include "../Actor.h"
4 | #include "PhysicsComponent.h"
5 | #include "../Utility.h"
6 |
7 | #include
8 |
9 | const char* JumpComponent::name = "Jump";
10 | const s64 JumpCooldown = 500;
11 |
12 | using mc::Vector3d;
13 |
14 | void JumpComponent::Update(double dt) {
15 | auto physics = GetActorComponent(m_Owner, PhysicsComponent);
16 | if (!physics) return;
17 |
18 | s64 time = util::GetTime();
19 |
20 | bool onGround = m_CollisionDetector.DetectCollision(physics->GetPosition(), Vector3d(0, -32 * 50.0 / 1000.0, 0), nullptr);
21 |
22 | if (onGround && m_Jump) {
23 | if (time >= m_LastJump + JumpCooldown) {
24 | physics->ApplyAcceleration(Vector3d(0, m_Power, 0));
25 | m_LastJump = time;
26 | }
27 | }
28 |
29 | m_Jump = false;
30 | }
31 |
--------------------------------------------------------------------------------
/mcbot/components/TargetingComponent.h:
--------------------------------------------------------------------------------
1 | #ifndef MCBOT_TARGETING_COMPONENT_H_
2 | #define MCBOT_TARGETING_COMPONENT_H_
3 |
4 | #include "../Component.h"
5 | #include "../Collision.h"
6 |
7 | class TargetingComponent : public Component {
8 | public:
9 | static const char* name;
10 |
11 | private:
12 | mc::Vector3i m_Target;
13 | mc::EntityId m_TargetEntity;
14 |
15 | public:
16 | TargetingComponent()
17 | : m_TargetEntity(-1)
18 | {
19 |
20 | }
21 |
22 | void Update(double dt) { }
23 |
24 | mc::Vector3i GetTarget() const { return m_Target; }
25 | void SetTarget(mc::Vector3i target) { m_Target = target; }
26 |
27 | mc::EntityId GetTargetEntity() const { return m_TargetEntity; }
28 | void SetTargetEntity(mc::EntityId eid) { m_TargetEntity = eid; }
29 |
30 | const char* GetName() const { return name; }
31 | };
32 |
33 | #endif
34 |
--------------------------------------------------------------------------------
/mcbot/Pathfinder.h:
--------------------------------------------------------------------------------
1 | #ifndef MCBOT_PATHFINDER_H_
2 | #define MCBOT_PATHFINDER_H_
3 |
4 | #include "GameClient.h"
5 | #include "Collision.h"
6 | #include "Pathing.h"
7 |
8 | #include
9 |
10 | mc::Vector3i GetGroundLevel(mc::world::World* world, mc::Vector3i pos);
11 |
12 | struct CastResult {
13 | std::vector hit;
14 | std::size_t length;
15 | bool full;
16 | };
17 |
18 | CastResult RayCast(mc::world::World* world, WorldGraph* graph, const mc::Vector3d& from, mc::Vector3d direction, std::size_t length);
19 |
20 | class Pathfinder {
21 | private:
22 | GameClient* m_Client;
23 | std::shared_ptr m_Plan;
24 | CollisionDetector m_CollisionDetector;
25 |
26 | bool IsNearBlocks(mc::Vector3d pos);
27 | void SmoothPath();
28 |
29 | public:
30 | Pathfinder(GameClient* client);
31 |
32 | void Update();
33 | };
34 |
35 | #endif
36 |
--------------------------------------------------------------------------------
/mcbot/components/JumpComponent.h:
--------------------------------------------------------------------------------
1 | #ifndef MCBOT_JUMP_COMPONENT_H_
2 | #define MCBOT_JUMP_COMPONENT_H_
3 |
4 | #include "../Component.h"
5 | #include "../Collision.h"
6 | #include
7 |
8 | class JumpComponent : public Component {
9 | public:
10 | static const char* name;
11 |
12 | private:
13 | CollisionDetector m_CollisionDetector;
14 | mc::world::World* m_World;
15 | double m_Power;
16 | s64 m_LastJump;
17 | bool m_Jump;
18 |
19 | public:
20 | JumpComponent(mc::world::World* world, double power)
21 | : m_CollisionDetector(world), m_World(world), m_Power(power), m_LastJump(0), m_Jump(false)
22 | {
23 |
24 | }
25 |
26 | void Jump() { m_Jump = true; }
27 | void Update(double dt);
28 | void SetJumpPower(double power) { m_Power = power; }
29 |
30 | const char* GetName() const { return name; }
31 | };
32 |
33 | #endif
34 |
--------------------------------------------------------------------------------
/mcbot/Utility.h:
--------------------------------------------------------------------------------
1 | #ifndef AI_UTILITY_H_
2 | #define AI_UTILITY_H_
3 |
4 | #include
5 |
6 | #undef min
7 | #undef max
8 |
9 | namespace util {
10 |
11 | s64 GetTime();
12 | mc::Vector3d OrientationToVector(double orientation);
13 |
14 | template
15 | class Smoother {
16 | private:
17 | T m_Values[Amount];
18 | std::size_t m_Index;
19 |
20 | public:
21 | Smoother() : m_Index(0) {
22 | for (std::size_t i = 0; i < Amount; ++i)
23 | m_Values[i] = T();
24 | }
25 |
26 | void AddValue(T value) {
27 | m_Values[m_Index] = value;
28 | m_Index = (m_Index + 1) % Amount;
29 | }
30 |
31 | T GetSmoothedValue() {
32 | T total = T();
33 |
34 | for (std::size_t i = 0; i < Amount; ++i)
35 | total = total + m_Values[i];
36 |
37 | return total / Amount;
38 | }
39 | };
40 |
41 | } // ns util
42 |
43 | #endif
44 |
--------------------------------------------------------------------------------
/mcbot/Math.h:
--------------------------------------------------------------------------------
1 | #ifndef MCBOT_MATH_H_
2 | #define MCBOT_MATH_H_
3 |
4 | #include
5 | #include
6 |
7 | #ifndef M_PI
8 | #define M_PI 3.14159265358979323846
9 | #endif
10 |
11 | #ifndef M_TAU
12 | #define M_TAU M_PI * 2
13 | #endif
14 |
15 | inline mc::Vector3d Hadamard(mc::Vector3d first, mc::Vector3d second) {
16 | return mc::Vector3d(first.x * second.x, first.y * second.y, first.z * second.z);
17 | }
18 |
19 | inline double WrapMax(double x, double max) {
20 | return fmod(max + fmod(x, max), max);
21 | }
22 |
23 | inline double WrapMinMax(double x, double min, double max) {
24 | return min + WrapMax(x - min, max - min);
25 | }
26 |
27 | template
28 | double RandomBinomial(Engine engine) {
29 | std::uniform_real_distribution dist(0, 1);
30 |
31 | return dist(engine) - dist(engine);
32 | }
33 |
34 | inline double WrapToPi(double rads) {
35 | return WrapMinMax(rads, -M_PI, M_PI);
36 | }
37 |
38 | #endif
39 |
--------------------------------------------------------------------------------
/mcbot/Collision.h:
--------------------------------------------------------------------------------
1 | #ifndef MCBOT_COLLISION_H_
2 | #define MCBOT_COLLISION_H_
3 |
4 | #include
5 |
6 | class PhysicsComponent;
7 |
8 | class Collision {
9 | private:
10 | mc::Vector3d m_Position;
11 | mc::Vector3d m_Normal;
12 |
13 | public:
14 | Collision() noexcept { }
15 | Collision(mc::Vector3d position, mc::Vector3d normal) noexcept : m_Position(position), m_Normal(normal) { }
16 |
17 | mc::Vector3d GetPosition() const noexcept { return m_Position; }
18 | mc::Vector3d GetNormal() const noexcept { return m_Normal; }
19 | };
20 |
21 | class CollisionDetector {
22 | private:
23 | mc::world::World* m_World;
24 |
25 | std::vector GetSurroundingLocations(mc::AABB bounds);
26 |
27 | public:
28 | CollisionDetector(mc::world::World* world) noexcept : m_World(world) { }
29 |
30 | bool DetectCollision(mc::Vector3d from, mc::Vector3d rayVector, Collision* collision) const;
31 |
32 | void ResolveCollisions(PhysicsComponent* physics, double dt);
33 | };
34 |
35 | #endif
36 |
--------------------------------------------------------------------------------
/mcbot/BotUpdate.h:
--------------------------------------------------------------------------------
1 | #ifndef MCBOT_BOTUPDATE_H_
2 | #define MCBOT_BOTUPDATE_H_
3 |
4 | #include "GameClient.h"
5 | #include "PlayerList.h"
6 | #include "Pathfinder.h"
7 | #include "Decision.h"
8 | #include "Component.h"
9 |
10 | class BotUpdate : public mc::core::ClientListener {
11 | private:
12 | GameClient* m_Client;
13 | PlayerList m_Players;
14 | s64 m_StartupTime;
15 |
16 | std::unique_ptr m_Pathfinder;
17 |
18 | DecisionTreeNodePtr m_DecisionTree;
19 |
20 | public:
21 | BotUpdate(GameClient* client);
22 | ~BotUpdate();
23 |
24 | BotUpdate(const BotUpdate& rhs) = delete;
25 | BotUpdate& operator=(const BotUpdate& rhs) = delete;
26 | BotUpdate(BotUpdate&& rhs) = delete;
27 | BotUpdate& operator=(BotUpdate&& rhs) = delete;
28 |
29 | GameClient* GetClient() noexcept { return m_Client; }
30 |
31 | void OnTick() override;
32 |
33 | void AddComponent(std::unique_ptr component);
34 | void SetDecisionTree(DecisionTreeNodePtr tree) noexcept;
35 | };
36 |
37 | #endif
38 |
--------------------------------------------------------------------------------
/mcbot/Component.h:
--------------------------------------------------------------------------------
1 | #ifndef MCBOT_COMPONENT_H_
2 | #define MCBOT_COMPONENT_H_
3 |
4 | #include
5 | #include