annotate api/Util/UnitEquipmentUtil.cs @ 174:33433862467f

* Make use of local variable no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Tue, 20 Oct 2009 19:14:30 +0000
parents 624422e91a1c
children 6fe336109128
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (UnitEquipmentUtil.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Text;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using IBBoard.WarFoundry.API.Objects;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 namespace IBBoard.WarFoundry.API.Util
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
162
624422e91a1c Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents: 161
diff changeset
12 public class UnitEquipmentUtil
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 /// <summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 /// Gets an array of allowed <see cref="UnitEquipmentItem"/>s based on the current selections of the unit, taking in to account Mutex groups and other limits.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 /// </summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 /// <param name="unit">The <see cref="Unit"/> to get equipment items for</param>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 /// <returns>The array of allowed <see cref="UnitEquipmentItem"/>s</returns>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 public static UnitEquipmentItem[] GetAllowedEquipmentItems(Unit unit)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 UnitEquipmentItem[] currItems = unit.GetEquipment();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 foreach (UnitEquipmentItem item in GetAllEquipmentItems(unit))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 bool allowed = IsAllowedByMutex(item, currItems);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 if (allowed)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 list.Add(item);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 return list.ToArray();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 private static bool IsAllowedByMutex(UnitEquipmentItem item, UnitEquipmentItem[] currItems)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 bool allowed = true;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 foreach (UnitEquipmentItem currItem in currItems)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 if (ItemsAreMutuallyExclusive(currItem, item))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 allowed = false;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 break;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 return allowed;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 /// <summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 /// Gets a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code> because of mutex groups.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 /// </summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 /// <param name="unit">The unit that wants to take the equipment item</param>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 /// <param name="item">The item to check blocking items for</param>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 /// <returns>a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code></returns>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 public static List<UnitEquipmentItem> GetBlockingEquipmentItems(Unit unit, UnitEquipmentItem item)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 UnitEquipmentItem[] currItems = unit.GetEquipment();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63
174
33433862467f * Make use of local variable
IBBoard <dev@ibboard.co.uk>
parents: 162
diff changeset
64 foreach (UnitEquipmentItem unitItem in currItems)
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 if (!ItemsAreMutuallyExclusive(unitItem, item))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 items.Add(unitItem);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 return items;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 public static UnitEquipmentItem[] GetAllEquipmentItems(Unit unit)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 return unit.UnitType.GetEquipmentItems();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 public static bool ItemsAreMutuallyExclusive(UnitEquipmentItem item1, UnitEquipmentItem item2)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 bool areMutex = false;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 string[] item1mutex = item1.MutexGroups;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 string[] item2mutex = item2.MutexGroups;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 foreach (string mutex in item1mutex)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 foreach (string otherMutex in item2mutex)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 if (mutex.Equals(otherMutex))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 areMutex = true;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 goto postLoop;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 postLoop:
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 return areMutex;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 }