Biomedef commonalityOfAnimal isnt working correctly

Started by skullywag, May 15, 2016, 05:33:16 AM

Previous topic - Next topic

skullywag

If a mod defines WildBiomes in the RaceDef of a pawn, if more than 1 wildBiome is added the game will error with:

System.ArgumentException: An element with the same key already exists in the dictionary.
  at System.Collections.Generic.Dictionary`2[Verse.PawnKindDef,System.Single].Add (Verse.PawnKindDef key, Single value) [0x00000] in <filename unknown>:0
  at RimWorld.BiomeDef.CommonalityOfAnimal (Verse.PawnKindDef animalDef) [0x00000] in <filename unknown>:0
  at RimWorld.BiomeDef+<>c__Iterator70.MoveNext () [0x00000] in <filename unknown>:0
  at RimWorld.WildSpawner.get_DesiredAnimalDensity () [0x00000] in <filename unknown>:0
  at RimWorld.WildSpawner.get_DesiredTotalAnimalWeight () [0x00000] in <filename unknown>:0
  at RimWorld.WildSpawner.get_AnimalEcosystemFull () [0x00000] in <filename unknown>:0
  at RimWorld.Genstep_Animals.Generate () [0x00000] in <filename unknown>:0
  at Verse.MapGenerator.GenerateContentsIntoCurrentMap (Verse.MapGeneratorDef def) [0x00000] in <filename unknown>:0


due to the logic in this method not checking the biome so adding the animal to all biomes, to fix:


public float CommonalityOfAnimal(PawnKindDef animalDef)
    {
      if (this.cachedAnimalCommonalities == null)
      {
        this.cachedAnimalCommonalities = new Dictionary<PawnKindDef, float>();
        for (int index = 0; index < this.wildAnimals.Count; ++index)
          this.cachedAnimalCommonalities.Add(this.wildAnimals[index].animal, this.wildAnimals[index].commonality);
        foreach (PawnKindDef pawnKindDef in DefDatabase<PawnKindDef>.AllDefs)
        {
          if (pawnKindDef.RaceProps.wildBiomes != null)
          {
            for (int index = 0; index < pawnKindDef.RaceProps.wildBiomes.Count; ++index)
if( pawnKindDef.RaceProps.wildBiomes[index].biome == this )
                  this.cachedAnimalCommonalities.Add(pawnKindDef.RaceProps.wildBiomes[index].animal, pawnKindDef.RaceProps.wildBiomes[index].commonality);
          }
        }
      }
      float num;
      if (this.cachedAnimalCommonalities.TryGetValue(animalDef, out num))
        return num;
      return 0.0f;
    }


The same issue would also effects plants and diseases if added from another def other than the biomdef. Im going to Detour this in CCL for A13 but would be good to fix this for A14.
Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

ison