修割除
This commit is contained in:
@@ -8,41 +8,58 @@ namespace ArachnaeSwarm
|
||||
public class JobGiver_Grower : ThinkNode_JobGiver
|
||||
{
|
||||
private WorkGiver_GrowerHarvest _workGiverHarvest;
|
||||
private WorkGiver_ArachnaeSow _workGiverArachnaeSow; // 修改为我们的新 WorkGiver
|
||||
|
||||
private WorkGiver_Scanner _workGiverPlantsCut; // 通用扫描器类型
|
||||
private WorkGiver_ArachnaeSow _workGiverArachnaeSow;
|
||||
|
||||
protected override Job TryGiveJob(Pawn pawn)
|
||||
{
|
||||
// 懒加载 WorkGiver 实例,确保 DefOf 已被初始化
|
||||
if (_workGiverHarvest == null)
|
||||
{
|
||||
_workGiverHarvest = WorkGiverDefOf.GrowerHarvest.Worker as WorkGiver_GrowerHarvest;
|
||||
_workGiverArachnaeSow = new WorkGiver_ArachnaeSow(); // 直接实例化我们的 WorkGiver
|
||||
|
||||
if (_workGiverHarvest == null || _workGiverArachnaeSow == null)
|
||||
_workGiverPlantsCut = WorkGiverDefOf.PlantsCut.Worker as WorkGiver_Scanner;
|
||||
_workGiverArachnaeSow = new WorkGiver_ArachnaeSow();
|
||||
|
||||
if (_workGiverHarvest == null || _workGiverPlantsCut == null)
|
||||
{
|
||||
Log.ErrorOnce("JobGiver_Grower: Failed to get WorkGiver_GrowerHarvest or WorkGiver_ArachnaeSow. DefOfs might not be initialized or WorkGiver_ArachnaeSow could not be instantiated.", 123457);
|
||||
Log.ErrorOnce("JobGiver_Grower: Failed to get a required WorkGiver. DefOfs might not be initialized.", 123458);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 优先收获 - 使用基于 Cell 的新方法
|
||||
|
||||
// 1. 优先收获(自动)
|
||||
IntVec3 bestHarvestCell = FindClosestHarvestableCell(pawn);
|
||||
if (bestHarvestCell.IsValid)
|
||||
{
|
||||
return _workGiverHarvest.JobOnCell(pawn, bestHarvestCell);
|
||||
}
|
||||
|
||||
// 2. 其次播种
|
||||
(IntVec3 bestSowCell, ThingDef plantToSow) = FindClosestSowableCellAndPlant(pawn, _workGiverArachnaeSow); // 使用我们的新 WorkGiver
|
||||
|
||||
// 2. 其次处理手动指定的砍伐/收获任务
|
||||
Thing bestCuttable = FindClosestWorkableThing(pawn, _workGiverPlantsCut);
|
||||
if (bestCuttable != null)
|
||||
{
|
||||
return _workGiverPlantsCut.JobOnThing(pawn, bestCuttable);
|
||||
}
|
||||
|
||||
// 3. 最后播种(自动,并清理障碍)
|
||||
(IntVec3 bestSowCell, ThingDef plantToSow) = FindClosestSowableCellAndPlant(pawn, _workGiverArachnaeSow);
|
||||
if (bestSowCell.IsValid && plantToSow != null)
|
||||
{
|
||||
// 现在直接调用 WorkGiver_ArachnaeSow 的 JobOnCell,它会处理 Job 的创建和 plantDefToSow 的设置
|
||||
return _workGiverArachnaeSow.JobOnCell(pawn, bestSowCell);
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -52,13 +69,11 @@ namespace ArachnaeSwarm
|
||||
{
|
||||
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))
|
||||
{
|
||||
// 使用 HasJobOnCell 来判断是否可以收获
|
||||
if (_workGiverHarvest.HasJobOnCell(pawn, cell))
|
||||
{
|
||||
bestDistSq = distSq;
|
||||
@@ -70,8 +85,8 @@ namespace ArachnaeSwarm
|
||||
}
|
||||
return bestCell;
|
||||
}
|
||||
|
||||
private (IntVec3, ThingDef) FindClosestSowableCellAndPlant(Pawn pawn, WorkGiver_ArachnaeSow scanner) // 修改为我们的新 WorkGiver 类型
|
||||
|
||||
private (IntVec3, ThingDef) FindClosestSowableCellAndPlant(Pawn pawn, WorkGiver_ArachnaeSow scanner)
|
||||
{
|
||||
IntVec3 bestCell = IntVec3.Invalid;
|
||||
ThingDef bestPlantToSow = null;
|
||||
@@ -81,7 +96,7 @@ namespace ArachnaeSwarm
|
||||
{
|
||||
if (zone is Zone_Growing growingZone)
|
||||
{
|
||||
ThingDef wantedPlant = growingZone.GetPlantDefToGrow();
|
||||
ThingDef wantedPlant = growingZone.GetPlantDefToGrow();
|
||||
if (wantedPlant == null) continue;
|
||||
|
||||
foreach (IntVec3 cell in growingZone.Cells)
|
||||
@@ -89,13 +104,11 @@ namespace ArachnaeSwarm
|
||||
float distSq = pawn.Position.DistanceToSquared(cell);
|
||||
if (distSq < bestDistSq && pawn.CanReach(cell, PathEndMode.ClosestTouch, Danger.Deadly))
|
||||
{
|
||||
// 这里不再需要 WorkGiver_Grower.wantedPlantDef 的复杂处理
|
||||
// 因为 WorkGiver_ArachnaeSow.JobOnCell 会直接使用它计算出的 wantedPlantDef
|
||||
if (scanner.HasJobOnCell(pawn, cell)) // HasJobOnCell 内部会根据 wantedPlant 计算
|
||||
if (scanner.HasJobOnCell(pawn, cell))
|
||||
{
|
||||
bestDistSq = distSq;
|
||||
bestCell = cell;
|
||||
bestPlantToSow = wantedPlant; // 确保返回正确的 plantDef
|
||||
bestPlantToSow = wantedPlant;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,12 +119,12 @@ namespace ArachnaeSwarm
|
||||
}
|
||||
}
|
||||
|
||||
// 确保 WorkGiverDefOf 被正确初始化,放在命名空间顶层
|
||||
[DefOf]
|
||||
public static class WorkGiverDefOf
|
||||
{
|
||||
public static WorkGiverDef GrowerHarvest;
|
||||
|
||||
public static WorkGiverDef PlantsCut;
|
||||
|
||||
static WorkGiverDefOf()
|
||||
{
|
||||
DefOfHelper.EnsureInitializedInCtor(typeof(WorkGiverDefOf));
|
||||
|
||||
Reference in New Issue
Block a user