zc
This commit is contained in:
Binary file not shown.
@@ -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 => "<search_thing_def><query>string</query><maxResults>int (optional, default 10)</maxResults><itemsOnly>true/false (optional, default true)</itemsOnly></search_thing_def>";
|
||||||
|
|
||||||
|
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 <query>.";
|
||||||
|
}
|
||||||
|
|
||||||
|
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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -17,55 +17,61 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
|||||||
"Do NOT blindly follow the player's requested amount. " +
|
"Do NOT blindly follow the player's requested amount. " +
|
||||||
"If goodwill is low (< 0), give significantly less than asked or refuse. " +
|
"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. " +
|
"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 (<defName> or put DefName into <name>) to avoid language mismatch.";
|
||||||
public override string UsageSchema => "<spawn_resources><items><item><name>Item Name</name><count>Integer</count></item></items></spawn_resources>";
|
public override string UsageSchema => "<spawn_resources><items><item><name>Item Name</name><count>Integer</count></item></items></spawn_resources>";
|
||||||
|
|
||||||
public override string Execute(string args)
|
public override string Execute(string args)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (args == null) args = "";
|
||||||
|
|
||||||
// Custom XML parsing for nested items
|
// Custom XML parsing for nested items
|
||||||
var itemsToSpawn = new List<(ThingDef def, int count)>();
|
var itemsToSpawn = new List<(ThingDef def, int count)>();
|
||||||
|
var substitutions = new List<string>();
|
||||||
|
|
||||||
// Match all <item>...</item> blocks
|
// Match all <item>...</item> blocks
|
||||||
var itemMatches = Regex.Matches(args, @"<item>(.*?)</item>", RegexOptions.Singleline);
|
var itemMatches = Regex.Matches(args, @"<item\b[^>]*>(.*?)</item>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
foreach (Match match in itemMatches)
|
foreach (Match match in itemMatches)
|
||||||
{
|
{
|
||||||
string itemXml = match.Groups[1].Value;
|
string itemXml = match.Groups[1].Value;
|
||||||
|
|
||||||
// Extract name (supports <name> or <defName> for backward compatibility)
|
// Extract name (supports <name> or <defName> for backward compatibility)
|
||||||
string name = "";
|
string ExtractTag(string xml, string tag)
|
||||||
var nameMatch = Regex.Match(itemXml, @"<name>(.*?)</name>");
|
|
||||||
if (nameMatch.Success)
|
|
||||||
{
|
{
|
||||||
name = nameMatch.Groups[1].Value;
|
var m = Regex.Match(
|
||||||
}
|
xml,
|
||||||
else
|
$@"<{tag}\b[^>]*>(?:<!\[CDATA\[(.*?)\]\]>|(.*?))</{tag}>",
|
||||||
{
|
RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
||||||
var defNameMatch = Regex.Match(itemXml, @"<defName>(.*?)</defName>");
|
if (!m.Success) return null;
|
||||||
if (defNameMatch.Success) name = defNameMatch.Groups[1].Value;
|
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;
|
if (string.IsNullOrEmpty(name)) continue;
|
||||||
|
|
||||||
// Extract count
|
// Extract count
|
||||||
var countMatch = Regex.Match(itemXml, @"<count>(.*?)</count>");
|
string countStr = ExtractTag(itemXml, "count");
|
||||||
if (!countMatch.Success) continue;
|
if (string.IsNullOrEmpty(countStr)) continue;
|
||||||
if (!int.TryParse(countMatch.Groups[1].Value, out int count)) continue;
|
if (!int.TryParse(countStr, out int count)) continue;
|
||||||
|
if (count <= 0) continue;
|
||||||
|
|
||||||
// Search for ThingDef
|
// Search for ThingDef
|
||||||
ThingDef def = null;
|
ThingDef def = null;
|
||||||
|
|
||||||
// 1. Try exact defName match
|
// 1. Try exact defName match
|
||||||
def = DefDatabase<ThingDef>.GetNamed(name, false);
|
def = DefDatabase<ThingDef>.GetNamed(name.Trim(), false);
|
||||||
|
|
||||||
// 2. Try exact label match (case-insensitive)
|
// 2. Try exact label match (case-insensitive)
|
||||||
if (def == null)
|
if (def == null)
|
||||||
{
|
{
|
||||||
foreach (var d in DefDatabase<ThingDef>.AllDefs)
|
foreach (var d in DefDatabase<ThingDef>.AllDefs)
|
||||||
{
|
{
|
||||||
if (d.label != null && d.label.Equals(name, StringComparison.OrdinalIgnoreCase))
|
if (d.label != null && d.label.Equals(name.Trim(), StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
def = d;
|
def = d;
|
||||||
break;
|
break;
|
||||||
@@ -73,7 +79,7 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Try fuzzy search
|
// 3. Try fuzzy search (thresholded)
|
||||||
if (def == null)
|
if (def == null)
|
||||||
{
|
{
|
||||||
var searchResult = ThingDefSearcher.ParseAndSearch(name);
|
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));
|
itemsToSpawn.Add((def, count));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (itemsToSpawn.Count == 0)
|
||||||
|
{
|
||||||
|
// Fallback: allow natural language without <item> 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)
|
if (itemsToSpawn.Count == 0)
|
||||||
{
|
{
|
||||||
string msg = "Error: No valid items found in request. Usage: <spawn_resources><items><item><name>...</name><count>...</count></item></items></spawn_resources>";
|
string msg = "Error: No valid items found in request. Usage: <spawn_resources><items><item><name>...</name><count>...</count></item></items></spawn_resources>";
|
||||||
@@ -146,6 +176,11 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
|||||||
|
|
||||||
resultLog.Length -= 2; // Remove trailing comma
|
resultLog.Length -= 2; // Remove trailing comma
|
||||||
resultLog.Append($" at {dropSpot}. {(isPaused ? "(placed immediately because game is paused)" : "(drop pods inbound)")}");
|
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();
|
return resultLog.ToString();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ Use this tool when:
|
|||||||
- The player explicitly requests resources (e.g., food, medicine, materials).
|
- 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`.
|
- 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: 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 `<defName>` to avoid localization/name mismatches.
|
||||||
Parameters:
|
Parameters:
|
||||||
- items: (REQUIRED) A list of items to spawn. Each item must have a `name` (English label or DefName) and `count`.
|
- 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.
|
* 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:
|
|||||||
</items>
|
</items>
|
||||||
</spawn_resources>
|
</spawn_resources>
|
||||||
|
|
||||||
|
## 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:
|
||||||
|
<search_thing_def>
|
||||||
|
<query>Fine Meal</query>
|
||||||
|
<maxResults>10</maxResults>
|
||||||
|
<itemsOnly>true</itemsOnly>
|
||||||
|
</search_thing_def>
|
||||||
|
|
||||||
## modify_goodwill
|
## modify_goodwill
|
||||||
Description: Adjusts your internal goodwill towards the player based on the conversation. This tool is INVISIBLE to the player.
|
Description: Adjusts your internal goodwill towards the player based on the conversation. This tool is INVISIBLE to the player.
|
||||||
Use this tool when:
|
Use this tool when:
|
||||||
@@ -180,7 +196,9 @@ When the player requests any form of resources, you MUST follow this multi-turn
|
|||||||
<resourceName>MedicineIndustrial</resourceName>
|
<resourceName>MedicineIndustrial</resourceName>
|
||||||
</get_map_resources>
|
</get_map_resources>
|
||||||
|
|
||||||
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 `<defName>` (or put the defName inside `<name>`) to avoid ambiguity.
|
||||||
- *(Internal thought after confirming they have no medicine)*
|
- *(Internal thought after confirming they have no medicine)*
|
||||||
- *Your Response (Turn 3)*:
|
- *Your Response (Turn 3)*:
|
||||||
<spawn_resources>
|
<spawn_resources>
|
||||||
@@ -196,8 +214,8 @@ When the player requests any form of resources, you MUST follow this multi-turn
|
|||||||
</items>
|
</items>
|
||||||
</spawn_resources>
|
</spawn_resources>
|
||||||
|
|
||||||
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.
|
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 4)*: ""We have dispatched nutrient packs and medical supplies to your location. Do not waste our generosity.""
|
- *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)
|
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_GetColonistStatus());
|
||||||
_tools.Add(new Tool_GetMapResources());
|
_tools.Add(new Tool_GetMapResources());
|
||||||
_tools.Add(new Tool_ChangeExpression());
|
_tools.Add(new Tool_ChangeExpression());
|
||||||
|
_tools.Add(new Tool_SearchThingDef());
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Vector2 InitialSize => def.windowSize != Vector2.zero ? def.windowSize : Dialog_CustomDisplay.Config.windowSize;
|
public override Vector2 InitialSize => def.windowSize != Vector2.zero ? def.windowSize : Dialog_CustomDisplay.Config.windowSize;
|
||||||
|
|||||||
@@ -16,6 +16,35 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
|
|||||||
public float Score;
|
public float Score;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<SearchResult> Search(string query, int maxResults = 20, bool itemsOnly = false, float minScore = 0.15f)
|
||||||
|
{
|
||||||
|
var results = new List<SearchResult>();
|
||||||
|
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<ThingDef>.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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses a natural language request string into a list of spawnable items.
|
/// Parses a natural language request string into a list of spawnable items.
|
||||||
/// Example: "5 beef, 1 persona core, 30 wood"
|
/// Example: "5 beef, 1 persona core, 30 wood"
|
||||||
@@ -25,9 +54,7 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
|
|||||||
var results = new List<SearchResult>();
|
var results = new List<SearchResult>();
|
||||||
if (string.IsNullOrEmpty(request)) return results;
|
if (string.IsNullOrEmpty(request)) return results;
|
||||||
|
|
||||||
// Split by common separators
|
|
||||||
var parts = request.Split(new[] { ',', ',', ';', '、', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
var parts = request.Split(new[] { ',', ',', ';', '、', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
foreach (var part in parts)
|
foreach (var part in parts)
|
||||||
{
|
{
|
||||||
var result = ParseSingleItem(part.Trim());
|
var result = ParseSingleItem(part.Trim());
|
||||||
@@ -40,27 +67,28 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
|
|||||||
return results;
|
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)
|
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;
|
int count = 1;
|
||||||
string nameQuery = itemRequest;
|
string nameQuery = itemRequest;
|
||||||
|
|
||||||
// Try to find digits
|
|
||||||
var match = Regex.Match(itemRequest, @"(\d+)");
|
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;
|
||||||
{
|
nameQuery = itemRequest.Replace(match.Value, "").Trim();
|
||||||
count = parsedCount;
|
nameQuery = Regex.Replace(nameQuery, @"[个只把张条xX×]", "").Trim();
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(nameQuery))
|
if (string.IsNullOrWhiteSpace(nameQuery))
|
||||||
@@ -68,66 +96,106 @@ namespace WulaFallenEmpire.EventSystem.AI.Utils
|
|||||||
return new SearchResult { Def = null, Count = 0, Score = 0 };
|
return new SearchResult { Def = null, Count = 0, Score = 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for the Def
|
TryFindBestThingDef(nameQuery, out ThingDef bestDef, out float bestScore, itemsOnly: false, minScore: 0.15f);
|
||||||
var bestMatch = FindBestThingDef(nameQuery);
|
|
||||||
|
|
||||||
return new SearchResult
|
return new SearchResult
|
||||||
{
|
{
|
||||||
Def = bestMatch.Def,
|
Def = bestDef,
|
||||||
Count = count,
|
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<string> tokens)
|
||||||
{
|
{
|
||||||
ThingDef bestDef = null;
|
string label = def.label?.ToLowerInvariant() ?? "";
|
||||||
float bestScore = 0f;
|
string defName = def.defName?.ToLowerInvariant() ?? "";
|
||||||
string lowerQuery = query.ToLower();
|
string normalizedLabel = NormalizeKey(label);
|
||||||
|
string normalizedDefName = NormalizeKey(defName);
|
||||||
|
|
||||||
foreach (var def in DefDatabase<ThingDef>.AllDefs)
|
float score = 0f;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(normalizedQuery))
|
||||||
{
|
{
|
||||||
// Filter out non-items or abstract defs
|
if (normalizedLabel == normalizedQuery) score = Math.Max(score, 1.00f);
|
||||||
if (def.category != ThingCategory.Item && def.category != ThingCategory.Building) continue;
|
if (normalizedDefName == normalizedQuery) score = Math.Max(score, 0.98f);
|
||||||
if (def.label == null) continue;
|
}
|
||||||
|
|
||||||
float score = 0f;
|
if (!string.IsNullOrEmpty(lowerQuery))
|
||||||
string label = def.label.ToLower();
|
{
|
||||||
string defName = def.defName.ToLower();
|
if (label == lowerQuery) score = Math.Max(score, 0.95f);
|
||||||
|
if (defName == lowerQuery) score = Math.Max(score, 0.93f);
|
||||||
|
|
||||||
// Exact match
|
if (label.StartsWith(lowerQuery)) score = Math.Max(score, 0.80f);
|
||||||
if (label == lowerQuery) score = 1.0f;
|
if (defName.StartsWith(lowerQuery)) score = Math.Max(score, 0.85f);
|
||||||
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 (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;
|
score = Math.Max(score, 0.45f + 0.35f * coverage);
|
||||||
bestDef = def;
|
}
|
||||||
|
|
||||||
|
if (matched == tokens.Count && tokens.Count >= 2)
|
||||||
|
{
|
||||||
|
score = Math.Max(score, 0.80f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Threshold
|
bool queryLooksLikeFood = tokens.Any(t => t == "meal" || t == "food" || t.Contains("meal") || t.Contains("food")) ||
|
||||||
if (bestScore < 0.4f) return (null, 0f);
|
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<string> TokenizeQuery(string lowerQuery)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(lowerQuery)) return new List<string>();
|
||||||
|
|
||||||
|
string q = lowerQuery.Trim();
|
||||||
|
q = q.Replace('_', ' ').Replace('-', ' ');
|
||||||
|
var rawTokens = Regex.Split(q, @"\s+").Where(t => !string.IsNullOrWhiteSpace(t)).ToList();
|
||||||
|
|
||||||
|
var tokens = new List<string>();
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user