Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic AI player research #561

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion C7Engine/AI/PlayerAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace C7Engine {
public class PlayerAI {
private static ILogger log = Log.ForContext<PlayerAI>();

public static void PlayTurn(Player player, Random rng) {
public static void PlayTurn(Player player, Random rng, List<Tech> techs) {
if (player.isHuman || player.isBarbarians) {
return;
}
Expand All @@ -32,6 +32,17 @@ public static void PlayTurn(Player player, Random rng) {
player.turnsUntilPriorityReevaluation--;
}

if (player.currentlyResearchedTech == null) {
Tech toResearch = PickTechToResearch(player, techs);
if (toResearch == null) {
log.Information($"Player {player.civilization.name} has no techs available to research.");
player.SetCurrentlyResearchedTech(null);
} else {
log.Information($"Player {player.civilization.name} is researching {toResearch.Name}.");
player.SetCurrentlyResearchedTech(toResearch.id);
}
}

//Do things with units. Copy into an array first to avoid collection-was-modified exception
foreach (MapUnit unit in player.units.ToArray()) {
//For each unit, if there's already an AI task assigned, it will attempt to complete its goal.
Expand Down Expand Up @@ -253,5 +264,33 @@ public static UnitAI getAIForUnitStrategy(UnitAIData aiData) {
}
throw new Exception("AI data not recognized" + aiData);
}

private static Tech PickTechToResearch(Player player, List<Tech> techs) {
List<Tech> possibleTechs = new();

// Figure out what techs we're allowed to research.
foreach (Tech tech in techs) {
if (tech.EraCivilopediaName != player.eraCivilopediaName) {
continue;
}
bool prereqsKnown = true;
foreach (Tech prereq in tech.Prerequisites) {
if (!player.knownTechs.Contains(prereq.id)) {
prereqsKnown = false;
break;
}
}
if (!prereqsKnown) {
continue;
}
possibleTechs.Add(tech);
}

if (possibleTechs.Count == 0) {
return null;
}

return possibleTechs[(int)GameData.rng.NextInt64(possibleTechs.Count)];
}
}
}
2 changes: 1 addition & 1 deletion C7Engine/EntryPoints/TurnHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static bool PlayPlayerTurns(GameData gameData, bool firstTurn) {
new BarbarianAI().PlayTurn(player, gameData);
player.hasPlayedThisTurn = true;
} else if (!player.isHuman) {
PlayerAI.PlayTurn(player, GameData.rng);
PlayerAI.PlayTurn(player, GameData.rng, gameData.techs);
player.hasPlayedThisTurn = true;
} else if (player.id != EngineStorage.uiControllerID) {
player.hasPlayedThisTurn = true;
Expand Down