zc
This commit is contained in:
@@ -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. " +
|
||||
"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 (<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 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<string>();
|
||||
|
||||
// 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)
|
||||
{
|
||||
string itemXml = match.Groups[1].Value;
|
||||
|
||||
// Extract name (supports <name> or <defName> for backward compatibility)
|
||||
string name = "";
|
||||
var nameMatch = Regex.Match(itemXml, @"<name>(.*?)</name>");
|
||||
if (nameMatch.Success)
|
||||
string ExtractTag(string xml, string tag)
|
||||
{
|
||||
name = nameMatch.Groups[1].Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
var defNameMatch = Regex.Match(itemXml, @"<defName>(.*?)</defName>");
|
||||
if (defNameMatch.Success) name = defNameMatch.Groups[1].Value;
|
||||
var m = Regex.Match(
|
||||
xml,
|
||||
$@"<{tag}\b[^>]*>(?:<!\[CDATA\[(.*?)\]\]>|(.*?))</{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, @"<count>(.*?)</count>");
|
||||
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<ThingDef>.GetNamed(name, false);
|
||||
def = DefDatabase<ThingDef>.GetNamed(name.Trim(), false);
|
||||
|
||||
// 2. Try exact label match (case-insensitive)
|
||||
if (def == null)
|
||||
{
|
||||
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;
|
||||
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 <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)
|
||||
{
|
||||
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.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
|
||||
|
||||
Reference in New Issue
Block a user