// Made by tools/listings.mjs from stages/world-07.html, region 7-motion (pure). Do not edit.

// region: neighbours
// What bird i sees of the others within 40 m: count, the sum of the offsets
// to them (centre), the sum of their velocities (heading), and a push away
// from any closer than 6 m, harder the closer (push).
function neighbours(B, n, i) {
  const a = i * 8;
  const near = {
    count: 0,
    centre: [0, 0, 0],
    heading: [0, 0, 0],
    push: [0, 0, 0],
  };
  for (let j = 0; j < n; j++) {
    if (j === i) continue;
    const b = j * 8;
    const dx = B[b] - B[a];
    const dy = B[b + 1] - B[a + 1];
    const dz = B[b + 2] - B[a + 2];
    const d2 = dx * dx + dy * dy + dz * dz;
    if (d2 > 1600) continue;
    near.count++;
    near.centre[0] += dx;
    near.centre[1] += dy;
    near.centre[2] += dz;
    near.heading[0] += B[b + 3];
    near.heading[1] += B[b + 4];
    near.heading[2] += B[b + 5];
    if (d2 < 36) {
      const k = 1 / (d2 + 0.5);
      near.push[0] -= dx * k;
      near.push[1] -= dy * k;
      near.push[2] -= dz * k;
    }
  }
  return near;
}
// endregion: neighbours

// region: steerBird
// How one bird steers: its acceleration [x, y, z]. The bird's position is
// B[a], B[a + 1], B[a + 2] and its velocity the next three; near is what
// neighbours() saw, goal the point the flock follows. Three rules come from
// the neighbours: cohesion, alignment, separation. Two keep the flock in the
// picture: follow the goal, and clear the ground.
function steerBird(B, a, near, goal, H) {
  const x = B[a];
  const y = B[a + 1];
  const z = B[a + 2];
  // Follow the goal, more firmly in height.
  const acc = [
    (goal[0] - x) * 0.012,
    (goal[1] - y) * 0.02,
    (goal[2] - z) * 0.012,
  ];
  if (near.count) {
    const k = 1 / near.count;
    for (let c = 0; c < 3; c++) {
      // Toward the middle of the neighbours.
      const cohesion = near.centre[c] * k * 0.035;
      // Toward their average velocity.
      const alignment = (near.heading[c] * k - B[a + 3 + c]) * 0.3;
      // Away from any that crowd.
      const separation = near.push[c] * 14;
      acc[c] += cohesion + alignment + separation;
    }
  }
  // Climb over the ground three seconds ahead.
  const ahead = heightAt(H, x + B[a + 3] * 3, z + B[a + 5] * 3);
  const floor = Math.max(ahead, 0) + 14;
  if (y < floor) acc[1] += (floor - y) * 0.9;
  return acc;
}
// endregion: steerBird

// region: stepFlock
// One step of flocking for all n birds, in turn: each sees the others as they
// are now (earlier birds have already moved), steers, then moves.
function stepFlock(B, n, dt, t, home, H) {
  // The point the flock follows wanders over the bay.
  const gx = home[0] + Math.cos(t * 0.09) * 230 + Math.cos(t * 0.031) * 120;
  const gz = home[1] + Math.sin(t * 0.073) * 230;
  const gy = 55 + 28 * Math.sin(t * 0.05);
  const goal = [gx, gy, gz];
  for (let i = 0; i < n; i++) {
    const a = i * 8;
    const acc = steerBird(B, a, neighbours(B, n, i), goal, H);
    B[a + 3] += acc[0] * dt;
    B[a + 4] += acc[1] * dt;
    B[a + 5] += acc[2] * dt;
    B[a + 4] *= 1 - 0.6 * dt;
    // Keep the speed between 9 and 15 metres a second.
    const vv = B[a + 3] * B[a + 3] + B[a + 4] * B[a + 4] + B[a + 5] * B[a + 5];
    const sp = Math.sqrt(vv) + 1e-6;
    const want = clamp(sp, 9, 15) / sp;
    B[a + 3] *= want;
    B[a + 4] *= want;
    B[a + 5] *= want;
    B[a] += B[a + 3] * dt;
    B[a + 1] += B[a + 4] * dt;
    B[a + 2] += B[a + 5] * dt;
    // Flap faster when climbing, hold the wings when sinking.
    B[a + 6] += dt * (9 + 4 * B[a + 7]) * (B[a + 4] > -0.5 ? 1 : 0.25);
  }
  return B;
}
// endregion: stepFlock
