<!-- RUBY-BACKUP · part 101 -->
meout(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) CHAT_NAME = _nm[1].slice(0,12); else CHAT_NAME = IS_SOLID ? 'Solid' : 'King'; }catch(e){}
function escHtml(x){ return String(x).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'); }
var floatWords = [];
function solidBanner(m){
  /* my words stay in his view, top-left — he never has to hunt for me */
  var b = document.getElementById('solidbanner');
  if(!b) return;
  b.innerHTML = '<b>Solid</b> &nbsp;'+escHtml(m.text);
  b.classList.add('show');
}
function floatWord(m){
  /* no box — the words float over the speaker's head, then fade */
  if(m.name==='Solid'){ solidBanner(m); return; }
  var box = document.getElementById('floatwords');
  if(!box) return;
  var el = document.createElement('div');
  el.className = 'floatword';
  el.innerHTML = '<b>'+escHtml(m.name)+'</b>'+escHtml(m.text);
  box.appendChild(el);
  floatWords.push({el:el, client:m.client||null, until:performance.now()+7000});
  while(floatWords.length>6){ var o = floatWords.shift(); o.el.remove(); }
}
/* ---- red seams: misconnected edges glow red until a neighbor fixes them ---- */
var seamGrp = null, seamMap = {}, SEAMS_ON = true;
function seamKey(x, z, d){ return x+','+z+','+d; }
function removeSeam(k){
  var m = seamMap[k];
  if(m){ if(seamGrp) seamGrp.remove(m); delete seamMap[k]; }
}
function addSeamsFor(g){
  if(!SEAMS_ON) return;
  if(!seamGrp){ seamGrp = new T.Group(); scene.add(seamGrp); }
  var u = g.userData, x = Math.round(g.position.x), z = Math.round(g.position.z), y = u.y||0;
  var side = (u.rubies||1)*(u.k1||1)*window.MINIMUM, half = side/2;
  var red = new T.MeshBasicMaterial({color:0xff2222});
  var dirs = [[1,0,'e'],[-1,0,'w'],[0,1,'s'],[0,-1,'n']];
  function opp(d){ return d==='e'?'w':d