Merge branch 'master' of github.com:annabunches/4etools
This commit is contained in:
commit
f6f8de5415
225
battleman.py
225
battleman.py
|
@ -20,7 +20,6 @@ import os.path
|
||||||
|
|
||||||
|
|
||||||
class CombatGroup():
|
class CombatGroup():
|
||||||
|
|
||||||
# What we're mostly getting here is a definition of the *members*
|
# What we're mostly getting here is a definition of the *members*
|
||||||
# of the group... then we build them all and stick them in
|
# of the group... then we build them all and stick them in
|
||||||
# the group
|
# the group
|
||||||
|
@ -33,13 +32,17 @@ class CombatGroup():
|
||||||
surges = input_int('healing surges', 0)
|
surges = input_int('healing surges', 0)
|
||||||
|
|
||||||
recharges = []
|
recharges = []
|
||||||
recharge = ['']
|
|
||||||
while True:
|
while True:
|
||||||
recharge = input_str("recharge", default='').split(',')
|
data = []
|
||||||
if recharge == ['']:
|
data = input_str("recharge", default='').split(',')
|
||||||
break
|
if len(data) == 2:
|
||||||
else:
|
recharge = {}
|
||||||
|
recharge['name'] = data[0]
|
||||||
|
recharge['value'] = int(data[1])
|
||||||
|
recharge['used'] = False
|
||||||
recharges.append(recharge)
|
recharges.append(recharge)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
count = input_int('count', 1)
|
count = input_int('count', 1)
|
||||||
|
|
||||||
|
@ -79,13 +82,10 @@ class CombatGroup():
|
||||||
|
|
||||||
|
|
||||||
def begin_turn(self):
|
def begin_turn(self):
|
||||||
msg = None
|
print '{} {} initiative.'.format(self.name, ['has', 'have'][len(self.members) != 1])
|
||||||
if self.is_solo_group():
|
|
||||||
msg = '{} has initiative.'.format(self.name)
|
|
||||||
else:
|
|
||||||
msg = '{} have initiative.'.format(self.name)
|
|
||||||
|
|
||||||
print msg
|
for c in self.members:
|
||||||
|
c.begin_turn()
|
||||||
|
|
||||||
|
|
||||||
def end_turn(self):
|
def end_turn(self):
|
||||||
|
@ -106,12 +106,19 @@ class Combatant():
|
||||||
self.surges = surges
|
self.surges = surges
|
||||||
self.ap = ap
|
self.ap = ap
|
||||||
self.sw = sw
|
self.sw = sw
|
||||||
self.recharges = []
|
|
||||||
self.conditions = {}
|
self.conditions = {}
|
||||||
self.index = Combatant.next_index
|
self.index = Combatant.next_index
|
||||||
self.next_condition_index = 0
|
self.next_condition_index = 0
|
||||||
Combatant.next_index += 1
|
Combatant.next_index += 1
|
||||||
|
|
||||||
|
self.recharges = {}
|
||||||
|
recharge_index = 0
|
||||||
|
for r in recharges:
|
||||||
|
r['index'] = recharge_index
|
||||||
|
r['just_used'] = False
|
||||||
|
self.recharges[recharge_index] = r
|
||||||
|
recharge_index += 1
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "{}: {} ({})".format(self.index, self.name, self.format_health_summary())
|
return "{}: {} ({})".format(self.index, self.name, self.format_health_summary())
|
||||||
|
@ -119,14 +126,15 @@ class Combatant():
|
||||||
|
|
||||||
# cond_type can be 's' or 't', for 'save' or 'timed'. If it is 't', condition expires at the end of the players turn
|
# cond_type can be 's' or 't', for 'save' or 'timed'. If it is 't', condition expires at the end of the players turn
|
||||||
# 'duration' rounds from now
|
# 'duration' rounds from now
|
||||||
def add_condition(self, name, cond_type, duration=None):
|
def add_condition(self, name, cond_type, duration=None, end_type='e'):
|
||||||
condition = {}
|
condition = {}
|
||||||
condition['name'] = name
|
condition['name'] = name
|
||||||
condition['cond_type'] = cond_type
|
condition['cond_type'] = cond_type
|
||||||
condition['duration'] = duration
|
condition['duration'] = duration
|
||||||
|
condition['end_type'] = end_type
|
||||||
condition['index'] = self.next_condition_index
|
condition['index'] = self.next_condition_index
|
||||||
if cond_type == 'timed' and duration == None:
|
if cond_type == 'timed' and duration == None:
|
||||||
print('Error: specified a timed condition with no duration.')
|
print 'Error: specified a timed condition with no duration.'
|
||||||
return
|
return
|
||||||
self.conditions[self.next_condition_index] = condition
|
self.conditions[self.next_condition_index] = condition
|
||||||
self.next_condition_index += 1
|
self.next_condition_index += 1
|
||||||
|
@ -139,12 +147,16 @@ class Combatant():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
c = self.conditions.pop(index)
|
c = self.conditions.pop(index)
|
||||||
print('{} is no longer affected by {}.'.format(self, c['name']))
|
print '{} is no longer affected by {}.'.format(self, c['name'])
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
def choose_condition(self):
|
def choose_condition(self):
|
||||||
|
if not len(self.conditions):
|
||||||
|
print '{} has no conditions.'.format(self)
|
||||||
|
return None
|
||||||
|
|
||||||
print self.format_condition_summary()
|
print self.format_condition_summary()
|
||||||
index = input_int('choice')
|
index = input_int('choice')
|
||||||
|
|
||||||
|
@ -161,30 +173,50 @@ class Combatant():
|
||||||
c['duration'] -= 1
|
c['duration'] -= 1
|
||||||
|
|
||||||
|
|
||||||
|
def begin_turn(self):
|
||||||
|
for c in self.conditions.values():
|
||||||
|
if c['cond_type'] == 't' and c['end_type'] == 's':
|
||||||
|
if c['duration'] <= 0:
|
||||||
|
self.remove_condition(c['index'])
|
||||||
|
else:
|
||||||
|
print '{} is still affected by {} ({} round{} left).'.format(self, c['name'], c['duration'], 's'[c['duration']==1:])
|
||||||
|
|
||||||
|
|
||||||
def end_turn(self):
|
def end_turn(self):
|
||||||
for (index, c) in self.conditions.items():
|
for c in self.conditions.values():
|
||||||
if c['cond_type'] == 's':
|
if c['cond_type'] == 's':
|
||||||
r = None
|
r = None
|
||||||
if self.pc:
|
if self.pc:
|
||||||
print('{}: save against {}.'.format(self, c['name']))
|
print '{}: save against {}.'.format(self, c['name'])
|
||||||
r = input_int('saving throw')
|
r = input_int('saving throw')
|
||||||
else:
|
else:
|
||||||
save_die = Dice.from_str('1d20')
|
save_die = Dice.from_str('1d20')
|
||||||
r = save_die.roll()['total']
|
r = save_die.roll()['total']
|
||||||
|
|
||||||
if r >= 10:
|
if r >= 10:
|
||||||
self.remove_condition(index)
|
self.remove_condition(c['index'])
|
||||||
|
print '{} successfully saved against {}.'.format(self, c['name'])
|
||||||
else:
|
else:
|
||||||
print('{} failed a save against {}.'.format(self, c['name']))
|
print '{} failed a save against {}.'.format(self, c['name'])
|
||||||
|
|
||||||
elif c['cond_type'] == 't':
|
elif c['cond_type'] == 't' and c['end_type'] == 'e':
|
||||||
c['duration'] -= 1
|
if c['duration'] <= 0:
|
||||||
if c['duration'] < 0:
|
self.remove_condition(c['index'])
|
||||||
self.remove_condition(index)
|
|
||||||
else:
|
else:
|
||||||
print('{} is still affected by {} ({} round{} left).'.format(self, c['name'], c['duration']+1, 's'[c['duration']==0:]))
|
print '{} is still affected by {} ({} round{} left).'.format(self, c['name'], c['duration'], 's'[c['duration']==1:])
|
||||||
|
|
||||||
# fixme: still need to add recharges
|
for r in self.recharges.values():
|
||||||
|
if r['used']:
|
||||||
|
if r['just_used']:
|
||||||
|
r['just_used'] = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Roll to recharge
|
||||||
|
d = Dice.from_str('1d6')
|
||||||
|
n = d.roll()['total']
|
||||||
|
if n >= r['value']:
|
||||||
|
r['used'] = False
|
||||||
|
print '{} can use {} again!'.format(self, r['name'])
|
||||||
|
|
||||||
|
|
||||||
def damage(self, amount):
|
def damage(self, amount):
|
||||||
|
@ -201,9 +233,9 @@ class Combatant():
|
||||||
print '{} took {} points of damage.'.format(self, amount)
|
print '{} took {} points of damage.'.format(self, amount)
|
||||||
|
|
||||||
if self.is_down():
|
if self.is_down():
|
||||||
print('{} is down!'.format(self))
|
print '{} is down!'.format(self)
|
||||||
elif self.is_bloodied() and not was_bloodied:
|
elif self.is_bloodied() and not was_bloodied:
|
||||||
print('{} is bloodied!'.format(self))
|
print '{} is bloodied!'.format(self)
|
||||||
|
|
||||||
|
|
||||||
def heal(self, amount):
|
def heal(self, amount):
|
||||||
|
@ -220,14 +252,14 @@ class Combatant():
|
||||||
self.hp += amount_healed
|
self.hp += amount_healed
|
||||||
|
|
||||||
if was_down:
|
if was_down:
|
||||||
print('{} regains consciousness.'.format(self))
|
print '{} regains consciousness.'.format(self)
|
||||||
|
|
||||||
print('{} regained {} hit points.'.format(self, amount_healed))
|
print '{} regained {} hit points.'.format(self, amount_healed)
|
||||||
|
|
||||||
if was_bloodied and not self.is_bloodied():
|
if was_bloodied and not self.is_bloodied():
|
||||||
print('{} is no longer bloodied.'.format(self))
|
print '{} is no longer bloodied.'.format(self)
|
||||||
elif was_bloodied and self.is_bloodied():
|
elif was_bloodied and self.is_bloodied():
|
||||||
print('{} is still bloodied.'.format(self))
|
print '{} is still bloodied.'.format(self)
|
||||||
|
|
||||||
|
|
||||||
def add_temp_hp(self, amount):
|
def add_temp_hp(self, amount):
|
||||||
|
@ -265,6 +297,30 @@ class Combatant():
|
||||||
self.add_condition('Second Wind (+2 all def)', 't', 1)
|
self.add_condition('Second Wind (+2 all def)', 't', 1)
|
||||||
|
|
||||||
|
|
||||||
|
def use_recharge_power(self, index):
|
||||||
|
if index not in self.recharges:
|
||||||
|
print "Error: Invalid recharge index"
|
||||||
|
return
|
||||||
|
|
||||||
|
self.recharges[index]['used'] = True
|
||||||
|
self.recharges[index]['just_used'] = True
|
||||||
|
|
||||||
|
|
||||||
|
def choose_recharge_power(self):
|
||||||
|
if not len(self.recharges):
|
||||||
|
print '{} has no rechargable powers.'.format(self)
|
||||||
|
return None
|
||||||
|
|
||||||
|
print self.format_recharge_summary()
|
||||||
|
index = input_int('choice')
|
||||||
|
|
||||||
|
if index not in self.recharges:
|
||||||
|
print 'Error: {} is not a valid index'.format(index)
|
||||||
|
return self.choose_recharge_power()
|
||||||
|
|
||||||
|
return self.recharges[index]
|
||||||
|
|
||||||
|
|
||||||
def is_bloodied(self):
|
def is_bloodied(self):
|
||||||
return self.hp <= self.max_hp / 2
|
return self.hp <= self.max_hp / 2
|
||||||
|
|
||||||
|
@ -273,6 +329,21 @@ class Combatant():
|
||||||
return self.hp <= 0
|
return self.hp <= 0
|
||||||
|
|
||||||
|
|
||||||
|
def format_full_info(self):
|
||||||
|
return """{name}
|
||||||
|
{separator}
|
||||||
|
index: {index}
|
||||||
|
hp: {hp}/{max_hp}
|
||||||
|
temp hp: {temp_hp}
|
||||||
|
surges: {surge}
|
||||||
|
ap: {ap}
|
||||||
|
sw: {sw}
|
||||||
|
conditions:
|
||||||
|
{conditions}
|
||||||
|
recharge powers:
|
||||||
|
{recharge}""".format(index=self.index, name=self.name, hp=self.hp, max_hp=self.max_hp, temp_hp=self.temp_hp, surge=self.surges, ap=self.ap, sw=self.sw, conditions=self.format_condition_summary(' '), recharge=self.format_recharge_summary(' '), separator='='*len(self.name))
|
||||||
|
|
||||||
|
|
||||||
def format_health_summary(self):
|
def format_health_summary(self):
|
||||||
bloodied = ''
|
bloodied = ''
|
||||||
temp_info = ''
|
temp_info = ''
|
||||||
|
@ -287,7 +358,7 @@ class Combatant():
|
||||||
return '{} hp{}{}{}'.format(self.hp, temp_info, bloodied, ', '.join([x['name'] for x in self.conditions.values()]))
|
return '{} hp{}{}{}'.format(self.hp, temp_info, bloodied, ', '.join([x['name'] for x in self.conditions.values()]))
|
||||||
|
|
||||||
|
|
||||||
def format_condition_summary(self):
|
def format_condition_summary(self, initial=''):
|
||||||
summary = ''
|
summary = ''
|
||||||
for (index, c) in self.conditions.items():
|
for (index, c) in self.conditions.items():
|
||||||
type_string = ''
|
type_string = ''
|
||||||
|
@ -295,10 +366,15 @@ class Combatant():
|
||||||
type_string = 'Save Ends'
|
type_string = 'Save Ends'
|
||||||
elif c['cond_type'] == 't':
|
elif c['cond_type'] == 't':
|
||||||
type_string = '{} Round{}'.format(c['duration'], 's'[ c['duration']==1: ] )
|
type_string = '{} Round{}'.format(c['duration'], 's'[ c['duration']==1: ] )
|
||||||
summary = summary + '{}: {} ({})\n'.format(index, c['name'], type_string)
|
summary = summary + '{}{}: {} ({})\n'.format(initial, index, c['name'], type_string)
|
||||||
|
|
||||||
return summary.rstrip()
|
return summary.rstrip()
|
||||||
|
|
||||||
|
def format_recharge_summary(self, initial):
|
||||||
|
summary = ''
|
||||||
|
for (index, r) in self.recharges.items():
|
||||||
|
summary = summary + '{}{}: {} (Recharge: {}, Available: {})\n'.format(initial, index, r['name'], r['value'], ['Yes', 'No'][ r['used'] ])
|
||||||
|
return summary.rstrip()
|
||||||
|
|
||||||
|
|
||||||
# data about the battle - includes combatant list, etc
|
# data about the battle - includes combatant list, etc
|
||||||
|
@ -377,7 +453,7 @@ class Battle():
|
||||||
|
|
||||||
def begin(self):
|
def begin(self):
|
||||||
if self.is_started():
|
if self.is_started():
|
||||||
print("Error: battle is already running")
|
print "Error: battle is already running"
|
||||||
|
|
||||||
for g in self.groups:
|
for g in self.groups:
|
||||||
if g.is_solo_group() and g.members[0].pc:
|
if g.is_solo_group() and g.members[0].pc:
|
||||||
|
@ -417,16 +493,13 @@ class Battle():
|
||||||
if self.validate_started():
|
if self.validate_started():
|
||||||
return self.validate_started()
|
return self.validate_started()
|
||||||
|
|
||||||
ret = ''
|
|
||||||
|
|
||||||
g = self.groups[self.current]
|
g = self.groups[self.current]
|
||||||
if g.is_solo_group():
|
if g.is_solo_group():
|
||||||
ret = ret + '{}\n'.format(g.members[0])
|
return '{}'.format(g.members[0].format_full_info())
|
||||||
else:
|
else:
|
||||||
ret = ret + '{}:\n'.format(g.name)
|
ret = '{}\n'.format(g.name)
|
||||||
for c in g.members:
|
for c in g.members:
|
||||||
ret = ret + ' {}\n'.format(c)
|
ret = ret + ' {}\n'.format(c)
|
||||||
|
|
||||||
return ret.rstrip()
|
return ret.rstrip()
|
||||||
|
|
||||||
|
|
||||||
|
@ -458,7 +531,7 @@ class Battle():
|
||||||
for c in self.combatant_hash.values():
|
for c in self.combatant_hash.values():
|
||||||
c.tick_conditions()
|
c.tick_conditions()
|
||||||
|
|
||||||
print('Beginning round {}'.format(self.round))
|
print 'Beginning round {}'.format(self.round)
|
||||||
|
|
||||||
|
|
||||||
def validate_started(self):
|
def validate_started(self):
|
||||||
|
@ -500,21 +573,21 @@ def main():
|
||||||
do_prompt()
|
do_prompt()
|
||||||
|
|
||||||
|
|
||||||
|
# fixme - change input behavior. If an action has a sensible default, do that when no args are passed - require args to change default behavior
|
||||||
def do_prompt():
|
def do_prompt():
|
||||||
print('')
|
print ''
|
||||||
(comm, rdata) = input_str('', default='?', show_default=False, prompt_str='>').partition(' ')[::2]
|
(comm, rdata) = input_str('', default='?', show_default=False, prompt_str='>').partition(' ')[::2]
|
||||||
data = rdata.split(' ')
|
data = rdata.split(' ')
|
||||||
|
|
||||||
# To simplify our checks later
|
|
||||||
if data == ['']:
|
if data == ['']:
|
||||||
data = None
|
data = []
|
||||||
|
|
||||||
if comm == '?':
|
if comm == '?':
|
||||||
do_help()
|
do_help()
|
||||||
elif comm == 'a':
|
elif comm == 'a':
|
||||||
do_add_combatants(data)
|
do_add_combatants(data)
|
||||||
elif comm == 'p':
|
elif comm == 'p':
|
||||||
print battle.format_current_group()
|
do_print_combatant_info(data)
|
||||||
elif comm == 'l':
|
elif comm == 'l':
|
||||||
print battle.format_combatants()
|
print battle.format_combatants()
|
||||||
elif comm == 'b':
|
elif comm == 'b':
|
||||||
|
@ -539,11 +612,14 @@ def do_prompt():
|
||||||
do_remove_condition(data)
|
do_remove_condition(data)
|
||||||
elif comm == 'n':
|
elif comm == 'n':
|
||||||
battle.next_combatant()
|
battle.next_combatant()
|
||||||
|
elif comm == 'r':
|
||||||
|
do_use_recharge_power(data)
|
||||||
elif comm == 'w':
|
elif comm == 'w':
|
||||||
do_wait(data)
|
do_wait(data)
|
||||||
elif comm == 'W':
|
elif comm == 'W':
|
||||||
do_unwait(data)
|
do_unwait(data)
|
||||||
print('Sorry, this is still a stub function.')
|
elif comm == 'x':
|
||||||
|
do_stub()
|
||||||
elif comm == 'q':
|
elif comm == 'q':
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
@ -578,20 +654,32 @@ T - remove temporary hit points [stub]
|
||||||
s - use a healing surge
|
s - use a healing surge
|
||||||
so - use a healing surge, but don't regain hit points
|
so - use a healing surge, but don't regain hit points
|
||||||
sw - use a second wind
|
sw - use a second wind
|
||||||
c - apply a condition
|
c/C - apply / remove a condition
|
||||||
C - remove a condition
|
r - use a rechargable power
|
||||||
n - next (end the current combat group's turn)
|
n - next (end the current combat group's turn)
|
||||||
wW - wait / unwait (remove a combatant from the initiative order and into a separate pool, then put them back) [stub]
|
w/W - wait / unwait (remove a combatant from the initiative order and into a separate pool, then put them back) [stub]
|
||||||
|
x - force save the progress to the current battle cache (for use with --resume) [stub]
|
||||||
q - quit""")
|
q - quit""")
|
||||||
|
|
||||||
|
|
||||||
def do_add_combatants(data):
|
def do_add_combatants(data):
|
||||||
ngroups = input_int('number of groups')
|
ngroups = input_int('number of groups')
|
||||||
for i in range(1, ngroups+1):
|
for i in range(1, ngroups+1):
|
||||||
print("Adding group {}".format(i))
|
print "Adding group {}".format(i)
|
||||||
battle.add_group(CombatGroup.from_input())
|
battle.add_group(CombatGroup.from_input())
|
||||||
|
|
||||||
|
|
||||||
|
def do_print_combatant_info(data):
|
||||||
|
if len(data) >= 1:
|
||||||
|
c = battle.get_combatant(int(data[0]))
|
||||||
|
if not c:
|
||||||
|
print('Error: Invalid combatant index.')
|
||||||
|
else:
|
||||||
|
print c.format_full_info()
|
||||||
|
else:
|
||||||
|
print battle.format_current_group()
|
||||||
|
|
||||||
|
|
||||||
def do_damage(data):
|
def do_damage(data):
|
||||||
if len(data) >= 1:
|
if len(data) >= 1:
|
||||||
c = battle.get_combatant(int(data[0]))
|
c = battle.get_combatant(int(data[0]))
|
||||||
|
@ -673,6 +761,7 @@ def do_second_wind(data):
|
||||||
|
|
||||||
def do_add_condition(data):
|
def do_add_condition(data):
|
||||||
duration = None
|
duration = None
|
||||||
|
end_type = 'e'
|
||||||
|
|
||||||
if len(data) >= 1:
|
if len(data) >= 1:
|
||||||
c = battle.get_combatant(int(data[0]))
|
c = battle.get_combatant(int(data[0]))
|
||||||
|
@ -695,7 +784,12 @@ def do_add_condition(data):
|
||||||
else:
|
else:
|
||||||
duration = input_int('duration')
|
duration = input_int('duration')
|
||||||
|
|
||||||
c.add_condition(name, ctype, duration)
|
if len(data) >= 5:
|
||||||
|
end_type = data[4]
|
||||||
|
else:
|
||||||
|
end_type = input_str('(s)tart|(e)nd', default='e', show_default=True)
|
||||||
|
|
||||||
|
c.add_condition(name, ctype, duration, end_type)
|
||||||
|
|
||||||
|
|
||||||
def do_remove_condition(data):
|
def do_remove_condition(data):
|
||||||
|
@ -710,11 +804,36 @@ def do_remove_condition(data):
|
||||||
if len(data) >= 2:
|
if len(data) >= 2:
|
||||||
index = int(data[1])
|
index = int(data[1])
|
||||||
else:
|
else:
|
||||||
index = c.choose_condition()['index']
|
cond = c.choose_condition()
|
||||||
|
index = None
|
||||||
|
if cond:
|
||||||
|
index = cond['index']
|
||||||
|
|
||||||
|
if index != None:
|
||||||
c.remove_condition(index)
|
c.remove_condition(index)
|
||||||
|
|
||||||
|
|
||||||
|
def do_use_recharge_power(data):
|
||||||
|
if len(data) >= 1:
|
||||||
|
c = battle.get_combatant(int(data[0]))
|
||||||
|
if not c:
|
||||||
|
print ('Error: Invalid combatant index.')
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
c = battle.choose_combatant()
|
||||||
|
|
||||||
|
if len(data) >= 2:
|
||||||
|
index = int(data[1])
|
||||||
|
else:
|
||||||
|
r = c.choose_recharge_power()
|
||||||
|
index = None
|
||||||
|
if r:
|
||||||
|
index = r['index']
|
||||||
|
|
||||||
|
if index != None:
|
||||||
|
c.use_recharge_power(index)
|
||||||
|
|
||||||
|
|
||||||
def do_wait(data):
|
def do_wait(data):
|
||||||
do_stub()
|
do_stub()
|
||||||
|
|
||||||
|
@ -724,7 +843,7 @@ def do_unwait(data):
|
||||||
|
|
||||||
|
|
||||||
def do_stub():
|
def do_stub():
|
||||||
print("Sorry, this is a stub function")
|
print "Sorry, this is a stub function"
|
||||||
|
|
||||||
|
|
||||||
def input_str(prompt, default=None, show_default=False, prompt_str=':'):
|
def input_str(prompt, default=None, show_default=False, prompt_str=':'):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user