-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
127 lines (110 loc) · 3.53 KB
/
utils.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
#ifndef UTILS
#define UTILS
#include <SFML/Network.hpp>
#include <array>
#include <dirent.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define len(a) sizeof a / sizeof a[0];
// 22 bytes
struct Configuration {
float frontFriction = .2;
float sideFriction = .99;
float acc = 0.5;
float dec = 0.5;
float rotationSpeed = 5.0;
float maxVelocity = 1.;
int timeout = 10;
int framesPerAnswer = 1;
int numOfPlayers = 4;
array<sf::Vector2f, 3> checkpoints = {
sf::Vector2f(rand()%900+50, rand()%900+50), sf::Vector2f(rand()%900+50, rand()%900+50), sf::Vector2f(rand()%900+50, rand()%900+50)};
int numOfCheckpoints = checkpoints.size();
friend sf::Packet &operator<<(sf::Packet &os, const Configuration &c) {
os << c.frontFriction << c.sideFriction << c.acc << c.dec << c.rotationSpeed
<< c.maxVelocity << c.timeout << c.framesPerAnswer << c.numOfPlayers
<< c.numOfCheckpoints;
for (auto &checkpoint : c.checkpoints) {
os << checkpoint.x << checkpoint.y;
}
return os;
}
friend sf::Packet &operator>>(sf::Packet &os, Configuration &c) {
os >> c.frontFriction >> c.sideFriction >> c.acc >> c.dec >>
c.rotationSpeed >> c.maxVelocity >> c.timeout >> c.framesPerAnswer >>
c.numOfPlayers >> c.numOfCheckpoints;
for (auto &checkpoint : c.checkpoints) {
os >> checkpoint.x >> checkpoint.y;
}
return os;
}
friend ostream &operator<<(ostream &os, const Configuration &c) {
os << " " << c.frontFriction << " " << c.sideFriction << " " << c.acc << " "
<< c.dec << " " << c.rotationSpeed << " " << c.maxVelocity << " "
<< c.timeout << " " << c.framesPerAnswer << " " << c.numOfPlayers;
for (auto &checkpoint : c.checkpoints) {
os << checkpoint.x << checkpoint.y;
}
return os;
}
};
// 28 bytes
struct PlayerState {
sf::Vector2f pos;
sf::Vector2f vel;
float rotation;
int lap;
int checkpoint;
int health;
friend sf::Packet &operator<<(sf::Packet &os, const PlayerState &p) {
os << p.pos.x << p.pos.y << p.vel.x << p.vel.y << p.rotation << p.lap
<< p.checkpoint << p.health;
return os;
}
friend sf::Packet &operator>>(sf::Packet &os, PlayerState &p) {
return os >> p.pos.x >> p.pos.y >> p.vel.x >> p.vel.y >> p.rotation >>
p.lap >> p.checkpoint >> p.health;
}
friend ostream &operator<<(ostream &os, const PlayerState &p) {
os << p.pos.x << ", " << p.pos.y << ", " << p.vel.x << ", " << p.vel.y << ", "
<< p.rotation << ", " << p.lap << ", " << p.checkpoint << ", " << p.health;
return os;
}
};
struct Response {
int gas = 0; // 0-100
int rotate = 0; // {-1, 0, 1}
bool boost = 0;
friend sf::Packet &operator<<(sf::Packet &os, const Response &r) {
os << r.gas << r.rotate << r.boost;
return os;
}
friend sf::Packet &operator>>(sf::Packet &os, Response &r) {
return os >> r.gas >> r.rotate >> r.boost;
}
friend ostream &operator<<(ostream &os, const Response &r) {
os << r.gas << ", " << r.rotate << ", " << r.boost;
return os;
}
};
vector<string> getAllFilesInDirectory(char *path) {
DIR *dir;
struct dirent *diread;
vector<string> files;
if ((dir = opendir(path)) != nullptr) {
while ((diread = readdir(dir)) != nullptr) {
files.push_back(diread->d_name);
}
closedir(dir);
} else {
perror("opendir");
cout << "Folder " << path << " not found." << endl;
return files;
}
files.erase(files.begin()); // . directory
files.erase(files.begin()); // .. directory
return files;
}
#endif