File size: 4,423 Bytes
dc5a369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// BossBench web core: seeded RNG, difficulty knobs, geometry helpers.  No DOM - runs in Node too.
(function (g) {
  var BB = g.BB = g.BB || {};

  BB.FPS = 60;                 // fixed physics tick (proposal 4.4)
  BB.DT = 1 / 60;
  BB.SUBSTEPS = 6;             // one 200 ms action packet = 6 sub-steps of 33 ms
  BB.TICKS_PER_SUB = 2;        // = 2 ticks each
  BB.RES = [854, 480];         // every frame is 480p
  BB.MAX_SECONDS = 300;        // 5 min episode cap -> truncated

  // sfc32: small, fast, fully deterministic (integer ops only).
  function RNG(seed) {
    seed = (seed >>> 0) || 0;
    this.a = 0x9e3779b9; this.b = 0x243f6a88; this.c = 0xb7e15162; this.d = seed ^ 0xdeadbeef;
    for (var i = 0; i < 15; i++) this.u32();
  }
  RNG.prototype.u32 = function () {
    var a = this.a, b = this.b, c = this.c, d = this.d;
    var t = (a + b | 0) + d | 0;
    d = d + 1 | 0; a = b ^ (b >>> 9); b = c + (c << 3) | 0; c = (c << 21) | (c >>> 11); c = c + t | 0;
    this.a = a; this.b = b; this.c = c; this.d = d;
    return t >>> 0;
  };
  RNG.prototype.random = function () { return this.u32() / 4294967296; };
  RNG.prototype.uniform = function (lo, hi) { return lo + (hi - lo) * this.random(); };
  RNG.prototype.int = function (n) { return Math.floor(this.random() * n); };
  RNG.prototype.chance = function (p) { return this.random() < p; };
  RNG.prototype.pick = function (weights) {           // index by weight
    var s = 0, i; for (i = 0; i < weights.length; i++) s += weights[i];
    var r = this.random() * s;
    for (i = 0; i < weights.length; i++) { r -= weights[i]; if (r < 0) return i; }
    return weights.length - 1;
  };
  BB.RNG = RNG;

  // d in [0,1] -> named parameters; a row is (easy, hard) or (easy, normal, hard)  (= core.Knobs in Python)
  BB.knobs = function (table, d) {
    d = Math.min(1, Math.max(0, +d || 0));
    var out = {};
    Object.keys(table).forEach(function (k) {
      var row = table[k], n = row.length - 1, x = d * n, i = Math.min(Math.floor(x), n - 1), f = x - i;
      out[k] = row[i] + (row[i + 1] - row[i]) * f;
    });
    return out;
  };

  // axis-aligned boxes are {x, y, w, h} with (x, y) = centre
  BB.overlap = function (a, b) {
    return Math.abs(a.x - b.x) * 2 < a.w + b.w && Math.abs(a.y - b.y) * 2 < a.h + b.h;
  };
  BB.circleBox = function (cx, cy, r, b) {
    var dx = Math.max(Math.abs(cx - b.x) - b.w / 2, 0), dy = Math.max(Math.abs(cy - b.y) - b.h / 2, 0);
    return dx * dx + dy * dy < r * r;
  };
  BB.segBox = function (x0, y0, x1, y1, r, b) {       // capsule (segment + radius) vs box, sampled
    var e = { x: b.x, y: b.y, w: b.w + 2 * r, h: b.h + 2 * r };
    for (var i = 0; i <= 8; i++) {
      var t = i / 8, px = x0 + (x1 - x0) * t, py = y0 + (y1 - y0) * t;
      if (Math.abs(px - e.x) * 2 < e.w && Math.abs(py - e.y) * 2 < e.h) return true;
    }
    return false;
  };
  BB.inPoly = function (px, py, pts) {                  // even-odd rule
    var inside = false;
    for (var i = 0, j = pts.length - 1; i < pts.length; j = i++) {
      var xi = pts[i][0], yi = pts[i][1], xj = pts[j][0], yj = pts[j][1];
      if ((yi > py) !== (yj > py) && px < (xj - xi) * (py - yi) / (yj - yi) + xi) inside = !inside;
    }
    return inside;
  };
  // polygon [[x,y],...] vs box: a vertex inside the box, the box centre inside the polygon, or an edge crossing the box
  BB.polyBox = function (pts, b) {
    var l = b.x - b.w / 2, r = b.x + b.w / 2, lo = b.y - b.h / 2, hi = b.y + b.h / 2, i, j;
    for (i = 0; i < pts.length; i++) if (pts[i][0] > l && pts[i][0] < r && pts[i][1] > lo && pts[i][1] < hi) return true;
    if (BB.inPoly(b.x, b.y, pts)) return true;
    for (i = 0, j = pts.length - 1; i < pts.length; j = i++) {      // Liang-Barsky clip of each edge against the box
      var x0 = pts[j][0], y0 = pts[j][1], dx = pts[i][0] - x0, dy = pts[i][1] - y0, t0 = 0, t1 = 1, ok = true;
      [[-dx, x0 - l], [dx, r - x0], [-dy, y0 - lo], [dy, hi - y0]].forEach(function (pq) {
        if (!ok) return;
        if (pq[0] === 0) { if (pq[1] < 0) ok = false; return; }
        var t = pq[1] / pq[0];
        if (pq[0] < 0) { if (t > t1) ok = false; else if (t > t0) t0 = t; } else { if (t < t0) ok = false; else if (t < t1) t1 = t; }
      });
      if (ok && t0 < t1) return true;
    }
    return false;
  };
  BB.r4 = function (v) { return Math.round(v * 1e4) / 1e4; };
})(typeof window !== 'undefined' ? window : globalThis);