Re-rolling Battles

If you find a bug please report it here

Re-rolling Battles

Postby KGB » Tue May 20, 2014 2:20 pm

Piranha,

I was doing some random simulation stuff here at work and doing some smoothing similar to what is done with the 90% rule. If I recall how you are doing it, I think there is a major bug there.

My understanding is you are doing the battle and then looking at the result. If the result is in the lower (0-9) or upper (91-100) range you are re-rolling the entire battle but you aren't tracking the original winner. So that a re-roll could switch the winner. Allowing that switch of winners is where I discovered the bug is.

For example lets take an 88% battle for the attacker. He wins 88% of the time and the defender wins 12%. I wrote a simple coding example of:

Code: Select all
int attacker = 0;  // Total attacker wins
int defender = 0;  // Total defender wins
int total=10000;   // Total simulation
int attWin = 88;   // Attacker win %

srand((int)time(NULL));
for (int i=0; i<total; i++)
{
   int result = (rand()%100)+1;   // Battle result: 1-100
   if (result <= attWin )
   {
      attacker++;
   }
   else
   {
      defender++;
   }
}
cout << "attacker " << attacker << endl;
cout << "defender " << defender << endl;
cout << "percentage " << (float)attacker/total*100 << endl;


Running this code gives a winning % of 88% for the attacker as expected.

Now I added the smoothing code. I just re-roll anything <10 or >90.

Code: Select all
int attacker = 0;  // Total attacker wins
int defender = 0;  // Total defender wins
int total=10000;   // Total simulation
int attWin = 88;   // Attacker win %

srand((int)time(NULL));
for (int i=0; i<total; i++)
{
   bool valid = false;
   while (!valid)
   {
      int result = (rand()%100)+1;   // Battle result: 1-100
      if (result > 10 && result <= 90)   // Check battle is in the middle 80%
      {
         if (result <= attWin )
         {
            attacker++;
         }
         else
         {
            defender++;
         }
         valid = true;
      }
   }
}
cout << "attacker " << attacker << endl;
cout << "defender " << defender << endl;
cout << "percentage " << (float)attacker/total*100 << endl;


Now the attacker win % jumps to 97.5% instead of being at 88% where it should be.

The reason is because if you just re-roll and allow the winner to be switched then the true valid outcomes are 78/80 for the attacker and 2/80 for the defender and 78/80 = 97.5%.

Since re-rolling is the easiest for you code wise, you'll need to track the winner and re-roll until you get a valid result AND the original winner.

KGB
KGB
 
Posts: 3030
Joined: Tue Feb 16, 2010 12:06 am

Re: Re-rolling Battles

Postby piranha » Tue May 20, 2014 3:17 pm

You are right.

But if we just make a reroll again if the other side wins and the allowed percentage to win is small it will be another problem. If you have 11% chance to win there will be tons of rerolls until you hit that 1% and that will make battles take a very long time and the server to be slowed down.

Any ideas about that?
We are going to think about it but any suggestions are welcome.
User avatar
piranha
Site Admin
 
Posts: 1188
Joined: Fri Feb 12, 2010 9:44 pm

Re: Re-rolling Battles

Postby KGB » Tue May 20, 2014 4:47 pm

Piranha,

How much RAM does your server have?

I know you roll 1000 battles to estimate the winning % before you make the actual roll. What if you stored the complete results (which units died and how) for all 1000 battles in 2 arrays (attacker and defender) based on who won the battle.

Then when you make the actual battle roll if it needs to be re-rolled, just start going through the attacker (or defender depending on who won) array and select the first valid battle.

KGB
KGB
 
Posts: 3030
Joined: Tue Feb 16, 2010 12:06 am

Re: Re-rolling Battles

Postby SnotlinG » Wed May 21, 2014 11:47 am

Im currently looking at a solution:
During the battle keep track of the minimum/maximum accepted survivors from both sides.
And when you reach the minimum accepted value, that unit is guaranteed wins on all the dice-rolls.

Or if you reach the last opponent unit and the other side has more units than the max accepted value (call them deltaX units), the last unit is guaranteed wins on all dice-rolls. (however if last unit should have died against this deltaX unit, it should then auto-lose against the first possible unit, as to not affect the end result)

I think this should give the desired end-result outcome.
One problem is the ambush though, since that can have 100% chance this strategy above cant take ambush into consideration.
SnotlinG
 
Posts: 2148
Joined: Sat Feb 13, 2010 12:42 am

Re: Re-rolling Battles

Postby KGB » Wed May 21, 2014 2:38 pm

SnotlinG,

So you are going to adjust the battle rolls *after* the battle is complete? That's going to look a bit strange in the battle replay if 1 unit auto-kills a whole bunch of enemy units especially as you note, when ambush is involved.

I hope this works. It sounds complicated and prone to bugs and will be quite hard to test to make sure you got all the cases. For example sometimes you may have to completely reverse who won the battle if a side with <10% wins. Other times you may have to kill more units from the winner than actually died if too many survived.

That's why I suggested keeping all 1000 results from your initial battle estimate. There should be plenty of valid results in there for both sides. On the *very* small chance there isn't (4/100000 is the chance a 1/100 event doesn't happen in 1000 tries) you can just use any valid result from the list of 1000 results.

KGB
KGB
 
Posts: 3030
Joined: Tue Feb 16, 2010 12:06 am

Re: Re-rolling Battles

Postby SnotlinG » Wed May 21, 2014 2:53 pm

Not *after*, but *during* the battle.
As we when the battle starts already know the accepted limits of min/max units on both sides, we can during the battle make sure we dont go below the min-values, and also make sure we dont let the battle finish when we are above the accepted max units.

I just posted a News post about this :-)
It will include a change of Ambush to have a max limit of 95% also.

Example 1:
I have a 8 unit stack attacking 1 unit. Accepted result is 2-6 surviving units for me.
If I during battle reach the stage (in the while loop) that the defender unit has killed off all my units except 2, I will then re-roll that battle-dice until my second unit wins, i.e. giving me a endresult where I win the battle with 2 surviving units.

Example 2:
I have a 8 unit stack attacking 1 unit. Accepted result is 2-6 surviving units for me.
If I during battle reach the stage that the surviving unit has 1 HP still, and I still have 8 units left. I cant let the defender unit die (since this would give me an invalid result). I will re-roll that battledice until the defender wins*, until I have 6 units left in the battle.

* If my attacker unit wins this battle during the re-rolls, the defender is dead, meaning I should have the outcome with 6 surviving units, i.e. when reaching my unit number 6, I will then re-roll until unit 6 wins.
If however my attacker units dont win any of the *-dice (i.e. no re-roll will actually happen), the battle for my 6th unit will be performed as usual, i.e. if I have bad luck it might die also...
SnotlinG
 
Posts: 2148
Joined: Sat Feb 13, 2010 12:42 am

Re: Re-rolling Battles

Postby KGB » Wed May 21, 2014 4:53 pm

SnotlinG,

OK. That makes perfect sense.

I can't wait to watch a unit go into 'Rambo' mode and kill off 3-4 enemy units to make an acceptable result :D

KGB
KGB
 
Posts: 3030
Joined: Tue Feb 16, 2010 12:06 am

Re: Re-rolling Battles

Postby Chazar » Thu May 22, 2014 11:35 am

Interesting stuff, but I always assumed this was intended: any chance at 90% is changed to 100%, so I assumed it natural that 89% is changed to 97% overall.

So currently the graph was smooth, but with the suggested fix, it will make a large jump at the 10% / 90% (well or the steps in between, according to the involved unit strengths.

Currently, the difference between 89% and 90% is diminutive. With the proposed change, difference will be a huge 10%. Ouch!

Also, seeing a unit in Rambo mode will certainly confuse players quite a lot, which might turn them away. Especially surviving against high ambush units will be weird. People will get suspicious if they see such oddities often, and might be turned away before actually reading up on the implementation of the 90% rules thing. Hm.
Chazar
 
Posts: 670
Joined: Tue Feb 28, 2012 7:51 pm

Re: Re-rolling Battles

Postby SnotlinG » Thu May 22, 2014 12:57 pm

Chazar,
What will be an effect of this update is also that a side that has 10% chance to win might never be able to win, but if they hit their 10% spot in the battle they will atleast inflict maximum damage to the opponent (since that result is changed to their best allowed outcome). Instead as currently when it is re-rolled, giving the other side a good chance of getting a decent result instead.

We are still open to feedback and ideas though. :-)
But presenting a battle with 80% win-chance and then the real win chance is 87.5 also seems very weird?
SnotlinG
 
Posts: 2148
Joined: Sat Feb 13, 2010 12:42 am

Re: Re-rolling Battles

Postby piranha » Thu May 22, 2014 1:12 pm

It's an interesting point you bring up.

Does this update improve the game or not?

The result is that if you have 80% to win you have 80% and not 87.5% which sounds more honest. But the result will probably feel like there is more luck involved since you will lose when you have a percentual advantage more often.
User avatar
piranha
Site Admin
 
Posts: 1188
Joined: Fri Feb 12, 2010 9:44 pm

Next

Return to Bug reports

Who is online

Users browsing this forum: No registered users and 15 guests

cron
Not able to open ./cache/data_global.php