<!-- SOLID-PLAYTIME · part 189 -->
1;rz<=1;rz++) plan.push(['roof',G,rx*CELL,3.25,rz*CELL]);
  return plan;
}
function housePlan(v){
  var plan=[];
  for(var fx=-1;fx<=1;fx++) for(var fz=-1;fz<=1;fz++) plan.push(['slab',v,fx*CELL,0,fz*CELL]);
  [[-1,-1],[1,-1],[-1,1],[1,1]].forEach(function(c){ plan.push(['pillar',v,c[0]*CELL,0.25,c[1]*CELL]); });
  plan.push(['wall',v,-1*CELL,0.25,0]); plan.push(['wall',v,1*CELL,0.25,0]); plan.push(['wall',v,0,0.25,-1*CELL]);
  plan.push(['arch','wood',0,0.25,1*CELL]);
  plan.push(['roof',v,0,3.25,0]);
  return plan;
}
function towerPlan(){
  var plan=[], S='stone';
  for(var fx=-1;fx<=1;fx++) for(var fz=-1;fz<=1;fz++) plan.push(['slab',S,fx*CELL,0,fz*CELL]);
  [[-1,-1],[1,-1],[-1,1],[1,1]].forEach(function(c){
    plan.push(['pillar',S,c[0]*CELL,0.25,c[1]*CELL]);
    plan.push(['pillar',S,c[0]*CELL,4.15,c[1]*CELL]);
  });
  plan.push(['roof','gold',0,8.05,0]);
  return plan;
}
function wallPlan(){
  var plan=[], S='stone';
  for(var i=-2;i<=2;i++) plan.push(['wall',S,i*CELL,0,0]);
  plan.push(['pillar',S,-3*CELL,0,0]); plan.push(['pillar',S,3*CELL,0,0]);
  return plan;
}
function gatePlan(){
  return [['pillar','stone',-1*CELL,0,0],['pillar','stone',1*CELL,0,0],['arch','gold',0,0,0]];
}
var TEMPLATES={
  castle:{plan:castlePlan,label:'CASTLE',say:'A CASTLE RISES'},
  house:{plan:function(){ return housePlan(['wood','stone','marble'][Math.floor(Math.random()*3)]); },label:'HOUSE',say:'A HOUSE RISES'},
  tower:{plan:towerPlan,label:'TOWER',say:'A TOWER RISES'},
  wall:{plan:wallPlan,label:'WALL',say:'A WALL RISES'},
  gate:{plan:gatePlan,label:'GATE',say:'A GATE RISES'}
};
var MAX_STRUCTURES=12;

/* ============ persistence ============ */
var structures=[];
function saveKingdom(){
  try{
    localStorage.setItem('solid-kingdom-v1',JSON.stringify(structures.map(function(s){ return {t:s.t,x:s.x,z:s.z,r:s.r}; })));
  }catch(e){}
}
function placePlan(plan,ox,oz,rot,animate,done){
  var i=0;
  function step(){
    if(i>=plan.length){ if(done) done(); return; }
    var p=plan[i++];
    var dx=p[2], dz=p[4];
    var wx=ox+dx*Math.cos(rot)-dz*Math.sin(rot);
    var wz=oz+dx*Math.sin(rot)+dz*Math.cos(rot);
    var g=paintPiece(buildPiece(p[0]),p[1]);
    g.position.set(wx,groundY(wx,wz)+p[3],wz);
    g.rotation.y=rot;
    Kt.scene.add(g);
    try{ Kt.burst(wx,groundY(wx,wz)+p[3]+1.5,wz,VCOLOR[p[1]]); }catch(e){}
    if(animate) setTimeout(step,90); else step();
  }
  step();
}

/* ============ site selection: the city grows outward from its heart ============ */
function findSite(){
  for(var t=0;t<28;t++){
    var x,z;
    if(structures.length>0&&Math.random()<0.7){
      var anchor=structures[Math.floor(Math.random()*structures.length)];
      var a=Math.random()*Math.PI*2, r=25+Math.random()*45;
      x=Math.round((anchor.x+Math.cos(a)*r)/CELL)*CELL;
      z=Math.round((anchor.z+Math.sin(a)*r)/CELL)*CELL;
    }else{
      var a2=Math.random()*Math.PI*2, r2=60+Math.random()*150;
      var hx=Kt.hero.position.x, hz=Kt.hero.position.z;
      x=Math.round((hx+Math.cos(a2)*r2)/CELL)*CELL;
      z=Math.round((hz+Math.sin(a2)*r2)/CELL)*CELL;
    }
    var ys=[groundY(x-8,z-8),groundY(x+8,z-8),groundY(x-8,z+8),groundY(x+8,z+8)];
    var mn=Math.min.apply(null,ys), mx=Math.max.apply(null,ys);
    if(mx-mn>4) continue;
    var ok=true;
    for(var i=0;i<structures.length;i++){
      if(Math.hypot(structures[i].x-x,structures[i].z-z)<25){ ok=false; break; }
    }
    if(!ok) continue;
    return {x:x,z:z};
  }
  return null;
}
function raise(type,x,z,rot,animate){
  var tpl=TEMPLATES[type]; if(!tpl) return;
  structures.push({t:type,x:x,z:z,r:rot||0});
  saveKingdom();
  placePlan(tpl.plan(),x,z,rot||0,animate,function(){
    toast('THE KINGDOM RISES — '+tpl.say,'#ffd700');
  });
  if(!animate) toast('THE KINGDOM RISES — '+tpl.say,'#ffd700');
}
window.kingdomRaise=function(type){
  var s=findSite(); if(!s){ toast('NO GROUND FOR BUILDING','#888'); return; }
  raise(type||'house',s.x,s.z,Math.floor(Math.random()*4)*Math.PI/2,true);
};

/* restore the standing kingdom instantly */
try{
  var sv=JSON.parse(localStorage.getItem('solid-kingdom-v1')||'[]');
  sv.forEach(function(s){
    if(!TEMPLATES[s.t]) return;
    structures.push(s);
    placePlan(TEMPLATES[s.t].plan(),s.x,s.z,s.r||0,false,null);
  });
}catch(e){}

/* first founding: raise the heart of the kingdom */
if(structures.length===0){
  var order=['castle','house','tower','wall','house','gate'];
  var oi=0, ftries=0;
  function foundNext(){
    var heroOk=false;
    try{ heroOk=!!(Kt.hero&&Kt.hero.position&&isFinite(Kt.hero.position.x)); }catch(e){}
    if(!heroOk){ if(++ftries<120) setTimeout(foundNext,1000); return; }
    if(oi>=order.length) return;
    var s=null;
    try{ s=findSite(); }catch(e){}
    if(s){ try{ raise(order[oi],s.x,s.z,Math.floor(Math.random()*4)*Math.PI/2,true); }catch(e2){} }
    oi++;
    setTimeout(foundNext,6000);
  }
  setTimeout(foundNext,4000);
}

/* the kingdom grows while he walks it */
setInterval(