Coverage report for ai


src/
File: src/bots/Forker.cpp
Date: 2024-06-25 10:57:00
Lines:
0/48
0.0%
Functions:
0/8
0.0%
Branches:
0/54
0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy/ai
4 ** File description:
5 ** Forker.cpp
6 */
7
8 #include "Forker.hpp"
9 #include "SimpleBot.hpp"
10
11 void Forker::initChild()
12 {
13 std::cout << "🍴✅ Forker initialized" << std::endl;
14 }
15
16 void Forker::forkNewBot()
17 {
18 pid_t pid = fork();
19
20 if (pid == -1)
21 {
22 std::cerr << "Fork failed" << std::endl;
23 return;
24 }
25
26 if (pid == 0)
27 {
28 // TODO: use cpp function
29 // without term
30 // execl("./zappy_ai", "./zappy_ai", "-p", std::to_string(_port).c_str(), "-n", _teamName.c_str(), "-h", _host.c_str(), nullptr);
31
32 // with term that close
33 // execl("/usr/bin/gnome-terminal", "/usr/bin/gnome-terminal", "--", "./zappy_ai", "-p", std::to_string(_port).c_str(), "-n", _teamName.c_str(), "-h", _host.c_str(), nullptr);
34
35 // with term that stay open
36 execl("/usr/bin/gnome-terminal", "/usr/bin/gnome-terminal", "--", "bash", "-c",
37 ("trap 'echo \"[DEBUG]\"; kill -INT $$' SIGINT; ./zappy_ai -p " + std::to_string(_port) + " -n " + _teamName + " -h " + _host + "; exec bash").c_str(), nullptr);
38
39 std::cerr << "execl failed" << std::endl;
40 while (1)
41 ;
42 }
43 else
44 {
45 std::string msg = "you_are_bot=" + std::to_string(_idBot) + "your_job=" + jobMap[SIMPLE_BOT] + "currentMessageId=" + std::to_string(_currentMessageId);
46 // sleep(1);
47 addBroadcastAction(msg);
48
49 wait(nullptr);
50 }
51 }
52
53 void Forker::updateStrategy()
54 {
55 static unsigned int limitFork = 2; // TODO: it is to debug
56 static bool verifySlot = true;
57
58 std::cout << "🍴 Forker updateStrategy" << std::endl;
59 if (verifySlot)
60 {
61 _state.slot = 0;
62 queue.push_back(std::make_pair([&]()
63 { doAction(CONNECT_NBR, ""); }, "CONNECT_NBR"));
64 verifySlot = false;
65 }
66 else if (handleSurvive())
67 return;
68 else if ((_state.state == FORKED || _state.slot > 0) && limitFork > 0)
69 {
70 forkNewBot();
71 // TODO: try to put a sleep because server doesn't handle multiple fork in chain sleep(2);
72 _idBot++;
73 _state.slot--;
74 limitFork--;
75 }
76 else if (_state.slot == 0 && limitFork > 0)
77 {
78 queue.push_back(std::make_pair([&]()
79 { doAction(FORK, ""); }, "FORK"));
80 }
81 }
82
83 bool Forker::handleSurvive()
84 {
85 static int searchFood = 0;
86 const int limitFood = 50;
87
88 if (_iteration % 40 == 0)
89 {
90 _state.state = STANDARD;
91 queue.push_back({[&]()
92 { doAction(INVENTORY, ""); }, "INVENTORY"});
93 return true;
94 }
95
96 if (_state.ressources.food < limitFood)
97 {
98 // TODO: we want differant searchFood for each level ?
99 searchFood = 250;
100 }
101 if (searchFood > 0)
102 {
103 if (searchFood == 1)
104 queue.push_back({[&]()
105 { doAction(INVENTORY, ""); }, "INVENTORY"});
106 else
107 survive();
108 _state.state = STANDARD;
109 _state.pattern = "survive";
110 searchFood--;
111 return true;
112 }
113 return false;
114 }
115