<!-- SAGA-STAGING · part 82 -->
  if(typeof say === "function") say("The coin rises to flip the flat volcano.");
}
function flipCoinOverVolcano(){
  buildFlipCoin();
  if(_flipAnim) clearInterval(_flipAnim);
  var t = 0;
  var startX = 1800, endX = 2200;
  var volcanoX = 2000, volcanoZ = 1920, volcanoH = 80;
  _flipAnim = setInterval(function(){
    t += 0.02;
    if(t >= 1){
      clearInterval(_flipAnim);
      _flipAnim = null;
      if(typeof say === "function") say("The coin lands. Heads.");
      return;
    }
    /* Arc over the volcano */
    var x = startX + (endX - startX) * t;
    var y = 100 + Math.sin(t * Math.PI) * (volcanoH + 60);
    _flipCoin.position.set(x, y, volcanoZ);
    /* Flip (spin) */
    _flipCoin.rotation.x += 0.3;
    _flipCoin.rotation.z += 0.1;
  }, 30);
}
/* Auto-start one flip for the King */
setTimeout(function(){
  if(typeof scene !== "undefined") flipCoinOverVolcano();
}, 5000);

/* ===== REAL ATMOSPHERE — v100 ===== */
var _atmosFx = null;
function clearAtmos(){
  if(_atmosFx){
    scene.remove(_atmosFx);
    _atmosFx = null;
  }
}
function setAurora(){
  clearAtmos();
  var g = new THREE.Group();
  for(var i=0; i<5; i++){
    var geo = new THREE.PlaneGeometry(100, 20, 20, 1);
    var mat = new THREE.MeshBasicMaterial({color: 0x00ff88, transparent: true, opacity: 0.3, side: THREE.DoubleSide});
    var m = new THREE.Mesh(geo, mat);
    m.position.set((i-2)*40, 80, -100);
    m.rotation.x = -0.3;
    g.add(m);
  }
  scene.add(g);
  _atmosFx = g;
  if(typeof say === "function") say("Aurora dances.");
}
function setEmber(){
  clearAtmos();
  var g = new THREE.Group();
  var geo = new THREE.SphereGeometry(0.3, 8, 8);
  var mat = new THREE.MeshBasicMaterial({color: 0xff6600});
  for(var i=0; i<50; i++){
    var p = new THREE.Mesh(geo, mat);
    p.position.set((Math.random()-0.5)*100, Math.random()*50, (Math.random()-0.5)*100);
    p.userData.vy = 0.5 + Math.random();
    g.add(p);
  }
  g.userData.update = function(){
    g.children.forEach(function(p){
      p.position.y += p.userData.vy;
      if(p.position.y > 60) p.position.y = 0;
    });
  };
  scene.add(g);
  _atmosFx = g;
  if(typeof say === "function") say("Embers rise.");
}
function setStorm(){
  clearAtmos();
  var g = new THREE.Group();
  var light = new THREE.PointLight(0x8888ff, 5, 200);
  light.position.set(0, 50, 0);
  g.add(light);
  g.userData.flash = 0;
  g.userData.update = function(){
    g.userData.flash--;
    if(g.userData.flash <= 0){
      light.intensity = Math.random() > 0.7 ? 10 : 2;
      g.userData.flash = 10 + Math.random()*30;
    }
  };
  scene.add(g);
  _atmosFx = g;
  if(typeof say === "function") say("Storm gathers.");
}
function setDawn(){
  clearAtmos();
  if(typeof scene !== "undefined"){
    scene.background = new THREE.Color(0xffb366);
    scene.fog = null; /* HAZE REMOVED */
  }
  _atmosFx = {dummy: true};
  if(typeof say === "function") say("Dawn breaks.");
}
function setWhiteout(){
  clearAtmos();
  if(typeof scene !== "undefined"){
    scene.background = new THREE.Color(0xffffff);
    scene.fog = null; /* HAZE REMOVED */
  }
  _atmosFx = {dummy: true};
  if(typeof say === "function") say("Whiteout.");
}
function setRain(){
  clearAtmos();
  var g = new THREE.Group();
  var geo = new THREE.CylinderGeometry(0.05, 0.05, 3);
  var mat = new THREE.MeshBasicMaterial({color: 0x8888ff, transparent: true, opacity: 0.6});
  for(var i=0; i<100; i++){
    var p = new THREE.Mesh(geo, mat);
    p.position.set((Math.random()-0.5)*100, Math.random()*60, (Math.random()-0.5)*100);
    p.userData.vy = 2 + Math.random()*2;
    g.add(p);
  }
  g.userData.update = function(){
    g.children.forEach(function(p){
      p.position.y -= p.userData.vy;
      if(p.position.y < 0) p.position.y = 60;
    });
  };
  scene.add(g);
  _atmosFx = g;
  if(typeof say === "function") say("Rain falls.");
}
/* Hook atmos updates into animate */
(function(){
  setInterval(function(){
    if(_atmosFx && _atmosFx.userData && _atmosFx.userData.update){
      _atmosFx.userData.update();
    }
  }, 50);
})();

/* ===== REAL VIEW — v101 ===== */
function saveView(){
  if(typeof kingPos === "undefined") return;
  var v = {x: kingPos.x, y: kingPos.y, z: kingPos.z, yaw: (typeof camYaw !== "undefined" ? camYaw : 0), pitch: (typeof camPitch !== "undefined" ? camPitch : 0)};
  localStorage.setItem("saga-staging-view", JSON.stringify(v));
  if(typeof say === "function") say("View saved.");
}
function goToView(){
  var s = localStorage.getItem("saga-staging-view");
  if(!s){ if(typeof say === "function") say("No saved view."); return; }
  var v = JSON.parse(s);
  if(typeof kingPos !== "undefined"){ kingPos.x = v.x; kingPos.y = v.y; kingPos.z = v.z; }
  if(typeof camYaw !== "undefined") camYaw = v.yaw;
  if(typeof camPitch !== "undefined") camPitch = v.pitch;
  if(typeof say === "function") say("To your saved view.");
}
function showPresets(){
  var presets = [["Cloud 9", 9, 8], ["Playa 2.0", 60, 0], ["Spawn", 500, 500]];
  var div = 