//
// STIFT GÖTTWEIG – WUNDERKAMMER / BRUTAL (PRINTSAFE + RENDERFRIENDLY)
// -------------------------------------------------------------------
// Goals:
// - brutal/experimental look + still printable
// - avoid CSG normalization blow-up (staged booleans + render() barriers)
// - keep complexity (2 courtyards, church, towers, roofs, windows, arcades, buttresses, terrain, wall, stairs, forest)
// - forest uses CLUSTERS (not 1000 individual trees) -> render-friendly
//
// Usage:
// - F5 Preview: fast
// - F6 Render: should finish (much more stable than single giant boolean tree)
// - STL export after F6
//

// ======================================================
// 0) QUALITY / GLOBALS
// ======================================================
$fn = $preview ? 22 : 96;
eps = 0.01;

// ======================================================
// 1) USER CONTROLS
// ======================================================
target_width = 300;                 // mm: 240..340
mode = "brutal";                    // "brutal" or "clean-ish"
print_friendly = true;              // stepped domes, safer slopes
for_print = true;                   // engages safe limits (still detailed)

// DETAIL LEVELS (0..3)
detail_arch   = 3;                  // windows, arches, cornices, buttresses
detail_roof   = 3;                  // dormers, chimneys, tiles
detail_relief = 2;                  // shallow engravings (kept modest for print)
detail_wunder = 3;                  // brutal ops

// Surroundings
add_plateau = true;
add_retaining_walls = true;
add_stairs = true;
add_forest = true;

// Brutal intensities
cuts_strength     = (mode == "brutal") ? 1.8 : 0.0;
fracture_strength = (mode == "brutal") ? 1.2 : 0.0;
void_strength     = (mode == "brutal") ? 1.0 : 0.0;
spike_strength    = (mode == "brutal") ? 1.0 : 0.0;

// Keep courtyard readable
preserve_courtyard = true;

// ======================================================
// 2) MASTER PLAN (UNSCALED UNITS)
// ======================================================
outer_w = 285;
outer_d = 220;

base_margin = 160;
terrain_w = outer_w + base_margin;
terrain_d = outer_d + base_margin;

sf = target_width / terrain_w;

// ======================================================
// 3) BUILDING PARAMETERS
// ======================================================
base_th = 6;

wall_h = 28;
wing_t = 30;

roof_h = 10;
roof_overhang = 1.8;

court_w = outer_w - 2*wing_t;
court_d = outer_d - 2*wing_t;

// inner courtyard (klausur)
inner_w = court_w * 0.58;
inner_d = court_d * 0.50;
inner_t = wing_t * 0.74;
inner_h = wall_h * 0.88;

// Towers
tower_r = 14.0;
tower_h = 38;
dome_h  = 22;

// Corner pavilions
pav_r = 10.0;
pav_h = 28;

// Church parameters
church_nave_w = 58;
church_nave_d = 84;
church_h = 36;
church_roof_h = 11;

transept_w = 96;
transept_d = 32;

apse_r = 20;

front_w = 110;
front_d = 54;
front_h = 44;

front_tower_r = 10.8;
front_tower_h = 32;

// ======================================================
// 4) TOPOGRAPHY PARAMETERS
// ======================================================
plateau_shift_y = 22;
plateau_top_h = 36;
terrace_lip_h = 5.0;

plateau_w = terrain_w * 0.66;
plateau_d = terrain_d * 0.54;

cliff_angle = 14;

// ======================================================
// 5) DETAIL PARAMETERS (ARCH / ROOF / RELIEF)
// ======================================================
window_pitch = 10.6;
window_w = 4.2;         // print-safe
window_h = 11.0;
window_depth = 1.9;

arcade_pitch = 10.4;
arcade_w = 6.5;
arcade_h = 10.2;
arcade_depth = 2.2;
arcade_z = wall_h * 0.46;

cornice_h = 2.3;
cornice_out = 2.0;

buttress_w = 4.2;
buttress_d = 2.8;
buttress_h = wall_h * 0.70;

dormer_w = 12;
dormer_d = 10;
dormer_h = 9;

chimney_w = 4;
chimney_d = 4;
chimney_h = 10;

tile_pitch = 2.3;
tile_h = 0.55;

// ======================================================
// 6) WUNDERKAMMER PARAMETERS (PRINTSAFE + RENDERFRIENDLY)
// ======================================================
cut_planes   = (for_print ? ($preview ? 6 : 10) : ($preview ? 7 : 14));
cut_thick    = 2.8;
cut_slits    = (for_print ? ($preview ? 4 : 8)  : ($preview ? 5 : 12));

fracture_count = (for_print ? ($preview ? 6 : 12) : ($preview ? 8 : 22));
fracture_scale = 1.15;

void_tunnels = (for_print ? ($preview ? 1 : 3) : ($preview ? 2 : 7));
void_r = 6.0;

// Forest as clusters (NOT thousands of single trees)
forest_clusters = (for_print ? ($preview ? 16 : 40) : ($preview ? 20 : 60));
forest_per_cluster = (for_print ? ($preview ? 6 : 10) : ($preview ? 8 : 14));
forest_spread = (for_print ? ($preview ? 8 : 12) : ($preview ? 10 : 16));
forest_height = 3.2;
forest_radius = 1.8;       // a bit thicker, printable

// Spikes (fewer but thicker) -> printable, still brutal
spike_count = (for_print ? ($preview ? 60 : 160) : ($preview ? 120 : 420));
spike_h = (for_print ? 8 : 10);
spike_r = (for_print ? 1.6 : 1.1);

// ======================================================
// 7) UTILS / MATH
// ======================================================
function fract(x) = x - floor(x);
function hash1(x) = fract(sin(x*127.1 + 311.7) * 43758.5453123);
function clamp(x,a,b) = min(max(x,a),b);
function lerp(a,b,t) = a + (b-a)*t;
function normrad(x,y,rx,ry) = sqrt( (x/rx)*(x/rx) + (y/ry)*(y/ry) );

// ======================================================
// 8) BASIC SHAPES
// ======================================================
module rr2d(w,d,r){
  iw = max(0.01, w - 2*r);
  id = max(0.01, d - 2*r);
  offset(r=r) square([iw,id], center=true);
}

module rr3d(w,d,r,h){
  translate([0,0,h/2])
    linear_extrude(height=h, center=true)
      rr2d(w,d,r);
}

// ======================================================
// 9) ROOFS / DOMES
// ======================================================
module gable_roof(w=40, d=20, h=6){
  linear_extrude(height=d, center=true)
    polygon(points=[[-w/2,0],[0,h],[w/2,0]]);
}

module dome_print(r=10, h=16){
  steps = 10;
  for(i=[0:steps-1]){
    z1 = i*(h/steps);
    z2 = (i+1)*(h/steps);
    rr1 = r * (1 - 0.86*(i/steps));
    rr2 = r * (1 - 0.86*((i+1)/steps));
    translate([0,0,z1]) cylinder(r1=rr1, r2=rr2, h=(z2-z1));
  }
  translate([0,0,h]) cylinder(r=0.85, h=6);
}

module onion_dome(r=10, h=16){
  rotate_extrude(convexity=10)
    polygon(points=[
      [0,0],[r*0.18,0],[r*0.70,h*0.30],[r*0.35,h*0.60],[r*0.58,h*0.86],[0,h]
    ]);
  translate([0,0,h]) cylinder(r=0.85, h=7);
}

module dome(r=10,h=16){
  if(print_friendly) dome_print(r=r,h=h);
  else               onion_dome(r=r,h=h);
}

// ======================================================
// 10) ARCH DETAILS
// ======================================================
module window_slot(){
  cube([window_w, window_depth, window_h], center=true);
}

module arch_slot(){
  union(){
    cube([arcade_w, arcade_depth, arcade_h], center=true);
    translate([0,0,arcade_h/2])
      rotate([90,0,0])
        cylinder(r=arcade_w*0.50, h=arcade_depth, center=true);
  }
}

module cornice_band(w=100,d=20){
  difference(){
    cube([w, d+2*cornice_out, cornice_h], center=true);
    cube([w+eps, d, cornice_h+eps], center=true);
  }
}

module buttress_line(len=120, side="posY"){
  n = floor(len / (buttress_w*2.8));
  for(i=[-n/2:n/2]){
    x = i*(len/(n+1));
    if(side=="posY") translate([x,0,buttress_h/2]) cube([buttress_w, buttress_d, buttress_h], center=true);
    if(side=="negY") translate([x,0,buttress_h/2]) cube([buttress_w, buttress_d, buttress_h], center=true);
    if(side=="posX") translate([0,x,buttress_h/2]) cube([buttress_d, buttress_w, buttress_h], center=true);
    if(side=="negX") translate([0,x,buttress_h/2]) cube([buttress_d, buttress_w, buttress_h], center=true);
  }
}

// ======================================================
// 11) ROOF DETAILS
// ======================================================
module dormer(){
  union(){
    translate([0,0,dormer_h/2]) cube([dormer_w, dormer_d, dormer_h], center=true);
    translate([0,0,dormer_h + roof_h*0.35])
      gable_roof(w=dormer_w*1.05, d=dormer_d, h=roof_h*0.55);
  }
}

module chimney(){
  union(){
    translate([0,0,chimney_h/2]) cube([chimney_w, chimney_d, chimney_h], center=true);
    translate([0,0,chimney_h]) cylinder(r=chimney_w*0.30, h=2);
  }
}

module roof_tiles_area(w=100, d=20){
  if(detail_roof >= 3){
    for(i=[-floor(d/(tile_pitch*2)):floor(d/(tile_pitch*2))]){
      y = i*(tile_pitch*2);
      translate([0,y,0]) cube([w, tile_pitch, tile_h], center=true);
    }
  }
}

// ======================================================
// 12) TOWERS
// ======================================================
module tower_openings(r=10, h=26){
  if(detail_arch >= 2){
    for(k=[0:7]){
      ang = k*360/8;
      rotate([0,0,ang])
        translate([r*0.82,0,h*0.62])
          cube([2.0, 1.6, 10.0], center=true);
    }
  }
}

module tower_complex(pos=[0,0,0], r=10, h=26, dh=16){
  translate(pos){
    difference(){
      union(){
        cylinder(r=r*1.05, h=h*0.30);
        translate([0,0,h*0.30]) cylinder(r=r, h=h*0.55);
        translate([0,0,h*0.85]) cylinder(r=r*0.92, h=h*0.15);
        translate([0,0,h]) cylinder(r=r*0.55, h=3);
        translate([0,0,h+3]) dome(r=r*0.90, h=dh);
      }
      tower_openings(r=r, h=h);
    }

    if(detail_wunder >= 2){
      for(k=[0:15]){
        ang = k*360/16;
        rotate([0,0,ang])
          translate([r*0.90,0,h*0.90])
            cube([2.0, 3.0, 3.0], center=true);
      }
    }
  }
}

// ======================================================
// 13) WING MASS
// ======================================================
module wing_mass(w=100, d=22, h=18, roof=6, roof_dir="x"){
  union(){
    translate([0,0,h/2]) cube([w,d,h], center=true);

    if(detail_arch >= 2)
      translate([0,0,h - cornice_h*0.3]) cornice_band(w=w, d=d);

    translate([0,0,h + roof/2]){
      if(roof_dir=="x")
        rotate([0,90,0]) gable_roof(w=(d+roof_overhang)*1.08, d=w, h=roof);
      else
        gable_roof(w=w*1.00, d=(d+roof_overhang), h=roof);
    }

    if(detail_roof >= 3)
      translate([0,0,h + roof*0.35]) roof_tiles_area(w=w*0.96, d=d*1.05);

    if(detail_roof >= 2 && w > 70){
      for(i=[-2:2]){
        x = i*(w/6);
        translate([x, 0, h + roof*0.55]) dormer();
      }
    }

    if(detail_roof >= 2){
      translate([ w*0.22, d*0.20, h + roof*0.65]) chimney();
      translate([-w*0.18,-d*0.22, h + roof*0.60]) chimney();
    }
  }
}

// ======================================================
// 14) WINDOW / ARCADE LINES (CUT VOLUMES)
// ======================================================
module window_line(len=120, side="posY"){
  if(detail_arch >= 1){
    n = floor(len / window_pitch);
    for(i=[-n/2:n/2]){
      x = i*window_pitch;
      if(side=="posY") translate([x, 0, wall_h*0.56]) window_slot();
      if(side=="negY") translate([x, 0, wall_h*0.56]) window_slot();
      if(side=="posX") translate([0, x, wall_h*0.56]) rotate([0,0,90]) window_slot();
      if(side=="negX") translate([0, x, wall_h*0.56]) rotate([0,0,90]) window_slot();
    }
  }
}

module arcade_line(len=120, side="posY"){
  if(detail_arch >= 2){
    n = floor(len / arcade_pitch);
    for(i=[-n/2:n/2]){
      x = i*arcade_pitch;
      if(side=="posY") translate([x, 0, arcade_z]) arch_slot();
      if(side=="negY") translate([x, 0, arcade_z]) arch_slot();
      if(side=="posX") translate([0, x, arcade_z]) rotate([0,0,90]) arch_slot();
      if(side=="negX") translate([0, x, arcade_z]) rotate([0,0,90]) arch_slot();
    }
  }
}

// ======================================================
// 15) CHURCH (MASS + CUTS)
// ======================================================
module church_mass(){
  union(){
    translate([0,0,church_h/2]) cube([church_nave_w, church_nave_d, church_h], center=true);
    translate([0,0,church_h + church_roof_h/2]) gable_roof(w=church_nave_w*1.02, d=church_nave_d, h=church_roof_h);
    translate([0, church_nave_d*0.10, church_h*0.58]) cube([transept_w, transept_d, church_h*0.74], center=true);

    translate([0, church_nave_d/2 - apse_r*0.35, church_h*0.52])
      rotate([90,0,0]) cylinder(r=apse_r, h=apse_r*1.25, center=true);

    translate([0, -(church_nave_d/2 + front_d*0.30), front_h/2])
      cube([front_w, front_d, front_h], center=true);

    translate([0, -(church_nave_d/2 + front_d*0.10), front_h*0.26])
      cube([front_w*0.56, front_d*0.34, front_h*0.52], center=true);

    translate([-front_w*0.34, -(church_nave_d/2 + front_d*0.22), 0])
      tower_complex([0,0,0], r=front_tower_r, h=front_tower_h, dh=dome_h*0.82);
    translate([ front_w*0.34, -(church_nave_d/2 + front_d*0.22), 0])
      tower_complex([0,0,0], r=front_tower_r, h=front_tower_h, dh=dome_h*0.82);
  }
}

module church_cuts(){
  if(detail_arch >= 2){
    for(i=[-3:3]){
      x = i*(church_nave_w/7);
      translate([x, church_nave_d*0.48, church_h*0.55]) cube([4.0, 2.0, 12.0], center=true);
      translate([x,-church_nave_d*0.48, church_h*0.55]) cube([4.0, 2.0, 12.0], center=true);
    }
  }
}

// ======================================================
// 16) BRUTAL OPS (CUT VOLUMES) — SEPARATE
// ======================================================
module brutal_cut_planes(){
  if(detail_wunder >= 1 && cuts_strength > 0){
    for(k=[0:cut_planes-1]){
      a = 10 + k*(180/cut_planes);
      rotate([0,0,a])
        translate([0,0,wall_h*0.60])
          cube([outer_w*2.0, cut_thick*cuts_strength, wall_h*2.0], center=true);
    }
  }
}

module brutal_slits(){
  if(detail_wunder >= 2 && cuts_strength > 0){
    for(k=[0:cut_slits-1]){
      x = lerp(-outer_w*0.45, outer_w*0.45, k/(cut_slits-1));
      translate([x, 0, wall_h*0.62])
        cube([2.2*cuts_strength, outer_d*2.2, wall_h*1.6], center=true);
    }
  }
}

module brutal_fractures(){
  if(detail_wunder >= 2 && fracture_strength > 0){
    for(i=[0:fracture_count-1]){
      rx = (hash1(i*3.1)*2-1);
      ry = (hash1(i*7.7)*2-1);
      rz = (hash1(i*11.9)*2-1);

      x = rx * (outer_w*0.45);
      y = ry * (outer_d*0.45);
      z = (rz*0.35 + 0.55) * wall_h;

      sx = (10 + 26*hash1(i*13.1)) * fracture_scale * fracture_strength;
      sy = (10 + 30*hash1(i*17.7)) * fracture_scale * fracture_strength;
      sz = (10 + 30*hash1(i*19.9)) * fracture_scale * fracture_strength;

      ang = 360*hash1(i*29.3);

      if(!preserve_courtyard || (abs(x) > court_w*0.25 || abs(y) > court_d*0.25)){
        translate([x,y,z])
          rotate([0,0,ang])
            cube([sx,sy,sz], center=true);
      }
    }
  }
}

module brutal_void_tunnels(){
  if(detail_wunder >= 3 && void_strength > 0){
    for(i=[0:void_tunnels-1]){
      ang  = 360*hash1(i*41.7);
      tilt = 20 + 40*hash1(i*47.1);

      rloc = void_r * (0.8 + 0.6*hash1(i*53.7)) * void_strength;
      len  = outer_w*2.2;

      x = (hash1(i*59.3)*2-1) * outer_w*0.28;
      y = (hash1(i*61.1)*2-1) * outer_d*0.28;
      z = wall_h*(0.35 + 0.45*hash1(i*67.9));

      if(!preserve_courtyard || (abs(x) > court_w*0.22 || abs(y) > court_d*0.22)){
        translate([x,y,z])
          rotate([0,tilt,ang])
            cylinder(r=rloc, h=len, center=true);
      }
    }
  }
}

module brutal_spikes(){
  if(detail_wunder >= 2 && spike_strength > 0){
    for(i=[0:spike_count-1]){
      rx = (hash1(i*3.7)*2-1);
      ry = (hash1(i*9.1)*2-1);

      x = rx * (outer_w*0.52);
      y = ry * (outer_d*0.52);
      rad = normrad(x,y,outer_w*0.52, outer_d*0.52);

      if(rad > 0.55){
        h = spike_h * (0.5 + 0.95*hash1(i*21.3)) * spike_strength;
        r = spike_r * (0.7 + 0.70*hash1(i*33.9)) * spike_strength;

        z = base_th + wall_h + roof_h*0.40 + (2.0*hash1(i*55.5));
        translate([x,y,z])
          rotate([0, 20*hash1(i*72.1), 360*hash1(i*81.7)])
            cylinder(r1=r, r2=r*0.25, h=h);  // print-safe tip (not too sharp)
      }
    }
  }
}

// ======================================================
// 17) STIFT COMPLEX: MASS + CUTS (SPLIT + BARRIERS)
// ======================================================
module stift_complex_mass(){
  union(){
    // Outer ring wings
    translate([0,  (court_d/2 + wing_t/2), base_th])
      wing_mass(w=outer_w, d=wing_t, h=wall_h, roof=roof_h, roof_dir="x");
    translate([0, -(court_d/2 + wing_t/2), base_th])
      wing_mass(w=outer_w, d=wing_t, h=wall_h, roof=roof_h, roof_dir="x");

    translate([ (court_w/2 + wing_t/2), 0, base_th])
      wing_mass(w=wing_t, d=outer_d, h=wall_h, roof=roof_h, roof_dir="y");
    translate([-(court_w/2 + wing_t/2), 0, base_th])
      wing_mass(w=wing_t, d=outer_d, h=wall_h, roof=roof_h, roof_dir="y");

    // Big courtyard floor
    translate([0,0,base_th + 1.1])
      cube([court_w - 14, court_d - 14, 2.2], center=true);

    // Inner ring (klausur)
    translate([0,  (inner_d/2 + inner_t/2), base_th])
      wing_mass(w=inner_w, d=inner_t, h=inner_h, roof=roof_h*0.70, roof_dir="x");
    translate([0, -(inner_d/2 + inner_t/2), base_th])
      wing_mass(w=inner_w, d=inner_t, h=inner_h, roof=roof_h*0.70, roof_dir="x");

    translate([ (inner_w/2 + inner_t/2), 0, base_th])
      wing_mass(w=inner_t, d=inner_d, h=inner_h, roof=roof_h*0.70, roof_dir="y");
    translate([-(inner_w/2 + inner_t/2), 0, base_th])
      wing_mass(w=inner_t, d=inner_d, h=inner_h, roof=roof_h*0.70, roof_dir="y");

    // Small courtyard floor
    translate([0,0,base_th + 1.0])
      cube([inner_w - 22, inner_d - 22, 1.8], center=true);

    // 3 main towers
    tower_complex([-(outer_w/2 - wing_t*0.55), -(outer_d/2 - wing_t*0.55), base_th], r=tower_r, h=tower_h, dh=dome_h);
    tower_complex([ (outer_w/2 - wing_t*0.55),  (outer_d/2 - wing_t*0.55), base_th], r=tower_r, h=tower_h, dh=dome_h);
    tower_complex([0, (outer_d/2 - wing_t*0.60), base_th], r=tower_r*0.96, h=tower_h*0.96, dh=dome_h*0.96);

    // Corner pavilions + domes
    translate([-(outer_w*0.26),  (outer_d*0.18), base_th]) cylinder(r=pav_r, h=pav_h);
    translate([ (outer_w*0.26),  (outer_d*0.18), base_th]) cylinder(r=pav_r, h=pav_h);
    translate([-(outer_w*0.26), (outer_d*0.18), base_th + pav_h]) dome(r=pav_r*0.95, h=dome_h*0.60);
    translate([ (outer_w*0.26), (outer_d*0.18), base_th + pav_h]) dome(r=pav_r*0.95, h=dome_h*0.60);

    // Church integrated
    translate([0, -(outer_d/2 + church_nave_d*0.28), base_th])
      church_mass();

    // Optional big approach ramp
    if(add_stairs){
      translate([0, -(outer_d/2 + church_nave_d*0.66), base_th])
        rotate([90,0,0])
          linear_extrude(height=84, center=true)
            polygon(points=[[-52,0],[52,0],[36,20],[-36,20]]);
    }

    // Buttresses outside
    if(detail_arch >= 2){
      translate([0, (court_d/2 + wing_t/2) + wing_t*0.52, base_th])
        buttress_line(len=outer_w*0.90, side="posY");
      translate([0, -(court_d/2 + wing_t/2) - wing_t*0.52, base_th])
        buttress_line(len=outer_w*0.90, side="negY");
    }
  }
}

// ARCHITECTURE cuts only
module stift_arch_cuts(){
  union(){
    if(detail_arch >= 1){
      translate([0,  (court_d/2 + wing_t/2), base_th]) window_line(len=outer_w*0.92, side="posY");
      translate([0, -(court_d/2 + wing_t/2), base_th]) window_line(len=outer_w*0.92, side="negY");
      translate([ (court_w/2 + wing_t/2), 0, base_th]) window_line(len=outer_d*0.78, side="posX");
      translate([-(court_w/2 + wing_t/2), 0, base_th]) window_line(len=outer_d*0.78, side="negX");
    }

    if(detail_arch >= 2){
      translate([0,  (court_d/2), base_th]) translate([0, -wing_t*0.42, 0])
        arcade_line(len=outer_w*0.90, side="posY");
      translate([0, -(court_d/2), base_th]) translate([0,  wing_t*0.42, 0])
        arcade_line(len=outer_w*0.90, side="negY");

      translate([ (court_w/2), 0, base_th]) translate([-wing_t*0.42, 0, 0])
        arcade_line(len=outer_d*0.78, side="posX");
      translate([-(court_w/2), 0, base_th]) translate([ wing_t*0.42, 0, 0])
        arcade_line(len=outer_d*0.78, side="negX");

      // inner courtyard arcades
      translate([0,  (inner_d/2), base_th]) translate([0, -inner_t*0.42, 0])
        arcade_line(len=inner_w*0.86, side="posY");
      translate([0, -(inner_d/2), base_th]) translate([0,  inner_t*0.42, 0])
        arcade_line(len=inner_w*0.86, side="negY");
    }

    // church cuts
    translate([0, -(outer_d/2 + church_nave_d*0.28), base_th])
      church_cuts();
  }
}

// BRUTAL cuts only
module stift_brutal_cuts(){
  union(){
    brutal_cut_planes();
    brutal_slits();
    brutal_fractures();
    brutal_void_tunnels();
  }
}

// FINAL complex with staged booleans + render barriers
module stift_complex_final(){
  // Stage 1: architecture difference
  module stage_arch(){
    difference(){
      stift_complex_mass();
      stift_arch_cuts();
    }
  }

  // Stage 2: brutal difference (barrier on both)
  module stage_brutal(){
    if(mode == "brutal"){
      difference(){
        render(convexity=10) stage_arch();
        render(convexity=10) stift_brutal_cuts();
      }
    } else {
      render(convexity=10) stage_arch();
    }
  }

  stage_brutal();

  // Additive spikes outside booleans
  if(mode == "brutal") brutal_spikes();
}

// ======================================================
// 18) TOPOGRAPHY / PLATEAU / WALLS / STAIRS
// ======================================================
module plateau_terrain(){
  difference(){
    union(){
      hull(){
        rr3d(terrain_w, terrain_d, 14, 3);
        translate([0, plateau_shift_y*0.65, plateau_top_h*0.55])
          rr3d(plateau_w*1.10, plateau_d*1.08, 14, 10);
        translate([0, plateau_shift_y, plateau_top_h])
          rr3d(plateau_w, plateau_d, 14, 16);
      }
      translate([0, plateau_shift_y, plateau_top_h + terrace_lip_h/2])
        rr3d(plateau_w*1.03, plateau_d*1.03, 14, terrace_lip_h);
    }

    translate([0, -(terrain_d*0.28), plateau_top_h*0.55])
      rotate([cliff_angle,0,0])
        cube([terrain_w*1.6, terrain_d*0.95, plateau_top_h*2.4], center=true);
  }
}

module retaining_wall(){
  if(add_retaining_walls){
    translate([0, -(terrain_d*0.12), plateau_top_h*0.42])
      rotate([cliff_angle,0,0])
        cube([plateau_w*1.08, 8.0, plateau_top_h*0.80], center=true);
  }
}

module topo_stairs(){
  if(add_stairs){
    translate([0, -(terrain_d*0.20), 3.0])
      rotate([10,0,0])
        linear_extrude(height=80, center=true)
          polygon(points=[[-42,0],[42,0],[30,18],[-30,18]]);
  }
}

// ======================================================
// 19) FOREST (CLUSTERS: RENDER-FRIENDLY)
// ======================================================
module forest_cluster(cx, cy, n=10, spread=12){
  for(j=[0:n-1]){
    a = 360*hash1((cx+cy)*0.001 + j*7.13);
    rr = spread*(0.2 + 0.8*hash1((cx+cy)*0.002 + j*13.7));
    x = cx + rr*cos(a);
    y = cy + rr*sin(a);

    h = forest_height*(0.65 + 0.75*hash1(j*19.9 + cx*0.01));
    r = forest_radius*(0.70 + 0.70*hash1(j*23.3 + cy*0.01));

    // printable tip (not needle)
    translate([x,y,2.0]) cylinder(r1=r, r2=r*0.22, h=h);
  }
}

module forest_texture(){
  if(add_forest){
    for(i=[0:forest_clusters-1]){
      rx = (hash1(i*3.1)*2 - 1);
      ry = (hash1(i*7.7)*2 - 1);

      x = rx * (terrain_w*0.48);
      y = ry * (terrain_d*0.40);

      rad = normrad(x,y,terrain_w*0.50, terrain_d*0.42);

      // forest ring outside plateau
      if(rad > 0.60){
        forest_cluster(x,y,n=forest_per_cluster,spread=forest_spread);
      }
    }
  }
}

// ======================================================
// 20) FINAL ASSEMBLY
// ======================================================
module scene(){
  union(){
    if(add_plateau){
      render(convexity=10) plateau_terrain(); // barrier: terrain baked
      retaining_wall();
      topo_stairs();
    } else {
      rr3d(terrain_w, terrain_d, 10, 4);
    }

    // forest outside booleans
    forest_texture();

    // Place Stift on plateau top
    translate([0, plateau_shift_y, plateau_top_h + terrace_lip_h + 0.2])
      stift_complex_final();
  }
}

scale(sf) scene();
