This commit is contained in:
2025-08-09 20:17:54 +08:00
parent 52d07b6a07
commit 0e63046eca
9 changed files with 124 additions and 90 deletions

View File

@@ -1,5 +1,4 @@
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using Verse;
@@ -11,61 +10,52 @@ namespace WulaFallenEmpire
{
public override ThingRequest PotentialWorkThingRequest => ThingRequest.ForDef(ThingDefOf_WULA.WULA_MaintenancePod);
public override PathEndMode PathEndMode => PathEndMode.Touch;
public override Danger MaxPathDanger(Pawn pawn) => Danger.Deadly;
public override IEnumerable<Thing> PotentialWorkThingsGlobal(Pawn pawn)
{
return pawn.Map.listerBuildings.AllBuildingsColonistOfDef(ThingDefOf_WULA.WULA_MaintenancePod);
}
public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
{
if (!(t is Building building) || building.IsForbidden(pawn) || !pawn.CanReserve(building, 1, -1, null, forced))
if (!(t is Building pod) || !pawn.CanReserve(pod, 1, -1, null, forced))
{
return false;
}
CompMaintenancePod comp = building.GetComp<CompMaintenancePod>();
if (comp == null || comp.State != MaintenancePodState.Idle)
var podComp = pod.GetComp<CompMaintenancePod>();
if (podComp == null || podComp.State != MaintenancePodState.Idle || !podComp.PowerOn)
{
return false;
}
// Check if it needs more components
if (comp.storedComponents >= comp.Props.capacity)
{
return false;
}
if (FindBestComponent(pawn, comp) == null)
{
JobFailReason.Is("WULA_NoComponentsToHaul".Translate());
return false;
}
return true;
Pawn patient = FindPatientFor(pawn, podComp);
return patient != null && pawn.CanReserve(patient, 1, -1, null, forced);
}
public override Job JobOnThing(Pawn pawn, Thing t, bool forced = false)
{
Building building = (Building)t;
CompMaintenancePod comp = building.GetComp<CompMaintenancePod>();
Thing component = FindBestComponent(pawn, comp);
if (component == null)
var pod = (Building)t;
var podComp = pod.GetComp<CompMaintenancePod>();
Pawn patient = FindPatientFor(pawn, podComp);
if (patient == null)
{
return null;
}
Job job = JobMaker.MakeJob(JobDefOf_WULA.WULA_LoadComponentsToMaintenancePod, component, t);
job.count = Math.Min(component.stackCount, (int)(comp.Props.capacity - comp.storedComponents));
return job;
return JobMaker.MakeJob(JobDefOf_WULA.WULA_HaulToMaintenancePod, patient, pod);
}
private Thing FindBestComponent(Pawn pawn, CompMaintenancePod podComp)
private Pawn FindPatientFor(Pawn rescuer, CompMaintenancePod podComp)
{
ThingFilter filter = podComp.GetStoreSettings().filter;
Predicate<Thing> validator = (Thing x) => !x.IsForbidden(pawn) && pawn.CanReserve(x) && filter.Allows(x);
return GenClosest.ClosestThingReachable(pawn.Position, pawn.Map, filter.BestThingRequest, PathEndMode.ClosestTouch, TraverseParms.For(pawn), 9999f, validator);
return rescuer.Map.mapPawns.AllPawnsSpawned
.Where(p => p.def == ThingDefOf_WULA.Wula &&
p.Faction == rescuer.Faction &&
!p.IsForbidden(rescuer) &&
p.Downed && // Key condition: pawn cannot walk
p.health.hediffSet.HasHediff(podComp.Props.hediffToRemove) &&
podComp.RequiredComponents(p) <= podComp.parent.GetComp<CompRefuelable>().Fuel &&
rescuer.CanReserve(p))
.OrderBy(p => p.Position.DistanceTo(rescuer.Position))
.FirstOrDefault();
}
}
}