# HG changeset patch # User IBBoard # Date 1250197760 0 # Node ID f7b9423c2a5a17a35d874a7799b4f0d5797f19a4 # Parent 38e7888591994c58f3dc5ef867139628011434aa Big mess of updates, breaking our rules on "commit little and often" because the code was so ugly. This revision will be broken for the WinForms UI, but as MonoDevelop/eSVN don't have a way of committing multiple projects in one go it can't be helped (Eclipse's Team Sync view could handle it) Fixes #122: Make usage of percentage or ratio common * All usage of ratio amounts for equipment items should now assume percentage * Properly calculate number taken for ratio selection (divide by 0 now we're using percentages) Fixes #118: Allow equipment amounts of "ratio" equipment to be define as absolute or ratio amounts * Added extra commands that differentiate between ratio and absolute amounts Fixes #120: Numeric limit equipment items show large percentages * Now made formatting treat ratios as percentages (don't multiply by 100) * Move string formatting to UnitEquipmentItem...Selection classes * Add method to Unit to say whether an equipment item is a numeric or ratio amount diff -r 38e788859199 -r f7b9423c2a5a IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Thu Aug 13 19:07:04 2009 +0000 +++ b/IBBoard.WarFoundry.API.csproj Thu Aug 13 21:09:20 2009 +0000 @@ -58,9 +58,9 @@ - + - + @@ -130,6 +130,10 @@ + + + + diff -r 38e788859199 -r f7b9423c2a5a api/Commands/AbstractReplaceUnitEquipmentCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Commands/AbstractReplaceUnitEquipmentCommand.cs Thu Aug 13 21:09:20 2009 +0000 @@ -0,0 +1,66 @@ +// This file (AbstractReplaceUnitEquipmentCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. + +using System; +using IBBoard.Commands; +using IBBoard.Lang; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Commands +{ + /// + /// An abstract implementation of the core method for replacing one equipment item with another + /// + public abstract class AbstractReplaceUnitEquipmentCommand : Command + { + private SetUnitEquipmentNumericAmountCommand removeOldCommand; + private AbstractSetUnitEquipmentAmountCommand addNewCommand; + + public AbstractReplaceUnitEquipmentCommand(Unit unit, UnitEquipmentItem oldItem, AbstractSetUnitEquipmentAmountCommand addNewEquipmentCommand) + { + //We can get away with a numeric amount here even if it is a ratio item because we're setting it to 0 + removeOldCommand = new SetUnitEquipmentNumericAmountCommand(unit, oldItem, 0); + addNewCommand = addNewEquipmentCommand; + } + + public override bool CanExecute() + { + return removeOldCommand.CanExecute() && addNewCommand.CanExecute(); + } + + public override string Description + { + get { return "Replace "+StringManipulation.CutToLength(removeOldCommand.EquipItem.Name, 20)+" with "+StringManipulation.CutToLength(addNewCommand.EquipItem.Name, 20)+ " for "+StringManipulation.CutToLength(removeOldCommand.Unit.Name, 20); } + } + + public override string UndoDescription + { + get { return "Replace "+StringManipulation.CutToLength(addNewCommand.EquipItem.Name, 20)+" with "+StringManipulation.CutToLength(removeOldCommand.EquipItem.Name, 20)+ " for "+StringManipulation.CutToLength(removeOldCommand.Unit.Name, 20); } + } + + public override bool Execute() + { + this.Redo(); + return true; + } + + public override void Redo() + { + removeOldCommand.Redo(); + addNewCommand.Redo(); + } + + public override void Undo() + { + addNewCommand.Undo(); + removeOldCommand.Undo(); + } + + + public override string Name + { + get { return "Replace required equipment"; } + } + } +} diff -r 38e788859199 -r f7b9423c2a5a api/Commands/AbstractSetUnitEquipmentAmountCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Commands/AbstractSetUnitEquipmentAmountCommand.cs Thu Aug 13 21:09:20 2009 +0000 @@ -0,0 +1,110 @@ +// This file (AbstractSetUnitEquipmentAmountCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. +// + +using System; +using IBBoard.Commands; +using IBBoard.Lang; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Commands +{ + /// + /// Abstract parent class for commands that set the amount of an equipment item a unit has to a fixed numeric or ratio value + /// + public abstract class AbstractSetUnitEquipmentAmountCommand : Command + { + private Unit unit; + private UnitEquipmentItem equip; + private double oldAmount; + private bool oldAmountWasRatio; + + public AbstractSetUnitEquipmentAmountCommand(Unit unit, UnitEquipmentItem item) + { + this.unit = unit; + equip = item; + oldAmount = unit.GetEquipmentAmount(equip); + oldAmountWasRatio = unit.GetEquipmentAmountIsRatio(equip); + } + + public override bool CanExecute() + { + return (unit!=null && equip!=null); + } + + public override string Description + { + get + { + return "Set " + StringManipulation.CutToLength(equip.Name, 20) + " ratio for " + StringManipulation.CutToLength(unit.Name, 20) + " to " + GetNewAmountString(); + } + } + + /// + /// Gets the string representation for the new amount of the equipment item to take + /// + /// + /// the string representation for the new amount of the equipment item to take + /// + protected abstract string GetNewAmountString(); + + public override string UndoDescription + { + get { + return "Set " + StringManipulation.CutToLength(equip.Name, 20) + " ratio for " + StringManipulation.CutToLength(unit.Name, 20) + " to " + GetOldAmountString(); + } + } + + /// + /// Gets the string representation for the old amount of the equipment item to take + /// + /// + /// the string representation for the old amount of the equipment item to take + /// + protected string GetOldAmountString() + { + string oldAmountString; + if (oldAmountWasRatio) + { + oldAmountString = UnitEquipmentRatioSelection.GetEquipmentAmountString(oldAmount); + } + else + { + oldAmountString = UnitEquipmentNumericSelection.GetEquipmentAmountString(oldAmount); + } + + return oldAmountString; + } + + + public override bool Execute() + { + this.Redo(); + return true; + } + + public override void Undo () + { + if (oldAmountWasRatio) + { + unit.SetEquipmentRatio(equip, oldAmount); + } + else + { + unit.SetEquipmentAmount(equip, (int)oldAmount); + } + } + + + public UnitEquipmentItem EquipItem + { + get { return equip; } + } + + public Unit Unit + { + get { return unit; } + } + } +} diff -r 38e788859199 -r f7b9423c2a5a api/Commands/ReplaceUnitEquipmentCommand.cs --- a/api/Commands/ReplaceUnitEquipmentCommand.cs Thu Aug 13 19:07:04 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -// This file (ReplaceUnitEquipmentCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. -// -// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. - -using System; -using IBBoard.Commands; -using IBBoard.Lang; -using IBBoard.WarFoundry.API.Objects; - -namespace IBBoard.WarFoundry.API.Commands -{ - /// - /// Summary description for ReplaceUnitEquipmentCommand. - /// - public class ReplaceUnitEquipmentCommand : Command - { - private SetUnitEquipmentAmountCommand removeOldCommand, addNewCommand; - - public ReplaceUnitEquipmentCommand(Unit unit, UnitEquipmentItem oldItem, UnitEquipmentItem newItem, double amount) - { - removeOldCommand = new SetUnitEquipmentAmountCommand(unit, oldItem, 0); - addNewCommand = new SetUnitEquipmentAmountCommand(unit, newItem, amount); - } - - public override bool CanExecute() - { - return removeOldCommand.CanExecute() && addNewCommand.CanExecute(); - } - - public override string Description - { - get { return "Replace "+StringManipulation.CutToLength(removeOldCommand.EquipItem.Name, 20)+" with "+StringManipulation.CutToLength(addNewCommand.EquipItem.Name, 20)+ " for "+StringManipulation.CutToLength(removeOldCommand.Unit.Name, 20); } - } - - public override string UndoDescription - { - get { return "Replace "+StringManipulation.CutToLength(addNewCommand.EquipItem.Name, 20)+" with "+StringManipulation.CutToLength(removeOldCommand.EquipItem.Name, 20)+ " for "+StringManipulation.CutToLength(removeOldCommand.Unit.Name, 20); } - } - - public override bool Execute() - { - this.Redo(); - return true; - } - - public override void Redo() - { - removeOldCommand.Redo(); - addNewCommand.Redo(); - } - - public override void Undo() - { - addNewCommand.Undo(); - removeOldCommand.Undo(); - } - - - public override string Name - { - get { return "Replace required equipment"; } - } - } -} diff -r 38e788859199 -r f7b9423c2a5a api/Commands/ReplaceUnitEquipmentWithNumericAmountItemCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Commands/ReplaceUnitEquipmentWithNumericAmountItemCommand.cs Thu Aug 13 21:09:20 2009 +0000 @@ -0,0 +1,21 @@ +// This file (ReplaceUnitEquipmentWithNumericAmountItemCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. +// + +using System; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Commands +{ + /// + /// A concrete implementation of an equipment replacing command that replaces a given equipment item with a different item that has an absolute numeric amount. + /// + public class ReplaceUnitEquipmentWithNumericAmountItemCommand : AbstractReplaceUnitEquipmentCommand + { + public ReplaceUnitEquipmentWithNumericAmountItemCommand(Unit unit, UnitEquipmentItem oldItem, UnitEquipmentItem newItem, int newItemAmount) : base(unit, oldItem, new SetUnitEquipmentNumericAmountCommand(unit, newItem, newItemAmount)) + { + //Do nothing special + } + } +} diff -r 38e788859199 -r f7b9423c2a5a api/Commands/ReplaceUnitEquipmentWithRatioAmountItemCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Commands/ReplaceUnitEquipmentWithRatioAmountItemCommand.cs Thu Aug 13 21:09:20 2009 +0000 @@ -0,0 +1,21 @@ +// This file (ReplaceUnitEquipmentWithRatioAmountItemCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. +// + +using System; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Commands +{ + /// + /// A concrete implementation of an equipment replacing command that replaces a given equipment item with a different item that has a ratio/percentage amount. + /// + public class ReplaceUnitEquipmentWithRatioAmountItemCommand : AbstractReplaceUnitEquipmentCommand + { + public ReplaceUnitEquipmentWithRatioAmountItemCommand(Unit unit, UnitEquipmentItem oldItem, UnitEquipmentItem newItem, double newItemRatio) : base(unit, oldItem, new SetUnitEquipmentRatioAmountCommand(unit, newItem, newItemRatio)) + { + //Do nothing special + } + } +} diff -r 38e788859199 -r f7b9423c2a5a api/Commands/SetUnitEquipmentAmountCommand.cs --- a/api/Commands/SetUnitEquipmentAmountCommand.cs Thu Aug 13 19:07:04 2009 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -// This file (SetUnitEquipmentAmountCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. -// -// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. - -using System; -using IBBoard.Commands; -using IBBoard.Lang; -using IBBoard.WarFoundry.API.Objects; - -namespace IBBoard.WarFoundry.API.Commands -{ - /// - /// Summary description for SetUnitEquipmentAmountCommand. - /// - public class SetUnitEquipmentAmountCommand : Command - { - private Unit unit; - private UnitEquipmentItem equip; - private double newAmount; - private double oldAmount; - - public SetUnitEquipmentAmountCommand(Unit unit, UnitEquipmentItem item, double amount) - { - this.unit = unit; - equip = item; - newAmount = amount; - oldAmount = unit.GetEquipmentAmount(equip.ID); - } - - public override bool CanExecute() - { - return (unit!=null && equip!=null); - } - - public override string Description - { - get { return "Set "+StringManipulation.CutToLength(equip.Name, 20)+" amount for "+StringManipulation.CutToLength(unit.Name, 20)+" to "+UnitEquipmentItem.FormatEquipmentAmount(equip, newAmount); } - } - - public override string UndoDescription - { - get { return "Set "+StringManipulation.CutToLength(equip.Name, 20)+" amount for "+StringManipulation.CutToLength(unit.Name, 20)+" to "+UnitEquipmentItem.FormatEquipmentAmount(equip, oldAmount); } - } - - public override bool Execute() - { - this.Redo(); - return true; - } - - public override void Redo() - { - if (equip.IsRatioLimit) - { - unit.SetEquipmentRatio(equip, newAmount); - } - else - { - unit.SetEquipmentAmount(equip, (int)newAmount); - } - } - - public override void Undo() - { - if (equip.IsRatioLimit) - { - unit.SetEquipmentRatio(equip, oldAmount); - } - else - { - unit.SetEquipmentAmount(equip, (int)oldAmount); - } - } - - public override string Name - { - get { return "Set equipment amount"; } - } - - public UnitEquipmentItem EquipItem - { - get { return equip; } - } - - public Unit Unit - { - get { return unit; } - } - } -} diff -r 38e788859199 -r f7b9423c2a5a api/Commands/SetUnitEquipmentNumericAmountCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Commands/SetUnitEquipmentNumericAmountCommand.cs Thu Aug 13 21:09:20 2009 +0000 @@ -0,0 +1,39 @@ +// This file (SetUnitEquipmentNumericAmountCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. + +using System; +using IBBoard.Commands; +using IBBoard.Lang; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Commands +{ + /// + /// Summary description for SetUnitEquipmentNumericAmountCommand. + /// + public class SetUnitEquipmentNumericAmountCommand : AbstractSetUnitEquipmentAmountCommand + { + private int newAmount; + + public SetUnitEquipmentNumericAmountCommand(Unit unit, UnitEquipmentItem item, int amount) : base(unit, item) + { + newAmount = amount; + } + + protected override string GetNewAmountString () + { + return UnitEquipmentNumericSelection.GetEquipmentAmountString(newAmount); + } + + public override void Redo() + { + Unit.SetEquipmentAmount(EquipItem, newAmount); + } + + public override string Name + { + get { return "Set equipment amount"; } + } + } +} diff -r 38e788859199 -r f7b9423c2a5a api/Commands/SetUnitEquipmentRatioAmountCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Commands/SetUnitEquipmentRatioAmountCommand.cs Thu Aug 13 21:09:20 2009 +0000 @@ -0,0 +1,39 @@ +// This file (SetUnitEquipmentRatioAmountCommand.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. + +using System; +using IBBoard.Commands; +using IBBoard.Lang; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Commands +{ + /// + /// Summary description for SetUnitEquipmentRatioAmountCommand. + /// + public class SetUnitEquipmentRatioAmountCommand : AbstractSetUnitEquipmentAmountCommand + { + private double newAmount; + + public SetUnitEquipmentRatioAmountCommand(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item) + { + newAmount = amount; + } + + protected override string GetNewAmountString () + { + return UnitEquipmentRatioSelection.GetEquipmentAmountString(newAmount); + } + + public override void Redo() + { + Unit.SetEquipmentRatio(EquipItem, newAmount); + } + + public override string Name + { + get { return "Set equipment ratio"; } + } + } +} diff -r 38e788859199 -r f7b9423c2a5a api/Objects/AbstractUnitEquipmentItemSelection.cs --- a/api/Objects/AbstractUnitEquipmentItemSelection.cs Thu Aug 13 19:07:04 2009 +0000 +++ b/api/Objects/AbstractUnitEquipmentItemSelection.cs Thu Aug 13 21:09:20 2009 +0000 @@ -76,5 +76,7 @@ { get; } + + public abstract string GetEquipmentAmountString(); } } diff -r 38e788859199 -r f7b9423c2a5a api/Objects/Unit.cs --- a/api/Objects/Unit.cs Thu Aug 13 19:07:04 2009 +0000 +++ b/api/Objects/Unit.cs Thu Aug 13 21:09:20 2009 +0000 @@ -268,6 +268,16 @@ { return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID)); } + + public bool GetEquipmentAmountIsRatio(UnitEquipmentItem item) + { + return (DictionaryUtils.GetValue(equipment, item) is UnitEquipmentRatioSelection); + } + + public bool GetEquipmentAmountIsRatio(string itemID) + { + return GetEquipmentAmountIsRatio(UnitType.GetEquipmentItem(itemID)); + } public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) { @@ -330,9 +340,9 @@ throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit"); } - if (ratio > 1) + if (ratio > 100) { - ratio = 1; + ratio = 100; } else if (ratio < 0) { diff -r 38e788859199 -r f7b9423c2a5a api/Objects/UnitEquipmentItem.cs --- a/api/Objects/UnitEquipmentItem.cs Thu Aug 13 19:07:04 2009 +0000 +++ b/api/Objects/UnitEquipmentItem.cs Thu Aug 13 21:09:20 2009 +0000 @@ -218,21 +218,5 @@ { return EquipmentItem.CanBeUsedWithArmourType(otherItemType); } - - public static string FormatEquipmentAmount(UnitEquipmentItem item, double amount) - { - if (amount == WarFoundryCore.INFINITY) - { - return "all"; //TODO: Translate - } - else if (item.IsRatioLimit) - { - return Math.Round(amount * 100) + "%"; - } - else - { - return amount.ToString(); - } - } } } diff -r 38e788859199 -r f7b9423c2a5a api/Objects/UnitEquipmentNumericSelection.cs --- a/api/Objects/UnitEquipmentNumericSelection.cs Thu Aug 13 19:07:04 2009 +0000 +++ b/api/Objects/UnitEquipmentNumericSelection.cs Thu Aug 13 21:09:20 2009 +0000 @@ -49,5 +49,35 @@ return isInRange; } + + public override string GetEquipmentAmountString () + { + return GetEquipmentAmountString(AmountTaken); + } + + /// + /// Gets a string representing the amount of equipment taken when the amount is a numeric selection + /// + /// + /// A + /// + /// + /// A + /// + public static string GetEquipmentAmountString(double amount) + { + string amountString; + + if (amount == WarFoundryCore.INFINITY) + { + amountString = "all"; //TODO: Translate + } + else + { + amountString = amount.ToString(); + } + + return amountString; + } } } diff -r 38e788859199 -r f7b9423c2a5a api/Objects/UnitEquipmentRatioSelection.cs --- a/api/Objects/UnitEquipmentRatioSelection.cs Thu Aug 13 19:07:04 2009 +0000 +++ b/api/Objects/UnitEquipmentRatioSelection.cs Thu Aug 13 21:09:20 2009 +0000 @@ -24,7 +24,7 @@ { get { - double numberTaken = AmountTaken * EquipmentForUnit.Size; + double numberTaken = (AmountTaken / 100) * EquipmentForUnit.Size; return (int) (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken)); } } @@ -33,5 +33,27 @@ { return (EquipmentItem.MinPercentage <= newValue) && (newValue <= EquipmentItem.MaxPercentage); } + + public override string GetEquipmentAmountString () + { + return GetEquipmentAmountString(AmountTaken); + } + + public static string GetEquipmentAmountString(double amount) + { + string amountString; + + if (amount == 100) + { + amountString = "all"; //TODO: Translate + } + else + { + amountString = amount + "%"; + } + + return amountString; + } + } }