Simulator Bug

I found a problem with the GalCiv Battle Simulator that is available from the metaverse link. While it is a useful utility, it reports that the victorius ship will have a bit more hitpoints than it does after winning a battle. This bug only occurrs on close calls like 50-50 chances.

In the average hitpoint calculation, it only reports the result assuming that the victorius ship had a 100% chance of winning. From this, the reader incorrectly assumes that a Ranger (60/12/6) attacking a Dreadnought (50/12/8) would have an average of 16 hitpoints remaining if it was victorius, instead of the actual average of 2.

For the record, the bug is easy to fix - I've done so myself, and can submit the changes as soon as one of the webmasters is interested.
176 views 3 replies
Reply #2 Top
Okay, here's the change to the function.


function Simulate() {
var trials = 1000;
var aWin = 0;
var dWin = 0;
var tHpAvg = 0;
var aAtt = parseInt( document.frmBSim.attackerAtt.value );
var aDef = parseInt( document.frmBSim.attackerDef.value );
var dAtt = parseInt( document.frmBSim.defenderAtt.value );
var dDef = parseInt( document.frmBSim.defenderDef.value );
var aHpOrig = parseInt( document.frmBSim.attackerHp.value );
var dHpOrig = parseInt( document.frmBSim.defenderHp.value );
var resultMsg = "";

if ( isNaN( aHpOrig ) || isNaN( aAtt ) || isNaN( aDef ) ||
isNaN( dHpOrig ) || isNaN( dAtt ) || isNaN( dDef ) ) {
alert( "Numeric values are required for all input fields." );
return false;
}

for ( var i = 0; i dAtt ) ? dAtt + Math.floor( dDef / 2 ) : dAtt;

for ( var i = 0; i 0 && dHp > 0 ) {
// Attacker attacks
var aRoll = rand( aAtt + 1 )-1 ;
var dRoll = rand( dDef + 1 ) -1;
if ( aRoll > dRoll ) {
dHp = dHp - ( aRoll - dRoll );
if ( dHp 0 ) {
dRoll = rand( dAtt + 1 ) -1;
aRoll = rand( aDef + 1 ) -1;
if ( dRoll > aRoll ) {
aHp = aHp - ( dRoll - aRoll );
if ( aHp dHp ) {
aWin++;
/* aHpAvg += aHp; */
tHpAvg += aHp;
}
else {
dWin++;
/* dHpAvg += dHp; */
tHpAvg -= dHp;
}
}

// Calculate statistics and display results
/* aHpAvg = ( aWin > 0 ) ? Math.round( aHpAvg /= aWin ) : 0; */
/* dHpAvg = ( dWin > 0 ) ? Math.round( dHpAvg /= dWin ) : 0; */
tHpAvg = Math.round( tHpAvg /= (aWin + dWin) );

if ( aWin >= dWin ) {
resultMsg += "The attacking ship is expected to win "
+ ( Math.round( aWin / trials * 10000 ) / 100 )
+ "% of engagements";
if ( tHpAvg > 0) {
resultMsg +=", with an average of "+tHpAvg+" hit points remaining."
} else if (tHpAvg 0) {
resultMsg +=", with an average of "+tHpAvg+" hit points remaining.";
if ( dHpOrig - tHpAvg >= 1 ) {
resultMsg += "\n\nOn average, it would take "
+ Math.ceil( dHpOrig / ( dHpOrig - tHpAvg ) )
+ " attacking ships to destroy one defender.";
}
} else if (tHpAvg < (-dHpOrig / 10) ) {
resultMsg +=", but isn't effective in holding the enemy back.";
} else {
resultMsg +=". The victory is marginal.";
}
}
document.frmBSim.results.value = resultMsg;
};


[Message Edited]
Reply #3 Top
For some reason, the forum doesn't seem to like the <pre> or <code> tags. I'd love to make it nice and neat in such a way that it can be cut-and-pasted, but those tags kill any oppertunity to do so - unless you open the page source and manually edit out the <br>s...