Merge 'remotes/trunk'
[0ad.git] / binaries / data / mods / public / maps / random / rivers.js
blob02ec39d734d865fc1b1a7415322e6086c5065825
1 Engine.LoadLibrary("rmgen");
2 Engine.LoadLibrary("rmgen-common");
3 Engine.LoadLibrary("rmbiome");
5 function* GenerateMap()
7         setSelectedBiome();
9         const tMainTerrain = g_Terrains.mainTerrain;
10         const tForestFloor1 = g_Terrains.forestFloor1;
11         const tForestFloor2 = g_Terrains.forestFloor2;
12         const tCliff = g_Terrains.cliff;
13         const tTier1Terrain = g_Terrains.tier1Terrain;
14         const tTier2Terrain = g_Terrains.tier2Terrain;
15         const tTier3Terrain = g_Terrains.tier3Terrain;
16         const tHill = g_Terrains.hill;
17         const tRoad = g_Terrains.road;
18         const tRoadWild = g_Terrains.roadWild;
19         const tTier4Terrain = g_Terrains.tier4Terrain;
20         let tShore = g_Terrains.shore;
21         let tWater = g_Terrains.water;
22         if (currentBiome() == "generic/india")
23         {
24                 tShore = "tropic_dirt_b_plants";
25                 tWater = "tropic_dirt_b";
26         }
27         const oTree1 = g_Gaia.tree1;
28         const oTree2 = g_Gaia.tree2;
29         const oTree3 = g_Gaia.tree3;
30         const oTree4 = g_Gaia.tree4;
31         const oTree5 = g_Gaia.tree5;
32         const oFruitBush = g_Gaia.fruitBush;
33         const oMainHuntableAnimal = g_Gaia.mainHuntableAnimal;
34         const oFish = g_Gaia.fish;
35         const oSecondaryHuntableAnimal = g_Gaia.secondaryHuntableAnimal;
36         const oStoneLarge = g_Gaia.stoneLarge;
37         const oStoneSmall = g_Gaia.stoneSmall;
38         const oMetalLarge = g_Gaia.metalLarge;
39         const oMetalSmall = g_Gaia.metalSmall;
41         const aGrass = g_Decoratives.grass;
42         const aGrassShort = g_Decoratives.grassShort;
43         const aReeds = g_Decoratives.reeds;
44         const aLillies = g_Decoratives.lillies;
45         const aRockLarge = g_Decoratives.rockLarge;
46         const aRockMedium = g_Decoratives.rockMedium;
47         const aBushMedium = g_Decoratives.bushMedium;
48         const aBushSmall = g_Decoratives.bushSmall;
50         const pForest1 = [
51                 tForestFloor2 + TERRAIN_SEPARATOR + oTree1,
52                 tForestFloor2 + TERRAIN_SEPARATOR + oTree2,
53                 tForestFloor2
54         ];
55         const pForest2 = [
56                 tForestFloor1 + TERRAIN_SEPARATOR + oTree4,
57                 tForestFloor1 + TERRAIN_SEPARATOR + oTree5,
58                 tForestFloor1
59         ];
61         const heightSeaGround = -3;
62         const heightShallows = -1;
63         const heightLand = 1;
65         globalThis.g_Map = new RandomMap(heightLand, tMainTerrain);
67         const numPlayers = getNumPlayers();
68         const mapSize = g_Map.getSize();
69         const mapCenter = g_Map.getCenter();
71         const clPlayer = g_Map.createTileClass();
72         const clHill = g_Map.createTileClass();
73         const clForest = g_Map.createTileClass();
74         const clWater = g_Map.createTileClass();
75         const clDirt = g_Map.createTileClass();
76         const clRock = g_Map.createTileClass();
77         const clMetal = g_Map.createTileClass();
78         const clFood = g_Map.createTileClass();
79         const clBaseResource = g_Map.createTileClass();
80         const clShallow = g_Map.createTileClass();
82         const [playerIDs, playerPosition, playerAngle, startAngle] =
83                 playerPlacementCircle(fractionToTiles(0.35));
85         placePlayerBases({
86                 "PlayerPlacement": [playerIDs, playerPosition],
87                 "PlayerTileClass": clPlayer,
88                 "BaseResourceClass": clBaseResource,
89                 "CityPatch": {
90                         "outerTerrain": tRoadWild,
91                         "innerTerrain": tRoad
92                 },
93                 "StartingAnimal": {
94                 },
95                 "Berries": {
96                         "template": oFruitBush
97                 },
98                 "Mines": {
99                         "types": [
100                                 { "template": oMetalLarge },
101                                 { "template": oStoneLarge }
102                         ]
103                 },
104                 "Trees": {
105                         "template": oTree1,
106                         "count": 2
107                 },
108                 "Decoratives": {
109                         "template": aGrassShort
110                 }
111         });
113         g_Map.log("Creating central lake");
114         createArea(
115                 new ClumpPlacer(diskArea(fractionToTiles(0.075)), 0.7, 0.1, Infinity, mapCenter),
116                 [
117                         new LayeredPainter([tShore, tWater], [1]),
118                         new SmoothElevationPainter(ELEVATION_SET, heightSeaGround, 4),
119                         new TileClassPainter(clWater)
120                 ]);
122         g_Map.log("Creating rivers between opponents");
123         const numRivers = isNomad() ? randIntInclusive(4, 8) : numPlayers;
124         const rivers = distributePointsOnCircle(numRivers, startAngle + Math.PI / numRivers,
125                 fractionToTiles(0.5), mapCenter)[0];
126         for (let i = 0; i < numRivers; ++i)
127         {
128                 if (isNomad() ? randBool() : areAllies(playerIDs[i], playerIDs[(i + 1) % numPlayers]))
129                         continue;
131                 const shallowLocation = randFloat(0.2, 0.7);
132                 const shallowWidth = randFloat(0.12, 0.21);
134                 paintRiver({
135                         "parallel": true,
136                         "start": rivers[i],
137                         "end": mapCenter,
138                         "width": scaleByMapSize(10, 30),
139                         "fadeDist": 5,
140                         "deviation": 0,
141                         "heightLand": heightLand,
142                         "heightRiverbed": heightSeaGround,
143                         "minHeight": heightSeaGround,
144                         "meanderShort": 10,
145                         "meanderLong": 0,
146                         "waterFunc": (position, height, riverFraction) => {
148                                 clWater.add(position);
150                                 const isShallow = height < heightShallows &&
151                                         riverFraction > shallowLocation &&
152                                         riverFraction < shallowLocation + shallowWidth;
154                                 const newHeight = isShallow ? heightShallows : Math.max(height, heightSeaGround);
156                                 if (g_Map.getHeight(position) < newHeight)
157                                         return;
159                                 g_Map.setHeight(position, newHeight);
160                                 createTerrain(height >= 0 ? tShore : tWater).place(position);
162                                 if (isShallow)
163                                         clShallow.add(position);
164                         }
165                 });
166         }
167         yield 40;
169         createBumps(avoidClasses(clWater, 2, clPlayer, 20));
171         if (randBool())
172                 createHills([tMainTerrain, tCliff, tHill],
173                         avoidClasses(clPlayer, 20, clHill, 15, clWater, 2), clHill, scaleByMapSize(3, 15));
174         else
175                 createMountains(tCliff,
176                         avoidClasses(clPlayer, 20, clHill, 15, clWater, 2), clHill, scaleByMapSize(3, 15));
178         const [forestTrees, stragglerTrees] = getTreeCounts(...rBiomeTreeCount(1));
179         createDefaultForests(
180                 [tMainTerrain, tForestFloor1, tForestFloor2, pForest1, pForest2],
181                 avoidClasses(clPlayer, 20, clForest, 17, clHill, 0, clWater, 2),
182                 clForest,
183                 forestTrees);
185         yield 50;
187         g_Map.log("Creating dirt patches");
188         createLayeredPatches(
189                 [scaleByMapSize(3, 6), scaleByMapSize(5, 10), scaleByMapSize(8, 21)],
190                 [[tMainTerrain, tTier1Terrain], [tTier1Terrain, tTier2Terrain], [tTier2Terrain, tTier3Terrain]],
191                 [1, 1],
192                 avoidClasses(clWater, 3, clForest, 0, clHill, 0, clDirt, 5, clPlayer, 12),
193                 scaleByMapSize(15, 45),
194                 clDirt);
196         g_Map.log("Creating grass patches");
197         createPatches(
198                 [scaleByMapSize(2, 4), scaleByMapSize(3, 7), scaleByMapSize(5, 15)],
199                 tTier4Terrain,
200                 avoidClasses(clWater, 3, clForest, 0, clHill, 0, clDirt, 5, clPlayer, 12),
201                 scaleByMapSize(15, 45),
202                 clDirt);
203         yield 55;
205         g_Map.log("Creating metal mines");
206         createBalancedMetalMines(
207                 oMetalSmall,
208                 oMetalLarge,
209                 clMetal,
210                 avoidClasses(clWater, 3, clForest, 1, clPlayer, scaleByMapSize(20, 35), clHill, 1)
211         );
213         g_Map.log("Creating stone mines");
214         createBalancedStoneMines(
215                 oStoneSmall,
216                 oStoneLarge,
217                 clRock,
218                 avoidClasses(clWater, 3, clForest, 1, clPlayer, scaleByMapSize(20, 35), clHill, 1, clMetal, 10)
219         );
221         yield 65;
223         let planetm = 1;
225         if (currentBiome() == "generic/india")
226                 planetm = 8;
228         createDecoration(
229                 [
230                         [new SimpleObject(aRockMedium, 1, 3, 0, 1)],
231                         [new SimpleObject(aRockLarge, 1, 2, 0, 1), new SimpleObject(aRockMedium, 1, 3, 0, 2)],
232                         [new SimpleObject(aGrassShort, 1, 2, 0, 1)],
233                         [new SimpleObject(aGrass, 2, 4, 0, 1.8), new SimpleObject(aGrassShort, 3, 6, 1.2, 2.5)],
234                         [new SimpleObject(aBushMedium, 1, 2, 0, 2), new SimpleObject(aBushSmall, 2, 4, 0, 2)]
235                 ],
236                 [
237                         scaleByMapAreaAbsolute(16),
238                         scaleByMapAreaAbsolute(8),
239                         planetm * scaleByMapAreaAbsolute(13),
240                         planetm * scaleByMapAreaAbsolute(13),
241                         planetm * scaleByMapAreaAbsolute(13)
242                 ],
243                 avoidClasses(clWater, 0, clForest, 0, clPlayer, 0, clHill, 0));
245         createDecoration(
246                 [
247                         [new SimpleObject(aReeds, 1, 3, 0, 1)],
248                         [new SimpleObject(aLillies, 1, 2, 0, 1)]
249                 ],
250                 [
251                         scaleByMapAreaAbsolute(800),
252                         scaleByMapAreaAbsolute(800)
253                 ],
254                 stayClasses(clShallow, 0));
256         yield 70;
258         createFood(
259                 [
260                         [new SimpleObject(oMainHuntableAnimal, 5, 7, 0, 4)],
261                         [new SimpleObject(oSecondaryHuntableAnimal, 2, 3, 0, 2)]
262                 ],
263                 [
264                         3 * numPlayers,
265                         3 * numPlayers
266                 ],
267                 avoidClasses(clWater, 3, clForest, 0, clPlayer, 20, clHill, 1, clFood, 20),
268                 clFood);
270         createFood(
271                 [
272                         [new SimpleObject(oFruitBush, 5, 7, 0, 4)]
273                 ],
274                 [
275                         3 * numPlayers
276                 ],
277                 avoidClasses(clWater, 3, clForest, 0, clPlayer, 20, clHill, 1, clFood, 10),
278                 clFood);
280         createFood(
281                 [
282                         [new SimpleObject(oFish, 2, 3, 0, 2)]
283                 ],
284                 [
285                         25 * numPlayers
286                 ],
287                 [avoidClasses(clFood, 20), stayClasses(clWater, 6)],
288                 clFood);
290         yield 85;
292         createStragglerTrees(
293                 [oTree1, oTree2, oTree4, oTree3],
294                 avoidClasses(clWater, 5, clForest, 7, clHill, 1, clPlayer, 12, clMetal, 6, clRock, 6),
295                 clForest,
296                 stragglerTrees);
298         placePlayersNomad(clPlayer,
299                 avoidClasses(clWater, 4, clForest, 1, clMetal, 4, clRock, 4, clFood, 2, clHill, 4));
301         setWaterWaviness(3.0);
302         setWaterType("lake");
304         return g_Map;