ZoneManhattanMap.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using CommonAI.RTS.Manhattan;
  2. using CommonLang;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Collections;
  8. namespace CommonAI.Zone.Helper
  9. {
  10. public class ZoneManhattanMap : Disposable, IManhattanMap
  11. {
  12. public ZoneInfo Data { get; private set; }
  13. public int[,] Terrain { get; private set; }
  14. readonly private float TotalW, TotalH;
  15. readonly private HashMap<int, TerrainDefinitionMap.MapBlockBrush> Brushes;
  16. public ZoneManhattanMap(ZoneInfo info, TerrainDefinitionMap define)
  17. {
  18. this.Data = info;
  19. this.TotalW = info.TotalWidth;
  20. this.TotalH = info.TotalHeight;
  21. this.Terrain = info.TerrainMatrix;
  22. this.Brushes = new HashMap<int, TerrainDefinitionMap.MapBlockBrush>((define != null) ? define.Brushes.Count : 0);
  23. if (define != null)
  24. {
  25. foreach (var b in define.Brushes)
  26. {
  27. Brushes.Put(b.Value, b);
  28. }
  29. }
  30. }
  31. protected override void Disposing()
  32. {
  33. Brushes.Clear();
  34. Data = null;
  35. Terrain = null;
  36. }
  37. public int XCount { get { return Data.mXCount; } }
  38. public int YCount { get { return Data.mYCount; } }
  39. public float CellW { get { return Data.mGridCellW; } }
  40. public float CellH { get { return Data.mGridCellH; } }
  41. public float TotalWidth { get { return TotalW; } }
  42. public float TotalHeight { get { return TotalH; } }
  43. public virtual bool TestBlock(int bx, int by)
  44. {
  45. TerrainDefinitionMap.MapBlockBrush brush;
  46. if (Terrain[bx, by] != 0)
  47. {
  48. if (Brushes.TryGetValue(Terrain[bx, by], out brush))
  49. {
  50. return brush.IsBlock;
  51. }
  52. return true;
  53. }
  54. return false;
  55. }
  56. public bool TestBlockValue(int value)
  57. {
  58. TerrainDefinitionMap.MapBlockBrush brush;
  59. if (value != 0)
  60. {
  61. if (Brushes.TryGetValue(value, out brush))
  62. {
  63. return brush.IsBlock;
  64. }
  65. return true;
  66. }
  67. return false;
  68. }
  69. public virtual int GetValue(int bx, int by)
  70. {
  71. return Terrain[bx, by];
  72. }
  73. public virtual bool SetValue(int bx, int by, int value)
  74. {
  75. if (value != Terrain[bx, by])
  76. {
  77. Terrain[bx, by] = value;
  78. return true;
  79. }
  80. return false;
  81. }
  82. public virtual AstarManhattan.MMapNode CreateMapNode()
  83. {
  84. return new ZoneMapNode();
  85. }
  86. }
  87. public class ZoneMapNode : AstarManhattan.MMapNode
  88. {
  89. private BindArea bind_area;
  90. public ZoneClient.ZoneEditorArea ClientArea
  91. {
  92. get { return (bind_area == null) ? null : bind_area.client_area; }
  93. }
  94. public Instance.ZoneArea ServerArea
  95. {
  96. get { return (bind_area == null) ? null : bind_area.server_area; }
  97. }
  98. public void SetClientArea(ZoneClient.ZoneEditorArea area)
  99. {
  100. if (bind_area == null) { bind_area = new BindArea(); }
  101. bind_area.client_area = area;
  102. }
  103. public void SetServerArea(Instance.ZoneArea area)
  104. {
  105. if (bind_area == null) { bind_area = new BindArea(); }
  106. bind_area.server_area = area;
  107. }
  108. public class BindArea
  109. {
  110. public ZoneClient.ZoneEditorArea client_area;
  111. public Instance.ZoneArea server_area;
  112. }
  113. }
  114. public class ManhattanMapAreaGenerator
  115. {
  116. public class MapNodeIndex
  117. {
  118. public readonly int BX;
  119. public readonly int BY;
  120. public MapNodeIndex(int bx, int by)
  121. {
  122. this.BX = bx;
  123. this.BY = by;
  124. }
  125. public override bool Equals(object obj)
  126. {
  127. if (obj is MapNodeIndex)
  128. {
  129. var src = (MapNodeIndex)obj;
  130. return src.BX == this.BX && src.BY == this.BY;
  131. }
  132. return base.Equals(obj);
  133. }
  134. public override int GetHashCode()
  135. {
  136. return base.GetHashCode();
  137. }
  138. }
  139. /// <summary>
  140. /// 扫描地块区域结果
  141. /// </summary>
  142. public class ContinuousMapNode : IEnumerable<MapNodeIndex>
  143. {
  144. private readonly ZoneInfo map;
  145. private readonly int bsx, bsy, bdx, bdy;
  146. private readonly int value;
  147. private readonly List<MapNodeIndex> nodes;
  148. private readonly MapNodeIndex[,] node_matrix;
  149. public int Value { get { return value; } }
  150. internal ContinuousMapNode(ZoneInfo map, int bx, int by, int xr, int yr)
  151. {
  152. this.map = map;
  153. this.value = map.TerrainMatrix[bx, by];
  154. this.nodes = new List<MapNodeIndex>();
  155. this.bsx = bx - xr;
  156. this.bsy = by - yr;
  157. this.bdx = bx + xr;
  158. this.bdy = by + yr;
  159. this.node_matrix = new MapNodeIndex[xr + 1 + xr, yr + 1 + yr];
  160. }
  161. public bool IsInRect(int bx, int by)
  162. {
  163. if (bx >= this.bsx && bx <= this.bdx &&
  164. by >= this.bsy && by <= this.bdy)
  165. {
  166. return true;
  167. }
  168. return false;
  169. }
  170. public bool IsInRect(float x, float y)
  171. {
  172. int bx = (int)(x / map.GridCellW);
  173. int by = (int)(y / map.GridCellH);
  174. return IsInRect(bx, by);
  175. }
  176. public MapNodeIndex GetIndex(int bx, int by)
  177. {
  178. if (IsInRect(bx, by))
  179. {
  180. return node_matrix[bx - bsx, by - bsy];
  181. }
  182. return null;
  183. }
  184. public bool Contains(MapNodeIndex src)
  185. {
  186. return GetIndex(src.BX, src.BY) != null;
  187. }
  188. public bool TryAdd(MapNodeIndex src)
  189. {
  190. if (IsInRect(src.BX, src.BY) && (node_matrix[src.BX - bsx, src.BY - bsy] == null))
  191. {
  192. nodes.Add(src);
  193. node_matrix[src.BX - bsx, src.BY - bsy] = src;
  194. return true;
  195. }
  196. return false;
  197. }
  198. public IEnumerator<MapNodeIndex> GetEnumerator()
  199. {
  200. return nodes.GetEnumerator();
  201. }
  202. IEnumerator IEnumerable.GetEnumerator()
  203. {
  204. return nodes.GetEnumerator();
  205. }
  206. }
  207. private static int[][] near_table_cross = new int[][]
  208. {
  209. new int[]{ 0,-1},
  210. new int[]{-1, 0},
  211. new int[]{ 1, 0},
  212. new int[]{ 0, 1},
  213. };
  214. private ZoneInfo map;
  215. public ManhattanMapAreaGenerator(ZoneInfo map)
  216. {
  217. this.map = map;
  218. }
  219. public bool TryGetValue(int bx, int by, out int value)
  220. {
  221. if (bx >= 0 && bx < map.XCount && by >= 0 && by < map.YCount)
  222. {
  223. value = map.TerrainMatrix[bx, by];
  224. return true;
  225. }
  226. value = 0;
  227. return false;
  228. }
  229. public bool TryGetValue(float x, float y, out int value)
  230. {
  231. int bx = (int)(x / map.GridCellW);
  232. int by = (int)(y / map.GridCellH);
  233. return TryGetValue(bx, by, out value);
  234. }
  235. public bool TryGetIndex(int bx, int by, int value, out MapNodeIndex index)
  236. {
  237. if (bx >= 0 && bx < map.XCount && by >= 0 && by < map.YCount && map.TerrainMatrix[bx, by] == value)
  238. {
  239. index = new MapNodeIndex(bx, by);
  240. return true;
  241. }
  242. index = new MapNodeIndex(-1, -1);
  243. return false;
  244. }
  245. public bool TryGetIndex(float x, float y, int value, out MapNodeIndex index)
  246. {
  247. int bx = (int)(x / map.GridCellW);
  248. int by = (int)(y / map.GridCellH);
  249. return TryGetIndex(bx, by, value, out index);
  250. }
  251. /// <summary>
  252. ///
  253. /// </summary>
  254. /// <param name="x">中心点</param>
  255. /// <param name="y">中心点</param>
  256. /// <param name="xr">x半径</param>
  257. /// <param name="yr">y半径</param>
  258. /// <returns></returns>
  259. public ContinuousMapNode GetContinuousMapNode(float x, float y, float xr, float yr)
  260. {
  261. return GetContinuousMapNode(
  262. (int)(x / map.GridCellW),
  263. (int)(y / map.GridCellH),
  264. (int)(xr / map.GridCellW),
  265. (int)(yr / map.GridCellH));
  266. }
  267. public virtual ContinuousMapNode GetContinuousMapNode(int bx, int by, int xr, int yr)
  268. {
  269. if (bx >= 0 && bx < map.XCount && by >= 0 && by < map.YCount)
  270. {
  271. ContinuousMapNode list = new ContinuousMapNode(map, bx, by, xr, yr);
  272. MapNodeIndex src = new MapNodeIndex(bx, by);
  273. if (list.TryAdd(src))
  274. {
  275. Stack<MapNodeIndex> stack = new Stack<MapNodeIndex>((xr * 2) * (yr * 2));
  276. stack.Push(src);
  277. while (stack.Count > 0)
  278. {
  279. MapNodeIndex cur = stack.Pop();
  280. for (int i = 0; i < near_table_cross.Length; i++)
  281. {
  282. MapNodeIndex next;
  283. if (TryGetIndex(cur.BX + near_table_cross[i][0], cur.BY + near_table_cross[i][1], list.Value, out next))
  284. {
  285. if (list.TryAdd(next))
  286. {
  287. stack.Push(next);
  288. }
  289. }
  290. }
  291. }
  292. return list;
  293. }
  294. }
  295. return null;
  296. }
  297. }
  298. }