zc
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using RimWorld;
|
||||
using Verse;
|
||||
using WulaFallenEmpire.EventSystem.AI.Utils;
|
||||
@@ -10,46 +11,70 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
||||
public class Tool_SpawnResources : AITool
|
||||
{
|
||||
public override string Name => "spawn_resources";
|
||||
public override string Description => "Spawns resources via drop pod. Accepts a natural language description of items and quantities (e.g., '5 beef, 10 medicine'). " +
|
||||
public override string Description => "Spawns resources via drop pod. " +
|
||||
"IMPORTANT: You MUST decide the quantity based on your goodwill and mood. " +
|
||||
"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.";
|
||||
public override string UsageSchema => "<spawn_resources><request>string describing items</request></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)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parsedArgs = ParseXmlArgs(args);
|
||||
string request = "";
|
||||
// Custom XML parsing for nested items
|
||||
var itemsToSpawn = new List<(ThingDef def, int count)>();
|
||||
|
||||
if (parsedArgs.TryGetValue("request", out string req))
|
||||
// Match all <item>...</item> blocks
|
||||
var itemMatches = Regex.Matches(args, @"<item>(.*?)</item>", RegexOptions.Singleline);
|
||||
|
||||
foreach (Match match in itemMatches)
|
||||
{
|
||||
request = req;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback: try to treat the whole args as the request if parsing failed or format is weird
|
||||
// But with strict XML, this shouldn't happen often.
|
||||
// Let's just log a warning or return error.
|
||||
// Actually, for robustness, if the args doesn't contain tags, maybe it's raw text?
|
||||
if (!args.Trim().StartsWith("<"))
|
||||
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)
|
||||
{
|
||||
request = args;
|
||||
name = nameMatch.Groups[1].Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
var defNameMatch = Regex.Match(itemXml, @"<defName>(.*?)</defName>");
|
||||
if (defNameMatch.Success) name = defNameMatch.Groups[1].Value;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Search for ThingDef using fuzzy search
|
||||
ThingDef def = null;
|
||||
var searchResult = ThingDefSearcher.ParseAndSearch(name);
|
||||
if (searchResult.Count > 0)
|
||||
{
|
||||
def = searchResult[0].Def;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback: try exact defName match just in case
|
||||
def = DefDatabase<ThingDef>.GetNamed(name, false);
|
||||
}
|
||||
|
||||
if (def != null && count > 0)
|
||||
{
|
||||
itemsToSpawn.Add((def, count));
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(request))
|
||||
if (itemsToSpawn.Count == 0)
|
||||
{
|
||||
return "Error: Empty request. Usage: <spawn_resources><request>...</request></spawn_resources>";
|
||||
}
|
||||
|
||||
var items = ThingDefSearcher.ParseAndSearch(request);
|
||||
if (items.Count == 0)
|
||||
{
|
||||
return $"Error: Could not identify any valid items in request '{request}'.";
|
||||
return "Error: No valid items found in request. Usage: <spawn_resources><items><item><name>...</name><count>...</count></item></items></spawn_resources>";
|
||||
}
|
||||
|
||||
Map map = Find.CurrentMap;
|
||||
@@ -63,12 +88,12 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
||||
StringBuilder resultLog = new StringBuilder();
|
||||
resultLog.Append("Success: Dropped ");
|
||||
|
||||
foreach (var item in items)
|
||||
foreach (var (def, count) in itemsToSpawn)
|
||||
{
|
||||
Thing thing = ThingMaker.MakeThing(item.Def);
|
||||
thing.stackCount = item.Count;
|
||||
Thing thing = ThingMaker.MakeThing(def);
|
||||
thing.stackCount = count;
|
||||
thingsToDrop.Add(thing);
|
||||
resultLog.Append($"{item.Count}x {item.Def.label}, ");
|
||||
resultLog.Append($"{count}x {def.label}, ");
|
||||
}
|
||||
|
||||
if (thingsToDrop.Count > 0)
|
||||
|
||||
Reference in New Issue
Block a user