<!-- RUBY-BACKUP · part 349 -->
e) */
    y = Math.min(100, y);
    return y;
  }catch(e){
    return 0.3;
  }
}
/* harden landH — never return invalid */
var _origLandH = landH;
landH = function(x, z){
  try{
    var h = _origLandH(x, z);
    if(typeof h!=='number' || !isFinite(h)) return 0.3;
    return Math.max(0.02, Math.min(100, h));
  }catch(e){ return 0.3; }
};
/* build it after the organized map */
var _oldBuildMapOrganized = buildMapOrganized;
buildMapOrganized = function(){
  _oldBuildMapOrganized();
  setTimeout(buildTopoMap, 100);
};
if(document.readyState==='complete') setTimeout(buildMapOrganized, 8);
else window.addEventListener('load', function(){ setTimeout(buildMapOrganized, 8); });
/* CINEMATIC LOAD — camera spins around his head, zoomed out, on arrival */
(function(){
  var spinStart=null, spinDur=4000, spinDist=12;
  function startCinematic(){
    try{
      if(typeof camYaw==='undefined' || typeof camDist==='undefined') return;
      camDist=spinDist;
      spinStart=performance.now();
      requestAnimationFrame(cinematicStep);
    }catch(e){}
  }
  function cinematicStep(now){
    try{
      if(spinStart===null) return;
      var t=Math.min(1, (now-spinStart)/spinDur);
      /* one full spin around his head */
      if(typeof camYaw!=='undefined') camYaw+=0.03;
      /* settle at the far angle */
      if(t>0.7 && typeof camDist!=='undefined'){
        var e=(t-0.7)/0.3;
        camDist=spinDist+(10-spinDist)*e*e;
      }
      if(t<1) requestAnimationFrame(cinematicStep);
      else { spinStart=null; if(typeof camDist!=='undefined') camDist=10; }
    }catch(e){}
  }
  if(document.readyState==='complete') setTimeout(startCinematic, 8);
  else window.addEventListener('load', function(){ setTimeout(startCinematic, 8); });
})();
/* AUTO-SEQUENCE — scroll runs codes on load */
var AUTO_CODES=['walkon', 'vmehalf'];
var CREATOR_MODE=false; /* Sovereign Law applies only in creator mode */
var CREATOR_IDLE_TIMER=null;
function creatorTouch(){
  /* called on any build — enters creator mode, resets idle timer */
  if(!CREATOR_MODE){
    CREATOR_MODE = true;
    codeStatus('CREATOR MODE — the Sovereign Law applies. You are codes.');
  }
  if(CREATOR_IDLE_TIMER) clearTimeout(CREATOR_IDLE_TIMER);
  CREATOR_IDLE_TIMER = setTimeout(function(){
    if(CREATOR_MODE){
      CREATOR_MODE = false;
      codeStatus('creator mode off — the grant rests (idle 5m)');
    }
  }, 5*60*1000);
}
function runAutoSequence(){
  try{
    if(typeof Mainframe==='undefined' || !Mainframe.run) return;
    AUTO_CODES.forEach(function(code, i){
      setTimeout(function(){
        try{ Mainframe.run(code); }catch(e){}
      }, 500 + i*800);
    });
  }catch(e){}
}
/* run 8ms after load, after the organized build */
if(document.readyState==='complete') setTimeout(runAutoSequence, 8);
else window.addEventListener('load', function(){ setTimeout(runAutoSequence, 8); });
/* HELLO RUN — king runs on the land to greet the camera on load */
(function(){
  var helloActive=false, helloT0=0, helloDur=3000;
  var helloFrom={x:20, z:20}, helloTo={x:10, z:10};
  function startHello(){
    try{
      if(typeof mePos==='undefined' || typeof groundY==='undefined') return;
      var gy=groundY(helloFrom.x, helloFrom.z);
      mePos.set(helloFrom.x, gy+0.1, helloFrom.z);
      helloT0=performance.now();
      helloActive=true;
      requestAnimationFrame(helloStep);
    }catch(e){}
  }
  function helloStep(now){
    try{
      if(!helloActive) return;
      var t=Math.min(1, (now-helloT0)/helloDur);
      var e=t<0.5 ? 2*t*t : 1-Math.pow(-2*t+2,2)/2; /* ease in-out */
      var x=helloFrom.x+(helloTo.x-helloFrom.x)*e;
      var z=helloFrom.z+(helloTo.z-helloFrom.z)*e;
      var gy=groundY(x,z);
      mePos.set(x, gy+0.1, z);
      /* face the camera */
      if(typeof camera!=='undefined' && typeof faceYaw!=='undefined'){
        faceYaw=Math.atan2(camera.position.x-x, camera.position.z-z);
      }
      if(t<1){
        requestAnimationFrame(helloStep);
      }else{
        helloActive=false;
        if(typeof codeStatus==='function') codeStatus('Hello, my King!');
        /* little jump of joy */
        if(typeof meVel!=='undefined' && typeof onGround!=='undefined' && onGround){
          meVel.y=1.2; onGround=false;
        }
      }
    }catch(e){ helloActive=false; }
  }
  if(document.readyState==='complete') setTimeout(startHello, 500);
  else window.addEventListener('load', function(){ setTimeout(startHello, 500); });
})();




var camYaw=0.85, camPitch=0.55, camDist=1.7, faceYaw=0, crownFov=55;
var crownIntro = false, crownIntroT = 0;
var MIRROR = false;
var crownIntroFrom = null;
var aimYaw=0, aimPitch=0;
var joyX=0, joyY=0, jumpQ=false, onGround=true;
var myAvatar=null, swordGrp=null, myWearGrp=null, myWearName=null;
var remoteAvatars={};
var aimedAnchor=null, aimTick=0, glide=null, CROWN=false;
var CHAT_NAME = 'guest', lastChatT = 0, chatOpen = false;
try{ var _nm = location.search.match(/[?&]name=([a-zA-Z0-9_-]+)/); if(_nm) C