Skip to content

Commit

Permalink
Implement basic AI player research
Browse files Browse the repository at this point in the history
Right now they just randomly pick techs to research without any particular strategy, but this will get us closer to some niceties, like tech trading and era advancement.

#391
  • Loading branch information
TomWerner committed Feb 16, 2025
1 parent d187bb8 commit fd69b28
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 @@ -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

0 comments on commit fd69b28

Please sign in to comment.