整理scoure

This commit is contained in:
2025-10-31 09:57:45 +08:00
parent 9e6aa98830
commit 8fee1bcfba
103 changed files with 5547 additions and 916 deletions

View File

@@ -0,0 +1,118 @@
using System;
using Verse;
namespace WulaFallenEmpire
{
public class HediffCompProperties_Spawner : HediffCompProperties
{
public HediffCompProperties_Spawner()
{
this.compClass = typeof(HediffComp_Spawner);
}
/// <summary>
/// 要生成的物品的ThingDef。如果animalThing为false则使用此项。
/// </summary>
public ThingDef thingToSpawn;
/// <summary>
/// 每次生成的基础物品数量。
/// </summary>
public int spawnCount = 1;
/// <summary>
/// 如果为true则生成一个Pawn动物。如果为false则生成一个Thing。
/// </summary>
public bool animalThing;
/// <summary>
/// 要生成的动物的PawnKindDef。如果animalThing为true则使用此项。
/// </summary>
public PawnKindDef animalToSpawn;
/// <summary>
/// 如果为true生成的动物将属于玩家派系。
/// </summary>
public bool factionOfPlayerAnimal;
/// <summary>
/// 下一次生成事件发生前的最少天数。
/// </summary>
public float minDaysB4Next = 1f;
/// <summary>
/// 下一次生成事件发生前的最大天数。
/// </summary>
public float maxDaysB4Next = 2f;
/// <summary>
/// 生成后进入宽限期延迟下一次生成的几率0.0到1.0)。
/// </summary>
public float randomGrace;
/// <summary>
/// 如果触发,宽限期的持续时间(天)。
/// </summary>
public float graceDays = 0.5f;
/// <summary>
/// 附近允许的相同Pawn的最大数量。如果超过该数量则暂停生成。-1为禁用。
/// </summary>
public int spawnMaxAdjacent = -1;
/// <summary>
/// 如果为true生成的物品将被禁用。
/// </summary>
public bool spawnForbidden;
/// <summary>
/// 如果为true当宿主Pawn饥饿时生成将暂停。
/// </summary>
public bool hungerRelative;
/// <summary>
/// 如果为true当宿主Pawn受伤时生成将暂停。
/// </summary>
public bool healthRelative;
/// <summary>
/// 如果为true生成数量将根据宿主的年龄进行调整。
/// </summary>
public bool ageWeightedQuantity;
/// <summary>
/// 如果为true生成周期两次生成之间的时间将根据宿主的年龄进行调整。
/// </summary>
public bool ageWeightedPeriod;
/// <summary>
/// 如果为true且ageWeightedPeriod为true则随着宿主年龄增长生成周期变短。如果为false则变长。
/// </summary>
public bool olderSmallerPeriod;
/// <summary>
/// 如果为true且ageWeightedQuantity为true则随着宿主年龄增长生成数量变多。如果为false则变少。
/// </summary>
public bool olderBiggerQuantity;
/// <summary>
/// 如果为true且ageWeightedQuantity为true则随年龄增长的数量缩放将是指数性的而非线性的。
/// </summary>
public bool exponentialQuantity;
/// <summary>
/// 指数级数量缩放的最大乘数,以防止出现荒谬的数字。
/// </summary>
public int exponentialRatioLimit = 15;
/// <summary>
/// 生成时显示的消息的翻译键(例如,“{PAWN}下了一个蛋。”)。
/// </summary>
public string spawnVerb = "delivery";
/// <summary>
/// 如果为true则为此组件启用详细的调试日志记录。
/// </summary>
public bool debug;
}
}

View File

@@ -0,0 +1,565 @@
using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using RimWorld.Planet;
using Verse;
namespace WulaFallenEmpire
{
public class HediffComp_Spawner : HediffComp
{
public HediffCompProperties_Spawner Props
{
get
{
return (HediffCompProperties_Spawner)this.props;
}
}
public override void CompExposeData()
{
Scribe_Values.Look<int>(ref this.ticksUntilSpawn, "ticksUntilSpawn", 0, false);
Scribe_Values.Look<int>(ref this.initialTicksUntilSpawn, "initialTicksUntilSpawn", 0, false);
Scribe_Values.Look<float>(ref this.calculatedMinDaysB4Next, "calculatedMinDaysB4Next", 0f, false);
Scribe_Values.Look<float>(ref this.calculatedMaxDaysB4Next, "calculatedMaxDaysB4Next", 0f, false);
Scribe_Values.Look<int>(ref this.calculatedQuantity, "calculatedQuantity", 0, false);
Scribe_Values.Look<int>(ref this.graceTicks, "graceTicks", 0, false);
}
public override void CompPostMake()
{
this.myDebug = this.Props.debug;
Tools.Warn(string.Concat(new string[]
{
">>> ",
this.parent.pawn.Label,
" - ",
this.parent.def.defName,
" - CompPostMake start"
}), this.myDebug);
this.TraceProps();
this.CheckProps();
this.CalculateValues();
this.CheckCalculatedValues();
this.TraceCalculatedValues();
if (this.initialTicksUntilSpawn == 0)
{
Tools.Warn("Reseting countdown bc initialTicksUntilSpawn == 0 (comppostmake)", this.myDebug);
this.ResetCountdown();
}
}
public override void CompPostTick(ref float severityAdjustment)
{
this.pawn = this.parent.pawn;
if (!Tools.OkPawn(this.pawn))
{
return;
}
if (this.blockSpawn)
{
return;
}
if (this.graceTicks > 0)
{
this.graceTicks--;
return;
}
if (this.Props.hungerRelative && this.pawn.IsHungry(this.myDebug))
{
int num = (int)(this.RandomGraceDays() * 60000f);
this.hungerReset++;
this.graceTicks = num;
return;
}
if (this.Props.healthRelative && this.pawn.IsInjured(this.myDebug))
{
int num2 = (int)(this.RandomGraceDays() * 60000f);
this.healthReset++;
this.graceTicks = num2;
return;
}
this.hungerReset = (this.healthReset = 0);
if (this.CheckShouldSpawn())
{
Tools.Warn("Reseting countdown bc spawned thing", this.myDebug);
this.CalculateValues();
this.CheckCalculatedValues();
this.ResetCountdown();
if (Rand.Chance(this.Props.randomGrace))
{
int num3 = (int)(this.RandomGraceDays() * 60000f);
this.graceTicks = num3;
}
}
}
private void TraceProps()
{
Tools.Warn(string.Concat(new string[]
{
"Props => minDaysB4Next: ",
this.Props.minDaysB4Next.ToString(),
"; maxDaysB4Next: ",
this.Props.maxDaysB4Next.ToString(),
"; randomGrace: ",
this.Props.randomGrace.ToString(),
"; graceDays: ",
this.Props.graceDays.ToString(),
"; hungerRelative: ",
this.Props.hungerRelative.ToString(),
"; healthRelative: ",
this.Props.healthRelative.ToString(),
"; "
}), this.myDebug);
if (this.Props.animalThing)
{
Tools.Warn(string.Concat(new string[]
{
"animalThing: ",
this.Props.animalThing.ToString(),
"; animalName: ",
this.Props.animalToSpawn.defName,
"; factionOfPlayerAnimal: ",
this.Props.factionOfPlayerAnimal.ToString(),
"; "
}), this.myDebug);
}
if (this.Props.ageWeightedQuantity)
{
Tools.Warn(string.Concat(new string[]
{
"ageWeightedQuantity:",
this.Props.ageWeightedQuantity.ToString(),
"; olderBiggerQuantity:",
this.Props.olderBiggerQuantity.ToString(),
"; ",
this.myDebug.ToString()
}), false);
if (this.Props.exponentialQuantity)
{
Tools.Warn(string.Concat(new string[]
{
"exponentialQuantity:",
this.Props.exponentialQuantity.ToString(),
"; exponentialRatioLimit:",
this.Props.exponentialRatioLimit.ToString(),
"; "
}), this.myDebug);
}
}
Tools.Warn(string.Concat(new string[]
{
"ageWeightedPeriod:",
this.Props.ageWeightedPeriod.ToString(),
"; olderSmallerPeriod:",
this.Props.olderSmallerPeriod.ToString(),
"; ",
this.myDebug.ToString()
}), false);
}
private void CalculateValues()
{
float num = Tools.GetPawnAgeOverlifeExpectancyRatio(this.parent.pawn, this.myDebug);
num = ((num > 1f) ? 1f : num);
this.calculatedMinDaysB4Next = this.Props.minDaysB4Next;
this.calculatedMaxDaysB4Next = this.Props.maxDaysB4Next;
if (this.Props.ageWeightedPeriod)
{
float num2 = this.Props.olderSmallerPeriod ? (-num) : num;
this.calculatedMinDaysB4Next = this.Props.minDaysB4Next * (1f + num2);
this.calculatedMaxDaysB4Next = this.Props.maxDaysB4Next * (1f + num2);
Tools.Warn(string.Concat(new string[]
{
" ageWeightedPeriod: ",
this.Props.ageWeightedPeriod.ToString(),
" ageRatio: ",
num.ToString(),
" minDaysB4Next: ",
this.Props.minDaysB4Next.ToString(),
" maxDaysB4Next: ",
this.Props.maxDaysB4Next.ToString(),
" daysAgeRatio: ",
num2.ToString(),
" calculatedMinDaysB4Next: ",
this.calculatedMinDaysB4Next.ToString(),
"; calculatedMaxDaysB4Next: ",
this.calculatedMaxDaysB4Next.ToString(),
"; "
}), this.myDebug);
}
this.calculatedQuantity = this.Props.spawnCount;
if (this.Props.ageWeightedQuantity)
{
float num3 = this.Props.olderBiggerQuantity ? num : (-num);
Tools.Warn("quantityAgeRatio: " + num3.ToString(), this.myDebug);
this.calculatedQuantity = (int)Math.Round((double)this.Props.spawnCount * (double)(1f + num3));
if (this.Props.exponentialQuantity)
{
num3 = 1f - num;
if (num3 == 0f)
{
Tools.Warn(">ERROR< quantityAgeRatio is f* up : " + num3.ToString(), this.myDebug);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
float num4 = this.Props.olderBiggerQuantity ? (1f / num3) : (num3 * num3);
bool flag = false;
bool flag2 = false;
if (num4 > (float)this.Props.exponentialRatioLimit)
{
num4 = (float)this.Props.exponentialRatioLimit;
flag = true;
}
this.calculatedQuantity = (int)Math.Round((double)this.Props.spawnCount * (double)num4);
if (this.calculatedQuantity < 1)
{
this.calculatedQuantity = 1;
flag2 = true;
}
Tools.Warn(string.Concat(new string[]
{
" exponentialQuantity: ",
this.Props.exponentialQuantity.ToString(),
"; expoFactor: ",
num4.ToString(),
"; gotLimited: ",
flag.ToString(),
"; gotAugmented: ",
flag2.ToString()
}), this.myDebug);
}
Tools.Warn("; Props.spawnCount: " + this.Props.spawnCount.ToString() + "; calculatedQuantity: " + this.calculatedQuantity.ToString(), this.myDebug);
}
}
private void CheckCalculatedValues()
{
if (this.calculatedQuantity > this.errorSpawnCount)
{
Tools.Warn(string.Concat(new string[]
{
">ERROR< calculatedQuantity is too high: ",
this.calculatedQuantity.ToString(),
"(>",
this.errorSpawnCount.ToString(),
"), check and adjust your hediff props"
}), this.myDebug);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
if (this.calculatedMinDaysB4Next < this.errorMinDaysB4Next)
{
this.calculatedMinDaysB4Next = this.errorMinDaysB4Next;
}
if (this.calculatedMaxDaysB4Next < this.errorMinDaysB4Next)
{
this.calculatedMaxDaysB4Next = this.errorMinDaysB4Next;
}
}
private void TraceCalculatedValues()
{
Tools.Warn("calculatedMinDaysB4Next:" + this.calculatedMinDaysB4Next.ToString(), this.myDebug);
Tools.Warn("calculatedMaxDaysB4Next:" + this.calculatedMaxDaysB4Next.ToString(), this.myDebug);
Tools.Warn("calculatedQuantity:" + this.calculatedQuantity.ToString(), this.myDebug);
}
private void CheckProps()
{
if (this.Props.animalThing && this.Props.animalToSpawn == null)
{
Tools.Warn(this.parent.pawn.Label + " has a hediffcomp_spawner with animalflag but without animalToSpawn", true);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
if (this.Props.minDaysB4Next <= 0f)
{
Tools.Warn(this.parent.pawn.Label + " has a hediffcomp_spawner with null/negative minDaysB4Next", true);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
if (this.Props.maxDaysB4Next <= 0f)
{
Tools.Warn(this.parent.pawn.Label + " has a hediffcomp_spawner with null/negative maxDaysB4Next", true);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
if (this.Props.maxDaysB4Next < this.Props.minDaysB4Next)
{
Tools.Warn(this.parent.pawn.Label + " has a hediffcomp_spawner with maxDaysB4Next < minDaysB4Next", true);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
if (this.Props.spawnCount <= 0)
{
Tools.Warn(this.parent.pawn.Label + " has a hediffcomp_spawner with null/negative spawnCount", true);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
if (!this.Props.animalThing && this.Props.thingToSpawn == null)
{
Tools.Warn(this.parent.pawn.Label + " has a hediffcomp_spawner without thingToSpawn", true);
this.blockSpawn = true;
Tools.DestroyParentHediff(this.parent, this.myDebug);
return;
}
if (this.Props.ageWeightedQuantity && this.Props.exponentialQuantity && this.Props.exponentialRatioLimit > this.errorExponentialLimit)
{
Tools.Warn(string.Concat(new string[]
{
this.parent.pawn.Label,
" has a hediffcomp_spawner with exponentialRatioLimit>",
this.errorExponentialLimit.ToString(),
" this is not allowed. It will be set to ",
this.errorExponentialLimit.ToString()
}), true);
this.Props.exponentialRatioLimit = this.errorExponentialLimit;
}
}
private bool CheckShouldSpawn()
{
this.ticksUntilSpawn--;
if (this.ticksUntilSpawn <= 0)
{
if (this.TryDoSpawn())
{
return true;
}
Tools.Warn("Did not spawn, reseting countdown", this.myDebug);
this.ResetCountdown();
}
return false;
}
private PawnKindDef MyPawnKindDefNamed(string myDefName)
{
return DefDatabase<PawnKindDef>.GetNamed(myDefName, true);
}
public bool TryDoSpawn()
{
Pawn pawn = this.parent.pawn;
if (this.Props.spawnMaxAdjacent > 0 && pawn.Map.mapPawns.AllPawns.Where(delegate(Pawn mP)
{
ThingDef defToCompare = this.Props.animalThing ? this.Props.animalToSpawn?.race : this.Props.thingToSpawn;
if (defToCompare?.race == null)
{
return false;
}
return mP.def == defToCompare && mP.Position.InHorDistOf(pawn.Position, (float)this.Props.spawnMaxAdjacent);
}).Count<Pawn>() >= this.Props.spawnMaxAdjacent)
{
return false;
}
if (this.Props.animalThing)
{
if (this.Props.animalToSpawn == null)
{
return false;
}
Faction faction = this.Props.factionOfPlayerAnimal ? Faction.OfPlayer : null;
int i = 0;
while (i < this.calculatedQuantity)
{
IntVec3 intVec;
if (!this.TryFindSpawnCell(out intVec))
{
return false;
}
Pawn pawn2 = PawnGenerator.GeneratePawn(this.Props.animalToSpawn, faction);
if (pawn2 == null)
{
return false;
}
GenSpawn.Spawn(pawn2, intVec, pawn.Map, WipeMode.Vanish);
pawn2.SetFaction(faction, null);
FilthMaker.TryMakeFilth(intVec, pawn.Map, ThingDefOf.Filth_AmnioticFluid, pawn.LabelIndefinite(), 5, FilthSourceFlags.None);
if (!this.Props.spawnForbidden)
{
pawn2.playerSettings.Master = pawn;
pawn2.training.Train(TrainableDefOf.Obedience, pawn, true);
}
i++;
continue;
}
if (PawnUtility.ShouldSendNotificationAbout(pawn) || PawnUtility.ShouldSendNotificationAbout(pawn))
{
Messages.Message(this.Props.spawnVerb.Translate(pawn.Named("PAWN")), pawn, MessageTypeDefOf.PositiveEvent, true);
}
return true;
}
else
{
IntVec3 intVec2;
if (!this.TryFindSpawnCell(out intVec2))
{
return false;
}
Thing thing = ThingMaker.MakeThing(this.Props.thingToSpawn, null);
if (thing == null)
{
return false;
}
thing.stackCount = this.calculatedQuantity;
if (this.Props.spawnForbidden)
{
thing.SetForbidden(true, true);
}
GenPlace.TryPlaceThing(thing, intVec2, pawn.Map, ThingPlaceMode.Direct, null, null, default(Rot4));
if (PawnUtility.ShouldSendNotificationAbout(pawn))
{
Messages.Message(this.Props.spawnVerb.Translate(pawn.Named("PAWN"), thing.Named("THING")), thing, MessageTypeDefOf.PositiveEvent, true);
}
return true;
}
}
private bool TryFindSpawnCell(out IntVec3 result)
{
result = IntVec3.Invalid;
bool result2;
if (this.pawn == null)
{
result2 = false;
}
else
{
Map map = this.pawn.Map;
if (map == null)
{
result2 = false;
}
else
{
result = CellFinder.RandomClosewalkCellNear(this.pawn.Position, map, 5, null);
result2 = true;
}
}
return result2;
}
private void ResetCountdown()
{
this.ticksUntilSpawn = (int)(this.RandomDays2wait() * 60000f);
this.initialTicksUntilSpawn = this.ticksUntilSpawn;
}
private float RandomDays2wait()
{
return Rand.Range(this.calculatedMinDaysB4Next, this.calculatedMaxDaysB4Next);
}
private float RandomGraceDays()
{
return Rand.Range(this.Props.graceDays / 2f, this.Props.graceDays);
}
public override string CompTipStringExtra
{
get
{
if (!this.myDebug)
{
return null;
}
string text = "ticksUntilSpawn: " + this.ticksUntilSpawn.ToString() + "\n";
string text2 = text;
text = string.Concat(new string[]
{
text2,
"initialTicksUntilSpawn: ",
this.initialTicksUntilSpawn.ToString(),
"\n"
});
text2 = text;
text = string.Concat(new string[]
{
text2,
"graceTicks: ",
this.graceTicks.ToString(),
"\n"
});
text2 = text;
text = string.Concat(new string[]
{
text2,
"hunger resets: ",
this.hungerReset.ToString(),
"\n"
});
text2 = text;
text = string.Concat(new string[]
{
text2,
"health resets: ",
this.healthReset.ToString(),
"\n"
});
text2 = text;
text = string.Concat(new string[]
{
text2,
"calculatedMinDaysB4Next: ",
this.calculatedMinDaysB4Next.ToString(),
"\n"
});
text2 = text;
text = string.Concat(new string[]
{
text2,
"calculatedMaxDaysB4Next: ",
this.calculatedMaxDaysB4Next.ToString(),
"\n"
});
text2 = text;
text = string.Concat(new string[]
{
text2,
"calculatedQuantity: ",
this.calculatedQuantity.ToString(),
"\n"
});
return text + "blockSpawn: " + this.blockSpawn.ToString();
}
}
private int ticksUntilSpawn;
private int initialTicksUntilSpawn;
private int hungerReset;
private int healthReset;
private int graceTicks;
private Pawn pawn;
private float calculatedMaxDaysB4Next = 2f;
private float calculatedMinDaysB4Next = 1f;
private int calculatedQuantity = 1;
private bool blockSpawn;
private bool myDebug;
private readonly float errorMinDaysB4Next = 0.001f;
private readonly int errorExponentialLimit = 20;
private readonly int errorSpawnCount = 750;
}
}

View File

@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using RimWorld;
using UnityEngine;
using Verse;
namespace WulaFallenEmpire
{
public static class Tools
{
public static void DestroyParentHediff(Hediff parentHediff, bool debug = false)
{
if (parentHediff.pawn != null && parentHediff.def.defName != null && debug)
{
Log.Warning(parentHediff.pawn.Label + "'s Hediff: " + parentHediff.def.defName + " says goodbye.");
}
parentHediff.Severity = 0f;
}
public static float GetPawnAgeOverlifeExpectancyRatio(Pawn pawn, bool debug = false)
{
float result = 1f;
if (pawn == null)
{
if (debug)
{
Log.Warning("GetPawnAgeOverlifeExpectancyRatio pawn NOT OK");
}
return result;
}
result = pawn.ageTracker.AgeBiologicalYearsFloat / pawn.RaceProps.lifeExpectancy;
if (debug)
{
Log.Warning(string.Concat(new string[]
{
pawn.Label,
" Age: ",
pawn.ageTracker.AgeBiologicalYearsFloat.ToString(),
"; lifeExpectancy: ",
pawn.RaceProps.lifeExpectancy.ToString(),
"; ratio:",
result.ToString()
}));
}
return result;
}
public static bool IsInjured(this Pawn pawn, bool debug = false)
{
if (pawn == null)
{
if (debug)
{
Log.Warning("pawn is null - wounded ");
}
return false;
}
float num = 0f;
List<Hediff> hediffs = pawn.health.hediffSet.hediffs;
for (int i = 0; i < hediffs.Count; i++)
{
if (hediffs[i] is Hediff_Injury && !hediffs[i].IsPermanent())
{
num += hediffs[i].Severity;
}
}
if (debug && num > 0f)
{
Log.Warning(pawn.Label + " is wounded ");
}
return num > 0f;
}
public static bool IsHungry(this Pawn pawn, bool debug = false)
{
if (pawn == null)
{
if (debug)
{
Log.Warning("pawn is null - IsHungry ");
}
return false;
}
bool flag = pawn.needs.food != null && pawn.needs.food.CurCategory == HungerCategory.Starving;
if (debug && flag)
{
Log.Warning(pawn.Label + " is hungry ");
}
return flag;
}
public static bool OkPawn(Pawn pawn)
{
return pawn != null && pawn.Map != null;
}
public static void Warn(string warning, bool debug = false)
{
if (debug)
{
Log.Warning(warning);
}
}
}
}