RecastLayers.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <float.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "Recast.h"
  24. #include "RecastAlloc.h"
  25. #include "RecastAssert.h"
  26. // Must be 255 or smaller (not 256) because layer IDs are stored as
  27. // a byte where 255 is a special value.
  28. #ifndef RC_MAX_LAYERS_DEF
  29. #define RC_MAX_LAYERS_DEF 63
  30. #endif
  31. #if RC_MAX_LAYERS_DEF > 255
  32. #error RC_MAX_LAYERS_DEF must be 255 or smaller
  33. #endif
  34. #ifndef RC_MAX_NEIS_DEF
  35. #define RC_MAX_NEIS_DEF 16
  36. #endif
  37. // Keep type checking.
  38. static const int RC_MAX_LAYERS = RC_MAX_LAYERS_DEF;
  39. static const int RC_MAX_NEIS = RC_MAX_NEIS_DEF;
  40. struct rcLayerRegion
  41. {
  42. unsigned char layers[RC_MAX_LAYERS];
  43. unsigned char neis[RC_MAX_NEIS];
  44. unsigned short ymin, ymax;
  45. unsigned char layerId; // Layer ID
  46. unsigned char nlayers; // Layer count
  47. unsigned char nneis; // Neighbour count
  48. unsigned char base; // Flag indicating if the region is the base of merged regions.
  49. };
  50. static bool contains(const unsigned char* a, const unsigned char an, const unsigned char v)
  51. {
  52. const int n = (int)an;
  53. for (int i = 0; i < n; ++i)
  54. {
  55. if (a[i] == v)
  56. return true;
  57. }
  58. return false;
  59. }
  60. static bool addUnique(unsigned char* a, unsigned char& an, int anMax, unsigned char v)
  61. {
  62. if (contains(a, an, v))
  63. return true;
  64. if ((int)an >= anMax)
  65. return false;
  66. a[an] = v;
  67. an++;
  68. return true;
  69. }
  70. inline bool overlapRange(const unsigned short amin, const unsigned short amax,
  71. const unsigned short bmin, const unsigned short bmax)
  72. {
  73. return (amin > bmax || amax < bmin) ? false : true;
  74. }
  75. struct rcLayerSweepSpan
  76. {
  77. unsigned short ns; // number samples
  78. unsigned char id; // region id
  79. unsigned char nei; // neighbour id
  80. };
  81. /// @par
  82. ///
  83. /// See the #rcConfig documentation for more information on the configuration parameters.
  84. ///
  85. /// @see rcAllocHeightfieldLayerSet, rcCompactHeightfield, rcHeightfieldLayerSet, rcConfig
  86. bool rcBuildHeightfieldLayers(rcContext* ctx, const rcCompactHeightfield& chf,
  87. const int borderSize, const int walkableHeight,
  88. rcHeightfieldLayerSet& lset)
  89. {
  90. rcAssert(ctx);
  91. rcScopedTimer timer(ctx, RC_TIMER_BUILD_LAYERS);
  92. const int w = chf.width;
  93. const int h = chf.height;
  94. rcScopedDelete<unsigned char> srcReg((unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP));
  95. if (!srcReg)
  96. {
  97. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'srcReg' (%d).", chf.spanCount);
  98. return false;
  99. }
  100. memset(srcReg,0xff,sizeof(unsigned char)*chf.spanCount);
  101. const int nsweeps = chf.width;
  102. rcScopedDelete<rcLayerSweepSpan> sweeps((rcLayerSweepSpan*)rcAlloc(sizeof(rcLayerSweepSpan)*nsweeps, RC_ALLOC_TEMP));
  103. if (!sweeps)
  104. {
  105. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'sweeps' (%d).", nsweeps);
  106. return false;
  107. }
  108. // Partition walkable area into monotone regions.
  109. int prevCount[256];
  110. unsigned char regId = 0;
  111. for (int y = borderSize; y < h-borderSize; ++y)
  112. {
  113. memset(prevCount,0,sizeof(int)*regId);
  114. unsigned char sweepId = 0;
  115. for (int x = borderSize; x < w-borderSize; ++x)
  116. {
  117. const rcCompactCell& c = chf.cells[x+y*w];
  118. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  119. {
  120. const rcCompactSpan& s = chf.spans[i];
  121. if (chf.areas[i] == RC_NULL_AREA) continue;
  122. unsigned char sid = 0xff;
  123. // -x
  124. if (rcGetCon(s, 0) != RC_NOT_CONNECTED)
  125. {
  126. const int ax = x + rcGetDirOffsetX(0);
  127. const int ay = y + rcGetDirOffsetY(0);
  128. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0);
  129. if (chf.areas[ai] != RC_NULL_AREA && srcReg[ai] != 0xff)
  130. sid = srcReg[ai];
  131. }
  132. if (sid == 0xff)
  133. {
  134. sid = sweepId++;
  135. sweeps[sid].nei = 0xff;
  136. sweeps[sid].ns = 0;
  137. }
  138. // -y
  139. if (rcGetCon(s,3) != RC_NOT_CONNECTED)
  140. {
  141. const int ax = x + rcGetDirOffsetX(3);
  142. const int ay = y + rcGetDirOffsetY(3);
  143. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3);
  144. const unsigned char nr = srcReg[ai];
  145. if (nr != 0xff)
  146. {
  147. // Set neighbour when first valid neighbour is encoutered.
  148. if (sweeps[sid].ns == 0)
  149. sweeps[sid].nei = nr;
  150. if (sweeps[sid].nei == nr)
  151. {
  152. // Update existing neighbour
  153. sweeps[sid].ns++;
  154. prevCount[nr]++;
  155. }
  156. else
  157. {
  158. // This is hit if there is nore than one neighbour.
  159. // Invalidate the neighbour.
  160. sweeps[sid].nei = 0xff;
  161. }
  162. }
  163. }
  164. srcReg[i] = sid;
  165. }
  166. }
  167. // Create unique ID.
  168. for (int i = 0; i < sweepId; ++i)
  169. {
  170. // If the neighbour is set and there is only one continuous connection to it,
  171. // the sweep will be merged with the previous one, else new region is created.
  172. if (sweeps[i].nei != 0xff && prevCount[sweeps[i].nei] == (int)sweeps[i].ns)
  173. {
  174. sweeps[i].id = sweeps[i].nei;
  175. }
  176. else
  177. {
  178. if (regId == 255)
  179. {
  180. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Region ID overflow.");
  181. return false;
  182. }
  183. sweeps[i].id = regId++;
  184. }
  185. }
  186. // Remap local sweep ids to region ids.
  187. for (int x = borderSize; x < w-borderSize; ++x)
  188. {
  189. const rcCompactCell& c = chf.cells[x+y*w];
  190. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  191. {
  192. if (srcReg[i] != 0xff)
  193. srcReg[i] = sweeps[srcReg[i]].id;
  194. }
  195. }
  196. }
  197. // Allocate and init layer regions.
  198. const int nregs = (int)regId;
  199. rcScopedDelete<rcLayerRegion> regs((rcLayerRegion*)rcAlloc(sizeof(rcLayerRegion)*nregs, RC_ALLOC_TEMP));
  200. if (!regs)
  201. {
  202. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'regs' (%d).", nregs);
  203. return false;
  204. }
  205. memset(regs, 0, sizeof(rcLayerRegion)*nregs);
  206. for (int i = 0; i < nregs; ++i)
  207. {
  208. regs[i].layerId = 0xff;
  209. regs[i].ymin = 0xffff;
  210. regs[i].ymax = 0;
  211. }
  212. // Find region neighbours and overlapping regions.
  213. for (int y = 0; y < h; ++y)
  214. {
  215. for (int x = 0; x < w; ++x)
  216. {
  217. const rcCompactCell& c = chf.cells[x+y*w];
  218. unsigned char lregs[RC_MAX_LAYERS];
  219. int nlregs = 0;
  220. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  221. {
  222. const rcCompactSpan& s = chf.spans[i];
  223. const unsigned char ri = srcReg[i];
  224. if (ri == 0xff) continue;
  225. regs[ri].ymin = rcMin(regs[ri].ymin, s.y);
  226. regs[ri].ymax = rcMax(regs[ri].ymax, s.y);
  227. // Collect all region layers.
  228. if (nlregs < RC_MAX_LAYERS)
  229. lregs[nlregs++] = ri;
  230. // Update neighbours
  231. for (int dir = 0; dir < 4; ++dir)
  232. {
  233. if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
  234. {
  235. const int ax = x + rcGetDirOffsetX(dir);
  236. const int ay = y + rcGetDirOffsetY(dir);
  237. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
  238. const unsigned char rai = srcReg[ai];
  239. if (rai != 0xff && rai != ri)
  240. {
  241. // Don't check return value -- if we cannot add the neighbor
  242. // it will just cause a few more regions to be created, which
  243. // is fine.
  244. addUnique(regs[ri].neis, regs[ri].nneis, RC_MAX_NEIS, rai);
  245. }
  246. }
  247. }
  248. }
  249. // Update overlapping regions.
  250. for (int i = 0; i < nlregs-1; ++i)
  251. {
  252. for (int j = i+1; j < nlregs; ++j)
  253. {
  254. if (lregs[i] != lregs[j])
  255. {
  256. rcLayerRegion& ri = regs[lregs[i]];
  257. rcLayerRegion& rj = regs[lregs[j]];
  258. if (!addUnique(ri.layers, ri.nlayers, RC_MAX_LAYERS, lregs[j]) ||
  259. !addUnique(rj.layers, rj.nlayers, RC_MAX_LAYERS, lregs[i]))
  260. {
  261. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS.");
  262. return false;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. // Create 2D layers from regions.
  270. unsigned char layerId = 0;
  271. static const int MAX_STACK = 64;
  272. unsigned char stack[MAX_STACK];
  273. int nstack = 0;
  274. for (int i = 0; i < nregs; ++i)
  275. {
  276. rcLayerRegion& root = regs[i];
  277. // Skip already visited.
  278. if (root.layerId != 0xff)
  279. continue;
  280. // Start search.
  281. root.layerId = layerId;
  282. root.base = 1;
  283. nstack = 0;
  284. stack[nstack++] = (unsigned char)i;
  285. while (nstack)
  286. {
  287. // Pop front
  288. rcLayerRegion& reg = regs[stack[0]];
  289. nstack--;
  290. for (int j = 0; j < nstack; ++j)
  291. stack[j] = stack[j+1];
  292. const int nneis = (int)reg.nneis;
  293. for (int j = 0; j < nneis; ++j)
  294. {
  295. const unsigned char nei = reg.neis[j];
  296. rcLayerRegion& regn = regs[nei];
  297. // Skip already visited.
  298. if (regn.layerId != 0xff)
  299. continue;
  300. // Skip if the neighbour is overlapping root region.
  301. if (contains(root.layers, root.nlayers, nei))
  302. continue;
  303. // Skip if the height range would become too large.
  304. const int ymin = rcMin(root.ymin, regn.ymin);
  305. const int ymax = rcMax(root.ymax, regn.ymax);
  306. if ((ymax - ymin) >= 255)
  307. continue;
  308. if (nstack < MAX_STACK)
  309. {
  310. // Deepen
  311. stack[nstack++] = (unsigned char)nei;
  312. // Mark layer id
  313. regn.layerId = layerId;
  314. // Merge current layers to root.
  315. for (int k = 0; k < regn.nlayers; ++k)
  316. {
  317. if (!addUnique(root.layers, root.nlayers, RC_MAX_LAYERS, regn.layers[k]))
  318. {
  319. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS.");
  320. return false;
  321. }
  322. }
  323. root.ymin = rcMin(root.ymin, regn.ymin);
  324. root.ymax = rcMax(root.ymax, regn.ymax);
  325. }
  326. }
  327. }
  328. layerId++;
  329. }
  330. // Merge non-overlapping regions that are close in height.
  331. const unsigned short mergeHeight = (unsigned short)walkableHeight * 4;
  332. for (int i = 0; i < nregs; ++i)
  333. {
  334. rcLayerRegion& ri = regs[i];
  335. if (!ri.base) continue;
  336. unsigned char newId = ri.layerId;
  337. for (;;)
  338. {
  339. unsigned char oldId = 0xff;
  340. for (int j = 0; j < nregs; ++j)
  341. {
  342. if (i == j) continue;
  343. rcLayerRegion& rj = regs[j];
  344. if (!rj.base) continue;
  345. // Skip if the regions are not close to each other.
  346. if (!overlapRange(ri.ymin,ri.ymax+mergeHeight, rj.ymin,rj.ymax+mergeHeight))
  347. continue;
  348. // Skip if the height range would become too large.
  349. const int ymin = rcMin(ri.ymin, rj.ymin);
  350. const int ymax = rcMax(ri.ymax, rj.ymax);
  351. if ((ymax - ymin) >= 255)
  352. continue;
  353. // Make sure that there is no overlap when merging 'ri' and 'rj'.
  354. bool overlap = false;
  355. // Iterate over all regions which have the same layerId as 'rj'
  356. for (int k = 0; k < nregs; ++k)
  357. {
  358. if (regs[k].layerId != rj.layerId)
  359. continue;
  360. // Check if region 'k' is overlapping region 'ri'
  361. // Index to 'regs' is the same as region id.
  362. if (contains(ri.layers,ri.nlayers, (unsigned char)k))
  363. {
  364. overlap = true;
  365. break;
  366. }
  367. }
  368. // Cannot merge of regions overlap.
  369. if (overlap)
  370. continue;
  371. // Can merge i and j.
  372. oldId = rj.layerId;
  373. break;
  374. }
  375. // Could not find anything to merge with, stop.
  376. if (oldId == 0xff)
  377. break;
  378. // Merge
  379. for (int j = 0; j < nregs; ++j)
  380. {
  381. rcLayerRegion& rj = regs[j];
  382. if (rj.layerId == oldId)
  383. {
  384. rj.base = 0;
  385. // Remap layerIds.
  386. rj.layerId = newId;
  387. // Add overlaid layers from 'rj' to 'ri'.
  388. for (int k = 0; k < rj.nlayers; ++k)
  389. {
  390. if (!addUnique(ri.layers, ri.nlayers, RC_MAX_LAYERS, rj.layers[k]))
  391. {
  392. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: layer overflow (too many overlapping walkable platforms). Try increasing RC_MAX_LAYERS.");
  393. return false;
  394. }
  395. }
  396. // Update height bounds.
  397. ri.ymin = rcMin(ri.ymin, rj.ymin);
  398. ri.ymax = rcMax(ri.ymax, rj.ymax);
  399. }
  400. }
  401. }
  402. }
  403. // Compact layerIds
  404. unsigned char remap[256];
  405. memset(remap, 0, 256);
  406. // Find number of unique layers.
  407. layerId = 0;
  408. for (int i = 0; i < nregs; ++i)
  409. remap[regs[i].layerId] = 1;
  410. for (int i = 0; i < 256; ++i)
  411. {
  412. if (remap[i])
  413. remap[i] = layerId++;
  414. else
  415. remap[i] = 0xff;
  416. }
  417. // Remap ids.
  418. for (int i = 0; i < nregs; ++i)
  419. regs[i].layerId = remap[regs[i].layerId];
  420. // No layers, return empty.
  421. if (layerId == 0)
  422. return true;
  423. // Create layers.
  424. rcAssert(lset.layers == 0);
  425. const int lw = w - borderSize*2;
  426. const int lh = h - borderSize*2;
  427. // Build contracted bbox for layers.
  428. float bmin[3], bmax[3];
  429. rcVcopy(bmin, chf.bmin);
  430. rcVcopy(bmax, chf.bmax);
  431. bmin[0] += borderSize*chf.cs;
  432. bmin[2] += borderSize*chf.cs;
  433. bmax[0] -= borderSize*chf.cs;
  434. bmax[2] -= borderSize*chf.cs;
  435. lset.nlayers = (int)layerId;
  436. lset.layers = (rcHeightfieldLayer*)rcAlloc(sizeof(rcHeightfieldLayer)*lset.nlayers, RC_ALLOC_PERM);
  437. if (!lset.layers)
  438. {
  439. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'layers' (%d).", lset.nlayers);
  440. return false;
  441. }
  442. memset(lset.layers, 0, sizeof(rcHeightfieldLayer)*lset.nlayers);
  443. // Store layers.
  444. for (int i = 0; i < lset.nlayers; ++i)
  445. {
  446. unsigned char curId = (unsigned char)i;
  447. rcHeightfieldLayer* layer = &lset.layers[i];
  448. const int gridSize = sizeof(unsigned char)*lw*lh;
  449. layer->heights = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM);
  450. if (!layer->heights)
  451. {
  452. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'heights' (%d).", gridSize);
  453. return false;
  454. }
  455. memset(layer->heights, 0xff, gridSize);
  456. layer->areas = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM);
  457. if (!layer->areas)
  458. {
  459. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'areas' (%d).", gridSize);
  460. return false;
  461. }
  462. memset(layer->areas, 0, gridSize);
  463. layer->cons = (unsigned char*)rcAlloc(gridSize, RC_ALLOC_PERM);
  464. if (!layer->cons)
  465. {
  466. ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'cons' (%d).", gridSize);
  467. return false;
  468. }
  469. memset(layer->cons, 0, gridSize);
  470. // Find layer height bounds.
  471. int hmin = 0, hmax = 0;
  472. for (int j = 0; j < nregs; ++j)
  473. {
  474. if (regs[j].base && regs[j].layerId == curId)
  475. {
  476. hmin = (int)regs[j].ymin;
  477. hmax = (int)regs[j].ymax;
  478. }
  479. }
  480. layer->width = lw;
  481. layer->height = lh;
  482. layer->cs = chf.cs;
  483. layer->ch = chf.ch;
  484. // Adjust the bbox to fit the heightfield.
  485. rcVcopy(layer->bmin, bmin);
  486. rcVcopy(layer->bmax, bmax);
  487. layer->bmin[1] = bmin[1] + hmin*chf.ch;
  488. layer->bmax[1] = bmin[1] + hmax*chf.ch;
  489. layer->hmin = hmin;
  490. layer->hmax = hmax;
  491. // Update usable data region.
  492. layer->minx = layer->width;
  493. layer->maxx = 0;
  494. layer->miny = layer->height;
  495. layer->maxy = 0;
  496. // Copy height and area from compact heightfield.
  497. for (int y = 0; y < lh; ++y)
  498. {
  499. for (int x = 0; x < lw; ++x)
  500. {
  501. const int cx = borderSize+x;
  502. const int cy = borderSize+y;
  503. const rcCompactCell& c = chf.cells[cx+cy*w];
  504. for (int j = (int)c.index, nj = (int)(c.index+c.count); j < nj; ++j)
  505. {
  506. const rcCompactSpan& s = chf.spans[j];
  507. // Skip unassigned regions.
  508. if (srcReg[j] == 0xff)
  509. continue;
  510. // Skip of does nto belong to current layer.
  511. unsigned char lid = regs[srcReg[j]].layerId;
  512. if (lid != curId)
  513. continue;
  514. // Update data bounds.
  515. layer->minx = rcMin(layer->minx, x);
  516. layer->maxx = rcMax(layer->maxx, x);
  517. layer->miny = rcMin(layer->miny, y);
  518. layer->maxy = rcMax(layer->maxy, y);
  519. // Store height and area type.
  520. const int idx = x+y*lw;
  521. layer->heights[idx] = (unsigned char)(s.y - hmin);
  522. layer->areas[idx] = chf.areas[j];
  523. // Check connection.
  524. unsigned char portal = 0;
  525. unsigned char con = 0;
  526. for (int dir = 0; dir < 4; ++dir)
  527. {
  528. if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
  529. {
  530. const int ax = cx + rcGetDirOffsetX(dir);
  531. const int ay = cy + rcGetDirOffsetY(dir);
  532. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
  533. unsigned char alid = srcReg[ai] != 0xff ? regs[srcReg[ai]].layerId : 0xff;
  534. // Portal mask
  535. if (chf.areas[ai] != RC_NULL_AREA && lid != alid)
  536. {
  537. portal |= (unsigned char)(1<<dir);
  538. // Update height so that it matches on both sides of the portal.
  539. const rcCompactSpan& as = chf.spans[ai];
  540. if (as.y > hmin)
  541. layer->heights[idx] = rcMax(layer->heights[idx], (unsigned char)(as.y - hmin));
  542. }
  543. // Valid connection mask
  544. if (chf.areas[ai] != RC_NULL_AREA && lid == alid)
  545. {
  546. const int nx = ax - borderSize;
  547. const int ny = ay - borderSize;
  548. if (nx >= 0 && ny >= 0 && nx < lw && ny < lh)
  549. con |= (unsigned char)(1<<dir);
  550. }
  551. }
  552. }
  553. layer->cons[idx] = (portal << 4) | con;
  554. }
  555. }
  556. }
  557. if (layer->minx > layer->maxx)
  558. layer->minx = layer->maxx = 0;
  559. if (layer->miny > layer->maxy)
  560. layer->miny = layer->maxy = 0;
  561. }
  562. return true;
  563. }