|
Britbot
|
00001 #region #Usings 00002 00003 using System; 00004 using System.Collections.Generic; 00005 using System.Collections.ObjectModel; 00006 using Pirates; 00007 00008 #endregion 00009 00010 namespace Britbot 00011 { 00015 public static class Extensions 00016 { 00023 public static T[] Flatten<T>(this T[][] jaggedAray) 00024 { 00025 List<T> flatList = new List<T>(); 00026 00027 for (int i = 0; i < jaggedAray.Length; i++) 00028 { 00029 for (int k = 0; k < jaggedAray[i].Length; k++) 00030 { 00031 flatList.Add(jaggedAray[i][k]); 00032 } 00033 } 00034 00035 return flatList.ToArray(); 00036 } 00037 00038 public static int Distance(this IPirateGame game, Location loc, SmartIsland isle) 00039 { 00040 return Bot.Game.Distance(loc, isle.Loc); 00041 } 00042 00051 public static double EuclidianDistanceSquared(this IPirateGame game, Location loc1, Location loc2) 00052 { 00053 return Math.Pow(loc1.Col - loc2.Col, 2) + Math.Pow(loc1.Row - loc2.Row, 2); 00054 } 00055 00063 public static bool IsReallyInRange(this IPirateGame game, Location loc1, Location loc2) 00064 { 00065 return game.EuclidianDistanceSquared(loc1, loc2) <= game.GetAttackRadius(); 00066 } 00067 00077 public static bool IsPassableEnough(this IPirateGame game, Location loc, Group group) 00078 { 00079 //Logger.BeginTime("IsPassableEnough"); 00080 //going over all the pirates in the group 00081 foreach (int pirate in group.Pirates) 00082 { 00083 //calculate differance vector between the Group's center and the given pirate 00084 HeadingVector difference = HeadingVector.CalcDifference(group.FindCenter(true) 00085 , Bot.Game.GetMyPirate(pirate).Loc); 00086 00087 //calculate the location of this pirate if the group is placed in loc 00088 Location newLocation = HeadingVector.AddvanceByVector(loc, difference); 00089 00090 //check if isn't passable, if so return false 00091 if (!game.IsInMap(newLocation) || !game.IsPassable(newLocation)) 00092 return false; 00093 } 00094 //Logger.StopTime("IsPassableEnough"); 00095 //otherwise return true since all the pirates can fit here 00096 return true; 00097 /* 00098 //go over the locations and check if they are passable. 00099 for (int deltaX = -passRadius; deltaX <= passRadius; deltaX++) 00100 { 00101 for (int deltaY = -passRadius + Math.Abs(deltaX); deltaY <= passRadius - Math.Abs(deltaX); deltaY++) 00102 { 00103 Location testLocation = new Location(loc.Row + deltaY, loc.Col + deltaX); 00104 00105 //check if passable 00106 if (!game.IsInMap(testLocation) || !game.IsPassable(testLocation)) 00107 return false; 00108 } 00109 } 00110 */ 00111 //return true if Ok 00112 } 00113 00114 public static bool IsInMap(this IPirateGame game, Location testLocation) 00115 { 00116 int mapRow = game.GetRows(); 00117 int mapCol = game.GetCols(); 00118 00119 //check if outside of the map 00120 if (testLocation.Row >= mapRow || testLocation.Col >= mapCol || 00121 testLocation.Row < 0 || testLocation.Col < 0) 00122 return false; 00123 00124 return true; 00125 } 00126 00132 public static bool IsActuallyPassable(this Location loc) 00133 { 00134 return !Bot.Game.IsOccupied(loc) && Bot.Game.IsPassable(loc); 00135 } 00136 00143 public static Location Subtract(this Location loc1, Location loc2) 00144 { 00145 return new Location(loc1.Row - loc2.Row, loc2.Col - loc2.Col); 00146 } 00147 00153 public static Location AdvancePivot(this Location pivot) 00154 { 00155 //this basically moves the location closer to the center of the map 00156 int maxCols = Bot.Game.GetCols(); 00157 int maxRows = Bot.Game.GetRows(); 00158 00159 int addCol = 0, addRow = 0; 00160 int deltaCol = maxCols - pivot.Col; 00161 int deltaRow = maxRows - pivot.Row; 00162 00163 if (deltaCol > pivot.Col) 00164 addCol++; 00165 else if (deltaCol < pivot.Col) 00166 addCol--; 00167 00168 if (deltaRow > pivot.Row) 00169 addRow++; 00170 else if (deltaRow < pivot.Row) 00171 addRow--; 00172 00173 pivot = new Location(pivot.Row + addRow, pivot.Col + addCol); 00174 return pivot; 00175 } 00176 00177 public static void AddRange<TSource>(this ObservableCollection<TSource> source, IEnumerable<TSource> items) 00178 { 00179 foreach (var item in items) 00180 { 00181 source.Add(item); 00182 } 00183 } 00184 00185 public static void RemoveAll<T>(this ObservableCollection<T> collection, Func<T, bool> condition) 00186 { 00187 for (int i = collection.Count - 1; i >= 0; i--) 00188 { 00189 if (condition(collection[i])) 00190 { 00191 collection.RemoveAt(i); 00192 } 00193 } 00194 } 00195 } 00196 00197 }
1.7.6.1