battleman: Prevent wait/unwait from calling begin_turn and end_turn more than is correct.

This commit is contained in:
Anna Rose 2012-04-06 02:25:41 -04:00
parent 4c6f96832b
commit 0fea33760b

View File

@ -149,16 +149,21 @@ class Battle():
return ret.rstrip() return ret.rstrip()
# Fixme - if someone is waited/unwaited, both them and the person def next_combatant(self, same_index = False, from_unwait = False):
# after them may have begin_turn() and/or end_turn() called more than once
def next_combatant(self, same_index = False):
if self.validate_started(): if self.validate_started():
print self.validate_started() print self.validate_started()
return return
g = self.get_current_group() g = self.get_current_group()
# 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() g.end_turn()
# same_index gets set when we're coming from wait() or unwait()
if not same_index: if not same_index:
self.current += 1 self.current += 1
@ -167,6 +172,9 @@ class Battle():
self.next_round() self.next_round()
g = self.get_current_group() 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() g.begin_turn()
@ -225,9 +233,16 @@ class Battle():
if index in self.wait_list: if index in self.wait_list:
c = self.wait_list[index] c = self.wait_list[index]
del 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) print '{} is no longer waiting.'.format(c)
self.next_combatant(same_index = True) self.next_combatant(same_index = True, from_unwait = True)
else: else:
if index in self.combatant_hash: if index in self.combatant_hash:
print '{} is not waiting'.format(self.combatant_hash[index]) print '{} is not waiting'.format(self.combatant_hash[index])
@ -280,6 +295,7 @@ class CombatGroup():
self.members = members self.members = members
self.init_mod = init_mod self.init_mod = init_mod
self.init = 0 self.init = 0
self.turn_began = False
def roll_init(self): def roll_init(self):
@ -324,15 +340,25 @@ class CombatGroup():
def begin_turn(self): def begin_turn(self):
if self.turn_began:
return
print '{} {} initiative.'.format(self.name, ['has', 'have'][len(self.members) != 1]) print '{} {} initiative.'.format(self.name, ['has', 'have'][len(self.members) != 1])
for c in self.members.values(): for c in self.members.values():
c.begin_turn() c.begin_turn()
self.turn_began = True
def end_turn(self): def end_turn(self):
if not self.turn_began:
return
for c in self.members.values(): for c in self.members.values():
c.end_turn() c.end_turn()
self.turn_began = False