@@ -18,8 +18,10 @@ public struct ProductionTableFlow(IObjectWithQuality<Goods> goods, float amount,
1818 public IProductionLink ? link = link ;
1919}
2020
21+ [ DeserializeWithNonPublicConstructor ]
2122public sealed partial class ProductionTable : ProjectPageContents , IComparer < ProductionTableFlow > , IElementGroup < RecipeRow > {
2223 private static readonly ILogger logger = Logging . GetLogger < ProductionTable > ( ) ;
24+ private readonly ProductionTable rootTable ;
2325 [ SkipSerialization ] public Dictionary < IObjectWithQuality < Goods > , IProductionLink > linkMap { get ; } = [ ] ;
2426 List < RecipeRow > IElementGroup < RecipeRow > . elements => recipes ;
2527 [ NoUndo ]
@@ -43,12 +45,26 @@ public sealed partial class ProductionTable : ProjectPageContents, IComparer<Pro
4345 public ModuleFillerParameters ? modules { get ; } // If you add a setter for this, ensure it calls RecipeRow.ModuleFillerParametersChanging().
4446 public bool containsDesiredProducts { get ; private set ; }
4547
46- public ProductionTable ( ModelObject owner ) : base ( owner ) {
47- if ( owner is ProjectPage ) {
48+ // For deserialization
49+ private ProductionTable ( ModelObject owner ) : base ( owner ) {
50+ if ( owner is RecipeRow row ) {
51+ rootTable = row . owner . rootTable ;
52+ }
53+ else {
4854 modules = new ModuleFillerParameters ( this ) ;
55+ rootTable = this ;
4956 }
5057 }
5158
59+ // For the ProjectPage constructor
60+ public ProductionTable ( ProjectPage owner ) : base ( owner ) {
61+ modules = new ModuleFillerParameters ( this ) ;
62+ rootTable = this ;
63+ }
64+
65+ // For creating nested tables
66+ public ProductionTable ( RecipeRow owner ) : base ( owner ) => rootTable = owner . owner . rootTable ;
67+
5268 protected internal override void ThisChanged ( bool visualOnly ) {
5369 RebuildLinkMap ( ) ;
5470 if ( owner is ProjectPage page ) {
@@ -775,5 +791,45 @@ public int Compare(ProductionTableFlow x, ProductionTableFlow y) {
775791
776792 return amt1 . CompareTo ( amt2 ) ;
777793 }
778- }
779794
795+ /// <summary>
796+ /// Returns <see langword="true"/> if this table (including its parent recipe, if applicable) contains the specified recipe at the specified quality.
797+ /// </summary>
798+ public bool Contains ( IObjectWithQuality < RecipeOrTechnology > obj ) {
799+ if ( owner is RecipeRow { recipe : IObjectWithQuality < RecipeOrTechnology > recipe } && recipe == obj ) {
800+ return true ;
801+ }
802+ return recipes . Any ( r => r . recipe == obj ) ;
803+ }
804+
805+ /// <summary>
806+ /// Returns <see langword="true"/> if this table (including its parent recipe, if applicable) contains the specified recipe at any quality.
807+ /// </summary>
808+ /// <remarks>This is most commonly used for deciding whether to draw a green checkmark.</remarks>
809+ public bool Contains ( RecipeOrTechnology obj ) {
810+ if ( owner is RecipeRow { recipe . target : RecipeOrTechnology recipe } && recipe == obj ) {
811+ return true ;
812+ }
813+ return recipes . Any ( r => r . recipe . target == obj ) ;
814+ }
815+
816+ /// <summary>
817+ /// Returns <see langword="true"/> if the specified recipe appears at the specified quality anywhere on this table's <see cref="ProjectPage"/>.
818+ /// </summary>
819+ public bool ContainsAnywhere ( IObjectWithQuality < RecipeOrTechnology > obj ) => rootTable . GetAllRecipes ( ) . Any ( r => r . recipe == obj ) ;
820+
821+ /// <summary>
822+ /// Returns <see langword="true"/> if the specified recipe appears at any quality anywhere on this table's <see cref="ProjectPage"/>.
823+ /// </summary>
824+ /// <remarks>This is most commonly used for deciding whether to draw a yellow checkmark.</remarks>
825+ public bool ContainsAnywhere ( RecipeOrTechnology obj ) => rootTable . GetAllRecipes ( ) . Any ( r => r . recipe . target == obj ) ;
826+
827+ public bool CreateLink ( IObjectWithQuality < Goods > goods ) {
828+ if ( linkMap . GetValueOrDefault ( goods ) is ProductionLink || ! goods . target . isLinkable ) {
829+ return false ;
830+ }
831+
832+ this . RecordUndo ( ) . links . Add ( new ( this , goods . target . With ( goods . quality ) ) ) ;
833+ return true ;
834+ }
835+ }
0 commit comments