-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiwotigo-unite-islands.mjs
195 lines (166 loc) · 5.38 KB
/
kiwotigo-unite-islands.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const uniq = (arr) => Array.from(new Set(arr));
function findIslands(continent) {
const visitedRegions = new Set();
const crawlIsland = (regionId) => {
visitedRegions.add(regionId);
const { neighbors } = continent.regions[regionId];
return [
regionId,
...neighbors
.filter((neighborId) => !visitedRegions.has(neighborId))
.map((neighborId) => crawlIsland(neighborId))
.flat(),
];
};
let islands = [];
continent.regions.forEach((region) => {
if (!visitedRegions.has(region.id)) {
islands.push(crawlIsland(region.id));
}
});
// remove duplicates
islands = islands.map(uniq);
const regions = continent.regions.map((region) => {
let islandId;
for (let i = 0; i < islands.length; i++) {
if (islands[i].includes(region.id)) {
islandId = i;
break;
}
}
return {
...region,
islandId,
};
});
return {
...continent,
islands,
regions,
};
}
const calcDistance = (from, to) =>
Math.sqrt(Math.pow(to.x - from.x, 2) + Math.pow(to.y - from.y, 2));
const calcRegionDistances = ({ centerPoint: from }, regions) =>
regions.map(({ centerPoint: to }) => calcDistance(from, to));
const findMinIndex = (arr) => {
let min = arr[0];
let minIndex = 0;
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
minIndex = i;
}
}
return minIndex;
};
const findRegionsWithinOuterRange = ({
continent,
exlcudeIslands,
includeIslands,
exlcudeRegions,
from,
range,
maxRange,
}) =>
continent.regions
.filter(
(region) =>
!exlcudeRegions.includes(region.id) &&
!exlcudeIslands.includes(region.islandId) &&
(!includeIslands || includeIslands.includes(region.islandId))
)
.filter(({ centerPoint: to }) => {
const distance = calcDistance(from, to);
return distance <= maxRange && distance - to.oR <= range;
})
.map((region) => region.id);
function makeNewConnections(regions, connections) {
connections.forEach(([from, to]) => {
regions[from].neighbors.push(to);
regions[to].neighbors.push(from);
});
regions.forEach((region) => {
region.neighbors = uniq(region.neighbors);
});
}
function connectIslands(continent, config) {
const cfg = {
enableExtendedConnections: true,
maxExtendedOuterRangeFactor: 4.0,
...config,
};
let islands = [...continent.islands];
while (islands.length > 1) {
const [curIsland, ...otherIslands] = islands;
const distancesToOtherIslands = curIsland.map((regionId) => {
const region = continent.regions[regionId];
const islandDistances = otherIslands.map(
(otherIsland, otherIslandsIndex) => {
const otherRegions = otherIsland.map((rId) => continent.regions[rId]);
const distancesToOtherRegions = calcRegionDistances(
region,
otherRegions
);
const idx = findMinIndex(distancesToOtherRegions);
return {
regionId: otherRegions[idx].id,
distance: distancesToOtherRegions[idx],
otherIslandsIndex,
};
}
);
const minIndex = findMinIndex(
islandDistances.map(({ distance }) => distance)
);
return {
regionFrom: regionId,
regionTo: islandDistances[minIndex].regionId,
distance: islandDistances[minIndex].distance,
otherIslandsIndex: islandDistances[minIndex].otherIslandsIndex,
islandDistances,
};
});
const minIndex = findMinIndex(
distancesToOtherIslands.map(({ distance }) => distance)
);
const nearest = distancesToOtherIslands[minIndex];
const connections = [[nearest.regionFrom, nearest.regionTo]];
if (cfg.enableExtendedConnections) {
const extendedConnections = [
...findRegionsWithinOuterRange({
continent,
exlcudeRegions: [nearest.regionFrom, nearest.regionTo],
exlcudeIslands: [continent.regions[nearest.regionFrom].islandId],
from: continent.regions[nearest.regionFrom].centerPoint,
range: nearest.distance,
maxRange:
continent.regions[nearest.regionFrom].centerPoint.oR *
cfg.maxExtendedOuterRangeFactor,
}).map((id) => [nearest.regionFrom, id]),
...findRegionsWithinOuterRange({
continent,
exlcudeRegions: [nearest.regionFrom, nearest.regionTo],
exlcudeIslands: [continent.regions[nearest.regionTo].islandId],
// includeIslands: [continent.regions[nearest.regionFrom].islandId],
from: continent.regions[nearest.regionTo].centerPoint,
range: nearest.distance,
maxRange:
continent.regions[nearest.regionTo].centerPoint.oR *
cfg.maxExtendedOuterRangeFactor,
}).map((id) => [nearest.regionTo, id]),
];
connections.push(...extendedConnections);
makeNewConnections(continent.regions, connections);
// TODO find sharp triangular relationships to filter out triangles which are too flat
} else {
makeNewConnections(continent.regions, connections);
}
curIsland.push(...otherIslands[nearest.otherIslandsIndex]);
otherIslands.splice(nearest.otherIslandsIndex, 1);
islands = [curIsland, ...otherIslands];
}
return continent;
}
export const findAndConnectAllIslands = (continent, config) =>
connectIslands(findIslands(continent), config);