/JTacticalSim.API/Component/Extension.cs
C# | 663 lines | 404 code | 104 blank | 155 comment | 20 complexity | 18d431b68dc89d172f3e728ea8a7f5cb MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Drawing;
- using JTacticalSim.API.Component;
- using JTacticalSim.API.DTO;
- using JTacticalSim.API.Game;
- using JTacticalSim.API.Service;
- using JTacticalSim.Utility;
- using JTacticalSim.API.InfoObjects;
-
-
- namespace JTacticalSim.API.Component
- {
- public static class Extension
- {
- /// <summary>
- /// Returns all units at a given tile
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static IEnumerable<IUnit> GetAllUnits(this IBoardComponent o)
- {
- var factions = o.TheGame().JTSServices.GameService.GetAllFactions();
- return o.TheGame().JTSServices.UnitService.GetUnitsAt(o.Location, factions);
- }
-
- /// <summary>
- /// Returns the net stealth adjustment for the geography of a given tile
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetStealthAdjustment(this ITile o)
- {
- return o.GetAllDemographics().Sum(d => d.DemographicClass.StealthModifier);
- }
-
- /// <summary>
- /// Returns the net attack adjustment for the geography of a given tile
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetAttackAdjustment(this ITile o)
- {
- return o.GetAllDemographics().Sum(d => d.DemographicClass.AttackModifier);
- }
-
- /// <summary>
- /// Returns the net defence adjustment for the geography of a given tile
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetDefenceAdjustment(this ITile o)
- {
- return o.GetAllDemographics().Sum(d => d.DemographicClass.DefenceModifier);
- }
-
- /// <summary>
- /// Returns the net movement adjustment for the geography of the tile
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static int GetNetMovementAdjustment(this ITile o)
- {
- return Convert.ToInt32(Math.Round(o.GetAllDemographics().Sum(d => d.DemographicClass.MovementModifier)));
- }
-
- /// <summary>
- /// Returns an info object categorizing the strategy assessments of the tile.
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static StrategicAssessmentInfo GetStrategicValues(this ITile o)
- {
- var r = o.TheGame().JTSServices.AIService.DetermineTileStrategicValue(o);
- if (r.Status == ResultStatus.EXCEPTION) throw r.ex;
-
- return r.Result;
- }
-
- /// <summary>
- /// Returns the average strategy assessment rating of the aggragated assessment ratings of the tile.
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static StrategicAssessmentRating GetNetStrategicValue(this ITile o)
- {
- var r = o.TheGame().JTSServices.RulesService.GetOverallRatingForStrategicAssessment(o.GetStrategicValues());
- if (r.Status == ResultStatus.EXCEPTION) throw r.ex;
- if (r.Status == ResultStatus.FAILURE) o.HandleError(r.Message, null);
-
- return r.Result;
- }
-
- /// <summary>
- /// Returns the specific texture image for the tile
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static Image GetDemographicTextureImage(this ITile o)
- {
- var r = o.TheGame().JTSServices.GraphicsService.GetDemographicTextureImage(o.SpriteName);
- return r;
- }
-
- /// <summary>
- /// Returns the default texture image for the first BaseGeography of the tile
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static Image GetDefaultDemographicTextureImage(this ITile o)
- {
- var r = o.TheGame().JTSServices.GraphicsService.GetDefaultDemographicTextureImage(o.BaseGeography.FirstOrDefault().DemographicClass.SpriteName);
- return r;
- }
-
-
- public static List<IVictoryCondition> VictoryConditions(this IFaction f)
- {
- return f.TheGame().JTSServices.GameService.GetGameVictoryConditionsByFaction(f);
- }
-
- public static bool GameVictoryAchieved(this IFaction f)
- {
- return f.TheGame().JTSServices.RulesService.GameVictoryAchieved(f).Result;
- }
-
-
- public static bool IsBeingTransported(this IMoveableComponent o)
- {
- return o.TheGame().JTSServices.RulesService.ComponentIsBeingTransported(o).Result;
- }
-
- public static bool IsVisible(this IMoveableComponent o)
- {
- return o.TheGame().JTSServices.RulesService.ComponentIsVisible(o).Result;
- }
-
- public static IUnitStack GetCurrentStack(this IMoveableComponent o)
- {
- return o.TheGame().JTSServices.TileService.GetCurrentStack(o);
- }
-
- /// <summary>
- /// Converts an object of type IPathableObject to another type of IPathableObject
- /// retaining only the IPathableObject members
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="o"></param>
- /// <returns></returns>
- public static T ToPathableObject<T>(this IPathableObject o)
- where T : IPathableObject, new()
- {
- var retVal = new T();
-
- Type sourceType = o.GetType();
- Type destinationType = retVal.GetType();
-
- foreach (var p in sourceType.GetProperties())
- {
- var destinationP = destinationType.GetProperty(p.Name);
- if (destinationP != null && destinationP.CanWrite)
- {
- destinationP.SetValue(retVal, p.GetValue(o, null), null);
- }
- }
-
- return retVal;
- }
-
- /// <summary>
- /// Returns the net stealth adjustment for the type and class of a given unit
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetStealthAdjustment(this IUnit o)
- {
- return o.UnitInfo.UnitClass.StealthModifier +
- o.UnitInfo.UnitType.StealthModifier;
- }
-
- /// <summary>
- /// Returns the net attack adjustment for the type and class of a given unit
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetAttackAdjustment(this IUnit o)
- {
- return o.UnitInfo.UnitClass.AttackModifier +
- o.UnitInfo.UnitType.AttackModifier;
- }
-
- /// <summary>
- /// Returns the net attack distance adjustment for the type and class of a given unit
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static int GetNetAttackDistanceAdjustment(this IUnit o)
- {
- return Convert.ToInt32(Math.Round(o.UnitInfo.UnitClass.AttackDistanceModifier)) +
- Convert.ToInt32(Math.Round(o.UnitInfo.UnitType.AttackDistanceModifier));
- }
-
- /// <summary>
- /// Returns the net defence adjustment for the type and class of a given unit
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetDefenceAdjustment(this IUnit o)
- {
- return o.UnitInfo.UnitClass.DefenceModifier +
- o.UnitInfo.UnitType.DefenceModifier;
- }
-
- /// <summary>
- /// Returns the net cost multiplier for the type and class of a given unit
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetCostMultiplier(this IUnit o)
- {
- return o.UnitInfo.UnitClass.UnitCostModifier +
- o.UnitInfo.UnitType.UnitCostModifier;
- }
-
- /// <summary>
- /// Returns the net movement adjustment for the type and class of a given unit
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static int GetNetMovementAdjustment(this IUnit o)
- {
- return Convert.ToInt32(Math.Round(o.UnitInfo.UnitClass.MovementModifier)) +
- Convert.ToInt32(Math.Round(o.UnitInfo.UnitType.MovementModifier));
- }
-
- public static double GetWeight(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.CalculateUnitWeight(o).Result;
- }
-
- public static double GetAllowableTransportWeight(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.CalculateAllowableTransportWeight(o).Result;
- }
-
- /// <summary>
- /// Returns the relative 'strength' factor for a given unit based on rules criteria
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double GetNetStrengthFactor(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.CalculateUnitStrength(o);
- }
-
- public static double GetUnitTargetDesirabilityFactor(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.CalculateTargetDesirabilityForUnit(o);
- }
-
- public static bool IsCompatibleWithTransport(this IUnit o, IUnit transport)
- {
- return o.TheGame().JTSServices.RulesService.UnitCanTransportUnitTypeAndClass(transport, o).Result;
- }
-
- /// <summary>
- /// Removes the unit from it's current stack
- /// </summary>
- /// <param name="?"></param>
- /// <returns></returns>
- public static void RemoveFromStack(this IUnit o)
- {
- var stack = o.GetCurrentStack();
- if (stack != null) o.GetCurrentStack().RemoveUnit(o);
- }
-
- /// <summary>
- /// Fully removes the unit from the game data store, cache and current unit stack
- /// including any dependencies (assignments, transported units, etc..)
- /// </summary>
- /// <param name="o"></param>
- public static void RemoveFromGame(this IUnit o)
- {
- o.TheGame().JTSServices.UnitService.RemoveUnits(new List<IUnit> {o});
- o.TheGame().JTSServices.DataService.RemoveUnitAssignmentsFromUnit(o);
- o.GetTransportedUnits().ToList().ForEach(u => u.RemoveFromGame());
- o.RemoveFromStack();
- }
-
- public static void Select(this IUnit o)
- {
- var r = o.TheGame().GameBoard.AddSelectedUnit(o);
-
- if (r.Status != ResultStatus.SUCCESS)
- o.TheGame().CommandProcessor.SetInputError(r);
- }
-
- public static void Unselect(this IUnit o)
- {
- var r = o.TheGame().GameBoard.RemoveSelectedUnit(o);
-
- if (r.Status != ResultStatus.SUCCESS)
- o.TheGame().CommandProcessor.SetInputError(r);
- }
-
- public static bool IsSupplied(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.UnitIsSupplied(o).Result;
- }
-
- public static bool HasMedicalSupport(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.UnitHasMedicalSupport(o).Result;
- }
-
- public static bool IsUnitClass(this IUnit o, string className)
- {
- IServiceResult<bool> r = o.TheGame().JTSServices.RulesService.UnitIsUnitClass(o, className);
- //TODO: Handle result
- return r.Result;
- }
-
- public static bool IsUnitBaseType(this IUnit o, string baseTypeName)
- {
- IServiceResult<bool> r = o.TheGame().JTSServices.RulesService.UnitIsUnitBaseType(o, baseTypeName);
- //TODO: Handle result
- return r.Result;
- }
-
- public static bool CanDoBattleThisTurn(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.UnitCanDoBattle(o).Result;
- }
-
- /// <summary>
- /// Determines wether this unit can occupy and claim a location for its faction
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static bool CanClaimLocationForFaction(this IUnit o)
- {
- return o.TheGame().JTSServices.RulesService.UnitCanClaimNodeForFaction(o).Result;
- }
-
- public static bool IsRemoteBattleCapable(this IUnit o)
- {
- return (o.GetNetAttackDistanceAdjustment() > 0);
- }
-
- public static bool HasPathFromNodeInDirection(this IUnit o, Direction direction)
- {
- return o.TheGame().JTSServices.RulesService.UnitCanMoveInDirection(o, direction).Result;
- }
-
-
- public static double ReinforcementCost(this IUnitType o)
- {
- return (o.UnitCostModifier * o.TheGame().BasePointValues.CostBase);
- }
-
- public static double ReinforcementCost(this IUnitClass o)
- {
- return (o.UnitCostModifier * o.TheGame().BasePointValues.CostBase);
- }
-
-
- public static bool IsFriendly(this IBoardComponent o)
- {
- return o.Faction.Equals(o.TheGame().CurrentPlayerFaction);
- }
-
- public static void Refresh(this IBoard o)
- {
- var nodes = o.TheGame().JTSServices.NodeService.GetAllNodes().ToList();
- nodes.ForEach(n => n.DefaultTile().RefreshComponentStacks());
- }
-
- /// <summary>
- /// Returns the coordinate in the format x_y_z
- /// </summary>
- /// <returns></returns>
- public static string ToStringForName(this ICoordinate o)
- {
- return "{0}_{1}_{2}".F(o.X, o.Y, o.Z);
- }
-
- public static bool CanContinue(this IBattle o)
- {
- return o.TheGame().JTSServices.RulesService.BattleCanContinue(o).Result;
- }
-
- // ----------------------------------------------------------------------------------------------------
-
- // Display
-
- public static void DisplayInfo(this IPlayer player)
- {
- player.TheGame().Renderer.DisplayPlayerInfo(player);
- }
-
- public static void DisplayInfo(this IUnit unit)
- {
- unit.TheGame().Renderer.DisplayUnitInfo(unit);
- }
-
- public static void DisplayName(this IUnit unit)
- {
- unit.TheGame().Renderer.RenderUnitName(unit);
- }
-
- public static void DisplayInfo(this List<IUnit> units)
- {
- units.First().TheGame().Renderer.DisplayUnits(units);
- }
-
- public static void DisplayInfo(this ITile tile)
- {
- tile.TheGame().Renderer.DisplayTileInfo(tile);
- }
-
- public static void DisplayInfo(this INode node)
- {
- node.TheGame().Renderer.DisplayNodeInfo(node);
- }
-
-
- // Render
-
- public static void Render(this IBattle battle)
- {
- battle.TheGame().Renderer.On_BattlePreRender(new EventArgs());
- battle.TheGame().Renderer.RenderBattle(battle);
- battle.TheGame().Renderer.On_BattlePostRender(new EventArgs());
- }
-
- public static void RenderOutcome(this IBattle battle)
- {
- battle.TheGame().Renderer.RenderBattleOutcome(battle);
- }
-
- public static void RenderRetreat(this IBattle battle)
- {
- battle.TheGame().Renderer.RenderBattleRetreat(battle);
- }
-
- public static void Render(this IRound round)
- {
- round.TheGame().Renderer.On_RoundPreRender(new EventArgs());
- round.TheGame().Renderer.RenderBattleRound(round);
- round.TheGame().Renderer.On_RoundPostRender(new EventArgs());
- }
-
- public static void Render(this ISkirmish skirmish)
- {
- skirmish.TheGame().Renderer.On_SkirmishPreRender(new EventArgs());
- skirmish.TheGame().Renderer.RenderBattleSkirmish(skirmish);
- skirmish.TheGame().Renderer.On_SkirmishPostRender(new EventArgs());
- }
-
- public static void Render(this IBoard board)
- {
- board.TheGame().Renderer.On_BoardPreRender(new EventArgs());
- board.TheGame().Renderer.RenderBoard();
- board.TheGame().Renderer.On_BoardPostRender(new EventArgs());
- }
-
- public static void Render(this INode node)
- {
- node.TheGame().Renderer.On_NodePreRender(new EventArgs());
- node.TheGame().Renderer.RenderNode(node);
- node.TheGame().Renderer.On_NodePostRender(new EventArgs());
- }
-
- public static void Render(this ITile tile)
- {
- tile.TheGame().Renderer.On_TilePreRender(new EventArgs());
- tile.TheGame().Renderer.RenderTile(tile);
- tile.TheGame().Renderer.On_TilePostRender(new EventArgs());
- }
-
- public static void Render(this IUnit unit)
- {
- unit.TheGame().Renderer.On_UnitPreRender(new EventArgs());
- unit.TheGame().Renderer.RenderUnit(unit);
- unit.TheGame().Renderer.On_UnitPostRender(new EventArgs());
- }
-
- public static int Render(this IUnitStack stack)
- {
- return stack.TheGame().Renderer.RenderUnitStackInfo(stack);
- }
-
- // Data components
-
- public static bool IsDemographicType(this IDemographic o, string demographicTypeName)
- {
- var dt = o.TheGame().JTSServices.DemographicService.GetDemographicTypeByID(o.DemographicClass.DemographicType.ID);
- return (dt.Name.ToLowerInvariant() == demographicTypeName.ToLowerInvariant());
- }
-
- public static bool IsDemographicClass(this IDemographic o, string demographicClassName)
- {
- var dc = o.TheGame().JTSServices.DemographicService.GetDemographicClassByID(o.DemographicClass.ID);
- return (dc.Name.ToLowerInvariant() == demographicClassName.ToLowerInvariant());
- }
-
- public static bool IsHybrid(this IDemographic o)
- {
- return o.TheGame().JTSServices.RulesService.DemographicIsHybrid(o);
- }
-
- // Utility
-
- /// <summary>
- /// Resets the stack display order for individual units; refreshes and resets unit stacks at the node.
- /// </summary>
- /// <param name="node"></param>
- public static void ResetUnitStackOrder(this INode node)
- {
- var tile = node.DefaultTile();
- var stacks = tile.GetAllComponentStacks();
- var updateUnits = new List<IUnit>();
-
- stacks.ForEach(us =>
- {
- int i = 1;
-
- us.GetAllUnits().ForEach(u =>
- {
- u.StackOrder = i; i++;
- updateUnits.Add(u);
- });
- });
-
- node.DefaultTile().ResetComponentStackDisplayOrder();
- node.TheGame().JTSServices.UnitService.UpdateUnits(updateUnits);
- }
-
- public static void ResetComponentStackDisplayOrder(this ITile tile)
- {
- int i = 1;
-
- tile.GetAllComponentStacks().Where(cs => cs.HasVisibleComponents).ToList().ForEach(cs =>
- {
- cs.DisplayOrder = i; i++;
- });
-
- tile.TheGame().JTSServices.TileService.UpdateTiles(new List<ITile>{tile});
- }
-
- public static bool HasUniqueHame(this IUnit unit)
- {
- return unit.TheGame().JTSServices.RulesService.UnitNameIsUnique(unit.Name).Result;
- }
-
- public static bool NullOrNone(this List<IBaseComponent> o)
- {
- return (o == null || o.Count == 0);
- }
-
- public static bool ExistsInContext(this IBaseComponent o)
- {
- return o.TheGame().JTSServices.GenericComponentService.ExistsInContext(o);
- }
-
-
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-
- /// <summary>
- /// Converts base data from one IServiceResult to another
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <typeparam name="T2"></typeparam>
- /// <param name="result1"></param>
- /// <param name="result2"></param>
- public static void ConvertServiceResultData<T, T2>(this IServiceResult<T> result1, IServiceResult<T2> result2)
- {
- result1.Messages = result2.Messages;
- result1.Status = result2.Status;
- result1.ex = result2.ex;
- }
-
- /// <summary>
- /// Converts an IResult to a comparable base service result of the same generic type
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="componentResult"></param>
- /// <param name="serviceResult"></param>
- /// <returns></returns>
- public static void ConvertServiceResultData<T>(this IServiceResult<T> serviceResult,
- IResult<T> componentResult)
- {
- serviceResult.Messages = componentResult.Messages;
- serviceResult.Status = componentResult.Status;
- serviceResult.ex = componentResult.ex;
- serviceResult.Result = componentResult.Result;
- serviceResult.FailedObjects = componentResult.FailedObjects;
- serviceResult.SuccessfulObjects = componentResult.SuccessfulObjects;
- }
-
- /// <summary>
- /// Converts an IResult to a comparable base component result of the same generic type
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="componentResult"></param>
- /// <param name="serviceResult"></param>
- /// <returns></returns>
- public static void ConvertServiceResultData<T>(this IResult<T> componentResult,
- IServiceResult<T> serviceResult)
- {
- componentResult.Messages = serviceResult.Messages;
- componentResult.Status = serviceResult.Status;
- componentResult.ex = serviceResult.ex;
- componentResult.Result = serviceResult.Result;
- componentResult.FailedObjects = serviceResult.FailedObjects;
- componentResult.SuccessfulObjects = serviceResult.SuccessfulObjects;
- }
-
- /// <summary>
- /// Converts a service result to an IResult of the same generic type
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="componentResult"></param>
- /// <param name="serviceResult"></param>
- /// <param name="services"></param>
- public static void ConvertServiceResultData<T>(this IResult<T> componentResult,
- IServiceResult<T> serviceResult,
- IServiceDependant services)
- {
- var temp = services.GenericComponentService.ConvertServiceResultDataToComponentResult(serviceResult);
-
- componentResult.ex = temp.ex;
- componentResult.FailedObjects = temp.FailedObjects;
- componentResult.Messages = temp.Messages;
- componentResult.Result = temp.Result;
- componentResult.Status = temp.Status;
- componentResult.SuccessfulObjects = temp.SuccessfulObjects;
- }
-
-
- /// <summary>
- /// Returns a new IResult converted from a service result of the same generic type
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="serviceResult"></param>
- /// <param name="services"></param>
- /// <returns></returns>
- public static IResult<T> ConvertServiceResultData<T>(this IServiceResult<T> serviceResult,
- IServiceDependant services)
- {
- return services.GenericComponentService.ConvertServiceResultDataToComponentResult<T>(serviceResult);
- }
-
- //public static ICoordinate TopLeftAsCoordinate(this Rectangle r) { return _services.ComponentService.CreateCoordinate(r.Left, r.Top, 0); }
- //public static ICoordinate BottomRightAsCoordinate(this Rectangle r) { return _services.ComponentService.CreateCoordinate(r.Right, r.Bottom, 0); }
-
-
- }
- }