RTSManhattan.Map.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonAI.RTS;
  5. using CommonLang.Vector;
  6. using CommonLang;
  7. using CommonLang.Concurrent;
  8. namespace CommonAI.RTS.Manhattan
  9. {
  10. partial class AstarManhattan
  11. {
  12. //---------------------------------------------------------------------------------------------------------------------
  13. internal class MTerrain : ITerrain<MMapNode>
  14. {
  15. internal readonly IManhattanMap map_data;
  16. internal readonly AstarManhattan manhattan;
  17. internal readonly bool is_inclined;
  18. internal readonly int xcount;
  19. internal readonly int ycount;
  20. private int nodes_count;
  21. private MMapNode[,] all_nodes_map;
  22. public int TotalNodeCount
  23. {
  24. get { return nodes_count; }
  25. }
  26. internal MTerrain(AstarManhattan manhattan, IManhattanMap map, bool inclined)
  27. {
  28. this.manhattan = manhattan;
  29. this.map_data = map;
  30. this.is_inclined = inclined;
  31. this.xcount = map.XCount;
  32. this.ycount = map.YCount;
  33. this.nodes_count = 0;
  34. this.all_nodes_map = new MMapNode[xcount, ycount];
  35. for (int y = 0; y < ycount; y++)
  36. {
  37. for (int x = 0; x < xcount; x++)
  38. {
  39. if (map.TestBlock(x, y) == false)
  40. {
  41. all_nodes_map[x, y] = map.CreateMapNode();
  42. nodes_count++;
  43. }
  44. }
  45. }
  46. for (int y = 0; y < ycount; y++)
  47. {
  48. for (int x = 0; x < xcount; x++)
  49. {
  50. MMapNode node = all_nodes_map[x, y];
  51. if (node != null)
  52. {
  53. node.Init(this, x, y);
  54. }
  55. }
  56. }
  57. for (int y = 0; y < ycount; y++)
  58. {
  59. for (int x = 0; x < xcount; x++)
  60. {
  61. MMapNode node = all_nodes_map[x, y];
  62. if (node != null)
  63. {
  64. node.resetNextNodes(this);
  65. }
  66. }
  67. }
  68. }
  69. public void Dispose()
  70. {
  71. for (int y = 0; y < ycount; y++)
  72. {
  73. for (int x = 0; x < xcount; x++)
  74. {
  75. MMapNode node = all_nodes_map[x, y];
  76. if (node != null)
  77. {
  78. node.Dispose();
  79. }
  80. }
  81. }
  82. this.map_data.Dispose();
  83. this.all_nodes_map = null;
  84. }
  85. public void ForEachNodes(Action<MMapNode> action)
  86. {
  87. for (int y = 0; y < ycount; y++)
  88. {
  89. for (int x = 0; x < xcount; x++)
  90. {
  91. MMapNode node = all_nodes_map[x, y];
  92. if (node != null)
  93. {
  94. action(node);
  95. }
  96. }
  97. }
  98. }
  99. public MMapNode getMapNode(int bx, int by)
  100. {
  101. return all_nodes_map[bx, by];
  102. }
  103. public bool touchMapBlock(int bx, int by)
  104. {
  105. if (bx < 0 || bx >= xcount)
  106. return true;
  107. if (by < 0 || by >= ycount)
  108. return true;
  109. var node = all_nodes_map[bx, by];
  110. if (node != null)
  111. {
  112. return node.Blocked;
  113. }
  114. return true;
  115. }
  116. }
  117. public class MMapNode : MapNode
  118. {
  119. /// <summary>
  120. /// 实例数量
  121. /// </summary>
  122. public static int ActiveObjectCount { get { return s_active_object_count.Value; } }
  123. private static AtomicInteger s_active_object_count = new AtomicInteger(0);
  124. private static MMapNode[] ZeroNexts = new MMapNode[0];
  125. internal Action<MMapNode> OnResetValue;
  126. internal byte area = 0;
  127. internal MMapNode[] nextnodes = ZeroNexts;
  128. private BitSet8 bitset;
  129. private ushort bx, by;
  130. private float px, py;
  131. private int value;
  132. private BlockMesh mesh;
  133. private float complexity;
  134. public override MapNode[] Nexts { get { return nextnodes; } }
  135. public BlockMesh Mesh { get { return mesh; } }
  136. public int BX { get { return bx; } }
  137. public int BY { get { return by; } }
  138. public float PosX { get { return px; } }
  139. public float PosY { get { return py; } }
  140. public bool Blocked { get { return bitset.Get(0); } private set { bitset.Set(0, value); } }
  141. public bool IsAllCross { get { return bitset.Get(1); } private set { bitset.Set(1, value); } }
  142. public byte AreaValue { get { return area; } }
  143. public int Value { get { return value; } }
  144. public MMapNode() { s_active_object_count++; }
  145. ~MMapNode() { s_active_object_count--; }
  146. internal void Init(MTerrain map, int x, int y)
  147. {
  148. this.bx = (ushort)x;
  149. this.by = (ushort)y;
  150. this.px = bx * map.map_data.CellW + map.map_data.CellW / 2;
  151. this.py = by * map.map_data.CellH + map.map_data.CellH / 2;
  152. this.value = map.map_data.GetValue(BX, BY);
  153. this.Blocked = map.map_data.TestBlock(BX, BY);
  154. }
  155. public override void Dispose()
  156. {
  157. mesh = null;
  158. nextnodes = null;
  159. OnResetValue = null;
  160. }
  161. internal bool resetValue(MTerrain mmap)
  162. {
  163. this.value = mmap.map_data.GetValue(BX, BY);
  164. var block = mmap.map_data.TestBlock(BX, BY);
  165. if (this.Blocked != block)
  166. {
  167. this.Blocked = block;
  168. if (OnResetValue != null)
  169. {
  170. OnResetValue.Invoke(this);
  171. }
  172. return true;
  173. }
  174. return false;
  175. }
  176. internal void resetNextNodes(MTerrain mmap)
  177. {
  178. this.mesh = BlockMesh.CreateMesh(bx, by, mmap);
  179. if (Blocked)
  180. {
  181. this.nextnodes = ZeroNexts;
  182. this.IsAllCross = false;
  183. this.complexity = 0;
  184. }
  185. else
  186. {
  187. int[][] near_table = GetNearTables(mmap.is_inclined);
  188. using (var nexts = ListObjectPool<MMapNode>.AllocAutoRelease())
  189. {
  190. foreach (int[] offset in near_table)
  191. {
  192. int dx = bx + offset[0];
  193. int dy = by + offset[1];
  194. if (dx >= 0 && dx < mmap.xcount && dy >= 0 && dy < mmap.ycount)
  195. {
  196. MMapNode near = mmap.getMapNode(dx, dy);
  197. if (near != null && !near.Blocked)
  198. {
  199. nexts.Add(near);
  200. }
  201. }
  202. }
  203. this.nextnodes = nexts.ToArray();
  204. }
  205. this.IsAllCross = (near_table.Length == nextnodes.Length);
  206. this.complexity = mmap.manhattan.cell_sqrt * (near_table.Length - nextnodes.Length);
  207. }
  208. }
  209. public override bool TestCross(MapNode father)
  210. {
  211. return !Blocked;
  212. }
  213. public override float GetG(MapNode target)
  214. {
  215. MMapNode t = (MMapNode)target;
  216. float ndx = this.bx - t.bx;
  217. float ndy = this.by - t.by;
  218. //sqrt (ndx * ndx + ndy * ndy)//
  219. return (float)Math.Sqrt((ndx * ndx) + (ndy * ndy)) + this.complexity;
  220. }
  221. public override float GetH(MapNode father)
  222. {
  223. if (father == this) return 0;
  224. MMapNode t = (MMapNode)father;
  225. if (this.bx != t.bx && this.by != t.by)
  226. {
  227. //sqrt (1 * 1 + 1 * 1)//
  228. return SQRT_TOW + t.complexity;// SQRT_TOW;
  229. }
  230. return SQRT_ONE + t.complexity;
  231. }
  232. private readonly static float SQRT_TOW = (float)Math.Sqrt(2);
  233. private readonly static float SQRT_ONE = 1;
  234. //----------------------------------------------------------------------------------------------------------------------------
  235. /// <summary>
  236. /// 和矩形碰撞
  237. /// </summary>
  238. /// <param name="x1"></param>
  239. /// <param name="y1"></param>
  240. /// <param name="x2"></param>
  241. /// <param name="y2"></param>
  242. /// <returns></returns>
  243. public bool TouchRect(float x1, float y1, float x2, float y2)
  244. {
  245. return (mesh != null && mesh.TouchRect(x1, y1, x2, y2));
  246. }
  247. /// <summary>
  248. /// 和线段检测碰撞
  249. /// </summary>
  250. /// <returns></returns>
  251. /// <param name="x1"></param>
  252. /// <param name="y1"></param>
  253. /// <param name="x2"></param>
  254. /// <param name="y2"></param>
  255. /// <returns></returns>
  256. public bool TouchLine(float x1, float y1, float x2, float y2)
  257. {
  258. return (mesh != null && mesh.TouchLine(x1, y1, x2, y2));
  259. }
  260. /// <summary>
  261. /// 获得线段和此碰撞最近的焦点
  262. /// </summary>
  263. /// <returns></returns>
  264. public bool GetLineTouchPoint(float sx, float sy, float dx, float dy, out float touch_x, out float touch_y, out float touch_distance)
  265. {
  266. if (mesh != null)
  267. {
  268. return mesh.GetLineTouchPoint(sx, sy, dx, dy, out touch_x, out touch_y, out touch_distance);
  269. }
  270. touch_x = 0;
  271. touch_y = 0;
  272. touch_distance = 0;
  273. return false;
  274. }
  275. /// <summary>
  276. /// 单元格碰撞网格
  277. /// </summary>
  278. public class BlockMesh
  279. {
  280. /// <summary>
  281. /// 实例数量
  282. /// </summary>
  283. public static int ActiveObjectCount { get { return s_active_object_count.Value; } }
  284. private static AtomicInteger s_active_object_count = new AtomicInteger(0);
  285. private BlockMesh() { s_active_object_count++; }
  286. ~BlockMesh() { s_active_object_count--; }
  287. private BitSet8 c_block = new BitSet8(0xFF);
  288. private CommonLang.Geometry.Vector2 c_point_TL;
  289. private CommonLang.Geometry.Vector2 c_point_TR;
  290. private CommonLang.Geometry.Vector2 c_point_BR;
  291. private CommonLang.Geometry.Vector2 c_point_BL;
  292. private bool c_block_T { get { return c_block.Get(0); } set { c_block.Set(0, value); } }
  293. private bool c_block_L { get { return c_block.Get(1); } set { c_block.Set(1, value); } }
  294. private bool c_block_R { get { return c_block.Get(2); } set { c_block.Set(2, value); } }
  295. private bool c_block_B { get { return c_block.Get(3); } set { c_block.Set(3, value); } }
  296. public bool IsTestTop { get { return c_block_T; } }
  297. public bool IsTestLeft { get { return c_block_L; } }
  298. public bool IsTestRight { get { return c_block_R; } }
  299. public bool IsTestBottom { get { return c_block_B; } }
  300. internal static BlockMesh CreateMesh(int bx, int by, MTerrain mmap)
  301. {
  302. bool c_block_L = mmap.touchMapBlock(bx - 1, by);
  303. bool c_block_R = mmap.touchMapBlock(bx + 1, by);
  304. bool c_block_T = mmap.touchMapBlock(bx, by - 1);
  305. bool c_block_B = mmap.touchMapBlock(bx, by + 1);
  306. // 四周全部阻挡,忽略 //
  307. if (c_block_L && c_block_R && c_block_T && c_block_B)
  308. {
  309. return null;
  310. }
  311. // 四周全部非阻挡,忽略 //
  312. if (!c_block_L && !c_block_R && !c_block_T && !c_block_B)
  313. {
  314. return null;
  315. }
  316. BlockMesh ret = new BlockMesh();
  317. float cellw = mmap.map_data.CellW;
  318. float cellh = mmap.map_data.CellH;
  319. float c_sx = bx * cellw;
  320. float c_sy = by * cellh;
  321. float c_dx = c_sx + cellw;
  322. float c_dy = c_sy + cellh;
  323. ret.c_point_TL = new CommonLang.Geometry.Vector2(c_sx, c_sy);
  324. ret.c_point_TR = new CommonLang.Geometry.Vector2(c_dx, c_sy);
  325. ret.c_point_BR = new CommonLang.Geometry.Vector2(c_dx, c_dy);
  326. ret.c_point_BL = new CommonLang.Geometry.Vector2(c_sx, c_dy);
  327. ret.c_block_L = c_block_L;
  328. ret.c_block_R = c_block_R;
  329. ret.c_block_T = c_block_T;
  330. ret.c_block_B = c_block_B;
  331. return ret;
  332. }
  333. /// <summary>
  334. /// 和矩形碰撞
  335. /// </summary>
  336. /// <param name="x1"></param>
  337. /// <param name="y1"></param>
  338. /// <param name="x2"></param>
  339. /// <param name="y2"></param>
  340. /// <returns></returns>
  341. public bool TouchRect(float x1, float y1, float x2, float y2)
  342. {
  343. return CMath.intersectRect(c_point_TL.X, c_point_TL.Y, c_point_BR.X, c_point_BR.Y, x1, y1, x2, y2);
  344. }
  345. /// <summary>
  346. /// 和线段检测碰撞
  347. /// </summary>
  348. /// <returns></returns>
  349. public bool TouchLine(float x1, float y1, float x2, float y2)
  350. {
  351. // T
  352. if (c_block_T && CMath.intersectLine(c_point_TL.X, c_point_TL.Y, c_point_TR.X, c_point_TR.Y, x1, y1, x2, y2))
  353. {
  354. return true;
  355. }
  356. // L
  357. if (c_block_L && CMath.intersectLine(c_point_TL.X, c_point_TL.Y, c_point_BL.X, c_point_BL.Y, x1, y1, x2, y2))
  358. {
  359. return true;
  360. }
  361. // R
  362. if (c_block_R && CMath.intersectLine(c_point_TR.X, c_point_TR.Y, c_point_BR.X, c_point_BR.Y, x1, y1, x2, y2))
  363. {
  364. return true;
  365. }
  366. // B
  367. if (c_block_B && CMath.intersectLine(c_point_BL.X, c_point_BL.Y, c_point_BR.X, c_point_BR.Y, x1, y1, x2, y2))
  368. {
  369. return true;
  370. }
  371. return false;
  372. }
  373. /// <summary>
  374. /// 获得线段和此碰撞最近的焦点
  375. /// </summary>
  376. /// <returns></returns>
  377. public bool GetLineTouchPoint(float sx, float sy, float dx, float dy, out float touch_x, out float touch_y, out float touch_distance)
  378. {
  379. CommonLang.Geometry.Vector2 p0 = new CommonLang.Geometry.Vector2(sx, sy);
  380. CommonLang.Geometry.Vector2 p1 = new CommonLang.Geometry.Vector2(dx, dy);
  381. CommonLang.Geometry.Vector2 touch = p0;
  382. CommonLang.Geometry.Vector2 min_p = p0;
  383. float min_d = float.MaxValue;
  384. bool ret = false;
  385. // T
  386. if (c_block_T && LineLineIntersect(ref p0, ref p1, ref c_point_TL, ref c_point_TR, ref touch, ref min_p, ref min_d))
  387. {
  388. ret = true;
  389. }
  390. // R
  391. if (c_block_R && LineLineIntersect(ref p0, ref p1, ref c_point_TR, ref c_point_BR, ref touch, ref min_p, ref min_d))
  392. {
  393. ret = true;
  394. }
  395. // B
  396. if (c_block_B && LineLineIntersect(ref p0, ref p1, ref c_point_BL, ref c_point_BR, ref touch, ref min_p, ref min_d))
  397. {
  398. ret = true;
  399. }
  400. // L
  401. if (c_block_L && LineLineIntersect(ref p0, ref p1, ref c_point_TL, ref c_point_BL, ref touch, ref min_p, ref min_d))
  402. {
  403. ret = true;
  404. }
  405. touch_x = min_p.X;
  406. touch_y = min_p.Y;
  407. touch_distance = min_d;
  408. return ret;
  409. }
  410. public void ToLines(List<CommonLang.Geometry.Line2> ret)
  411. {
  412. if (c_block_T)
  413. {
  414. ret.Add(new CommonLang.Geometry.Line2(c_point_TL, c_point_TR));
  415. }
  416. if (c_block_R)
  417. {
  418. ret.Add(new CommonLang.Geometry.Line2(c_point_TR, c_point_BR));
  419. }
  420. if (c_block_B)
  421. {
  422. ret.Add(new CommonLang.Geometry.Line2(c_point_BL, c_point_BR));
  423. }
  424. if (c_block_L)
  425. {
  426. ret.Add(new CommonLang.Geometry.Line2(c_point_TL, c_point_BL));
  427. }
  428. }
  429. private bool LineLineIntersect(
  430. ref CommonLang.Geometry.Vector2 sp0,
  431. ref CommonLang.Geometry.Vector2 sp1,
  432. ref CommonLang.Geometry.Vector2 dp0,
  433. ref CommonLang.Geometry.Vector2 dp1,
  434. ref CommonLang.Geometry.Vector2 out_touch,
  435. ref CommonLang.Geometry.Vector2 out_min_p,
  436. ref float out_min_d)
  437. {
  438. if (CommonLang.Geometry.CollisionMath.LineLineIntersect(sp0, sp1, dp0, dp1, out out_touch))
  439. {
  440. float d = (out_touch - sp0).LengthSquared();
  441. if (d < out_min_d)
  442. {
  443. out_min_p = out_touch;
  444. out_min_d = d;
  445. return true;
  446. }
  447. }
  448. return false;
  449. }
  450. }
  451. }
  452. /// <summary>
  453. /// 曼哈顿寻路路点
  454. /// </summary>
  455. public class MWayPoint : WayPoint, IEnumerable<MWayPoint>
  456. {
  457. /// <summary>
  458. /// 实例数量
  459. /// </summary>
  460. public static int ActiveObjectCount { get { return s_active_object_count.Value; } }
  461. private static AtomicInteger s_active_object_count = new AtomicInteger(0);
  462. private float _px, _py;
  463. private bool _center;
  464. public int BX { get; private set; }
  465. public int BY { get; private set; }
  466. public MWayPoint Next { get { return base.next as MWayPoint; } }
  467. public MWayPoint Tail { get { return base.tail as MWayPoint; } }
  468. public float PosX { get { return _px; } }
  469. public float PosY { get { return _py; } }
  470. public bool IsCenter { get { return _center; } }
  471. internal MWayPoint(MMapNode node)
  472. : base(node)
  473. {
  474. s_active_object_count++;
  475. this.BX = node.BX;
  476. this.BY = node.BY;
  477. this._px = node.PosX;
  478. this._py = node.PosY;
  479. this._center = true;
  480. }
  481. ~MWayPoint()
  482. {
  483. s_active_object_count--;
  484. }
  485. internal void SetPos(float x, float y)
  486. {
  487. this._px = x;
  488. this._py = y;
  489. this._center = (_px == (node as MMapNode).PosX && _py == (node as MMapNode).PosY);
  490. }
  491. public override void Dispose()
  492. {
  493. base.Dispose();
  494. }
  495. public override WayPoint Clone()
  496. {
  497. var ret = new MWayPoint(this.node as MMapNode);
  498. ret._px = this._px;
  499. ret._px = this._px;
  500. if (this.next != null)
  501. {
  502. ret.LinkNext(this.next.Clone());
  503. }
  504. return ret;
  505. }
  506. public float GetTotalDistance()
  507. {
  508. float ret = 0;
  509. MWayPoint cur = this;
  510. while (cur != null)
  511. {
  512. MWayPoint nex = cur.Next;
  513. if (cur != null && nex != null)
  514. {
  515. ret += CMath.getDistance(cur.PosX, cur.PosY, nex.PosX, nex.PosY);
  516. }
  517. cur = nex;
  518. }
  519. return ret;
  520. }
  521. #region IEnumerable
  522. IEnumerator<MWayPoint> IEnumerable<MWayPoint>.GetEnumerator()
  523. {
  524. return new Iterator(this);
  525. }
  526. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  527. {
  528. return new Iterator(this);
  529. }
  530. private struct Iterator : IEnumerator<MWayPoint>
  531. {
  532. private MWayPoint root;
  533. private MWayPoint current;
  534. public MWayPoint Current { get { return current; } }
  535. object System.Collections.IEnumerator.Current { get { return current; } }
  536. public Iterator(MWayPoint root)
  537. {
  538. this.root = root;
  539. this.current = null;
  540. }
  541. public void Dispose()
  542. {
  543. this.current = null;
  544. }
  545. public bool MoveNext()
  546. {
  547. if (current == null)
  548. {
  549. this.current = root;
  550. }
  551. else
  552. {
  553. this.current = current.Next;
  554. }
  555. return current != null;
  556. }
  557. public void Reset()
  558. {
  559. this.current = null;
  560. }
  561. }
  562. #endregion
  563. }
  564. }
  565. }