Skip to content

Commit 2e078e2

Browse files
committed
Fix partial region surface
1 parent 75df8ad commit 2e078e2

File tree

5 files changed

+64
-42
lines changed

5 files changed

+64
-42
lines changed

src/lib/geometry/point/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ const ADJACENT_NEIGHBORHOOD = [
99
[ 0, 1, Direction.SOUTH]
1010
]
1111

12-
13-
const AROUND_NEIGHBORHOOD = ADJACENT_NEIGHBORHOOD.concat([
12+
const DIAGONAL_NEIGHBORHOOD = [
1413
[-1, -1, Direction.NORTHWEST],
1514
[ 1, 1, Direction.SOUTHEAST],
1615
[ 1, -1, Direction.NORTHEAST],
1716
[-1, 1, Direction.SOUTHWEST],
18-
])
17+
]
18+
19+
const AROUND_NEIGHBORHOOD = ADJACENT_NEIGHBORHOOD.concat(DIAGONAL_NEIGHBORHOOD)
1920

2021

2122
export class Point {
@@ -72,6 +73,16 @@ export class Point {
7273
return points
7374
}
7475

76+
static diagonals(center, callback=()=>{}) {
77+
const points = []
78+
for (let [x, y, direction] of DIAGONAL_NEIGHBORHOOD) {
79+
const point = Point.plus(center, [x, y])
80+
callback(point, direction)
81+
points.push(point)
82+
}
83+
return points
84+
}
85+
7586
static around(center, callback=()=>{}) {
7687
const points = []
7788
for (let [x, y, direction] of AROUND_NEIGHBORHOOD) {

src/model/tilemap/worldmap/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import { RiverZone } from './zone/river'
2626

2727
const SCHEMA = new Schema(
2828
'WorldTileMap',
29-
Type.number('size', 'Size', { default: 100, min: 64, max: 200 }),
29+
Type.number('size', 'Size', { default: 32, min: 32, max: 32 }),
3030
Type.text('seed', 'Seed', { default: '' }),
31-
Type.number('realms', 'Realms', { default: 10, min: 3, max: 16 }),
31+
Type.number('realms', 'Realms', { default: 8, min: 3, max: 8 }),
3232
)
3333

3434

35-
const ZONE_SIZE = 17
35+
const ZONE_SIZE = 13
3636
const ZONE_RECT = new Rect(ZONE_SIZE, ZONE_SIZE)
3737

3838

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,17 @@ export class SurfaceZone {
4444
const zoneCanvasPoint = Point.plus(canvasPoint, [ySize, xSize])
4545
const surface = this.get(zonePoint)
4646
let color = surface.color
47-
// if (Point.equals(tilePoint, [42, 4])) {
47+
// if (Point.equals(tilePoint, [39, 8])) {
4848
// const regionId = this._regionGrid.get(zonePoint)
49-
// const direction = Direction.fromId(this._regionDirMap.get(regionId))
50-
// const c = direction.id + 1
51-
// color = new Color(c * 10, c * 20, c * 30)
49+
// color = new Color(regionId * 10, regionId * 20, regionId * 30))
5250
// }
53-
if (world.surface.isBorder(tilePoint)) {
54-
if (world.surface.isWater(tilePoint)) {
55-
color = color.average(Color.BLUE)
56-
} else color = color.average(Color.DARKGREEN)
57-
color = color.darken(20)
51+
// if (world.surface.isBorder(tilePoint)) {
52+
// if (world.surface.isWater(tilePoint)) {
53+
// color = color.average(Color.BLUE)
54+
// } else color = color.average(Color.DARKGREEN)
55+
// color = color.darken(20)
5856

59-
}
57+
// }
6058
canvas.rect(zoneCanvasPoint, size, color.toHex())
6159
}
6260
}

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export function buildModel(context) {
2020
return SURFACE_GRID_CACHE.get(hash)
2121
}
2222
const regionGridContext = buildRegionGridMap(context)
23-
const regionSurfaceMap = buildDirectionTypeMap(context)
24-
const landMaskGrid = buildLandMaskGrid({...context, ...regionGridContext, regionSurfaceMap})
23+
const directionTypeMap = buildDirectionTypeMap(context)
24+
const landMaskGrid = buildLandMaskGrid({...context, ...regionGridContext, directionTypeMap})
2525
const model = {landMaskGrid, ...regionGridContext}
2626
SURFACE_GRID_CACHE.set(hash, model)
2727
return model
@@ -30,45 +30,57 @@ export function buildModel(context) {
3030

3131
function buildDirectionTypeMap(context) {
3232
const {world, worldPoint} = context
33+
const surfaceLayer = world.surface
3334
const directionTypeMap = new Map()
34-
const isWorldPointLand = world.surface.isLand(worldPoint)
35-
35+
const isWorldPointLand = surfaceLayer.isLand(worldPoint)
36+
// set center region
37+
directionTypeMap.set(Direction.CENTER.id, isWorldPointLand)
38+
// set adjacents regions
3639
Point.adjacents(worldPoint, (sidePoint, direction) => {
37-
const isSideLand = world.surface.isLand(sidePoint)
40+
const isSideLand = surfaceLayer.isLand(sidePoint)
41+
if (isWorldPointLand == isSideLand) {
42+
directionTypeMap.set(direction.id, isSideLand)
43+
}
44+
})
45+
// set diagonal regions
46+
Point.diagonals(worldPoint, (sidePoint, direction) => {
47+
const ortoDirs = Direction.getComponents(direction)
48+
const orto1 = Point.atDirection(worldPoint, ortoDirs[0])
49+
const orto2 = Point.atDirection(worldPoint, ortoDirs[1])
3850
if (isWorldPointLand) {
39-
if (isSideLand) {
40-
const type = isWaterChannel(context, direction) ? REGION_WATER : REGION_LAND
41-
directionTypeMap.set(direction.id, type)
42-
}
43-
} else if (! isSideLand) {
44-
directionTypeMap.set(direction.id, REGION_WATER)
51+
const sameType = surfaceLayer.isLand(sidePoint)
52+
const ortoDiff = surfaceLayer.isWater(orto1) && surfaceLayer.isWater(orto2)
53+
if (sameType && ortoDiff)
54+
directionTypeMap.set(direction.id, REGION_WATER)
55+
} else {
56+
const sameType = surfaceLayer.isWater(sidePoint)
57+
const ortoDiff = surfaceLayer.isLand(orto1) && surfaceLayer.isLand(orto2)
58+
if (sameType && ortoDiff)
59+
directionTypeMap.set(direction.id, REGION_WATER)
4560
}
4661
})
4762
return directionTypeMap
4863
}
4964

5065

51-
function isWaterChannel(context, direction) {
52-
const {world, worldPoint} = context
53-
const ortoDirs = Direction.getComponents(direction)
54-
if (ortoDirs.length != 2) return false
55-
const isWater1 = world.surface.isWater(Point.atDirection(worldPoint, ortoDirs[0]))
56-
const isWater2 = world.surface.isWater(Point.atDirection(worldPoint, ortoDirs[1]))
57-
return isWater1 && isWater2
58-
}
59-
60-
6166
function buildLandMaskGrid(context) {
62-
/*
63-
Generate a boolean grid (land or water)
64-
*/
65-
const {worldPoint, world, rect, zoneRect} = context
67+
// Generate a boolean grid (land or water)
68+
const {
69+
worldPoint, world, rect, zoneRect, regionGrid,
70+
directionTypeMap, regionDirMap
71+
} = context
6672
const relativePoint = Point.multiplyScalar(worldPoint, zoneRect.width)
6773
const noiseRect = Rect.multiply(rect, zoneRect.width)
6874
return Grid.fromRect(zoneRect, zonePoint => {
6975
// if (! world.surface.isBorder(worldPoint)) {
7076
// return world.surface.isLand(worldPoint)
7177
// }
78+
const regionId = regionGrid.get(zonePoint)
79+
const regionDirId = regionDirMap.get(regionId)
80+
const regionType = directionTypeMap.get(regionDirId)
81+
// if (regionType === REGION_LAND || regionType === REGION_WATER) {
82+
// return regionType
83+
// }
7284
const noisePoint = Point.plus(relativePoint, zonePoint)
7385
const noise = world.noise.get4DZoneOutline(noiseRect, noisePoint)
7486
return noise > SURFACE_NOISE_RATIO ? REGION_LAND : REGION_WATER

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { EvenPointSampling } from '/src/lib/geometry/point/sampling'
77

88

99
const EMPTY = null
10-
const REGION_SCALE = 3 // distance between region origins
10+
const REGION_SCALE = 2 // distance between region origins
1111
const REGION_GROWTH = [2, 1]
1212
const REGION_CHANCE = .1
1313
const REGION_CENTER_SIZE = 7
@@ -26,6 +26,7 @@ export function buildRegionGridMap(context) {
2626
const regionIdMap = new Map(origins.map((origin, id) => {
2727
const direction = getDirection(zoneRect, origin, REGION_CENTER_SIZE)
2828
regionDirMap.set(id, direction.id)
29+
2930
// set the origin for each region
3031
originMap.set(id, origin)
3132
return [id, {origin}]

0 commit comments

Comments
 (0)