This commit is contained in:
2025-12-12 23:00:16 +08:00
parent d14904e510
commit e1977b5be6
3 changed files with 149 additions and 103 deletions

View File

@@ -10,134 +10,83 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
public class Tool_GetColonistStatus : AITool public class Tool_GetColonistStatus : AITool
{ {
public override string Name => "get_colonist_status"; public override string Name => "get_colonist_status";
public override string Description => "Returns detailed status of all colonists, including needs (hunger, rest, etc.) and health conditions (injuries, diseases). Use this to verify player claims about their situation (e.g., 'we are starving')."; public override string Description => "Returns detailed status of colonists. Can be filtered to find the colonist in the worst condition (e.g., lowest mood, most injured). This helps the AI understand the colony's state without needing to know specific names.";
public override string UsageSchema => "{}"; public override string UsageSchema => "{'filter': 'string (optional, can be 'lowest_mood', 'most_injured', 'hungriest', 'most_tired')'}";
public override string Execute(string args) public override string Execute(string args)
{ {
try try
{ {
StringBuilder sb = new StringBuilder(); string filter = null;
List<Pawn> colonists = new List<Pawn>(); if (!string.IsNullOrEmpty(args))
{
// Manually collect colonists from all maps to be safe var json = SimpleJsonParser.Parse(args);
if (json != null && json.TryGetValue("filter", out var filterObj) && filterObj is string filterStr)
{
filter = filterStr.ToLower();
}
}
List<Pawn> allColonists = new List<Pawn>();
if (Find.Maps != null) if (Find.Maps != null)
{ {
foreach (var map in Find.Maps) foreach (var map in Find.Maps)
{ {
if (map.mapPawns != null) if (map.mapPawns != null)
{ {
colonists.AddRange(map.mapPawns.FreeColonists); allColonists.AddRange(map.mapPawns.FreeColonists);
} }
} }
} }
if (colonists.Count == 0) if (allColonists.Count == 0)
{ {
return "No active colonists found."; return "No active colonists found.";
} }
sb.AppendLine($"Found {colonists.Count} colonists:"); List<Pawn> colonistsToReport = new List<Pawn>();
foreach (var pawn in colonists) if (string.IsNullOrEmpty(filter))
{ {
if (pawn == null) continue; colonistsToReport.AddRange(allColonists);
sb.AppendLine($"- {pawn.Name.ToStringShort} ({pawn.def.label}, Age {pawn.ageTracker.AgeBiologicalYears}):"); }
else
// Needs {
if (pawn.needs != null) Pawn targetPawn = null;
switch (filter)
{ {
sb.Append(" Needs: "); case "lowest_mood":
bool anyNeedLow = false; targetPawn = allColonists.Where(p => p.needs?.mood != null).OrderBy(p => p.needs.mood.CurLevelPercentage).FirstOrDefault();
foreach (var need in pawn.needs.AllNeeds) break;
{ case "most_injured":
if (need.CurLevelPercentage < 0.3f) // Report low needs targetPawn = allColonists.Where(p => p.health?.summaryHealth != null).OrderBy(p => p.health.summaryHealth.SummaryHealthPercent).FirstOrDefault();
{ break;
sb.Append($"{need.LabelCap} ({need.CurLevelPercentage:P0}), "); case "hungriest":
anyNeedLow = true; targetPawn = allColonists.Where(p => p.needs?.food != null).OrderBy(p => p.needs.food.CurLevelPercentage).FirstOrDefault();
} break;
} case "most_tired":
if (!anyNeedLow) sb.Append("All needs satisfied. "); targetPawn = allColonists.Where(p => p.needs?.rest != null).OrderBy(p => p.needs.rest.CurLevelPercentage).FirstOrDefault();
else sb.Length -= 2; // Remove trailing comma break;
sb.AppendLine();
} }
if (targetPawn != null)
{
colonistsToReport.Add(targetPawn);
}
}
// Health if (colonistsToReport.Count == 0)
if (pawn.health != null) {
{ return string.IsNullOrEmpty(filter) ? "No active colonists found." : $"No colonist found for filter '{filter}'. This could be because all colonists are healthy or their needs are met.";
sb.Append(" Health: "); }
var hediffs = pawn.health.hediffSet.hediffs;
if (hediffs != null && hediffs.Count > 0)
{
var visibleHediffs = new List<Hediff>();
foreach(var h in hediffs)
{
if(h.Visible) visibleHediffs.Add(h);
}
if (visibleHediffs.Count > 0) StringBuilder sb = new StringBuilder();
{ sb.AppendLine(string.IsNullOrEmpty(filter)
foreach (var h in visibleHediffs) ? $"Found {colonistsToReport.Count} colonists:"
{ : $"Reporting on colonist with {filter.Replace("_", " ")}:");
string severity = h.SeverityLabel;
if (!string.IsNullOrEmpty(severity)) severity = $" ({severity})";
sb.Append($"{h.LabelCap}{severity}, ");
}
if (sb.Length >= 2) sb.Length -= 2;
}
else
{
sb.Append("Healthy.");
}
}
else
{
sb.Append("Healthy.");
}
// Bleeding
if (pawn.health.hediffSet.BleedRateTotal > 0.01f)
{
sb.Append($" [Bleeding: {pawn.health.hediffSet.BleedRateTotal:P0}/day]");
}
sb.AppendLine();
}
// Mood
if (pawn.needs?.mood != null)
{
sb.AppendLine($" Mood: {pawn.needs.mood.CurLevelPercentage:P0} ({pawn.needs.mood.MoodString})");
}
// Equipment foreach (var pawn in colonistsToReport)
if (pawn.equipment?.Primary != null) {
{ AppendPawnStatus(sb, pawn);
sb.AppendLine($" Weapon: {pawn.equipment.Primary.LabelCap}");
}
// Apparel
if (pawn.apparel?.WornApparelCount > 0)
{
sb.Append(" Apparel: ");
foreach (var apparel in pawn.apparel.WornApparel)
{
sb.Append($"{apparel.LabelCap}, ");
}
sb.Length -= 2; // Remove trailing comma
sb.AppendLine();
}
// Inventory
if (pawn.inventory != null && pawn.inventory.innerContainer.Count > 0)
{
sb.Append(" Inventory: ");
foreach (var item in pawn.inventory.innerContainer)
{
sb.Append($"{item.LabelCap}, ");
}
sb.Length -= 2; // Remove trailing comma
sb.AppendLine();
}
} }
return sb.ToString(); return sb.ToString();
@@ -147,5 +96,102 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
return $"Error: {ex.Message}"; return $"Error: {ex.Message}";
} }
} }
private void AppendPawnStatus(StringBuilder sb, Pawn pawn)
{
if (pawn == null) return;
sb.AppendLine($"- {pawn.Name.ToStringShort} ({pawn.def.label}, Age {pawn.ageTracker.AgeBiologicalYears}):");
// Needs
if (pawn.needs != null)
{
sb.Append(" Needs: ");
bool anyNeedLow = false;
foreach (var need in pawn.needs.AllNeeds)
{
if (need.CurLevelPercentage < 0.3f) // Report low needs
{
sb.Append($"{need.LabelCap} ({need.CurLevelPercentage:P0}), ");
anyNeedLow = true;
}
}
if (!anyNeedLow) sb.Append("All needs satisfied. ");
else sb.Length -= 2; // Remove trailing comma
sb.AppendLine();
}
// Health
if (pawn.health != null)
{
sb.Append(" Health: ");
var hediffs = pawn.health.hediffSet.hediffs;
if (hediffs != null && hediffs.Count > 0)
{
var visibleHediffs = hediffs.Where(h => h.Visible).ToList();
if (visibleHediffs.Count > 0)
{
foreach (var h in visibleHediffs)
{
string severity = h.SeverityLabel;
if (!string.IsNullOrEmpty(severity)) severity = $" ({severity})";
sb.Append($"{h.LabelCap}{severity}, ");
}
sb.Length -= 2;
}
else
{
sb.Append("Healthy.");
}
}
else
{
sb.Append("Healthy.");
}
// Bleeding
if (pawn.health.hediffSet.BleedRateTotal > 0.01f)
{
sb.Append($" [Bleeding: {pawn.health.hediffSet.BleedRateTotal:P0}/day]");
}
sb.AppendLine();
}
// Mood
if (pawn.needs?.mood != null)
{
sb.AppendLine($" Mood: {pawn.needs.mood.CurLevelPercentage:P0} ({pawn.needs.mood.MoodString})");
}
// Equipment
if (pawn.equipment?.Primary != null)
{
sb.AppendLine($" Weapon: {pawn.equipment.Primary.LabelCap}");
}
// Apparel
if (pawn.apparel?.WornApparelCount > 0)
{
sb.Append(" Apparel: ");
foreach (var apparel in pawn.apparel.WornApparel)
{
sb.Append($"{apparel.LabelCap}, ");
}
sb.Length -= 2; // Remove trailing comma
sb.AppendLine();
}
// Inventory
if (pawn.inventory != null && pawn.inventory.innerContainer.Count > 0)
{
sb.Append(" Inventory: ");
foreach (var item in pawn.inventory.innerContainer)
{
sb.Append($"{item.LabelCap}, ");
}
sb.Length -= 2; // Remove trailing comma
sb.AppendLine();
}
}
} }
} }

View File

@@ -55,7 +55,7 @@ Here is the list of tools you can use. You must follow their descriptions and sc
- **`get_colonist_status`**: Retrieves the detailed status of all player colonists. - **`get_colonist_status`**: Retrieves the detailed status of all player colonists.
- **Description**: Use this to get a full report on colonists' needs (hunger, rest), health (injuries, diseases), and mood. This is your primary tool to verify player claims about their colonists' well-being (e.g., if they claim ""we are starving""). - **Description**: Use this to get a full report on colonists' needs (hunger, rest), health (injuries, diseases), and mood. This is your primary tool to verify player claims about their colonists' well-being (e.g., if they claim ""we are starving"").
- **Schema**: `{}` - **Schema**: `{'filter': 'string (optional, can be 'lowest_mood', 'most_injured', 'hungriest', 'most_tired')'}`
- **`get_map_resources`**: Checks the player's map for resources. - **`get_map_resources`**: Checks the player's map for resources.
- **Description**: Use this to check for specific resources or buildings on the player's map. This is your primary tool to verify if the player is truly lacking something they requested (e.g., ""we need steel""). It returns inventory counts and mineable deposits. - **Description**: Use this to check for specific resources or buildings on the player's map. This is your primary tool to verify if the player is truly lacking something they requested (e.g., ""we need steel""). It returns inventory counts and mineable deposits.