Skip to content

Commit 37cc5ed

Browse files
authored
Merge pull request #214 from njustesen/fix/auto-setup
Fix to forced setup
2 parents f578581 + 605faff commit 37cc5ed

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

botbowl/core/game.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,15 @@ def step(self, action=None) -> None:
210210
# Else continue procedure with no action
211211
self.action = None
212212

213-
214213
def refresh(self) -> None:
215214
"""
216215
Checks clocks and runs forced actions. Useful in called in human games.
217216
"""
218217
self.action = None
219218
if self.config.competition_mode:
220219
self._check_clocks()
221-
if self.action is not None:
222-
self.step(self.action)
220+
if not self.state.game_over and len(self.state.available_actions) == 0:
221+
self.step(None)
223222

224223
def _check_clocks(self) -> None:
225224
"""
@@ -247,14 +246,11 @@ def _check_clocks(self) -> None:
247246
action = None
248247

249248
# Take action if it doesn't end the turn
249+
if self.config.debug_mode:
250+
print(f"Forcing action: {self.action.to_json() if self.action is not None else 'None'}")
250251
if self.action is None or self.action.action_type not in [ActionType.END_TURN, ActionType.END_SETUP]:
251-
if self.config.debug_mode:
252-
print("Forced step action")
253252
done = self._one_step(action)
254253
else:
255-
if self.config.debug_mode:
256-
print(f"Forcing action: {self.action.to_json() if self.action is not None else 'None'}")
257-
self.action = action
258254
break
259255

260256
if not clock.is_running():
@@ -349,8 +345,17 @@ def _forced_action(self) -> Action:
349345
ActionType.DONT_USE_APOTHECARY]:
350346
for action in self.state.available_actions:
351347
if action.action_type == action_type:
352-
return Action(action_type)
348+
if action_type == ActionType.END_SETUP:
349+
if self.is_setup_legal(self.get_agent_team(self.actor)):
350+
return Action(action_type)
351+
else:
352+
return Action(action_type)
353353
# Take random action
354+
while True:
355+
action_choice = self.rnd.choice(self.state.available_actions)
356+
# Ignore PLACE_PLAYER actions
357+
if action_choice.action_type != botbowl.ActionType.PLACE_PLAYER:
358+
break
354359
action_choice = self.rnd.choice(self.state.available_actions)
355360
position = self.rnd.choice(action_choice.positions) if len(action_choice.positions) > 0 else None
356361
player = self.rnd.choice(action_choice.players) if len(action_choice.players) > 0 else None

tests/framework/test_forced_action.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
from botbowl.core.load import *
3+
from botbowl.core.game import Game
4+
5+
6+
def test_forced_action():
7+
config = load_config("gym-11")
8+
config.time_limits.turn = 0.01
9+
config.time_limits.secondary = 0.01
10+
config.competition_mode = True
11+
config.fast_mode = True
12+
ruleset = load_rule_set(config.ruleset)
13+
home = load_team_by_filename("human", ruleset)
14+
away = load_team_by_filename("human", ruleset)
15+
home_agent = Agent("human1", human=True)
16+
away_agent = Agent("human2", human=True)
17+
game = Game(1, home, away, home_agent, away_agent, config)
18+
game.init()
19+
game.step(Action(ActionType.START_GAME))
20+
for i in range(100):
21+
time.sleep(config.time_limits.turn * 1.5)
22+
game.refresh()
23+
assert game.state.game_over
24+
25+
26+
def test_forced_setup():
27+
config = load_config("gym-11")
28+
config.time_limits.turn = 0.1
29+
config.competition_mode = True
30+
ruleset = load_rule_set(config.ruleset)
31+
home = load_team_by_filename("human", ruleset)
32+
away = load_team_by_filename("human", ruleset)
33+
home_agent = Agent("human1", human=True)
34+
away_agent = Agent("human2", human=True)
35+
game = Game(1, home, away, home_agent, away_agent, config)
36+
game.init()
37+
game.step(Action(ActionType.START_GAME))
38+
game.step(Action(ActionType.HEADS))
39+
game.step(Action(ActionType.KICK))
40+
time.sleep(config.time_limits.turn * 1.5)
41+
game.refresh()
42+
assert len(game.get_players_on_pitch()) == 11
43+
time.sleep(config.time_limits.turn * 1.5)
44+
game.refresh()
45+
assert len(game.get_players_on_pitch()) == 22

0 commit comments

Comments
 (0)