zc
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using RimWorld;
|
||||
using Verse;
|
||||
using WulaFallenEmpire.EventSystem.AI.Utils;
|
||||
|
||||
namespace WulaFallenEmpire.EventSystem.AI.Tools
|
||||
{
|
||||
public class Tool_SearchPawnKind : AITool
|
||||
{
|
||||
public override string Name => "search_pawn_kind";
|
||||
public override string Description => "Rough-searches PawnKindDefs by natural language (label/defName). Returns candidate defNames for send_reinforcement.";
|
||||
public override string UsageSchema => "<search_pawn_kind><query>string</query><maxResults>int (optional, default 10)</maxResults><minScore>float (optional, default 0.15)</minScore></search_pawn_kind>";
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
float minScore = 0.15f;
|
||||
if (parsed.TryGetValue("minScore", out string minStr) && float.TryParse(minStr, out float ms))
|
||||
{
|
||||
minScore = Math.Max(0.01f, Math.Min(1.0f, ms));
|
||||
}
|
||||
|
||||
var candidates = PawnKindDefSearcher.Search(query, maxResults: maxResults, minScore: minScore);
|
||||
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 ?? best.Def.defName}");
|
||||
sb.AppendLine($"BEST_SCORE: {best.Score:F2}");
|
||||
sb.AppendLine("CANDIDATES:");
|
||||
|
||||
int idx = 1;
|
||||
foreach (var c in candidates)
|
||||
{
|
||||
var def = c.Def;
|
||||
string label = def.label ?? def.defName;
|
||||
string race = def.race != null ? def.race.defName : "None";
|
||||
sb.AppendLine($"{idx}. defName='{def.defName}' label='{label}' race='{race}' score={c.Score:F2}");
|
||||
idx++;
|
||||
}
|
||||
|
||||
return sb.ToString().Trim();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return $"Error: {ex.Message}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,31 +19,65 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("Sends military units to the player's map. If hostile, this triggers a raid. If neutral/allied, this sends reinforcements. ");
|
||||
|
||||
float points = 0;
|
||||
|
||||
float basePoints = 0f;
|
||||
Map map = Find.CurrentMap;
|
||||
if (map != null)
|
||||
{
|
||||
points = StorytellerUtility.DefaultThreatPointsNow(map);
|
||||
basePoints = StorytellerUtility.DefaultThreatPointsNow(map);
|
||||
}
|
||||
|
||||
int goodwill = 0;
|
||||
float goodwillFactor = 1.0f;
|
||||
bool hostile = false;
|
||||
var eventVarManager = Find.World?.GetComponent<EventVariableManager>();
|
||||
if (eventVarManager != null)
|
||||
{
|
||||
goodwill = eventVarManager.GetVariable<int>("Wula_Goodwill_To_PIA", 0);
|
||||
}
|
||||
|
||||
sb.Append($"Current Raid Points Budget: {points:F0}. ");
|
||||
sb.Append("Available Units (Name: Cost): ");
|
||||
|
||||
Faction faction = Find.FactionManager.FirstFactionOfDef(FactionDef.Named("Wula_PIA_Legion_Faction"));
|
||||
if (faction != null)
|
||||
{
|
||||
var pawnKinds = DefDatabase<PawnKindDef>.AllDefs
|
||||
.Where(pk => faction.def.pawnGroupMakers != null && faction.def.pawnGroupMakers.Any(pgm => pgm.options.Any(o => o.kind == pk)))
|
||||
.Distinct()
|
||||
.OrderBy(pk => pk.combatPower);
|
||||
hostile = faction.HostileTo(Faction.OfPlayer);
|
||||
}
|
||||
|
||||
if (hostile)
|
||||
{
|
||||
if (goodwill < -50) goodwillFactor = 1.5f;
|
||||
else if (goodwill < 0) goodwillFactor = 1.2f;
|
||||
else if (goodwill > 50) goodwillFactor = 0.8f;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (goodwill < -50) goodwillFactor = 0.5f;
|
||||
else if (goodwill < 0) goodwillFactor = 0.8f;
|
||||
else if (goodwill > 50) goodwillFactor = 1.5f;
|
||||
}
|
||||
|
||||
float adjustedMaxPoints = basePoints * goodwillFactor * 1.5f;
|
||||
|
||||
sb.Append($"Current Raid Points Budget: {basePoints:F0}. ");
|
||||
sb.Append($"Adjusted Budget (Goodwill {goodwill}, Hostile={hostile}): {adjustedMaxPoints:F0}. ");
|
||||
sb.Append("Available Units (defName | label | cost): ");
|
||||
|
||||
if (faction != null)
|
||||
{
|
||||
var pawnKinds = DefDatabase<PawnKindDef>.AllDefs
|
||||
.Where(pk => IsWulaPawnKind(pk, faction))
|
||||
.Distinct()
|
||||
.OrderBy(pk => pk.combatPower)
|
||||
.ThenBy(pk => pk.defName)
|
||||
.Take(40)
|
||||
.ToList();
|
||||
|
||||
bool first = true;
|
||||
foreach (var pk in pawnKinds)
|
||||
{
|
||||
if (pk.combatPower > 0)
|
||||
{
|
||||
sb.Append($"{pk.defName}: {pk.combatPower:F0}, ");
|
||||
}
|
||||
string label = string.IsNullOrWhiteSpace(pk.label) ? pk.defName : pk.label;
|
||||
if (!first) sb.Append("; ");
|
||||
sb.Append($"{pk.defName} | {label} | {pk.combatPower:F0}");
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -56,6 +90,19 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsWulaPawnKind(PawnKindDef pk, Faction faction)
|
||||
{
|
||||
if (pk == null) return false;
|
||||
string defName = pk.defName ?? "";
|
||||
string raceName = pk.race?.defName ?? "";
|
||||
|
||||
if (defName.IndexOf("wula", StringComparison.OrdinalIgnoreCase) >= 0) return true;
|
||||
if (raceName.IndexOf("wula", StringComparison.OrdinalIgnoreCase) >= 0) return true;
|
||||
if (defName.IndexOf("cat", StringComparison.OrdinalIgnoreCase) >= 0) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override string UsageSchema => "<send_reinforcement><units>string (e.g., 'Wula_PIA_Heavy_Unit_Melee: 2, Wula_PIA_Legion_Escort_Unit: 5')</units></send_reinforcement>";
|
||||
|
||||
public override string Execute(string args)
|
||||
@@ -180,4 +227,4 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user