Skip to content

Commit

Permalink
Merge pull request #561 from C7-Game/twrner/ai-researches-tech
Browse files Browse the repository at this point in the history
Implement basic AI player research
  • Loading branch information
TomWerner authored Feb 23, 2025
2 parents 44a81be + ba4f0c4 commit f883720
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
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 @@ -85,7 +85,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

0 comments on commit f883720

Please sign in to comment.