扫地和hediff

This commit is contained in:
2025-09-05 13:53:55 +08:00
parent 3a3c30087a
commit a84e7bbb94
8 changed files with 113 additions and 17 deletions

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Verse;
using RimWorld;
namespace ArachnaeSwarm
{
public class CompHediffGiver : ThingComp
{
public CompProperties_HediffGiver Props => (CompProperties_HediffGiver)this.props;
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
// 只有当thing是pawn时才添加hediff
if (this.parent is Pawn pawn)
{
AddHediffsToPawn(pawn);
}
}
private void AddHediffsToPawn(Pawn pawn)
{
// 检查是否有hediff列表
if (Props.hediffs == null || Props.hediffs.Count == 0)
return;
// 检查概率
if (Props.addChance < 1.0f && Rand.Value > Props.addChance)
return;
// 为每个hediff添加到pawn
foreach (HediffDef hediffDef in Props.hediffs)
{
// 检查是否允许重复添加
if (!Props.allowDuplicates && pawn.health.hediffSet.HasHediff(hediffDef))
continue;
// 添加hediff
pawn.health.AddHediff(hediffDef);
}
}
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using Verse;
namespace ArachnaeSwarm
{
public class CompProperties_HediffGiver : CompProperties
{
// 要添加的hediff列表
public List<HediffDef> hediffs;
// 添加hediff的概率0-1之间
public float addChance = 1.0f;
// 是否允许重复添加相同的hediff
public bool allowDuplicates = false;
public CompProperties_HediffGiver()
{
this.compClass = typeof(CompHediffGiver);
}
}
}

View File

@@ -36,12 +36,15 @@ namespace ArachnaeSwarm
if (filthJob != null) return filthJob;
}
// If no filth, find the closest snow/sand to clear
IntVec3 closestSnowCell = _workGiver.FindClosestSnow(pawn);
if (closestSnowCell.IsValid)
// If no filth, check if there's any snow to clear at all before searching
if (pawn.Map.areaManager.SnowOrSandClear.TrueCount > 0)
{
Job snowJob = _workGiver.JobOnCell(pawn, closestSnowCell);
if (snowJob != null) return snowJob;
IntVec3 closestSnowCell = _workGiver.FindClosestSnow(pawn);
if (closestSnowCell.IsValid)
{
Job snowJob = _workGiver.JobOnCell(pawn, closestSnowCell);
if (snowJob != null) return snowJob;
}
}
return null;

View File

@@ -62,14 +62,31 @@ namespace ArachnaeSwarm
public IntVec3 FindClosestSnow(Pawn pawn)
{
return CellFinder.RandomClosewalkCellNear(pawn.Position, pawn.Map, 100,
c => HasJobOnCell(pawn, c) && pawn.CanReach(c, PathEndMode.Touch, Danger.Deadly));
IntVec3 bestCell = IntVec3.Invalid;
float bestDistSq = float.MaxValue;
// Efficiently iterate through the designated snow clear area
foreach (IntVec3 cell in pawn.Map.areaManager.SnowOrSandClear.ActiveCells)
{
// Check for a valid job on the cell first
if (HasJobOnCell(pawn, cell))
{
float distSq = pawn.Position.DistanceToSquared(cell);
if (distSq < bestDistSq && pawn.CanReach(cell, PathEndMode.Touch, Danger.Deadly))
{
bestDistSq = distSq;
bestCell = cell;
}
}
}
return bestCell;
}
public override bool HasJobOnCell(Pawn pawn, IntVec3 c, bool forced = false)
{
// The check for SnowOrSandClear area is implicitly handled by the caller (FindClosestSnow)
// We only need to check the snow depth and reservation status.
if (pawn.Map.snowGrid.GetDepth(c) < 0.2f) return false;
if (!pawn.Map.areaManager.SnowOrSandClear[c]) return false; // Must be in the clear zone
if (!pawn.CanReserve(c, 1, -1, null, forced)) return false;
return true;
}

View File

@@ -85,6 +85,8 @@
<Compile Include="ARA_SpawnPawnFromList\JobGiver_MaintainBuildings.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="ARA_CompHediffGiver\CompHediffGiver.cs" />
<Compile Include="ARA_CompHediffGiver\CompProperties_HediffGiver.cs" />
<Compile Include="ARA_CompMilkableArachnae\CompMilkableArachnae.cs" />
<Compile Include="ARA_CompMilkableArachnae\CompProperties_MilkableArachnae.cs" />
</ItemGroup>