RTSManhattan.Map.cs 22 KB

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