diff --git a/1.6/1.6/Assemblies/ArachnaeSwarm.dll b/1.6/1.6/Assemblies/ArachnaeSwarm.dll index b47907e..4060780 100644 Binary files a/1.6/1.6/Assemblies/ArachnaeSwarm.dll and b/1.6/1.6/Assemblies/ArachnaeSwarm.dll differ diff --git a/1.6/1.6/Defs/ThingDef_Races/ARA_RaceBaseSwarm.xml b/1.6/1.6/Defs/ThingDef_Races/ARA_RaceBaseSwarm.xml index 8f8c58d..9c9c0d9 100644 --- a/1.6/1.6/Defs/ThingDef_Races/ARA_RaceBaseSwarm.xml +++ b/1.6/1.6/Defs/ThingDef_Races/ARA_RaceBaseSwarm.xml @@ -14,6 +14,7 @@ 0.3 1.1 +
  • @@ -37,7 +38,9 @@
  • + ARA_InsectWorker BeetleLikeWithClaw + ARA_Insect_WithPlanting 0.25 0.8 1.7 @@ -46,6 +49,7 @@ Advanced
  • Dig
  • +
  • ARA_Sowing
  • @@ -70,6 +74,20 @@ DeathActionWorker_Vanish + +
  • + +
  • + Plants + 10 +
  • + + +
  • ARA_Sowing
  • +
    + true + +
  • AnimalInsect
  • diff --git a/1.6/1.6/Defs/ThingDef_Races/ARA_RaceNodeSwarm.xml b/1.6/1.6/Defs/ThingDef_Races/ARA_RaceNodeSwarm.xml index 30454ac..38b36f0 100644 --- a/1.6/1.6/Defs/ThingDef_Races/ARA_RaceNodeSwarm.xml +++ b/1.6/1.6/Defs/ThingDef_Races/ARA_RaceNodeSwarm.xml @@ -622,7 +622,7 @@ 9999
  • - Spelopede + ArachnaeBase_Race_Scavenger 3 6000
  • diff --git a/1.6/1.6/Defs/ThinkTreeDefs/ARA_AnimalWorkThinkTrees.xml b/1.6/1.6/Defs/ThinkTreeDefs/ARA_AnimalWorkThinkTrees.xml new file mode 100644 index 0000000..d1c3516 --- /dev/null +++ b/1.6/1.6/Defs/ThinkTreeDefs/ARA_AnimalWorkThinkTrees.xml @@ -0,0 +1,444 @@ + + + + ARA_InsectWorker + + + +
  • + + +
  • + true +
  • + + +
  • + +
  • + +
  • + Downed +
  • +
  • + BurningResponse +
  • +
  • + MentalStateCritical +
  • + + +
  • + + +
  • + + +
  • + true + +
  • + +
  • + 0.7 + +
  • + DigOutIfCannotReachMapEdge +
  • +
    + + + + + + + +
  • + +
  • + Misc + +
  • + Walk +
  • + + + + + + +
  • + MentalStateNonCritical +
  • + + +
  • + +
  • + Misc + +
  • + +
  • + + + + +
  • + RopedPawn +
  • + + +
  • + LordDuty +
  • + + +
  • + Animal_PreMain +
  • +
  • + Insect_PreMain +
  • + +
  • + true + + +
  • + 30 + 35 +
  • + + +
  • + LeaveIfWrongSeason +
  • +
  • + LeaveIfStarving +
  • + + +
  • + 60 + +
  • + Misc + +
  • + Walk +
  • + + + + + + + + +
  • + true + +
  • + +
  • + +
  • + 60 + +
  • + Misc + +
  • + Walk +
  • + + + + + + + + + + + + +
  • + +
  • + TrainedAnimalBehavior + + +
  • + Obedience + +
  • + +
  • + true +
  • +
  • +
  • + +
  • + + + + +
  • + Rescue + +
  • + 75 +
  • + + + + + + + +
  • + RestingForMedicalReasons + +
  • + +
  • + +
  • +
  • + + +
  • + + +
  • + 60 + +
  • + SatisfyingNeeds + +
  • + +
  • + + + + +
  • + SatisfyBasicNeeds +
  • + + +
  • + + + +
  • + +
  • +
  • + true +
  • +
  • + + +
  • + +
  • + SatisfyingNeeds + +
  • + +
  • + + + + +
  • + +
  • + Misc + +
  • + +
  • + + + + +
  • + +
  • + Misc + +
  • + +
  • + + + + +
  • + 1.5 + +
  • + +
  • Manipulation
  • + + +
  • + Haul + +
  • + TrainedAnimalBehavior + +
  • + +
  • +
    + + + + + + + +
  • + Forage + +
  • + +
  • + +
  • + +
  • + + + + + + +
  • + Dig + +
  • + +
  • + +
  • + + + + + + + +
  • + Animal_PreWander +
  • +
  • + Insect_PreWander +
  • + + +
  • + +
  • + Idle + + +
  • + +
  • + None + 120~240 +
  • + + + +
  • + +
  • + None + 120~240 + 500 +
  • + + +
  • + 0.1 + +
  • + None + 120~240 + 500 +
  • + + + +
  • + None + 120~240 +
  • + + + + +
  • + None + 120~240 +
  • + + + + + + +
  • + +
  • + RestingForMedicalReasons + +
  • + +
  • + +
  • + Misc + +
  • + Walk +
  • + + + + + + +
  • + Idle + +
  • + +
  • + Deadly + 120~240 +
  • + + + +
  • + Deadly + 120~240 +
  • + + + +
  • + + + + \ No newline at end of file diff --git a/1.6/1.6/Defs/ThinkTreeDefs/ARA_ThinkTrees.xml b/1.6/1.6/Defs/ThinkTreeDefs/ARA_ThinkTrees.xml index 7a49c85..a5101a2 100644 --- a/1.6/1.6/Defs/ThinkTreeDefs/ARA_ThinkTrees.xml +++ b/1.6/1.6/Defs/ThinkTreeDefs/ARA_ThinkTrees.xml @@ -1,7 +1,455 @@ - + + - ARA_Humanlike + ARA_Insect_WithPlanting + + + +
  • + + +
  • + true +
  • + + +
  • + +
  • + +
  • + Downed +
  • +
  • + BurningResponse +
  • +
  • + MentalStateCritical +
  • + + +
  • + + +
  • + + +
  • + true + +
  • + +
  • + 0.7 + +
  • + DigOutIfCannotReachMapEdge +
  • + + + + + + + + +
  • + +
  • + Misc + +
  • + Walk +
  • + + + + + + +
  • + MentalStateNonCritical +
  • + + +
  • + +
  • + Misc + +
  • + +
  • + + + + +
  • + RopedPawn +
  • + + +
  • + LordDuty +
  • + + +
  • + Animal_PreMain +
  • +
  • + Insect_PreMain +
  • + +
  • + true + + +
  • + 30 + 35 +
  • + + +
  • + LeaveIfWrongSeason +
  • +
  • + LeaveIfStarving +
  • + + +
  • + 60 + +
  • + Misc + +
  • + Walk +
  • + + + + + + + + +
  • + true + +
  • + +
  • + +
  • + 60 + +
  • + Misc + +
  • + Walk +
  • + + + + + + + + + + + + +
  • + +
  • + TrainedAnimalBehavior + + +
  • + Obedience + +
  • + +
  • + true +
  • +
  • +
  • + +
  • + + + + +
  • + Rescue + +
  • + 75 +
  • + + + + + + + +
  • + RestingForMedicalReasons + +
  • + +
  • + +
  • +
  • + + +
  • + + +
  • + 60 + +
  • + SatisfyingNeeds + +
  • + +
  • + + + + +
  • + SatisfyBasicNeeds +
  • + + +
  • + + + +
  • + +
  • + SatisfyingNeeds + +
  • + +
  • + + + + +
  • + +
  • + Misc + +
  • + +
  • + + + + +
  • + +
  • + Misc + +
  • + +
  • + + + + +
  • + 1.5 + +
  • + +
  • Manipulation
  • + + +
  • + Haul + +
  • + TrainedAnimalBehavior + +
  • + +
  • +
    + + + + + + + +
  • + Forage + +
  • + +
  • + +
  • + +
  • + + + + + + +
  • + Dig + +
  • + +
  • + +
  • + + + + +
  • + ARA_Sowing + + +
  • + + +
  • + +
  • + + + + + + + +
  • + Animal_PreWander +
  • +
  • + Insect_PreWander +
  • + + +
  • + +
  • + Idle + + +
  • + +
  • + None + 120~240 +
  • + + + +
  • + +
  • + None + 120~240 + 500 +
  • + + +
  • + 0.1 + +
  • + None + 120~240 + 500 +
  • + + + +
  • + None + 120~240 +
  • + + + + +
  • + None + 120~240 +
  • + + + + + + +
  • + +
  • + RestingForMedicalReasons + +
  • + +
  • + +
  • + Misc + +
  • + Walk +
  • + + + + + + +
  • + Idle + +
  • + +
  • + Deadly + 120~240 +
  • + + + +
  • + Deadly + 120~240 +
  • + + + +
  • + + + + + + ARA_Humanlike @@ -20,7 +468,7 @@
  • - +
  • @@ -45,12 +493,12 @@
  • - +
  • - +
  • Downed
  • @@ -65,7 +513,7 @@
  • Abilities_Escape
  • - +
  • @@ -83,7 +531,7 @@
  • Humanlike_PostMentalState
  • - +
  • @@ -121,21 +569,21 @@
  • Humanlike_PostDuty
  • - +
  • 2.5
  • -
  • ARA_InteractiveEggSac
  • +
  • ARA_InteractiveEggSac
  • -
  • ARA_InteractiveEggSac
  • +
  • ARA_InteractiveEggSac
  • true @@ -158,7 +606,7 @@ - +
  • Escaping @@ -168,7 +616,7 @@
  • - +
  • @@ -189,14 +637,14 @@
  • - +
  • ChangingApparel
  • - +
  • SatisfyingNeeds @@ -220,7 +668,7 @@
  • - +
  • @@ -247,7 +695,7 @@
  • - +
  • Idle @@ -256,7 +704,7 @@
  • - +
  • @@ -279,7 +727,7 @@ true true - +
  • @@ -375,7 +823,7 @@
  • Humanlike_PreMain
  • - +
  • @@ -416,7 +864,7 @@
  • - +
  • None @@ -454,7 +902,7 @@
  • - +
  • Misc @@ -498,4 +946,5 @@ + \ No newline at end of file diff --git a/1.6/1.6/Defs/TrainableDefs/ARA_Sowing.xml b/1.6/1.6/Defs/TrainableDefs/ARA_Sowing.xml new file mode 100644 index 0000000..ca09fab --- /dev/null +++ b/1.6/1.6/Defs/TrainableDefs/ARA_Sowing.xml @@ -0,0 +1,23 @@ + + + + + ARA_Sowing + + 允许该生物执行种植任务。 + + + true + + + 5 + Advanced + + + 3 + + + 100 + + + \ No newline at end of file diff --git a/Source/ArachnaeSwarm/ArachnaeSwarm.csproj b/Source/ArachnaeSwarm/ArachnaeSwarm.csproj index e339504..97942aa 100644 --- a/Source/ArachnaeSwarm/ArachnaeSwarm.csproj +++ b/Source/ArachnaeSwarm/ArachnaeSwarm.csproj @@ -85,6 +85,14 @@ + + + + + + + + diff --git a/Source/ArachnaeSwarm/CompAdvancedTraining.cs b/Source/ArachnaeSwarm/CompAdvancedTraining.cs new file mode 100644 index 0000000..9a0b8c9 --- /dev/null +++ b/Source/ArachnaeSwarm/CompAdvancedTraining.cs @@ -0,0 +1,77 @@ +using System.Collections.Generic; +using Verse; +using RimWorld; + +namespace ArachnaeSwarm +{ + public class CompProperties_AdvancedTraining : CompProperties + { + // 1. 用于设置固定技能等级 + public List skillLevels = new List(); + + // 2. 用于指定生成时立即完成的训练 + public List instantTrainables = new List(); + + // 3. 全局开关:是否阻止所有技能衰减 + public bool disableAllSkillDecay = false; + + public CompProperties_AdvancedTraining() + { + this.compClass = typeof(CompAdvancedTraining); + } + } + + public class SkillLevelEntry + { + public SkillDef skill; + public int level = 0; + // 这里的 disableDecay 字段现在是冗余的,因为我们有全局的 disableAllSkillDecay + // 但为了兼容性或未来可能的需求,可以保留。 + // 在当前方案中,它的值将被忽略。 + public bool disableDecay = true; + } + + public class CompAdvancedTraining : ThingComp + { + public CompProperties_AdvancedTraining Props => (CompProperties_AdvancedTraining)this.props; + + public override void PostSpawnSetup(bool respawningAfterLoad) + { + base.PostSpawnSetup(respawningAfterLoad); + + Pawn pawn = this.parent as Pawn; + if (pawn == null) return; + + // --- 1. 设置固定技能等级 --- + if (pawn.skills != null && !Props.skillLevels.NullOrEmpty()) + { + foreach (var entry in Props.skillLevels) + { + if (entry.skill != null) + { + var skillRecord = pawn.skills.GetSkill(entry.skill); + if (skillRecord != null) + { + skillRecord.Level = entry.level; + // 注意: 激情 (passion) 影响学习速度,不直接阻止衰减。 + // 实际的衰减阻止逻辑在 TrainingSystem_Patcher.cs 中处理。 + // 默认情况下,我们不改变 passion,除非有特殊需求。 + } + } + } + } + + // --- 2. 执行瞬间训练 (只在初次生成时) --- + if (!respawningAfterLoad && pawn.training != null && !Props.instantTrainables.NullOrEmpty()) + { + foreach (var trainable in Props.instantTrainables) + { + if (trainable != null && !pawn.training.HasLearned(trainable)) + { + pawn.training.Train(trainable, null, complete: true); + } + } + } + } + } +} \ No newline at end of file diff --git a/Source/ArachnaeSwarm/JobGiver_Grower.cs b/Source/ArachnaeSwarm/JobGiver_Grower.cs new file mode 100644 index 0000000..0e42089 --- /dev/null +++ b/Source/ArachnaeSwarm/JobGiver_Grower.cs @@ -0,0 +1,158 @@ +using System.Collections.Generic; +using Verse; +using Verse.AI; +using RimWorld; + +namespace ArachnaeSwarm +{ + public class JobGiver_Grower : ThinkNode_JobGiver + { + private WorkGiver_GrowerHarvest _workGiverHarvest; + private WorkGiver_Scanner _workGiverPlantsCut; // 通用扫描器类型 + private WorkGiver_ArachnaeSow _workGiverArachnaeSow; + + protected override Job TryGiveJob(Pawn pawn) + { + // 懒加载 WorkGiver 实例,确保 DefOf 已被初始化 + if (_workGiverHarvest == null) + { + _workGiverHarvest = DefDatabase.GetNamed("GrowerHarvest").Worker as WorkGiver_GrowerHarvest; + _workGiverPlantsCut = DefDatabase.GetNamed("PlantsCut").Worker as WorkGiver_Scanner; + _workGiverArachnaeSow = new WorkGiver_ArachnaeSow(); + + if (_workGiverHarvest == null || _workGiverPlantsCut == null || _workGiverArachnaeSow == null) + { + Log.ErrorOnce("JobGiver_Grower: Failed to get a required WorkGiver. DefOfs might not be initialized.", 123458); + return null; + } + } + + // 1. 优先收获(自动) + IntVec3 bestHarvestCell = FindClosestHarvestableCell(pawn); + if (bestHarvestCell.IsValid) + { + Job harvestJob = _workGiverHarvest.JobOnCell(pawn, bestHarvestCell); + if (harvestJob != null) + { + return harvestJob; + } + } + + // 2. 其次处理手动指定的砍伐/收获任务 + Thing bestCuttable = FindClosestWorkableThing(pawn, _workGiverPlantsCut); + if (bestCuttable != null) + { + Job cutJob = _workGiverPlantsCut.JobOnThing(pawn, bestCuttable); + if (cutJob != null) + { + return cutJob; + } + } + + // 3. 最后处理播种或清理障碍(由 WorkGiver_ArachnaeSow 处理) + Job sowOrClearJob = FindClosestSowableOrClearJob(pawn, _workGiverArachnaeSow); + if (sowOrClearJob != null) + { + return sowOrClearJob; + } + + return null; + } + + private Thing FindClosestWorkableThing(Pawn pawn, WorkGiver_Scanner scanner) + { + return GenClosest.ClosestThing_Global( + pawn.Position, + scanner.PotentialWorkThingsGlobal(pawn), + maxDistance: 9999f, + validator: t => t != null && !t.IsForbidden(pawn) && scanner.HasJobOnThing(pawn, t) && pawn.CanReach(t, PathEndMode.Touch, Danger.Deadly) + ); + } + + private IntVec3 FindClosestHarvestableCell(Pawn pawn) + { + IntVec3 bestCell = IntVec3.Invalid; + float bestDistSq = float.MaxValue; + + foreach (Zone zone in pawn.Map.zoneManager.AllZones) + { + if (zone is Zone_Growing growingZone) + { + foreach (IntVec3 cell in growingZone.Cells) + { + float distSq = pawn.Position.DistanceToSquared(cell); + if (distSq < bestDistSq && pawn.CanReach(cell, PathEndMode.ClosestTouch, Danger.Deadly)) + { + if (_workGiverHarvest.HasJobOnCell(pawn, cell)) + { + bestDistSq = distSq; + bestCell = cell; + } + } + } + } + } + return bestCell; + } + + // 修改后的方法:寻找最近的播种或清理 Job + private Job FindClosestSowableOrClearJob(Pawn pawn, WorkGiver_ArachnaeSow scanner) + { + IntVec3 bestClearCell = IntVec3.Invalid; + Job bestClearJob = null; + float bestClearDistSq = float.MaxValue; + + IntVec3 bestSowCell = IntVec3.Invalid; + Job bestSowJob = null; + float bestSowDistSq = float.MaxValue; + + foreach (Zone zone in pawn.Map.zoneManager.AllZones) + { + if (zone is Zone_Growing growingZone) + { + ThingDef wantedPlant = growingZone.GetPlantDefToGrow(); + if (wantedPlant == null) continue; + + foreach (IntVec3 cell in growingZone.Cells) + { + float distSq = pawn.Position.DistanceToSquared(cell); + if (pawn.CanReach(cell, PathEndMode.ClosestTouch, Danger.Deadly)) + { + Job potentialJob = scanner.JobOnCell(pawn, cell); + if (potentialJob != null) + { + if (potentialJob.def == JobDefOf.CutPlant || potentialJob.def == JobDefOf.HaulToContainer || potentialJob.def == JobDefOf.HaulToCell) + { + if (distSq < bestClearDistSq) + { + bestClearDistSq = distSq; + bestClearJob = potentialJob; + } + } + else if (potentialJob.def == JobDefOf.Sow) + { + if (distSq < bestSowDistSq) + { + bestSowDistSq = distSq; + bestSowJob = potentialJob; + } + } + } + } + } + } + } + // 优先返回清理 Job + if (bestClearJob != null) + { + return bestClearJob; + } + // 其次返回播种 Job + if (bestSowJob != null) + { + return bestSowJob; + } + return null; + } + } +} \ No newline at end of file diff --git a/Source/ArachnaeSwarm/MainHarmony.cs b/Source/ArachnaeSwarm/MainHarmony.cs new file mode 100644 index 0000000..8d6fe4d --- /dev/null +++ b/Source/ArachnaeSwarm/MainHarmony.cs @@ -0,0 +1,21 @@ +using Verse; +using HarmonyLib; +using System.Reflection; + +namespace ArachnaeSwarm +{ + // [StaticConstructorOnStartup] 属性确保这个类的静态构造函数在游戏启动时被调用 + [StaticConstructorOnStartup] + public static class MainHarmony + { + static MainHarmony() + { + // 创建一个 Harmony 实例。ID 应该是唯一的,通常使用 "作者.Mod名称" 的格式。 + var harmony = new Harmony("com.kalospacer.arachnaeswarm"); + + // Harmony 会自动扫描当前整个程序集(我们的 .dll 文件), + // 寻找所有带有 [HarmonyPatch] 属性的类,并应用它们。 + harmony.PatchAll(Assembly.GetExecutingAssembly()); + } + } +} \ No newline at end of file diff --git a/Source/ArachnaeSwarm/ThinkNode_ConditionalAnimalShouldDoGrowingWork.cs b/Source/ArachnaeSwarm/ThinkNode_ConditionalAnimalShouldDoGrowingWork.cs new file mode 100644 index 0000000..c577380 --- /dev/null +++ b/Source/ArachnaeSwarm/ThinkNode_ConditionalAnimalShouldDoGrowingWork.cs @@ -0,0 +1,38 @@ +using Verse; +using Verse.AI; +using RimWorld; + +namespace ArachnaeSwarm +{ + // 将 DefOf 类放在这里,以便在命名空间内共享 + [DefOf] + public static class ARA_TrainableDefOf + { + public static TrainableDef ARA_Sowing; + + static ARA_TrainableDefOf() + { + DefOfHelper.EnsureInitializedInCtor(typeof(ARA_TrainableDefOf)); + } + } + + // 这个新的条件节点将检查动物是否应该执行任何农业工作(播种或切割/收获) + public class ThinkNode_ConditionalAnimalShouldDoGrowingWork : ThinkNode_Conditional + { + protected override bool Satisfied(Pawn pawn) + { + // 首先,进行安全检查,确保 pawn.training 存在 + if (pawn.training == null) + { + return false; + } + + // 检查动物是否学会并被允许执行“播种”工作 + bool canSow = pawn.training.HasLearned(ARA_TrainableDefOf.ARA_Sowing) && + pawn.training.GetWanted(ARA_TrainableDefOf.ARA_Sowing); + + // 现在只需要检查播种技能,因为切割功能已合并 + return canSow; + } + } +} \ No newline at end of file diff --git a/Source/ArachnaeSwarm/TrainingSystem_Patcher.cs b/Source/ArachnaeSwarm/TrainingSystem_Patcher.cs new file mode 100644 index 0000000..6383318 --- /dev/null +++ b/Source/ArachnaeSwarm/TrainingSystem_Patcher.cs @@ -0,0 +1,48 @@ +using HarmonyLib; +using RimWorld; +using Verse; + +namespace ArachnaeSwarm +{ + // Patcher 1: 阻止训练退化 + [HarmonyPatch(typeof(Pawn_TrainingTracker), "TrainingTrackerTickRare")] + public static class Patch_TrainingTracker_TickRare + { + public static bool Prefix(Pawn_TrainingTracker __instance) + { + Pawn pawn = Traverse.Create(__instance).Field("pawn").GetValue(); + if (pawn == null) return true; + + var comp = pawn.GetComp(); + if (comp != null && comp.Props.disableAllSkillDecay) + { + return false; // 阻止原版方法运行 + } + return true; + } + } + + // Patcher 2: 阻止特定技能的衰减 + [HarmonyPatch(typeof(SkillRecord), "Interval")] + public static class Patch_SkillRecord_Interval + { + // 使用 __instance 来获取 SkillRecord 对象, __pawn 为 SkillRecord 内部的私有字段 + public static bool Prefix(SkillRecord __instance, Pawn ___pawn) + { + if (___pawn == null) return true; + + var comp = ___pawn.GetComp(); + if (comp == null || comp.Props.skillLevels.NullOrEmpty()) + { + return true; // 没有组件或配置,正常执行原版衰减 + } + + // 检查全局开关:如果设置了 disableAllSkillDecay 为 true,则阻止衰减 + if (comp.Props.disableAllSkillDecay) + { + return false; // 阻止原版 Interval 方法的执行 + } + return true; // 正常执行原版衰减 + } + } +} \ No newline at end of file diff --git a/Source/ArachnaeSwarm/WorkGiver_ArachnaeSow.cs b/Source/ArachnaeSwarm/WorkGiver_ArachnaeSow.cs new file mode 100644 index 0000000..6ff01b2 --- /dev/null +++ b/Source/ArachnaeSwarm/WorkGiver_ArachnaeSow.cs @@ -0,0 +1,227 @@ +using System.Collections.Generic; +using Verse; +using Verse.AI; +using RimWorld; + +namespace ArachnaeSwarm +{ + public class WorkGiver_ArachnaeSow : WorkGiver_Grower + { + protected static string CantSowCavePlantBecauseOfLightTrans; + protected static string CantSowCavePlantBecauseUnroofedTrans; + + public override PathEndMode PathEndMode => PathEndMode.ClosestTouch; + + public static void ResetStaticData() + { + CantSowCavePlantBecauseOfLightTrans = "CantSowCavePlantBecauseOfLight".Translate(); + CantSowCavePlantBecauseUnroofedTrans = "CantSowCavePlantBecauseUnroofed".Translate(); + } + + protected override bool ExtraRequirements(IPlantToGrowSettable settable, Pawn pawn) + { + if (!settable.CanAcceptSowNow()) + { + return false; + } + IntVec3 c; + if (settable is Zone_Growing zone_Growing) + { + if (!zone_Growing.allowSow) + { + return false; + } + c = zone_Growing.Cells[0]; + } + else + { + c = ((Thing)settable).Position; + } + ThingDef wantedPlantDef = WorkGiver_Grower.CalculateWantedPlantDef(c, pawn.Map); + if (wantedPlantDef == null) + { + return false; + } + return true; + } + + public override Job JobOnCell(Pawn pawn, IntVec3 c, bool forced = false) + { + Map map = pawn.Map; + if (c.GetVacuum(pawn.Map) >= 0.5f) + { + return null; + } + + // 直接计算 wantedPlantDef + ThingDef wantedPlantDefLocal = WorkGiver_Grower.CalculateWantedPlantDef(c, map); + if (wantedPlantDefLocal == null) + { + return null; + } + + if (!PlantUtility.GrowthSeasonNow(c, map, wantedPlantDefLocal)) + { + return null; + } + List thingList = c.GetThingList(map); + Zone_Growing zone_Growing = c.GetZone(map) as Zone_Growing; + bool flag = false; + for (int i = 0; i < thingList.Count; i++) + { + Thing thing = thingList[i]; + if (thing.def == wantedPlantDef) + { + return null; + } + if ((thing is Blueprint || thing is Frame) && thing.Faction == pawn.Faction) + { + flag = true; + } + } + if (flag) + { + Thing edifice = c.GetEdifice(map); + if (edifice == null || edifice.def.fertility < 0f) + { + return null; + } + } + if (wantedPlantDefLocal.plant.diesToLight) + { + if (!c.Roofed(map) && !map.GameConditionManager.IsAlwaysDarkOutside) + { + JobFailReason.Is(CantSowCavePlantBecauseUnroofedTrans); + return null; + } + if (map.glowGrid.GroundGlowAt(c, ignoreCavePlants: true) > 0f) + { + JobFailReason.Is(CantSowCavePlantBecauseOfLightTrans); + return null; + } + } + if (wantedPlantDefLocal.plant.interferesWithRoof && c.Roofed(pawn.Map)) + { + return null; + } + Plant plant = c.GetPlant(map); + if (plant != null) // 只要地块上有植物 + { + // 如果地块上的植物不是我们想要种植的植物,就割除 + if (plant.def != wantedPlantDefLocal) + { + if (!pawn.CanReserve(plant, 1, -1, null, forced) || plant.IsForbidden(pawn)) + { + return null; + } + if (zone_Growing != null && !zone_Growing.allowCut) + { + return null; + } + if (!forced && plant.TryGetComp(out var comp) && comp.PreventCutting) + { + return null; + } + return JobMaker.MakeJob(JobDefOf.CutPlant, plant); + } + // 如果地块上的植物是我们想要种植的植物,并且它阻碍了相邻播种,则不割除 + // 因为它已经是我们想要种植的植物了 + if (plant.def.plant.blockAdjacentSow) + { + return null; + } + } + Thing thing2 = PlantUtility.AdjacentSowBlocker(wantedPlantDefLocal, c, map); + if (thing2 != null) + { + if (thing2 is Plant plant2) + { + // 如果阻碍播种的是植物,并且不是我们想要种植的植物,就割除 + if (plant2.def != wantedPlantDefLocal) + { + if (pawn.CanReserveAndReach(plant2, PathEndMode.Touch, Danger.Deadly, 1, -1, null, forced) && !plant2.IsForbidden(pawn)) + { + IPlantToGrowSettable plantToGrowSettable = plant2.Position.GetPlantToGrowSettable(plant2.Map); + if (plantToGrowSettable == null || plantToGrowSettable.GetPlantDefToGrow() != plant2.def) + { + Zone_Growing zone_Growing2 = c.GetZone(map) as Zone_Growing; + Zone_Growing zone_Growing3 = c.GetZone(map) as Zone_Growing; + if ((zone_Growing2 != null && !zone_Growing2.allowCut) || (zone_Growing3 != null && !zone_Growing3.allowCut && plant2.def == zone_Growing3.GetPlantDefToGrow())) + { + return null; + } + } + if (!forced && thing2.TryGetComp(out CompPlantPreventCutting comp2) && comp2.PreventCutting) + { + return null; + } + if (PlantUtility.TreeMarkedForExtraction(plant2)) + { + return null; + } + return JobMaker.MakeJob(JobDefOf.CutPlant, plant2); + } + } + } + else if (thing2.def.EverHaulable) + { + return HaulAIUtility.HaulAsideJobFor(pawn, thing2); + } + return null; + } + if (wantedPlantDefLocal.plant.sowMinSkill > 0 && ((pawn.skills != null && pawn.skills.GetSkill(SkillDefOf.Plants).Level < wantedPlantDefLocal.plant.sowMinSkill) || (pawn.IsColonyMech && pawn.RaceProps.mechFixedSkillLevel < wantedPlantDefLocal.plant.sowMinSkill))) + { + JobFailReason.Is("UnderAllowedSkill".Translate(wantedPlantDefLocal.plant.sowMinSkill), def.label); + return null; + } + for (int j = 0; j < thingList.Count; j++) + { + Thing thing3 = thingList[j]; + if (!thing3.def.BlocksPlanting()) + { + continue; + } + if (!pawn.CanReserve(thing3, 1, -1, null, forced)) + { + return null; + } + if (thing3.def.category == ThingCategory.Plant) + { + // 如果阻碍播种的是植物,并且不是我们想要种植的植物,就割除 + if (thing3.def != wantedPlantDefLocal) + { + if (thing3.IsForbidden(pawn)) + { + return null; + } + if (zone_Growing != null && !zone_Growing.allowCut) + { + return null; + } + if (!forced && thing3.TryGetComp(out var comp3) && comp3.PreventCutting) + { + return null; + } + if (PlantUtility.TreeMarkedForExtraction(thing3)) + { + return null; + } + return JobMaker.MakeJob(JobDefOf.CutPlant, thing3); + } + } + else if (thing3.def.EverHaulable) + { + return HaulAIUtility.HaulAsideJobFor(pawn, thing3); + } + return null; + } + if (!wantedPlantDefLocal.CanNowPlantAt(c, map) || !PlantUtility.GrowthSeasonNow(c, map, wantedPlantDefLocal) || !pawn.CanReserve(c, 1, -1, null, forced)) + { + return null; + } + Job job = JobMaker.MakeJob(JobDefOf.Sow, c); + job.plantDefToSow = wantedPlantDefLocal; + return job; + } + } +} \ No newline at end of file diff --git a/Source/Documents/AnimalWorkModeSolution_Final_Implementation.md b/Source/Documents/AnimalWorkModeSolution_Final_Implementation.md new file mode 100644 index 0000000..002e461 --- /dev/null +++ b/Source/Documents/AnimalWorkModeSolution_Final_Implementation.md @@ -0,0 +1,514 @@ +# 动物工作模式解决方案文档(最终实现版) + +## 1. 项目概述 + +本项目旨在实现一个功能,使动物(特别是昆虫)能够像机械体一样持续工作,而不需要通过机械师控制 WorkMode。通过为动物启用工作系统并创建自定义行为树,动物将始终处于工作状态,持续执行允许的工作类型。 + +## 2. 原版代码分析 + +### 2.1 昆虫 ThinkTreeDef 结构 + +原版昆虫的 ThinkTree (`Insect.xml`) 包含以下主要逻辑: + +1. **紧急情况处理**:倒地、燃烧、精神状态等 +2. **基本需求满足**:睡觉、吃饭等 +3. **驯服动物行为**:跟随主人、救援等 +4. **闲置行为**:漫游、待命等 + +### 2.2 机械体 ThinkTreeDef 结构 + +原版机械体的 ThinkTree (`Mechanoid.xml`) 包含 WorkMode 系统,这是动物不具备的。 + +## 3. 为什么机械体有 workSettings 而动物没有 + +### 3.1 核心原因分析 + +机械体和动物虽然都是 Pawn,但它们的 workSettings 初始化逻辑不同: + +1. **`Pawn_WorkSettings` 的存在性**: + - `Pawn` 类内部持有一个 `Pawn_WorkSettings` 类型的引用 `priorities`,默认为 `null` + - `public bool EverWork => priorities != null;` 属性表明只有当 `priorities` 不为 `null` 时,Pawn 才有工作能力 + +2. **初始化条件**: + - `EnableAndInitializeIfNotAlreadyInitialized()` 方法检查 `priorities` 是否为 `null`,如果为 `null` 则调用 `EnableAndInitialize()` + - `EnableAndInitialize()` 方法是真正执行初始化的地方,创建 `priorities` 对象并根据条件设置初始优先级 + +3. **Mechanoid 有 WorkSettings 的证据**: + - `EnableAndInitialize()` 方法明确包含对机械体的特殊处理: + ```csharp + if (ModsConfig.BiotechActive && pawn.RaceProps.IsMechanoid && !pawn.RaceProps.mechWorkTypePriorities.NullOrEmpty()) + { + for (int i = 0; i < pawn.RaceProps.mechWorkTypePriorities.Count; i++) + { + MechWorkTypePriority mechWorkTypePriority = pawn.RaceProps.mechWorkTypePriorities[i]; + SetPriority(mechWorkTypePriority.def, mechWorkTypePriority.priority); + } + } + ``` + - 这表明机械体的设计意图就是拥有并使用 `Pawn_WorkSettings` + +4. **Animal 没有 WorkSettings 的证据**: + - **直接初始化逻辑**:`EnableAndInitialize()` 方法在为 Pawn 分配默认"活跃"工作类型时,会过滤掉 `pawn.WorkTypeIsDisabled(w)` 的工作类型。对于大多数动物来说,它们的 `RaceProps` 会禁用几乎所有需要手动分配的 `WorkTypeDef` + - **工作类型的定义**:绝大多数工作类型都包含限制,使得动物 Pawn 默认无法执行这些工作 + - **特定工作提供者的处理**:针对动物的操作是通过独立的 `WorkGiver` 系统直接处理的,不依赖于通用的 `workSettings` 优先级系统 + +### 3.2 结论 + +机械体被设计为拥有 `Pawn_WorkSettings`,因为它们的工作优先级可以通过其 `RaceProps.mechWorkTypePriorities` 进行预设,并且游戏逻辑会在需要时初始化它。 + +动物通常没有有效的 `Pawn_WorkSettings`,因为: +1. 它们被设计为不能执行绝大多数需要手动分配的工作 +2. 它们与玩家互动的特定工作是通过独立的 `WorkGiver` 系统直接处理的 +3. 因此,除非有特定的游戏逻辑或 Mod 显式地为动物调用 `EnableAndInitialize()`,否则它们的 `priorities` 对象很可能保持 `null` 状态 + +## 4. 为什么 `RaceProps.mechWorkTypePriorities` 只对机械体有效 + +### 4.1 核心解释 + +`mechWorkTypePriorities` 是 `RaceProperties` 类中的一个字段,专门用于定义机械体(Mechanoid)在生成时其工作类型(Work Types)的默认优先级。它的设计和使用都与机械体的独特机制紧密相关,而这些机制在动物或其他生物上并不存在或不以相同方式运作。 + +### 4.2 详细证据和解释 + +1. **字段定义与命名**: + - `mechWorkTypePriorities` 被明确定义为: + ```csharp + public List mechWorkTypePriorities; + ``` + - 其名称 `mechWorkTypePriorities` 包含了 "Mech" 前缀,明确指示其用途是针对机械体的。 + +2. **`MechWorkTypePriority` 类型**: + - 该字段存储的是 `MechWorkTypePriority` 对象的列表。虽然代码片段中没有直接给出 `MechWorkTypePriority` 的定义,但其名称和用途(存储工作类型及其优先级)强烈暗示它是专门为机械体设计的数据结构。 + +3. **使用场景 - 初始化工作设置**: + - 关键证据出现在 `Pawn_WorkSettings.EnableAndInitialize()` 方法中: + ```csharp + // ... (其他初始化代码) ... + if (ModsConfig.BiotechActive && pawn.RaceProps.IsMechanoid && !pawn.RaceProps.mechWorkTypePriorities.NullOrEmpty()) + { + for (int i = 0; i < pawn.RaceProps.mechWorkTypePriorities.Count; i++) + { + MechWorkTypePriority mechWorkTypePriority = pawn.RaceProps.mechWorkTypePriorities[i]; + SetPriority(mechWorkTypePriority.def, mechWorkTypePriority.priority); + } + } + // ... (其他初始化代码,如处理禁用的工作类型) ... + ``` + - 这段代码明确地检查了三个条件: + * `ModsConfig.BiotechActive`:生物技术(Biotech)模组是否激活。这表明该功能是与 Biotech 模组引入的机械体相关的。 + * `pawn.RaceProps.IsMechanoid`:当前 Pawn(单位)的种族属性是否为机械体。 + * `!pawn.RaceProps.mechWorkTypePriorities.NullOrEmpty()`:该机械体的 `mechWorkTypePriorities` 列表是否存在且非空。 + - **只有当这三个条件都满足时,才会遍历 `mechWorkTypePriorities` 列表,并根据其中定义的 `WorkTypeDef` 和优先级来设置该机械体 Pawn 的工作优先级。** + - 对于动物或其他非机械体生物,`pawn.RaceProps.IsMechanoid` 条件为假,因此即使在它们的 `ThingDef` 中定义了 `mechWorkTypePriorities`,这段代码也不会执行,这些优先级设置就会被忽略。 + +4. **机制差异 - 动物 vs 机械体**: + - **动物**:动物的工作能力(如驯服、训练、特定任务)通常是通过 `trainability`(可训练性)、`trainableTags`(可训练标签)等属性来定义的。它们的工作行为(如觅食、繁殖、战斗)主要由其 `intelligence`(智力)、`thinkTreeMain`(主思考树)和 `dutyBoss`(职责)等属性驱动。它们的初始工作优先级(如果有的话)通常是固定的或基于简单规则随机分配,而不是通过一个专门的优先级列表来精确控制。 + - **机械体**:机械体是 Biotech 模组引入的复杂单位,它们拥有类似殖民者的工作系统。它们可以被指派执行各种殖民者能做的工作(如建造、种植、烹饪等),其能力范围和初始工作优先级需要更精细的控制。`mechWorkTypePriorities` 就是为了满足这种需求而设计的,允许设计者为每种机械体明确指定哪些工作类型应该默认开启以及它们的优先级。 + +5. **辅助证据 - MechWorkUtility**: + - 在 `MechWorkUtility` 类中,多个方法(如 `SpecialDisplayStats`, `AnyWorkMechCouldDo`)在处理与机械体工作相关逻辑时,都会首先检查 `parentDef.race.IsMechanoid`。这进一步证明了与机械体工作类型相关的逻辑是严格限定于机械体的。 + +### 4.3 结论 + +`RaceProperties.mechWorkTypePriorities` 虽然是 `RaceProperties` 类的一个通用属性,但它被设计并实现为**仅对机械体(Mechanoid)生效**。这是因为: + +1. 它的命名和数据类型 (`MechWorkTypePriority`) 明确指向机械体。 +2. 核心的使用代码 (`Pawn_WorkSettings.EnableAndInitialize`) 通过 `pawn.RaceProps.IsMechanoid` 条件严格限制了其应用范围。 +3. 机械体和动物在工作系统上的根本差异决定了需要不同的机制来管理其初始工作能力,`mechWorkTypePriorities` 是为满足机械体特有需求而生的。 + +因此,即使在动物的 `ThingDef` 文件中添加了 `mechWorkTypePriorities` 部分,游戏代码也不会读取或应用这些设置,因为动物不是机械体,相关的初始化逻辑不会被执行。 + +## 5. 通过 Harmony 补丁拦截 `pawn.RaceProps.IsMechanoid` 的分析 + +### 5.1 用户提供的实现思路 + +用户提供了一种更精确和安全的实现思路:通过 Harmony 补丁拦截 `Pawn_WorkSettings.EnableAndInitialize` 方法,在特定条件下局部伪装成机械体来为动物启用工作系统。 + +### 5.2 实现方法 + +1. **引入 Harmony**:确保你的 Mod 项目中引用了 Harmony 库。 +2. **定义补丁类**:创建一个静态类来存放你的 Harmony 补丁。 +3. **编写前缀补丁**: + ```csharp + [HarmonyPatch(typeof(Pawn_WorkSettings), nameof(Pawn_WorkSettings.EnableAndInitialize))] + public static class Patch_Pawn_WorkSettings_EnableAndInitialize + { + public static bool Prefix(Pawn_WorkSettings __instance, Pawn ___pawn) + { + // 检查是否是我们想要启用工作系统的动物 + if (___pawn.Faction != null && ___pawn.Faction.IsPlayer && + !___pawn.RaceProps.IsMechanoid && // 真实身份不是机械体 + ShouldEnableWorkSystem(___pawn)) // 但我们需要为它启用工作系统 + { + // 局部伪装成机械体来执行初始化逻辑 + + // 执行机械体工作优先级初始化逻辑 + if (ModsConfig.BiotechActive && !___pawn.RaceProps.mechWorkTypePriorities.NullOrEmpty()) + { + for (int i = 0; i < ___pawn.RaceProps.mechWorkTypePriorities.Count; i++) + { + var priority = ___pawn.RaceProps.mechWorkTypePriorities[i]; + __instance.SetPriority(priority.def, priority.priority); + } + } + + // 同时也可以初始化 mechEnabledWorkTypes 中的工作类型 + if (!___pawn.RaceProps.mechEnabledWorkTypes.NullOrEmpty()) + { + foreach (var workType in ___pawn.RaceProps.mechEnabledWorkTypes) + { + if (!__instance.WorkIsActive(workType) && !___pawn.WorkTypeIsDisabled(workType)) + { + __instance.SetPriority(workType, 3); // 默认优先级 + } + } + } + + // 阻止原方法继续执行 + return false; + } + + return true; // 其他情况正常执行原逻辑 + } + + private static bool ShouldEnableWorkSystem(Pawn pawn) + { + // 检查是否有特定的 Comp + if (pawn.TryGetComp() != null) + return true; + + // 检查是否有 mechWorkTypePriorities 或 mechEnabledWorkTypes 配置 + if (!pawn.RaceProps.mechWorkTypePriorities.NullOrEmpty() || + !pawn.RaceProps.mechEnabledWorkTypes.NullOrEmpty()) + return true; + + return false; + } + } + ``` +4. **应用补丁**: + ```csharp + [StaticConstructorOnStartup] + public static class AnimalWorkSystemPatcher + { + static AnimalWorkSystemPatcher() + { + var harmony = new Harmony("com.yourname.animalworksystem"); + harmony.PatchAll(); + } + } + ``` + +### 5.3 潜在问题和风险 + +1. **副作用**: + - 这种方法比直接补丁 `IsMechanoid` 属性更加精确,因为它只在 `Pawn_WorkSettings.EnableAndInitialize` 方法中进行伪装,不影响其他系统。 + - 但仍需注意确保伪装逻辑只在特定条件下触发,避免意外影响其他 Pawn。 + +2. **补丁冲突**: + - 其他 Mod 也可能使用 Harmony 来修改 `Pawn_WorkSettings` 或相关逻辑。 + - 需要确保补丁逻辑具有良好的兼容性。 + +3. **性能影响**: + - Harmony 补丁会增加方法调用的开销,但在这个场景下影响很小。 + +4. **维护困难**: + - 如果游戏更新修改了相关逻辑,你的补丁可能失效或产生新的问题,需要随之更新。 + +### 5.4 结论和建议 + +用户提供的实现思路是一种更加精确和安全的方法,通过局部伪装成机械体来为动物启用工作系统。这种方法的优点包括: + +1. **精确性**:只在特定条件下(动物是玩家阵营且需要启用工作系统)才进行伪装。 +2. **安全性**:只在 `Pawn_WorkSettings.EnableAndInitialize` 方法中进行伪装,不影响其他系统。 +3. **兼容性**:通过检查特定的 Comp 或配置来决定是否启用工作系统,具有良好的可扩展性。 + +## 6. 完整实现方案 + +### 6.1 核心思路 + +1. 为动物添加 `CompWorkForNonMechs` 以启用工作系统 +2. 创建 `ThinkNode_AnimalWorker` 替代默认行为,强制工作 +3. 使用 Harmony 补丁局部伪装成机械体来为动物启用工作系统 +4. 使用自定义 ThinkTree 替代默认动物行为树 +5. 定义允许的工作类型,并确保其与 WorkGiver 兼容 + +### 6.2 设计要点 + +1. **为动物启用 WorkSettings 系统**:通过 Harmony 补丁局部伪装成机械体来为动物启用工作系统 +2. **使用自定义 ThinkNode 强制工作行为**:创建 ThinkNode,跳过所有动物默认行为,直接进入工作逻辑 +3. **修改动物定义**:添加 ThinkTree 和 Comp +4. **为动物添加工作类型支持**:定义允许的工作类型 + +## 7. 实现细节 + +### 7.1 CompWorkForNonMechs.cs + +```csharp +using System.Collections.Generic; +using RimWorld; +using Verse; + +namespace YourModName +{ + public class CompWorkForNonMechs : ThingComp + { + public class CompProperties_WorkForNonMechs : CompProperties + { + public List workTypes; + + public CompProperties_WorkForNonMechs() + { + compClass = typeof(CompWorkForNonMechs); + } + } + + public override void PostSpawnSetup(bool respawningAfterLoad) + { + base.PostSpawnSetup(respawningAfterLoad); + + var pawn = parent as Pawn; + if (pawn == null || pawn.Faction == null || !pawn.Faction.IsPlayer) return; + + // 启用工作设置 + if (pawn.workSettings == null) + { + pawn.workSettings = new Pawn_WorkSettings(pawn); + } + + pawn.workSettings.EnableAndInitialize(); + + // 启用指定的工作类型 + var props = (CompProperties_WorkForNonMechs)props; + if (props.workTypes != null) + { + foreach (var workType in props.workTypes) + { + if (!pawn.WorkTypeIsDisabled(workType)) + { + pawn.workSettings.SetPriority(workType, 3); // 默认优先级 + } + } + } + } + } +} +``` + +### 7.2 ThinkNode_AnimalWorker.cs + +```csharp +using RimWorld; +using Verse; + +namespace YourModName +{ + public class ThinkNode_AnimalWorker : ThinkNode_Priority + { + public override ThinkResult TryIssueJobPackage(Pawn pawn, JobIssueParams jobParams) + { + if (pawn.workSettings == null || !pawn.Faction.IsPlayer) + { + return ThinkResult.NoJob; + } + + // 优先执行紧急工作 + var job = WorkGiverUtility.GetPriorityWork(pawn, emergency: true); + if (job != null) return new ThinkResult(job, this); + + // 然后执行普通工作 + job = WorkGiverUtility.GetPriorityWork(pawn, emergency: false); + if (job != null) return new ThinkResult(job, this); + + return ThinkResult.NoJob; + } + } +} +``` + +### 7.3 AnimalWorkSystemPatcher.cs + +```csharp +using System.Collections.Generic; +using System.Reflection; +using HarmonyLib; +using RimWorld; +using Verse; + +namespace YourModName +{ + [StaticConstructorOnStartup] + public static class AnimalWorkSystemPatcher + { + static AnimalWorkSystemPatcher() + { + var harmony = new Harmony("com.yourname.animalworksystem"); + harmony.PatchAll(); + } + } + + [HarmonyPatch(typeof(Pawn_WorkSettings), nameof(Pawn_WorkSettings.EnableAndInitialize))] + public static class Patch_Pawn_WorkSettings_EnableAndInitialize + { + // 缓存原始的 IsMechanoid 属性 Getter + private static PropertyInfo isMechanoidProperty = + typeof(RaceProperties).GetProperty("IsMechanoid", BindingFlags.Public | BindingFlags.Instance); + + public static bool Prefix(Pawn_WorkSettings __instance, Pawn ___pawn) + { + // 检查是否是我们想要启用工作系统的动物 + if (___pawn.Faction != null && ___pawn.Faction.IsPlayer && + !___pawn.RaceProps.IsMechanoid && // 真实身份不是机械体 + ShouldEnableWorkSystem(___pawn)) // 但我们需要为它启用工作系统 + { + // 局部伪装成机械体来执行初始化逻辑 + + // 执行机械体工作优先级初始化逻辑(来自 Pawn_WorkSettings.EnableAndInitialize) + if (ModsConfig.BiotechActive && !___pawn.RaceProps.mechWorkTypePriorities.NullOrEmpty()) + { + for (int i = 0; i < ___pawn.RaceProps.mechWorkTypePriorities.Count; i++) + { + var priority = ___pawn.RaceProps.mechWorkTypePriorities[i]; + __instance.SetPriority(priority.def, priority.priority); + } + } + + // 同时也可以初始化 mechEnabledWorkTypes 中的工作类型(如果你需要) + if (!___pawn.RaceProps.mechEnabledWorkTypes.NullOrEmpty()) + { + foreach (var workType in ___pawn.RaceProps.mechEnabledWorkTypes) + { + if (!__instance.WorkIsActive(workType) && !___pawn.WorkTypeIsDisabled(workType)) + { + __instance.SetPriority(workType, 3); // 默认优先级 + } + } + } + + // 阻止原方法继续执行(因为我们已经手动处理了初始化) + return false; + } + + return true; // 其他情况正常执行原逻辑 + } + + private static bool ShouldEnableWorkSystem(Pawn pawn) + { + // 你可以通过多种方式判断: + // 1. 检查是否有特定的 Comp + if (pawn.TryGetComp() != null) + return true; + + // 2. 检查是否有 mechWorkTypePriorities 或 mechEnabledWorkTypes 配置 + if (!pawn.RaceProps.mechWorkTypePriorities.NullOrEmpty() || + !pawn.RaceProps.mechEnabledWorkTypes.NullOrEmpty()) + return true; + + // 3. 检查特定的标签或 defName + // return pawn.def.defName.Contains("Worker"); + + return false; + } + } +} +``` + +### 7.4 修改动物定义 + +```xml + + Megascarab_Worker + + A genetically modified megascarab, capable of performing simple mechanical tasks. + + 3.0 + 300 + + + InsectWorker + AnimalConstant + +
  • + Hauling + 3 +
  • +
  • + Cleaning + 2 +
  • + +
    + +
  • + +
  • Hauling
  • +
  • Cleaning
  • +
  • BasicWorker
  • + + +
    + +``` + +### 7.5 自定义 ThinkTreeDef + +```xml + + InsectWorker + + + +
  • Downed
  • +
  • BurningResponse
  • +
  • MentalStateCritical
  • + + +
  • + + +
  • + + + +``` + +## 8. 原始方案的反驳分析 + +### 8.1 项目概述:不成立 + +**反驳证据:** +- RimWorld 原版设计中,动物的行为树是完全独立于机械体的,没有 WorkMode 系统。 +- 动物不能直接复用机械体的工作逻辑。 + +### 8.2 ThinkNode_AnimalAlwaysWork 类:不成立 + +**反驳证据:** +- `ThinkNode_Priority` 是一个标准的 ThinkNode,它会按顺序尝试子节点,直到某一个返回 `ThinkResult`。 +- 该类不会实现"始终工作"的功能,因为它没有改变行为树的执行逻辑。 + +### 8.3 InsectWorker ThinkTreeDef:不成立 + +**反驳证据:** +- RimWorld 中,动物的 ThinkTree 并不会调用 `JobGiver_Work`,因为该类是为机械体设计的。 +- 即使强制插入 `JobGiver_Work`,它也会因为动物的 `WorkSettings` 未初始化而失败。 + +### 8.4 Harmony 补丁拦截 IsMechanoid:不成立 + +**反驳证据:** +- `RaceProperties.IsMechanoid` 是一个只读属性,由 `true` 控制。 +- Patch 该属性会导致所有依赖该属性的逻辑被干扰,风险极高。 + +## 9. 使用说明 + +1. **编译代码**:将所有 C# 文件编译到您的 Mod 中 +2. **添加 XML 文件**:将新的 ThinkTreeDef 和动物定义添加到您的 Mod 中 +3. **配置工作类型**:在动物定义中配置 `mechWorkTypePriorities` 和 `CompWorkForNonMechs` 组件 +4. **测试**:在游戏中生成修改后的动物,观察其工作行为 + +## 10. 注意事项 + +1. **兼容性**:此方案绕过了 WorkMode 系统,动物将始终处于工作状态 +2. **性能**:频繁的工作查找可能会有轻微性能影响 +3. **调试**:如果遇到问题,请检查日志文件中的错误信息 +4. **避免 patch IsMechanoid**:不干扰原版逻辑 +5. **副作用风险**:直接 patch `IsMechanoid` 会导致严重的副作用,强烈不推荐使用 +6. **精确补丁**:用户提供的实现通过局部伪装成机械体来为动物启用工作系统,是一种更加精确和安全的方法 \ No newline at end of file