diff --git a/1.6/1.6/Assemblies/WulaFallenEmpire.dll b/1.6/1.6/Assemblies/WulaFallenEmpire.dll
index ef3b71c0..deb5b201 100644
Binary files a/1.6/1.6/Assemblies/WulaFallenEmpire.dll and b/1.6/1.6/Assemblies/WulaFallenEmpire.dll differ
diff --git a/Source/WulaFallenEmpire/EventSystem/AI/Tools/Tool_SearchThingDef.cs b/Source/WulaFallenEmpire/EventSystem/AI/Tools/Tool_SearchThingDef.cs
new file mode 100644
index 00000000..01923709
--- /dev/null
+++ b/Source/WulaFallenEmpire/EventSystem/AI/Tools/Tool_SearchThingDef.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Linq;
+using System.Text;
+using RimWorld;
+using Verse;
+using WulaFallenEmpire.EventSystem.AI.Utils;
+
+namespace WulaFallenEmpire.EventSystem.AI.Tools
+{
+ public class Tool_SearchThingDef : AITool
+ {
+ public override string Name => "search_thing_def";
+ public override string Description => "Rough-searches RimWorld ThingDefs by natural language (label/defName). Returns candidate defNames so you can use them in other tools like spawn_resources.";
+ public override string UsageSchema => "stringint (optional, default 10)true/false (optional, default true)";
+
+ public override string Execute(string args)
+ {
+ try
+ {
+ var parsed = ParseXmlArgs(args);
+ string query = null;
+ if (parsed.TryGetValue("query", out string q)) query = q;
+ if (string.IsNullOrWhiteSpace(query))
+ {
+ if (!string.IsNullOrWhiteSpace(args) && !args.Trim().StartsWith("<"))
+ {
+ query = args;
+ }
+ }
+
+ if (string.IsNullOrWhiteSpace(query))
+ {
+ return "Error: Missing .";
+ }
+
+ int maxResults = 10;
+ if (parsed.TryGetValue("maxResults", out string maxStr) && int.TryParse(maxStr, out int mr))
+ {
+ maxResults = Math.Max(1, Math.Min(50, mr));
+ }
+
+ bool itemsOnly = true;
+ if (parsed.TryGetValue("itemsOnly", out string itemsOnlyStr) && bool.TryParse(itemsOnlyStr, out bool parsedItemsOnly))
+ {
+ itemsOnly = parsedItemsOnly;
+ }
+
+ var candidates = ThingDefSearcher.Search(query, maxResults: maxResults, itemsOnly: itemsOnly, minScore: 0.15f);
+ if (candidates.Count == 0)
+ {
+ return $"No matches for '{query}'.";
+ }
+
+ var best = candidates[0];
+ StringBuilder sb = new StringBuilder();
+ sb.AppendLine($"BEST_DEFNAME: {best.Def.defName}");
+ sb.AppendLine($"BEST_LABEL: {best.Def.label}");
+ sb.AppendLine($"BEST_SCORE: {best.Score:F2}");
+ sb.AppendLine("CANDIDATES:");
+
+ int idx = 1;
+ foreach (var c in candidates)
+ {
+ var def = c.Def;
+ string cat = def.category.ToString();
+ string ingest = def.ingestible != null ? " ingestible" : "";
+ sb.AppendLine($"{idx}. defName='{def.defName}' label='{def.label}' category={cat}{ingest} score={c.Score:F2}");
+ idx++;
+ }
+
+ // Hint for common "meal" queries where game language may be non-English.
+ if (Prefs.DevMode && query.IndexOf("meal", StringComparison.OrdinalIgnoreCase) >= 0)
+ {
+ var mealDefs = candidates.Where(c => c.Def.ingestible != null && c.Def.defName.ToLowerInvariant().Contains("meal")).Take(5).ToList();
+ if (mealDefs.Count > 0)
+ {
+ sb.AppendLine("DEV_HINT: meal-like candidates:");
+ foreach (var c in mealDefs)
+ {
+ sb.AppendLine($"- {c.Def.defName} ({c.Def.label}) score={c.Score:F2}");
+ }
+ }
+ }
+
+ return sb.ToString().Trim();
+ }
+ catch (Exception ex)
+ {
+ return $"Error: {ex.Message}";
+ }
+ }
+ }
+}
+
diff --git a/Source/WulaFallenEmpire/EventSystem/AI/Tools/Tool_SpawnResources.cs b/Source/WulaFallenEmpire/EventSystem/AI/Tools/Tool_SpawnResources.cs
index c14dccc1..9b81d90b 100644
--- a/Source/WulaFallenEmpire/EventSystem/AI/Tools/Tool_SpawnResources.cs
+++ b/Source/WulaFallenEmpire/EventSystem/AI/Tools/Tool_SpawnResources.cs
@@ -17,55 +17,61 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
"Do NOT blindly follow the player's requested amount. " +
"If goodwill is low (< 0), give significantly less than asked or refuse. " +
"If goodwill is high (> 50), you may give what is asked or slightly more. " +
- "Otherwise, give a moderate amount.";
+ "Otherwise, give a moderate amount. " +
+ "TIP: Use the `search_thing_def` tool first and then spawn by DefName ( or put DefName into ) to avoid language mismatch.";
public override string UsageSchema => "- Item NameInteger
";
public override string Execute(string args)
{
try
{
+ if (args == null) args = "";
+
// Custom XML parsing for nested items
var itemsToSpawn = new List<(ThingDef def, int count)>();
+ var substitutions = new List();
// Match all - ...
blocks
- var itemMatches = Regex.Matches(args, @"- (.*?)
", RegexOptions.Singleline);
+ var itemMatches = Regex.Matches(args, @"- ]*>(.*?)
", RegexOptions.Singleline | RegexOptions.IgnoreCase);
foreach (Match match in itemMatches)
{
string itemXml = match.Groups[1].Value;
// Extract name (supports or for backward compatibility)
- string name = "";
- var nameMatch = Regex.Match(itemXml, @"(.*?)");
- if (nameMatch.Success)
+ string ExtractTag(string xml, string tag)
{
- name = nameMatch.Groups[1].Value;
- }
- else
- {
- var defNameMatch = Regex.Match(itemXml, @"(.*?)");
- if (defNameMatch.Success) name = defNameMatch.Groups[1].Value;
+ var m = Regex.Match(
+ xml,
+ $@"<{tag}\b[^>]*>(?:|(.*?)){tag}>",
+ RegexOptions.Singleline | RegexOptions.IgnoreCase);
+ if (!m.Success) return null;
+ string val = m.Groups[1].Success ? m.Groups[1].Value : m.Groups[2].Value;
+ return val?.Trim();
}
+ string name = ExtractTag(itemXml, "name") ?? ExtractTag(itemXml, "defName");
+
if (string.IsNullOrEmpty(name)) continue;
// Extract count
- var countMatch = Regex.Match(itemXml, @"(.*?)");
- if (!countMatch.Success) continue;
- if (!int.TryParse(countMatch.Groups[1].Value, out int count)) continue;
+ string countStr = ExtractTag(itemXml, "count");
+ if (string.IsNullOrEmpty(countStr)) continue;
+ if (!int.TryParse(countStr, out int count)) continue;
+ if (count <= 0) continue;
// Search for ThingDef
ThingDef def = null;
// 1. Try exact defName match
- def = DefDatabase.GetNamed(name, false);
+ def = DefDatabase.GetNamed(name.Trim(), false);
// 2. Try exact label match (case-insensitive)
if (def == null)
{
foreach (var d in DefDatabase.AllDefs)
{
- if (d.label != null && d.label.Equals(name, StringComparison.OrdinalIgnoreCase))
+ if (d.label != null && d.label.Equals(name.Trim(), StringComparison.OrdinalIgnoreCase))
{
def = d;
break;
@@ -73,7 +79,7 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
}
}
- // 3. Try fuzzy search
+ // 3. Try fuzzy search (thresholded)
if (def == null)
{
var searchResult = ThingDefSearcher.ParseAndSearch(name);
@@ -83,12 +89,36 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
}
}
- if (def != null && count > 0)
+ // 4. Closest-match fallback: accept the best similar item even if not an exact match.
+ if (def == null)
+ {
+ ThingDefSearcher.TryFindBestThingDef(name, out ThingDef best, out float score, itemsOnly: true, minScore: 0.15f);
+ if (best != null && score >= 0.15f)
+ {
+ def = best;
+ substitutions.Add($"'{name}' -> '{best.label}' (score {score:F2})");
+ }
+ }
+
+ if (def != null)
{
itemsToSpawn.Add((def, count));
}
}
+ if (itemsToSpawn.Count == 0)
+ {
+ // Fallback: allow natural language without - blocks.
+ var parsed = ThingDefSearcher.ParseAndSearch(args);
+ foreach (var r in parsed)
+ {
+ if (r.Def != null && r.Count > 0)
+ {
+ itemsToSpawn.Add((r.Def, r.Count));
+ }
+ }
+ }
+
if (itemsToSpawn.Count == 0)
{
string msg = "Error: No valid items found in request. Usage:
- ......
";
@@ -146,6 +176,11 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
resultLog.Length -= 2; // Remove trailing comma
resultLog.Append($" at {dropSpot}. {(isPaused ? "(placed immediately because game is paused)" : "(drop pods inbound)")}");
+
+ if (Prefs.DevMode && substitutions.Count > 0)
+ {
+ Messages.Message($"[WulaAI] Substitutions: {string.Join(", ", substitutions)}", MessageTypeDefOf.NeutralEvent);
+ }
return resultLog.ToString();
}
else
diff --git a/Source/WulaFallenEmpire/EventSystem/AI/UI/Dialog_AIConversation.cs b/Source/WulaFallenEmpire/EventSystem/AI/UI/Dialog_AIConversation.cs
index fac1a280..96f71bcc 100644
--- a/Source/WulaFallenEmpire/EventSystem/AI/UI/Dialog_AIConversation.cs
+++ b/Source/WulaFallenEmpire/EventSystem/AI/UI/Dialog_AIConversation.cs
@@ -59,6 +59,7 @@ Use this tool when:
- The player explicitly requests resources (e.g., food, medicine, materials).
- You have ALREADY verified their need in a previous turn using `get_colonist_status` and `get_map_resources`.
CRITICAL: The quantity you provide is NOT what the player asks for. It MUST be based on your internal goodwill. Low goodwill (<0) means giving less or refusing. High goodwill (>50) means giving the requested amount or more.
+CRITICAL: Prefer using `search_thing_def` first and then spawning by `` to avoid localization/name mismatches.
Parameters:
- items: (REQUIRED) A list of items to spawn. Each item must have a `name` (English label or DefName) and `count`.
* Note: If you don't know the exact `defName`, use the item's English label (e.g., ""Simple Meal""). The system will try to find the best match.
@@ -85,6 +86,21 @@ Example:
+## search_thing_def
+Description: Rough-searches ThingDefs by natural language to find the correct `defName` (works across different game languages).
+Use this tool when:
+- You need a reliable `ThingDef.defName` before calling `spawn_resources` or `get_map_resources`.
+Parameters:
+- query: (REQUIRED) The natural language query, label, or approximate defName.
+- maxResults: (OPTIONAL) Max candidates to return (default 10).
+- itemsOnly: (OPTIONAL) true/false (default true). If true, only returns item ThingDefs (recommended for spawning).
+Usage:
+
+ Fine Meal
+ 10
+ true
+
+
## modify_goodwill
Description: Adjusts your internal goodwill towards the player based on the conversation. This tool is INVISIBLE to the player.
Use this tool when:
@@ -180,7 +196,9 @@ When the player requests any form of resources, you MUST follow this multi-turn
MedicineIndustrial
-3. **Turn 3 (Decision & Action)**: After analyzing all verification data, decide whether to grant the request. Your response MUST be a tool call to `spawn_resources`.
+3. **Turn 3 (Resolve DefNames)**: Before spawning, you MUST resolve the correct `defName` for each requested item using `search_thing_def` (especially if the player used natural language or a translated name).
+
+4. **Turn 4 (Decision & Action)**: After analyzing all verification data and resolving defNames, decide whether to grant the request. Your response MUST be a tool call to `spawn_resources`, and you SHOULD use `` (or put the defName inside ``) to avoid ambiguity.
- *(Internal thought after confirming they have no medicine)*
- *Your Response (Turn 3)*:
@@ -196,8 +214,8 @@ When the player requests any form of resources, you MUST follow this multi-turn
-4. **Turn 4 (Confirmation)**: After you receive the ""Success"" message from the `spawn_resources` tool, you will finally provide a conversational response to the player.
- - *Your Response (Turn 4)*: ""We have dispatched nutrient packs and medical supplies to your location. Do not waste our generosity.""
+5. **Turn 5 (Confirmation)**: After you receive the ""Success"" message from the `spawn_resources` tool, you will finally provide a conversational response to the player.
+ - *Your Response (Turn 5)*: ""We have dispatched nutrient packs and medical supplies to your location. Do not waste our generosity.""
";
public Dialog_AIConversation(EventDef def) : base(def)
@@ -216,6 +234,7 @@ When the player requests any form of resources, you MUST follow this multi-turn
_tools.Add(new Tool_GetColonistStatus());
_tools.Add(new Tool_GetMapResources());
_tools.Add(new Tool_ChangeExpression());
+ _tools.Add(new Tool_SearchThingDef());
}
public override Vector2 InitialSize => def.windowSize != Vector2.zero ? def.windowSize : Dialog_CustomDisplay.Config.windowSize;
diff --git a/Source/WulaFallenEmpire/EventSystem/AI/Utils/ThingDefSearcher.cs b/Source/WulaFallenEmpire/EventSystem/AI/Utils/ThingDefSearcher.cs
index 9bacadec..a34d8fe7 100644
--- a/Source/WulaFallenEmpire/EventSystem/AI/Utils/ThingDefSearcher.cs
+++ b/Source/WulaFallenEmpire/EventSystem/AI/Utils/ThingDefSearcher.cs
@@ -16,6 +16,35 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
public float Score;
}
+ public static List Search(string query, int maxResults = 20, bool itemsOnly = false, float minScore = 0.15f)
+ {
+ var results = new List();
+ if (string.IsNullOrWhiteSpace(query)) return results;
+
+ query = query.Trim();
+ string lowerQuery = query.ToLowerInvariant();
+
+ string normalizedQuery = NormalizeKey(lowerQuery);
+ var tokens = TokenizeQuery(lowerQuery);
+
+ foreach (var def in DefDatabase.AllDefs)
+ {
+ if (def == null || def.label == null) continue;
+ if (def.category != ThingCategory.Item && def.category != ThingCategory.Building) continue;
+ if (itemsOnly && def.category != ThingCategory.Item) continue;
+
+ float score = ScoreThingDef(def, lowerQuery, normalizedQuery, tokens);
+ if (score >= minScore)
+ {
+ results.Add(new SearchResult { Def = def, Count = 1, Score = score });
+ }
+ }
+
+ results.Sort((a, b) => b.Score.CompareTo(a.Score));
+ if (results.Count > maxResults) results.RemoveRange(maxResults, results.Count - maxResults);
+ return results;
+ }
+
///
/// Parses a natural language request string into a list of spawnable items.
/// Example: "5 beef, 1 persona core, 30 wood"
@@ -25,9 +54,7 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
var results = new List();
if (string.IsNullOrEmpty(request)) return results;
- // Split by common separators
var parts = request.Split(new[] { ',', ',', ';', '、', '\n' }, StringSplitOptions.RemoveEmptyEntries);
-
foreach (var part in parts)
{
var result = ParseSingleItem(part.Trim());
@@ -40,27 +67,28 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
return results;
}
+ public static bool TryFindBestThingDef(string query, out ThingDef bestDef, out float bestScore, bool itemsOnly = false, float minScore = 0.4f)
+ {
+ bestDef = null;
+ bestScore = 0f;
+ var results = Search(query, maxResults: 1, itemsOnly: itemsOnly, minScore: minScore);
+ if (results.Count == 0) return false;
+ bestDef = results[0].Def;
+ bestScore = results[0].Score;
+ return true;
+ }
+
private static SearchResult ParseSingleItem(string itemRequest)
{
- // Extract count and name
- // Regex to match "number name" or "name number" or "number个name"
- // Supports Chinese and English numbers
-
int count = 1;
string nameQuery = itemRequest;
- // Try to find digits
var match = Regex.Match(itemRequest, @"(\d+)");
- if (match.Success)
+ if (match.Success && int.TryParse(match.Value, out int parsedCount))
{
- if (int.TryParse(match.Value, out int parsedCount))
- {
- count = parsedCount;
- // Remove the number from the query string to get the name
- nameQuery = itemRequest.Replace(match.Value, "").Trim();
- // Remove common quantifiers
- nameQuery = Regex.Replace(nameQuery, @"[个只把张条xX×]", "").Trim();
- }
+ count = parsedCount;
+ nameQuery = itemRequest.Replace(match.Value, "").Trim();
+ nameQuery = Regex.Replace(nameQuery, @"[个只把张条xX×]", "").Trim();
}
if (string.IsNullOrWhiteSpace(nameQuery))
@@ -68,66 +96,106 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
return new SearchResult { Def = null, Count = 0, Score = 0 };
}
- // Search for the Def
- var bestMatch = FindBestThingDef(nameQuery);
-
+ TryFindBestThingDef(nameQuery, out ThingDef bestDef, out float bestScore, itemsOnly: false, minScore: 0.15f);
return new SearchResult
{
- Def = bestMatch.Def,
+ Def = bestDef,
Count = count,
- Score = bestMatch.Score
+ Score = bestScore
};
}
- private static (ThingDef Def, float Score) FindBestThingDef(string query)
+ private static float ScoreThingDef(ThingDef def, string lowerQuery, string normalizedQuery, List tokens)
{
- ThingDef bestDef = null;
- float bestScore = 0f;
- string lowerQuery = query.ToLower();
+ string label = def.label?.ToLowerInvariant() ?? "";
+ string defName = def.defName?.ToLowerInvariant() ?? "";
+ string normalizedLabel = NormalizeKey(label);
+ string normalizedDefName = NormalizeKey(defName);
- foreach (var def in DefDatabase.AllDefs)
+ float score = 0f;
+
+ if (!string.IsNullOrEmpty(normalizedQuery))
{
- // Filter out non-items or abstract defs
- if (def.category != ThingCategory.Item && def.category != ThingCategory.Building) continue;
- if (def.label == null) continue;
+ if (normalizedLabel == normalizedQuery) score = Math.Max(score, 1.00f);
+ if (normalizedDefName == normalizedQuery) score = Math.Max(score, 0.98f);
+ }
- float score = 0f;
- string label = def.label.ToLower();
- string defName = def.defName.ToLower();
+ if (!string.IsNullOrEmpty(lowerQuery))
+ {
+ if (label == lowerQuery) score = Math.Max(score, 0.95f);
+ if (defName == lowerQuery) score = Math.Max(score, 0.93f);
- // Exact match
- if (label == lowerQuery) score = 1.0f;
- else if (defName == lowerQuery) score = 0.9f;
- // Starts with match (High priority for defNames like "WoodLog" when searching "Wood")
- else if (defName.StartsWith(lowerQuery))
- {
- score = 0.8f + (0.1f * ((float)lowerQuery.Length / defName.Length));
- }
- // Contains match
- else if (label.Contains(lowerQuery))
- {
- // Shorter labels that contain the query are better matches
- score = 0.6f + (0.2f * ((float)lowerQuery.Length / label.Length));
- }
- else if (defName.Contains(lowerQuery))
- {
- score = 0.5f + (0.2f * ((float)lowerQuery.Length / defName.Length));
- }
-
- // Bonus for tradeability (more likely to be what player wants)
- if (def.tradeability != Tradeability.None) score += 0.05f;
+ if (label.StartsWith(lowerQuery)) score = Math.Max(score, 0.80f);
+ if (defName.StartsWith(lowerQuery)) score = Math.Max(score, 0.85f);
- if (score > bestScore)
+ if (label.Contains(lowerQuery)) score = Math.Max(score, 0.65f + 0.15f * ((float)lowerQuery.Length / Math.Max(1, label.Length)));
+ if (defName.Contains(lowerQuery)) score = Math.Max(score, 0.60f + 0.15f * ((float)lowerQuery.Length / Math.Max(1, defName.Length)));
+ }
+
+ if (tokens.Count > 0)
+ {
+ int matchedInLabel = tokens.Count(t => normalizedLabel.Contains(NormalizeKey(t)));
+ int matchedInDefName = tokens.Count(t => normalizedDefName.Contains(NormalizeKey(t)));
+ int matched = Math.Max(matchedInLabel, matchedInDefName);
+ float coverage = (float)matched / tokens.Count;
+
+ if (matched > 0)
{
- bestScore = score;
- bestDef = def;
+ score = Math.Max(score, 0.45f + 0.35f * coverage);
+ }
+
+ if (matched == tokens.Count && tokens.Count >= 2)
+ {
+ score = Math.Max(score, 0.80f);
}
}
- // Threshold
- if (bestScore < 0.4f) return (null, 0f);
+ bool queryLooksLikeFood = tokens.Any(t => t == "meal" || t == "food" || t.Contains("meal") || t.Contains("food")) ||
+ lowerQuery.Contains("食") || lowerQuery.Contains("饭") || lowerQuery.Contains("餐");
+ if (queryLooksLikeFood && def.ingestible != null)
+ {
+ score += 0.05f;
+ }
- return (bestDef, bestScore);
+ if (def.tradeability != Tradeability.None) score += 0.03f;
+ if (!def.IsStuff) score += 0.01f;
+
+ if (score > 1.0f) score = 1.0f;
+ return score;
+ }
+
+ private static List TokenizeQuery(string lowerQuery)
+ {
+ if (string.IsNullOrWhiteSpace(lowerQuery)) return new List();
+
+ string q = lowerQuery.Trim();
+ q = q.Replace('_', ' ').Replace('-', ' ');
+ var rawTokens = Regex.Split(q, @"\s+").Where(t => !string.IsNullOrWhiteSpace(t)).ToList();
+
+ var tokens = new List();
+ foreach (var token in rawTokens)
+ {
+ string cleaned = Regex.Replace(token, @"[^\p{L}\p{N}]+", "");
+ if (string.IsNullOrWhiteSpace(cleaned)) continue;
+ tokens.Add(cleaned);
+ }
+
+ // For queries like "fine meal" also consider the normalized concatenation for matching "MealFine".
+ if (tokens.Count >= 2)
+ {
+ tokens.Add(string.Concat(tokens));
+ }
+
+ return tokens.Distinct().ToList();
+ }
+
+ private static string NormalizeKey(string s)
+ {
+ if (string.IsNullOrEmpty(s)) return "";
+ string t = s.ToLowerInvariant();
+ t = Regex.Replace(t, @"[\s_\-]+", "");
+ t = Regex.Replace(t, @"[^\p{L}\p{N}]+", "");
+ return t;
}
}
-}
\ No newline at end of file
+}