Clean code of Belgian Uplands
- The heights are now a constant array. Instead of pushing the heights to an array. - The terrain are now always constructed in one expression. Instead of calling `.concat` repeatedly. - The actors and the probability thereof are now also part of the object. Separation of data and code. - Player placement is now a iife. Comments by: @Stan, @sera Accepted by: @elexis Differential Revision: https://code.wildfiregames.com/D5272 This was SVN commit r28098.
This commit is contained in:
parent
000b1d8148
commit
163063c2a4
@ -5,9 +5,7 @@ Engine.LoadLibrary("heightmap");
|
||||
const tPrimary = ["temp_grass", "temp_grass_b", "temp_grass_c", "temp_grass_d",
|
||||
"temp_grass_long_b", "temp_grass_clovers_2", "temp_grass_mossy", "temp_grass_plants"];
|
||||
|
||||
const heightLand = 0;
|
||||
const g_Map = new RandomMap(heightLand, tPrimary);
|
||||
const numPlayers = getNumPlayers();
|
||||
const g_Map = new RandomMap(0, tPrimary);
|
||||
const mapSize = g_Map.getSize();
|
||||
const mapCenter = g_Map.getCenter();
|
||||
|
||||
@ -21,68 +19,177 @@ const heightSeaGround = -MIN_HEIGHT + heightRange.min + averageWaterCoverage * (
|
||||
const heightSeaGroundAdjusted = heightSeaGround + MIN_HEIGHT;
|
||||
setWaterHeight(heightSeaGround);
|
||||
|
||||
const textueByHeight = [];
|
||||
|
||||
const terrainTypes = [
|
||||
// Deep water
|
||||
textueByHeight.push({"upperHeightLimit": heightRange.min + 1/3 * (heightSeaGroundAdjusted - heightRange.min), "terrain": "temp_sea_rocks"});
|
||||
{
|
||||
"upperHeightLimit": heightRange.min + 1/3 * (heightSeaGroundAdjusted - heightRange.min),
|
||||
"terrain": "temp_sea_rocks",
|
||||
"actors": [
|
||||
[100, "actor|props/flora/pond_lillies_large.xml"],
|
||||
[40, "actor|props/flora/water_lillies.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Medium deep water (with fish)
|
||||
var terrains = ["temp_sea_weed"];
|
||||
terrains = terrains.concat(terrains, terrains, terrains, terrains);
|
||||
terrains = terrains.concat(terrains, terrains, terrains, terrains);
|
||||
terrains.push("temp_sea_weed|gaia/fish/generic");
|
||||
textueByHeight.push({"upperHeightLimit": heightRange.min + 2/3 * (heightSeaGroundAdjusted - heightRange.min), "terrain": terrains});
|
||||
{
|
||||
"upperHeightLimit": heightRange.min + 2/3 * (heightSeaGroundAdjusted - heightRange.min),
|
||||
"terrain": [
|
||||
Array(25).fill("temp_sea_weed"),
|
||||
"temp_sea_weed|gaia/fish/generic"
|
||||
].flat(),
|
||||
"actors": [
|
||||
[200, "actor|props/flora/pond_lillies_large.xml"],
|
||||
[100, "actor|props/flora/water_lillies.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Flat Water
|
||||
textueByHeight.push({"upperHeightLimit": heightRange.min + 3/3 * (heightSeaGroundAdjusted - heightRange.min), "terrain": "temp_mud_a"});
|
||||
{
|
||||
"upperHeightLimit": heightRange.min + 3/3 * (heightSeaGroundAdjusted - heightRange.min),
|
||||
"terrain": "temp_mud_a",
|
||||
"actors": [
|
||||
[200, "actor|props/flora/water_log.xml"],
|
||||
[100, "actor|props/flora/water_lillies.xml"],
|
||||
[40, "actor|geology/highland_c.xml"],
|
||||
[20, "actor|props/flora/reeds_pond_lush_b.xml"],
|
||||
[10, "actor|props/flora/reeds_pond_lush_a.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Water surroundings/bog (with stone/metal some rabits and bushes)
|
||||
var terrains = ["temp_plants_bog", "temp_plants_bog_aut", "temp_dirt_gravel_plants", "temp_grass_d"];
|
||||
terrains = terrains.concat(terrains, terrains, terrains, terrains, terrains);
|
||||
terrains = ["temp_plants_bog|gaia/tree/bush_temperate"].concat(terrains, terrains);
|
||||
terrains = ["temp_dirt_gravel_plants|gaia/ore/temperate_small", "temp_dirt_gravel_plants|gaia/rock/temperate_small", "temp_plants_bog|gaia/fauna_rabbit"].concat(terrains, terrains);
|
||||
terrains = ["temp_plants_bog_aut|gaia/tree/dead"].concat(terrains, terrains);
|
||||
textueByHeight.push({"upperHeightLimit": heightSeaGroundAdjusted + 1/6 * (heightRange.max - heightSeaGroundAdjusted), "terrain": terrains});
|
||||
{
|
||||
"upperHeightLimit": heightSeaGroundAdjusted + 1/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": [
|
||||
Array(48).fill([
|
||||
"temp_plants_bog",
|
||||
"temp_plants_bog_aut",
|
||||
"temp_dirt_gravel_plants",
|
||||
"temp_grass_d"
|
||||
]),
|
||||
Array(4).fill("temp_plants_bog|gaia/tree/bush_temperate"),
|
||||
Array(2).fill([
|
||||
"temp_dirt_gravel_plants|gaia/ore/temperate_small",
|
||||
"temp_dirt_gravel_plants|gaia/rock/temperate_small",
|
||||
"temp_plants_bog|gaia/fauna_rabbit"
|
||||
]),
|
||||
"temp_plants_bog_aut|gaia/tree/dead"
|
||||
].flat(2),
|
||||
"actors": [
|
||||
[200, "actor|props/flora/water_log.xml"],
|
||||
[100, "actor|geology/highland_c.xml"],
|
||||
[40, "actor|props/flora/reeds_pond_lush_a.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Juicy grass near bog
|
||||
textueByHeight.push({"upperHeightLimit": heightSeaGroundAdjusted + 2/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": ["temp_grass", "temp_grass_d", "temp_grass_long_b", "temp_grass_plants"]});
|
||||
{
|
||||
"upperHeightLimit": heightSeaGroundAdjusted + 2/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": [
|
||||
"temp_grass",
|
||||
"temp_grass_d",
|
||||
"temp_grass_long_b",
|
||||
"temp_grass_plants"
|
||||
],
|
||||
"actors": [
|
||||
[800, "actor|props/flora/grass_field_flowering_tall.xml"],
|
||||
[400, "actor|geology/gray_rock1.xml"],
|
||||
[200, "actor|props/flora/bush_tempe_sm_lush.xml"],
|
||||
[100, "actor|props/flora/bush_tempe_b.xml"],
|
||||
[40, "actor|props/flora/grass_soft_small_tall.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Medium level grass
|
||||
// const testActor = "actor|geology/decal_stone_medit_a.xml";
|
||||
textueByHeight.push({"upperHeightLimit": heightSeaGroundAdjusted + 3/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": ["temp_grass", "temp_grass_b", "temp_grass_c", "temp_grass_mossy"]});
|
||||
{
|
||||
"upperHeightLimit": heightSeaGroundAdjusted + 3/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": [
|
||||
"temp_grass",
|
||||
"temp_grass_b",
|
||||
"temp_grass_c",
|
||||
"temp_grass_mossy"
|
||||
],
|
||||
"actors": [
|
||||
[800, "actor|geology/decal_stone_medit_a.xml"],
|
||||
[400, "actor|props/flora/decals_flowers_daisies.xml"],
|
||||
[200, "actor|props/flora/bush_tempe_underbrush.xml"],
|
||||
[100, "actor|props/flora/grass_soft_small_tall.xml"],
|
||||
[40, "actor|props/flora/grass_temp_field.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Long grass near forest border
|
||||
textueByHeight.push({"upperHeightLimit": heightSeaGroundAdjusted + 4/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": ["temp_grass", "temp_grass_b", "temp_grass_c", "temp_grass_d", "temp_grass_long_b", "temp_grass_clovers_2", "temp_grass_mossy", "temp_grass_plants"]});
|
||||
{
|
||||
"upperHeightLimit": heightSeaGroundAdjusted + 4/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": [
|
||||
"temp_grass",
|
||||
"temp_grass_b",
|
||||
"temp_grass_c",
|
||||
"temp_grass_d",
|
||||
"temp_grass_long_b",
|
||||
"temp_grass_clovers_2",
|
||||
"temp_grass_mossy",
|
||||
"temp_grass_plants"
|
||||
],
|
||||
"actors": [
|
||||
[400, "actor|geology/stone_granite_boulder.xml"],
|
||||
[200, "actor|props/flora/foliagebush.xml"],
|
||||
[100, "actor|props/flora/bush_tempe_underbrush.xml"],
|
||||
[40, "actor|props/flora/grass_soft_small_tall.xml"],
|
||||
[20, "actor|props/flora/ferns.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Forest border (With wood/food plants/deer/rabits)
|
||||
var terrains = ["temp_grass_plants|gaia/tree/euro_beech", "temp_grass_mossy|gaia/tree/poplar", "temp_grass_mossy|gaia/tree/poplar_lombardy",
|
||||
"temp_grass_long|gaia/tree/bush_temperate", "temp_mud_plants|gaia/tree/bush_temperate", "temp_mud_plants|gaia/tree/bush_badlands",
|
||||
"temp_grass_long|gaia/fruit/apple", "temp_grass_clovers|gaia/fruit/berry_01", "temp_grass_clovers_2|gaia/fruit/grapes",
|
||||
"temp_grass_plants|gaia/fauna_deer", "temp_grass_long_b|gaia/fauna_rabbit"];
|
||||
|
||||
const numTerrains = terrains.length;
|
||||
for (let i = 0; i < numTerrains; i++)
|
||||
terrains.push("temp_grass_plants");
|
||||
textueByHeight.push({"upperHeightLimit": heightSeaGroundAdjusted + 5/6 * (heightRange.max - heightSeaGroundAdjusted), "terrain": terrains});
|
||||
{
|
||||
"upperHeightLimit": heightSeaGroundAdjusted + 5/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": [
|
||||
"temp_grass_plants|gaia/tree/euro_beech",
|
||||
"temp_grass_mossy|gaia/tree/poplar",
|
||||
"temp_grass_mossy|gaia/tree/poplar_lombardy",
|
||||
"temp_grass_long|gaia/tree/bush_temperate",
|
||||
"temp_mud_plants|gaia/tree/bush_temperate",
|
||||
"temp_mud_plants|gaia/tree/bush_badlands",
|
||||
"temp_grass_long|gaia/fruit/apple",
|
||||
"temp_grass_clovers|gaia/fruit/berry_01",
|
||||
"temp_grass_clovers_2|gaia/fruit/grapes",
|
||||
"temp_grass_plants|gaia/fauna_deer",
|
||||
"temp_grass_long_b|gaia/fauna_rabbit"
|
||||
].flatMap(t => [t, "temp_grass_plants"]),
|
||||
"actors": [
|
||||
[400, "actor|geology/highland_c.xml"],
|
||||
[200, "actor|props/flora/bush_tempe_a.xml"],
|
||||
[100, "actor|props/flora/ferns.xml"],
|
||||
[40, "actor|props/flora/grass_soft_tuft_a.xml"]
|
||||
]
|
||||
},
|
||||
|
||||
// Unpassable woods
|
||||
textueByHeight.push({"upperHeightLimit": heightSeaGroundAdjusted + 6/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": ["temp_grass_mossy|gaia/tree/oak", "temp_forestfloor_pine|gaia/tree/pine",
|
||||
"temp_grass_mossy|gaia/tree/oak", "temp_forestfloor_pine|gaia/tree/pine",
|
||||
"temp_mud_plants|gaia/tree/dead", "temp_plants_bog|gaia/tree/oak_large",
|
||||
"temp_dirt_gravel_plants|gaia/tree/aleppo_pine", "temp_forestfloor_autumn|gaia/tree/carob"]});
|
||||
{
|
||||
"upperHeightLimit": heightSeaGroundAdjusted + 6/6 * (heightRange.max - heightSeaGroundAdjusted),
|
||||
"terrain": [
|
||||
"temp_grass_mossy|gaia/tree/oak",
|
||||
"temp_forestfloor_pine|gaia/tree/pine",
|
||||
"temp_grass_mossy|gaia/tree/oak",
|
||||
"temp_forestfloor_pine|gaia/tree/pine",
|
||||
"temp_mud_plants|gaia/tree/dead",
|
||||
"temp_plants_bog|gaia/tree/oak_large",
|
||||
"temp_dirt_gravel_plants|gaia/tree/aleppo_pine",
|
||||
"temp_forestfloor_autumn|gaia/tree/carob"
|
||||
],
|
||||
"actors": [
|
||||
[200, "actor|geology/highland2_moss.xml"],
|
||||
[100, "actor|props/flora/grass_soft_tuft_a.xml"],
|
||||
[40, "actor|props/flora/ferns.xml"]
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
Engine.SetProgress(5);
|
||||
|
||||
const lowerHeightLimit = textueByHeight[3].upperHeightLimit;
|
||||
const upperHeightLimit = textueByHeight[6].upperHeightLimit;
|
||||
|
||||
let playerPosition;
|
||||
let playerIDs;
|
||||
const lowerHeightLimit = terrainTypes[3].upperHeightLimit;
|
||||
const upperHeightLimit = terrainTypes[6].upperHeightLimit;
|
||||
|
||||
const [playerIDs, playerPosition] = (() => {
|
||||
while (true)
|
||||
{
|
||||
g_Map.log("Randomizing heightmap");
|
||||
@ -108,142 +215,30 @@ while (true)
|
||||
|
||||
const players = area && playerPlacementRandom(sortAllPlayers(), stayClasses(tHeightRange, 15), true);
|
||||
if (players)
|
||||
{
|
||||
[playerIDs, playerPosition] = players;
|
||||
break;
|
||||
}
|
||||
return players;
|
||||
|
||||
g_Map.log("Too few starting locations");
|
||||
}
|
||||
})();
|
||||
|
||||
Engine.SetProgress(60);
|
||||
|
||||
g_Map.log("Painting terrain by height and add props");
|
||||
let propDensity = 1; // 1 means as determined in the loop, less for large maps as set below
|
||||
if (mapSize > 500)
|
||||
propDensity = 1/4;
|
||||
else if (mapSize > 400)
|
||||
propDensity = 3/4;
|
||||
// 1 means as determined in the loop, less for large maps as set below
|
||||
const propDensity = mapSize > 500 ? 1 / 4 : mapSize > 400 ? 3 / 4 : 1;
|
||||
|
||||
for (let x = 0; x < mapSize; ++x)
|
||||
for (let y = 0; y < mapSize; ++y)
|
||||
{
|
||||
const position = new Vector2D(x, y);
|
||||
if (!g_Map.validHeight(position))
|
||||
if (!g_Map.validHeight(position) || g_Map.getHeight(position) < heightRange.min)
|
||||
continue;
|
||||
|
||||
let textureMinHeight = heightRange.min;
|
||||
for (let i = 0; i < textueByHeight.length; i++)
|
||||
{
|
||||
if (g_Map.getHeight(position) >= textureMinHeight && g_Map.getHeight(position) <= textueByHeight[i].upperHeightLimit)
|
||||
{
|
||||
createTerrain(textueByHeight[i].terrain).place(position);
|
||||
|
||||
let template;
|
||||
|
||||
if (i == 0) // ...deep water
|
||||
{
|
||||
if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/pond_lillies_large.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|props/flora/water_lillies.xml";
|
||||
}
|
||||
if (i == 1) // ...medium water (with fish)
|
||||
{
|
||||
if (randBool(propDensity / 200))
|
||||
template = "actor|props/flora/pond_lillies_large.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/water_lillies.xml";
|
||||
}
|
||||
if (i == 2) // ...low water/mud
|
||||
{
|
||||
if (randBool(propDensity / 200))
|
||||
template = "actor|props/flora/water_log.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/water_lillies.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|geology/highland_c.xml";
|
||||
else if (randBool(propDensity / 20))
|
||||
template = "actor|props/flora/reeds_pond_lush_b.xml";
|
||||
else if (randBool(propDensity / 10))
|
||||
template = "actor|props/flora/reeds_pond_lush_a.xml";
|
||||
}
|
||||
if (i == 3) // ...water suroundings/bog
|
||||
{
|
||||
if (randBool(propDensity / 200))
|
||||
template = "actor|props/flora/water_log.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|geology/highland_c.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|props/flora/reeds_pond_lush_a.xml";
|
||||
}
|
||||
if (i == 4) // ...low height grass
|
||||
{
|
||||
if (randBool(propDensity / 800))
|
||||
template = "actor|props/flora/grass_field_flowering_tall.xml";
|
||||
else if (randBool(propDensity / 400))
|
||||
template = "actor|geology/gray_rock1.xml";
|
||||
else if (randBool(propDensity / 200))
|
||||
template = "actor|props/flora/bush_tempe_sm_lush.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/bush_tempe_b.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|props/flora/grass_soft_small_tall.xml";
|
||||
}
|
||||
if (i == 5) // ...medium height grass
|
||||
{
|
||||
if (randBool(propDensity / 800))
|
||||
template = "actor|geology/decal_stone_medit_a.xml";
|
||||
else if (randBool(propDensity / 400))
|
||||
template = "actor|props/flora/decals_flowers_daisies.xml";
|
||||
else if (randBool(propDensity / 200))
|
||||
template = "actor|props/flora/bush_tempe_underbrush.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/grass_soft_small_tall.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|props/flora/grass_temp_field.xml";
|
||||
}
|
||||
if (i == 6) // ...high height grass
|
||||
{
|
||||
if (randBool(propDensity / 400))
|
||||
template = "actor|geology/stone_granite_boulder.xml";
|
||||
else if (randBool(propDensity / 200))
|
||||
template = "actor|props/flora/foliagebush.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/bush_tempe_underbrush.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|props/flora/grass_soft_small_tall.xml";
|
||||
else if (randBool(propDensity / 20))
|
||||
template = "actor|props/flora/ferns.xml";
|
||||
}
|
||||
if (i == 7) // ...forest border (with wood/food plants/deer/rabits)
|
||||
{
|
||||
if (randBool(propDensity / 400))
|
||||
template = "actor|geology/highland_c.xml";
|
||||
else if (randBool(propDensity / 200))
|
||||
template = "actor|props/flora/bush_tempe_a.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/ferns.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|props/flora/grass_soft_tuft_a.xml";
|
||||
}
|
||||
if (i == 8) // ...woods
|
||||
{
|
||||
if (randBool(propDensity / 200))
|
||||
template = "actor|geology/highland2_moss.xml";
|
||||
else if (randBool(propDensity / 100))
|
||||
template = "actor|props/flora/grass_soft_tuft_a.xml";
|
||||
else if (randBool(propDensity / 40))
|
||||
template = "actor|props/flora/ferns.xml";
|
||||
}
|
||||
|
||||
const elem = terrainTypes.find(e => g_Map.getHeight(position) <= e.upperHeightLimit);
|
||||
createTerrain(elem.terrain).place(position);
|
||||
const template = elem.actors.find(([propability]) => randBool(propDensity / propability));
|
||||
if (template)
|
||||
g_Map.placeEntityAnywhere(template, 0, position, randomAngle());
|
||||
|
||||
break;
|
||||
}
|
||||
else
|
||||
textureMinHeight = textueByHeight[i].upperHeightLimit;
|
||||
}
|
||||
g_Map.placeEntityAnywhere(template[1], 0, position, randomAngle());
|
||||
}
|
||||
|
||||
Engine.SetProgress(90);
|
||||
@ -258,26 +253,26 @@ else
|
||||
const resourceSpacing = 1;
|
||||
const resourceCount = 4;
|
||||
|
||||
for (let i = 0; i < numPlayers; ++i)
|
||||
{
|
||||
placeCivDefaultStartingEntities(playerPosition[i], playerIDs[i], false);
|
||||
playerPosition.forEach((position, i) => {
|
||||
placeCivDefaultStartingEntities(position, playerIDs[i], false);
|
||||
|
||||
for (let j = 1; j <= 4; ++j)
|
||||
{
|
||||
const uAngle = BUILDING_ORIENTATION - Math.PI * (2 - j) / 2;
|
||||
for (let k = 0; k < resourceCount; ++k)
|
||||
{
|
||||
const pos = Vector2D.sum([
|
||||
playerPosition[i],
|
||||
g_Map.placeEntityPassable(
|
||||
j % 2 ? "gaia/tree/cypress" : "gaia/fruit/berry_01",
|
||||
0,
|
||||
Vector2D.sum([
|
||||
position,
|
||||
new Vector2D(resourceDistance, 0).rotate(-uAngle),
|
||||
new Vector2D(k * resourceSpacing, 0).rotate(-uAngle - Math.PI / 2),
|
||||
new Vector2D(-0.75 * resourceSpacing * Math.floor(resourceCount / 2), 0).rotate(-uAngle - Math.PI/2)
|
||||
]);
|
||||
|
||||
g_Map.placeEntityPassable(j % 2 ? "gaia/tree/cypress" : "gaia/fruit/berry_01", 0, pos, randomAngle());
|
||||
}
|
||||
}
|
||||
new Vector2D(-0.75 * resourceSpacing * Math.floor(resourceCount / 2), 0)
|
||||
.rotate(-uAngle - Math.PI/2)
|
||||
]),
|
||||
randomAngle());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
g_Map.ExportMap();
|
||||
|
Loading…
Reference in New Issue
Block a user