Mercurial > repos > snowblizz-super-API-ideas
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 wrap: on
line diff
--- a/api/Objects/AbstractUnitEquipmentItemSelection.cs Sun Aug 09 10:34:09 2009 +0000 +++ b/api/Objects/AbstractUnitEquipmentItemSelection.cs Sun Aug 09 11:09:12 2009 +0000 @@ -19,7 +19,7 @@ { selectionForUnit = unit; selectedItem = item; - amountTaken = amount; + AmountTaken = amount; } public Unit EquipmentForUnit
--- a/api/Objects/Unit.cs Sun Aug 09 10:34:09 2009 +0000 +++ b/api/Objects/Unit.cs Sun Aug 09 11:09:12 2009 +0000 @@ -20,7 +20,7 @@ private Unit parentUnit; private double points; private ArmyCategory cat; - private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>(); + private Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection> equipment = new Dictionary<UnitEquipmentItem, AbstractUnitEquipmentItemSelection>(); private List<Unit> containedUnits = new List<Unit>(); public event DoubleValChangedDelegate PointsValueChanged; public event IntValChangedDelegate UnitSizeChanged; @@ -35,6 +35,23 @@ Size = startSize; SetInitialEquipment(); CalcCost(); + UnitEquipmentAmountChanged+= new DoubleValChangedDelegate(UnitEquipmentAmountChangedHandler); + UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChangedHandler); + } + + private void UnitEquipmentAmountChangedHandler(WarFoundryObject obj, double oldVal, double newVal) + { + CalcCost(); + } + + private void UnitSizeChangedHandler(WarFoundryObject obj, int oldVal, int newVal) + { + CalcCost(); + + if (HasDefaultName()) + { + OnNameChanged("", Name); + } } protected override string DefaultName() @@ -64,7 +81,14 @@ { if (CanEquipWithItem(unitEquip)) { - equipment[unitEquip] = unitEquip.MinNumber; + if (unitEquip.IsRatioLimit) + { + equipment[unitEquip] = new UnitEquipmentRatioSelection(this, unitEquip); + } + else + { + equipment[unitEquip] = new UnitEquipmentNumericSelection(this, unitEquip); + } } } } @@ -76,32 +100,9 @@ double oldpoints = points; points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost; - foreach (UnitEquipmentItem unitEquipItem in equipment.Keys) + foreach (AbstractUnitEquipmentItemSelection equipSelection in equipment.Values) { - double count = equipment[unitEquipItem]; - - if (unitEquipItem.IsRatioLimit) - { - if (unitEquipItem.RoundNumberUp) - { - points += Math.Ceiling(size * count) * unitEquipItem.Cost; - } - else - { - points += Math.Floor(size * count) * unitEquipItem.Cost; - } - } - else - { - if (count == WarFoundryCore.INFINITY) - { - points += size * unitEquipItem.Cost; - } - else - { - points += count * unitEquipItem.Cost; - } - } + points += equipSelection.TotalCost; } if (oldpoints!=points) @@ -129,7 +130,6 @@ { int oldValue = size; size = (value>0 ? value : 1); - CalcCost(); OnUnitSizeChanged(oldValue, size); } } @@ -253,7 +253,15 @@ public double GetEquipmentAmount(UnitEquipmentItem item) { - return DictionaryUtils.GetValue(equipment, item); + double amount = 0; + AbstractUnitEquipmentItemSelection selection = DictionaryUtils.GetValue(equipment, item); + + if (selection != null) + { + amount = selection.AmountTaken; + } + + return amount; } public double GetEquipmentAmount(string equipID) @@ -263,17 +271,34 @@ public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) { - if (equip.IsRatioLimit) - { - throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit"); - } - if (amount <1 && amount != WarFoundryCore.INFINITY) { amount = 0; } - SetEquipmentAmount(equip, (double)amount); + if (amount == 0) + { + RemoveEquipmentItem(equip); + } + else + { + double oldAmount = GetEquipmentAmount(equip); + + if (amount!=oldAmount) + { + if (oldAmount == 0) + { + equipment[equip] = new UnitEquipmentNumericSelection(this, equip, amount); + } + else + { + AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip); + currSelection.AmountTaken = amount; + } + + OnUnitEquipmentAmountChanged(equip, oldAmount, amount); + } + } } public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) @@ -292,31 +317,39 @@ ratio = 0; } - SetEquipmentAmount(equip, ratio); + if (ratio == 0) + { + RemoveEquipmentItem(equip); + } + else + { + double oldRatio = GetEquipmentAmount(equip); + + if (ratio != oldRatio) + { + if (oldRatio == 0) + { + equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio); + } + else + { + AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip); + currSelection.AmountTaken = ratio; + } + + OnUnitEquipmentAmountChanged(equip, oldRatio, ratio); + } + } } - private void SetEquipmentAmount(UnitEquipmentItem equip, double amount) + private void RemoveEquipmentItem(UnitEquipmentItem equip) { - double oldAmount = 0; - - if (equipment.ContainsKey(equip)) - { - oldAmount = equipment[equip]; - } - - if (amount!=oldAmount) + double oldAmount = GetEquipmentAmount(equip); + + if (oldAmount != 0) { - if (amount != 0) - { - equipment[equip] = amount; - } - else - { - equipment.Remove(equip); - } - - OnUnitEquipmentAmountChanged(equip, oldAmount, amount); - CalcCost(); + equipment.Remove(equip); + OnUnitEquipmentAmountChanged(equip, oldAmount, 0); } }
--- a/api/Objects/UnitEquipmentItem.cs Sun Aug 09 10:34:09 2009 +0000 +++ b/api/Objects/UnitEquipmentItem.cs Sun Aug 09 11:09:12 2009 +0000 @@ -220,10 +220,10 @@ } public static string FormatEquipmentAmount(UnitEquipmentItem item, double amount) - { - if (amount == WarFoundryCore.INFINITY) - { - return "all"; //TODO: Translate + { + if (amount == WarFoundryCore.INFINITY) + { + return "all"; //TODO: Translate } else if (item.IsRatioLimit) {
--- a/api/Objects/UnitEquipmentNumericSelection.cs Sun Aug 09 10:34:09 2009 +0000 +++ b/api/Objects/UnitEquipmentNumericSelection.cs Sun Aug 09 11:09:12 2009 +0000 @@ -20,13 +20,18 @@ { get { - return AmountTaken * EquipmentItem.Cost; + return CalculateAmount() * EquipmentItem.Cost; } } + + private double CalculateAmount() + { + return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : AmountTaken); + } protected override bool IsValidValue (double newValue) { - return newValue = Math.Round(newValue); + return newValue == Math.Round(newValue); } } }