triplea-game / triplea

TripleA is a turn based strategy game and board game engine, similar to Axis & Allies or Risk.
https://triplea-game.org/
GNU General Public License v3.0
1.33k stars 392 forks source link

CasualtyOrderOfLosses#sortUnitsForCasualtiesWithSupport can build identical keys for different unit type lists #7020

Closed trevan closed 4 years ago

trevan commented 4 years ago

How can the problem be recreated?

Play the save game.

In CasualtyOrderOfLosses#sortUnitsForCasualtiesWithSupport, a key is generated to look up in CasualtyOrderOfLosses#oolCache. This key is generated by summing up all of the hash codes of the types in the targets.

In Warcraft War Heroes, there are some combination of types that have the same hashcode as other combinations of types.

An example is:

Unit Type HashCode
Footmen 984658568
Veteran-Pikemen 2139503253
SUM 3124161821

And

Unit Type HashCode
Pikemen 1093844931
Veteran-Footmen 2030316890
SUM 3124161821

Do you have any ideas for an expected fix?

The hashcode of unittype either needs to be changed to not just be the name or the key needs to be more than a sum.

Attach a Save Game

wow-u25.zip

If playing a prerelease, which version are you using?

Game Version: Master

If playing a prerelease, does this happen on the latest release?

Is there anything else we should know?

This causes an exception java.lang.IllegalStateException: sortedTargetsToPickFrom must have the same size as targetsToPickFrom list so it is visible.

DanVanAtta commented 4 years ago

This is a good spot. :eagle: The manual computation of hashcode likely should be replaced with either a hashcode builder or lombok EqualsAndHashCode

trevan commented 4 years ago

I've gone through git history and the code to calculate the key was better. But during a refactoring in 57f11454522a762d2ff273ed445d29b0ee17725e (back in 2015), the key calculation was converted into the sum and has been that way since.