Skip to content

Commit

Permalink
Keep track of the currently researched tech and let the user change it
Browse files Browse the repository at this point in the history
This PR also loads this information from save files.

#392
  • Loading branch information
TomWerner committed Jan 25, 2025
1 parent 191b440 commit e3a9b66
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 17 deletions.
5 changes: 5 additions & 0 deletions C7/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public void processEngineMessages(GameData gameData) {
setSelectedUnit(CurrentlySelectedUnit);
}
break;
case MsgUpdateUiAfterTechSelection mUUATS:
// F6 is the science advisor.
// TODO: Move the F* key strings to a set of constants/enum.
EmitSignal(SignalName.ShowSpecificAdvisor, "F6");
break;
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions C7/UIElements/Advisors/Advisors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ private void OnShowSpecificAdvisor(string advisorType) {
this.Show();
}
if (advisorType.Equals("F6")) {
if (scienceAdvisor == null) {
scienceAdvisor = new ScienceAdvisor();
advisors.Add(scienceAdvisor);
AddChild(scienceAdvisor);
} else {
scienceAdvisor.Show();
// TODO: What's the best way to refresh the tech tree UI without
// adding too many children?
if (scienceAdvisor != null) {
RemoveChild(scienceAdvisor);
scienceAdvisor = null;
}

scienceAdvisor = new ScienceAdvisor();
advisors.Add(scienceAdvisor);
AddChild(scienceAdvisor);
this.Show();
}
}
Expand Down
24 changes: 18 additions & 6 deletions C7/UIElements/Advisors/ScienceAdvisor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
using System.Collections.Generic;

public partial class ScienceAdvisor : TextureRect {
private ImageTexture AncientBackground;
private ImageTexture MiddleBackground;
private ImageTexture IndustrialBackground;
private ImageTexture ModernBackground;

// Called when the node enters the scene tree for the first time.
public override void _Ready() {
this.CreateUI();
this.DrawTechTree();
}

private void CreateUI() {
Expand All @@ -16,10 +22,10 @@ private void CreateUI() {
//
// science_industrial_new is used as the industrial tech tree is
// different from vanilla civ3.
ImageTexture AncientBackground = Util.LoadTextureFromPCX("Art/Advisors/science_ancient.pcx");
ImageTexture MiddleBackground = Util.LoadTextureFromPCX("Art/Advisors/science_middle.pcx");
ImageTexture IndustrialBackground = Util.LoadTextureFromPCX("Art/Advisors/science_industrial_new.pcx");
ImageTexture ModernBackground = Util.LoadTextureFromPCX("Art/Advisors/science_modern.pcx");
AncientBackground = Util.LoadTextureFromPCX("Art/Advisors/science_ancient.pcx");
MiddleBackground = Util.LoadTextureFromPCX("Art/Advisors/science_middle.pcx");
IndustrialBackground = Util.LoadTextureFromPCX("Art/Advisors/science_industrial_new.pcx");
ModernBackground = Util.LoadTextureFromPCX("Art/Advisors/science_modern.pcx");

// TODO: Age-based background. Only use Ancient for now.
// TODO: Consider moving this to an advisor utility, since we're copying
Expand Down Expand Up @@ -53,11 +59,13 @@ private void CreateUI() {
GoBackButton.SetPosition(new Vector2(952, 720));
AddChild(GoBackButton);
GoBackButton.Pressed += ReturnToMenu;
}

void DrawTechTree() {
using (UIGameDataAccess gameDataAccess = new()) {
List<Tech> techs = gameDataAccess.gameData.techs;
Player player = gameDataAccess.gameData.GetHumanPlayers()[0];
List<ID> knownTechs = player.knownTechs;
HashSet<ID> knownTechs = player.knownTechs;

// Set the tech background based on the player's era.
if (player.eraCivilopediaName == "ERAS_Ancient_Times") {
Expand All @@ -75,10 +83,11 @@ private void CreateUI() {
continue;
}

// TODO: track the currently researched tech for kInProgress.
TechBox.TechState techState = TechBox.TechState.kBlocked;
if (knownTechs.Contains(tech.id)) {
techState = TechBox.TechState.kKnown;
} else if (player.currentlyResearchedTech == tech.id) {
techState = TechBox.TechState.kInProgress;
} else {
bool prereqsKnown = true;
foreach (Tech prereq in tech.Prerequisites) {
Expand All @@ -92,6 +101,9 @@ private void CreateUI() {

TechBox techButton = new(tech, techState);
techButton.SetPosition(new Vector2(tech.X, tech.Y));
techButton.Pressed += () => {
new MsgChooseResearch(tech.id).send();
};
AddChild(techButton);
}
}
Expand Down
28 changes: 28 additions & 0 deletions C7Engine/EntryPoints/MessageToEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,34 @@ public override void process() {
}
}

public class MsgChooseResearch : MessageToEngine {
private ID techId;
public MsgChooseResearch(ID techId) {
this.techId = techId;
}

public override void process() {
Player player = EngineStorage.gameData.GetHumanPlayers()[0];
if (player.currentlyResearchedTech == techId) {
return;
}
Tech requestedTech = EngineStorage.gameData.techs.Find(t => t.id == techId);

// Ensure this is an eligible tech to research.
//
// TODO: do a topological sort to allow a queue of techs to study.
foreach (Tech prereq in requestedTech.Prerequisites) {
if (!player.knownTechs.Contains(prereq.id)) {
return;
}
}

// Start researching this tech and update the UI.
player.currentlyResearchedTech = requestedTech.id;
new MsgUpdateUiAfterTechSelection().send();
}
}

public class MsgEndTurn : MessageToEngine {

private ILogger log = Log.ForContext<MsgEndTurn>();
Expand Down
2 changes: 2 additions & 0 deletions C7Engine/EntryPoints/MessageToUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class MsgStartTurn : MessageToUI { }

public class MsgUpdateUiAfterMove : MessageToUI { }

public class MsgUpdateUiAfterTechSelection : MessageToUI { }

public class MsgCityDestroyed : MessageToUI {
public City city;

Expand Down
7 changes: 4 additions & 3 deletions C7GameData/ImportCiv3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,10 @@ private void ImportSavLeaders() {
/*cityNameIndex=*/leader.FoundedCities,
/*era=*/theBiq.Eras[leader.Era].CivilopediaEntry);


// TODO: store non-negative values of leader.Researching,
// which indexes into save.Techs.
// Record what the player is currently researching.
if (leader.Researching > -1) {
player.currentlyResearchedTech = save.Techs[leader.Researching].id;
}

// Record any techs that this player knows.
for (int k = 0; k < savData.KnownTechFlags.Length; ++k) {
Expand Down
5 changes: 4 additions & 1 deletion C7GameData/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public class Player {
public List<StrategicPriority> strategicPriorityData = new List<StrategicPriority>();

// The list of techs known by this player.
public List<ID> knownTechs = new();
public HashSet<ID> knownTechs = new();

// The tech the player is currently researching.
public ID currentlyResearchedTech;

// The civilopedia name of the era this player is in.
//
Expand Down
7 changes: 6 additions & 1 deletion C7GameData/Save/SavePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public class SavePlayer {
public List<TileLocation> tileKnowledge = new List<TileLocation>();

// The list of techs known by this player.
public List<ID> knownTechs = new();
public HashSet<ID> knownTechs = new();

// The tech the player is currently researching.
public ID currentlyResearchedTech;

// The civilopedia name of the era this player is in.
//
Expand All @@ -36,6 +39,7 @@ public Player ToPlayer(GameMap map, List<Civilization> civilizations) {
cityNameIndex = cityNameIndex,
tileKnowledge = new TileKnowledge(),
knownTechs = knownTechs,
currentlyResearchedTech = currentlyResearchedTech,
eraCivilopediaName = eraCivilopediaName,
};
foreach (TileLocation tile in tileKnowledge) {
Expand Down Expand Up @@ -64,6 +68,7 @@ public SavePlayer(Player player) {
tileKnowledge = player.tileKnowledge.AllKnownTiles().ConvertAll(tile => new TileLocation(tile));
turnsUntilPriorityReevaluation = player.turnsUntilPriorityReevaluation;
knownTechs = player.knownTechs;
currentlyResearchedTech = player.currentlyResearchedTech;
eraCivilopediaName = player.eraCivilopediaName;
}
}
Expand Down

0 comments on commit e3a9b66

Please sign in to comment.