Exemple #1
0
 public void AddGeneOccurence(Gene gene, int occurences)
 {
     if (GeneOccurences.ContainsKey(gene.geneString))
     {
         GeneOccurences[gene.geneString] += occurences;
         genePossibilities += occurences;
     }
     else
     {
         GeneOccurences.Add(gene.geneString, occurences);
         genePossibilities += occurences;
     }
 }
 public AnimalData()
 {
     AnimalId      = 0;
     SpeciesId     = 0;
     NameId        = 0;
     Name          = "";
     IsFertile     = true;
     SizeGene      = new Gene();
     LongevityGene = new Gene();
     UnknownGene_1 = new Gene();
     FertilityGene = new Gene();
     ImmunityGene  = new Gene();
     UnknownGene_2 = new Gene();
 }
        private void ReadAnimalData(Stream stream, List <string> stringsList)
        {
            byte[] buffer = new byte[1024];
            stream.Seek(serializationSection_start, SeekOrigin.Begin);
            stream.Read(buffer, 0, 1);
            Debug.WriteLine("Expected number of entries: " + (int)buffer[0]);

            while (true)
            {
                long animalDataBlock_Start = stream.Position;
                stream.Read(buffer, 0, 1024);

                if (buffer[0] == 0x00)
                {
                    break;
                }

                ReadOnlySpan <byte> ro   = buffer;
                long animalDataBlock_End = animalDataBlock_Start + ro.IndexOf(animalSerialization_endBytes) + animalSerialization_endBytes.Length;

                AnimalData animal = new AnimalData();
                Animals.Add(animal);

                if (Gamemode == Gamemode.FRANCHISE)
                {
                    if (Helper.CheckFirstFourBits(buffer[0], 0xC0))
                    {
                        animal.AnimalId = Helper.ConvertLastThreeBytes(buffer[0], buffer[1]);

                        if (Helper.CheckFirstFourBits(buffer[2], 0xC0))
                        {
                            animal.SpeciesId = Helper.ConvertLastThreeBytes(buffer[2], buffer[3]);
                        }
                        else
                        {
                            animal.SpeciesId = buffer[2];
                        }
                    }
                    else
                    {
                        animal.AnimalId  = buffer[0];
                        animal.SpeciesId = buffer[1];
                    }
                }
                else
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        animal.AnimalId = BitConverter.ToUInt32(buffer.Skip(1).Take(4).Reverse().ToArray(), 0);
                    }
                    else
                    {
                        animal.AnimalId = BitConverter.ToUInt32(buffer.Skip(1).Take(4).ToArray(), 0);
                    }
                    animal.SpeciesId = buffer[5];
                }

                if (stringsList.Count >= animal.SpeciesId)
                {
                    animal.Species = stringsList[(int)animal.SpeciesId];
                }

                int genePos = 0;
                IEnumerable <byte> geneSequence = FallbackGeneSearch(buffer, stream, ref genePos);

                // 8 bytes that we will use to find the animal's name and verify genes
                byte[] geneIdBytes = new byte[8];
                for (int i = genePos; i < buffer.Length; i++)
                {
                    if (Helper.CheckFirstFourBits(buffer[i], 0x80))
                    {
                        geneIdBytes = buffer.Skip(i).Take(8).ToArray();
                        break;
                    }
                }

                animal.GeneId = BitConverter.ToUInt64(geneIdBytes, 0);

                stream.Seek(datastoreSection_start, SeekOrigin.Begin);
                long geneIdPosition = Helper.Seek(stream, geneIdBytes, datastoreSection_end);

                byte[] buffer2 = new byte[60];
                stream.Seek(geneIdPosition + 8, SeekOrigin.Begin);
                stream.Read(buffer2, 0, 60);
                ro = buffer2;

                //Verify gene, both should be the same; only type of error should be gene being offset by some number of bytes
                // If they don't match, check if a subset of the 1st gene sequence is within the 2nd gene sequence
                // else, keep checking ahead for the ID and repeat
                if (ro.IndexOf(geneSequence.ToArray()) == -1)
                {
                    if (ro.IndexOf(geneSequence.Skip(8).ToArray()) != -1)
                    {
                        geneSequence = ro.ToArray();
                    }
                    else
                    {
                        long geneIdPosition2 = Helper.Seek(stream, geneIdBytes, datastoreSection_end);
                        while (geneIdPosition2 != -1)
                        {
                            stream.Seek(geneIdPosition2 + 8, SeekOrigin.Begin);
                            stream.Read(buffer2, 0, 60);
                            ro = buffer2;
                            if (ro.IndexOf(geneSequence.ToArray()) == -1 && ro.IndexOf(geneSequence.Skip(8).ToArray()) != -1)
                            {
                                geneIdPosition2 = Helper.Seek(stream, geneIdBytes, datastoreSection_end);
                            }
                            else
                            {
                                geneIdPosition = geneIdPosition2;
                                break;
                            }
                        }
                    }
                }

                if (Gene.IsValidGeneSequence(geneSequence))
                {
                    animal.SetGene(geneSequence);
                }

                animal.Name = ReadAnimalDataStore(animal, stream, geneIdPosition, AnimalData.GENE_LENGTH * 5 + geneIdBytes.Length);

                stream.Seek(animalDataBlock_End, SeekOrigin.Begin);
            }

            Debug.WriteLine("Found " + Animals.Count);
        }
        public static Dictionary <int, List <Gene> > CalculateGenePairings(AnimalData female, AnimalData male, GeneType geneType)
        {
            byte[] femaleGene;
            byte[] maleGene;
            switch (geneType)
            {
            case GeneType.SIZE:
                femaleGene = female.SizeGene.geneBytes;
                maleGene   = male.SizeGene.geneBytes;
                break;

            case GeneType.LONGEVITY:
                femaleGene = female.LongevityGene.geneBytes;
                maleGene   = male.LongevityGene.geneBytes;
                break;

            case GeneType.UNKNOWN_1:
                return(null);

            case GeneType.FERTILITY:
                femaleGene = female.FertilityGene.geneBytes;
                maleGene   = male.FertilityGene.geneBytes;
                break;

            case GeneType.IMMUNITY:
                femaleGene = female.ImmunityGene.geneBytes;
                maleGene   = male.ImmunityGene.geneBytes;
                break;

            case GeneType.UNKNOWN_2:
                return(null);

            default:
                return(null);
            }
            Dictionary <int, List <Gene> > pairings = new Dictionary <int, List <Gene> >();

            Gene[] genes = new Gene[4];
            genes[0] = new Gene(femaleGene.Take(6).Concat(maleGene.Take(6)).ToArray());
            genes[1] = new Gene(femaleGene.Skip(6).Take(6).Concat(maleGene.Take(6)).ToArray());
            genes[2] = new Gene(femaleGene.Take(6).Concat(maleGene.Skip(6).Take(6)).ToArray());
            genes[3] = new Gene(femaleGene.Skip(6).Take(6).Concat(maleGene.Skip(6).Take(6)).ToArray());
            switch (geneType)
            {
            case GeneType.SIZE:
            case GeneType.LONGEVITY:
                foreach (Gene gene in genes)
                {
                    int value = (int)(gene.GetGeneValue(GeneValueType.HOMOGENEITY) * 100f);
                    if (!pairings.ContainsKey(value))
                    {
                        pairings.Add(value, new List <Gene>()
                        {
                            gene
                        });
                    }
                    else
                    {
                        pairings[value].Add(gene);
                    }
                }
                break;

            case GeneType.FERTILITY:
            case GeneType.IMMUNITY:
                foreach (Gene gene in genes)
                {
                    int value = (int)(gene.GetGeneValue(GeneValueType.DIVERSITY) * 100f);
                    if (!pairings.ContainsKey(value))
                    {
                        pairings.Add(value, new List <Gene>()
                        {
                            gene
                        });
                    }
                    else
                    {
                        pairings[value].Add(gene);
                    }
                }
                break;

            default:
                return(null);
            }

            return(pairings);
        }