battleman.py: Built a Battle class and factored a bunch of relevant code into it. Still needs some cleanup.

This commit is contained in:
Anna Rose 2012-03-21 18:31:11 -04:00
parent 9c60ed4d9c
commit 38108a4d7b

View File

@ -7,6 +7,7 @@
# to do anything fancier... may develop that at some point. # to do anything fancier... may develop that at some point.
import dice import dice
import sys
class CombatGroup(): class CombatGroup():
@ -64,6 +65,10 @@ class CombatGroup():
self.members.append(c) self.members.append(c)
def is_solo_group(self):
return len(self.members) == 1
class Combatant(): class Combatant():
next_index = 0 next_index = 0
@ -90,7 +95,7 @@ class Combatant():
self.conditions.append(condition) self.conditions.append(condition)
def turn_begin(self): def begin_turn(self):
print("{} has initiative.".format(self)) print("{} has initiative.".format(self))
@ -112,22 +117,77 @@ def input_int(prompt, default=-1):
# data about the battle - includes combatant list, etc # data about the battle - includes combatant list, etc
battle = {} class Battle():
def __init__(self):
self.groups = []
self.current = None
self.round = -1
def add_group(self, group):
self.groups.append(group)
def get_current_group(self):
if self.current:
return self.groups[self.current]
else:
return None
def begin(self):
if self.current != None:
print("Error: battle is already running")
return
for g in self.groups:
if g.single() and g.members[0].pc:
raw_input('Initiative for {}: '.format(g.name))
else:
g.roll_init()
self.groups.sort(reverse=True, key=lambda group: group.init)
self.current = 0
self.round = 1
for g in self.groups:
print '{} ({})'.format(g.name, g.init)
def list_combatants(self):
for g in battle.groups:
if g.is_solo_group():
print('{}: {}'.format(g.members[0].index, g.name))
else:
print('{}:'.format(g.name))
for c in g.members:
print('\t{}: {}'.format(c.index, c.name))
def next_combatant(self):
print('Sorry, this is still a stub function.')
g = self.get_current_group()
battle = Battle()
def main(): def main():
battle['groups'] = []
battle['current'] = None
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['groups'].append(CombatGroup("Adele", [Combatant("Adele", hp=26, pc=True, surges=8, sw=1)], 2)) battle.add_group(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)) battle.add_group(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): battle.add_group(CombatGroup("Foobolds", [Combatant("Foobold", hp=50)], 20))
print("Adding enemy group {}".format(i)) battle.add_group(CombatGroup("Barglins", [Combatant("Barglin", hp=1)], 3))
battle['groups'].append(CombatGroup.from_input()) battle.add_group(CombatGroup("Orcs of Baz", [Combatant("Orc", hp=32)], 1))
# ngroups = input_int('Number of enemy groups:')
# for i in range(1, ngroups+1):
# print("Adding enemy group {}".format(i))
# battle.add_group(CombatGroup.from_input())
while True: while True:
do_prompt() do_prompt()
@ -141,9 +201,9 @@ def do_prompt():
elif comm == 'a': elif comm == 'a':
print('Sorry, this is still a stub function.') print('Sorry, this is still a stub function.')
elif comm == 'l': elif comm == 'l':
do_list_combatants() battle.list_combatants()
elif comm == 'b': elif comm == 'b':
do_begin_battle() battle.begin()
elif comm == 'd': elif comm == 'd':
print('Sorry, this is still a stub function.') print('Sorry, this is still a stub function.')
elif comm == 'h': elif comm == 'h':
@ -155,14 +215,15 @@ def do_prompt():
elif comm == 'r': elif comm == 'r':
print('Sorry, this is still a stub function.') print('Sorry, this is still a stub function.')
elif comm == 'n': elif comm == 'n':
print('Sorry, this is still a stub function.') battle.next_combatant()
elif comm == 'w': elif comm == 'w':
print('Sorry, this is still a stub function.') print('Sorry, this is still a stub function.')
elif comm == 'q':
sys.exit(0)
def do_help(): def do_help():
print(""" print("""Possible commands:
Possible commands:
? - print this help menu (yay, you already figured that one out) ? - print this help menu (yay, you already figured that one out)
a - add more combatants (works during battle) a - add more combatants (works during battle)
l - list current combatants l - list current combatants
@ -174,33 +235,9 @@ 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 combat group's turn) n - next (end the current combat group's turn)
w - wait (remove a combatant from the initiative order and into a separate pool) w - wait (remove a combatant from the initiative order and into a separate pool)
q - quit
""") """)
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__': if __name__ == '__main__':
main() main()