Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy/ai |
4 |
|
|
** File description: |
5 |
|
|
** ABot.cpp |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "ABot.hpp" |
9 |
|
|
|
10 |
|
✗ |
ABot::ABot() |
11 |
|
|
{ |
12 |
|
✗ |
_state.ressources.food = 9; |
13 |
|
✗ |
} |
14 |
|
|
|
15 |
|
✗ |
ABot::~ABot() |
16 |
|
|
{ |
17 |
|
✗ |
} |
18 |
|
|
|
19 |
|
✗ |
void ABot::sendMessage(const std::string &message) |
20 |
|
|
{ |
21 |
|
✗ |
std::string messageToSend = message + "\n"; |
22 |
|
|
|
23 |
|
✗ |
send(_sockfd, messageToSend.c_str(), messageToSend.size(), 0); |
24 |
|
✗ |
} |
25 |
|
|
|
26 |
|
|
// TODO: decrypt parameter in a tmp to debug it easily. |
27 |
|
✗ |
void ABot::doAction(Action action, const std::string parameter) |
28 |
|
|
{ |
29 |
|
|
try |
30 |
|
|
{ |
31 |
|
✗ |
ActionInfo actionInfo = getActionInfo(action); |
32 |
|
✗ |
std::string actionToServer = actionInfo.getName(); |
33 |
|
|
|
34 |
|
✗ |
if (!parameter.empty()) |
35 |
|
✗ |
actionToServer += " " + parameter; |
36 |
|
✗ |
debugAction(actionInfo, parameter); |
37 |
|
✗ |
sendMessage(actionToServer); |
38 |
|
|
|
39 |
|
✗ |
_state.lastAction.action = action; |
40 |
|
✗ |
_state.lastAction.parameter = parameter; |
41 |
|
|
|
42 |
|
✗ |
saveMetrics(actionInfo); |
43 |
|
✗ |
} |
44 |
|
✗ |
catch (const ActionInfoException &e) |
45 |
|
|
{ |
46 |
|
✗ |
PRINT_ERROR(e.what()); |
47 |
|
✗ |
} |
48 |
|
|
// Bots moved or took a ressource, his environment changed |
49 |
|
✗ |
if (action == FORWARD || action == RIGHT || action == LEFT || action == TAKE || action == INCANTATION) |
50 |
|
✗ |
_state.metadata["should_update_env"] = "true"; |
51 |
|
✗ |
if (action == LOOK) |
52 |
|
✗ |
_state.metadata["should_update_env"] = "false"; |
53 |
|
✗ |
} |
54 |
|
|
|
55 |
|
✗ |
void ABot::saveMetrics(ActionInfo actionInfo) |
56 |
|
|
{ |
57 |
|
✗ |
auto it = std::find_if(_state.actionsData.begin(), _state.actionsData.end(), |
58 |
|
✗ |
[&actionInfo](const ActionInfo &ad) |
59 |
|
✗ |
{ return ad.getName() == actionInfo.getName(); }); |
60 |
|
✗ |
if (it == _state.actionsData.end()) |
61 |
|
|
{ |
62 |
|
✗ |
ActionInfo newActionInfo = actionInfo; |
63 |
|
|
|
64 |
|
✗ |
newActionInfo.parameter = actionInfo.parameter; |
65 |
|
✗ |
newActionInfo.count = 1; |
66 |
|
✗ |
_state.actionsData.push_back(newActionInfo); |
67 |
|
✗ |
} |
68 |
|
|
else |
69 |
|
|
{ |
70 |
|
✗ |
it->count += 1; |
71 |
|
|
} |
72 |
|
✗ |
} |
73 |
|
|
|
74 |
|
✗ |
void ABot::saveDataActions(const std::string &filename) |
75 |
|
|
{ |
76 |
|
✗ |
std::ofstream out(filename, std::ios_base::app); |
77 |
|
|
|
78 |
|
✗ |
out << "Iteration:" << _iteration << std::endl; |
79 |
|
✗ |
for (auto &action : _state.actionsData) |
80 |
|
|
{ |
81 |
|
✗ |
out << action.getName(); |
82 |
|
✗ |
if (!action.parameter.empty()) |
83 |
|
✗ |
out << " " << action.parameter; |
84 |
|
✗ |
out << ":" + std::to_string(action.count) << std::endl; |
85 |
|
|
} |
86 |
|
✗ |
out << "\n"; |
87 |
|
✗ |
} |
88 |
|
|
|
89 |
|
✗ |
void ABot::addBroadcastAction(const std::string message) |
90 |
|
|
{ |
91 |
|
✗ |
Message tmp(message); |
92 |
|
✗ |
tmp.format(tmp.content); |
93 |
|
✗ |
std::string messageToSent = tmp.content; |
94 |
|
✗ |
queue.push_back({[this, messageToSent]() |
95 |
|
✗ |
{ doAction(BROADCAST, messageToSent); }, "BROADCAST"}); |
96 |
|
✗ |
} |
97 |
|
|
|
98 |
|
✗ |
BotState ABot::getState() |
99 |
|
|
{ |
100 |
|
✗ |
return _state; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
✗ |
void ABot::setState(BotState state) |
104 |
|
|
{ |
105 |
|
✗ |
_state = state; |
106 |
|
✗ |
} |
107 |
|
|
|