Line data Source code
1 : /**
2 : * A Centered Placer generates a shape (array of Vector2D points) around a variable center location satisfying a Constraint.
3 : * The center can be modified externally using setCenterPosition, typically called by createAreas.
4 : */
5 7 : Engine.LoadLibrary("rmgen/placer/centered");
6 :
7 : /**
8 : * A Non-Centered Placer generates a shape (array of Vector2D points) at a fixed location meeting a Constraint and
9 : * is typically called by createArea.
10 : * Since this type of Placer has no x and z property, its location cannot be randomized using createAreas.
11 : */
12 7 : Engine.LoadLibrary("rmgen/placer/noncentered");
13 :
14 : /**
15 : * A Painter modifies an arbitrary feature in a given Area, for instance terrain textures, elevation or calling other painters on that Area.
16 : * Typically the area is determined by a Placer called from createArea or createAreas.
17 : */
18 7 : Engine.LoadLibrary("rmgen/painter");
19 :
20 7 : const TERRAIN_SEPARATOR = "|";
21 7 : const SEA_LEVEL = 20.0;
22 7 : const HEIGHT_UNITS_PER_METRE = 92;
23 :
24 : /**
25 : * Constants needed for heightmap_manipulation.js
26 : */
27 7 : const MAX_HEIGHT_RANGE = 0xFFFF / HEIGHT_UNITS_PER_METRE; // Engine limit, Roughly 700 meters
28 7 : const MIN_HEIGHT = -SEA_LEVEL;
29 :
30 7 : const MAX_HEIGHT = MAX_HEIGHT_RANGE - SEA_LEVEL;
31 :
32 : /**
33 : * Default angle for buildings.
34 : */
35 7 : const BUILDING_ORIENTATION = -1/4 * Math.PI;
36 :
37 7 : const g_CivData = deepfreeze(loadCivFiles(false));
38 :
39 7 : const g_ActorPrefix = "actor|";
40 :
41 : /**
42 : * Sets whether setHeight operates on the center of a tile or on the vertices.
43 : */
44 7 : var TILE_CENTERED_HEIGHT_MAP = false;
45 :
46 : function actorTemplate(templateName)
47 : {
48 0 : return g_ActorPrefix + templateName + ".xml";
49 : }
50 :
51 : function getObstructionSize(templateName, margin = 0)
52 : {
53 0 : const obstruction = Engine.GetTemplate(templateName).Obstruction;
54 :
55 : const obstructionSize =
56 0 : obstruction.Static ?
57 : new Vector2D(obstruction.Static["@depth"], obstruction.Static["@width"]) :
58 : // Used for gates, should consider the position too
59 : obstruction.Obstructions ?
60 : new Vector2D(
61 0 : Object.keys(obstruction.Obstructions).reduce((depth, key) => Math.max(depth, +obstruction.Obstructions[key]["@depth"]), 0),
62 0 : Object.keys(obstruction.Obstructions).reduce((width, key) => width + +obstruction.Obstructions[key]["@width"], 0)) :
63 : new Vector2D(0, 0);
64 :
65 0 : return obstructionSize.div(TERRAIN_TILE_SIZE).add(new Vector2D(2, 2).mult(margin));
66 : }
67 :
68 : function fractionToTiles(f)
69 : {
70 0 : return g_MapSettings.Size * f;
71 : }
72 :
73 : function tilesToFraction(t)
74 : {
75 0 : return t / g_MapSettings.Size;
76 : }
77 :
78 : function scaleByMapSize(min, max, minMapSize = 128, maxMapSize = 512)
79 : {
80 0 : return min + (max - min) * (g_MapSettings.Size - minMapSize) / (maxMapSize - minMapSize);
81 : }
82 :
83 : /**
84 : * Interpolate quadratic between (min, minMapArea) and (max, maxMapArea) with respect to the mapSize.
85 : * Default values set on the area of tiny and giant map sizes according to the map shape (square or circular).
86 : */
87 : function scaleByMapArea(min, max, minMapArea = g_Map.getArea(128), maxMapArea = g_Map.getArea(512))
88 : {
89 0 : return min + (max - min) * (g_Map.getArea() - minMapArea) / (maxMapArea - minMapArea);
90 : }
91 :
92 : /**
93 : * Interpolate quadraticly between (0,0) and (base, baseArea) with respect to the map size.
94 : * @param base - Value we should attain at baseArea.
95 : * @param disallowedArea - Area deducted from the map area.
96 : * @param baseArea - Area at which the base value should be attained. Defaults to the map area of a tiny map of current shape (square or circular).
97 : */
98 : function scaleByMapAreaAbsolute(base, disallowedArea = 0, baseArea = g_Map.getArea(128))
99 : {
100 0 : return scaleByMapArea(0, base, disallowedArea, baseArea + disallowedArea);
101 : }
102 :
103 : function randomPositionOnTile(tilePosition)
104 : {
105 0 : return Vector2D.add(tilePosition, new Vector2D(randFloat(0, 1), randFloat(0, 1)));
106 : }
107 :
108 : /**
109 : * Retries the given function with those arguments as often as specified.
110 : */
111 : function retryPlacing(placeFunc, retryFactor, amount, behaveDeprecated = false)
112 : {
113 0 : const maxFail = amount * retryFactor;
114 :
115 0 : const results = [];
116 0 : let bad = 0;
117 :
118 0 : while (results.length < amount && bad <= maxFail)
119 : {
120 0 : const result = placeFunc();
121 :
122 0 : if (result !== undefined || behaveDeprecated)
123 0 : results.push(result);
124 : else
125 0 : ++bad;
126 : }
127 :
128 0 : return results;
129 : }
130 :
131 : // TODO this is a hack to simulate the old behaviour of those functions
132 : // until all old maps are changed to use the correct version of these functions
133 : function createObjectGroupsDeprecated(group, player, constraints, amount, retryFactor = 10)
134 : {
135 0 : return createObjectGroups(group, player, constraints, amount, retryFactor, true);
136 : }
137 :
138 : function createObjectGroupsByAreasDeprecated(group, player, constraints, amount, retryFactor, areas)
139 : {
140 0 : return createObjectGroupsByAreas(group, player, constraints, amount, retryFactor, areas, true);
141 : }
142 :
143 : /**
144 : * Attempts to place the given number of areas in random places of the map.
145 : * Returns actually placed areas.
146 : */
147 : function createAreas(centeredPlacer, painter, constraints, amount, retryFactor = 10)
148 : {
149 0 : const placeFunc = function()
150 : {
151 0 : centeredPlacer.setCenterPosition(g_Map.randomCoordinate(false));
152 0 : return createArea(centeredPlacer, painter, constraints);
153 : };
154 :
155 0 : return retryPlacing(placeFunc, retryFactor, amount, false);
156 : }
157 :
158 : /**
159 : * Attempts to place the given number of areas in random places of the given areas.
160 : * Returns actually placed areas.
161 : */
162 : function createAreasInAreas(centeredPlacer, painter, constraints, amount, retryFactor, areas)
163 : {
164 0 : areas = areas.filter(area => area.getPoints().length);
165 0 : if (!areas.length)
166 : {
167 0 : log("createAreasInAreas: 'areas' was either empty or only contained empty areas thus returning an empty array.\n" + new Error().stack);
168 0 : return [];
169 : }
170 :
171 0 : const placeFunc = function()
172 : {
173 0 : centeredPlacer.setCenterPosition(pickRandom(pickRandom(areas).getPoints()));
174 0 : return createArea(centeredPlacer, painter, constraints);
175 : };
176 :
177 0 : return retryPlacing(placeFunc, retryFactor, amount, false);
178 : }
179 :
180 : /**
181 : * Attempts to place the given number of groups in random places of the map.
182 : * Returns the number of actually placed groups.
183 : */
184 : function createObjectGroups(group, player, constraints, amount, retryFactor = 10, behaveDeprecated = false)
185 : {
186 0 : const placeFunc = function()
187 : {
188 0 : group.setCenterPosition(g_Map.randomCoordinate(true));
189 0 : return createObjectGroup(group, player, constraints);
190 : };
191 :
192 0 : return retryPlacing(placeFunc, retryFactor, amount, behaveDeprecated);
193 : }
194 :
195 : /**
196 : * Attempts to place the given number of groups in random places of the given areas.
197 : * Returns the number of actually placed groups.
198 : */
199 : function createObjectGroupsByAreas(group, player, constraints, amount, retryFactor, areas, behaveDeprecated = false)
200 : {
201 0 : areas = areas.filter(area => area.getPoints().length);
202 0 : if (!areas.length)
203 : {
204 0 : log("createObjectGroupsByAreas: 'areas' was either empty or only contained empty areas.\n" + new Error().stack);
205 0 : return [];
206 : }
207 :
208 0 : const placeFunc = function()
209 : {
210 0 : group.setCenterPosition(pickRandom(pickRandom(areas).getPoints()));
211 0 : return createObjectGroup(group, player, constraints);
212 : };
213 :
214 0 : return retryPlacing(placeFunc, retryFactor, amount, behaveDeprecated);
215 : }
216 :
217 : function createTerrain(terrain)
218 : {
219 2 : return typeof terrain === "string" ?
220 : new SimpleTerrain(...terrain.split(TERRAIN_SEPARATOR)) :
221 0 : new RandomTerrain(terrain.map(t => createTerrain(t)));
222 : }
223 :
224 : /**
225 : * Constructs a new Area shaped by the Placer meeting the Constraints and calls the Painters there.
226 : * Supports both Centered and Non-Centered Placers.
227 : */
228 : function createArea(placer, painters, constraints)
229 : {
230 10 : const points = placer.place(new AndConstraint(constraints));
231 10 : if (!points)
232 0 : return undefined;
233 :
234 10 : const area = new Area(points);
235 :
236 10 : new MultiPainter(painters).paint(area);
237 :
238 10 : return area;
239 : }
240 :
241 : /**
242 : * @param mode is one of the HeightPlacer constants determining whether to exclude the min/max elevation.
243 : */
244 : function paintTerrainBasedOnHeight(minHeight, maxHeight, mode, terrain)
245 : {
246 0 : return createArea(
247 : new HeightPlacer(mode, minHeight, maxHeight),
248 : new TerrainPainter(terrain));
249 : }
250 :
251 : function paintTileClassBasedOnHeight(minHeight, maxHeight, mode, tileClass)
252 : {
253 0 : return createArea(
254 : new HeightPlacer(mode, minHeight, maxHeight),
255 : new TileClassPainter(tileClass));
256 : }
257 :
258 : function unPaintTileClassBasedOnHeight(minHeight, maxHeight, mode, tileClass)
259 : {
260 0 : return createArea(
261 : new HeightPlacer(mode, minHeight, maxHeight),
262 : new TileClassUnPainter(tileClass));
263 : }
264 :
265 : /**
266 : * Places the Entities of the given Group if they meet the Constraints
267 : * and sets the given player as the owner.
268 : */
269 : function createObjectGroup(group, player, constraints)
270 : {
271 0 : return group.place(player, new AndConstraint(constraints));
272 : }
273 :
274 : /**
275 : * Create an avoid constraint for the given classes by the given distances
276 : */
277 : function avoidClasses(/* class1, dist1, class2, dist2, etc */)
278 : {
279 0 : const ar = [];
280 0 : for (let i = 0; i < arguments.length/2; ++i)
281 0 : ar.push(new AvoidTileClassConstraint(arguments[2*i], arguments[2*i+1]));
282 :
283 : // Return single constraint
284 0 : if (ar.length == 1)
285 0 : return ar[0];
286 :
287 0 : return new AndConstraint(ar);
288 : }
289 :
290 : /**
291 : * Create a stay constraint for the given classes by the given distances
292 : */
293 : function stayClasses(/* class1, dist1, class2, dist2, etc */)
294 : {
295 0 : const ar = [];
296 0 : for (let i = 0; i < arguments.length/2; ++i)
297 0 : ar.push(new StayInTileClassConstraint(arguments[2*i], arguments[2*i+1]));
298 :
299 : // Return single constraint
300 0 : if (ar.length == 1)
301 0 : return ar[0];
302 :
303 0 : return new AndConstraint(ar);
304 : }
305 :
306 : /**
307 : * Create a border constraint for the given classes by the given distances
308 : */
309 : function borderClasses(/* class1, idist1, odist1, class2, idist2, odist2, etc */)
310 : {
311 0 : const ar = [];
312 0 : for (let i = 0; i < arguments.length/3; ++i)
313 0 : ar.push(new BorderTileClassConstraint(arguments[3*i], arguments[3*i+1], arguments[3*i+2]));
314 :
315 : // Return single constraint
316 0 : if (ar.length == 1)
317 0 : return ar[0];
318 :
319 0 : return new AndConstraint(ar);
320 : }
321 :
322 : /**
323 : * Returns a subset of the given heightmap.
324 : */
325 : function extractHeightmap(heightmap, topLeft, size)
326 : {
327 0 : const result = [];
328 0 : for (let x = 0; x < size; ++x)
329 : {
330 0 : result[x] = new Float32Array(size);
331 0 : for (let y = 0; y < size; ++y)
332 0 : result[x][y] = heightmap[x + topLeft.x][y + topLeft.y];
333 : }
334 0 : return result;
335 : }
336 :
337 : function convertHeightmap1Dto2D(heightmap)
338 : {
339 0 : const result = [];
340 0 : const hmSize = Math.sqrt(heightmap.length);
341 0 : for (let x = 0; x < hmSize; ++x)
342 : {
343 0 : result[x] = new Float32Array(hmSize);
344 0 : for (let y = 0; y < hmSize; ++y)
345 0 : result[x][y] = heightmap[y * hmSize + x];
346 : }
347 0 : return result;
348 : }
349 :
350 : function getDifficulties()
351 : {
352 0 : return Engine.ReadJSONFile("simulation/data/settings/trigger_difficulties.json").Data;
353 : }
354 :
355 : /**
356 : * Returns the numeric difficulty level the player chose.
357 : */
358 : function getDifficulty()
359 : {
360 0 : const level = g_MapSettings.TriggerDifficulty || 3;
361 0 : return getDifficulties().find(difficulty => difficulty.Difficulty == level).Difficulty;
362 : }
|