@@ -12,7 +12,6 @@ import {
1212} from './type'
1313
1414
15- const EMPTY = null
1615const LAND = 0
1716const WATER = 1
1817// Area ratios
@@ -21,20 +20,22 @@ const MINIMUN_OCEAN_RATIO = 2
2120const MINIMUN_SEA_RATIO = .05
2221const MINIMUN_CONTINENT_RATIO = 1
2322
23+ export const EMPTY = null
24+
2425
2526export function buildSurfaceModel ( context ) {
2627 const { rect, world} = context
2728 // detect land or water tiles in the grid
2829 const landWaterGrid = detectLandWater ( rect , world )
2930 // detect surface type by filling empty bodies
30- const { bodyGrid, bodyTypeMap, bodyAreaMap} = detectSurfaceAreas ( context , landWaterGrid )
31- const borderGrid = detectBorders ( context , landWaterGrid )
31+ const { bodyGrid, bodyTypeMap, bodyAreaMap} = detectBodies ( context , landWaterGrid )
32+ const borderTypeGrid = detectBorderTypes ( context , bodyTypeMap , bodyGrid )
3233 const waterArea = readWaterArea ( bodyTypeMap , bodyAreaMap )
3334 return {
34- border : borderGrid ,
3535 body : bodyGrid ,
3636 bodyType : bodyTypeMap ,
3737 bodyArea : bodyAreaMap ,
38+ borderType : borderTypeGrid ,
3839 waterArea,
3940 }
4041}
@@ -49,13 +50,13 @@ function detectLandWater(rect, world) {
4950}
5051
5152
52- function detectSurfaceAreas ( baseContext , landWaterGrid ) {
53+ function detectBodies ( baseContext , landWaterGrid ) {
5354 // flood fill "empty" points and determine body type by total area
5455 let bodyId = 1
5556 const bodyTypeMap = new Map ( )
5657 const bodyAreaMap = new Map ( )
5758 const bodyGrid = Grid . fromRect ( baseContext . rect , ( ) => EMPTY )
58- const context = { ...baseContext , bodyTypeMap , bodyGrid, landWaterGrid}
59+ const context = { ...baseContext , bodyGrid, landWaterGrid}
5960
6061 landWaterGrid . forEach ( originPoint => {
6162 if ( bodyGrid . get ( originPoint ) != EMPTY ) return
@@ -106,20 +107,21 @@ function fillBodyArea(context, originPoint, bodyId) {
106107}
107108
108109
109- function detectBorders ( context , landWaterGrid ) {
110- // surface body matrix already defined, update it by setting
111- // water/land borders as negative ids
112- const borderGrid = Grid . fromRect ( context . rect , point => {
113- const isWater = landWaterGrid . get ( point ) == WATER
110+ function detectBorderTypes ( context , bodyTypeMap , bodyGrid ) {
111+ const getType = point => {
112+ const bodyId = bodyGrid . get ( point )
113+ return Surface . parse ( bodyTypeMap . get ( bodyId ) )
114+ }
115+ return Grid . fromRect ( context . rect , point => {
116+ let type = getType ( point )
114117 for ( let sidePoint of Point . adjacents ( point ) ) {
115- const isSideWater = landWaterGrid . get ( sidePoint ) == WATER
116- if ( isWater && ! isSideWater || ! isWater && isSideWater ) {
117- return 1
118+ const sideType = getType ( sidePoint )
119+ if ( type . isWater && ! sideType . isWater || ! type . isWater && sideType . isWater ) {
120+ return sideType . id
118121 }
119122 }
120- return 0
123+ return Surface . id
121124 } )
122- return borderGrid
123125}
124126
125127
0 commit comments