#region Copyright & License Information /* * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, * see LICENSE. */ #endregion using System.Collections.Generic; using System.Linq; using OpenRA.FileFormats; using OpenRA.GameRules; namespace OpenRA.Traits { class TechTreeCacheInfo : TraitInfo { } class TechTreeCache : ITick { class Watcher { public readonly List prerequisites; public readonly ITechTreeElement watcher; bool hasPrerequisites; public Watcher(List prerequisites, ITechTreeElement watcher) { this.prerequisites = prerequisites; this.watcher = watcher; this.hasPrerequisites = false; } public void Tick( Player owner, Cache> buildings ) { if (owner.Country == null) return; var effectivePrereq = prerequisites.Where( a => a.Traits.Contains() && a.Traits.Get().Owner.Contains( owner.Country.Race ) ); var nowHasPrerequisites = effectivePrereq.Any() && effectivePrereq.All( a => buildings[ a.Name ].Any( b => !b.Trait().Disabled ) ); if( nowHasPrerequisites && !hasPrerequisites ) watcher.Available(); if( !nowHasPrerequisites && hasPrerequisites ) watcher.Unavailable(); hasPrerequisites = nowHasPrerequisites; } } readonly List watchers = new List(); public void Tick( Actor self ) { var buildings = Rules.TechTree.GatherBuildings( self.Owner ); foreach( var w in watchers ) w.Tick( self.Owner, buildings ); } public void Add( List prerequisites, ITechTreeElement tte ) { watchers.Add( new Watcher( prerequisites, tte ) ); } public void Remove( ITechTreeElement tte ) { watchers.RemoveAll( x => x.watcher == tte ); } } interface ITechTreeElement { void Available(); void Unavailable(); } }