Skip to content

Commit

Permalink
research data mod compat
Browse files Browse the repository at this point in the history
  • Loading branch information
PeteTimesSix committed Jul 15, 2023
1 parent 82a8b04 commit e3b6715
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ static HumanoidAlienRaces()
public static void PatchDelayed(Harmony harmony)
{
var raceCheck = new HarmonyMethod(AccessTools.TypeByName("AlienRace.HarmonyPatches").GetMethod("ShouldSkipResearchPostfix"));
harmony.Patch(AccessTools.Method(typeof(WorkGiver_ResearcherRR), nameof(WorkGiver_ResearcherRR.ShouldSkip)), postfix: raceCheck);
harmony.Patch(AccessTools.Method(typeof(WorkGiver_Analyse), nameof(WorkGiver_Analyse.ShouldSkip)), postfix: raceCheck);
harmony.Patch(AccessTools.Method(typeof(WorkGiver_AnalyseInPlace), nameof(WorkGiver_AnalyseInPlace.ShouldSkip)), postfix: raceCheck);
harmony.Patch(AccessTools.Method(typeof(WorkGiver_AnalyseTerrain), nameof(WorkGiver_AnalyseTerrain.ShouldSkip)), postfix: raceCheck);
harmony.Patch(AccessTools.Method(typeof(WorkGiver_ResearcherRR), nameof(WorkGiver_ResearcherRR.ShouldSkip)), postfix: raceCheck);
}
}
}
8 changes: 6 additions & 2 deletions ResearchReinvented/Source/ModCompat/OptionalPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public static void PatchDelayed(Harmony harmony)
if (HumanoidAlienRaces.active)
{
HumanoidAlienRaces.PatchDelayed(harmony);
}
}
}
if (ResearchData.active)
{
ResearchData.PatchDelayed(harmony);
}
}

}
}
49 changes: 49 additions & 0 deletions ResearchReinvented/Source/ModCompat/ResearchData/_ResearchData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using HarmonyLib;
using PeteTimesSix.ResearchReinvented.Rimworld.WorkGivers;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace PeteTimesSix.ResearchReinvented.ModCompat
{
[StaticConstructorOnStartup]
public static class ResearchData
{
public delegate float ConsumptionRatePerTickGetter(CompRefuelable instance);

public static ConsumptionRatePerTickGetter getter_ConsumptionRatePerTick;

public enum ResearchDataCompatMode
{
TheoryOnly,
AllBenchResearch
}

public static bool active = false;

static ResearchData()
{
active = ModLister.GetActiveModWithIdentifier("kongkim.ResearchData") != null;

getter_ConsumptionRatePerTick = AccessTools.MethodDelegate<ConsumptionRatePerTickGetter>(AccessTools.PropertyGetter(typeof(CompRefuelable), "ConsumptionRatePerTick"));
}

public static void PatchDelayed(Harmony harmony)
{
//these patches generate refuel jobs
var workGiverHasJobOnThingPrefix = new HarmonyMethod(AccessTools.TypeByName("ResearchData.WorkGiver_Researcher_HasJobOnThing_Patch").GetMethod("Prefix"));
harmony.Patch(AccessTools.Method(typeof(WorkGiver_ResearcherRR), nameof(WorkGiver_ResearcherRR.HasJobOnThing)), prefix: workGiverHasJobOnThingPrefix);
var workGiverJobOnThingPrefix = new HarmonyMethod(AccessTools.TypeByName("ResearchData.WorkGiver_Researcher_JobOnThing_Patch").GetMethod("Prefix"));
harmony.Patch(AccessTools.Method(typeof(WorkGiver_ResearcherRR), nameof(WorkGiver_ResearcherRR.JobOnThing)), prefix: workGiverJobOnThingPrefix);
}

public static void ConsumeTickFuel(this CompRefuelable comp)
{
comp.ConsumeFuel(getter_ConsumptionRatePerTick(comp));
}
}
}
1 change: 1 addition & 0 deletions ResearchReinvented/Source/ResearchReinvented.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<Compile Include="Managers\OpportunityFactories\OF_Unlocks.cs" />
<Compile Include="Managers\PrototypeKeeper.cs" />
<Compile Include="Managers\PrototypeTerrainGrid.cs" />
<Compile Include="ModCompat\ResearchData\_ResearchData.cs" />
<Compile Include="Opportunities\ResearchOpportunityCategoryTotalsStore.cs" />
<Compile Include="OpportunityComps\ROComp_RequiresFaction.cs" />
<Compile Include="OpportunityComps\ROComp_RequiresFactionlessPawn.cs" />
Expand Down
30 changes: 22 additions & 8 deletions ResearchReinvented/Source/ResearchReinvented_Settings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PeteTimesSix.ResearchReinvented.Data;
using PeteTimesSix.ResearchReinvented.DefOfs;
using PeteTimesSix.ResearchReinvented.Defs;
using PeteTimesSix.ResearchReinvented.ModCompat;
using PeteTimesSix.ResearchReinvented.Rimworld;
using PeteTimesSix.ResearchReinvented.Utilities;
using PeteTimesSix.ResearchReinvented.Utilities.CustomWidgets;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class ResearchReinvented_Settings : ModSettings
public SettingTab temp_activeTab = SettingTab.CATEGORY_PRESETS;
public ResearchOpportunityCategoryDef temp_selectedCategory = null;

public ResearchData.ResearchDataCompatMode researchDataCompatMode = ResearchData.ResearchDataCompatMode.AllBenchResearch;

private static Color LightGreen = new Color(0.7f, 1f, 0.7f);

public override void ExposeData()
Expand All @@ -56,6 +59,8 @@ public override void ExposeData()
Scribe_Values.Look(ref kitlessNeolithicResearch, "kitlessNeolithicResearch", true);

Scribe_Collections.Look(ref categorySettingChanges, "categorySettingChanges", LookMode.Deep);

Scribe_Values.Look(ref researchDataCompatMode, "researchDataCompatMode", ResearchData.ResearchDataCompatMode.AllBenchResearch);
//do not save/load categorySettings, let them generate
}

Expand Down Expand Up @@ -322,40 +327,49 @@ private void DoModIntegrationTab(Rect inRect)
listingStandard.ColumnWidth = maxWidth / 2f;

//active
if(ModCompat.CombatExtended.active)
if(CombatExtended.active)
{
GUI.color = LightGreen;
listingStandard.Label("RR_setting_modCompat_detected_combatExtended".Translate());
}

if (ModCompat.DubsMintMenus.active)
if (DubsMintMenus.active)
{
GUI.color = LightGreen;
listingStandard.Label("RR_setting_modCompat_detected_dubsMintMenus".Translate());
}

if (ModCompat.HumanoidAlienRaces.active)
if (HumanoidAlienRaces.active)
{
GUI.color = LightGreen;
listingStandard.Label("RR_setting_modCompat_detected_humanoidAlienRaces".Translate());
}
if (ResearchData.active)
{
GUI.color = LightGreen;
listingStandard.Label("RR_setting_modCompat_detected_researchData".Translate());
listingStandard.EnumSelector("RR_setting_modCompat_researchData_mode".Translate(), ref researchDataCompatMode, "RR_modCompat_researchData_mode_");
}

//inactive
if (!ModCompat.CombatExtended.active)
if (!CombatExtended.active)
{
GUI.color = Color.gray;
listingStandard.Label("RR_setting_modCompat_notDetected_combatExtended".Translate());
}
if (!ModCompat.DubsMintMenus.active)
if (!DubsMintMenus.active)
{
GUI.color = Color.gray;
listingStandard.Label("RR_setting_modCompat_notDetected_dubsMintMenus".Translate());
}
if (!ModCompat.HumanoidAlienRaces.active)
if (!HumanoidAlienRaces.active)
{
GUI.color = Color.gray;
listingStandard.Label("RR_setting_modCompat_notDetected_humanoidAlienRaces".Translate());
}
if (!ResearchData.active)
{
GUI.color = Color.gray;
listingStandard.Label("RR_setting_modCompat_notDetected_researchData".Translate());
}

listingStandard.End();
}
Expand Down
14 changes: 14 additions & 0 deletions ResearchReinvented/Source/Rimworld/JobDrivers/JobDriver_Analyse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PeteTimesSix.ResearchReinvented.DefOfs;
using PeteTimesSix.ResearchReinvented.Defs;
using PeteTimesSix.ResearchReinvented.Managers;
using PeteTimesSix.ResearchReinvented.ModCompat;
using PeteTimesSix.ResearchReinvented.Opportunities;
using PeteTimesSix.ResearchReinvented.Rimworld.Toils;
using PeteTimesSix.ResearchReinvented.Rimworld.WorkGivers;
Expand All @@ -12,6 +13,7 @@
using System.Threading.Tasks;
using Verse;
using Verse.AI;
using static UnityEngine.GridBrushBase;

namespace PeteTimesSix.ResearchReinvented.Rimworld.JobDrivers
{
Expand Down Expand Up @@ -56,6 +58,10 @@ protected override IEnumerable<Toil> MakeNewToils()

this.FailOn(() => currentProject != Find.ResearchManager.currentProj);
this.FailOn(() => opportunity.CurrentAvailability != OpportunityAvailability.Available);
if(ResearchData.active && ResearchReinventedMod.Settings.researchDataCompatMode == ResearchData.ResearchDataCompatMode.AllBenchResearch)
{
this.FailOn(() => ResearchBench.GetComp<CompRefuelable>() is CompRefuelable comp && comp.HasFuel is false);
}

this.FailOnBurningImmobile(ResearchBenchIndex);

Expand All @@ -76,6 +82,14 @@ protected override IEnumerable<Toil> MakeNewToils()
float num = actor.GetStatValue(StatDefOf.ResearchSpeed, true);
num *= ResearchBench.GetStatValue(StatDefOf.ResearchSpeedFactor, true);
actor.GainComfortFromCellIfPossible(true);
if (ResearchData.active && ResearchReinventedMod.Settings.researchDataCompatMode == ResearchData.ResearchDataCompatMode.AllBenchResearch)
{
var fuelComp = ResearchBench.GetComp<CompRefuelable>();
if (fuelComp != null && fuelComp.Props.consumeFuelOnlyWhenUsed)
{
fuelComp.ConsumeTickFuel();
}
}
bool finished = opportunity.ResearchTickPerformed(num, actor, SkillDefOf.Intellectual);
if (finished)
this.ReadyForNextToil();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PeteTimesSix.ResearchReinvented.DefOfs;
using PeteTimesSix.ResearchReinvented.Extensions;
using PeteTimesSix.ResearchReinvented.Managers;
using PeteTimesSix.ResearchReinvented.ModCompat;
using PeteTimesSix.ResearchReinvented.Opportunities;
using PeteTimesSix.ResearchReinvented.Rimworld.WorkGivers;
using RimWorld;
Expand Down Expand Up @@ -43,6 +44,10 @@ protected override IEnumerable<Toil> MakeNewToils()

this.FailOn(() => currentProject != Find.ResearchManager.currentProj);
this.FailOn(() => opportunity.CurrentAvailability != OpportunityAvailability.Available);
if (ResearchData.active)
{
this.FailOn(() => ResearchBench.TryGetComp<CompRefuelable>() is CompRefuelable comp && comp.HasFuel is false);
}

this.FailOnDespawnedNullOrForbidden(ResearchBenchIndex);
this.FailOnBurningImmobile(ResearchBenchIndex);
Expand All @@ -55,7 +60,15 @@ protected override IEnumerable<Toil> MakeNewToils()
float num = actor.GetStatValue(StatDefOf.ResearchSpeed, true);
num *= ResearchBench.GetStatValue(StatDefOf.ResearchSpeedFactor, true);
actor.GainComfortFromCellIfPossible(true);
bool finished = opportunity.ResearchTickPerformed(num, actor, SkillDefOf.Intellectual);
if (ResearchData.active)
{
var fuelComp = ResearchBench.GetComp<CompRefuelable>();
if (fuelComp != null && fuelComp.Props.consumeFuelOnlyWhenUsed)
{
fuelComp.ConsumeTickFuel();
}
}
bool finished = opportunity.ResearchTickPerformed(num, actor, SkillDefOf.Intellectual);
if (finished)
this.ReadyForNextToil();
};
Expand Down
23 changes: 15 additions & 8 deletions ResearchReinvented/Source/Rimworld/WorkGivers/WorkGiver_Analyse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PeteTimesSix.ResearchReinvented.DefOfs;
using PeteTimesSix.ResearchReinvented.Defs;
using PeteTimesSix.ResearchReinvented.Managers;
using PeteTimesSix.ResearchReinvented.ModCompat;
using PeteTimesSix.ResearchReinvented.Opportunities;
using PeteTimesSix.ResearchReinvented.OpportunityComps;
using PeteTimesSix.ResearchReinvented.Rimworld.JobDrivers;
Expand Down Expand Up @@ -61,15 +62,15 @@ public override bool ShouldSkip(Pawn pawn, bool forced = false)
return !MatchingOpportunities.Any(o => !o.IsFinished);
}

public override bool HasJobOnThing(Pawn pawn, Thing thing, bool forced = false)
public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
{
if (thing.IsForbidden(pawn))
if (t.IsForbidden(pawn))
return false;

if (!pawn.CanReserve(thing, 1, -1, null, forced))
if (!pawn.CanReserve(t, 1, -1, null, forced))
return false;

if (PrototypeKeeper.Instance.IsPrototype(thing))
if (PrototypeKeeper.Instance.IsPrototype(t))
{
JobFailReason.Is(StringsCache.JobFail_IsPrototype, null);
return false;
Expand All @@ -89,22 +90,22 @@ public override bool HasJobOnThing(Pawn pawn, Thing thing, bool forced = false)
return true;
}

public override Job JobOnThing(Pawn pawn, Thing thing, bool forced = false)
public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
{
var researchBench = GetBestResearchBench(pawn);

var thingDef = MinifyUtility.GetInnerIfMinified(thing).def;
var thingDef = MinifyUtility.GetInnerIfMinified(t).def;

if(!OpportunityCache.ContainsKey(thingDef))
{
Log.Warning($"opportunity cache did not contain {thingDef} for thing {thing}");
Log.Warning($"opportunity cache did not contain {thingDef} for thing {t}");
return null;
}

var opportunity = OpportunityCache[thingDef].First();

JobDef jobDef = opportunity.JobDefs.First(j => j.driverClass == DriverClass);
Job job = JobMaker.MakeJob(jobDef, thing, expiryInterval: 20000, checkOverrideOnExpiry: true);
Job job = JobMaker.MakeJob(jobDef, t, expiryInterval: 20000, checkOverrideOnExpiry: true);
job.targetB = researchBench;
//ResearchOpportunityManager.instance.AssociateJobWithOpportunity(pawn, job, opportunity);
job.count = 1;
Expand Down Expand Up @@ -158,6 +159,12 @@ public Building_ResearchBench GetBestResearchBench(Pawn pawn)
if (!pawn.CanReserveAndReach(new LocalTargetInfo(researchBench), PathEndMode.InteractionCell, Danger.Unspecified))
continue;

if (ResearchData.active && ResearchReinventedMod.Settings.researchDataCompatMode == ResearchData.ResearchDataCompatMode.AllBenchResearch)
{
if (researchBench.TryGetComp<CompRefuelable>() is CompRefuelable comp && comp.HasFuel is false)
continue;
}

found = true;
_benchCache[pawn] = researchBench;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
new HistoryEvent(HistoryEventDefOf.Researching, pawn.Named(HistoryEventArgsNames.Doer)).Notify_PawnAboutToDo_Job();
}

public override Job JobOnThing(Pawn pawn, Thing thing, bool forced = false)
public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
{
var opportunity = MatchingOpportunities.FirstOrDefault();

var jobDef = opportunity.JobDefs.First(j => j.driverClass == DriverClass);
Job job = JobMaker.MakeJob(jobDef, thing, expiryInterval: 3000, checkOverrideOnExpiry: true);
Job job = JobMaker.MakeJob(jobDef, t, expiryInterval: 3000, checkOverrideOnExpiry: true);
//ResearchOpportunityManager.instance.AssociateJobWithOpportunity(pawn, job, opportunity);
return job;
}
Expand Down
Binary file modified ResearchReinvented/v1.4/Assemblies/ResearchReinvented.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

<PeteTimesSix.ResearchReinvented.Defs.ResearchOpportunityCategoryDef ParentName="ResearchOpportunityCategoryBase">
<defName>Social</defName>
<label>knowledge base</label>
<label>information exchange</label>
<priority>100</priority>
<color>(187,250,170)</color>
<description>Knowledge gained from social interactions between your faction members and members of other factions.</description>
Expand Down
10 changes: 10 additions & 0 deletions ResearchReinvented/v1.4/Languages/English/Keyed/Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@

<RR_setting_modCompat_detected_combatExtended>Combat Extended detected.</RR_setting_modCompat_detected_combatExtended>
<RR_setting_modCompat_notDetected_combatExtended>Combat Extended not detected.</RR_setting_modCompat_notDetected_combatExtended>

<RR_setting_modCompat_detected_dubsMintMenus>Dubs Mint Menus detected.</RR_setting_modCompat_detected_dubsMintMenus>
<RR_setting_modCompat_notDetected_dubsMintMenus>Dubs Mint Menus detected.</RR_setting_modCompat_notDetected_dubsMintMenus>

<RR_setting_modCompat_detected_humanoidAlienRaces>Humanoid Alien Races detected.</RR_setting_modCompat_detected_humanoidAlienRaces>
<RR_setting_modCompat_notDetected_humanoidAlienRaces>Humanoid Alien Races not detected.</RR_setting_modCompat_notDetected_humanoidAlienRaces>

<RR_setting_modCompat_detected_researchData>Research Data detected.</RR_setting_modCompat_detected_researchData>
<RR_setting_modCompat_researchData_mode>Compatability mode</RR_setting_modCompat_researchData_mode>
<RR_modCompat_researchData_mode_TheoryOnly>Theory only</RR_modCompat_researchData_mode_TheoryOnly>
<RR_modCompat_researchData_mode_TheoryOnly_tooltip>Research Data "fuel" will only be needed and consumed by theory research.</RR_modCompat_researchData_mode_TheoryOnly_tooltip>
<RR_modCompat_researchData_mode_AllBenchResearch>All research</RR_modCompat_researchData_mode_AllBenchResearch>
<RR_modCompat_researchData_mode_AllBenchResearch_tooltip>Research Data "fuel" will be needed and consumed by any research done at a research bench.</RR_modCompat_researchData_mode_AllBenchResearch_tooltip>
<RR_setting_modCompat_notDetected_researchData>Research Data not detected.</RR_setting_modCompat_notDetected_researchData>


<!--- Obsolete -->
<RR_setting_debugPrintouts>Enable debug mode</RR_setting_debugPrintouts>
Expand Down

0 comments on commit e3b6715

Please sign in to comment.