|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +function drawReliefIcons() { |
| 4 | + TIME && console.time("drawRelief"); |
| 5 | + terrain.selectAll("*").remove(); |
| 6 | + |
| 7 | + const cells = pack.cells; |
| 8 | + const density = terrain.attr("density") || 0.4; |
| 9 | + const size = 2 * (terrain.attr("size") || 1); |
| 10 | + const mod = 0.2 * size; // size modifier |
| 11 | + const relief = []; |
| 12 | + |
| 13 | + for (const i of cells.i) { |
| 14 | + const height = cells.h[i]; |
| 15 | + if (height < 20) continue; // no icons on water |
| 16 | + if (cells.r[i]) continue; // no icons on rivers |
| 17 | + const biome = cells.biome[i]; |
| 18 | + if (height < 50 && biomesData.iconsDensity[biome] === 0) continue; // no icons for this biome |
| 19 | + |
| 20 | + const polygon = getPackPolygon(i); |
| 21 | + const [minX, maxX] = d3.extent(polygon, p => p[0]); |
| 22 | + const [minY, maxY] = d3.extent(polygon, p => p[1]); |
| 23 | + |
| 24 | + if (height < 50) placeBiomeIcons(i, biome); |
| 25 | + else placeReliefIcons(i); |
| 26 | + |
| 27 | + function placeBiomeIcons() { |
| 28 | + const iconsDensity = biomesData.iconsDensity[biome] / 100; |
| 29 | + const radius = 2 / iconsDensity / density; |
| 30 | + if (Math.random() > iconsDensity * 10) return; |
| 31 | + |
| 32 | + for (const [cx, cy] of poissonDiscSampler(minX, minY, maxX, maxY, radius)) { |
| 33 | + if (!d3.polygonContains(polygon, [cx, cy])) continue; |
| 34 | + let h = (4 + Math.random()) * size; |
| 35 | + const icon = getBiomeIcon(i, biomesData.icons[biome]); |
| 36 | + if (icon === "#relief-grass-1") h *= 1.2; |
| 37 | + relief.push({i: icon, x: rn(cx - h, 2), y: rn(cy - h, 2), s: rn(h * 2, 2)}); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + function placeReliefIcons(i) { |
| 42 | + const radius = 2 / density; |
| 43 | + const [icon, h] = getReliefIcon(i, height); |
| 44 | + |
| 45 | + for (const [cx, cy] of poissonDiscSampler(minX, minY, maxX, maxY, radius)) { |
| 46 | + if (!d3.polygonContains(polygon, [cx, cy])) continue; |
| 47 | + relief.push({i: icon, x: rn(cx - h, 2), y: rn(cy - h, 2), s: rn(h * 2, 2)}); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function getReliefIcon(i, h) { |
| 52 | + const temp = grid.cells.temp[pack.cells.g[i]]; |
| 53 | + const type = h > 70 && temp < 0 ? "mountSnow" : h > 70 ? "mount" : "hill"; |
| 54 | + const size = h > 70 ? (h - 45) * mod : minmax((h - 40) * mod, 3, 6); |
| 55 | + return [getIcon(type), size]; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // sort relief icons by y+size |
| 60 | + relief.sort((a, b) => a.y + a.s - (b.y + b.s)); |
| 61 | + |
| 62 | + const reliefHTML = new Array(relief.length); |
| 63 | + for (const r of relief) { |
| 64 | + reliefHTML.push(`<use href="${r.i}" x="${r.x}" y="${r.y}" width="${r.s}" height="${r.s}"/>`); |
| 65 | + } |
| 66 | + terrain.html(reliefHTML.join("")); |
| 67 | + |
| 68 | + TIME && console.timeEnd("drawRelief"); |
| 69 | + |
| 70 | + function getBiomeIcon(i, b) { |
| 71 | + let type = b[Math.floor(Math.random() * b.length)]; |
| 72 | + const temp = grid.cells.temp[pack.cells.g[i]]; |
| 73 | + if (type === "conifer" && temp < 0) type = "coniferSnow"; |
| 74 | + return getIcon(type); |
| 75 | + } |
| 76 | + |
| 77 | + function getVariant(type) { |
| 78 | + switch (type) { |
| 79 | + case "mount": |
| 80 | + return rand(2, 7); |
| 81 | + case "mountSnow": |
| 82 | + return rand(1, 6); |
| 83 | + case "hill": |
| 84 | + return rand(2, 5); |
| 85 | + case "conifer": |
| 86 | + return 2; |
| 87 | + case "coniferSnow": |
| 88 | + return 1; |
| 89 | + case "swamp": |
| 90 | + return rand(2, 3); |
| 91 | + case "cactus": |
| 92 | + return rand(1, 3); |
| 93 | + case "deadTree": |
| 94 | + return rand(1, 2); |
| 95 | + default: |
| 96 | + return 2; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + function getOldIcon(type) { |
| 101 | + switch (type) { |
| 102 | + case "mountSnow": |
| 103 | + return "mount"; |
| 104 | + case "vulcan": |
| 105 | + return "mount"; |
| 106 | + case "coniferSnow": |
| 107 | + return "conifer"; |
| 108 | + case "cactus": |
| 109 | + return "dune"; |
| 110 | + case "deadTree": |
| 111 | + return "dune"; |
| 112 | + default: |
| 113 | + return type; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + function getIcon(type) { |
| 118 | + const set = terrain.attr("set") || "simple"; |
| 119 | + if (set === "simple") return "#relief-" + getOldIcon(type) + "-1"; |
| 120 | + if (set === "colored") return "#relief-" + type + "-" + getVariant(type); |
| 121 | + if (set === "gray") return "#relief-" + type + "-" + getVariant(type) + "-bw"; |
| 122 | + return "#relief-" + getOldIcon(type) + "-1"; // simple |
| 123 | + } |
| 124 | +} |
0 commit comments