4etools/battleman.py

224 lines
5.6 KiB
Python
Executable File

#!/usr/bin/python
#
# battleman.py - RPG Battle Manager
#
# A table-top RPG battle flow manager
# Tuned pretty specifically to D&D 4e for now... need a templatized system
# to do anything fancier... may develop that at some point.
import dice
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
@classmethod
def from_input(cls):
name = raw_input("Name: ")
hp = int(raw_input("hp: "))
init_mod = int(raw_input("init mod: "))
ap = raw_input("action points [0] ")
if ap:
ap = int(ap)
else:
ap = 0
surges = raw_input("surges [0]: ")
if surges:
surges = int(surges)
else:
surges = 0
recharges = []
recharge = '-1'
while True:
recharge = raw_input("recharge: ").split(',')
if recharge == ['']:
break
else:
recharges.append(recharge)
count = raw_input("count [1]: ")
if count:
count = int(count)
else:
count = 0
# Now make the combatants...
members = []
for i in range(count):
members.append(Combatant(name, hp, pc=False, surges=surges, ap=ap, sw=0, recharges=recharges))
if count > 1:
name = name + 's'
return CombatGroup(name, members, init_mod)
def __init__(self, name, members, init_mod=0):
self.name = name
self.members = members
self.init_mod = init_mod
self.init = 0
def roll_init(self):
d = Dice.from_desc('1d20+' + self.init_mod)
self.set_init(d.roll()['total'])
def set_init(self, init):
self.init = init
def add_member(self, c):
self.members.append(c)
class Combatant():
next_index = 0
def __init__(self, name, hp, pc=False, init_mod=0, surges=0, ap=0, sw=0, recharges=[]):
self.name = name
self.max_hp = hp
self.hp = self.max_hp
self.pc = pc
self.surges = surges
self.ap = ap
self.sw = sw
self.recharges = []
self.conditions = []
self.index = Combatant.next_index
Combatant.next_index += 1
def add_condition(self, name, cond_type, duration):
condition = {}
condition['name'] = name
condition['type'] = cond_type
condition['duration'] = duration
self.conditions.append(condition)
def turn_begin(self):
print("{} has initiative.".format(self))
def __str__(self):
return "{} ({hp} hp)".format(self.name, hp=self.hp)
def input_int(prompt, default=-1):
if default != -1:
prompt = prompt + '[{}]'.format(default)
prompt = prompt + ': '
data = raw_input(prompt)
if default != -1 and not data:
return default
else:
return int(data)
# data about the battle - includes combatant list, etc
battle = {}
def main():
battle['groups'] = []
battle['current'] = None
battle['round'] = -1
# hard-coding test cases for now.
# Eventually, use a state-saving text file that's easy to edit
battle['groups'].append(CombatGroup("Adele", [Combatant("Adele", hp=26, pc=True, surges=8, sw=1)], 2))
battle['groups'].append(CombatGroup("Aristaire", [Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1)], 0))
ngroups = int(raw_input("Number of enemy groups: "))
for i in range(1, ngroups+1):
print("Adding enemy group {}".format(i))
battle['groups'].append(CombatGroup.from_input())
while True:
do_prompt()
def do_prompt():
comm = raw_input('> ')
# debug
print ('|' + comm + '|')
if 'comm' == '?':
do_help()
elif 'comm' == 'a':
print('Sorry, this is still a stub function.')
elif 'comm' == 'l':
do_list_combatants()
elif 'comm' == 'b':
do_begin_battle()
elif 'comm' == 'd':
print('Sorry, this is still a stub function.')
elif 'comm' == 'h':
print('Sorry, this is still a stub function.')
elif 'comm' == 's':
print('Sorry, this is still a stub function.')
elif 'comm' == 'c':
print('Sorry, this is still a stub function.')
elif 'comm' == 'r':
print('Sorry, this is still a stub function.')
elif 'comm' == 'n':
print('Sorry, this is still a stub function.')
elif 'comm' == 'w':
print('Sorry, this is still a stub function.')
def do_help():
print("""
Possible commands:
? - print this help menu (yay, you already figured that one out)
a - add more combatants (works during battle)
l - list current combatants
b - begin the battle
d - deal damage to someone
h - heal someone
s - let someone use a healing surge
c - apply a condition
r - remove a condition (this can also happen automatically)
n - next (end the current combat group's turn)
w - wait (remove a combatant from the initiative order and into a separate pool)
""")
def do_list_combatants():
for g in battle['groups']:
for c in g.members:
print('{}: {}'.format(c.index, c.name))
def do_begin_battle():
if current != None:
print("Error: battle is already running")
return
for g in battle['groups']:
if g.single() and g.members[0].pc:
raw_input('Initiative for {}: '.format(g.name))
else:
g.roll_init()
battle['groups'].sort(reverse=True, key=lambda group: group.init)
battle['current'] = 0
battle['round'] = 1
for g in battle['groups']:
print '{} ({})'.format(g.name, g.init)
if __name__ == '__main__':
main()