-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
195 lines (173 loc) · 5.14 KB
/
Client.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
#ifndef CLIENT
#define CLIENT
#include "algebra.cpp"
#include "utils.cpp"
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
enum class Version { human, simple, adversary };
class Client {
private:
sf::IpAddress serverIP;
unsigned short serverPort;
sf::UdpSocket *socket;
Configuration config;
vector<PlayerState> playerStates = {};
Version version;
int num;
public:
Client(sf::IpAddress serverIP, unsigned short serverPort, Version version,
int num = 0) {
this->serverIP = serverIP;
this->serverPort = serverPort;
this->socket = new sf::UdpSocket();
this->socket->bind(sf::Socket::AnyPort);
this->version = version;
this->num = num;
}
void connectToServer() {
sf::Packet packet;
char ipAddress[20];
unsigned int port = this->socket->getLocalPort();
// send ip and port to server
strcpy(ipAddress, sf::IpAddress::getLocalAddress().toString().c_str());
packet << ipAddress << port;
if (this->socket->send(packet, this->serverIP, this->serverPort) !=
sf::Socket::Done) {
cout << "Can't connect to server" << endl;
}
sf::IpAddress senderIP;
unsigned short senderPort;
// receive config from server
packet.clear();
this->socket->receive(packet, senderIP, senderPort);
packet >> this->config;
}
void waitForGameState() {
sf::IpAddress senderIP;
unsigned short senderPort;
sf::Packet packet;
PlayerState playerState;
playerStates.clear();
if (this->socket->receive(packet, senderIP, senderPort) !=
sf::Socket::Done) {
cout << "error receiving game state" << endl;
}
for (int i = 0; i < config.numOfPlayers; i++) {
packet >> playerState;
this->playerStates.push_back(playerState);
}
}
Response calculateResponseBot() {
Response response;
sf::Vector2f nextCheckpoint =
this->config.checkpoints[this->playerStates[this->num].checkpoint];
sf::Vector2f myPos = this->playerStates[this->num].pos;
sf::Vector2f relVec = nextCheckpoint - myPos;
relVec.y = -relVec.y;
float myRot = this->playerStates[this->num].rotation;
float turn = myRot - vectorRotation(relVec);
if (abs(turn) > 180.f) {
turn = -turn;
}
if (turn == 0) turn = 10;
float gas = abs(100.f / turn);
response.rotate = -sign(turn, 1.f);
response.gas = gas;
return response;
}
Response calculateResponseAdversary() {
// drive around if you won
int lastLeft = -1;
for (auto const &p : this->playerStates){
if (p.health > 0){
lastLeft++;
}
}
if (lastLeft > 0){ return calculateResponseBot(); }
Response response;
sf::Vector2f target;
float minDistance = MAXFLOAT;
for (int i = 0; i<this->config.numOfPlayers; i++){
if (i == this->num or this->playerStates[i].health <= 0){ continue; }
float tmp = distance(this->playerStates[this->num].pos, this->playerStates[i].pos);
if (tmp < minDistance){
minDistance = tmp;
target = this->playerStates[i].pos;
}
}
sf::Vector2f myPos = this->playerStates[this->num].pos;
sf::Vector2f relVec = target - myPos;
relVec.y = -relVec.y;
float myRot = this->playerStates[this->num].rotation;
float turn = myRot - vectorRotation(relVec);
if (abs(turn) > 180.f) {
turn = -turn;
}
if (turn == 0) turn = 10;
response.rotate = -sign(turn, 1.f);
response.gas = 100;
return response;
}
Response calculateResponse() {
sf::Clock clock;
clock.restart();
sf::Time timeout = sf::milliseconds(config.timeout);
sf::Time elapsed = clock.getElapsedTime();
vector<vector<sf::Keyboard::Key>> keys;
keys.push_back({sf::Keyboard::Up, sf::Keyboard::Left, sf::Keyboard::Right});
keys.push_back({sf::Keyboard::W, sf::Keyboard::A, sf::Keyboard::D});
keys.push_back({sf::Keyboard::I, sf::Keyboard::J, sf::Keyboard::L});
Response response;
bool pressed = false;
while (elapsed < timeout) {
if (sf::Keyboard::isKeyPressed(keys[this->num][0])) {
response.gas = 100;
pressed = true;
}
if (sf::Keyboard::isKeyPressed(keys[this->num][1])) {
response.rotate = -1;
pressed = true;
}
if (sf::Keyboard::isKeyPressed(keys[this->num][2])) {
response.rotate = 1;
pressed = true;
}
if (pressed) {
break;
}
elapsed = clock.getElapsedTime();
}
return response;
}
void sendResponse(Response response) {
sf::Packet packet;
packet << response;
this->socket->send(packet, this->serverIP, this->serverPort);
}
void run() {
connectToServer();
Response response;
while (true) {
waitForGameState();
switch(this->version){
case Version::human:
response = calculateResponse();
break;
case Version::simple:
response = calculateResponseBot();
break;
case Version::adversary:
response = calculateResponseAdversary();
}
sendResponse(response);
}
}
};
#endif