-
Notifications
You must be signed in to change notification settings - Fork 1
/
Soccer.hpp
335 lines (299 loc) · 9.74 KB
/
Soccer.hpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#pragma once
#include <vector>
#include <mutex>
#include "Ball.hpp"
#include "Player.hpp"
#include "Timer.hpp"
struct Team;
struct Soccer {
std::vector<Player> players;
Ball ball;
std::recursive_mutex mtx;
Soccer(size_t team1sz=1, size_t team2sz=2):
players(),
team1(*this, team1sz, Team::RED_TEAM),
team2(*this, team2sz, Team::BLUE_TEAM)
{
for(int i = 0; i < team1sz + team2sz; ++i) {
Team &t = get_team(i);
float top1 = .1 * team1.size() / 2;
float top2 = .1 * team2.size() / 2;
if(get_team(i).id() == Team::RED_TEAM) {
players.push_back(Player(i, Team::RED_TEAM, {.1f, top1 - .1*i}));
} else {
players.push_back(Player(i, Team::BLUE_TEAM, {-.1f, top2 - .1*(i-team1.size())}));
}
players.back().unit.face(glm::vec3(0, 0, 0));
}
set_timer();
}
void set_timer() {
ball.set_timer();
for(auto &p : players) {
p.set_timer();
}
}
// gameplay
Timer timer;
enum class GameState {
IN_PROGRESS,
RED_START,
BLUE_START,
RED_THROWIN,
BLUE_THROWIN,
HALFTIME,
FINISHED
};
GameState state = GameState::RED_START;
struct Team {
static constexpr bool RED_TEAM = 0;
static constexpr bool BLUE_TEAM = 1;
Soccer &soccer;
size_t teamSize;
bool teamId;
Team(Soccer &soccer, size_t teamSize, bool id):
soccer(soccer), teamSize(teamSize), teamId(id)
{}
size_t size() const {
return teamSize;
}
bool id() const {
return teamId;
}
Player &operator[](size_t i) {
if(!teamId) {
return soccer.players[i];
} else {
return soccer.players[soccer.team1.size() + i];
}
}
Player operator[](size_t i) const {
if(!teamId) {
return soccer.players[i];
} else {
return soccer.players[soccer.team1.size() + i];
}
}
bool operator==(const Team &other) const {
return id() == other.id();
}
decltype(auto) begin() const { return soccer.players.begin() + ((teamId==RED_TEAM) ? 0 : soccer.team1.size()); }
decltype(auto) begin() { return soccer.players.begin() + ((teamId==RED_TEAM) ? 0 : soccer.team1.size()); }
decltype(auto) end() const {
if(teamId == RED_TEAM) {
if(soccer.team2.size())return soccer.players.end();
else return soccer.players.begin() + size();
} else {
return soccer.players.end();
}
}
decltype(auto) end() {
if(teamId == RED_TEAM) {
if(soccer.team2.size())return soccer.players.end();
else return soccer.players.begin() + size();
} else {
return soccer.players.end();
}
}
};
Team team1, team2;
void idle(Timer::time_t curtime) {
std::lock_guard<std::recursive_mutex> guard(mtx);
timer.set_time(curtime);
idle_control();
ball.idle(timer.current_time);
for(auto &p: players) {
p.idle(timer.current_time);
}
}
void idle_control() {
if(is_active_player(ball.owner()) && !ball.is_loose()) {
auto &p = get_player(ball.owner());
/* printf("controlling fully\n"); */
ball.position() = p.unit.point_offset(p.possession_offset);
ball.position().z = p.unit.height() + ball.default_height;
}
int new_owner = find_best_possession(ball);
set_control_player(new_owner);
}
bool is_active_player(int playerId) const {
return playerId != Ball::NO_OWNER;
}
const Unit::loc_t single_player_pass_point = glm::vec3(.0, 2./3, 0);
bool is_able_to_tackle(float val) {
return !std::isnan(val);
}
int find_best_possession(Ball &ball) {
// if noone controls, closest gets the ball
// if someone controls, closest other than the owner or nothing controls the ball
Unit::loc_t ball_pos = ball.position();
int owner = ball.owner();
double bcp = NAN; // best control potential
for(auto &p : players) {
if(p.is_owner(ball))continue;
if(is_active_player(ball.owner()) && get_team(ball.owner()).id() == p.team)continue;
double pcp = p.get_control_potential(ball);
if(!is_able_to_tackle(pcp))continue;
if(!is_able_to_tackle(bcp) || bcp > pcp) {
bcp = pcp;
owner = p.id();
}
}
// case when the current owner no longer controls the ball
if(is_active_player(ball.owner()) && !is_able_to_tackle(bcp)) {
auto &p = get_player(ball.owner());
double pcp = p.get_control_potential(ball);
if(!is_able_to_tackle(pcp)) {
owner = Ball::NO_OWNER;
}
}
return owner;
}
void set_control_player(int playerId) {
// lose the ball if we are a player and we lost it
if(is_active_player(ball.owner())) {
auto &p = get_player(ball.owner());
if(!is_active_player(playerId)) {
p.timestamp_dispossess(ball, Player::CANT_HOLD_BALL_SHOT);
} else if(playerId != ball.owner()) {
p.timestamp_dispossess(ball, Player::CANT_HOLD_BALL_DISPOSSESS);
} else if(players[playerId].is_sliding_fast()) {
p.timestamp_slowdown(Player::SLOWDOWN_SLID);
}
}
// get it if we are player and new owner
if(ball.owner() != playerId && is_active_player(playerId)) {
auto &p = get_player(playerId);
p.timestamp_got_ball(ball);
if(p.is_sliding_fast()) {
p.timestamp_dispossess(ball, Player::CANT_HOLD_BALL_SHOT);
ball.unit.face(p.unit.facing_angle(p.unit.dest));
ball.unit.moving_speed = p.unit.moving_speed;
} else if(p.is_going_up()) {
Unit::loc_t dest = single_player_pass_point;
int pass_to = get_pass_destination(p.id());
if(is_active_player(pass_to)) {
dest = players[pass_to].possession_point();
} else {
dest = single_player_pass_point;
}
float dist = glm::distance(ball.unit.pos, dest);
float time = std::sqrt(2 * ball.unit.height() / ball.G) * .1;
float speed = std::fmax(ball.unit.moving_speed, 350 * Unit::GAUGE);
if(dist < speed * time) {
ball.vertical_speed = 0;
speed = dist / time;
} else {
ball.vertical_speed = std::fmin(10. * Unit::GAUGE, 10. * Unit::GAUGE * ball.G * .5 * dist / speed);
}
ball.unit.moving_speed = speed;
ball.is_in_air = true;
ball.unit.facing_dest = ball.unit.facing_angle(dest);
p.timestamp_dispossess(ball, Player::CANT_HOLD_BALL_SHOT);
}
}
}
Team &get_team(int playerId) {
return (playerId < team1.size()) ? team1 : team2;
}
Player &get_player(int playerId) {
ASSERT(is_active_player(playerId));
return players[playerId];
}
const Player &get_player(int playerId) const {
ASSERT(is_active_player(playerId));
return players[playerId];
}
Unit &get_unit(int unit_id) {
return (unit_id == Ball::NO_OWNER) ? ball.unit : get_player(unit_id).unit;
}
int get_pass_destination(int playerId) {
if(!is_active_player(playerId))return playerId;
Team &team = get_team(playerId);
int ind_pass_to = Ball::NO_OWNER;
double range = NAN;
for(int i = 0; i < team.size(); ++i) {
if(playerId == team[i].id())continue;
float dist = glm::distance(ball.unit.pos, team[i].unit.pos);
if(std::isnan(range) || dist < range) {
range = dist;
ind_pass_to = team[i].id();
}
}
return ind_pass_to;
}
void z_action(int playerId) {
if(!is_active_player(playerId))return;
auto &p = get_player(playerId);
if(!p.can_pass())return;
p.timestamp_passed();
if(!p.is_owner(ball))return;
if(!p.is_jumping()) {
ball.is_in_air = false;
Unit::loc_t dest = single_player_pass_point;
int pass_to = get_pass_destination(ball.owner());
if(is_active_player(pass_to)) {
dest = players[pass_to].possession_point();
}
p.kick_the_ball(ball, p.running_speed * 1.8, .0, ball.unit.facing_angle(dest));
} else {
ball.is_in_air = true;
p.kick_the_ball(ball, p.running_speed * 1.8, .0, p.unit.facing);
}
}
void x_action(int playerId, float direction) {
if(!is_active_player(playerId))return;
auto &p = get_player(playerId);
if(playerId == ball.owner() && !p.is_sliding()) {
ball.is_in_air = true;
p.unit.face(direction);
p.kick_the_ball(ball, 300. * Unit::GAUGE, 20. * Unit::GAUGE, direction);
} else if(p.can_slide()) {
p.timestamp_slide();
Unit::vec_t slide_vec(std::cos(direction), std::sin(direction), 0);
slide_vec *= p.slide_speed * p.slide_duration;
p.unit.slide(p.unit.pos + slide_vec, p.slide_duration);
}
}
void c_action(int playerId, Unit::loc_t dest) {
if(!is_active_player(playerId))return;
auto &p = get_player(playerId);
float direction = p.unit.facing_angle(dest);
if(playerId == ball.owner() && !p.is_jumping()) {
ball.reset_height();
ball.is_in_air = true;
p.unit.face(direction);
float dist = glm::distance(ball.unit.pos, dest);
float vspeed = 30. * Unit::GAUGE;
float speed = std::min(522.f * Unit::GAUGE, 5.f * p.G * dist / vspeed);
p.kick_the_ball(ball, speed, vspeed, direction);
p.timestamp_slowdown(Player::SLOWDOWN_SHOT);
} else {
p.unit.face(direction);
}
}
void v_action(int playerId) {
if(!is_active_player(playerId))return;
auto &p = get_player(playerId);
if(p.is_owner(ball)) {
p.jump(15. * Unit::GAUGE);
} else {
p.jump(20. * Unit::GAUGE);
}
}
void f_action(int playerId, float direction) {
if(!is_active_player(playerId))return;
auto &p = get_player(playerId);
p.unit.face(direction);
}
void s_action(int playerId) {
if(!is_active_player(playerId))return;
auto &p = get_player(playerId);
p.unit.stop();
}
void m_action(int playerId, Unit::loc_t dest) {
if(!is_active_player(playerId))return;
auto &p = get_player(playerId);
p.unit.move(dest);
}
};