changeset 96:ced5a18d9f52

Re #118: Allow equipment amounts of "ratio" equipment to be define as absolute or ratio amounts * Rework Unit's internals on how it stores equipment selection amounts * Fix logic error in "IsValidValue()" for absolute equipment selections * Fix calculation error when using "equip all" for absolute equipment selections * Make abstract equipment selection object set amount taken using property to trigger value checking Also: * Line ending cleanup in UnitEquipmentItem
author IBBoard <dev@ibboard.co.uk>
date Sun, 09 Aug 2009 11:09:12 +0000
parents 6c6cfe5594fc
children 95746083d037
files api/Objects/AbstractUnitEquipmentItemSelection.cs api/Objects/Unit.cs api/Objects/UnitEquipmentItem.cs api/Objects/UnitEquipmentNumericSelection.cs
diffstat 4 files changed, 101 insertions(+), 63 deletions(-) [+]
line diff
     1.1 --- a/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Aug 09 10:34:09 2009 +0000
     1.2 +++ b/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Aug 09 11:09:12 2009 +0000
     1.3 @@ -19,7 +19,7 @@
     1.4  		{
     1.5  			selectionForUnit = unit;
     1.6  			selectedItem = item;
     1.7 -			amountTaken = amount;
     1.8 +			AmountTaken = amount;
     1.9  		}
    1.10  		
    1.11  		public Unit EquipmentForUnit
     2.1 --- a/api/Objects/Unit.cs	Sun Aug 09 10:34:09 2009 +0000
     2.2 +++ b/api/Objects/Unit.cs	Sun Aug 09 11:09:12 2009 +0000
     2.3 @@ -20,7 +20,7 @@
     2.4  		private Unit parentUnit;
     2.5  		private double points;
     2.6  		private ArmyCategory cat;
     2.7 -		private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>();
     2.8 +		private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>();
     2.9  		private List<Unit> containedUnits = new List<Unit>();
    2.10  		public event DoubleValChangedDelegate PointsValueChanged;
    2.11  		public event IntValChangedDelegate UnitSizeChanged;
    2.12 @@ -35,6 +35,23 @@
    2.13  			Size = startSize;
    2.14  			SetInitialEquipment();
    2.15  			CalcCost();
    2.16 +			UnitEquipmentAmountChanged+= new DoubleValChangedDelegate(UnitEquipmentAmountChangedHandler);
    2.17 +			UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChangedHandler);
    2.18 +		}
    2.19 +		
    2.20 +		private void UnitEquipmentAmountChangedHandler(WarFoundryObject obj, double oldVal, double newVal)
    2.21 +		{
    2.22 +			CalcCost();
    2.23 +		}
    2.24 +		
    2.25 +		private void UnitSizeChangedHandler(WarFoundryObject obj, int oldVal, int newVal)
    2.26 +		{
    2.27 +			CalcCost();
    2.28 +			
    2.29 +			if (HasDefaultName())
    2.30 +			{
    2.31 +				OnNameChanged("", Name);
    2.32 +			}
    2.33  		}
    2.34  
    2.35  		protected override string DefaultName()
    2.36 @@ -64,7 +81,14 @@
    2.37  				{
    2.38  					if (CanEquipWithItem(unitEquip))
    2.39  					{
    2.40 -						equipment[unitEquip] = unitEquip.MinNumber;
    2.41 +						if (unitEquip.IsRatioLimit)
    2.42 +						{
    2.43 +							equipment[unitEquip] = new UnitEquipmentRatioSelection(this, unitEquip);
    2.44 +						}
    2.45 +						else
    2.46 +						{
    2.47 +							equipment[unitEquip] = new UnitEquipmentNumericSelection(this, unitEquip);
    2.48 +						}
    2.49  					}
    2.50  				}
    2.51  			}
    2.52 @@ -76,32 +100,9 @@
    2.53  			double oldpoints = points;
    2.54  			points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost;
    2.55  
    2.56 -			foreach (UnitEquipmentItem unitEquipItem in equipment.Keys)
    2.57 +			foreach (AbstractUnitEquipmentItemSelection equipSelection in equipment.Values)
    2.58  			{
    2.59 -				double count = equipment[unitEquipItem];
    2.60 -				
    2.61 -				if (unitEquipItem.IsRatioLimit)
    2.62 -				{
    2.63 -					if (unitEquipItem.RoundNumberUp)
    2.64 -					{
    2.65 -						points += Math.Ceiling(size * count) * unitEquipItem.Cost;
    2.66 -					}
    2.67 -					else
    2.68 -					{
    2.69 -						points += Math.Floor(size * count) * unitEquipItem.Cost;
    2.70 -					}
    2.71 -				}
    2.72 -				else
    2.73 -				{
    2.74 -					if (count == WarFoundryCore.INFINITY)
    2.75 -					{
    2.76 -						points += size * unitEquipItem.Cost;
    2.77 -					}
    2.78 -					else
    2.79 -					{
    2.80 -						points += count * unitEquipItem.Cost;
    2.81 -					}
    2.82 -				}
    2.83 +				points += equipSelection.TotalCost;
    2.84  			}
    2.85  
    2.86  			if (oldpoints!=points)
    2.87 @@ -129,7 +130,6 @@
    2.88  				{
    2.89  					int oldValue = size;
    2.90  					size = (value>0 ? value : 1);
    2.91 -					CalcCost();
    2.92  					OnUnitSizeChanged(oldValue, size);
    2.93  				}
    2.94  			}
    2.95 @@ -253,7 +253,15 @@
    2.96  
    2.97  		public double GetEquipmentAmount(UnitEquipmentItem item)
    2.98  		{
    2.99 -			return DictionaryUtils.GetValue(equipment, item);
   2.100 +			double amount = 0;
   2.101 +			AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue(equipment, item);
   2.102 +			
   2.103 +			if (selection != null)
   2.104 +			{
   2.105 +				amount = selection.AmountTaken;
   2.106 +			}
   2.107 +			
   2.108 +			return amount;
   2.109  		}
   2.110  
   2.111  		public double GetEquipmentAmount(string equipID)
   2.112 @@ -263,17 +271,34 @@
   2.113  		
   2.114  		public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
   2.115  		{
   2.116 -			if (equip.IsRatioLimit)
   2.117 -			{
   2.118 -				throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit");
   2.119 -			}
   2.120 -			
   2.121  			if (amount <1 && amount != WarFoundryCore.INFINITY)
   2.122  			{
   2.123  				amount = 0;
   2.124  			}
   2.125  			
   2.126 -			SetEquipmentAmount(equip, (double)amount);
   2.127 +			if (amount == 0)
   2.128 +			{
   2.129 +				RemoveEquipmentItem(equip);
   2.130 +			}
   2.131 +			else
   2.132 +			{
   2.133 +				double oldAmount = GetEquipmentAmount(equip);
   2.134 +	
   2.135 +				if (amount!=oldAmount)
   2.136 +				{
   2.137 +					if (oldAmount == 0)
   2.138 +					{
   2.139 +						equipment[equip] = new UnitEquipmentNumericSelection(this, equip, amount);
   2.140 +					}
   2.141 +					else
   2.142 +					{
   2.143 +						AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
   2.144 +						currSelection.AmountTaken = amount;
   2.145 +					}
   2.146 +	
   2.147 +					OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
   2.148 +				}
   2.149 +			}
   2.150  		}
   2.151  		
   2.152  		public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
   2.153 @@ -292,31 +317,39 @@
   2.154  				ratio = 0;
   2.155  			}
   2.156  			
   2.157 -			SetEquipmentAmount(equip, ratio);
   2.158 +			if (ratio == 0)
   2.159 +			{
   2.160 +				RemoveEquipmentItem(equip);
   2.161 +			}
   2.162 +			else
   2.163 +			{
   2.164 +				double oldRatio = GetEquipmentAmount(equip);
   2.165 +	
   2.166 +				if (ratio != oldRatio)
   2.167 +				{
   2.168 +					if (oldRatio == 0)
   2.169 +					{
   2.170 +						equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio);
   2.171 +					}
   2.172 +					else
   2.173 +					{
   2.174 +						AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
   2.175 +						currSelection.AmountTaken = ratio;
   2.176 +					}
   2.177 +	
   2.178 +					OnUnitEquipmentAmountChanged(equip, oldRatio, ratio);
   2.179 +				}
   2.180 +			}
   2.181  		}
   2.182  		
   2.183 -		private void SetEquipmentAmount(UnitEquipmentItem equip, double amount)
   2.184 +		private void RemoveEquipmentItem(UnitEquipmentItem equip)
   2.185  		{
   2.186 -			double oldAmount = 0;
   2.187 -
   2.188 -			if (equipment.ContainsKey(equip))
   2.189 +			double oldAmount = GetEquipmentAmount(equip);
   2.190 +		
   2.191 +			if (oldAmount != 0)
   2.192  			{
   2.193 -				oldAmount = equipment[equip];
   2.194 -			}
   2.195 -
   2.196 -			if (amount!=oldAmount)
   2.197 -			{
   2.198 -				if (amount != 0)
   2.199 -				{
   2.200 -					equipment[equip] = amount;
   2.201 -				}
   2.202 -				else
   2.203 -				{
   2.204 -					equipment.Remove(equip);
   2.205 -				}
   2.206 -
   2.207 -				OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
   2.208 -				CalcCost();
   2.209 +				equipment.Remove(equip);
   2.210 +				OnUnitEquipmentAmountChanged(equip, oldAmount, 0);
   2.211  			}
   2.212  		}
   2.213  		
     3.1 --- a/api/Objects/UnitEquipmentItem.cs	Sun Aug 09 10:34:09 2009 +0000
     3.2 +++ b/api/Objects/UnitEquipmentItem.cs	Sun Aug 09 11:09:12 2009 +0000
     3.3 @@ -220,10 +220,10 @@
     3.4  		}
     3.5  		
     3.6  		public static string FormatEquipmentAmount(UnitEquipmentItem item, double amount)
     3.7 -		{
     3.8 -			if (amount == WarFoundryCore.INFINITY)
     3.9 -			{
    3.10 -				return "all"; //TODO: Translate
    3.11 +		{
    3.12 +			if (amount == WarFoundryCore.INFINITY)
    3.13 +			{
    3.14 +				return "all"; //TODO: Translate
    3.15  			}
    3.16  			else if (item.IsRatioLimit)
    3.17  			{
     4.1 --- a/api/Objects/UnitEquipmentNumericSelection.cs	Sun Aug 09 10:34:09 2009 +0000
     4.2 +++ b/api/Objects/UnitEquipmentNumericSelection.cs	Sun Aug 09 11:09:12 2009 +0000
     4.3 @@ -20,13 +20,18 @@
     4.4  		{
     4.5  			get
     4.6  			{
     4.7 -				return AmountTaken * EquipmentItem.Cost;
     4.8 +				return CalculateAmount() * EquipmentItem.Cost;
     4.9  			}
    4.10  		}
    4.11 +					
    4.12 +		private double CalculateAmount()
    4.13 +		{
    4.14 +			return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : AmountTaken);
    4.15 +		}
    4.16  		
    4.17  		protected override bool IsValidValue (double newValue)
    4.18  		{
    4.19 -			return newValue = Math.Round(newValue);
    4.20 +			return newValue == Math.Round(newValue);
    4.21  		}
    4.22  	}
    4.23  }