battleman.py: Started adding some logic for battle groups, that contain a group of entities who share a single initiative. Also implemented the first couple main menu functions.

This commit is contained in:
Anna Rose 2012-03-21 17:58:15 -04:00
parent ac09d41675
commit c48218da92

View File

@ -8,11 +8,23 @@
import dice import dice
class Combatant():
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 @classmethod
def from_input(cls): def from_input(cls):
name = raw_input("Name: ") name = raw_input("Name: ")
hp = int(raw_input("hp: ")) 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]: ") surges = raw_input("surges [0]: ")
if surges: if surges:
@ -20,29 +32,37 @@ class Combatant():
else: else:
surges = 0 surges = 0
recharges = [] recharges = []
recharge = '-1' recharge = '-1'
while True: while True:
recharge = raw_input("recharge: ").split(',') recharge = raw_input("recharge: ").split(',')
if recharge == ['']: if recharge == ['']:
break break
else: else:
recharges.append(recharge) recharges.append(recharge)
return Combatant(name, hp, pc=0, surges=surges, recharges=recharges) 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, hp, init_mod, pc=False, surges=0, ap=0, sw=0, recharges=[]): def __init__(self, name, members, init_mod=0):
self.name = name self.name = name
self.hp = hp self.members = members
self.pc = pc
self.surges = surges
self.ap = ap
self.sw = sw
self.recharges = []
self.init_mod = init_mod self.init_mod = init_mod
self.init = 0 self.init = 0
self.conditions = []
def roll_init(self): def roll_init(self):
@ -54,6 +74,28 @@ class Combatant():
self.init = 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): def add_condition(self, name, cond_type, duration):
condition = {} condition = {}
condition['name'] = name condition['name'] = name
@ -70,27 +112,36 @@ class Combatant():
return "{} ({hp} hp)".format(self.name, hp=self.hp) 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 # data about the battle - includes combatant list, etc
battle = {} battle = {}
def main(): def main():
battle['combatants'] = [] battle['groups'] = []
battle['current'] = None battle['current'] = None
battle['round'] = -1 battle['round'] = -1
# hard-coding test cases for now. # hard-coding test cases for now.
# Eventually, use a state-saving text file that's easy to edit # Eventually, use a state-saving text file that's easy to edit
battle['combatants'].append(Combatant("Adele", hp=26, init_mod=2, pc=True, surges=8, sw=1)) battle['groups'].append(CombatGroup("Adele", [Combatant("Adele", hp=26, pc=True, surges=8, sw=1)], 2))
battle['combatants'].append(Combatant("Aristaire", hp=20, init_mod=0, pc=True, surges=6, sw=1)) battle['groups'].append(CombatGroup("Aristaire", [Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1)], 0))
ngroups = int(raw_input("Num enemy groups: ")) ngroups = int(raw_input("Number of enemy groups: "))
for i in range(1, ngroups+1): for i in range(1, ngroups+1):
print("Adding enemy group {}".format(i)) print("Adding enemy group {}".format(i))
battle['combatants'].append(Combatant.from_input()) battle['groups'].append(CombatGroup.from_input())
# debug
for c in battle['combatants']:
print(c)
while True: while True:
do_prompt() do_prompt()
@ -98,40 +149,32 @@ def main():
def do_prompt(): def do_prompt():
comm = raw_input('> ') comm = raw_input('> ')
# debug
print ('|' + comm + '|')
# help
if 'comm' == '?': if 'comm' == '?':
do_help() do_help()
# add more combatants
elif 'comm' == 'a': elif 'comm' == 'a':
pass print('Sorry, this is still a stub function.')
# list combatants with indices
elif 'comm' == 'l': elif 'comm' == 'l':
do_list_combatants() do_list_combatants()
# begin the battle
elif 'comm' == 'b': elif 'comm' == 'b':
do_begin_battle() do_begin_battle()
# deal damage to a combatant
elif 'comm' == 'd': elif 'comm' == 'd':
pass print('Sorry, this is still a stub function.')
# heal damage from a combatant
elif 'comm' == 'h': elif 'comm' == 'h':
pass print('Sorry, this is still a stub function.')
# use a healing surge
elif 'comm' == 's': elif 'comm' == 's':
pass print('Sorry, this is still a stub function.')
# apply a condition
elif 'comm' == 'c': elif 'comm' == 'c':
pass print('Sorry, this is still a stub function.')
# remove a condition early
elif 'comm' == 'r': elif 'comm' == 'r':
pass print('Sorry, this is still a stub function.')
# end the current combatant's turn
elif 'comm' == 'n': elif 'comm' == 'n':
pass print('Sorry, this is still a stub function.')
# remove the combatant from the initiative order (delay/prepare)
elif 'comm' == 'w': elif 'comm' == 'w':
pass print('Sorry, this is still a stub function.')
def do_help(): def do_help():
@ -146,16 +189,15 @@ h - heal someone
s - let someone use a healing surge s - let someone use a healing surge
c - apply a condition c - apply a condition
r - remove a condition (this can also happen automatically) r - remove a condition (this can also happen automatically)
n - next (end the current combatant's turn) n - next (end the current combat group's turn)
w - wait (remove the combatant from the initiative order and into a separate pool) w - wait (remove a combatant from the initiative order and into a separate pool)
""") """)
def do_list_combatants(): def do_list_combatants():
index = 0 for g in battle['groups']:
for c in battle['combatants']: for c in g.members:
print('{}: {}'.format(index, c.name)) print('{}: {}'.format(c.index, c.name))
index += 1
def do_begin_battle(): def do_begin_battle():
@ -163,14 +205,18 @@ def do_begin_battle():
print("Error: battle is already running") print("Error: battle is already running")
return return
for c in battle['combatants']: for g in battle['groups']:
if c.pc: if g.single() and g.members[0].pc:
raw_input('Initiative for {}: '.format(c.name)) raw_input('Initiative for {}: '.format(g.name))
else: else:
c.roll_init() g.roll_init()
battle['combatants'].sort(reverse=True) battle['groups'].sort(reverse=True, key=lambda group: group.init)
current = 0 battle['current'] = 0
battle['round'] = 1
for g in battle['groups']:
print '{} ({})'.format(g.name, g.init)
if __name__ == '__main__': if __name__ == '__main__':