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