mirror of
				https://github.com/vegapnk/RJW-Genes.git
				synced 2024-08-15 00:23:31 +00:00 
			
		
		
		
	Added a ImmunityExtension
This commit is contained in:
		
							parent
							
								
									9ca57bdfa8
								
							
						
					
					
						commit
						65ff62cbf9
					
				
					 5 changed files with 154 additions and 60 deletions
				
			
		
							
								
								
									
										19
									
								
								Source/Genes/Diseases/Defs/ImmunityAgainstGenesExtension.cs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Source/Genes/Diseases/Defs/ImmunityAgainstGenesExtension.cs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,19 @@
 | 
			
		|||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Verse;
 | 
			
		||||
 | 
			
		||||
namespace RJW_Genes
 | 
			
		||||
{
 | 
			
		||||
    public class ImmunityAgainstGenesExtension : DefModExtension
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// A list of the exact defnames of disease-genes that this extension will make immune against.
 | 
			
		||||
        /// Must perfectly match!
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public List<string> givesImmunityAgainst;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										111
									
								
								Source/Genes/Diseases/DiseaseHelper.cs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								Source/Genes/Diseases/DiseaseHelper.cs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,111 @@
 | 
			
		|||
using rjw;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Verse;
 | 
			
		||||
 | 
			
		||||
namespace RJW_Genes
 | 
			
		||||
{
 | 
			
		||||
    public static class DiseaseHelper
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Checks for a pawn if it is immune against a disease.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="pawn">The pawn for which immunity is checked</param>
 | 
			
		||||
        /// <param name="disease">The genetic disease that is checked against</param>
 | 
			
		||||
        /// <returns>True if the pawn is immune, false if the pawn can be infected by it.</returns>
 | 
			
		||||
        public static bool IsImmuneAgainstGeneticDisease(Pawn pawn, GeneDef disease)
 | 
			
		||||
        {
 | 
			
		||||
            // Case 1: Something is null / not working, return Immune (to have less follow up effects)
 | 
			
		||||
            if (pawn == null || pawn.genes == null) return true;
 | 
			
		||||
            if (disease == null) return true;
 | 
			
		||||
            // Case 1.B: Dead people can spread, but not receive, diseases.
 | 
			
		||||
            if (pawn.Dead) return true;
 | 
			
		||||
 | 
			
		||||
            // Case 2: The pawn has general genetic immunity to diseases
 | 
			
		||||
            if (GeneUtility.HasGeneNullCheck(pawn, GeneDefOf.rjw_genes_genetic_disease_immunity))
 | 
			
		||||
                return true;
 | 
			
		||||
 | 
			
		||||
            // Case 3: The pawn already has the genetic disease
 | 
			
		||||
            if (GeneUtility.HasGeneNullCheck(pawn, disease))
 | 
			
		||||
                return true;
 | 
			
		||||
 | 
			
		||||
            // Case 4: Check all genes if one of them has the Immunity Extension that covers the GeneDef
 | 
			
		||||
            List<Gene> genes = pawn.genes.GenesListForReading;
 | 
			
		||||
            genes = genes.Where(x => pawn.genes.HasActiveGene(x.def)).ToList();
 | 
			
		||||
 | 
			
		||||
            foreach (Gene gene in genes)
 | 
			
		||||
            {
 | 
			
		||||
                ImmunityAgainstGenesExtension ext = gene.def.GetModExtension<ImmunityAgainstGenesExtension>();
 | 
			
		||||
                if (ext != null) { 
 | 
			
		||||
                    foreach (string defname in ext.givesImmunityAgainst)
 | 
			
		||||
                        if (disease.defName == defname)
 | 
			
		||||
                            return true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Case 5: Nothing special happens, so return false (not immune)
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Returns all active Genes with the `GeneticDiseaseExtension`.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="pawn"></param>
 | 
			
		||||
        /// <returns>List of all active Genes with the `GeneticDiseaseExtension` in pawn</returns>
 | 
			
		||||
        public static List<GeneDef> GetGeneticDiseaseGenes(Pawn pawn)
 | 
			
		||||
        {
 | 
			
		||||
            if (pawn != null && pawn.genes != null)
 | 
			
		||||
            {
 | 
			
		||||
                return pawn.genes
 | 
			
		||||
                    .GenesListForReading
 | 
			
		||||
                    .ConvertAll(gene => gene.def)
 | 
			
		||||
                    .Where(genedef => pawn.genes.HasActiveGene(genedef))
 | 
			
		||||
                    .Where(IsGeneticDiseaseGene)
 | 
			
		||||
                    .ToList();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new List<GeneDef>() { };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Checks if the performed sex was penetrative. 
 | 
			
		||||
        /// Condom check is not done here!
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="props">The sexprops </param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static bool IsPenetrativeSex(SexProps props)
 | 
			
		||||
        {
 | 
			
		||||
            if (props == null) return false;
 | 
			
		||||
 | 
			
		||||
            return props.sexType ==
 | 
			
		||||
                xxx.rjwSextype.Vaginal
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Anal
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Oral
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.DoublePenetration
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Fellatio
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Sixtynine;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static bool IsGeneticDiseaseGene(GeneDef geneDef)
 | 
			
		||||
        {
 | 
			
		||||
            if (geneDef == null) return false;
 | 
			
		||||
            GeneticDiseaseExtension diseaseExt = geneDef.GetModExtension<GeneticDiseaseExtension>();
 | 
			
		||||
            return diseaseExt != null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static float LookupDiseaseInfectionChance(GeneDef geneDef)
 | 
			
		||||
        {
 | 
			
		||||
            if (IsGeneticDiseaseGene(geneDef))
 | 
			
		||||
            {
 | 
			
		||||
                GeneticDiseaseExtension diseaseExt = geneDef.GetModExtension<GeneticDiseaseExtension>();
 | 
			
		||||
                return diseaseExt != null ? diseaseExt.infectionChance : 0.0f;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
                return 0.0f;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -29,7 +29,7 @@ namespace RJW_Genes.Genes.Diseases.Patches
 | 
			
		|||
            if (props.usedCondom) return;
 | 
			
		||||
 | 
			
		||||
            // Exit early if settings require penetrative sex, but this is not penetrative sex
 | 
			
		||||
            if (!IsPenetrativeSex(props) && RJW_Genes_Settings.rjw_genes_genetic_disease_spread_only_on_penetrative_sex) return;
 | 
			
		||||
            if (!DiseaseHelper.IsPenetrativeSex(props) && RJW_Genes_Settings.rjw_genes_genetic_disease_spread_only_on_penetrative_sex) return;
 | 
			
		||||
 | 
			
		||||
            ModLog.Debug($"Firing Patch_TransferGeneticDiseases for {pawn} and {partner}");
 | 
			
		||||
            TryTransferGeneticDiseases(pawn, partner, props);
 | 
			
		||||
| 
						 | 
				
			
			@ -38,74 +38,19 @@ namespace RJW_Genes.Genes.Diseases.Patches
 | 
			
		|||
 | 
			
		||||
        private static void TryTransferGeneticDiseases(Pawn infector, Pawn infected, SexProps props)
 | 
			
		||||
        {
 | 
			
		||||
            if (infected.genes.HasActiveGene(GeneDefOf.rjw_genes_genetic_disease_immunity))
 | 
			
		||||
            {
 | 
			
		||||
                ModLog.Debug($"{infected} is immune to genetic diseases");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            if (infected.Dead)
 | 
			
		||||
            {
 | 
			
		||||
                // Dead people can spread, but not receive, diseases.
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            foreach (GeneDef disease in GetGeneticDiseaseGenes(infector)) {
 | 
			
		||||
            
 | 
			
		||||
            foreach (GeneDef disease in DiseaseHelper.GetGeneticDiseaseGenes(infector)) {
 | 
			
		||||
                ModLog.Debug($"Found genetic disease {disease} in {infector}, trying to infect {infected}");
 | 
			
		||||
 | 
			
		||||
                if (infected.genes.HasActiveGene(disease))
 | 
			
		||||
                if (DiseaseHelper.IsImmuneAgainstGeneticDisease(infected,disease))
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                if ((new Random()).NextDouble() <= LookupDiseaseInfectionChance(disease))
 | 
			
		||||
                if ((new Random()).NextDouble() <= DiseaseHelper.LookupDiseaseInfectionChance(disease))
 | 
			
		||||
                {
 | 
			
		||||
                    infected.genes.AddGene(disease, !RJW_Genes_Settings.rjw_genes_genetic_disease_as_endogenes);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static List<GeneDef> GetGeneticDiseaseGenes(Pawn pawn)
 | 
			
		||||
        {
 | 
			
		||||
            if (pawn != null && pawn.genes != null)
 | 
			
		||||
            {
 | 
			
		||||
                return pawn.genes
 | 
			
		||||
                    .GenesListForReading
 | 
			
		||||
                    .ConvertAll(gene => gene.def)
 | 
			
		||||
                    .Where(genedef => pawn.genes.HasActiveGene(genedef))
 | 
			
		||||
                    .Where(IsGeneticDiseaseGene)
 | 
			
		||||
                    .ToList();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new List<GeneDef>() { };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static bool IsPenetrativeSex(SexProps props)
 | 
			
		||||
        {
 | 
			
		||||
            if (props == null) return false;
 | 
			
		||||
 | 
			
		||||
            return props.sexType ==
 | 
			
		||||
                xxx.rjwSextype.Vaginal
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Anal
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Oral
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.DoublePenetration
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Fellatio
 | 
			
		||||
                || props.sexType == xxx.rjwSextype.Sixtynine;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static bool IsGeneticDiseaseGene(GeneDef geneDef)
 | 
			
		||||
        {
 | 
			
		||||
            if (geneDef == null) return false;
 | 
			
		||||
            GeneticDiseaseExtension diseaseExt = geneDef.GetModExtension<GeneticDiseaseExtension>();
 | 
			
		||||
            return diseaseExt != null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static float LookupDiseaseInfectionChance(GeneDef geneDef)
 | 
			
		||||
        {
 | 
			
		||||
            if (IsGeneticDiseaseGene(geneDef))
 | 
			
		||||
            {
 | 
			
		||||
                GeneticDiseaseExtension diseaseExt = geneDef.GetModExtension<GeneticDiseaseExtension>();
 | 
			
		||||
                return diseaseExt != null ? diseaseExt.infectionChance : 0.0f;
 | 
			
		||||
            } 
 | 
			
		||||
            else 
 | 
			
		||||
                return 0.0f;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue