From ac09d41675b2c541c86412a86c39e095b6ef02b9 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 21 Mar 2012 13:58:59 -0400 Subject: [PATCH] battleman.py: Started implementing actual commands. --- battleman.py | 170 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 130 insertions(+), 40 deletions(-) diff --git a/battleman.py b/battleman.py index db31327..ee491c9 100755 --- a/battleman.py +++ b/battleman.py @@ -6,9 +6,33 @@ # 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 Combatant(): - def __init__(self, name, hp, pc=False, surges=0, ap=0, sw=0, recharges=[], count=1): + @classmethod + def from_input(cls): + name = raw_input("Name: ") + hp = int(raw_input("hp: ")) + + 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) + + return Combatant(name, hp, pc=0, surges=surges, recharges=recharges) + + + def __init__(self, name, hp, init_mod, pc=False, surges=0, ap=0, sw=0, recharges=[]): self.name = name self.hp = hp self.pc = pc @@ -16,71 +40,137 @@ class Combatant(): self.ap = ap self.sw = sw self.recharges = [] + self.init_mod = init_mod self.init = 0 self.conditions = [] - self.count = count + + + 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_condition(self, cond_type, duration): - condition.type = cond_type - condition.duration = duration + + 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): - add_msg("Initiative: " + self) + print("{} has initiative.".format(self)) + def __str__(self): return "{} ({hp} hp)".format(self.name, hp=self.hp) - -def combatant_from_input(): - name = raw_input("Name: ") - hp = int(raw_input("hp: ")) - - 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 = 1 - - return Combatant(name, hp, pc=0, surges=surges, recharges=recharges, count=count) +# data about the battle - includes combatant list, etc +battle = {} def main(): - combatants = [] + battle['combatants'] = [] + battle['current'] = None + battle['round'] = -1 # hard-coding test cases for now. # Eventually, use a state-saving text file that's easy to edit - combatants.append(Combatant("Adele", hp=26, pc=True, surges=8, sw=1)) - combatants.append(Combatant("Aristaire", hp=20, pc=True, surges=6, sw=1)) + battle['combatants'].append(Combatant("Adele", hp=26, init_mod=2, pc=True, surges=8, sw=1)) + battle['combatants'].append(Combatant("Aristaire", hp=20, init_mod=0, pc=True, surges=6, sw=1)) ngroups = int(raw_input("Num enemy groups: ")) for i in range(1, ngroups+1): print("Adding enemy group {}".format(i)) - combatants.append(combatant_from_input()) + battle['combatants'].append(Combatant.from_input()) - for c in combatants: + # debug + for c in battle['combatants']: print(c) -# while True: -# do_prompt() + while True: + do_prompt() + + +def do_prompt(): + comm = raw_input('> ') + + # help + if 'comm' == '?': + do_help() + # add more combatants + elif 'comm' == 'a': + pass + # list combatants with indices + elif 'comm' == 'l': + do_list_combatants() + # begin the battle + elif 'comm' == 'b': + do_begin_battle() + # deal damage to a combatant + elif 'comm' == 'd': + pass + # heal damage from a combatant + elif 'comm' == 'h': + pass + # use a healing surge + elif 'comm' == 's': + pass + # apply a condition + elif 'comm' == 'c': + pass + # remove a condition early + elif 'comm' == 'r': + pass + # end the current combatant's turn + elif 'comm' == 'n': + pass + # remove the combatant from the initiative order (delay/prepare) + elif 'comm' == 'w': + pass + + +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 combatant's turn) +w - wait (remove the combatant from the initiative order and into a separate pool) +""") + + +def do_list_combatants(): + index = 0 + for c in battle['combatants']: + print('{}: {}'.format(index, c.name)) + index += 1 + + +def do_begin_battle(): + if current != None: + print("Error: battle is already running") + return + + for c in battle['combatants']: + if c.pc: + raw_input('Initiative for {}: '.format(c.name)) + else: + c.roll_init() + + battle['combatants'].sort(reverse=True) + current = 0 if __name__ == '__main__':