-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cpp
More file actions
96 lines (84 loc) · 2.21 KB
/
Bot.cpp
File metadata and controls
96 lines (84 loc) · 2.21 KB
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
#include "Bot.h"
#include <chrono>
#include <iostream>
#include <GL/glut.h>
Bot::Bot(Tank t, int d): tank(t), difficulty(d)
{
keys.push_back(GLUT_KEY_LEFT);
keys.push_back(GLUT_KEY_UP);
keys.push_back(GLUT_KEY_RIGHT);
keys.push_back(GLUT_KEY_DOWN);
keys.push_back(32);
opDelay = 250/(1+difficulty);
perfDelay = 125/(1+difficulty);
shootKey = 32;
prevOp = std::chrono::system_clock::now();
prevPerf = std::chrono::system_clock::now();
}
void Bot::Tick()
{
tank.Tick();
EmulateKeyboard();
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
double delta = std::chrono::duration_cast<std::chrono::milliseconds>(now - prevPerf).count();
if(delta > perfDelay)
{
if(operationQueue.size() > 0)
{
int operation = operationQueue.front();
operationQueue.pop_front();
if(operation == shootKey)
{
tank.Keyboard(operation);
}
else
{
tank.SpecialKeyboard(operation);
}
prevPerf = now;
}
}
}
void Bot::Render()
{
tank.Render();
}
void Bot::EmulateKeyboard()
{
//проверить, прошло ли достаточно времени
//сгенерировать случайную длину цепочки
//добавить элементы цепочки в стек
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
double delta = std::chrono::duration_cast<std::chrono::milliseconds>(now - prevOp).count();
if(delta > opDelay)
{
//int length = rand() % 3 + 1; //генерируем цепочки длиной 1..4
int length = 1;
int operation = rand() % 5; //генерируем случайную операцию
for(int i = 0;i<length;i++)
{
operationQueue.push_back(keys[operation]);
}
prevOp = now;
}
}
void Bot::SetOpDelay(int delay)
{
opDelay = delay;
}
void Bot::SetPerfDelay(int delay)
{
perfDelay = delay;
}
int Bot::GetOpDelay()
{
return opDelay;
}
int Bot::GetPerfDelay()
{
return perfDelay;
}
Tank& Bot::GetTank()
{
return tank;
}