From 0fea33760bf1b498dbfb4e0dce2fb3c485c25f3d Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 6 Apr 2012 02:25:41 -0400 Subject: [PATCH] battleman: Prevent wait/unwait from calling begin_turn and end_turn more than is correct. --- lib/battle.py | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/battle.py b/lib/battle.py index 9b8de1f..936cd16 100644 --- a/lib/battle.py +++ b/lib/battle.py @@ -149,16 +149,21 @@ class Battle(): return ret.rstrip() - # Fixme - if someone is waited/unwaited, both them and the person - # after them may have begin_turn() and/or end_turn() called more than once - def next_combatant(self, same_index = False): + def next_combatant(self, same_index = False, from_unwait = False): if self.validate_started(): print self.validate_started() return g = self.get_current_group() - g.end_turn() - + + # If we get here from wait(), we're okay because calling this on the next + # CombatGroup will fail silently. + # In the case of unwait(), though, we don't want to end the newly-unwaited + # group's term prematurely + if not from_unwait: + g.end_turn() + + # same_index gets set when we're coming from wait() or unwait() if not same_index: self.current += 1 @@ -167,6 +172,9 @@ class Battle(): self.next_round() g = self.get_current_group() + + # We don't have to worry about calling this twice, since + # there's a sentinel value in CombatGroup g.begin_turn() @@ -225,9 +233,16 @@ class Battle(): if index in self.wait_list: c = self.wait_list[index] del self.wait_list[index] - self.groups.insert(self.current, CombatGroup(c.name, {c.index: c}, 0)) + + # This code is weirdly complicated because we're basically + # 'hacking' an already-begun turn back into the initiative list + g = CombatGroup(c.name, {c.index: c}, 0) + g.turn_began = True + self.groups.insert(self.current, g) + print '{} is no longer waiting.'.format(c) - self.next_combatant(same_index = True) + self.next_combatant(same_index = True, from_unwait = True) + else: if index in self.combatant_hash: print '{} is not waiting'.format(self.combatant_hash[index]) @@ -280,6 +295,7 @@ class CombatGroup(): self.members = members self.init_mod = init_mod self.init = 0 + self.turn_began = False def roll_init(self): @@ -324,15 +340,25 @@ class CombatGroup(): def begin_turn(self): + if self.turn_began: + return + print '{} {} initiative.'.format(self.name, ['has', 'have'][len(self.members) != 1]) for c in self.members.values(): c.begin_turn() + self.turn_began = True + def end_turn(self): + if not self.turn_began: + return + for c in self.members.values(): c.end_turn() + self.turn_began = False +