use of forestry.api.genetics.IChromosome in project ForestryMC by ForestryMC.
the class TreeGenome method calculateMatchesTemplateGenome.
private boolean calculateMatchesTemplateGenome() {
IAlleleTreeSpecies primary = getPrimary();
IAllele[] template = getSpeciesRoot().getTemplate(primary);
IChromosome[] chromosomes = getChromosomes();
for (int i = 0; i < chromosomes.length; i++) {
IChromosome chromosome = chromosomes[i];
String templateUid = template[i].getUID();
IAllele primaryAllele = chromosome.getPrimaryAllele();
if (!primaryAllele.getUID().equals(templateUid)) {
return false;
}
IAllele secondaryAllele = chromosome.getSecondaryAllele();
if (!secondaryAllele.getUID().equals(templateUid)) {
return false;
}
}
return true;
}
use of forestry.api.genetics.IChromosome in project ForestryMC by ForestryMC.
the class Genome method getChromosomes.
private static IChromosome[] getChromosomes(NBTTagCompound genomeNBT, ISpeciesRoot speciesRoot) {
NBTTagList chromosomesNBT = genomeNBT.getTagList("Chromosomes", 10);
IChromosome[] chromosomes = new IChromosome[speciesRoot.getDefaultTemplate().length];
String primarySpeciesUid = null;
String secondarySpeciesUid = null;
for (int i = 0; i < chromosomesNBT.tagCount(); i++) {
NBTTagCompound chromosomeNBT = chromosomesNBT.getCompoundTagAt(i);
byte chromosomeOrdinal = chromosomeNBT.getByte(SLOT_TAG);
if (chromosomeOrdinal >= 0 && chromosomeOrdinal < chromosomes.length) {
IChromosomeType chromosomeType = speciesRoot.getKaryotype()[chromosomeOrdinal];
Chromosome chromosome = Chromosome.create(primarySpeciesUid, secondarySpeciesUid, chromosomeType, chromosomeNBT);
chromosomes[chromosomeOrdinal] = chromosome;
if (chromosomeOrdinal == speciesRoot.getSpeciesChromosomeType().ordinal()) {
primarySpeciesUid = chromosome.getPrimaryAllele().getUID();
secondarySpeciesUid = chromosome.getSecondaryAllele().getUID();
}
}
}
return chromosomes;
}
use of forestry.api.genetics.IChromosome in project ForestryMC by ForestryMC.
the class Genome method checkChromosomes.
private void checkChromosomes(IChromosome[] chromosomes) {
if (chromosomes.length != getDefaultTemplate().length) {
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template.\n%s", getSpeciesRoot().getUID(), chromosomesToString(chromosomes));
throw new IllegalArgumentException(message);
}
IChromosomeType[] karyotype = getSpeciesRoot().getKaryotype();
for (int i = 0; i < karyotype.length; i++) {
IChromosomeType chromosomeType = karyotype[i];
IChromosome chromosome = chromosomes[i];
if (chromosome == null) {
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " + "Missing chromosome '%s'.\n%s", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
throw new IllegalArgumentException(message);
}
IAllele primary = chromosome.getPrimaryAllele();
if (primary == null) {
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " + "Missing primary allele for '%s'.\n%s", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
throw new IllegalArgumentException(message);
}
IAllele secondary = chromosome.getSecondaryAllele();
if (secondary == null) {
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " + "Missing secondary allele for '%s'.\n%s", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
throw new IllegalArgumentException(message);
}
Class<? extends IAllele> chromosomeAlleleClass = chromosomeType.getAlleleClass();
if (!chromosomeAlleleClass.isAssignableFrom(primary.getClass())) {
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " + "Incorrect type for primary allele '%s'.\n%s.", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
throw new IllegalArgumentException(message);
}
if (!chromosomeAlleleClass.isAssignableFrom(secondary.getClass())) {
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " + "Incorrect type for secondary allele '%s'.\n%s.", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
throw new IllegalArgumentException(message);
}
}
}
use of forestry.api.genetics.IChromosome in project ForestryMC by ForestryMC.
the class Genome method isGeneticEqual.
@Override
public boolean isGeneticEqual(IGenome other) {
IChromosome[] genetics = other.getChromosomes();
if (chromosomes.length != genetics.length) {
return false;
}
for (int i = 0; i < chromosomes.length; i++) {
IChromosome chromosome = chromosomes[i];
IChromosome otherChromosome = genetics[i];
if (chromosome == null && otherChromosome == null) {
continue;
}
if (chromosome == null || otherChromosome == null) {
return false;
}
if (!chromosome.getPrimaryAllele().getUID().equals(otherChromosome.getPrimaryAllele().getUID())) {
return false;
}
if (!chromosome.getSecondaryAllele().getUID().equals(otherChromosome.getSecondaryAllele().getUID())) {
return false;
}
}
return true;
}
use of forestry.api.genetics.IChromosome in project ForestryMC by ForestryMC.
the class Butterfly method mutateSpecies.
@Nullable
private static IChromosome[] mutateSpecies(World world, IButterflyNursery nursery, IGenome genomeOne, IGenome genomeTwo) {
IChromosome[] parent1 = genomeOne.getChromosomes();
IChromosome[] parent2 = genomeTwo.getChromosomes();
IGenome genome0;
IGenome genome1;
IAllele allele0;
IAllele allele1;
if (rand.nextBoolean()) {
allele0 = parent1[EnumButterflyChromosome.SPECIES.ordinal()].getPrimaryAllele();
allele1 = parent2[EnumButterflyChromosome.SPECIES.ordinal()].getSecondaryAllele();
genome0 = genomeOne;
genome1 = genomeTwo;
} else {
allele0 = parent2[EnumButterflyChromosome.SPECIES.ordinal()].getPrimaryAllele();
allele1 = parent1[EnumButterflyChromosome.SPECIES.ordinal()].getSecondaryAllele();
genome0 = genomeTwo;
genome1 = genomeOne;
}
for (IButterflyMutation mutation : ButterflyManager.butterflyRoot.getMutations(true)) {
float chance = mutation.getChance(world, nursery, allele0, allele1, genome0, genome1);
if (chance > rand.nextFloat() * 100) {
return ButterflyManager.butterflyRoot.templateAsChromosomes(mutation.getTemplate());
}
}
return null;
}
Aggregations