Permalink
Browse files

Merge summon_during/summon_after and reinforce_during/reinforce_after

This eliminates a bunch of duplicated code, at the cost of a few
extra conditionals within those methods.
  • Loading branch information...
1 parent 3b8ec06 commit ec63bee437bc8dc2b1611b85ae2f93d7f526da51 @dripton committed Aug 25, 2012
Showing with 35 additions and 66 deletions.
  1. +11 −16 slugathon/ai/AIClient.py
  2. +4 −10 slugathon/ai/Bot.py
  3. +20 −40 slugathon/ai/CleverBot.py
View
@@ -461,11 +461,10 @@ def update(self, observed, action, names):
if legion:
if legion.player.name == self.playername:
if legion == game.defender_legion:
- reactor.callLater(self.delay, self.ai.reinforce_during,
- game)
+ reactor.callLater(self.delay, self.ai.reinforce, game)
else:
- reactor.callLater(self.delay,
- self.ai.summon_angel_during, game)
+ reactor.callLater(self.delay, self.ai.summon_angel,
+ game)
else:
logging.info("game.battle_active_legion not found")
@@ -491,8 +490,7 @@ def update(self, observed, action, names):
reactor.callLater(self.delay, self.ai.recruit, game)
elif game.phase == Phase.FIGHT:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.reinforce_during,
- game)
+ reactor.callLater(self.delay, self.ai.reinforce, game)
else:
reactor.callLater(self.delay,
self.ai.choose_engagement, game)
@@ -538,8 +536,7 @@ def update(self, observed, action, names):
self.update_creatures(game)
if action.playername == self.playername:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.summon_angel_during,
- game)
+ reactor.callLater(self.delay, self.ai.summon_angel, game)
else:
reactor.callLater(self.delay, self.ai.choose_engagement,
game)
@@ -555,8 +552,7 @@ def update(self, observed, action, names):
self.update_creatures(game)
if action.playername == self.playername:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.summon_angel_during,
- game)
+ reactor.callLater(self.delay, self.ai.summon_angel, game)
else:
reactor.callLater(self.delay, self.ai.choose_engagement,
game)
@@ -565,8 +561,7 @@ def update(self, observed, action, names):
game = self.name_to_game(action.game_name)
if action.playername == self.playername:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.summon_angel_during,
- game)
+ reactor.callLater(self.delay, self.ai.summon_angel, game)
else:
reactor.callLater(self.delay, self.ai.choose_engagement,
game)
@@ -587,17 +582,17 @@ def update(self, observed, action, names):
legion = game.attacker_legion
if (legion.markerid == action.winner_markerid and
legion.can_summon):
- reactor.callLater(self.delay,
- self.ai.summon_angel_after, game)
+ reactor.callLater(self.delay, self.ai.summon_angel,
+ game)
return
else:
if game.defender_legion:
legion = game.defender_legion
if legion.player.name == self.playername:
if (legion.markerid == action.winner_markerid and
legion.can_recruit):
- reactor.callLater(self.delay,
- self.ai.reinforce_after, game)
+ reactor.callLater(self.delay, self.ai.reinforce,
+ game)
return
if game.active_player.name == self.playername:
reactor.callLater(self.delay, self.ai.choose_engagement, game)
View
@@ -46,17 +46,11 @@ def carry(game, striker_name, striker_hexlabel, target_name,
def recruit(game):
"""Recruit during the Muster phase."""
- def reinforce_during(game):
- """Muster a reinforcement during battle."""
+ def reinforce(game):
+ """Muster a reinforcement during or after battle."""
- def reinforce_after(game):
- """Muster a reinforcement after battle."""
-
- def summon_angel_during(game):
- """Summon angel, during battle."""
-
- def summon_angel_after(game):
- """Summon angel, after battle."""
+ def summon_angel(game):
+ """Summon an angel, during or after battle."""
def acquire_angels(game, markerid, num_angels, num_archangels):
"""Acquire angels."""
View
@@ -236,48 +236,39 @@ def recruit(self, game):
def1 = self.user.callRemote("done_with_recruits", game.name)
def1.addErrback(self.failure)
- def reinforce_during(self, game):
- """Reinforce, during the REINFORCE battle phase"""
- logging.info("")
- assert game.battle_active_player.name == self.playername
- assert game.battle_phase == Phase.REINFORCE
- legion = game.defender_legion
- assert legion.player.name == self.playername
- if game.battle_turn == 4 and legion.can_recruit:
- recruit, recruiters = self._pick_recruit_and_recruiters(legion)
- if recruit is not None:
- logging.info("CleverBot calling recruit_creature %s", recruit)
- def1 = self.user.callRemote("recruit_creature", game.name,
- legion.markerid, recruit, recruiters)
- def1.addErrback(self.failure)
- return
- logging.info("CleverBot calling done_with_reinforcements")
- def1 = self.user.callRemote("done_with_reinforcements", game.name)
- def1.addErrback(self.failure)
-
- def reinforce_after(self, game):
- """Reinforce, after the battle"""
+ def reinforce(self, game):
+ """Reinforce, during the REINFORCE battle phase or after the battle."""
logging.info("")
+ during_battle = (game.battle_phase == Phase.REINFORCE)
+ if during_battle:
+ assert game.battle_active_player.name == self.playername
legion = game.defender_legion
assert legion.player.name == self.playername
- if legion.can_recruit:
+ if (not during_battle or game.battle_turn == 4) and legion.can_recruit:
recruit, recruiters = self._pick_recruit_and_recruiters(legion)
if recruit is not None:
logging.info("CleverBot calling recruit_creature %s", recruit)
def1 = self.user.callRemote("recruit_creature", game.name,
legion.markerid, recruit, recruiters)
def1.addErrback(self.failure)
return
- logging.info("CleverBot calling do_not_reinforce %s", recruit)
- def1 = self.user.callRemote("do_not_reinforce", game.name,
- legion.markerid)
- def1.addErrback(self.failure)
+ if during_battle:
+ logging.info("CleverBot calling done_with_reinforcements")
+ def1 = self.user.callRemote("done_with_reinforcements", game.name)
+ def1.addErrback(self.failure)
+ else:
+ logging.info("CleverBot calling do_not_reinforce %s", recruit)
+ def1 = self.user.callRemote("do_not_reinforce", game.name,
+ legion.markerid)
+ def1.addErrback(self.failure)
- def _summon_angel(self, game, during_battle):
+ # TODO Consider value of this legion and donor legion before deciding
+ # whether to summon. But we may want to summon from a greater legion
+ # if it's 7 high and summoning lets it recruit.
+ def summon_angel(self, game):
logging.info("")
assert game.active_player.name == self.playername
- if during_battle and game.battle_phase != Phase.REINFORCE:
- return
+ during_battle = (game.battle_phase == Phase.REINFORCE)
legion = game.attacker_legion
assert legion.player.name == self.playername
if (legion.can_summon and (not during_battle or
@@ -321,17 +312,6 @@ def _summon_angel(self, game, during_battle):
def1 = self.user.callRemote("done_with_reinforcements", game.name)
def1.addErrback(self.failure)
- def summon_angel_during(self, game):
- """Summon, during the REINFORCE battle phase"""
- self._summon_angel(game, True)
-
- # TODO Consider value of this legion and donor legion before deciding
- # whether to summon. But we may want to summon from a greater legion
- # if it's 7 high and doing this lets it recruit.
- def summon_angel_after(self, game):
- """Summon, after the battle is over."""
- self._summon_angel(game, False)
-
def acquire_angels(self, game, markerid, num_angels, num_archangels):
logging.info("CleverBot.acquire_angels %s %s %s", markerid, num_angels,
num_archangels)

0 comments on commit ec63bee

Please sign in to comment.