using Arcen.AIW2.Core; using Arcen.Universal; using System; using System.Collections.Generic; using System.Text; namespace Arcen.AIW2.External { // Tracks the AIT data that should be attached to each planet. public class PlanetPathfindingExternalData : IArcenExternalDataPatternImplementation { public List PathingPenalties; public static int PatternIndex; public static string RelatedParentTypeName = "Planet"; public void ReceivePatternIndex( int Index ) { PatternIndex = Index; } public int GetNumberOfItems() { return 1; } public bool GetShouldInitializeOn( string ParentTypeName ) { return ArcenStrings.Equals( ParentTypeName, RelatedParentTypeName ); } public void InitializeData( object ParentObject, object[] Target ) { this.PathingPenalties = new List( World_AIW2.Instance.Factions.Count ); for ( int i = 0; i < World_AIW2.Instance.Factions.Count; i++ ) this.PathingPenalties.Add(0); Target[0] = this.PathingPenalties; } public void SerializeExternalData( object[] Source, ArcenSerializationBuffer Buffer ) { // For saving to disk, translate this object into the buffer" List pathingPenalties = (List)Source[0]; // There is one list per faction. for ( int i = 0; i < World_AIW2.Instance.Factions.Count; i++ ) Buffer.AddItem( pathingPenalties[i] ); } public void DeserializeExternalData( object ParentObject, object[] Target, int ItemsToExpect, ArcenDeserializationBuffer Buffer ) { //reverses SerializeData; gets the data out of the buffer and populates the variables List pathingPenalties = new List( World_AIW2.Instance.Factions.Count ); // There is one list per faction. for ( int i = 0; i < World_AIW2.Instance.Factions.Count; i++ ) pathingPenalties.Add( Buffer.ReadInt32() ); Target[0] = pathingPenalties; } } // The following is a halper function to the above, designed to allow us to save and load data on demand. public static class ExtensionMethodsFor_PlanetPathfindingExternalData { // Get the list of pathing penalties for this planet. public static int GetPathingPenalty( this Planet ParentObject, Faction faction ) { return ((List)ParentObject.ExternalData.GetCollectionByPatternIndex( PlanetPathfindingExternalData.PatternIndex ).Data[0])[faction.FactionIndex]; } // Adds a penalty to pathing through this planet. public static void AddPathingPenalty( this Planet ParentObject, Faction faction, int penalty ) { List pathingPenalties = (List)ParentObject.ExternalData.GetCollectionByPatternIndex( PlanetPathfindingExternalData.PatternIndex ).Data[0]; pathingPenalties[faction.FactionIndex] += penalty; } // Removes a penalty of the specified size from this planet. public static void RemovePathingPenalty( this Planet ParentObject, Faction faction, int penalty ) { List pathingPenalties = (List)ParentObject.ExternalData.GetCollectionByPatternIndex( PlanetPathfindingExternalData.PatternIndex ).Data[0]; pathingPenalties[faction.FactionIndex] -= penalty; // Perhaps there should be an error if the penalty becomes negative? Can the algorithm cope with negatives? } } }