/************************************************************************** .... find the probability of the attacker winning a battle .......... **************************************************************************/ int chance_of_winning(struct unit *attacker, struct unit *defender) { double p; int dfp,afp; int dhp,ahp; int attackpower=get_total_attack_power(attacker,defender); int defensepower=get_total_defense_power(attacker,defender); if (!attackpower) return 0; if (!defensepower) return 100; p=(double)(attackpower)/(double)(attackpower+defensepower); if(is_sailing_unit(defender) && map_get_city(defender->x, defender->y)) { afp=1; /* defending ships in port only get a FP of at most 1 */ } else { afp=get_unit_type(attacker->type)->firepower; }; dfp=get_unit_type(defender->type)->firepower; dhp=defender->hp+afp-1; /* add afp-1 so we round up instead of down */ ahp=attacker->hp+dfp-1; p=sum_binomial(p,dhp/afp,ahp/dfp); return((int)(100*p)); } /************************************************************************** sums the area under a binomial distribution with parameters p and n = x+y-1 from x to n. This is the chance a unit has of winning if the probably of it winning a round is p, the attacker needs to win x times, and the defender needs to win y times. **************************************************************************/ double sum_binomial(double p, int x, int y) { double q=1-p; double r,b,s; int n=x+y-1,k; if(x