Skip to content

Commit e0e8ed8

Browse files
authored
3304: Limit number of crease defenders (#3456)
1 parent ef3db1a commit e0e8ed8

3 files changed

Lines changed: 43 additions & 31 deletions

File tree

src/proto/parameters.proto

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ message DefensePlayConfig
525525
(bounds).min_double_value = 0.0,
526526
(bounds).max_double_value = 10.0
527527
];
528+
529+
// The max number of crease defender can be created at a moment
530+
required uint32 max_num_crease_defenders = 6
531+
[default = 1, (bounds).min_int_value = 1, (bounds).max_int_value = 3];
528532
}
529533

530534
// The distance at which a threat is considered "immediate" if there is only 1
@@ -634,12 +638,13 @@ message NetworkConfig
634638

635639
message CreaseDefenderConfig
636640
{
641+
// Unique steal config for crease defenders
642+
required DefenderStealConfig defender_steal_config = 1;
643+
637644
// The additional buffer length for each side of the goal
638645
// to determine if crease defender chipping is "towards goal"
639646
required double goal_post_offset_chipping = 4
640647
[default = 0.5, (bounds).min_double_value = 0.0, (bounds).max_double_value = 2.0];
641-
// Unique steal config for crease defenders
642-
required DefenderStealConfig defender_steal_config = 1;
643648
}
644649

645650
message PassDefenderConfig

src/software/ai/hl/stp/play/defense/defense_play_fsm.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@ void DefensePlayFSM::defendAgainstThreats(const Update& event)
1414
event.common.world_ptr->field(), event.common.world_ptr->friendlyTeam(),
1515
event.common.world_ptr->enemyTeam(), event.common.world_ptr->ball(), false);
1616

17+
auto defender_assignment_config =
18+
ai_config.defense_play_config().defender_assignment_config();
1719
auto assignments = getAllDefenderAssignments(
1820
enemy_threats, event.common.world_ptr->field(), event.common.world_ptr->ball(),
19-
ai_config.defense_play_config().defender_assignment_config());
21+
defender_assignment_config);
2022

2123
if (assignments.size() == 0)
2224
{
2325
return;
2426
}
2527

28+
unsigned int max_num_crease_defenders =
29+
defender_assignment_config.max_num_crease_defenders();
30+
2631
// Choose which defender assignments to assign defenders to based on number
2732
// of tactics available to set
2833
std::vector<DefenderAssignment> crease_defender_assignments;
@@ -42,17 +47,19 @@ void DefensePlayFSM::defendAgainstThreats(const Update& event)
4247
defender_assignment = assignments.front();
4348
}
4449

45-
if (defender_assignment.type == CREASE_DEFENDER)
50+
if (defender_assignment.type == CREASE_DEFENDER && max_num_crease_defenders > 0)
4651
{
4752
crease_defender_assignments.emplace_back(defender_assignment);
53+
max_num_crease_defenders--;
4854

4955
// If we have at least two available defenders, two defenders should
5056
// be assigned to the highest scoring crease defender assignment to better
5157
// block the shot cone of the most threatening enemy
52-
if (i == 0 && event.common.num_tactics >= 2)
58+
if (i == 0 && event.common.num_tactics >= 2 && max_num_crease_defenders > 0)
5359
{
5460
crease_defender_assignments.emplace_back(defender_assignment);
5561
i++;
62+
max_num_crease_defenders--;
5663
}
5764
}
5865
else

src/software/ai/hl/stp/tactic/defender/defender_fsm_base.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,48 @@ bool DefenderFSMBase::ballNearbyWithoutThreat(
99
const TbotsProto::BallStealMode& ball_steal_mode,
1010
const TbotsProto::DefenderStealConfig& defender_steal_config)
1111
{
12+
if (ball_steal_mode == TbotsProto::BallStealMode::IGNORE)
13+
{
14+
// Do nothing if stealing is disabled
15+
return false;
16+
}
17+
1218
Point robot_position = robot.position();
1319
Point ball_position = world_ptr->ball().position();
1420

1521
std::optional<Robot> nearest_friendly_to_ball =
1622
world_ptr->friendlyTeam().getNearestRobot(ball_position);
1723
std::optional<Robot> nearest_enemy_to_ball =
1824
world_ptr->enemyTeam().getNearestRobot(robot_position);
19-
if (ball_steal_mode == TbotsProto::BallStealMode::IGNORE)
20-
{
21-
// Do nothing if stealing is disabled
22-
return false;
23-
}
24-
else if (nearest_friendly_to_ball.has_value() &&
25-
robot.id() != nearest_friendly_to_ball.value().id())
25+
26+
if (nearest_friendly_to_ball.has_value() &&
27+
robot.id() != nearest_friendly_to_ball.value().id())
2628
{
2729
// Do nothing if this robot is not the closest to the ball. Resolves issue of
2830
// multiple simultaneous steals
2931
return false;
3032
}
31-
if (nearest_enemy_to_ball.has_value())
32-
{
33-
// Get the ball if ball is closer to robot than enemy threat by threshold ratio
34-
// and within max range
35-
double ball_distance_to_friendly = distance(robot_position, ball_position);
36-
double ball_distance_to_enemy =
37-
distance(nearest_enemy_to_ball.value().position(), ball_position);
38-
39-
bool ball_is_near_friendly =
40-
ball_distance_to_friendly <
41-
ball_distance_to_enemy *
42-
(1.0 - defender_steal_config.max_get_ball_ratio_threshold());
43-
bool ball_is_within_max_range =
44-
ball_distance_to_friendly <= defender_steal_config.max_get_ball_radius_m();
45-
bool ball_is_slow = world_ptr->ball().velocity().length() <=
46-
defender_steal_config.max_ball_speed_to_get_m_per_s();
4733

48-
return ball_is_near_friendly && ball_is_within_max_range && ball_is_slow;
49-
}
50-
else
34+
if (!nearest_enemy_to_ball.has_value())
5135
{
5236
return true;
5337
}
38+
39+
// Get the ball if ball is closer to robot than enemy threat by threshold ratio
40+
// and within max range
41+
double ball_distance_to_friendly = distance(robot_position, ball_position);
42+
double ball_distance_to_enemy =
43+
distance(nearest_enemy_to_ball.value().position(), ball_position);
44+
45+
bool ball_is_near_friendly =
46+
ball_distance_to_friendly <
47+
ball_distance_to_enemy * defender_steal_config.max_get_ball_ratio_threshold();
48+
bool ball_is_within_max_range =
49+
ball_distance_to_friendly <= defender_steal_config.max_get_ball_radius_m();
50+
bool ball_is_slow = world_ptr->ball().velocity().length() <=
51+
defender_steal_config.max_ball_speed_to_get_m_per_s();
52+
53+
return ball_is_near_friendly && ball_is_within_max_range && ball_is_slow;
5454
}
5555

5656
void DefenderFSMBase::prepareGetPossession(

0 commit comments

Comments
 (0)