This commit is contained in:
2025-12-13 15:43:46 +08:00
parent 6d8e28c05a
commit 35cef08906
3 changed files with 40 additions and 13 deletions

View File

@@ -53,17 +53,33 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
if (!countMatch.Success) continue; if (!countMatch.Success) continue;
if (!int.TryParse(countMatch.Groups[1].Value, out int count)) continue; if (!int.TryParse(countMatch.Groups[1].Value, out int count)) continue;
// Search for ThingDef using fuzzy search // Search for ThingDef
ThingDef def = null; ThingDef def = null;
var searchResult = ThingDefSearcher.ParseAndSearch(name);
if (searchResult.Count > 0) // 1. Try exact defName match
def = DefDatabase<ThingDef>.GetNamed(name, false);
// 2. Try exact label match (case-insensitive)
if (def == null)
{ {
def = searchResult[0].Def; foreach (var d in DefDatabase<ThingDef>.AllDefs)
{
if (d.label != null && d.label.Equals(name, StringComparison.OrdinalIgnoreCase))
{
def = d;
break;
}
}
} }
else
// 3. Try fuzzy search
if (def == null)
{ {
// Fallback: try exact defName match just in case var searchResult = ThingDefSearcher.ParseAndSearch(name);
def = DefDatabase<ThingDef>.GetNamed(name, false); if (searchResult.Count > 0)
{
def = searchResult[0].Def;
}
} }
if (def != null && count > 0) if (def != null && count > 0)
@@ -103,7 +119,7 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
Faction faction = Find.FactionManager.FirstFactionOfDef(FactionDef.Named("Wula_PIA_Legion_Faction")); Faction faction = Find.FactionManager.FirstFactionOfDef(FactionDef.Named("Wula_PIA_Legion_Faction"));
if (faction != null) if (faction != null)
{ {
Messages.Message("Wula_ResourceDrop".Translate(faction.Name.Named("FACTION_name")), new LookTargets(dropSpot, map), MessageTypeDefOf.PositiveEvent); Messages.Message("Wula_ResourceDrop".Translate(faction.def.defName.Named("FACTION_name")), new LookTargets(dropSpot, map), MessageTypeDefOf.PositiveEvent);
} }
resultLog.Length -= 2; // Remove trailing comma resultLog.Length -= 2; // Remove trailing comma

View File

@@ -27,7 +27,7 @@ namespace WulaFallenEmpire.EventSystem.AI.UI
public static Dialog_AIConversation Instance { get; private set; } public static Dialog_AIConversation Instance { get; private set; }
// Debug field to track current portrait ID // Debug field to track current portrait ID
private int _currentPortraitId = 2; private int _currentPortraitId = 0;
// Default Persona (used if XML doesn't provide one) // Default Persona (used if XML doesn't provide one)
private const string DefaultPersona = @" private const string DefaultPersona = @"
@@ -242,7 +242,18 @@ When the player requests any form of resources, you MUST follow this multi-turn
Log.Warning($"[WulaAI] Failed to load portrait: {path}"); Log.Warning($"[WulaAI] Failed to load portrait: {path}");
} }
} }
if (_portraits.ContainsKey(2))
// Use portraitPath from def as the initial portrait
if (this.portrait != null)
{
// Find the ID of the initial portrait
var initial = _portraits.FirstOrDefault(kvp => kvp.Value == this.portrait);
if (initial.Key != 0)
{
_currentPortraitId = initial.Key;
}
}
else if (_portraits.ContainsKey(2)) // Fallback to 2 if def has no portrait
{ {
this.portrait = _portraits[2]; this.portrait = _portraits[2];
_currentPortraitId = 2; _currentPortraitId = 2;
@@ -337,7 +348,7 @@ When the player requests any form of resources, you MUST follow this multi-turn
// REWRITTEN: Check for XML tool call format // REWRITTEN: Check for XML tool call format
// Use regex to detect if the response contains any XML tags // Use regex to detect if the response contains any XML tags
if (Regex.IsMatch(response, @"<[a-zA-Z0-9_]+(?:>.*?</\1>|/>)", RegexOptions.Singleline)) if (Regex.IsMatch(response, @"<([a-zA-Z0-9_]+)(?:>.*?</\1>|/>)", RegexOptions.Singleline))
{ {
await HandleXmlToolUsage(response); await HandleXmlToolUsage(response);
} }
@@ -427,7 +438,7 @@ When the player requests any form of resources, you MUST follow this multi-turn
_history.Add(("tool", combinedResults.ToString().Trim())); _history.Add(("tool", combinedResults.ToString().Trim()));
// Check if there is any text content in the response // Check if there is any text content in the response
string textContent = Regex.Replace(xml, @"<[a-zA-Z0-9_]+(?:>.*?</\1>|/>)", "", RegexOptions.Singleline).Trim(); string textContent = Regex.Replace(xml, @"<([a-zA-Z0-9_]+)(?:>.*?</\1>|/>)", "", RegexOptions.Singleline).Trim();
if (!string.IsNullOrEmpty(textContent)) if (!string.IsNullOrEmpty(textContent))
{ {
@@ -599,7 +610,7 @@ When the player requests any form of resources, you MUST follow this multi-turn
text = Regex.Replace(text, @"<([a-zA-Z0-9_]+)[^>]*>.*?</\1>", "", RegexOptions.Singleline); text = Regex.Replace(text, @"<([a-zA-Z0-9_]+)[^>]*>.*?</\1>", "", RegexOptions.Singleline);
// Remove self-closing tags: <tag/> // Remove self-closing tags: <tag/>
text = Regex.Replace(text, @"<[a-zA-Z0-9_]+[^>]*/>", ""); text = Regex.Replace(text, @"<([a-zA-Z0-9_]+)[^>]*/>", "");
text = text.Trim(); text = text.Trim();