Skip to content

Commit

Permalink
fixed performance issues + change way how stats are set for mechs wit…
Browse files Browse the repository at this point in the history
…h work modules
  • Loading branch information
rheirman committed May 2, 2020
1 parent e9b4c7a commit d6ebe03
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 92 deletions.
Binary file modified 1.1/Assemblies/WhatTheHack.dll
Binary file not shown.
1 change: 0 additions & 1 deletion 1.1/Source/WhatTheHack/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ where d.HasModExtension<DefModextension_Hediff>()
}
RecipeDef r = new RecipeDef();
r.defName = "WTH_UninstallModule_" + def.defName;
Log.Message("adding implied recipe def for: " + r.defName);
r.label = "WTH_UninstallModule".Translate(new object[] { def.label });
r.jobString = "WTH_UninstallModule_Jobstring".Translate(new object[] { def.label });
r.appliedOnFixedBodyParts = new List<BodyPartDef>() { WTH_DefOf.Reactor };
Expand Down
10 changes: 10 additions & 0 deletions 1.1/Source/WhatTheHack/Harmony/GenSpawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ static void Postfix(ref Thing newThing, bool respawningAfterLoad)
{
NameUnnamedMechs(newThing);
AddOwnershipIfNeeded(newThing);
//Log.Message("aa");
if(newThing is Pawn p && p.IsHacked())
{
var storage = Base.Instance.GetExtendedDataStorage();
if(storage != null)
{

Utilities.InitWorkTypesAndSkills(p, storage.GetExtendedDataFor(p));
}
}
}
if(newThing.def == WTH_DefOf.WTH_TableMechanoidWorkshop)
{
Expand Down
1 change: 1 addition & 0 deletions 1.1/Source/WhatTheHack/Harmony/Pawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static bool Prefix(Pawn __instance, ref List<WorkTypeDef> __result)
__result = shouldForbid;
return false;
}

}
return true;
}
Expand Down
94 changes: 26 additions & 68 deletions 1.1/Source/WhatTheHack/Harmony/StatWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,39 @@
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using UnityEngine;
using Verse;
using WhatTheHack.Buildings;
using WhatTheHack.Storage;

namespace WhatTheHack.Harmony
{

[HarmonyPatch(typeof(StatWorker), "GetExplanationUnfinalized")]
public static class StatWorker_GetExplanationUnfinalized
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
var instructionsList = new List<CodeInstruction>(instructions);
int i = 0;
foreach (CodeInstruction instruction in instructionsList)
{
if (instruction.operand as MethodInfo == typeof(Pawn).GetField("skills") && instructionsList[i + 1].opcode == OpCodes.Brfalse)
{
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StatWorker), "stat"));
yield return new CodeInstruction(OpCodes.Call, typeof(Utilities).GetMethod("ShouldGetStatValue"));
}
else
{
yield return instruction;
}
i++;
}
}
}

//This makes sure that mechanoids without work modules for certain skills don't use their skill value for those skills, but use the noSkillOffset or noSkillFactor instead.
//The transpiler below is more correct, but its impact on performance is too high, so I replaced it with a simple postfix that just replaces calculated stat value with the noSkillOfset.
[HarmonyPatch(typeof(StatWorker), "GetValueUnfinalized")]
public static class StatWorker_GetValueUnfinalized
{
static void Postfix(StatWorker __instance, StatRequest req, ref StatDef ___stat, ref float __result)
{
Pawn pawn = req.Thing as Pawn;
if (pawn != null && pawn.IsHacked())
{
if (Utilities.ShouldGetStatValue(pawn, ___stat))
{
return;
}
else
{
__result = GetBaseValueFor(req, ___stat) + ___stat.noSkillOffset;
if (req.HasThing)
{
__result *= ___stat.noSkillOffset;
}
}
}
}
private static float GetBaseValueFor(StatRequest request, StatDef stat)
{
float result = stat.defaultBaseValue;
if (request.StatBases != null)
{
for (int i = 0; i < request.StatBases.Count; i++)
{
if (request.StatBases[i].stat == stat)
{
result = request.StatBases[i].value;
break;
}
}
}
return result;
}
}

////This makes sure that mechanoids without work modules for certain skills don't use their skill value for those skills, but use the noSkillOffset or noSkillFactor instead.
//[HarmonyPatch(typeof(StatWorker), "GetExplanationUnfinalized")]
//public static class StatWorker_GetExplanationUnfinalized
//{
// static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
// {
// var instructionsList = new List<CodeInstruction>(instructions);
// int i = 0;
// foreach (CodeInstruction instruction in instructionsList)
// {
// if (instruction.operand as FieldInfo == typeof(Pawn).GetField("skills") && instructionsList[i + 1].opcode == OpCodes.Brfalse)
// {
// yield return new CodeInstruction(OpCodes.Ldarg_0);
// yield return new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StatWorker), "stat"));
// yield return new CodeInstruction(OpCodes.Call, typeof(Utilities).GetMethod("ShouldGetStatValue"));
// }
// else
// {
// yield return instruction;
// }
// i++;
// }
// }
//}
//This makes sure that mechanoids without work modules for certain skills don't use their skill value for those skills, but use the noSkillOffset or noSkillFactor instead.
//[HarmonyPatch(typeof(StatWorker), "GetValueUnfinalized")]
//public static class StatWorker_GetValueUnfinalized
//{
Expand All @@ -88,7 +47,7 @@ private static float GetBaseValueFor(StatRequest request, StatDef stat)
// var instructionsList = new List<CodeInstruction>(instructions);
// foreach (CodeInstruction instruction in instructionsList)
// {
// if (instruction.operand as MethodInfo == AccessTools.Field(typeof(Pawn), "skills"))
// if (instruction.operand as FieldInfo == AccessTools.Field(typeof(Pawn), "skills"))
// {
// yield return new CodeInstruction(OpCodes.Ldarg_0);
// yield return new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(StatWorker), "stat"));
Expand All @@ -109,7 +68,6 @@ private static float GetBaseValueFor(StatRequest request, StatDef stat)
// */

//}

[HarmonyPatch(typeof(StatWorker), "ShouldShowFor")]
static class StatWorker_ShouldShowFor
{
Expand Down
1 change: 0 additions & 1 deletion 1.1/Source/WhatTheHack/Jobs/JobGiver_Work_Mechanoid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class JobGiver_Work_Mechanoid : JobGiver_Work
*/
public override ThinkResult TryIssueJobPackage(Pawn pawn, JobIssueParams jobParams)
{
Log.Message("TryIssueJobPackage called!");
emergency = true;
ThinkResult result = base.TryIssueJobPackage(pawn, jobParams);
if(result.Job == null)
Expand Down
2 changes: 0 additions & 2 deletions 1.1/Source/WhatTheHack/Jobs/WorkGiver_HackRogueAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public override bool ShouldSkip(Pawn pawn, bool forced = false)
public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
{
Building_RogueAI rogueAI = t as Building_RogueAI;
Log.Message("WorkGiver_HackRogueAI called");
if (rogueAI != null && rogueAI.goingRogue)
{
LocalTargetInfo target = rogueAI;
Expand All @@ -45,7 +44,6 @@ public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
{
Building_RogueAI rogueAI = t as Building_RogueAI;
Log.Message("Job on thing called!");
if(rogueAI != null)
{
return new Job(WTH_DefOf.WTH_HackRogueAI, rogueAI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,26 @@ class Recipe_ModifyMechanoid_WorkModule : Recipe_ModifyMechanoid
protected override void PostSuccessfulApply(Pawn pawn, BodyPartRecord part, Pawn billDoer, List<Thing> ingredients, Bill bill)
{
base.PostSuccessfulApply(pawn, part, billDoer, ingredients, bill);
if (pawn.skills == null)
ExtendedPawnData pawnData = Base.Instance.GetExtendedDataStorage().GetExtendedDataFor(pawn);
if (pawnData.workTypes == null)
{
pawn.skills = new Pawn_SkillTracker(pawn);
pawnData.workTypes = new List<WorkTypeDef>();
}
if (pawn.workSettings == null)
if (pawn.skills == null)
{
pawn.workSettings = new Pawn_WorkSettings(pawn);
pawn.workSettings.EnableAndInitialize();
pawn.skills = new Pawn_SkillTracker(pawn);
}
Utilities.InitWorkTypesAndSkills(pawn, pawnData);
if (bill.recipe.addsHediff.GetModExtension<DefModExtension_Hediff_WorkModule>() is DefModExtension_Hediff_WorkModule modExt)
{
ExtendedPawnData pawnData = Base.Instance.GetExtendedDataStorage().GetExtendedDataFor(pawn);
if(pawnData.workTypes == null)
{
pawnData.workTypes = new List<WorkTypeDef>();
}
foreach(WorkTypeDef workType in modExt.workTypes)

foreach (WorkTypeDef workType in modExt.workTypes)
{
pawnData.workTypes.Add(workType);
pawn.workSettings.SetPriority(workType, 3);
if (modExt.skillLevel > 0)
{
foreach(SkillDef skillDef in workType.relevantSkills)
foreach (SkillDef skillDef in workType.relevantSkills)
{
pawn.skills.GetSkill(skillDef).Level = modExt.skillLevel;
}
Expand All @@ -46,6 +43,5 @@ protected override void PostSuccessfulApply(Pawn pawn, BodyPartRecord part, Pawn


}

}
}
4 changes: 1 addition & 3 deletions 1.1/Source/WhatTheHack/Stats/StatPart_Armor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ public override void TransformValue(StatRequest req, ref float val)
{
if (req.Thing is Pawn pawn)
{
float offset = 0;
foreach (Hediff h in pawn.health.hediffSet.hediffs)
{
if (h.def.GetModExtension<DefModextension_Hediff>() is DefModextension_Hediff modExt && modExt.armorOffset != 0)
{
offset += val + modExt.armorOffset;
val += modExt.armorOffset;
}
}
val += offset;
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions 1.1/Source/WhatTheHack/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,37 @@ public static void CalcDaysOfFuel(List<TransferableOneWay> transferables)
}
CalcDaysOfFuel(numMechanoids, fuelAmount, ref fuelConsumption, numPlatforms, ref daysOfFuel, daysOfFuelReason);
}
public static void InitWorkTypesAndSkills(Pawn pawn, ExtendedPawnData pawnData)
{

if (pawn.skills != null)
{
Log.Message("a");
if (pawn.skills.GetSkill(SkillDefOf.Shooting).Level == 0)
{
pawn.skills.GetSkill(SkillDefOf.Shooting).Level = 8;
}
if (pawn.skills.GetSkill(SkillDefOf.Melee).Level == 0)
{
pawn.skills.GetSkill(SkillDefOf.Melee).Level = 4;
}
Log.Message("b");

}
if (pawn.workSettings != null)
{
Log.Message("c");
var huntingWorkType = WorkTypeDefOf.Hunting;
if(pawnData.workTypes == null)
{
Log.Message("WTHACK: " + pawn.Name);
pawnData.workTypes = new List<WorkTypeDef>();
}
pawnData.workTypes.Add(huntingWorkType);
pawn.workSettings.SetPriority(huntingWorkType, 3);
Log.Message("d");
}
}
public static void CalcDaysOfFuel(int numMechanoids, float fuelAmount, ref float fuelConsumption, int numPlatforms, ref float daysOfFuel, StringBuilder daysOfFuelReason)
{
if (numMechanoids == 0)
Expand Down
2 changes: 1 addition & 1 deletion About/Version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
but no minimum version will be required -->

<!-- Optional. This specifies the version of your mod for the library. Otherwise, the version of your mod's assembly will be used. -->
<overrideVersion>2.1.3</overrideVersion>
<overrideVersion>2.1.4</overrideVersion>
<!-- Optional. Use this to specify the version of HugsLib you are targeting.
See https://github.com/UnlimitedHugs/RimworldHugsLib/releases for the current version of the library.-->
<requiredLibraryVersion>5.0.0</requiredLibraryVersion>
Expand Down
6 changes: 3 additions & 3 deletions Defs/DutyDefs/Duties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<alwaysShowWeapon>true</alwaysShowWeapon>
<thinkNode Class="ThinkNode_Priority">
<subNodes>
<li Class="JobGiver_AIFightEnemies">
<targetAcquireRadius>90</targetAcquireRadius>
<targetKeepRadius>20</targetKeepRadius>
<li Class="JobGiver_AIFightEnemies">
<targetAcquireRadius>65</targetAcquireRadius>
<targetKeepRadius>72</targetKeepRadius>
</li>
<li Class="JobGiver_AIGotoNearestHostile" />
</subNodes>
Expand Down

0 comments on commit d6ebe03

Please sign in to comment.