| 1 | // Prepare progress calculation |
| 2 | // var timeArray = []; |
| 3 | // timeArray.push(new Date().getTime()); |
| 4 | |
| 5 | // Importing rmgen libraries |
| 6 | RMS.LoadLibrary("rmgen"); |
| 7 | |
| 8 | const BUILDING_ANGlE = -PI/4; |
| 9 | |
| 10 | // initialize map |
| 11 | |
| 12 | log("Initializing map..."); |
| 13 | |
| 14 | InitMap(); |
| 15 | |
| 16 | var numPlayers = getNumPlayers(); |
| 17 | var mapSize = getMapSize(); |
| 18 | |
| 19 | |
| 20 | ////////// |
| 21 | // Heightmap functionality |
| 22 | ////////// |
| 23 | |
| 24 | // Some general heightmap settings |
| 25 | const MIN_HEIGHT = - SEA_LEVEL; // 20, should be set in the libs! |
| 26 | const MAX_HEIGHT = 0xFFFF/HEIGHT_UNITS_PER_METRE - SEA_LEVEL; // A bit smaler than 90, should be set in the libs! |
| 27 | |
| 28 | // Random heightmap generation |
| 29 | function getRandomReliefmap(minHeight, maxHeight) |
| 30 | { |
| 31 | minHeight = (minHeight || MIN_HEIGHT); |
| 32 | maxHeight = (maxHeight || MAX_HEIGHT); |
| 33 | if (minHeight < MIN_HEIGHT) |
| 34 | warn("getRandomReliefmap: Argument minHeight is smaler then the supported minimum height of " + MIN_HEIGHT + " (const MIN_HEIGHT): " + minHeight) |
| 35 | if (maxHeight > MAX_HEIGHT) |
| 36 | warn("getRandomReliefmap: Argument maxHeight is smaler then the supported maximum height of " + MAX_HEIGHT + " (const MAX_HEIGHT): " + maxHeight) |
| 37 | var reliefmap = []; |
| 38 | for (var x = 0; x <= mapSize; x++) |
| 39 | { |
| 40 | reliefmap.push([]); |
| 41 | for (var y = 0; y <= mapSize; y++) |
| 42 | { |
| 43 | reliefmap[x].push(randFloat(minHeight, maxHeight)); |
| 44 | } |
| 45 | } |
| 46 | return reliefmap; |
| 47 | } |
| 48 | |
| 49 | // Apply a heightmap |
| 50 | function setReliefmap(reliefmap) |
| 51 | { |
| 52 | // g_Map.height = reliefmap; |
| 53 | for (var x = 0; x <= mapSize; x++) |
| 54 | { |
| 55 | for (var y = 0; y <= mapSize; y++) |
| 56 | { |
| 57 | setHeight(x, y, reliefmap[x][y]); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Get minimum and maxumum height used in a heightmap |
| 63 | function getMinAndMaxHeight(reliefmap) |
| 64 | { |
| 65 | var height = {}; |
| 66 | height.min = Infinity; |
| 67 | height.max = -Infinity; |
| 68 | for (var x = 0; x <= mapSize; x++) |
| 69 | { |
| 70 | for (var y = 0; y <= mapSize; y++) |
| 71 | { |
| 72 | if (reliefmap[x][y] < height.min) |
| 73 | height.min = reliefmap[x][y]; |
| 74 | else if (reliefmap[x][y] > height.max) |
| 75 | height.max = reliefmap[x][y]; |
| 76 | } |
| 77 | } |
| 78 | return height; |
| 79 | } |
| 80 | |
| 81 | // Rescale a heightmap (Waterlevel is not taken into consideration!) |
| 82 | function getRescaledReliefmap(reliefmap, minHeight, maxHeight) |
| 83 | { |
| 84 | var newReliefmap = deepcopy(reliefmap); |
| 85 | minHeight = (minHeight || MIN_HEIGHT); |
| 86 | maxHeight = (maxHeight || MAX_HEIGHT); |
| 87 | if (minHeight < MIN_HEIGHT) |
| 88 | warn("getRescaledReliefmap: Argument minHeight is smaler then the supported minimum height of " + MIN_HEIGHT + " (const MIN_HEIGHT): " + minHeight) |
| 89 | if (maxHeight > MAX_HEIGHT) |
| 90 | warn("getRescaledReliefmap: Argument maxHeight is smaler then the supported maximum height of " + MAX_HEIGHT + " (const MAX_HEIGHT): " + maxHeight) |
| 91 | var oldHeightRange = getMinAndMaxHeight(reliefmap); |
| 92 | for (var x = 0; x <= mapSize; x++) |
| 93 | { |
| 94 | for (var y = 0; y <= mapSize; y++) |
| 95 | { |
| 96 | newReliefmap[x][y] = minHeight + (reliefmap[x][y] - oldHeightRange.min) / (oldHeightRange.max - oldHeightRange.min) * (maxHeight - minHeight); |
| 97 | } |
| 98 | } |
| 99 | return newReliefmap |
| 100 | } |
| 101 | |
| 102 | // Applying decay errosion (terrain independent) |
| 103 | function getHeightErrosionedReliefmap(reliefmap, strength) |
| 104 | { |
| 105 | var newReliefmap = deepcopy(reliefmap); |
| 106 | strength = (strength || 1.0); // Values much higher then 1 (1.32+ for an 8 tile map, 1.45+ for a 12 tile map, 1.62+ @ 20 tile map, 0.99 @ 4 tiles) will result in a resonance disaster/self interference |
| 107 | var map = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]; // Default |
| 108 | for (var x = 0; x <= mapSize; x++) |
| 109 | { |
| 110 | for (var y = 0; y <= mapSize; y++) |
| 111 | { |
| 112 | var div = 0; |
| 113 | for (var i = 0; i < map.length; i++) |
| 114 | newReliefmap[x][y] += strength / map.length * (reliefmap[(x + map[i][0] + mapSize + 1) % (mapSize + 1)][(y + map[i][1] + mapSize + 1) % (mapSize + 1)] - reliefmap[x][y]); // Not entirely sure if scaling with map.length is perfect but tested values seam to indicate it is |
| 115 | } |
| 116 | } |
| 117 | return newReliefmap; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | ////////// |
| 122 | // Apply hightmap munipulation |
| 123 | ////////// |
| 124 | |
| 125 | // Set target min and max height depending on map size to make average stepness the same on all map sizes |
| 126 | var heightRange = {"min": MIN_HEIGHT * mapSize / 512, "max": MAX_HEIGHT * mapSize / 512}; |
| 127 | |
| 128 | // Set average water coverage |
| 129 | var averageWaterCoverage = 1/3; // NOTE: Since errosion is not predictable actual water coverage might differ much with the same value |
| 130 | if (mapSize < 200) // Sink the waterlevel on tiny maps to ensure enough space |
| 131 | averageWaterCoverage = 2/3 * averageWaterCoverage; |
| 132 | var waterHeight = -MIN_HEIGHT + heightRange.min + averageWaterCoverage * (heightRange.max - heightRange.min); |
| 133 | var waterHeightAdjusted = waterHeight + MIN_HEIGHT; |
| 134 | setWaterHeight(waterHeight); |
| 135 | |
| 136 | // Time check 1 |
| 137 | // timeArray.push(new Date().getTime()); |
| 138 | RMS.SetProgress(2); |
| 139 | |
| 140 | |
| 141 | // Generate reliefmap |
| 142 | var myReliefmap = getRandomReliefmap(heightRange.min, heightRange.max); |
| 143 | // Cycles depend on mapsize (more cycles -> bigger structures) |
| 144 | for (var i = 0; i < 50 + mapSize/4; i++) |
| 145 | myReliefmap = getHeightErrosionedReliefmap(myReliefmap, 1); |
| 146 | myReliefmap = getRescaledReliefmap(myReliefmap, heightRange.min, heightRange.max); |
| 147 | setReliefmap(myReliefmap); |
| 148 | |
| 149 | // Time check 2 |
| 150 | // timeArray.push(new Date().getTime()); |
| 151 | RMS.SetProgress(66); |
| 152 | |
| 153 | |
| 154 | ////////// |
| 155 | // Apply terrain texture by height |
| 156 | ////////// |
| 157 | |
| 158 | var textueByHeight = []; |
| 159 | |
| 160 | // Deep water |
| 161 | textueByHeight.push({"upperHeightLimit": heightRange.min + 1/3 * (waterHeightAdjusted - heightRange.min), "terrain": "temp_sea_rocks"}); |
| 162 | // Medium deep water (with fish) |
| 163 | var terreins = ["temp_sea_weed"]; |
| 164 | terreins = terreins.concat(terreins, terreins, terreins, terreins); |
| 165 | terreins = terreins.concat(terreins, terreins, terreins, terreins); |
| 166 | terreins.push("temp_sea_weed|gaia/fauna_fish"); |
| 167 | textueByHeight.push({"upperHeightLimit": heightRange.min + 2/3 * (waterHeightAdjusted - heightRange.min), "terrain": terreins}); |
| 168 | // Flat Water |
| 169 | textueByHeight.push({"upperHeightLimit": heightRange.min + 3/3 * (waterHeightAdjusted - heightRange.min), "terrain": "temp_mud_a"}); |
| 170 | // Water surroundings/bog (with stone/metal some rabits and bushes) |
| 171 | var terreins = ["temp_plants_bog", "temp_plants_bog_aut", "temp_dirt_gravel_plants", "temp_grass_d"]; |
| 172 | terreins = terreins.concat(terreins, terreins, terreins, terreins, terreins); |
| 173 | terreins = ["temp_plants_bog|gaia/flora_bush_temperate"].concat(terreins, terreins); |
| 174 | terreins = ["temp_dirt_gravel_plants|gaia/geology_metal_temperate", "temp_dirt_gravel_plants|gaia/geology_stone_temperate", "temp_plants_bog|gaia/fauna_rabbit"].concat(terreins, terreins); |
| 175 | terreins = ["temp_plants_bog_aut|gaia/flora_tree_dead"].concat(terreins, terreins); |
| 176 | textueByHeight.push({"upperHeightLimit": waterHeightAdjusted + 1/6 * (heightRange.max - waterHeightAdjusted), "terrain": terreins}); |
| 177 | // Juicy grass near bog |
| 178 | textueByHeight.push({"upperHeightLimit": waterHeightAdjusted + 2/6 * (heightRange.max - waterHeightAdjusted), |
| 179 | "terrain": ["temp_grass", "temp_grass_d", "temp_grass_long_b", "temp_grass_plants"]}); |
| 180 | // Medium level grass |
| 181 | // var testActor = "actor|geology/decal_stone_medit_a.xml"; |
| 182 | textueByHeight.push({"upperHeightLimit": waterHeightAdjusted + 3/6 * (heightRange.max - waterHeightAdjusted), |
| 183 | "terrain": ["temp_grass", "temp_grass_b", "temp_grass_c", "temp_grass_mossy"]}); |
| 184 | // Long grass near forest border |
| 185 | textueByHeight.push({"upperHeightLimit": waterHeightAdjusted + 4/6 * (heightRange.max - waterHeightAdjusted), |
| 186 | "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"]}); |
| 187 | // Forest border (With wood/food plants/deer/rabits) |
| 188 | var terreins = ["temp_grass_plants|gaia/flora_tree_euro_beech", "temp_grass_mossy|gaia/flora_tree_poplar", "temp_grass_mossy|gaia/flora_tree_poplar_lombardy", |
| 189 | "temp_grass_long|gaia/flora_bush_temperate", "temp_mud_plants|gaia/flora_bush_temperate", "temp_mud_plants|gaia/flora_bush_badlands", |
| 190 | "temp_grass_long|gaia/flora_tree_apple", "temp_grass_clovers|gaia/flora_bush_berry", "temp_grass_clovers_2|gaia/flora_bush_grapes", |
| 191 | "temp_grass_plants|gaia/fauna_deer", "temp_grass_long_b|gaia/fauna_rabbit"]; |
| 192 | var numTerreins = terreins.length; |
| 193 | for (var i = 0; i < numTerreins; i++) |
| 194 | terreins.push("temp_grass_plants"); |
| 195 | textueByHeight.push({"upperHeightLimit": waterHeightAdjusted + 5/6 * (heightRange.max - waterHeightAdjusted), "terrain": terreins}); |
| 196 | // Unpassable woods |
| 197 | textueByHeight.push({"upperHeightLimit": waterHeightAdjusted + 6/6 * (heightRange.max - waterHeightAdjusted), |
| 198 | "terrain": ["temp_grass_mossy|gaia/flora_tree_oak", "temp_forestfloor_pine|gaia/flora_tree_pine", |
| 199 | "temp_grass_mossy|gaia/flora_tree_oak", "temp_forestfloor_pine|gaia/flora_tree_pine", |
| 200 | "temp_mud_plants|gaia/flora_tree_dead", "temp_plants_bog|gaia/flora_tree_oak_large", |
| 201 | "temp_dirt_gravel_plants|gaia/flora_tree_aleppo_pine", "temp_forestfloor_autumn|gaia/flora_tree_carob"]}); |
| 202 | var minTerrainDistToBorder = 3; |
| 203 | |
| 204 | // Paint terrain by height and add props |
| 205 | var propDensity = 1; // 1 means as determined in the loop, less for large maps as set below |
| 206 | if (mapSize > 500) |
| 207 | propDensity = 1/4; |
| 208 | else if (mapSize > 400) |
| 209 | propDensity = 3/4; |
| 210 | for(var x = minTerrainDistToBorder; x < mapSize - minTerrainDistToBorder; x++) |
| 211 | { |
| 212 | for (var y = minTerrainDistToBorder; y < mapSize - minTerrainDistToBorder; y++) |
| 213 | { |
| 214 | var textureMinHeight = heightRange.min; |
| 215 | for (var i = 0; i < textueByHeight.length; i++) |
| 216 | { |
| 217 | if (getHeight(x, y) >= textureMinHeight && getHeight(x, y) <= textueByHeight[i].upperHeightLimit) |
| 218 | { |
| 219 | placeTerrain(x, y, textueByHeight[i].terrain); |
| 220 | // Add some props at... |
| 221 | if (i == 0) // ...deep water |
| 222 | { |
| 223 | if (randFloat() < 1/50 * propDensity) |
| 224 | placeObject(x, y, "actor|props/flora/pond_lillies_large.xml", 0, randFloat(0, 2*PI)); |
| 225 | else if (randFloat() < 1/20 * propDensity) |
| 226 | placeObject(x, y, "actor|props/flora/water_lillies.xml", 0, randFloat(0, 2*PI)); |
| 227 | } |
| 228 | if (i == 1) // ...medium water (with fish) |
| 229 | { |
| 230 | if (randFloat() < 1/100 * propDensity) |
| 231 | placeObject(x, y, "actor|props/flora/pond_lillies_large.xml", 0, randFloat(0, 2*PI)); |
| 232 | else if (randFloat() < 1/50 * propDensity) |
| 233 | placeObject(x, y, "actor|props/flora/water_lillies.xml", 0, randFloat(0, 2*PI)); |
| 234 | } |
| 235 | if (i == 2) // ...low water/mud |
| 236 | { |
| 237 | if (randFloat() < 1/100 * propDensity) |
| 238 | placeObject(x, y, "actor|props/flora/water_log.xml", 0, randFloat(0, 2*PI)); |
| 239 | else if (randFloat() < 1/50 * propDensity) |
| 240 | placeObject(x, y, "actor|props/flora/water_lillies.xml", 0, randFloat(0, 2*PI)); |
| 241 | else if (randFloat() < 1/20 * propDensity) |
| 242 | placeObject(x, y, "actor|geology/highland_c.xml", 0, randFloat(0, 2*PI)); |
| 243 | else if (randFloat() < 1/10 * propDensity) |
| 244 | placeObject(x, y, "actor|props/flora/reeds_pond_lush_b.xml", 0, randFloat(0, 2*PI)); |
| 245 | else if (randFloat() < 1/5 * propDensity) |
| 246 | placeObject(x, y, "actor|props/flora/reeds_pond_lush_a.xml", 0, randFloat(0, 2*PI)); |
| 247 | } |
| 248 | if (i == 3) // ...water suroundings/bog |
| 249 | { |
| 250 | if (randFloat() < 1/100 * propDensity) |
| 251 | placeObject(x, y, "actor|props/flora/water_log.xml", 0, randFloat(0, 2*PI)); |
| 252 | else if (randFloat() < 1/50 * propDensity) |
| 253 | placeObject(x, y, "actor|geology/highland_c.xml", 0, randFloat(0, 2*PI)); |
| 254 | else if (randFloat() < 1/20 * propDensity) |
| 255 | placeObject(x, y, "actor|props/flora/reeds_pond_lush_a.xml", 0, randFloat(0, 2*PI)); |
| 256 | } |
| 257 | if (i == 4) // ...low height grass |
| 258 | { |
| 259 | if (randFloat() < 1/400 * propDensity) |
| 260 | placeObject(x, y, "actor|props/flora/grass_field_flowering_tall.xml", 0, randFloat(0, 2*PI)); |
| 261 | else if (randFloat() < 1/200 * propDensity) |
| 262 | placeObject(x, y, "actor|geology/gray_rock1.xml", 0, randFloat(0, 2*PI)); |
| 263 | else if (randFloat() < 1/100 * propDensity) |
| 264 | placeObject(x, y, "actor|props/flora/bush_tempe_sm_lush.xml", 0, randFloat(0, 2*PI)); |
| 265 | else if (randFloat() < 1/50 * propDensity) |
| 266 | placeObject(x, y, "actor|props/flora/bush_tempe_b.xml", 0, randFloat(0, 2*PI)); |
| 267 | else if (randFloat() < 1/20 * propDensity) |
| 268 | placeObject(x, y, "actor|props/flora/grass_soft_small_tall.xml", 0, randFloat(0, 2*PI)); |
| 269 | } |
| 270 | if (i == 5) // ...medium height grass |
| 271 | { |
| 272 | if (randFloat() < 1/400 * propDensity) |
| 273 | placeObject(x, y, "actor|geology/decal_stone_medit_a.xml", 0, randFloat(0, 2*PI)); |
| 274 | else if (randFloat() < 1/200 * propDensity) |
| 275 | placeObject(x, y, "actor|props/flora/decals_flowers_daisies.xml", 0, randFloat(0, 2*PI)); |
| 276 | else if (randFloat() < 1/100 * propDensity) |
| 277 | placeObject(x, y, "actor|props/flora/bush_tempe_underbrush.xml", 0, randFloat(0, 2*PI)); |
| 278 | else if (randFloat() < 1/50 * propDensity) |
| 279 | placeObject(x, y, "actor|props/flora/grass_soft_small_tall.xml", 0, randFloat(0, 2*PI)); |
| 280 | else if (randFloat() < 1/20 * propDensity) |
| 281 | placeObject(x, y, "actor|props/flora/grass_temp_field.xml", 0, randFloat(0, 2*PI)); |
| 282 | } |
| 283 | if (i == 6) // ...high height grass |
| 284 | { |
| 285 | if (randFloat() < 1/200 * propDensity) |
| 286 | placeObject(x, y, "actor|geology/stone_granite_boulder.xml", 0, randFloat(0, 2*PI)); |
| 287 | else if (randFloat() < 1/100 * propDensity) |
| 288 | placeObject(x, y, "actor|props/flora/foliagebush.xml", 0, randFloat(0, 2*PI)); |
| 289 | else if (randFloat() < 1/50 * propDensity) |
| 290 | placeObject(x, y, "actor|props/flora/bush_tempe_underbrush.xml", 0, randFloat(0, 2*PI)); |
| 291 | else if (randFloat() < 1/20 * propDensity) |
| 292 | placeObject(x, y, "actor|props/flora/grass_soft_small_tall.xml", 0, randFloat(0, 2*PI)); |
| 293 | else if (randFloat() < 1/10 * propDensity) |
| 294 | placeObject(x, y, "actor|props/flora/ferns.xml", 0, randFloat(0, 2*PI)); |
| 295 | } |
| 296 | if (i == 7) // ...forest border (with wood/food plants/deer/rabits) |
| 297 | { |
| 298 | if (randFloat() < 1/200 * propDensity) |
| 299 | placeObject(x, y, "actor|geology/highland_c.xml", 0, randFloat(0, 2*PI)); |
| 300 | else if (randFloat() < 1/100 * propDensity) |
| 301 | placeObject(x, y, "actor|props/flora/bush_tempe_a.xml", 0, randFloat(0, 2*PI)); |
| 302 | else if (randFloat() < 1/50 * propDensity) |
| 303 | placeObject(x, y, "actor|props/flora/ferns.xml", 0, randFloat(0, 2*PI)); |
| 304 | else if (randFloat() < 1/20 * propDensity) |
| 305 | placeObject(x, y, "actor|props/flora/grass_soft_tuft_a.xml", 0, randFloat(0, 2*PI)); |
| 306 | } |
| 307 | if (i == 8) // ...woods |
| 308 | { |
| 309 | if (randFloat() < 1/100 * propDensity) |
| 310 | placeObject(x, y, "actor|geology/highland2_moss.xml", 0, randFloat(0, 2*PI)); |
| 311 | else if (randFloat() < 1/50 * propDensity) |
| 312 | placeObject(x, y, "actor|props/flora/grass_soft_tuft_a.xml", 0, randFloat(0, 2*PI)); |
| 313 | else if (randFloat() < 1/20 * propDensity) |
| 314 | placeObject(x, y, "actor|props/flora/ferns.xml", 0, randFloat(0, 2*PI)); |
| 315 | } |
| 316 | break; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | textureMinHeight = textueByHeight[i].upperHeightLimit; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // Time check 3 |
| 327 | // timeArray.push(new Date().getTime()); |
| 328 | RMS.SetProgress(88); |
| 329 | |
| 330 | |
| 331 | ////////// |
| 332 | // Find good start position tiles |
| 333 | ////////// |
| 334 | |
| 335 | var startPositions = []; |
| 336 | var possibleStartPositions = []; |
| 337 | var neededDistance = 7; |
| 338 | var distToBorder = 2 * neededDistance; // Has to be greater than neededDistance! Otherwise the check if low/high ground is near will fail... |
| 339 | var lowerHeightLimit = textueByHeight[3].upperHeightLimit; |
| 340 | var upperHeightLimit = textueByHeight[6].upperHeightLimit; |
| 341 | // Check for valid points by height |
| 342 | for (var x = distToBorder + minTerrainDistToBorder; x < mapSize - distToBorder - minTerrainDistToBorder; x++) |
| 343 | { |
| 344 | for (var y = distToBorder + minTerrainDistToBorder; y < mapSize - distToBorder - minTerrainDistToBorder; y++) |
| 345 | { |
| 346 | var actualHeight = getHeight(x, y); |
| 347 | if (actualHeight > lowerHeightLimit && actualHeight < upperHeightLimit) |
| 348 | { |
| 349 | // Check for points within a valid area by height (rectangular since faster) |
| 350 | var isPossible = true; |
| 351 | for (var offX = - neededDistance; offX <= neededDistance; offX++) |
| 352 | { |
| 353 | for (var offY = - neededDistance; offY <= neededDistance; offY++) |
| 354 | { |
| 355 | var testHeight = getHeight(x + offX, y + offY); |
| 356 | if (testHeight <= lowerHeightLimit || testHeight >= upperHeightLimit) |
| 357 | { |
| 358 | isPossible = false; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | if (isPossible) |
| 364 | { |
| 365 | possibleStartPositions.push([x, y]); |
| 366 | // placeTerrain(x, y, "blue"); // For debug reasons. Plz don't remove. |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Time check 4 |
| 373 | // timeArray.push(new Date().getTime()); |
| 374 | RMS.SetProgress(91); |
| 375 | |
| 376 | // Trying to reduce the number of possible start locations... |
| 377 | |
| 378 | // ...by beeing in a circle of mapSize / 2 distance to the center |
| 379 | var possibleStartPositionsTemp = []; |
| 380 | var maxDistToCenter = mapSize / 2; |
| 381 | for (var i = 0; i < possibleStartPositions.length; i++) |
| 382 | { |
| 383 | var deltaX = possibleStartPositions[i][0] - mapSize / 2; |
| 384 | var deltaY = possibleStartPositions[i][1] - mapSize / 2; |
| 385 | var distToCenter = Math.pow(Math.pow(deltaX, 2) + Math.pow(deltaY, 2), 1/2); |
| 386 | if (distToCenter < maxDistToCenter) |
| 387 | { |
| 388 | possibleStartPositionsTemp.push(possibleStartPositions[i]); |
| 389 | // placeTerrain(possibleStartPositions[i][0], possibleStartPositions[i][1], "purple"); // For debug reasons. Plz don't remove. |
| 390 | } |
| 391 | } |
| 392 | possibleStartPositions = deepcopy(possibleStartPositionsTemp); |
| 393 | |
| 394 | // ...by checking if low and high ground is near. Rectangular check since faster... |
| 395 | var possibleStartPositionsTemp = []; |
| 396 | var maxDistToResources = distToBorder; // Has to be <= distToBorder! |
| 397 | var minNumLowTiles = 10; |
| 398 | var minNumHighTiles = 10; |
| 399 | for (var i = 0; i < possibleStartPositions.length; i++) |
| 400 | { |
| 401 | var numLowTiles = 0; |
| 402 | var numHighTiles = 0; |
| 403 | for (var dx = - maxDistToResources; dx < maxDistToResources; dx++) |
| 404 | { |
| 405 | for (var dy = - maxDistToResources; dy < maxDistToResources; dy++) |
| 406 | { |
| 407 | var testHeight = getHeight(possibleStartPositions[i][0] + dx, possibleStartPositions[i][1] + dy); |
| 408 | if (testHeight < lowerHeightLimit) |
| 409 | numLowTiles++; |
| 410 | if (testHeight > upperHeightLimit) |
| 411 | numHighTiles++; |
| 412 | if (numLowTiles > minNumLowTiles && numHighTiles > minNumHighTiles) |
| 413 | break; |
| 414 | } |
| 415 | if (numLowTiles > minNumLowTiles && numHighTiles > minNumHighTiles) |
| 416 | break; |
| 417 | } |
| 418 | if (numLowTiles > minNumLowTiles && numHighTiles > minNumHighTiles) |
| 419 | { |
| 420 | possibleStartPositionsTemp.push(possibleStartPositions[i]); |
| 421 | // placeTerrain(possibleStartPositions[i][0], possibleStartPositions[i][1], "red"); // For debug reasons. Plz don't remove. |
| 422 | } |
| 423 | } |
| 424 | possibleStartPositions = deepcopy(possibleStartPositionsTemp); |
| 425 | |
| 426 | // Time check 5 |
| 427 | // timeArray.push(new Date().getTime()); |
| 428 | RMS.SetProgress(97); |
| 429 | |
| 430 | |
| 431 | //////// |
| 432 | // Find a good start position derivation |
| 433 | //////// |
| 434 | |
| 435 | // Get some random start location derivations. NOTE: Itterating over all possible derivations is just to much (valid points ** numPlayers) |
| 436 | var maxTries = 100000; // floor(800000 / (Math.pow(numPlayers, 2) / 2)); |
| 437 | var possibleDerivations = []; |
| 438 | for (var i = 0; i < maxTries; i++) |
| 439 | { |
| 440 | var vector = []; |
| 441 | for (var p = 0; p < numPlayers; p++) |
| 442 | vector.push(randInt(possibleStartPositions.length)); |
| 443 | possibleDerivations.push(vector); |
| 444 | } |
| 445 | |
| 446 | // Choose the start location derivation with the greatest minimum distance between players |
| 447 | var maxMinDist = 0; |
| 448 | for (var d = 0; d < possibleDerivations.length; d++) |
| 449 | { |
| 450 | var minDist = 2 * mapSize; |
| 451 | for (var p1 = 0; p1 < numPlayers - 1; p1++) |
| 452 | { |
| 453 | for (var p2 = p1 + 1; p2 < numPlayers; p2++) |
| 454 | { |
| 455 | if (p1 != p2) |
| 456 | { |
| 457 | var StartPositionP1 = possibleStartPositions[possibleDerivations[d][p1]]; |
| 458 | var StartPositionP2 = possibleStartPositions[possibleDerivations[d][p2]]; |
| 459 | var actualDist = Math.pow(Math.pow(StartPositionP1[0] - StartPositionP2[0], 2) + Math.pow(StartPositionP1[1] - StartPositionP2[1], 2), 1/2); |
| 460 | if (actualDist < minDist) |
| 461 | minDist = actualDist; |
| 462 | if (minDist < maxMinDist) |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | if (minDist < maxMinDist) |
| 467 | break; |
| 468 | } |
| 469 | if (minDist > maxMinDist) |
| 470 | { |
| 471 | maxMinDist = minDist; |
| 472 | var bestDerivation = possibleDerivations[d]; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // Time check 6 |
| 477 | // timeArray.push(new Date().getTime()); |
| 478 | RMS.SetProgress(99); |
| 479 | |
| 480 | // Place players |
| 481 | for (var p = 0; p < numPlayers; p++) |
| 482 | { |
| 483 | var actualX = possibleStartPositions[bestDerivation[p]][0]; |
| 484 | var actualY = possibleStartPositions[bestDerivation[p]][1]; |
| 485 | placeCivDefaultEntities(actualX, actualY, p + 1, BUILDING_ANGlE, {"iberWall" : false}); |
| 486 | var uDist = 8; |
| 487 | var uSpace = 1; |
| 488 | for (var j = 1; j <= 4; ++j) |
| 489 | { |
| 490 | var uAngle = BUILDING_ANGlE - PI * (2-j) / 2; |
| 491 | var count = 4; |
| 492 | for (var numberofentities = 0; numberofentities < count; numberofentities++) |
| 493 | { |
| 494 | var ux = actualX + uDist * cos(uAngle) + numberofentities * uSpace * cos(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * cos(uAngle + PI/2)); |
| 495 | var uz = actualY + uDist * sin(uAngle) + numberofentities * uSpace * sin(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * sin(uAngle + PI/2)); |
| 496 | if (j % 2 == 0) |
| 497 | placeObject(ux, uz, "gaia/flora_bush_berry", 0, randFloat(0, 2*PI)); |
| 498 | else |
| 499 | placeObject(ux, uz, "gaia/flora_tree_cypress", 0, randFloat(0, 2*PI)); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
| 505 | // Export map data |
| 506 | ExportMap(); |
| 507 | |
| 508 | |
| 509 | // // Time check 7 |
| 510 | // timeArray.push(new Date().getTime()); |
| 511 | // RMS.SetProgress(100); |
| 512 | |
| 513 | // var generationTime = timeArray[timeArray.length - 1] - timeArray[0]; |
| 514 | // log("Total generation time (ms): " + generationTime); |
| 515 | // for (var i = 0; i < timeArray.length; i++) |
| 516 | // { |
| 517 | // var timeSinceStart = timeArray[i] - timeArray[0]; |
| 518 | // var progressPercentage = 100 * timeSinceStart / generationTime; |
| 519 | // log("Time check " + i + ": Progress (%): " + progressPercentage); |
| 520 | // } |