import sys;
import random;
import math;

def total_spite(num_dice):
   dice_array = range(num_dice); # Initialize array

   dice_array = [random.randint(1,6) for die in dice_array];

   total = 0;
   spite = 0;

   for die in dice_array:
       total = total+die;
       if (die==6):
           spite = spite + 1;


   return (total,spite);



num_trials = 10;

if (len(sys.argv) == 1):
   print "Usage: python tnt_combat.py <combat_file> [number trials]"
   sys.exit(0)


combat_filename = sys.argv[1];
if (len(sys.argv)>2):
   num_trials=int(sys.argv[2]);

cn_lost = range(num_trials);    # Hold CN lost for each trial
rounds_fighting = range(num_trials); # Count how many rounds it takes
pc_lost = range(num_trials);         # How many times PCs lost
npc_lost = range(num_trials);        # How many times monster lost

# Parse combat file
combat_file = open(combat_filename, "r");
line_idx = 0;
pc_dice = 0;
pc_adds = 0;
pc_armor = 0;
pc_cn = 0;
npc_mr = 0;
npc_armor = 0;

for line in combat_file:
   if line.startswith("#"):    # Comment line
       continue;
   curr_line = line.split(","); # Comma-separated
   if (line_idx == 0):         # PC Line

       pc_dice = int(curr_line[0]);
       pc_adds = int(curr_line[1]);
       pc_armor = int(curr_line[2]);
       pc_cn = int(curr_line[3]);
       line_idx = line_idx + 1 ;             # Next parse NPC line
   elif (line_idx == 1):
       npc_mr = int(curr_line[0]);
       npc_armor = int(curr_line[1]);
combat_file.close();


for combat_idx in range(num_trials): # Iterate over all trials
   cn_lost[combat_idx] = 0;         # Blank out CN lost
   rounds_fighting[combat_idx] = 0; # Blank out number of rounds
   pc_lost[combat_idx] = 0;
   npc_lost[combat_idx] = 0;   # Blank out losing record

   round_idx = 0;              # No rounds yet
   curr_pc_dice = pc_dice;
   curr_pc_adds = pc_adds;
   curr_pc_armor = pc_armor;
   curr_pc_cn = pc_cn;
   curr_npc_mr = npc_mr;
   curr_npc_armor = npc_armor;
   curr_npc_dice = (curr_npc_mr / 10)+1;

   print "*************"
   while ((curr_pc_cn > 0) and (curr_npc_mr > 0)):
       round_idx = round_idx+1; # Increment round counter

       print "Round {}".format(round_idx);
       print "************"

       curr_npc_adds = int(math.ceil(curr_npc_mr/2.0));

       pc_roll = total_spite(curr_pc_dice);
       pc_hpt = pc_roll[0]+curr_pc_adds;
       pc_spite = pc_roll[1];

       npc_roll = total_spite(curr_npc_dice);
       npc_hpt = npc_roll[0]+curr_npc_adds;
       npc_spite = npc_roll[1];

       print "PCs rolled: {}\nNPCs rolled: {}".format(pc_roll,npc_roll);
       print "PC, NPC HPTs are: {},{}".format(pc_hpt, npc_hpt);
       # Apply spite damage
       cn_lost[combat_idx] += npc_spite; # Log damage taken
       curr_pc_cn = curr_pc_cn - npc_spite;
       curr_npc_mr = curr_npc_mr - pc_spite;

       # Now compute HPT losses
       pc_margin = pc_hpt - npc_hpt;
       print "HPT Margin: {}".format(pc_margin);

       if (pc_margin > 0):     # Players won round
           npc_damage = pc_margin - curr_npc_armor;
           if (npc_damage > 0): # Exceeded armor
               print "Net NPC damage: {}".format(npc_damage);
               curr_npc_mr = curr_npc_mr - npc_damage;
           else:
               print "No net damage to NPC."
       elif (pc_margin < 0): #Players lost round
           pc_damage = (-pc_margin)-curr_pc_armor; # Need to invert HPT difference
           if (pc_damage > 0):                     # Exceeded armor
               print "Net PC damage: {}".format(pc_damage);
               curr_pc_cn = curr_pc_cn - pc_damage;
               cn_lost[combat_idx] += pc_damage;
           else:
               print "No net damage to PCs."
       else:
           pass                # Equal HPTs, so do nothing

       print "New Health: {},{}".format(curr_pc_cn, curr_npc_mr);

       if (curr_pc_cn <= 0):
           pc_lost[combat_idx] = 1;
           print "PC Lost";

       if (curr_npc_mr <= 0):
           npc_lost[combat_idx] = 1;
           print "Monster Lost";
       rounds_fighting[combat_idx] = round_idx;


print "*******************\nSummary\n*****************"
#Now gather statistics

total_lost = 0;
total_won = 0;
both_lost = 0;
winning_cn_lost_sum = 0;
rounds_fighting_sum = 0;

for combat_idx in range(num_trials):
   if ((pc_lost[combat_idx] !=0) and (npc_lost[combat_idx] != 0)):
       both_lost = both_lost + 1;

   if ((pc_lost[combat_idx] != 0) and (npc_lost[combat_idx] == 0)): # PC Lost
       total_lost = total_lost + 1;

   if (pc_lost[combat_idx] == 0):          # PC Won
       total_won = total_won + 1; # Increment count
       winning_cn_lost_sum = winning_cn_lost_sum + cn_lost[combat_idx];
       rounds_fighting_sum = rounds_fighting_sum + rounds_fighting[combat_idx];

print "Both Lost: {}, %{}".format(both_lost, int(((both_lost+0.0)/num_trials)*100));

print "PC Lost: {}, %{}".format(total_lost, int(((total_lost+0.0)/num_trials)*100));
print "PC Won: {}, %{}".format(total_won, int(((total_won+0.0)/num_trials)*100));

if (total_won > 0):
   print "Average Damage Taken on Win: {}".format((winning_cn_lost_sum+0.0)/total_won);
   print "Average Rounds Fought for Win: {}".format((rounds_fighting_sum+0.0)/total_won);