wobblyhead hg > minor_monsters
annotate tools/simulate.js @ 31:d1ae50439686
Added tag 0.9.2 for changeset a51e44094df2
| author | ark |
|---|---|
| date | 2026-10-20 17:58 +0200 |
| parents | 75deda08db90 |
| children | a464bd43b76c |
| rev | line source |
|---|---|
| 23 | 1 #!/usr/bin/env node |
| 2 // headless simulation of the game with scripted players (doc/balance.md, | |
| 3 // balancing). runs the game logic from www/ after a build: data/config.js, | |
| 4 // data/maps.js and the js/ files of the simulation, with MM.audio replaced | |
| 5 // by a stub. writes temp/preview/simulate.txt, the results as json in | |
| 6 // temp/preview/simulate.json (read by tools/balance.py), and prints a | |
| 7 // summary. | |
| 8 // | |
| 9 // usage: node tools/simulate.js [options] | |
| 10 // | |
| 11 // --maps 0,1 maps to play (default all) | |
| 12 // --players a,b players (default naive,greedy); also naive_t0, naive_mix | |
| 13 // --seeds n seeds per map and player, 1 to n (default 5) | |
| 14 // --need all measure the coins needed for every wave, not only the | |
| 15 // last one (slower) | |
| 16 // --waves file json {"0": [{"monsters": {...}}, ...], ...}: wave lists | |
| 17 // that replace the config's for the maps named | |
| 18 // --config file a config.js to use instead of www/data/config.js | |
| 19 // --json print the results as json instead of the summary, and | |
| 20 // write no files | |
| 21 // | |
| 22 // players (all build at the first moment they can afford the next | |
| 23 // purchase, through the game's own shop, preview and upgrade functions): | |
| 24 // | |
| 25 // naive t1 only, level 1, on cells of median t1 coverage. never | |
| 26 // starts a wave early | |
| 27 // greedy t1, cells picked by coverage of route not yet covered, and | |
| 28 // upgrades when that adds more damage per second per coin | |
| 29 // than a new tower (the rules of tools/balance.py). starts | |
| 30 // every wave early for the bonus | |
| 31 // naive_t0 as naive, t0 only, on cells of median t0 coverage | |
| 32 // naive_mix as naive, t1 and t0 in turn, each on cells of median | |
| 33 // coverage for its own range | |
| 34 // | |
| 35 // coverage of a cell is the route length within range, the mean over the | |
| 36 // map's routes, as in tools/balance.py. the coins a wave needs are the cost of the smallest | |
| 37 // number of the player's purchases, in its order, that hold the wave | |
| 38 // without losing a heart. it is measured by playing the wave on its own | |
| 39 // from an empty map, with the same seed. | |
| 40 | |
| 41 "use strict"; | |
| 42 | |
| 43 var fs = require("fs"); | |
| 44 var path = require("path"); | |
| 45 var vm = require("vm"); | |
| 46 | |
| 47 var ROOT = path.resolve(__dirname, ".."); | |
| 48 var WWW = path.join(ROOT, "www"); | |
| 49 var OUT = path.join(ROOT, "temp", "preview", "simulate.txt"); | |
| 50 var OUT_JSON = path.join(ROOT, "temp", "preview", "simulate.json"); | |
| 51 var DT = 1 / 60; | |
| 52 // distance between route samples for coverage, as in tools/balance.py | |
| 53 var SAMPLE_STEP = 4; | |
| 54 // naive cells: coverage within this share of the median counts as median | |
| 55 var MEDIAN_BAND = 0.05; | |
| 56 // longest purchase lists | |
| 57 var MAX_TOWERS = 80; | |
| 58 var MAX_SPENT = 4000; | |
| 59 // a run stops after this much game time | |
| 60 var MAX_TIME = 4000; | |
| 61 // hearts the naive player should have left (decided) | |
| 62 var HEARTS_TARGET = 5; | |
| 63 | |
| 64 // options | |
| 65 | |
| 66 function options(argv) { | |
| 67 var o = { maps: null, players: ["naive", "greedy"], seeds: 5, needAll: false, | |
| 68 waves: null, config: path.join(WWW, "data", "config.js"), json: false }; | |
| 69 for (var i = 0; i < argv.length; i++) { | |
| 70 var a = argv[i]; | |
| 71 if (a === "--maps") o.maps = argv[++i].split(",").map(Number); | |
| 72 else if (a === "--players") o.players = argv[++i].split(","); | |
| 73 else if (a === "--seeds") o.seeds = Number(argv[++i]); | |
| 74 else if (a === "--need") o.needAll = argv[++i] === "all"; | |
| 75 else if (a === "--waves") o.waves = JSON.parse(fs.readFileSync(argv[++i], "utf8")); | |
| 76 else if (a === "--config") o.config = path.resolve(argv[++i]); | |
| 77 else if (a === "--json") o.json = true; | |
| 78 else { | |
| 79 console.error("unknown option " + a + "; see the comment at the top of tools/simulate.js"); | |
| 80 process.exit(2); | |
| 81 } | |
| 82 } | |
| 83 return o; | |
| 84 } | |
| 85 | |
| 86 // load the game logic into this context, as the browser would | |
| 87 function load(configFile, waves) { | |
| 88 globalThis.window = globalThis; | |
| 89 var files = [configFile, path.join(WWW, "data", "maps.js")]; | |
| 90 ["util", "assets", "map", "monster", "tower", "projectile", "wave", "game"].forEach(function (f) { | |
| 91 files.push(path.join(WWW, "js", f + ".js")); | |
| 92 }); | |
| 93 files.forEach(function (f, i) { | |
| 94 vm.runInThisContext(fs.readFileSync(f, "utf8"), { filename: f }); | |
| 95 if (i === files.length - 1) countMelee(); | |
| 96 if (i === 1) { | |
| 97 if (waves) { | |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
23
diff
changeset
|
98 Object.keys(waves).forEach(function (m) { MM.config.maps[m].waves = waves[m]; }); |
| 23 | 99 } |
| 100 MM.audio = { play: function () {} }; | |
| 101 } | |
| 102 }); | |
| 103 } | |
| 104 | |
| 105 // melee attacks and the monsters they hit, by tower type, counted from the | |
| 106 // game's own tower update and damage functions | |
| 107 var melee = {}; | |
| 108 | |
| 109 function countMelee() { | |
| 110 var update = MM.tower.update, damage = MM.Game.prototype.damage, current = null; | |
| 111 MM.tower.update = function (t, game, dt) { | |
| 112 var before = t.cooldown; | |
| 113 current = t.def.projectile_speed === 0 ? t : null; | |
| 114 update(t, game, dt); | |
| 115 if (current && t.cooldown > before) { | |
| 116 var c = melee[t.type] || (melee[t.type] = { attacks: 0, hits: 0 }); | |
| 117 c.attacks++; | |
| 118 c.hits += current.hitsNow || 0; | |
| 119 } | |
| 120 if (current) current.hitsNow = 0; | |
| 121 current = null; | |
| 122 }; | |
| 123 MM.Game.prototype.damage = function (m, amount) { | |
| 124 if (current && m.alive) current.hitsNow = (current.hitsNow || 0) + 1; | |
| 125 return damage.call(this, m, amount); | |
| 126 }; | |
| 127 } | |
| 128 | |
| 129 // coverage | |
| 130 | |
| 131 // route samples about SAMPLE_STEP apart, in the middle of equal pieces: | |
| 132 // {xs, ys, weight}, weight being the length of a piece | |
| 133 function resample(r) { | |
| 134 var n = r.xs.length, cum = r.at, len = r.length; | |
| 135 var k = Math.max(1, Math.ceil(len / SAMPLE_STEP)); | |
| 136 var xs = [], ys = [], seg = 1; | |
| 137 for (var i = 0; i < k; i++) { | |
| 138 var s = (i + 0.5) * len / k; | |
| 139 while (seg < n - 1 && cum[seg] < s) seg++; | |
| 140 var t = (s - cum[seg - 1]) / (cum[seg] - cum[seg - 1]); | |
| 141 xs.push(r.xs[seg - 1] + (r.xs[seg] - r.xs[seg - 1]) * t); | |
| 142 ys.push(r.ys[seg - 1] + (r.ys[seg] - r.ys[seg - 1]) * t); | |
| 143 } | |
| 144 return { xs: xs, ys: ys, weight: len / k }; | |
| 145 } | |
| 146 | |
| 147 // legal cells and route samples of a map | |
| 148 function MapData(index) { | |
| 149 var gm = new MM.GameMap(index), d = gm.data; | |
| 150 this.index = index; | |
| 151 this.cells = []; | |
| 152 for (var i = 0; i < d.grid.length; i++) { | |
| 153 if (d.grid.charCodeAt(i) === 49) { | |
| 154 this.cells.push([(i % d.cols) * d.cell + d.cell / 2, Math.floor(i / d.cols) * d.cell + d.cell / 2]); | |
| 155 } | |
| 156 } | |
| 157 this.routes = gm.routes.map(resample); | |
| 158 this.cache = {}; | |
| 159 } | |
| 160 | |
| 161 // per route, per cell: indices of the samples within range rng | |
| 162 MapData.prototype.within = function (rng) { | |
| 163 if (this.cache[rng]) return this.cache[rng]; | |
| 164 var r2 = rng * rng, cells = this.cells; | |
| 165 var out = this.routes.map(function (r) { | |
| 166 return cells.map(function (c) { | |
| 167 var hit = []; | |
| 168 for (var i = 0; i < r.xs.length; i++) { | |
| 169 var dx = r.xs[i] - c[0], dy = r.ys[i] - c[1]; | |
| 170 if (dx * dx + dy * dy <= r2) hit.push(i); | |
| 171 } | |
| 172 return hit; | |
| 173 }); | |
| 174 }); | |
| 175 this.cache[rng] = out; | |
| 176 return out; | |
| 177 }; | |
| 178 | |
| 179 // per cell: route length within range, the mean over routes | |
| 180 MapData.prototype.coverage = function (rng) { | |
| 181 var w = this.within(rng), routes = this.routes; | |
| 182 return this.cells.map(function (c, j) { | |
| 183 var sum = 0; | |
| 184 for (var r = 0; r < routes.length; r++) sum += w[r][j].length * routes[r].weight; | |
| 185 return sum / routes.length; | |
| 186 }); | |
| 187 }; | |
| 188 | |
| 189 function median(v) { | |
| 190 var s = v.slice().sort(function (a, b) { return a - b; }), n = s.length; | |
| 191 return n % 2 ? s[(n - 1) / 2] : (s[n / 2 - 1] + s[n / 2]) / 2; | |
| 192 } | |
| 193 | |
| 194 function farEnough(cells, picked, j, minDist) { | |
| 195 for (var i = 0; i < picked.length; i++) { | |
| 196 var a = cells[picked[i]], b = cells[j]; | |
| 197 if (Math.hypot(a[0] - b[0], a[1] - b[1]) < minDist) return false; | |
| 198 } | |
| 199 return true; | |
| 200 } | |
| 201 | |
| 202 // cells for the naive players: the cells within MEDIAN_BAND of the median | |
| 203 // coverage in random order, then the others by distance from the median. | |
| 204 // towers keep the minimum distance from each other. returns cell indices. | |
| 205 function medianCells(md, rng, random, count, picked) { | |
| 206 var cov = md.coverage(rng), med = median(cov); | |
| 207 var idx = cov.map(function (_, j) { return j; }); | |
| 208 var band = idx.filter(function (j) { return Math.abs(cov[j] - med) <= MEDIAN_BAND * med; }); | |
| 209 for (var i = band.length - 1; i > 0; i--) { | |
| 210 var k = Math.floor(random() * (i + 1)), t = band[i]; | |
| 211 band[i] = band[k]; | |
| 212 band[k] = t; | |
| 213 } | |
| 214 var rest = idx.filter(function (j) { return Math.abs(cov[j] - med) > MEDIAN_BAND * med; }); | |
| 215 rest.sort(function (a, b) { return Math.abs(cov[a] - med) - Math.abs(cov[b] - med); }); | |
| 216 var order = band.concat(rest), out = []; | |
| 217 var minDist = MM.config.placement.min_tower_distance; | |
| 218 for (i = 0; i < order.length && out.length < count; i++) { | |
| 219 if (farEnough(md.cells, picked, order[i], minDist)) { | |
| 220 out.push(order[i]); | |
| 221 picked.push(order[i]); | |
| 222 } | |
| 223 } | |
| 224 return out; | |
| 225 } | |
| 226 | |
| 227 // cells picked one after the other by the route length in range that no | |
| 228 // earlier pick covers, mean over routes, keeping the minimum distance. once | |
| 229 // all route is covered, the cell with the most route in range is next. | |
| 230 function greedyCells(md, rng, count) { | |
| 231 var w = md.within(rng), routes = md.routes, cells = md.cells; | |
| 232 var full = md.coverage(rng); | |
| 233 var covered = routes.map(function (r) { return new Uint8Array(r.xs.length); }); | |
| 234 var free = cells.map(function () { return true; }); | |
| 235 var minDist = MM.config.placement.min_tower_distance; | |
| 236 var order = []; | |
| 237 while (order.length < count) { | |
| 238 var best = -1, bestGain = 0, bestFull = -1, bestFullV = -1; | |
| 239 for (var j = 0; j < cells.length; j++) { | |
| 240 if (!free[j]) continue; | |
| 241 var gain = 0; | |
| 242 for (var r = 0; r < routes.length; r++) { | |
| 243 var hit = w[r][j], cv = covered[r], n = 0; | |
| 244 for (var i = 0; i < hit.length; i++) if (!cv[hit[i]]) n++; | |
| 245 gain += n * routes[r].weight; | |
| 246 } | |
| 247 gain /= routes.length; | |
| 248 if (gain > bestGain) { bestGain = gain; best = j; } | |
| 249 if (full[j] > bestFullV) { bestFullV = full[j]; bestFull = j; } | |
| 250 } | |
| 251 if (best < 0) best = bestFull; | |
| 252 if (best < 0) break; | |
| 253 order.push(best); | |
| 254 for (r = 0; r < routes.length; r++) { | |
| 255 w[r][best].forEach(function (i) { covered[r][i] = 1; }); | |
| 256 } | |
| 257 for (j = 0; j < cells.length; j++) { | |
| 258 if (free[j] && Math.hypot(cells[j][0] - cells[best][0], cells[j][1] - cells[best][1]) < minDist) | |
| 259 free[j] = false; | |
| 260 } | |
| 261 } | |
| 262 return order; | |
| 263 } | |
| 264 | |
| 265 // players: a purchase list, each entry {type, x, y} for a new tower or | |
| 266 // {up: n} for an upgrade of the n-th tower built, with its cost | |
| 267 | |
| 268 function mapSpacing(index) { | |
| 269 var mc = MM.config.maps[index], mons = MM.config.monster; | |
| 270 var interval = mc.spawn_interval !== undefined ? mc.spawn_interval : MM.config.waves.spawn_interval; | |
| 271 var sum = 0, count = 0; | |
| 272 mc.waves.forEach(function (w) { | |
| 273 for (var k in w.monsters) { | |
| 274 if (mons[k].boss) continue; | |
| 275 sum += w.monsters[k] * mons[k].speed * interval; | |
| 276 count += w.monsters[k]; | |
| 277 } | |
| 278 }); | |
| 279 return sum / count; | |
| 280 } | |
| 281 | |
| 282 function naiveList(md, types, seed) { | |
| 283 var random = MM.util.rng(seed * 7919 + 17), picked = [], list = [], spent = 0; | |
| 284 var cfg = MM.config; | |
| 285 while (list.length < MAX_TOWERS && spent < MAX_SPENT) { | |
| 286 var type = types[list.length % types.length], def = cfg.tower[type]; | |
| 287 var c = medianCells(md, def.range[0], random, 1, picked); | |
| 288 if (!c.length) break; | |
| 289 var cell = md.cells[c[0]]; | |
| 290 list.push({ type: type, x: cell[0], y: cell[1], cost: def.cost[0] }); | |
| 291 spent += def.cost[0]; | |
| 292 } | |
| 293 return list; | |
| 294 } | |
| 295 | |
| 296 function greedyList(md, index) { | |
| 297 var def = MM.config.tower.t1, dmg = def.damage, cost = def.cost, at = def.attack_time; | |
| 298 var cells = greedyCells(md, def.range[0], MAX_TOWERS); | |
| 299 var cov = md.coverage(def.range[0]), spacing = mapSpacing(index); | |
| 300 function busy(j) { return 1 - Math.exp(-cov[j] / spacing); } | |
| 301 var levels = [], list = [], next = 0, spent = 0; | |
| 302 while (spent < MAX_SPENT) { | |
| 303 var best = 0, choice = null; | |
| 304 if (next < cells.length) { | |
| 305 best = dmg[0] / at * busy(cells[next]) / cost[0]; | |
| 306 choice = -1; | |
| 307 } | |
| 308 levels.forEach(function (t, n) { | |
| 309 if (t.level >= dmg.length) return; | |
| 310 var v = (dmg[t.level] - dmg[t.level - 1]) / at * busy(t.cell) / cost[t.level]; | |
| 311 if (v > best) { best = v; choice = n; } | |
| 312 }); | |
| 313 if (choice === null) break; | |
| 314 if (choice < 0) { | |
| 315 var cell = md.cells[cells[next]]; | |
| 316 levels.push({ cell: cells[next], level: 1 }); | |
| 317 list.push({ type: "t1", x: cell[0], y: cell[1], cost: cost[0] }); | |
| 318 next++; | |
| 319 spent += cost[0]; | |
| 320 } else { | |
| 321 var t = levels[choice]; | |
| 322 list.push({ up: choice, cost: cost[t.level] }); | |
| 323 spent += cost[t.level]; | |
| 324 t.level++; | |
| 325 } | |
| 326 } | |
| 327 return list; | |
| 328 } | |
| 329 | |
| 330 function makePlayer(name, md, index, seed) { | |
| 331 if (name === "naive") return { name: name, list: naiveList(md, ["t1"], seed), early: false }; | |
| 332 if (name === "naive_t0") return { name: name, list: naiveList(md, ["t0"], seed), early: false }; | |
| 333 if (name === "naive_mix") return { name: name, list: naiveList(md, ["t1", "t0"], seed), early: false }; | |
| 334 if (name === "greedy") return { name: name, list: greedyList(md, index), early: true }; | |
| 335 throw new Error("unknown player " + name); | |
| 336 } | |
| 337 | |
| 338 // make purchase p through the game's functions. built: the towers built so | |
| 339 // far, in order. returns false when it could not be made. | |
| 340 function buy(g, p, built) { | |
| 341 if (p.up !== undefined) { | |
| 342 var t = built[p.up]; | |
| 343 g.selected = null; | |
| 344 g.shop = null; | |
| 345 g.pressMap(t.x, t.y, 0); | |
| 346 if (g.selected !== t) return false; | |
| 347 var level = t.level; | |
| 348 g.upgrade(); | |
| 349 g.selected = null; | |
| 350 return t.level > level; | |
| 351 } | |
| 352 if (g.shop !== p.type) g.chooseShop(p.type); | |
| 353 if (!g.movePreview(p.x, p.y) || g.preview.x !== p.x || g.preview.y !== p.y) { | |
| 354 g.shop = null; | |
| 355 g.preview = null; | |
| 356 return false; | |
| 357 } | |
| 358 var n = g.towers.length; | |
| 359 if (!g.build()) return false; | |
| 360 built.push(g.towers[n]); | |
| 361 return true; | |
| 362 } | |
| 363 | |
| 364 // play | |
| 365 | |
| 366 function spentOn(g) { | |
| 367 return g.towers.reduce(function (s, t) { return s + t.paid; }, 0); | |
| 368 } | |
| 369 | |
| 370 // play map index with the player. returns per wave the coins on hand and | |
| 371 // the budget (coins on hand plus coins spent) at its start and the hearts | |
| 372 // lost in it, and the result. | |
| 373 function play(index, player, seed) { | |
| 374 melee = {}; | |
| 375 var g = new MM.Game(index, seed, 1); | |
| 376 var built = [], k = 0, recorded = 0, rows = [], stuck = false; | |
| 377 function act() { | |
| 378 while (k < player.list.length && g.canAfford(player.list[k].cost)) { | |
| 379 if (!buy(g, player.list[k], built)) { stuck = true; break; } | |
| 380 k++; | |
| 381 } | |
| 382 } | |
| 383 while (!g.result && g.time < MAX_TIME) { | |
| 384 act(); | |
| 385 if (player.early && g.waves.phase === "break") { | |
| 386 g.startEarly(); | |
| 387 act(); | |
| 388 } | |
| 389 if (g.waves.next !== recorded) { | |
| 390 if (rows.length) rows[rows.length - 1].lost -= g.hearts; | |
| 391 recorded = g.waves.next; | |
| 392 rows.push({ coins: g.coins, budget: g.coins + spentOn(g), towers: g.towers.length, | |
| 393 bought: k, lost: g.hearts }); | |
| 394 } | |
| 395 g.step(DT); | |
| 396 } | |
| 397 if (rows.length) rows[rows.length - 1].lost -= g.hearts; | |
| 398 return { rows: rows, hearts: g.hearts, won: g.result === "won", stuck: stuck, | |
| 399 coinsEnd: g.coins, purchases: k, melee: melee }; | |
| 400 } | |
| 401 | |
| 402 // hearts lost in wave w (from 0) played on its own from an empty map with | |
| 403 // the first k purchases of the player | |
| 404 function waveLoss(index, player, seed, w, k) { | |
| 405 var g = new MM.Game(index, seed, w + 1), built = []; | |
| 406 g.coins = 1e9; | |
| 407 for (var i = 0; i < k; i++) { | |
| 408 if (!buy(g, player.list[i], built)) throw new Error("purchase " + i + " failed in a wave test"); | |
| 409 } | |
| 410 var h = g.hearts; | |
| 411 g.startEarly(); | |
| 412 // the break after the wave starts while its monsters are still on the | |
| 413 // map; it is held so the next wave does not start | |
| 414 while (!g.result && (g.waves.phase === "spawning" || g.monsters.length) && g.time < MAX_TIME) { | |
| 415 if (g.waves.phase === "break") g.waves.left = Infinity; | |
| 416 g.step(DT); | |
| 417 } | |
| 418 return h - g.hearts; | |
| 419 } | |
| 420 | |
| 421 // coins of the smallest purchase prefix that holds wave w, null if none in | |
| 422 // the list does | |
| 423 function need(index, player, seed, w) { | |
| 424 var lo = 0, hi = player.list.length; | |
| 425 if (waveLoss(index, player, seed, w, hi) > 0) return null; | |
| 426 while (lo < hi) { | |
| 427 var mid = (lo + hi) >> 1; | |
| 428 if (waveLoss(index, player, seed, w, mid) === 0) hi = mid; | |
| 429 else lo = mid + 1; | |
| 430 } | |
| 431 var c = 0; | |
| 432 for (var i = 0; i < lo; i++) c += player.list[i].cost; | |
| 433 return c; | |
| 434 } | |
| 435 | |
| 436 // report | |
| 437 | |
| 438 function mean(v) { | |
| 439 return v.reduce(function (a, b) { return a + b; }, 0) / v.length; | |
| 440 } | |
| 441 | |
| 442 function pad(s, n) { | |
| 443 s = String(s); | |
| 444 return s.length >= n ? s : " ".repeat(n - s.length) + s; | |
| 445 } | |
| 446 | |
| 447 function fmt(v, d) { | |
| 448 return v === null || v === undefined || Number.isNaN(v) ? "-" : v.toFixed(d || 0); | |
| 449 } | |
| 450 | |
| 451 function summary(index, name, runs) { | |
| 452 var last = runs.map(function (r) { return r.rows[r.rows.length - 1]; }); | |
| 453 var needs = runs.map(function (r) { return r.need[r.need.length - 1]; }); | |
| 454 var surplus = runs.map(function (r, i) { | |
| 455 return needs[i] === null || !last[i] ? null : last[i].budget - needs[i]; | |
| 456 }); | |
| 457 var ok = surplus.filter(function (v) { return v !== null; }); | |
| 458 return { | |
| 459 map: index, player: name, seeds: runs.length, | |
| 460 won: runs.filter(function (r) { return r.won; }).length, | |
| 461 hearts: mean(runs.map(function (r) { return r.hearts; })), | |
| 462 heartsMin: Math.min.apply(null, runs.map(function (r) { return r.hearts; })), | |
| 463 heartsMax: Math.max.apply(null, runs.map(function (r) { return r.hearts; })), | |
| 464 coinsLast: mean(last.map(function (r) { return r ? r.coins : 0; })), | |
| 465 budgetLast: mean(last.map(function (r) { return r ? r.budget : 0; })), | |
| 466 needLast: ok.length ? mean(needs.filter(function (v) { return v !== null; })) : null, | |
| 467 surplus: ok.length === runs.length ? mean(ok) : null, | |
| 468 lost: runs[0].rows.map(function (_, w) { | |
| 469 return mean(runs.map(function (r) { return r.rows[w] ? r.rows[w].lost : 0; })); | |
| 470 }), | |
| 471 need: runs[0].need.map(function (_, w) { | |
| 472 var v = runs.map(function (r) { return r.need[w]; }); | |
| 473 return v.indexOf(null) >= 0 || v[0] === undefined ? null : mean(v); | |
| 474 }), | |
| 475 budget: runs[0].rows.map(function (_, w) { | |
| 476 return mean(runs.map(function (r) { return r.rows[w] ? r.rows[w].budget : 0; })); | |
| 477 }), | |
| 478 stuck: runs.some(function (r) { return r.stuck; }), | |
| 479 // monsters hit per t0 attack, over all runs | |
| 480 t0Hits: (function () { | |
| 481 var a = 0, h = 0; | |
| 482 runs.forEach(function (r) { if (r.melee.t0) { a += r.melee.t0.attacks; h += r.melee.t0.hits; } }); | |
| 483 return a ? h / a : null; | |
| 484 })() | |
| 485 }; | |
| 486 } | |
| 487 | |
| 488 function table(results, o) { | |
| 489 var lines = ["simulation, written by tools/simulate.js", "", | |
| 490 "seeds 1 to " + o.seeds + " per map and player. hearts: left at the end, mean (min to max).", | |
| 491 "coins: on hand at the start of the last wave. budget: coins on hand plus", | |
| 492 "coins spent then. need: coins of the purchases that hold the last wave", | |
| 493 "on its own. surplus: budget minus need, at the last wave. per wave: hearts", | |
| 494 "lost, mean over seeds; budget and need by wave where measured.", "", | |
| 495 "map player won hearts coins budget need surplus"]; | |
| 496 results.forEach(function (s) { | |
| 497 lines.push(pad("map" + s.map, 4) + " " + (s.player + " ").slice(0, 10) | |
| 498 + pad(s.won + "/" + s.seeds, 5) | |
| 499 + pad(fmt(s.hearts, 1), 7) + " (" + s.heartsMin + " to " + s.heartsMax + ")" | |
| 500 + pad("", 13 - (" (" + s.heartsMin + " to " + s.heartsMax + ")").length) | |
| 501 + pad(fmt(s.coinsLast, 1), 7) + pad(fmt(s.budgetLast), 8) + pad(fmt(s.needLast), 7) | |
| 502 + pad(fmt(s.surplus), 9) + (s.stuck ? " (a purchase failed)" : "") | |
| 503 + (s.t0Hits !== null ? " t0 hits per attack " + fmt(s.t0Hits, 2) : "")); | |
| 504 }); | |
| 505 results.forEach(function (s) { | |
| 506 lines.push("", "map" + s.map + " " + s.player + ", per wave:", | |
| 507 " lost " + s.lost.map(function (v) { return pad(fmt(v, 1), 6); }).join(""), | |
| 508 " budget " + s.budget.map(function (v) { return pad(fmt(v), 6); }).join(""), | |
| 509 " need " + s.need.map(function (v) { return pad(fmt(v), 6); }).join("")); | |
| 510 }); | |
| 511 return lines; | |
| 512 } | |
| 513 | |
| 514 function main() { | |
| 515 var o = options(process.argv.slice(2)); | |
| 516 load(o.config, o.waves); | |
| 517 var maps = o.maps || MM.maps.map(function (_, i) { return i; }); | |
| 518 var results = []; | |
| 519 maps.forEach(function (index) { | |
| 520 var md = new MapData(index); | |
| 521 var n = MM.config.maps[index].waves.length; | |
| 522 o.players.forEach(function (name) { | |
| 523 var runs = []; | |
| 524 for (var seed = 1; seed <= o.seeds; seed++) { | |
| 525 var player = makePlayer(name, md, index, seed); | |
| 526 var run = play(index, player, seed); | |
| 527 run.need = []; | |
| 528 for (var w = 0; w < n; w++) { | |
| 529 run.need.push(o.needAll || w === n - 1 ? need(index, player, seed, w) : undefined); | |
| 530 } | |
| 531 runs.push(run); | |
| 532 } | |
| 533 results.push(summary(index, name, runs)); | |
| 534 }); | |
| 535 }); | |
| 536 if (o.json) { | |
| 537 console.log(JSON.stringify(results)); | |
| 538 return; | |
| 539 } | |
| 540 var lines = table(results, o); | |
| 541 fs.mkdirSync(path.dirname(OUT), { recursive: true }); | |
| 542 fs.writeFileSync(OUT, lines.join("\n") + "\n"); | |
| 543 fs.writeFileSync(OUT_JSON, JSON.stringify(results, null, 1) + "\n"); | |
| 544 results.forEach(function (s) { | |
| 545 console.log("map" + s.map + " " + s.player + ": won " + s.won + "/" + s.seeds + ", hearts " | |
| 546 + fmt(s.hearts, 1) + ", coins at the last wave " + fmt(s.coinsLast, 1) | |
| 547 + ", surplus " + fmt(s.surplus) + (s.player === "naive" && s.hearts > HEARTS_TARGET + 1 | |
| 548 ? " (target " + HEARTS_TARGET + " hearts)" : "")); | |
| 549 }); | |
| 550 console.log("simulate: wrote temp/preview/simulate.txt, simulate.json"); | |
| 551 } | |
| 552 | |
| 553 if (require.main === module) main(); | |
| 554 | |
| 555 module.exports = { options: options, load: load, MapData: MapData, greedyCells: greedyCells, | |
| 556 makePlayer: makePlayer, play: play, need: need, waveLoss: waveLoss, | |
| 557 summary: summary }; |
