Merge branch 'master' of github.com:annabunches/4etools
This commit is contained in:
commit
f6f8de5415
257
battleman.py
257
battleman.py
|
@ -20,7 +20,6 @@ import os.path
|
|||
|
||||
|
||||
class CombatGroup():
|
||||
|
||||
# What we're mostly getting here is a definition of the *members*
|
||||
# of the group... then we build them all and stick them in
|
||||
# the group
|
||||
|
@ -33,13 +32,17 @@ class CombatGroup():
|
|||
surges = input_int('healing surges', 0)
|
||||
|
||||
recharges = []
|
||||
recharge = ['']
|
||||
while True:
|
||||
recharge = input_str("recharge", default='').split(',')
|
||||
if recharge == ['']:
|
||||
break
|
||||
else:
|
||||
data = []
|
||||
data = input_str("recharge", default='').split(',')
|
||||
if len(data) == 2:
|
||||
recharge = {}
|
||||
recharge['name'] = data[0]
|
||||
recharge['value'] = int(data[1])
|
||||
recharge['used'] = False
|
||||
recharges.append(recharge)
|
||||
else:
|
||||
break
|
||||
|
||||
count = input_int('count', 1)
|
||||
|
||||
|
@ -79,13 +82,10 @@ class CombatGroup():
|
|||
|
||||
|
||||
def begin_turn(self):
|
||||
msg = None
|
||||
if self.is_solo_group():
|
||||
msg = '{} has initiative.'.format(self.name)
|
||||
else:
|
||||
msg = '{} have initiative.'.format(self.name)
|
||||
print '{} {} initiative.'.format(self.name, ['has', 'have'][len(self.members) != 1])
|
||||
|
||||
print msg
|
||||
for c in self.members:
|
||||
c.begin_turn()
|
||||
|
||||
|
||||
def end_turn(self):
|
||||
|
@ -106,12 +106,19 @@ class Combatant():
|
|||
self.surges = surges
|
||||
self.ap = ap
|
||||
self.sw = sw
|
||||
self.recharges = []
|
||||
self.conditions = {}
|
||||
self.index = Combatant.next_index
|
||||
self.next_condition_index = 0
|
||||
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):
|
||||
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
|
||||
# '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['name'] = name
|
||||
condition['cond_type'] = cond_type
|
||||
condition['duration'] = duration
|
||||
condition['end_type'] = end_type
|
||||
condition['index'] = self.next_condition_index
|
||||
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
|
||||
self.conditions[self.next_condition_index] = condition
|
||||
self.next_condition_index += 1
|
||||
|
@ -139,12 +147,16 @@ class Combatant():
|
|||
return None
|
||||
|
||||
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
|
||||
|
||||
|
||||
def choose_condition(self):
|
||||
if not len(self.conditions):
|
||||
print '{} has no conditions.'.format(self)
|
||||
return None
|
||||
|
||||
print self.format_condition_summary()
|
||||
index = input_int('choice')
|
||||
|
||||
|
@ -161,30 +173,50 @@ class Combatant():
|
|||
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):
|
||||
for (index, c) in self.conditions.items():
|
||||
for c in self.conditions.values():
|
||||
if c['cond_type'] == 's':
|
||||
r = None
|
||||
if self.pc:
|
||||
print('{}: save against {}.'.format(self, c['name']))
|
||||
print '{}: save against {}.'.format(self, c['name'])
|
||||
r = input_int('saving throw')
|
||||
else:
|
||||
save_die = Dice.from_str('1d20')
|
||||
r = save_die.roll()['total']
|
||||
|
||||
if r >= 10:
|
||||
self.remove_condition(index)
|
||||
self.remove_condition(c['index'])
|
||||
print '{} successfully saved against {}.'.format(self, c['name'])
|
||||
else:
|
||||
print('{} failed a save against {}.'.format(self, c['name']))
|
||||
print '{} failed a save against {}.'.format(self, c['name'])
|
||||
|
||||
elif c['cond_type'] == 't':
|
||||
c['duration'] -= 1
|
||||
if c['duration'] < 0:
|
||||
self.remove_condition(index)
|
||||
elif c['cond_type'] == 't' and c['end_type'] == 'e':
|
||||
if c['duration'] <= 0:
|
||||
self.remove_condition(c['index'])
|
||||
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):
|
||||
|
@ -201,9 +233,9 @@ class Combatant():
|
|||
print '{} took {} points of damage.'.format(self, amount)
|
||||
|
||||
if self.is_down():
|
||||
print('{} is down!'.format(self))
|
||||
print '{} is down!'.format(self)
|
||||
elif self.is_bloodied() and not was_bloodied:
|
||||
print('{} is bloodied!'.format(self))
|
||||
print '{} is bloodied!'.format(self)
|
||||
|
||||
|
||||
def heal(self, amount):
|
||||
|
@ -220,14 +252,14 @@ class Combatant():
|
|||
self.hp += amount_healed
|
||||
|
||||
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():
|
||||
print('{} is no longer bloodied.'.format(self))
|
||||
print '{} is no longer bloodied.'.format(self)
|
||||
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):
|
||||
|
@ -265,6 +297,30 @@ class Combatant():
|
|||
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):
|
||||
return self.hp <= self.max_hp / 2
|
||||
|
||||
|
@ -273,6 +329,21 @@ class Combatant():
|
|||
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):
|
||||
bloodied = ''
|
||||
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()]))
|
||||
|
||||
|
||||
def format_condition_summary(self):
|
||||
def format_condition_summary(self, initial=''):
|
||||
summary = ''
|
||||
for (index, c) in self.conditions.items():
|
||||
type_string = ''
|
||||
|
@ -295,10 +366,15 @@ class Combatant():
|
|||
type_string = 'Save Ends'
|
||||
elif c['cond_type'] == 't':
|
||||
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()
|
||||
|
||||
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
|
||||
|
@ -377,7 +453,7 @@ class Battle():
|
|||
|
||||
def begin(self):
|
||||
if self.is_started():
|
||||
print("Error: battle is already running")
|
||||
print "Error: battle is already running"
|
||||
|
||||
for g in self.groups:
|
||||
if g.is_solo_group() and g.members[0].pc:
|
||||
|
@ -417,17 +493,14 @@ class Battle():
|
|||
if self.validate_started():
|
||||
return self.validate_started()
|
||||
|
||||
ret = ''
|
||||
|
||||
g = self.groups[self.current]
|
||||
if g.is_solo_group():
|
||||
ret = ret + '{}\n'.format(g.members[0])
|
||||
return '{}'.format(g.members[0].format_full_info())
|
||||
else:
|
||||
ret = ret + '{}:\n'.format(g.name)
|
||||
ret = '{}\n'.format(g.name)
|
||||
for c in g.members:
|
||||
ret = ret + ' {}\n'.format(c)
|
||||
|
||||
return ret.rstrip()
|
||||
return ret.rstrip()
|
||||
|
||||
|
||||
def next_combatant(self):
|
||||
|
@ -458,7 +531,7 @@ class Battle():
|
|||
for c in self.combatant_hash.values():
|
||||
c.tick_conditions()
|
||||
|
||||
print('Beginning round {}'.format(self.round))
|
||||
print 'Beginning round {}'.format(self.round)
|
||||
|
||||
|
||||
def validate_started(self):
|
||||
|
@ -500,21 +573,21 @@ def main():
|
|||
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():
|
||||
print('')
|
||||
print ''
|
||||
(comm, rdata) = input_str('', default='?', show_default=False, prompt_str='>').partition(' ')[::2]
|
||||
data = rdata.split(' ')
|
||||
|
||||
# To simplify our checks later
|
||||
if data == ['']:
|
||||
data = None
|
||||
data = []
|
||||
|
||||
if comm == '?':
|
||||
do_help()
|
||||
elif comm == 'a':
|
||||
do_add_combatants(data)
|
||||
elif comm == 'p':
|
||||
print battle.format_current_group()
|
||||
do_print_combatant_info(data)
|
||||
elif comm == 'l':
|
||||
print battle.format_combatants()
|
||||
elif comm == 'b':
|
||||
|
@ -539,11 +612,14 @@ def do_prompt():
|
|||
do_remove_condition(data)
|
||||
elif comm == 'n':
|
||||
battle.next_combatant()
|
||||
elif comm == 'r':
|
||||
do_use_recharge_power(data)
|
||||
elif comm == 'w':
|
||||
do_wait(data)
|
||||
elif comm == 'W':
|
||||
do_unwait(data)
|
||||
print('Sorry, this is still a stub function.')
|
||||
elif comm == 'x':
|
||||
do_stub()
|
||||
elif comm == 'q':
|
||||
sys.exit(0)
|
||||
|
||||
|
@ -566,32 +642,44 @@ def do_prompt():
|
|||
|
||||
def do_help():
|
||||
print("""Possible commands:
|
||||
? - print this help menu (yay, you already figured that one out)
|
||||
a - add more combatants (works during battle)
|
||||
b - begin the battle
|
||||
l - list combatants
|
||||
p - print info for combatant/group with initiative
|
||||
d - deal damage to someone
|
||||
h - heal someone
|
||||
t - add temporary hit points
|
||||
T - remove temporary hit points [stub]
|
||||
s - use a healing surge
|
||||
so - use a healing surge, but don't regain hit points
|
||||
sw - use a second wind
|
||||
c - apply a condition
|
||||
C - remove a condition
|
||||
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]
|
||||
q - quit""")
|
||||
? - print this help menu (yay, you already figured that one out)
|
||||
a - add more combatants (works during battle)
|
||||
b - begin the battle
|
||||
l - list combatants
|
||||
p - print info for combatant/group with initiative
|
||||
d - deal damage to someone
|
||||
h - heal someone
|
||||
t - add temporary hit points
|
||||
T - remove temporary hit points [stub]
|
||||
s - use a healing surge
|
||||
so - use a healing surge, but don't regain hit points
|
||||
sw - use a second wind
|
||||
c/C - apply / remove a condition
|
||||
r - use a rechargable power
|
||||
n - next (end the current combat group's turn)
|
||||
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""")
|
||||
|
||||
|
||||
def do_add_combatants(data):
|
||||
ngroups = input_int('number of groups')
|
||||
for i in range(1, ngroups+1):
|
||||
print("Adding group {}".format(i))
|
||||
print "Adding group {}".format(i)
|
||||
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):
|
||||
if len(data) >= 1:
|
||||
c = battle.get_combatant(int(data[0]))
|
||||
|
@ -673,6 +761,7 @@ def do_second_wind(data):
|
|||
|
||||
def do_add_condition(data):
|
||||
duration = None
|
||||
end_type = 'e'
|
||||
|
||||
if len(data) >= 1:
|
||||
c = battle.get_combatant(int(data[0]))
|
||||
|
@ -695,7 +784,12 @@ def do_add_condition(data):
|
|||
else:
|
||||
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):
|
||||
|
@ -710,9 +804,34 @@ def do_remove_condition(data):
|
|||
if len(data) >= 2:
|
||||
index = int(data[1])
|
||||
else:
|
||||
index = c.choose_condition()['index']
|
||||
cond = c.choose_condition()
|
||||
index = None
|
||||
if cond:
|
||||
index = cond['index']
|
||||
|
||||
c.remove_condition(index)
|
||||
if index != None:
|
||||
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):
|
||||
|
@ -724,7 +843,7 @@ def do_unwait(data):
|
|||
|
||||
|
||||
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=':'):
|
||||
|
|
Loading…
Reference in New Issue
Block a user