Skip to content

Commit 9a0445b

Browse files
committed
"Updated BhvStarterKickPlanner to import and use other behaviors, and modified KickDecisionMaker to use BhvStarterKickPlanner."
1 parent 172b078 commit 9a0445b

File tree

7 files changed

+552
-5
lines changed

7 files changed

+552
-5
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from src.interfaces.IBehavior import IBehavior
2+
from src.interfaces.IAgent import IAgent
3+
from pyrusgeom.soccer_math import *
4+
from pyrusgeom.geom_2d import *
5+
from src.utils.tools import Tools
6+
from pyrusgeom import vector_2d
7+
from service_pb2 import *
8+
9+
class ClearBall(IBehavior):
10+
11+
def __init__(self):
12+
pass
13+
14+
15+
def execute(self, agent: IAgent):
16+
wm = agent.wm
17+
ball_pos = Vector2D(wm.ball.position.x, wm.ball.position.y)
18+
target = Vector2D(agent.server_params.pitch_half_length, 0.0)
19+
if ball_pos.x() > -25.0 :
20+
if ball_pos.dist(Vector2D(0.0, -agent.server_params.pitch_half_width)) < ball_pos.dist(Vector2D(0.0, agent.server_params.pitch_half_width)) :
21+
target = Vector2D(0.0,-34.0)
22+
else:
23+
target = Vector2D(0.0,34.0)
24+
else :
25+
if abs(ball_pos.y()) < 10 and ball_pos.x() < -10.0 :
26+
if ball_pos.y() > 0.0 :
27+
target = Vector2D(-agent.server_params.pitch_half_length, 20.0)
28+
else :
29+
target = Vector2D(-agent.server_params.pitch_half_length, -20.0)
30+
else:
31+
if ball_pos.y() > 0.0 :
32+
target = Vector2D(ball_pos.x(), 34.0)
33+
else :
34+
target = Vector2D(ball_pos.x(), -34.0)
35+
return PlayerAction(body_smart_kick=Body_SmartKick(target_point=RpcVector2D(x=target.x(), y=target.y()),
36+
first_speed=2.7,
37+
first_speed_threshold=2.7,
38+
max_steps=2))
39+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from src.interfaces.IBehavior import IBehavior
2+
from src.interfaces.IAgent import IAgent
3+
from src.utils.tools import Tools
4+
from pyrusgeom.vector_2d import Vector2D
5+
from pyrusgeom.sector_2d import Sector2D
6+
from service_pb2 import *
7+
8+
9+
class Dribble(IBehavior):
10+
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent):
15+
wm = agent.wm
16+
ball_pos = Vector2D(wm.ball.position.x, wm.ball.position.y)
17+
dribble_angle = (Vector2D(52.5, 0) - ball_pos).th().degree()
18+
dribble_speed = 0.8
19+
dribble_threshold = 0.7
20+
dribble_sector = Sector2D(ball_pos, 0, 3, dribble_angle - 15, dribble_angle + 15)
21+
22+
if not Tools.ExistOpponentIn(agent , dribble_sector):
23+
Target = Vector2D.polar2vector(3, dribble_angle) + ball_pos
24+
return PlayerAction(body_smart_kick=Body_SmartKick(target_point=RpcVector2D(x=Target.x(), y=Target.y()),
25+
first_speed=dribble_speed,
26+
first_speed_threshold=dribble_threshold,
27+
max_steps=2))
28+
return
29+
30+
31+
32+
33+

src/behaviors/bhv_starter_kick_planner.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,39 @@
33
from pyrusgeom.soccer_math import *
44
from pyrusgeom.geom_2d import *
55
from service_pb2 import *
6-
6+
from src.behaviors.bhv_starter_clearball import ClearBall
7+
from src.behaviors.bhv_starter_pass import Pass
8+
from src.behaviors.bhv_starter_dribble import Dribble
9+
from src.behaviors.bhv_starter_shoot import Shoot
10+
from src.utils.tools import Tools
711

812
class BhvStarterKickPlanner(IBehavior):
913
def __init__(self):
10-
pass
14+
self.shoot = Shoot()
15+
self.clear_ball = ClearBall()
16+
self.dribble = Dribble()
17+
self.pass_ball = Pass()
1118

1219
def execute(self, agent: IAgent):
1320
agent.logger.debug("BhvStarterKickPlanner.execute")
1421
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
15-
assert isinstance(agent, SamplePlayerAgent)
16-
raise NotImplementedError("BhvStarterKickPlanner.execute not implemented")
22+
actions = []
23+
actions += [shoot] if (shoot := self.shoot.execute(agent)) is not None else []
24+
opps = Tools.OpponentsFromSelf(agent)
25+
nearest_opp = opps[0] if opps else None
26+
nearest_opp_dist = nearest_opp.dist_from_self if nearest_opp else 1000.0
27+
28+
if nearest_opp_dist < 10:
29+
actions += [passing] if (passing := self.pass_ball.execute(agent)) is not None else []
30+
31+
actions += [dribble] if (dribble := self.dribble.execute(agent)) is not None else []
32+
33+
if nearest_opp_dist > 2.5:
34+
actions.append(PlayerAction(body_hold_ball=Body_HoldBall()))
35+
36+
actions.append(self.clear_ball.execute(agent))
37+
38+
#Sending actions' queue
39+
for i in actions:
40+
if not i == []:
41+
agent.add_action(i)

src/behaviors/bhv_starter_pass.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from src.interfaces.IBehavior import IBehavior
2+
from src.interfaces.IAgent import IAgent
3+
from pyrusgeom.soccer_math import *
4+
from pyrusgeom.geom_2d import *
5+
from src.utils.tools import Tools
6+
from service_pb2 import *
7+
8+
9+
class Pass(IBehavior):
10+
11+
def __init__(self):
12+
pass
13+
14+
def execute(self, agent: IAgent) -> PlayerAction:
15+
16+
wm = agent.wm
17+
target = []
18+
ball_pos = Vector2D(wm.ball.position.x, wm.ball.position.y)
19+
self_pos = Vector2D(wm.self.position.x, wm.self.position.y)
20+
for i in wm.teammates :
21+
if i == None or i.uniform_number == wm.self.uniform_number or i.uniform_number < 0:
22+
continue
23+
tm_pos = Vector2D(i.position.x, i.position.y)
24+
if tm_pos.dist(ball_pos) > 30.0 :
25+
continue
26+
if self_pos.dist(tm_pos) < 2.0:
27+
continue
28+
check_root = Sector2D(ball_pos, 1.0, tm_pos.dist(ball_pos) + 3.0, (tm_pos - ball_pos).th().degree() - 15.0, (tm_pos - ball_pos).th().degree() + 15.0)
29+
if not Tools.ExistOpponentIn(agent, check_root):
30+
target.append(i)
31+
32+
if not target == []:
33+
best_target = target[0]
34+
for i in target:
35+
if i.position.x > best_target.position.x:
36+
best_target = i
37+
if not wm.game_mode_type == GameModeType.PlayOn:
38+
return PlayerAction(body_smart_kick=Body_SmartKick(target_point=best_target.position, first_speed=2.7, first_speed_threshold=2.5, max_steps=1))
39+
else :
40+
return PlayerAction(body_smart_kick=Body_SmartKick(target_point=best_target.position, first_speed=2.5, first_speed_threshold=2.5, max_steps=1))
41+
42+
return
43+

src/behaviors/bhv_starter_shoot.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from src.interfaces.IBehavior import IBehavior
2+
from src.interfaces.IAgent import IAgent
3+
from pyrusgeom.vector_2d import Vector2D
4+
from service_pb2 import *
5+
6+
7+
8+
class Shoot(IBehavior):
9+
def __init__(self):
10+
pass
11+
12+
def execute(self, agent: IAgent):
13+
wm = agent.wm
14+
ball_pos = Vector2D(wm.ball.position.x, wm.ball.position.y)
15+
ball_max_velocity = agent.server_params.ball_speed_max
16+
17+
center_goal = Vector2D ( agent.server_params.pitch_half_length, 0.0 )
18+
right_goal = Vector2D ( agent.server_params.pitch_half_length , agent.server_params.goal_width / 2.0 ) # Lower Pole
19+
left_goal = Vector2D ( agent.server_params.pitch_half_length , -(agent.server_params.goal_width / 2.0) ) # Upper Pole
20+
21+
if ball_pos.dist(center_goal) <= 25.0:
22+
23+
if left_goal.dist(ball_pos) < right_goal.dist(ball_pos):
24+
agent.add_log_message(LoggerLevel.SHOOT, f": Shooting to {left_goal}", agent.wm.self.position.x, agent.wm.self.position.y - 2, '\033[31m')
25+
return PlayerAction(body_smart_kick=Body_SmartKick(target_point=RpcVector2D(x=left_goal.x(), y=left_goal.y()),
26+
first_speed=ball_max_velocity ,
27+
first_speed_threshold=0.1 ,
28+
max_steps=2))
29+
else :
30+
agent.add_log_message(LoggerLevel.SHOOT, f": Shooting to {right_goal}", agent.wm.self.position.x, agent.wm.self.position.y - 2, '\033[31m')
31+
return PlayerAction(body_smart_kick=Body_SmartKick(target_point=RpcVector2D(x=right_goal.x(), y=right_goal.y()),
32+
first_speed=ball_max_velocity ,
33+
first_speed_threshold=0.1 ,
34+
max_steps=2))
35+
return
36+
37+
38+
39+

src/decision_makers/kick_decision_maker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
class KickDecisionMaker(IDecisionMaker):
1111
def __init__(self):
1212
self.bhv_kick_planner = BhvKickPlanner()
13-
# self.bhv_kick_planner = BhvStarterKickPlanner()
13+
self.bhv_kick_planner = BhvStarterKickPlanner()
1414

1515
def make_decision(self, agent: IAgent):
1616
agent.logger.debug("--- WithBallDecisionMaker ---")
1717
from src.sample_player_agent import SamplePlayerAgent # Local import to avoid circular import
1818
assert isinstance(agent, SamplePlayerAgent)
19+
#self.bhv_kick_planner.execute(agent)
1920
self.bhv_kick_planner.execute(agent)

0 commit comments

Comments
 (0)