forked from adepierre/Botcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatCommandClient.cpp
379 lines (335 loc) · 12.6 KB
/
ChatCommandClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <sstream>
#include <fstream>
#include <iostream>
#include <botcraft/Game/World/World.hpp>
#include <botcraft/Game/World/Block.hpp>
#include <botcraft/Game/Entities/EntityManager.hpp>
#include <botcraft/Game/Entities/LocalPlayer.hpp>
#include <botcraft/Network/NetworkManager.hpp>
#include <botcraft/AI/BehaviourTree.hpp>
#include <botcraft/AI/Tasks/AllTasks.hpp>
#include "ChatCommandClient.hpp"
using namespace Botcraft;
using namespace ProtocolCraft;
ChatCommandClient::ChatCommandClient(const bool use_renderer_) : TemplatedBehaviourClient<ChatCommandClient>(use_renderer_)
{
std::cout << "Known commands:\n";
std::cout << " Pathfinding to position:\n";
std::cout << " name goto x y z (speed)\n";
std::cout << " Stop what you're doing:\n";
std::cout << " name stop\n";
std::cout << " Check perimeter for spawnable blocks and save spawnable positions to file:\n";
std::cout << " name check_perimeter [x y z (default = player position)] radius (default = 128) [check_lighting (default = true)]\n";
std::cout << " Disconnect:\n";
std::cout << " name die\n";
std::cout << " Place a block:\n";
std::cout << " name place_block minecraft:item x y z\n";
std::cout << " Break a block:\n";
std::cout << " name dig x y z\n";
std::cout << " Interact (right click) a block:\n";
std::cout << " name interact x y z\n";
}
ChatCommandClient::~ChatCommandClient()
{
}
#if PROTOCOL_VERSION < 759
void ChatCommandClient::Handle(ClientboundChatPacket &msg)
{
ManagersClient::Handle(msg);
// Split the message
std::istringstream ss{ msg.GetMessage().GetText() };
const std::vector<std::string> splitted({ std::istream_iterator<std::string>{ss}, std::istream_iterator<std::string>{} });
// Process it
ProcessChatMsg(splitted);
}
#else
void ChatCommandClient::Handle(ProtocolCraft::ClientboundPlayerChatPacket& msg)
{
ManagersClient::Handle(msg);
// Split the message
#if PROTOCOL_VERSION < 760
std::istringstream ss{ msg.GetSignedContent().GetText() };
#else
std::istringstream ss{ msg.GetMessage_().GetSignedBody().GetContent().GetPlain() };
#endif
const std::vector<std::string> splitted({ std::istream_iterator<std::string>{ss}, std::istream_iterator<std::string>{} });
// Process it
ProcessChatMsg(splitted);
}
void ChatCommandClient::Handle(ProtocolCraft::ClientboundSystemChatPacket& msg)
{
ManagersClient::Handle(msg);
// Split the message
std::istringstream ss{ msg.GetContent().GetText() };
const std::vector<std::string> splitted({ std::istream_iterator<std::string>{ss}, std::istream_iterator<std::string>{} });
// Process it
ProcessChatMsg(splitted);
}
#endif
void ChatCommandClient::ProcessChatMsg(const std::vector<std::string>& splitted_msg)
{
if (splitted_msg.size() < 2 || splitted_msg[0] != network_manager->GetMyName())
{
return;
}
if (splitted_msg[1] == "goto")
{
if (splitted_msg.size() < 5)
{
SendChatMessage("Usage: [BotName] [goto] [x] [y] [z] [speed]");
return;
}
Position target_position;
float speed = 4.317f;
try
{
target_position = Position(std::stoi(splitted_msg[2]), std::stoi(splitted_msg[3]), std::stoi(splitted_msg[4]));
if (splitted_msg.size() > 5)
{
speed = std::stof(splitted_msg[5]);
}
}
catch (const std::invalid_argument &e)
{
return;
}
catch (const std::out_of_range &e)
{
return;
}
auto tree = Builder<ChatCommandClient>()
.sequence()
// Perform the pathfinding in a Selector,
// so it exits as soon as one leaf
// returns success
.selector()
// The next three lines do exactly the same,
// they're only here to show the different
// possibilities to create a leaf. Note that
// only the lambda solution can use default
// parameters for the last bool value
.leaf([=](ChatCommandClient& c) { return GoTo(c, target_position, 0, 0, speed); })
.leaf(GoTo, target_position, 0, 0, speed, true)
.leaf(std::bind(GoTo, std::placeholders::_1, target_position, 0, 0, speed, true))
// If goto fails, say something in chat
.leaf(Say, "Pathfinding failed :(")
.end()
// Switch back to empty behaviour
.leaf([](ChatCommandClient& c) { c.SetBehaviourTree(nullptr); return Status::Success; })
.end()
.build();
SetBehaviourTree(tree);
}
else if (splitted_msg[1] == "stop")
{
// Stop any running behaviour
SetBehaviourTree(nullptr);
}
else if (splitted_msg[1] == "check_perimeter")
{
float radius = 128.0f;
Position pos = Position(entity_manager->GetLocalPlayer()->GetPosition().x, entity_manager->GetLocalPlayer()->GetPosition().y, entity_manager->GetLocalPlayer()->GetPosition().z);
bool check_lighting = true;
if (splitted_msg.size() == 3)
{
radius = std::stof(splitted_msg[2]);
}
else if (splitted_msg.size() == 4)
{
radius = std::stof(splitted_msg[2]);
check_lighting = std::stoi(splitted_msg[3]);
}
else if (splitted_msg.size() == 6)
{
pos = Position(std::stoi(splitted_msg[2]), std::stoi(splitted_msg[3]), std::stoi(splitted_msg[4]));
radius = std::stof(splitted_msg[5]);
}
else if (splitted_msg.size() == 7)
{
pos = Position(std::stoi(splitted_msg[2]), std::stoi(splitted_msg[3]), std::stoi(splitted_msg[4]));
radius = std::stof(splitted_msg[5]);
check_lighting = std::stoi(splitted_msg[6]);
}
CheckPerimeter(pos, radius, check_lighting);
}
else if (splitted_msg[1] == "die")
{
should_be_closed = true;
}
else if (splitted_msg[1] == "place_block")
{
if (splitted_msg.size() < 6)
{
SendChatMessage("Usage: [BotName] [place_block] [item] [x] [y] [z]");
return;
}
const std::string& item = splitted_msg[2];
Position pos;
try
{
pos = Position(std::stoi(splitted_msg[3]), std::stoi(splitted_msg[4]), std::stoi(splitted_msg[5]));
}
catch (const std::invalid_argument& e)
{
return;
}
catch (const std::out_of_range& e)
{
return;
}
LOG_INFO("Asked to place a block at " << pos << " (" << item << ")");
auto tree = Builder<ChatCommandClient>()
// shortcut for composite<Sequence<ChatCommandClient>>()
.sequence()
.succeeder()
.leaf(PlaceBlock, item, pos, PlayerDiggingFace::Up, true)
.end()
// Switch back to empty behaviour
.leaf([](ChatCommandClient& c) { c.SetBehaviourTree(nullptr); return Status::Success; })
.end()
.build();
SetBehaviourTree(tree);
}
else if (splitted_msg[1] == "dig")
{
if (splitted_msg.size() < 5)
{
SendChatMessage("Usage: [BotName] [dig] [x] [y] [z]");
return;
}
Position pos;
try
{
pos = Position(std::stoi(splitted_msg[2]), std::stoi(splitted_msg[3]), std::stoi(splitted_msg[4]));
}
catch (const std::invalid_argument& e)
{
return;
}
catch (const std::out_of_range& e)
{
return;
}
auto tree = Builder<ChatCommandClient>()
// shortcut for composite<Sequence<ChatCommandClient>>()
.sequence()
.succeeder()
.leaf(Dig, pos, false, PlayerDiggingFace::Up, -1.0f)
.end()
// Switch back to empty behaviour
.leaf([](ChatCommandClient& c) { c.SetBehaviourTree(nullptr); return Status::Success; })
.end()
.build();
SetBehaviourTree(tree);
}
else if (splitted_msg[1] == "interact")
{
if (splitted_msg.size() < 5)
{
SendChatMessage("Usage: [BotName] [interact] [x] [y] [z]");
return;
}
Position pos;
try
{
pos = Position(std::stoi(splitted_msg[2]), std::stoi(splitted_msg[3]), std::stoi(splitted_msg[4]));
}
catch (const std::invalid_argument& e)
{
return;
}
catch (const std::out_of_range& e)
{
return;
}
auto tree = Builder<ChatCommandClient>()
// shortcut for composite<Sequence<ChatCommandClient>>()
.sequence()
.succeeder()
.sequence()
.leaf(GoTo, pos, 4, 1, 4.317f, true)
// Set interaction position in the blackboard
.leaf(SetBlackboardData<Position>, "InteractWithBlock.pos", pos)
.selector()
// Perform action using the data in the blackboard
.leaf(InteractWithBlockBlackboard)
// Say something if it fails
.leaf(Say, "Interacting failed :(")
.end()
// Remove interaction position in the blackboard because
// we don't want to leave a mess (and to show how to do it)
.leaf(RemoveBlackboardData, "InteractWithBlock.pos")
.end()
.end()
// Switch back to empty behaviour
.leaf([](ChatCommandClient& c) { c.SetBehaviourTree(nullptr); return Status::Success; })
.end()
.build();
SetBehaviourTree(tree);
}
else
{
return;
}
}
void ChatCommandClient::CheckPerimeter(const Position &pos, const float radius, const bool check_lighting)
{
std::vector<Position> found_positions;
Position current_position;
for (int y = -radius - 1; y < radius + 1; ++y)
{
current_position.y = pos.y + y;
for (int x = -radius - 1; x < radius + 1; ++x)
{
current_position.x = pos.x + x;
for (int z = -radius - 1; z < radius + 1; ++z)
{
current_position.z = pos.z + z;
if (x * x + y * y + z * z > radius * radius)
{
continue;
}
std::lock_guard<std::mutex> world_guard(world->GetMutex());
const Block *block = world->GetBlock(current_position);
if (!block || !block->GetBlockstate()->IsAir())
{
continue;
}
Position adjacent_position = current_position;
adjacent_position.y -= 1;
const Block *adjacent_block = world->GetBlock(adjacent_position);
if (!adjacent_block ||
adjacent_block->GetBlockstate()->IsFluid() ||
!adjacent_block->GetBlockstate()->IsSolid() ||
adjacent_block->GetBlockstate()->IsTransparent() ||
adjacent_block->GetBlockstate()->GetName() == "minecraft:bedrock" ||
adjacent_block->GetBlockstate()->GetName() == "minecraft:barrier")
{
continue;
}
adjacent_position.y += 2;
adjacent_block = world->GetBlock(adjacent_position);
if (adjacent_block &&
(adjacent_block->GetBlockstate()->IsSolid() ||
adjacent_block->GetBlockstate()->IsFluid()))
{
continue;
}
if (check_lighting && world->GetBlockLight(current_position) > 7)
{
continue;
}
found_positions.push_back(current_position);
}
}
}
std::ofstream output_file("perimeter_check_" + std::to_string(pos.x) + "_" + std::to_string(pos.y) + "_" + std::to_string(pos.z) + "_radius_" + std::to_string(radius) + ".txt", std::ios::out);
if (output_file.is_open())
{
for (int i = 0; i < found_positions.size(); ++i)
{
output_file << found_positions[i] << "\n";
}
output_file.close();
}
}