修改群友提出的问题

This commit is contained in:
Tourswen
2025-10-01 14:44:44 +08:00
parent 8dfec3d83a
commit f76ffb74de
11 changed files with 414 additions and 49 deletions

View File

@@ -2,6 +2,14 @@
"Version": 1,
"WorkspaceRootPath": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{EAE0DB6B-E282-C812-7F5A-6D13E9D24581}|ArachnaeSwarm.csproj|e:\\steamlibrary\\steamapps\\common\\rimworld\\mods\\arachnaeswarm\\source\\arachnaeswarm\\pawn_comps\\ara_compskillexperiencegiver\\compskillexperiencegiver.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{EAE0DB6B-E282-C812-7F5A-6D13E9D24581}|ArachnaeSwarm.csproj|solutionrelative:pawn_comps\\ara_compskillexperiencegiver\\compskillexperiencegiver.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{EAE0DB6B-E282-C812-7F5A-6D13E9D24581}|ArachnaeSwarm.csproj|e:\\steamlibrary\\steamapps\\common\\rimworld\\mods\\arachnaeswarm\\source\\arachnaeswarm\\pawn_comps\\ara_compskillexperiencegiver\\compproperties_skillexperiencegiver.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{EAE0DB6B-E282-C812-7F5A-6D13E9D24581}|ArachnaeSwarm.csproj|solutionrelative:pawn_comps\\ara_compskillexperiencegiver\\compproperties_skillexperiencegiver.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{EAE0DB6B-E282-C812-7F5A-6D13E9D24581}|ArachnaeSwarm.csproj|e:\\steamlibrary\\steamapps\\common\\rimworld\\mods\\arachnaeswarm\\source\\arachnaeswarm\\hediffs\\moharhediffs\\hediffcomp_spawner.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{EAE0DB6B-E282-C812-7F5A-6D13E9D24581}|ArachnaeSwarm.csproj|solutionrelative:hediffs\\moharhediffs\\hediffcomp_spawner.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
@@ -27,19 +35,45 @@
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "CompSkillExperienceGiver.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompSkillExperienceGiver.cs",
"RelativeDocumentMoniker": "Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompSkillExperienceGiver.cs",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompSkillExperienceGiver.cs",
"RelativeToolTip": "Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompSkillExperienceGiver.cs",
"ViewState": "AgIAAEQAAAAAAAAAAAAgwHkAAAAVAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-10-01T06:00:46.388Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "CompProperties_SkillExperienceGiver.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompProperties_SkillExperienceGiver.cs",
"RelativeDocumentMoniker": "Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompProperties_SkillExperienceGiver.cs",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompProperties_SkillExperienceGiver.cs*",
"RelativeToolTip": "Pawn_Comps\\ARA_CompSkillExperienceGiver\\CompProperties_SkillExperienceGiver.cs*",
"ViewState": "AgIAAAAAAAAAAAAAAAAAACAAAAAIAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-10-01T06:00:42.508Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 2,
"Title": "HediffComp_Spawner.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Hediffs\\MoharHediffs\\HediffComp_Spawner.cs",
"RelativeDocumentMoniker": "Hediffs\\MoharHediffs\\HediffComp_Spawner.cs",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Hediffs\\MoharHediffs\\HediffComp_Spawner.cs*",
"RelativeToolTip": "Hediffs\\MoharHediffs\\HediffComp_Spawner.cs*",
"ViewState": "AgIAAKABAAAAAAAAAAAUwLoBAAAUAAAAAAAAAA==",
"ToolTip": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Hediffs\\MoharHediffs\\HediffComp_Spawner.cs",
"RelativeToolTip": "Hediffs\\MoharHediffs\\HediffComp_Spawner.cs",
"ViewState": "AgIAAKABAAAAAAAAAAAUwLkBAAARAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-09-30T23:28:37.585Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"DocumentIndex": 3,
"Title": "StatWorker_IncubationInfo.cs",
"DocumentMoniker": "E:\\SteamLibrary\\steamapps\\common\\RimWorld\\Mods\\ArachnaeSwarm\\Source\\ArachnaeSwarm\\Stat\\StatWorker_IncubationInfo.cs",
"RelativeDocumentMoniker": "Stat\\StatWorker_IncubationInfo.cs",

View File

@@ -170,6 +170,8 @@
<Compile Include="Hediffs\WULA_HediffDamgeShield\DRMDamageShield.cs" />
<Compile Include="Hediffs\WULA_HediffDamgeShield\Hediff_DamageShield.cs" />
<Compile Include="Hediff_DynamicInterceptor.cs" />
<Compile Include="Pawn_Comps\ARA_CompSkillExperienceGiver\CompProperties_SkillExperienceGiver.cs" />
<Compile Include="Pawn_Comps\ARA_CompSkillExperienceGiver\CompSkillExperienceGiver.cs" />
<Compile Include="Stat\StatWorker_IncubationInfo.cs" />
<Compile Include="ThingComp_GuardianPsyField.cs" />
<Compile Include="Harmony_ProjectileInterceptor.cs" />

View File

@@ -0,0 +1,46 @@
using RimWorld;
using System;
using System.Collections.Generic;
using Verse;
namespace ArachnaeSwarm
{
public class CompProperties_SkillExperienceGiver : CompProperties
{
// 技能经验配置列表
public List<SkillExperienceConfig> skillExperiences;
// 全局添加概率0-1之间
public float addChance = 1.0f;
// 调试模式
public bool debug = false;
public CompProperties_SkillExperienceGiver()
{
this.compClass = typeof(CompSkillExperienceGiver);
}
}
// 技能经验配置类
public class SkillExperienceConfig
{
// 技能定义
public SkillDef skillDef;
// 基础经验值(如果使用固定值)
public float experienceAmount = 0f;
// 经验范围(如果使用随机值,优先于固定值)
public FloatRange? experienceRange = null;
// 该技能的经验给予概率0-1之间
public float skillChance = 1.0f;
// 可选:最小技能等级限制(只有达到该等级才会给予经验)
public int minSkillLevel = 0;
// 可选:最大技能等级限制(只有低于该等级才会给予经验)
public int maxSkillLevel = 20;
}
}

View File

@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using Verse;
using RimWorld;
namespace ArachnaeSwarm
{
public class CompSkillExperienceGiver : ThingComp
{
public CompProperties_SkillExperienceGiver Props => (CompProperties_SkillExperienceGiver)this.props;
private int ticksToWait = 5; // 等待5个tick确保完全初始化
private bool experienceGiven = false;
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
// 重置状态
experienceGiven = false;
ticksToWait = 5;
}
public override void CompTick()
{
base.CompTick();
if (!experienceGiven && this.parent is Pawn pawn)
{
if (ticksToWait > 0)
{
ticksToWait--;
return;
}
GiveSkillExperienceToPawn(pawn);
experienceGiven = true;
}
}
private void GiveSkillExperienceToPawn(Pawn pawn)
{
// 安全检查pawn和技能系统
if (pawn == null || pawn.Destroyed || !pawn.Spawned)
return;
if (pawn.skills == null)
return;
// 检查是否有技能经验列表
if (Props.skillExperiences == null || Props.skillExperiences.Count == 0)
return;
// 检查全局概率
if (Props.addChance < 1.0f && Rand.Value > Props.addChance)
return;
// 为每个配置的技能给予经验
foreach (var skillExp in Props.skillExperiences)
{
try
{
// 检查技能定义是否存在
if (skillExp.skillDef == null)
continue;
// 检查技能特定的概率
if (skillExp.skillChance < 1.0f && Rand.Value > skillExp.skillChance)
continue;
// 获取技能记录
SkillRecord skillRecord = pawn.skills.GetSkill(skillExp.skillDef);
// 检查技能是否可用
if (skillRecord == null || skillRecord.TotallyDisabled)
continue;
// 检查技能等级限制
int currentLevel = skillRecord.Level;
if (currentLevel < skillExp.minSkillLevel || currentLevel > skillExp.maxSkillLevel)
continue;
// 给予经验值
float experienceToGive = skillExp.experienceAmount;
// 如果有随机范围,则使用随机值
if (skillExp.experienceRange != null && skillExp.experienceRange.Value.min >= 0)
{
experienceToGive = Rand.Range(skillExp.experienceRange.Value.min, skillExp.experienceRange.Value.max);
}
// 应用经验 - 使用安全的方式
if (experienceToGive > 0)
{
// 直接修改经验值,避免触发绘制相关的计算
skillRecord.xpSinceLastLevel += experienceToGive;
// 检查是否需要升级
int originalLevel = skillRecord.Level;
while (skillRecord.xpSinceLastLevel >= skillRecord.XpRequiredForLevelUp)
{
// 检查是否已达到最大等级
if (skillRecord.Level >= 20) // RimWorld中技能最大等级是20
break;
skillRecord.xpSinceLastLevel -= skillRecord.XpRequiredForLevelUp;
skillRecord.Level++;
}
// 可选:记录日志(调试用)
if (Props.debug)
{
if (skillRecord.Level > originalLevel)
{
Log.Message($"{pawn.LabelShort} gained {experienceToGive} experience in {skillExp.skillDef.label} (Level up: {originalLevel} -> {skillRecord.Level})");
}
else
{
Log.Message($"{pawn.LabelShort} gained {experienceToGive} experience in {skillExp.skillDef.label} (Current level: {skillRecord.Level}, Progress: {skillRecord.xpSinceLastLevel}/{skillRecord.XpRequiredForLevelUp})");
}
}
}
}
catch (Exception ex)
{
// 记录错误但继续处理其他技能
Log.Warning($"Error giving experience to {pawn.LabelShort} for skill {skillExp.skillDef?.defName}: {ex.Message}");
}
}
}
public override void PostExposeData()
{
base.PostExposeData();
Scribe_Values.Look(ref ticksToWait, "ticksToWait", 5);
Scribe_Values.Look(ref experienceGiven, "experienceGiven", false);
}
}
}