Skip to content

Commit 6c7eb3d

Browse files
committed
Select regions
1 parent a7f7667 commit 6c7eb3d

File tree

5 files changed

+98
-81
lines changed

5 files changed

+98
-81
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414
- zone river follows an independent path from downstream to each tributary joint
1515
- create secondary biomes for zones
1616

17-
- detect regions by position
18-
if worldPoint is land border {
19-
if worldPoint has river and border
20-
- regions at lateral axis
21-
- if erosion.dir(NORTH) is water -> regions at vertical axis north are water
22-
} else worldPoint is water {
23-
for all water sides
24-
}
25-
26-
2717
if point is lake
2818
all inner regions
2919

src/lib/direction.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { Random } from '/src/lib/random'
22

3+
// 128 1 2
4+
// NW N NE
5+
// 64 W . E 4
6+
// SW S SE
7+
// 32 16 8
38

4-
// 135° 90° 45°
5-
// NW N NE
6-
// 180° W . E 0°
7-
// SW S SE
8-
// 225° 270° 360°
9-
10-
// these id counts starts from east, anti-clockwise
9+
// these id counts starts from north, clockwise
1110
const DIRECTIONS = {
12-
CENTER: { id: 0, code: 1, name: '', symbol: '\u2192', axis: [ 0, 0]},
13-
EAST: { id: 1, code: 2, name: 'E', symbol: '\u2192', axis: [ 1, 0]},
14-
NORTHEAST: { id: 2, code: 4, name: 'NE', symbol: '\u2197', axis: [ 1, -1]},
15-
NORTH: { id: 3, code: 8, name: 'N', symbol: '\u2191', axis: [ 0, -1]},
16-
NORTHWEST: { id: 4, code: 16, name: 'NW', symbol: '\u2196', axis: [-1, -1]},
17-
WEST: { id: 5, code: 32, name: 'W', symbol: '\u2190', axis: [-1, 0]},
18-
SOUTHWEST: { id: 6, code: 64, name: 'SW', symbol: '\u2199', axis: [-1, 1]},
19-
SOUTH: { id: 7, code: 128, name: 'S', symbol: '\u2193', axis: [ 0, 1]},
20-
SOUTHEAST: { id: 8, code: 256, name: 'SE', symbol: '\u2198', axis: [ 1, 1]},
11+
CENTER: { id: 0, code: 0, name: 'C', symbol: 'o', axis: [ 0, 0], components: []},
12+
NORTH: { id: 1, code: 1, name: 'N', symbol: '\u2191', axis: [ 0, -1], components: [1]},
13+
NORTHEAST: { id: 2, code: 2, name: 'NE', symbol: '\u2197', axis: [ 1, -1], components: [1, 3]},
14+
EAST: { id: 3, code: 4, name: 'E', symbol: '\u2192', axis: [ 1, 0], components: [3]},
15+
SOUTHEAST: { id: 4, code: 8, name: 'SE', symbol: '\u2198', axis: [ 1, 1], components: [5, 3]},
16+
SOUTH: { id: 5, code: 16, name: 'S', symbol: '\u2193', axis: [ 0, 1], components: [5]},
17+
SOUTHWEST: { id: 6, code: 32, name: 'SW', symbol: '\u2199', axis: [-1, 1], components: [5, 7]},
18+
WEST: { id: 7, code: 64, name: 'W', symbol: '\u2190', axis: [-1, 0], components: [7]},
19+
NORTHWEST: { id: 8, code: 128, name: 'NW', symbol: '\u2196', axis: [-1, -1], components: [1, 7]},
2120
}
2221

2322

@@ -64,9 +63,9 @@ export class Direction {
6463
return DIRECTION_MAP[id]
6564
}
6665

67-
static fromAxis([x, y]) {
68-
return AXIS_MAP.get(x).get(y)
69-
}
66+
// static fromAxis([x, y]) {
67+
// return AXIS_MAP.get(x).get(y)
68+
// }
7069

7170
static getName(direction) {
7271
return DIRECTION_MAP[direction.id].name
@@ -110,4 +109,8 @@ export class Direction {
110109
Direction.WEST.id
111110
].includes(direction.id)
112111
}
112+
113+
static getComponents(direction) {
114+
return direction.components.map(dir => Direction.fromId(dir))
115+
}
113116
}

src/lib/geometry/rect.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Direction } from '/src/lib/direction'
2+
13

24
export class Rect {
35
static fromHash(hash) {
@@ -75,6 +77,17 @@ export class Rect {
7577
return y === this.height - 1 && x >= 0 && x < this.width
7678
}
7779

80+
getCorners() {
81+
const xMax = this.width - 1
82+
const yMax = this.height - 1
83+
return [
84+
[[0, 0], Direction.NORTHWEST],
85+
[[xMax, 0], Direction.NORTHEAST],
86+
[[0, yMax], Direction.SOUTHWEST],
87+
[[xMax, yMax], Direction.SOUTHEAST],
88+
]
89+
}
90+
7891
wrap(point) {
7992
let [x, y] = point
8093
if (x >= this.width) { x %= this.width }

src/model/tilemap/worldmap/zone/surface/model.js

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { buildRegionGridMap } from './region'
1010
const SURFACE_NOISE_RATIO = .6
1111
const SURFACE_GRID_CACHE = new FIFOCache(256)
1212

13+
const m = [
14+
15+
]
16+
1317

1418
export function buildModel(context) {
1519
const hash = Point.hash(context.worldPoint)
@@ -18,64 +22,79 @@ export function buildModel(context) {
1822
return SURFACE_GRID_CACHE.get(hash)
1923
}
2024
const {regionGrid, originMap, regionColorMap} = buildRegionGridMap(context)
25+
const regionTypes = buildRegionSurfaceMap({...context, regionGrid})
2126
const landMaskGrid = buildLandMaskGrid(context)
2227
const model = {landMaskGrid, regionGrid, originMap, regionColorMap}
2328
SURFACE_GRID_CACHE.set(hash, model)
2429
return model
2530
}
2631

2732

28-
function buildLandMaskGrid(context) {
29-
// generate a grid with (land or water) information in bool
30-
const {worldPoint, world, rect, zoneRect} = context
31-
const relativePoint = Point.multiplyScalar(worldPoint, zoneRect.width)
32-
const noiseRect = Rect.multiply(rect, zoneRect.width)
33-
return Grid.fromRect(zoneRect, zonePoint => {
34-
const noisePoint = Point.plus(relativePoint, zonePoint)
35-
const noise = world.noise.get4DZoneOutline(noiseRect, noisePoint)
36-
return noise > SURFACE_NOISE_RATIO
33+
function buildRegionSurfaceMap(context) {
34+
const {world, worldPoint, regionGrid, zoneRect} = context
35+
const regionTypes = new Map()
36+
const isWorldPointLand = world.surface.isLand(worldPoint)
37+
const middle = Math.floor(zoneRect.with / 2)
38+
const midPoint = [middle, middle]
39+
// const isLake = world.surface.isLake(worldPoint)
40+
Point.around(worldPoint, (sidePoint, direction) => {
41+
if (isWorldPointLand) {
42+
if (world.surface.isLand(sidePoint)) {
43+
if (isWaterChannel(context, direction)) {
44+
// mark regions as water for in 4n square
45+
if (Point.equals(worldPoint, [39, 8])) {
46+
console.log(worldPoint, direction);
47+
}
48+
} else {
49+
const x = selectRegionsInLine(context, midPoint, direction)
50+
// mark regions as land
51+
}
52+
}
53+
} else {
54+
// water
55+
if (world.surface.isLand(sidePoint)) {
56+
if (world.river.has(sidePoint)) {
57+
58+
}
59+
} else {
60+
// mark regions as water
61+
}
62+
}
3763
})
64+
return regionTypes
3865
}
3966

4067

41-
function buildRegionSurfaceMap(context) {
42-
const {world, worldPoint, zone, zoneRect} = context
43-
const regionSurfaceMap = new Map()
44-
const isLand = world.surface.isLand(worldPoint)
45-
const isLake = world.surface.isLake(worldPoint)
46-
// read first edges, then corners
47-
// const gridPoints = [...getEdgePoints(zoneRect), ...getCornerPoints(zoneRect)]
48-
// for (let [zonePoint, direction] of gridPoints) {
49-
// const regionId = zone.topology.getRegion(zonePoint)
50-
51-
// const worldSidePoint = Point.atDirection(worldPoint, direction)
52-
// const sideSurface = world.surface.get(worldSidePoint)
53-
// // rule for lake zone
54-
// if (isLake && world.surface.isLand(worldSidePoint)) {
55-
// regionSurfaceMap.set(regionId, sideSurface)
56-
// }
57-
// // rule for general land zone
58-
// const isSideOcean = world.surface.isOcean(worldSidePoint)
59-
// const isSideSea = world.surface.isSea(worldSidePoint)
60-
// if (isLand && (isSideOcean || isSideSea)) {
61-
// regionSurfaceMap.set(regionId, sideSurface)
62-
// }
63-
// }
64-
return regionSurfaceMap
68+
function isWaterChannel(context, direction) {
69+
const {world, worldPoint} = context
70+
const ortoDirs = Direction.getComponents(direction)
71+
if (ortoDirs.length != 2) return false
72+
const isWater1 = world.surface.isWater(Point.atDirection(worldPoint, ortoDirs[0]))
73+
const isWater2 = world.surface.isWater(Point.atDirection(worldPoint, ortoDirs[1]))
74+
return isWater1 && isWater2
6575
}
6676

6777

78+
function selectRegionsInLine(context, target, direction) {
79+
const {worldPoint, regionGrid, zoneRect} = context
80+
const regions = []
81+
// regionGrid.get(zonePoint)
82+
return regions
83+
}
6884

6985

70-
function getCornerPoints(rect) {
71-
const xMax = rect.width - 1
72-
const yMax = rect.height - 1
73-
return [
74-
[[0, 0], Direction.NORTHWEST],
75-
[[xMax, 0], Direction.NORTHEAST],
76-
[[0, yMax], Direction.SOUTHWEST],
77-
[[xMax, yMax], Direction.SOUTHEAST],
78-
]
86+
function buildLandMaskGrid(context) {
87+
/*
88+
Generate a boolean grid (land or water)
89+
*/
90+
const {worldPoint, world, rect, zoneRect} = context
91+
const relativePoint = Point.multiplyScalar(worldPoint, zoneRect.width)
92+
const noiseRect = Rect.multiply(rect, zoneRect.width)
93+
return Grid.fromRect(zoneRect, zonePoint => {
94+
const noisePoint = Point.plus(relativePoint, zonePoint)
95+
const noise = world.noise.get4DZoneOutline(noiseRect, noisePoint)
96+
return noise > SURFACE_NOISE_RATIO
97+
})
7998
}
8099

81100

src/model/tilemap/worldmap/zone/surface/region.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Grid } from '/src/lib/grid'
22
import { Color } from '/src/lib/color'
3-
import { Direction } from '/src/lib/direction'
4-
import { Rect } from '/src/lib/geometry/rect'
53
import { Point } from '/src/lib/geometry/point'
64
import { ConcurrentFill } from '/src/lib/floodfill/concurrent'
75
import { Random } from '/src/lib/random'
@@ -59,13 +57,7 @@ class RegionFloodFill extends ConcurrentFill {
5957
return fill.context.regionGrid.get(fillPoint) === EMPTY
6058
}
6159

62-
onFill(fill, fillPoint, center) {
63-
const {zoneRect, regionGrid, worldPoint, regionTypes} = fill.context
64-
if (Point.equals(worldPoint, [39, 8])) {
65-
66-
}
67-
regionGrid.set(fillPoint, fill.id)
60+
onFill(fill, fillPoint) {
61+
fill.context.regionGrid.set(fillPoint, fill.id)
6862
}
69-
70-
7163
}

0 commit comments

Comments
 (0)